front.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import math
  2. XLIM = [-1.1, 1.1]
  3. YLIM = [-1.1, 1.1]
  4. def round_up(x, n=7):
  5. if x == 0:
  6. return 0
  7. deg = math.floor(math.log(abs(x), 10))
  8. return (10 ** deg) * round(x / (10 ** deg), n - 1)
  9. def circle(ax, x, y, radius, color='#1946BA'):
  10. from matplotlib.patches import Ellipse
  11. circle = Ellipse((x, y), radius * 2, radius * 2, clip_on=False,
  12. zorder=2, linewidth=2, edgecolor=color, facecolor=(0, 0, 0, .0125))
  13. ax.add_artist(circle)
  14. import streamlit as st
  15. import matplotlib.pyplot as plt
  16. import numpy as np
  17. def plot_data(r, i, g):
  18. fig = plt.figure(figsize=(10, 10))
  19. ax = fig.add_subplot()
  20. ax.set_xlim(XLIM)
  21. ax.set_ylim(YLIM)
  22. major_ticks = np.arange(-1.0, 1.1, 0.25)
  23. minor_ticks = np.arange(-1.1, 1.1, 0.05)
  24. ax.set_xticks(major_ticks)
  25. ax.set_xticks(minor_ticks, minor=True)
  26. ax.set_yticks(major_ticks)
  27. ax.set_yticks(minor_ticks, minor=True)
  28. ax.grid(which='major', color='grey', linewidth=1.5)
  29. ax.grid(which='minor', color='grey', linewidth=0.5, linestyle=':')
  30. plt.xlabel(r'$Re(\Gamma)$', color='gray', fontsize=16, fontname="Cambria")
  31. plt.ylabel('$Im(\Gamma)$', color='gray', fontsize=16, fontname="Cambria")
  32. plt.title('Smith chart', fontsize=24, fontname="Cambria")
  33. # cirlce approximation
  34. radius = abs(g[1] - g[0] / g[2]) / 2
  35. x = ((g[1] + g[0] / g[2]) / 2).real
  36. y = ((g[1] + g[0] / g[2]) / 2).imag
  37. circle(ax, x, y, radius, color='#FF8400')
  38. #
  39. # unit circle
  40. circle(ax, 0, 0, 1)
  41. #
  42. # data
  43. ax.plot(r, i, 'b+', ms=10, mew=2, color='#1946BA')
  44. #
  45. st.pyplot(fig)
  46. def run(calc_function):
  47. data = []
  48. uploaded_file = st.file_uploader('Upload a csv')
  49. if uploaded_file is not None:
  50. data = uploaded_file.readlines()
  51. col1, col2 = st.columns(2)
  52. select_data_format = col1.selectbox('Choose data format from a list',
  53. ['Frequency, Re(S11), Im(S11)', 'Frequency, Re(Zin), Im(Zin)'])
  54. select_separator = col2.selectbox('Choose separator', ['","', '" "', '";"'])
  55. def unpack_data(data):
  56. f, r, i = [], [], []
  57. for x in data:
  58. a, b, c = (float(y) for y in x.split())
  59. f.append(a) # frequency
  60. r.append(b) # Re of S11
  61. i.append(c) # Im of S11
  62. return f, r, i, 'very nice'
  63. validator_status = 'nice'
  64. # calculate
  65. circle_params = []
  66. if len(data) > 0:
  67. f, r, i, validator_status = unpack_data(data)
  68. Q0, sigmaQ0, QL, sigmaQl, circle_params = calc_function(f, r, i)
  69. Q0 = round_up(Q0)
  70. sigmaQ0= round_up(sigmaQ0)
  71. QL = round_up(QL)
  72. sigmaQl = round_up(sigmaQl)
  73. st.write("Cable attenuation")
  74. st.latex(r'Q_0 =' + f'{Q0} \pm {sigmaQ0}, ' + r'\;\;\varepsilon_{Q_0} =' + f'{round_up(sigmaQ0 / Q0)}')
  75. st.latex(r'Q_L =' + f'{QL} \pm {sigmaQl}, ' + r'\;\;\varepsilon_{Q_L} =' + f'{round_up(sigmaQl / QL)}')
  76. st.write("Status: " + validator_status)
  77. if len(data) > 0:
  78. f, r, i, validator_status = unpack_data(data)
  79. plot_data(r, i, circle_params)