front.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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):
  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)
  30. #
  31. ax.grid(True)
  32. ax.axis('square')
  33. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  34. ax.set_yticks(np.arange(-1, 1.2, 0.2))
  35. st.pyplot(fig)
  36. # ../../resource/data/1_M450.MEA
  37. # with open("/".join(os.path.dirname(absolute_path).split('/')[:-2]) + "/resource/data/1_M450.MEA") as f:
  38. # row = f.readlines()
  39. # f, r, i = [], [], []
  40. # for x in row:
  41. # a, b, c = (float(y) for y in x.split())
  42. # f.append(a) # frequency
  43. # r.append(b) # Re of something
  44. # i.append(c) # Im of something
  45. # plot_data(r,i)
  46. data = []
  47. uploaded_file = st.file_uploader('Upload a csv')
  48. if uploaded_file is not None:
  49. data = uploaded_file.readlines()
  50. col1, col2 = st.columns(2)
  51. select_data_format = col1.selectbox('Choose data format from a list',['Frequency, Re(S11), Im(S11)','Frequency, Re(Zin), Im(Zin)'])
  52. select_separator = col2.selectbox('Choose separator',['","' ,'" "','";"'])
  53. def unpack_data(data):
  54. f, r, i = [], [], []
  55. for x in data:
  56. a, b, c = (float(y) for y in x.split())
  57. f.append(a) # frequency
  58. r.append(b) # Re of something
  59. i.append(c) # Im of something
  60. return f, r, i, 'very nice'
  61. validator_status = 'nice'
  62. # calculate
  63. if len(data) > 0:
  64. f,r,i,validator_status = unpack_data(data)
  65. calc_data = prepare_data(f, r, i)
  66. a = solution(calc_data)
  67. Q = q_factor(a)
  68. st.write(Q)
  69. st.write(validator_status)
  70. if len(data) > 0:
  71. f,r,i,validator_status = unpack_data(data)
  72. plot_data(r,i)