Spaces:
Running
Running
watermark added
Browse files- marimo/bomb_calorimetry.py +4 -0
- marimo/crystal_violet.py +5 -0
- marimo/equilibrium.py +6 -1
- marimo/surface_adsorption.py +29 -23
- src/pycek_public/cek_labs.py +1 -11
- src/pycek_public/plotting.py +0 -166
marimo/bomb_calorimetry.py
CHANGED
|
@@ -82,6 +82,10 @@ def _(cek, lab, mo, reset_button, run_button, sample_selector):
|
|
| 82 |
message = ""
|
| 83 |
download_button = ""
|
| 84 |
if run_button.value:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
mo.stop(sample_selector.value is None, mo.md(f"### No sample selected !!"))
|
| 86 |
|
| 87 |
lab.set_parameters(sample=sample_selector.value)
|
|
|
|
| 82 |
message = ""
|
| 83 |
download_button = ""
|
| 84 |
if run_button.value:
|
| 85 |
+
mo.stop(
|
| 86 |
+
not student_ID.value.isdigit(),
|
| 87 |
+
mo.md(f"### Invalid Student ID: {student_ID.value}"),
|
| 88 |
+
)
|
| 89 |
mo.stop(sample_selector.value is None, mo.md(f"### No sample selected !!"))
|
| 90 |
|
| 91 |
lab.set_parameters(sample=sample_selector.value)
|
marimo/crystal_violet.py
CHANGED
|
@@ -119,6 +119,11 @@ def _(
|
|
| 119 |
message = ""
|
| 120 |
download_button = ""
|
| 121 |
if run_button.value:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
cv_vol = cv_volume.value
|
| 123 |
oh_vol = oh_volume.value
|
| 124 |
h2o_vol = h2o_volume.value
|
|
|
|
| 119 |
message = ""
|
| 120 |
download_button = ""
|
| 121 |
if run_button.value:
|
| 122 |
+
mo.stop(
|
| 123 |
+
not student_ID.value.isdigit(),
|
| 124 |
+
mo.md(f"### Invalid Student ID: {student_ID.value}"),
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
cv_vol = cv_volume.value
|
| 128 |
oh_vol = oh_volume.value
|
| 129 |
h2o_vol = h2o_volume.value
|
marimo/equilibrium.py
CHANGED
|
@@ -104,7 +104,7 @@ def _(compounds, mo, stoichiometry):
|
|
| 104 |
def _(mo, np):
|
| 105 |
step = mo.ui.slider(steps=np.logspace(-8,0,90),label="$\delta c$",show_value=True)
|
| 106 |
tol = mo.ui.slider(steps=np.logspace(-8,0,90),label="Convergence Threshold",show_value=True)
|
| 107 |
-
max_iterations = mo.ui.slider(steps=np.logspace(2,
|
| 108 |
|
| 109 |
check_0 = mo.ui.checkbox(label= "opt 0",)
|
| 110 |
check_1 = mo.ui.checkbox(label= f"Decrease $\delta c$",)
|
|
@@ -240,6 +240,11 @@ def _(
|
|
| 240 |
plt.ylabel(axes[1])
|
| 241 |
if log:
|
| 242 |
plt.yscale("log")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
plt.legend()
|
| 244 |
return plt.gca()
|
| 245 |
return plot, solve_equilibrium
|
|
|
|
| 104 |
def _(mo, np):
|
| 105 |
step = mo.ui.slider(steps=np.logspace(-8,0,90),label="$\delta c$",show_value=True)
|
| 106 |
tol = mo.ui.slider(steps=np.logspace(-8,0,90),label="Convergence Threshold",show_value=True)
|
| 107 |
+
max_iterations = mo.ui.slider(steps=np.logspace(2,6,90),label="Max Iterations",show_value=True)
|
| 108 |
|
| 109 |
check_0 = mo.ui.checkbox(label= "opt 0",)
|
| 110 |
check_1 = mo.ui.checkbox(label= f"Decrease $\delta c$",)
|
|
|
|
| 240 |
plt.ylabel(axes[1])
|
| 241 |
if log:
|
| 242 |
plt.yscale("log")
|
| 243 |
+
|
| 244 |
+
ax = plt.gca()
|
| 245 |
+
ax.text(0.5, 0.5, 'TEMPLATE', transform=ax.transAxes,
|
| 246 |
+
fontsize=40, color='gray', alpha=0.5,
|
| 247 |
+
ha='center', va='center', rotation=30)
|
| 248 |
plt.legend()
|
| 249 |
return plt.gca()
|
| 250 |
return plot, solve_equilibrium
|
marimo/surface_adsorption.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import marimo
|
| 2 |
|
| 3 |
-
__generated_with = "0.11.
|
| 4 |
app = marimo.App(width="medium")
|
| 5 |
|
| 6 |
|
|
@@ -9,7 +9,7 @@ def _():
|
|
| 9 |
import marimo as mo
|
| 10 |
import pycek_public as cek
|
| 11 |
|
| 12 |
-
lab = cek.
|
| 13 |
return cek, lab, mo
|
| 14 |
|
| 15 |
|
|
@@ -42,7 +42,7 @@ def _(mo):
|
|
| 42 |
|
| 43 |
|
| 44 |
@app.cell
|
| 45 |
-
def _(lab, mo):
|
| 46 |
def set_ID(value):
|
| 47 |
return cek.set_ID(mo, lab, value)
|
| 48 |
|
|
@@ -83,26 +83,30 @@ def _(cek, lab, mo, reset_button, run_button, student_ID, temperature):
|
|
| 83 |
image = ""
|
| 84 |
message = ""
|
| 85 |
download_button = ""
|
| 86 |
-
if
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
mo.hstack([mo.vstack([mo.md(message), download_button]), image])
|
| 108 |
return (
|
|
@@ -111,8 +115,10 @@ def _(cek, lab, mo, reset_button, run_button, student_ID, temperature):
|
|
| 111 |
file_content,
|
| 112 |
fname,
|
| 113 |
image,
|
|
|
|
| 114 |
message,
|
| 115 |
plot,
|
|
|
|
| 116 |
)
|
| 117 |
|
| 118 |
|
|
|
|
| 1 |
import marimo
|
| 2 |
|
| 3 |
+
__generated_with = "0.11.9"
|
| 4 |
app = marimo.App(width="medium")
|
| 5 |
|
| 6 |
|
|
|
|
| 9 |
import marimo as mo
|
| 10 |
import pycek_public as cek
|
| 11 |
|
| 12 |
+
lab = cek.surface_adsorption(make_plots=True)
|
| 13 |
return cek, lab, mo
|
| 14 |
|
| 15 |
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
@app.cell
|
| 45 |
+
def _(cek, lab, mo):
|
| 46 |
def set_ID(value):
|
| 47 |
return cek.set_ID(mo, lab, value)
|
| 48 |
|
|
|
|
| 83 |
image = ""
|
| 84 |
message = ""
|
| 85 |
download_button = ""
|
| 86 |
+
if run_button.value:
|
| 87 |
+
mo.stop(
|
| 88 |
+
not student_ID.value.isdigit(),
|
| 89 |
+
mo.md(f"### Invalid Student ID: {student_ID.value}"),
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
lab.set_parameters(temperature=temperature.value + 273.15)
|
| 93 |
+
data = lab.create_data()
|
| 94 |
+
file_content = lab.write_data_to_string()
|
| 95 |
+
|
| 96 |
+
fname = lab.filename_gen.random
|
| 97 |
+
message = f"### Running Experiment\n"
|
| 98 |
+
for k, v in lab.metadata.items():
|
| 99 |
+
message += f"####{k} = {v}\n"
|
| 100 |
+
message += f"#### File created = {fname}\n"
|
| 101 |
+
|
| 102 |
+
download_button = mo.download(
|
| 103 |
+
file_content,
|
| 104 |
+
filename=fname,
|
| 105 |
+
label=f"Download {fname}",
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
plot = cek.plotting()
|
| 109 |
+
image = plot.quick_plot(scatter=data, output="marimo")
|
| 110 |
|
| 111 |
mo.hstack([mo.vstack([mo.md(message), download_button]), image])
|
| 112 |
return (
|
|
|
|
| 115 |
file_content,
|
| 116 |
fname,
|
| 117 |
image,
|
| 118 |
+
k,
|
| 119 |
message,
|
| 120 |
plot,
|
| 121 |
+
v,
|
| 122 |
)
|
| 123 |
|
| 124 |
|
src/pycek_public/cek_labs.py
CHANGED
|
@@ -6,17 +6,6 @@ from collections import OrderedDict
|
|
| 6 |
|
| 7 |
from abc import ABC, abstractmethod
|
| 8 |
|
| 9 |
-
def set_ID(mo, lab, value):
|
| 10 |
-
try:
|
| 11 |
-
student_number = int(value.strip())
|
| 12 |
-
if student_number <= 0:
|
| 13 |
-
error = f"### Invalid Student ID: {value}"
|
| 14 |
-
print(mo.md(error))
|
| 15 |
-
raise ValueError(error)
|
| 16 |
-
print(mo.md(f"Valid Student ID: {student_number}"))
|
| 17 |
-
lab.set_student_ID(int(value))
|
| 18 |
-
except ValueError:
|
| 19 |
-
print(mo.md(f"### Invalid Student ID: {value}"))
|
| 20 |
|
| 21 |
class cek_labs(ABC):
|
| 22 |
def __init__(self, **kwargs):
|
|
@@ -447,3 +436,4 @@ class cek_labs(ABC):
|
|
| 447 |
@abstractmethod
|
| 448 |
def create_data(self):
|
| 449 |
pass
|
|
|
|
|
|
| 6 |
|
| 7 |
from abc import ABC, abstractmethod
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class cek_labs(ABC):
|
| 11 |
def __init__(self, **kwargs):
|
|
|
|
| 436 |
@abstractmethod
|
| 437 |
def create_data(self):
|
| 438 |
pass
|
| 439 |
+
|
src/pycek_public/plotting.py
CHANGED
|
@@ -67,169 +67,3 @@ class plotting():
|
|
| 67 |
plt.savefig(output)
|
| 68 |
plt.close()
|
| 69 |
|
| 70 |
-
## --- END STUDENT VERSION -- ##
|
| 71 |
-
|
| 72 |
-
def get_t_value(self, ndof, confidence=None):
|
| 73 |
-
"""
|
| 74 |
-
Calculate the two-tailed Student's t-value for a given number of degrees of freedom
|
| 75 |
-
and confidence level.
|
| 76 |
-
|
| 77 |
-
Parameters:
|
| 78 |
-
ndof (int): Number of degrees of freedom
|
| 79 |
-
confidence (float): Confidence level, default is 0.95 for 95% confidence
|
| 80 |
-
|
| 81 |
-
Returns:
|
| 82 |
-
float: The critical t-value for the specified parameters
|
| 83 |
-
|
| 84 |
-
Example:
|
| 85 |
-
For 95% confidence and 10 degrees of freedom:
|
| 86 |
-
>>> get_t_value(10)
|
| 87 |
-
2.2281388519495335
|
| 88 |
-
"""
|
| 89 |
-
if confidence is None:
|
| 90 |
-
confidence = self._confidence_level
|
| 91 |
-
|
| 92 |
-
# Calculate alpha (significance level) from confidence level
|
| 93 |
-
# For 95% confidence, alpha = 0.05
|
| 94 |
-
alpha = 1 - confidence
|
| 95 |
-
|
| 96 |
-
# Calculate the t-value using the percent point function (PPF) of the t-distribution
|
| 97 |
-
# We use alpha/2 for two-tailed test and (1 - alpha/2) for the upper tail
|
| 98 |
-
tval = stats.t.ppf(1.0 - alpha/2., ndof)
|
| 99 |
-
|
| 100 |
-
return tval
|
| 101 |
-
|
| 102 |
-
def plot_fit_with_confidence_band(
|
| 103 |
-
self, data, fit_model, popt,
|
| 104 |
-
confidence=None, output=None):
|
| 105 |
-
"""
|
| 106 |
-
Plot the data along with the best fit line and its associated confidence band.
|
| 107 |
-
|
| 108 |
-
Parameters:
|
| 109 |
-
x_data (array-like): Independent variable data points
|
| 110 |
-
y_data (array-like): Observed dependent variable values
|
| 111 |
-
fit_model (callable): The model function used in the fit
|
| 112 |
-
popt: Optimal parameter values from the fit
|
| 113 |
-
confidence (float, optional): Confidence level for the confidence band
|
| 114 |
-
(default is 0.95)
|
| 115 |
-
|
| 116 |
-
Returns:
|
| 117 |
-
None: Displays a matplotlib plot with data, fit line, and confidence band
|
| 118 |
-
|
| 119 |
-
Example:
|
| 120 |
-
>>> fit_result = scipy_function_fit(x_data, y_data, linear_model)
|
| 121 |
-
>>> plot_fit_with_confidence_band(x_data, y_data, fit_result)
|
| 122 |
-
"""
|
| 123 |
-
x_data = data[:,0]
|
| 124 |
-
y_data = data[:,1]
|
| 125 |
-
|
| 126 |
-
# Calculate degrees of freedom: number of data points minus number of parameters
|
| 127 |
-
ndof = max(0, len(x_data) - len(popt))
|
| 128 |
-
|
| 129 |
-
# Create the plot figure with a specified size
|
| 130 |
-
plt.figure(figsize=(10, 6))
|
| 131 |
-
|
| 132 |
-
# Plot the observed data points as a scatter plot
|
| 133 |
-
plt.scatter(x_data, y_data, color='blue', label='Data')
|
| 134 |
-
|
| 135 |
-
# Generate points for the fitted line
|
| 136 |
-
# Linearly spaced between the minimum and maximum of x_data
|
| 137 |
-
x_fit = np.linspace(min(x_data), max(x_data), 100)
|
| 138 |
-
|
| 139 |
-
# Calculate the fitted y values using the optimal parameters
|
| 140 |
-
y_fit = fit_model(x_fit, *popt)
|
| 141 |
-
|
| 142 |
-
# Plot the fitted line
|
| 143 |
-
plt.plot(x_fit, y_fit, 'r-', label='Best fit')
|
| 144 |
-
|
| 145 |
-
if confidence is not None:
|
| 146 |
-
# Calculate the error for the confidence band based on the data and fit
|
| 147 |
-
y_err = np.sqrt(1/len(x_data) + (x_fit - np.mean(x_data))**2 /
|
| 148 |
-
np.sum((x_data - np.mean(x_data))**2))
|
| 149 |
-
|
| 150 |
-
# t-statistic used to calculate the confidence interval
|
| 151 |
-
tval = self.get_t_value(ndof, confidence)
|
| 152 |
-
|
| 153 |
-
# Calculate the upper and lower bounds of the confidence band
|
| 154 |
-
bounds = np.sqrt(np.sum((y_data - fit_model(x_data, *popt))**2) / ndof)
|
| 155 |
-
y_upper = y_fit + tval * y_err * bounds
|
| 156 |
-
y_lower = y_fit - tval * y_err * bounds
|
| 157 |
-
|
| 158 |
-
# Plot the confidence band by shading the area between the upper and lower bounds
|
| 159 |
-
plt.fill_between(x_fit, y_lower, y_upper,
|
| 160 |
-
color='gray', alpha=0.2,
|
| 161 |
-
label=f'{int(confidence*100)}% Confidence band')
|
| 162 |
-
plt.title(f'Data with Linear Fit and {int(confidence*100)}% Confidence Bands')
|
| 163 |
-
else:
|
| 164 |
-
plt.title(f'Data with Linear Fit')
|
| 165 |
-
|
| 166 |
-
# Add labels and title to the plot
|
| 167 |
-
plt.xlabel('x')
|
| 168 |
-
plt.ylabel('y')
|
| 169 |
-
|
| 170 |
-
# ymin = np.min(y_data) - 0.1*np.abs(np.min(y_data))
|
| 171 |
-
# ymax = np.max(y_data) + 0.1*np.abs(np.max(y_data))
|
| 172 |
-
# ax = plt.gca()
|
| 173 |
-
# ax.set_ylim([ymin, ymax])
|
| 174 |
-
|
| 175 |
-
# Display the legend
|
| 176 |
-
plt.legend()
|
| 177 |
-
|
| 178 |
-
# Show the plot
|
| 179 |
-
if output is None:
|
| 180 |
-
plt.show()
|
| 181 |
-
else:
|
| 182 |
-
plt.savefig(output)
|
| 183 |
-
|
| 184 |
-
def plot_residuals(self, data, fit_model, popt):
|
| 185 |
-
"""
|
| 186 |
-
Create a residual plot to visualize the differences between observed and predicted values.
|
| 187 |
-
Residuals are the differences between actual y values and model predictions.
|
| 188 |
-
|
| 189 |
-
Parameters:
|
| 190 |
-
x_data (array-like): Independent variable data points (input for the model)
|
| 191 |
-
y_data (array-like): Observed dependent variable values (true values)
|
| 192 |
-
fit_model (callable): The model function used in the fit
|
| 193 |
-
popt: Optimal parameter values from the fit
|
| 194 |
-
The 'result' dictionary should include at least the 'popt' key, which contains the
|
| 195 |
-
optimal parameters for the fitted model.
|
| 196 |
-
|
| 197 |
-
Returns:
|
| 198 |
-
None: Displays a matplotlib plot of residuals
|
| 199 |
-
|
| 200 |
-
Example:
|
| 201 |
-
>>> fit_result = scipy_function_fit(x_data, y_data, linear_model)
|
| 202 |
-
>>> plot_residuals(x_data, y_data, fit_result)
|
| 203 |
-
"""
|
| 204 |
-
x_data = data[:,0]
|
| 205 |
-
y_data = data[:,1]
|
| 206 |
-
|
| 207 |
-
# Calculate model predictions using the fitted parameters ('popt' contains optimal parameters)
|
| 208 |
-
y_pred = fit_model(x_data, *popt)
|
| 209 |
-
|
| 210 |
-
# Calculate residuals (observed y values - predicted y values)
|
| 211 |
-
residuals = y_data - y_pred
|
| 212 |
-
|
| 213 |
-
# Create a new figure with a specified size for the plot
|
| 214 |
-
plt.figure(figsize=(10, 6))
|
| 215 |
-
|
| 216 |
-
# Scatter plot of the residuals vs the x_data points
|
| 217 |
-
# Each point in the scatter plot corresponds to the residual for a specific x_data value
|
| 218 |
-
plt.scatter(x_data, residuals, color='blue', label='Data')
|
| 219 |
-
|
| 220 |
-
# Add a horizontal line at y=0 for reference to see how residuals deviate from 0
|
| 221 |
-
plt.axhline(y=0, color='black', linestyle='--', label='Zero Residuals')
|
| 222 |
-
|
| 223 |
-
# Label the x and y axes
|
| 224 |
-
plt.xlabel('x') # x-axis represents the independent variable
|
| 225 |
-
plt.ylabel('e') # y-axis represents the residuals or errors (observed - predicted)
|
| 226 |
-
|
| 227 |
-
# Add a title to the plot
|
| 228 |
-
plt.title('Plot of the residuals')
|
| 229 |
-
|
| 230 |
-
# Optionally, add a legend to clarify the plot's labels
|
| 231 |
-
plt.legend()
|
| 232 |
-
|
| 233 |
-
# Display the plot
|
| 234 |
-
# plt.savefig("fit_residuals.png")
|
| 235 |
-
plt.show()
|
|
|
|
| 67 |
plt.savefig(output)
|
| 68 |
plt.close()
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|