front.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #cirlce approximation
  24. # g_c = (np.conj(g[2])*g[1]-g[0])/(np.conj(g[2])-g[2])
  25. # g_d = g[0]/g[2]
  26. # radius = abs(g_c-g_d)
  27. # p=[[],[]]
  28. # for alpha in range(0,360):
  29. # p[0].append(g_c.real+radius*math.cos(math.radians(alpha)))
  30. # p[1].append(g_c.imag+radius*math.sin(math.radians(alpha)))
  31. # ax.plot(p[0],p[1])
  32. #
  33. ax.grid(True)
  34. ax.axis('square')
  35. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  36. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  37. st.pyplot(fig)
  38. def run(calc_function):
  39. data = []
  40. uploaded_file = st.file_uploader('Upload a csv')
  41. if uploaded_file is not None:
  42. data = uploaded_file.readlines()
  43. col1, col2 = st.columns(2)
  44. select_data_format = col1.selectbox('Choose data format from a list',['Frequency, Re(S11), Im(S11)','Frequency, Re(Zin), Im(Zin)'])
  45. select_separator = col2.selectbox('Choose separator',['" "' ,'","','";"'])
  46. select_coupling_losses = st.checkbox('I want to apply corrections for coupling losses (lossy coupling)')
  47. def is_float(element) -> bool:
  48. try:
  49. float(element)
  50. val = float(element)
  51. if math.isnan(val) or math.isinf(val):
  52. raise ValueError
  53. return True
  54. except ValueError:
  55. return False
  56. def unpack_data(data):
  57. nonlocal select_separator
  58. nonlocal select_data_format
  59. f, r, i = [], [], []
  60. if select_data_format == 'Frequency, Re(S11), Im(S11)':
  61. for x in range(len(data)):
  62. # print(select_separator)
  63. select_separator=select_separator.replace('\"','')
  64. if select_separator==" ":
  65. tru = data[x].split()
  66. else:
  67. data[x]=data[x].replace(select_separator,' ')
  68. tru = data[x].split()
  69. if len(tru)!=3:
  70. return f, r, i, 'Bad line in your file. №:' + str(x)
  71. a, b, c = (y for y in tru)
  72. if not ((is_float(a)) or (is_float(b)) or (is_float(c))):
  73. return f, r, i, 'Bad data. Your data isnt numerical type. Number of bad line:' + str(x)
  74. f.append(float(a)) # frequency
  75. r.append(float(b)) # Re of S11
  76. i.append(float(c)) # Im of S11
  77. else:
  78. return f, r, i, 'Bad data format'
  79. return f, r, i, 'very nice'
  80. validator_status = 'nice'
  81. # calculate
  82. circle_params=[]
  83. if len(data) > 0:
  84. f,r,i,validator_status = unpack_data(data)
  85. Q0,sigmaQ0,QL,sigmaQl, circle_params =calc_function(f,r,i)
  86. st.write("Cable attenuation")
  87. st.write(f"Q0 = {Q0} +- {sigmaQ0}, epsilon Q0 ={sigmaQ0/Q0}")
  88. st.write(f"QL = {QL} +- {sigmaQl}, epsilon QL ={sigmaQl/QL}")
  89. st.write("Status: " +validator_status)
  90. if len(data) > 0:
  91. f,r,i,validator_status = unpack_data(data)
  92. if validator_status == 'very nice':
  93. plot_data(r,i,circle_params)