Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -122,30 +122,28 @@ def compute_alternate_low_expr(betas, z_a, y):
|
|
122 |
betas = np.array(betas)
|
123 |
return (z_a * y * betas * (z_a - 1) - 2*z_a*(1 - y) - 2*z_a**2) / (2 + 2*z_a)
|
124 |
|
125 |
-
def compute_custom_expression(betas, z_a, y,
|
126 |
"""
|
127 |
-
Compute a custom curve
|
128 |
-
|
129 |
-
Allowed variables: z_a, beta, y, s.
|
130 |
-
Also, 'a' is accepted as an alias for z_a.
|
131 |
"""
|
132 |
-
beta_sym, z_a_sym, y_sym
|
133 |
-
local_dict = {"beta": beta_sym, "z_a": z_a_sym, "y": y_sym
|
134 |
try:
|
135 |
-
num_expr = sp.sympify(
|
136 |
-
denom_expr = sp.sympify(
|
|
|
137 |
except sp.SympifyError as e:
|
138 |
st.error(f"Error parsing expressions: {e}")
|
139 |
return np.full_like(betas, np.nan)
|
140 |
|
141 |
-
|
142 |
-
denom_func = sp.lambdify((beta_sym, z_a_sym, y_sym, s_sym), denom_expr, modules=["numpy"])
|
143 |
with np.errstate(divide='ignore', invalid='ignore'):
|
144 |
-
result =
|
145 |
return result
|
146 |
|
147 |
def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
|
148 |
-
|
149 |
if z_a <= 0 or y <= 0 or z_min >= z_max:
|
150 |
st.error("Invalid input parameters.")
|
151 |
return None
|
@@ -158,23 +156,23 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
|
|
158 |
|
159 |
fig = go.Figure()
|
160 |
fig.add_trace(go.Scatter(x=betas, y=z_maxs, mode="markers+lines", name="Upper z*(β)",
|
161 |
-
|
162 |
fig.add_trace(go.Scatter(x=betas, y=z_mins, mode="markers+lines", name="Lower z*(β)",
|
163 |
-
|
164 |
fig.add_trace(go.Scatter(x=betas, y=low_y_curve, mode="markers+lines", name="Low y Expression",
|
165 |
-
|
166 |
fig.add_trace(go.Scatter(x=betas, y=high_y_curve, mode="markers+lines", name="High y Expression",
|
167 |
-
|
168 |
fig.add_trace(go.Scatter(x=betas, y=alt_low_expr, mode="markers+lines", name="Alternate Low Expression",
|
169 |
-
|
170 |
|
171 |
-
if
|
172 |
-
custom_curve = compute_custom_expression(betas, z_a, y,
|
173 |
-
fig.add_trace(go.Scatter(x=betas, y=custom_curve, mode="markers+lines", name="Custom Expression",
|
174 |
-
|
175 |
|
176 |
fig.update_layout(title="Curves vs β: z*(β) Boundaries and Asymptotic Expressions",
|
177 |
-
|
178 |
return fig
|
179 |
|
180 |
def compute_cubic_roots(z, beta, z_a, y):
|
@@ -299,39 +297,18 @@ with tab1:
|
|
299 |
with st.expander("Resolution Settings"):
|
300 |
beta_steps = st.slider("β steps", min_value=51, max_value=501, value=201, step=50, key="beta_steps")
|
301 |
z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000, step=1000, key="z_steps")
|
302 |
-
|
303 |
-
st.
|
304 |
-
st.markdown("
|
305 |
-
st.
|
306 |
-
|
307 |
-
|
308 |
-
custom_num_expr = st.text_input("Numerator Expression", value=default_num, key="custom_num")
|
309 |
-
custom_denom_expr = st.text_input("Denominator Expression", value=default_denom, key="custom_denom")
|
310 |
-
s_custom = st.number_input("Custom s value", value=1.0, step=0.1, key="s_custom")
|
311 |
if st.button("Compute z vs. β Curves", key="tab1_button"):
|
312 |
with col2:
|
313 |
fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
|
314 |
-
|
315 |
if fig is not None:
|
316 |
st.plotly_chart(fig, use_container_width=True)
|
317 |
-
st.markdown("### Additional Expressions")
|
318 |
-
st.markdown("""
|
319 |
-
**Low y Expression (Red):**
|
320 |
-
```
|
321 |
-
((y - 2)*((-1 + sqrt(y*beta*(z_a - 1)))/z_a) + y*beta*((z_a-1)/z_a) - 1/z_a - 1) /
|
322 |
-
(((-1 + sqrt(y*beta*(z_a - 1)))/z_a)**2 + ((-1 + sqrt(y*beta*(z_a - 1)))/z_a))
|
323 |
-
```
|
324 |
-
|
325 |
-
**High y Expression (Green):**
|
326 |
-
```
|
327 |
-
(-4*z_a*(z_a-1)*y*beta - 2*z_a*y + 2*z_a*(2*z_a-1))/(1-2*z_a)
|
328 |
-
```
|
329 |
-
|
330 |
-
**Alternate Low Expression (Orange):**
|
331 |
-
```
|
332 |
-
(z_a*y*beta*(z_a-1) - 2*z_a*(1-y) - 2*z_a**2)/(2+2*z_a)
|
333 |
-
```
|
334 |
-
""")
|
335 |
|
336 |
# ----- Tab 2: Im{s} vs. z -----
|
337 |
with tab2:
|
|
|
122 |
betas = np.array(betas)
|
123 |
return (z_a * y * betas * (z_a - 1) - 2*z_a*(1 - y) - 2*z_a**2) / (2 + 2*z_a)
|
124 |
|
125 |
+
def compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr):
|
126 |
"""
|
127 |
+
Compute a custom curve where s is defined by numerator/denominator expressions.
|
128 |
+
Allowed variables: z_a, beta, y
|
|
|
|
|
129 |
"""
|
130 |
+
beta_sym, z_a_sym, y_sym = sp.symbols("beta z_a y", positive=True)
|
131 |
+
local_dict = {"beta": beta_sym, "z_a": z_a_sym, "y": y_sym}
|
132 |
try:
|
133 |
+
num_expr = sp.sympify(s_num_expr, locals=local_dict)
|
134 |
+
denom_expr = sp.sympify(s_denom_expr, locals=local_dict)
|
135 |
+
s_expr = num_expr / denom_expr
|
136 |
except sp.SympifyError as e:
|
137 |
st.error(f"Error parsing expressions: {e}")
|
138 |
return np.full_like(betas, np.nan)
|
139 |
|
140 |
+
s_func = sp.lambdify((beta_sym, z_a_sym, y_sym), s_expr, modules=["numpy"])
|
|
|
141 |
with np.errstate(divide='ignore', invalid='ignore'):
|
142 |
+
result = s_func(betas, z_a, y)
|
143 |
return result
|
144 |
|
145 |
def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
|
146 |
+
s_num_expr=None, s_denom_expr=None):
|
147 |
if z_a <= 0 or y <= 0 or z_min >= z_max:
|
148 |
st.error("Invalid input parameters.")
|
149 |
return None
|
|
|
156 |
|
157 |
fig = go.Figure()
|
158 |
fig.add_trace(go.Scatter(x=betas, y=z_maxs, mode="markers+lines", name="Upper z*(β)",
|
159 |
+
marker=dict(size=5, color='blue'), line=dict(color='blue')))
|
160 |
fig.add_trace(go.Scatter(x=betas, y=z_mins, mode="markers+lines", name="Lower z*(β)",
|
161 |
+
marker=dict(size=5, color='lightblue'), line=dict(color='lightblue')))
|
162 |
fig.add_trace(go.Scatter(x=betas, y=low_y_curve, mode="markers+lines", name="Low y Expression",
|
163 |
+
marker=dict(size=5, color='red'), line=dict(color='red')))
|
164 |
fig.add_trace(go.Scatter(x=betas, y=high_y_curve, mode="markers+lines", name="High y Expression",
|
165 |
+
marker=dict(size=5, color='green'), line=dict(color='green')))
|
166 |
fig.add_trace(go.Scatter(x=betas, y=alt_low_expr, mode="markers+lines", name="Alternate Low Expression",
|
167 |
+
marker=dict(size=5, color='orange'), line=dict(color='orange')))
|
168 |
|
169 |
+
if s_num_expr and s_denom_expr:
|
170 |
+
custom_curve = compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr)
|
171 |
+
fig.add_trace(go.Scatter(x=betas, y=custom_curve, mode="markers+lines", name="Custom s Expression",
|
172 |
+
marker=dict(size=5, color='purple'), line=dict(color='purple')))
|
173 |
|
174 |
fig.update_layout(title="Curves vs β: z*(β) Boundaries and Asymptotic Expressions",
|
175 |
+
xaxis_title="β", yaxis_title="Value", hovermode="x unified")
|
176 |
return fig
|
177 |
|
178 |
def compute_cubic_roots(z, beta, z_a, y):
|
|
|
297 |
with st.expander("Resolution Settings"):
|
298 |
beta_steps = st.slider("β steps", min_value=51, max_value=501, value=201, step=50, key="beta_steps")
|
299 |
z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000, step=1000, key="z_steps")
|
300 |
+
|
301 |
+
st.subheader("Custom s Expression")
|
302 |
+
st.markdown("Enter expressions as functions of `y`, `beta`, and `z_a`")
|
303 |
+
s_num = st.text_input("s numerator", value="y*beta*(z_a-1)", key="s_num")
|
304 |
+
s_denom = st.text_input("s denominator", value="z_a", key="s_denom")
|
305 |
+
|
|
|
|
|
|
|
306 |
if st.button("Compute z vs. β Curves", key="tab1_button"):
|
307 |
with col2:
|
308 |
fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
|
309 |
+
s_num, s_denom)
|
310 |
if fig is not None:
|
311 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
312 |
|
313 |
# ----- Tab 2: Im{s} vs. z -----
|
314 |
with tab2:
|