front.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import streamlit as st
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. def plot_data(r,i, g):
  5. # unit circle
  6. unit_circle_x = []
  7. unit_circle_y = []
  8. for x in np.arange(-1, 1, 0.01):
  9. unit_circle_x.append(x)
  10. unit_circle_y.append((1-x**2)**0.5)
  11. unit_circle_x.append(1)
  12. unit_circle_y.append(0)
  13. for x in np.arange(-1, 1, 0.01)[::-1]:
  14. unit_circle_x.append(x)
  15. unit_circle_y.append(-(1-x**2)**0.5)
  16. fig, ax = plt.subplots()
  17. ax.plot(unit_circle_x, unit_circle_y)
  18. #
  19. # data
  20. ax.plot(r, i, 'b+')
  21. #
  22. #cirlce approximation
  23. t=np.linspace(0,1,100)
  24. z = (g[0]*t+g[1])/(g[2]+1)
  25. ax.plot(z.real,z.imag)
  26. #
  27. ax.grid(True)
  28. ax.axis('square')
  29. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  30. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  31. st.pyplot(fig)
  32. def run(calc_function):
  33. data = []
  34. uploaded_file = st.file_uploader('Upload a csv')
  35. if uploaded_file is not None:
  36. data = uploaded_file.readlines()
  37. col1, col2 = st.columns(2)
  38. select_data_format = col1.selectbox('Choose data format from a list',['Frequency, Re(S11), Im(S11)','Frequency, Re(Zin), Im(Zin)'])
  39. select_separator = col2.selectbox('Choose separator',['","' ,'" "','";"'])
  40. def unpack_data(data):
  41. f, r, i = [], [], []
  42. for x in data:
  43. a, b, c = (float(y) for y in x.split())
  44. f.append(a) # frequency
  45. r.append(b) # Re of S11
  46. i.append(c) # Im of S11
  47. return f, r, i, 'very nice'
  48. validator_status = 'nice'
  49. # calculate
  50. circle_params=[]
  51. if len(data) > 0:
  52. f,r,i,validator_status = unpack_data(data)
  53. Q0,sigmaQ0,QL,sigmaQl, circle_params =calc_function(f,r,i)
  54. st.write("Cable attenuation")
  55. st.write(f"Q0 = {Q0} +- {sigmaQ0}, epsilon Q0 ={sigmaQ0/Q0}")
  56. st.write(f"QL = {QL} +- {sigmaQl}, epsilon QL ={sigmaQl/QL}")
  57. st.write("Status: " +validator_status)
  58. if len(data) > 0:
  59. f,r,i,validator_status = unpack_data(data)
  60. plot_data(r,i,circle_params)