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