Skip to content

Commit d7878e1

Browse files
author
rmdocherty
committed
can now return arrays needed to calc intervals client-side
1 parent 76142af commit d7878e1

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

frontend/src/App.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ const App = () => {
137137
const minSide = Math.min(imageInfo?.width!, imageInfo?.height!)
138138
if (obj["l"] < minSide) { setShowWarning("over") }
139139

140+
console.log(obj["pf_1d"])
141+
140142
setTargetL(obj["l"]);
141143
} catch (e) {
142144
const error = e as Error;
@@ -167,9 +169,9 @@ const App = () => {
167169
<div className={`w-full h-full`}>
168170
<Topbar loadFromFile={appLoadFile} reset={reset}></Topbar>
169171
<div className={`flex`} style={{ margin: '1.5%' }} > {/*Canvas div on left, sidebar on right*/}
170-
{previewImg && <DragDrop loadFromFile={appLoadFile} />}
172+
{!previewImg && <DragDrop loadFromFile={appLoadFile} />}
171173
{previewImg && <PreviewCanvas />}
172-
<NormalSlider />
174+
{false && <NormalSlider />}
173175
</div>
174176
<Menu />
175177
<ErrorMessage />

representativity/core.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from itertools import product, chain
33
from scipy.stats import norm # type: ignore
44
from scipy.optimize import minimize # type: ignore
5-
5+
import json
66

77
DEFAULT_N_DIV = 301
88

@@ -709,7 +709,7 @@ def get_prediction_interval(
709709
pred_std_error_std: float,
710710
conf_level: float = 0.95,
711711
n_divisions: int = DEFAULT_N_DIV,
712-
) -> tuple[np.ndarray, np.ndarray]:
712+
) -> tuple[tuple[np.ndarray, np.ndarray], np.ndarray, np.ndarray]:
713713
"""Get the prediction interval for the phase fraction of the material given the image phase
714714
fraction, the predicted standard deviation and the standard deviation of the prediction error.
715715
@@ -755,16 +755,20 @@ def get_prediction_interval(
755755
# need a bit of normalization for symmetric bounds (it's very close to 1 already)
756756
sum_dist_norm /= np.trapz(sum_dist_norm, pf_x_1d)
757757
# Find the alpha confidence bounds
758-
# TODO: return cum_sum_sum_dist_norm array over HTTP
759-
# and write js function that does lines 759-762
758+
# TODO: return cum_sum_sum_dist_norm and pf_x_1_d array over HTTP
759+
# and write js function that does lines 762-764
760760
# will need to send the array somehow
761761
cum_sum_sum_dist_norm = np.cumsum(sum_dist_norm * np.diff(pf_x_1d)[0])
762762
half_conf_level = (1 + conf_level) / 2
763763
conf_level_beginning = np.where(cum_sum_sum_dist_norm > 1 - half_conf_level)[0][0]
764764
conf_level_end = np.where(cum_sum_sum_dist_norm > half_conf_level)[0][0]
765765

766766
# Calculate the interval
767-
return pf_x_1d[conf_level_beginning], pf_x_1d[conf_level_end]
767+
return (
768+
(pf_x_1d[conf_level_beginning], pf_x_1d[conf_level_end]),
769+
pf_x_1d,
770+
cum_sum_sum_dist_norm,
771+
)
768772

769773

770774
def find_n_for_err_targ(
@@ -795,7 +799,7 @@ def find_n_for_err_targ(
795799
"""
796800
n = n[0] # needs to be here during the optimize
797801
std_bernoulli = ((1 / n) * (image_pf * (1 - image_pf))) ** 0.5
798-
pred_interval = get_prediction_interval(
802+
pred_interval, _, _ = get_prediction_interval(
799803
image_pf, std_bernoulli, pred_std_error_std, conf_level, n_divisions
800804
)
801805
err_for_img = image_pf - pred_interval[0]
@@ -878,10 +882,10 @@ def make_error_prediction(
878882
) ** 0.5 # TODO: this is the std of phi relative to Phi with
879883
std_model = get_std_model(n_dims, n_elems)
880884
abs_err_target = target_error * phase_fraction
881-
z = 0
885+
z, pf_1d, cum_sum_sum = 0, None, None
882886
if model_error:
883887
# calculate the absolute error for the image:
884-
conf_bounds = get_prediction_interval(
888+
conf_bounds, pf_1d, cum_sum_sum = get_prediction_interval(
885889
phase_fraction, std_bern, std_model, confidence, DEFAULT_N_DIV
886890
)
887891
abs_err_for_img = phase_fraction - conf_bounds[0]
@@ -921,5 +925,7 @@ def make_error_prediction(
921925
"percent_err": percentage_err_for_img,
922926
"abs_err": abs_err_for_img,
923927
"l": l_for_err_targ,
928+
"pf_1d": list(pf_1d),
929+
"cum_sum_sum": list(cum_sum_sum),
924930
}
925931
return result # percentage_err_for_img, l_for_err_targ, integral_range

representativity/server.py

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ def representativity(request) -> Response:
138138
"percent_err": result["percent_err"] * 100,
139139
"l": result["l"],
140140
"cls": result["integral_range"],
141+
"pf_1d": result["pf_1d"],
142+
"cum_sum_sum": result["cum_sum_sum"],
141143
}
142144
print(out)
143145

0 commit comments

Comments
 (0)