front.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import streamlit as st
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import math
  5. def plot_data(r,i, g):
  6. # unit circle
  7. unit_circle_x = []
  8. unit_circle_y = []
  9. for x in np.arange(-1, 1, 0.01):
  10. unit_circle_x.append(x)
  11. unit_circle_y.append((1-x**2)**0.5)
  12. unit_circle_x.append(1)
  13. unit_circle_y.append(0)
  14. for x in np.arange(-1, 1, 0.01)[::-1]:
  15. unit_circle_x.append(x)
  16. unit_circle_y.append(-(1-x**2)**0.5)
  17. fig, ax = plt.subplots()
  18. ax.plot(unit_circle_x, unit_circle_y)
  19. #
  20. # data
  21. ax.plot(r, i, 'b+')
  22. #
  23. ax.xlabel("x")
  24. ax.ylabel("y")
  25. #cirlce approximation
  26. t=np.linspace(0,1,100)
  27. z = (g[0]*t+g[1])/(g[2]+1)
  28. ax.plot(z.real,z.imag)
  29. #
  30. ax.grid(True)
  31. ax.axis('square')
  32. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  33. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  34. st.pyplot(fig)
  35. def run(calc_function):
  36. data = []
  37. uploaded_file = st.file_uploader('Upload a csv')
  38. if uploaded_file is not None:
  39. data = uploaded_file.readlines()
  40. col1, col2 = st.columns(2)
  41. select_data_format = col1.selectbox('Choose data format from a list',['Frequency, Re(S11), Im(S11)','Frequency, Re(Zin), Im(Zin)'])
  42. select_separator = col2.selectbox('Choose separator',['","' ,'" "','";"'])
  43. select_coupling_losses = st.checkbox('I want to apply corrections for coupling losses (lossy coupling)')
  44. def is_float(element) -> bool:
  45. try:
  46. float(element)
  47. val = float(element)
  48. if math.isnan(val) or math.isinf(val):
  49. raise ValueError
  50. return True
  51. except ValueError:
  52. return False
  53. def unpack_data(data):
  54. f, r, i = [], [], []
  55. if select_data_format == 'Frequency, Re(S11), Im(S11)':
  56. for x in range(len(data)):
  57. tru = data[x].split(select_separator)
  58. if len(tru)!=3:
  59. return f, r, i, 'Bad line in your file. №:' + str(x)
  60. a, b, c = (y for y in tru)
  61. if not ((is_float(a)) or (is_float(b)) or (is_float(c))):
  62. return f, r, i, 'Bad data. Your data isnt numerical type. Number of bad line:' + str(x)
  63. f.append(a) # frequency
  64. r.append(b) # Re of S11
  65. i.append(c) # Im of S11
  66. else:
  67. return f, r, i, 'Bad data format'
  68. return f, r, i, 'very nice'
  69. validator_status = 'nice'
  70. # calculate
  71. circle_params=[]
  72. if len(data) > 0:
  73. f,r,i,validator_status = unpack_data(data)
  74. Q0,sigmaQ0,QL,sigmaQl, circle_params =calc_function(f,r,i)
  75. st.write("Cable attenuation")
  76. st.write(f"Q0 = {Q0} +- {sigmaQ0}, epsilon Q0 ={sigmaQ0/Q0}")
  77. st.write(f"QL = {QL} +- {sigmaQl}, epsilon QL ={sigmaQl/QL}")
  78. st.write("Status: " +validator_status)
  79. if len(data) > 0:
  80. f,r,i,validator_status = unpack_data(data)
  81. plot_data(r,i,circle_params)