front.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import streamlit as st
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import sys
  5. ### don't do it this way!
  6. import os
  7. absolute_path = os.path.abspath(__file__)
  8. # print("Full path: " + absolute_path)
  9. # print("Directory Path: " + os.path.dirname(absolute_path))
  10. # adding /backend to use its functions here
  11. sys.path.append("/".join(os.path.dirname(absolute_path).split('/')[:-1]))
  12. # print("/".join(os.path.dirname(absolute_path).split('/')[:-1]))
  13. from backend.calc import *
  14. def plot_data(r,i, g):
  15. # unit circle
  16. unit_circle_x = []
  17. unit_circle_y = []
  18. for x in np.arange(-1, 1, 0.01):
  19. unit_circle_x.append(x)
  20. unit_circle_y.append((1-x**2)**0.5)
  21. unit_circle_x.append(1)
  22. unit_circle_y.append(0)
  23. for x in np.arange(-1, 1, 0.01)[::-1]:
  24. unit_circle_x.append(x)
  25. unit_circle_y.append(-(1-x**2)**0.5)
  26. fig, ax = plt.subplots()
  27. ax.plot(unit_circle_x, unit_circle_y)
  28. #
  29. # data
  30. ax.plot(r, i, 'b+')
  31. #
  32. #cirlce approximation
  33. t=np.linspace(0,1,100)
  34. z = (g[0]*t+g[1])/(g[2]+1)
  35. ax.plot(z.real,z.imag)
  36. #
  37. ax.grid(True)
  38. ax.axis('square')
  39. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  40. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  41. st.pyplot(fig)
  42. # ../../resource/data/1_M450.MEA
  43. # with open("/".join(os.path.dirname(absolute_path).split('/')[:-2]) + "/resource/data/1_M450.MEA") as f:
  44. # row = f.readlines()
  45. # f, r, i = [], [], []
  46. # for x in row:
  47. # a, b, c = (float(y) for y in x.split())
  48. # f.append(a) # frequency
  49. # r.append(b) # Re of something
  50. # i.append(c) # Im of something
  51. # plot_data(r,i)
  52. ### move all that into to a function
  53. data = []
  54. uploaded_file = st.file_uploader('Upload a csv')
  55. if uploaded_file is not None:
  56. data = uploaded_file.readlines()
  57. col1, col2 = st.columns(2)
  58. select_data_format = col1.selectbox('Choose data format from a list',['Frequency, Re(S11), Im(S11)','Frequency, Re(Zin), Im(Zin)'])
  59. select_separator = col2.selectbox('Choose separator',['","' ,'" "','";"'])
  60. def unpack_data(data):
  61. f, r, i = [], [], []
  62. for x in data:
  63. a, b, c = (float(y) for y in x.split())
  64. f.append(a) # frequency
  65. r.append(b) # Re of S11
  66. i.append(c) # Im of S11
  67. return f, r, i, 'very nice'
  68. validator_status = 'nice'
  69. # calculate
  70. circle_params=[]
  71. if len(data) > 0:
  72. f,r,i,validator_status = unpack_data(data)
  73. Q0,sigmaQ0,QL,sigmaQl, circle_params =fl_fitting(f,r,i)
  74. st.write("Cable attenuation")
  75. st.write(f"Q0 = {Q0} +- {sigmaQ0}, epsilon Q0 ={sigmaQ0/Q0}")
  76. st.write(f"QL = {QL} +- {sigmaQl}, epsilon QL ={sigmaQl/QL}")
  77. st.write("Status: " +validator_status)
  78. if len(data) > 0:
  79. f,r,i,validator_status = unpack_data(data)
  80. plot_data(r,i,circle_params)