|
2 | 2 | from itertools import product, chain
|
3 | 3 | from scipy.stats import norm # type: ignore
|
4 | 4 | from scipy.optimize import minimize # type: ignore
|
5 |
| - |
| 5 | +import json |
6 | 6 |
|
7 | 7 | DEFAULT_N_DIV = 301
|
8 | 8 |
|
@@ -709,7 +709,7 @@ def get_prediction_interval(
|
709 | 709 | pred_std_error_std: float,
|
710 | 710 | conf_level: float = 0.95,
|
711 | 711 | n_divisions: int = DEFAULT_N_DIV,
|
712 |
| -) -> tuple[np.ndarray, np.ndarray]: |
| 712 | +) -> tuple[tuple[np.ndarray, np.ndarray], np.ndarray, np.ndarray]: |
713 | 713 | """Get the prediction interval for the phase fraction of the material given the image phase
|
714 | 714 | fraction, the predicted standard deviation and the standard deviation of the prediction error.
|
715 | 715 |
|
@@ -755,16 +755,20 @@ def get_prediction_interval(
|
755 | 755 | # need a bit of normalization for symmetric bounds (it's very close to 1 already)
|
756 | 756 | sum_dist_norm /= np.trapz(sum_dist_norm, pf_x_1d)
|
757 | 757 | # 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 |
760 | 760 | # will need to send the array somehow
|
761 | 761 | cum_sum_sum_dist_norm = np.cumsum(sum_dist_norm * np.diff(pf_x_1d)[0])
|
762 | 762 | half_conf_level = (1 + conf_level) / 2
|
763 | 763 | conf_level_beginning = np.where(cum_sum_sum_dist_norm > 1 - half_conf_level)[0][0]
|
764 | 764 | conf_level_end = np.where(cum_sum_sum_dist_norm > half_conf_level)[0][0]
|
765 | 765 |
|
766 | 766 | # 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 | + ) |
768 | 772 |
|
769 | 773 |
|
770 | 774 | def find_n_for_err_targ(
|
@@ -795,7 +799,7 @@ def find_n_for_err_targ(
|
795 | 799 | """
|
796 | 800 | n = n[0] # needs to be here during the optimize
|
797 | 801 | std_bernoulli = ((1 / n) * (image_pf * (1 - image_pf))) ** 0.5
|
798 |
| - pred_interval = get_prediction_interval( |
| 802 | + pred_interval, _, _ = get_prediction_interval( |
799 | 803 | image_pf, std_bernoulli, pred_std_error_std, conf_level, n_divisions
|
800 | 804 | )
|
801 | 805 | err_for_img = image_pf - pred_interval[0]
|
@@ -878,10 +882,10 @@ def make_error_prediction(
|
878 | 882 | ) ** 0.5 # TODO: this is the std of phi relative to Phi with
|
879 | 883 | std_model = get_std_model(n_dims, n_elems)
|
880 | 884 | abs_err_target = target_error * phase_fraction
|
881 |
| - z = 0 |
| 885 | + z, pf_1d, cum_sum_sum = 0, None, None |
882 | 886 | if model_error:
|
883 | 887 | # calculate the absolute error for the image:
|
884 |
| - conf_bounds = get_prediction_interval( |
| 888 | + conf_bounds, pf_1d, cum_sum_sum = get_prediction_interval( |
885 | 889 | phase_fraction, std_bern, std_model, confidence, DEFAULT_N_DIV
|
886 | 890 | )
|
887 | 891 | abs_err_for_img = phase_fraction - conf_bounds[0]
|
@@ -921,5 +925,7 @@ def make_error_prediction(
|
921 | 925 | "percent_err": percentage_err_for_img,
|
922 | 926 | "abs_err": abs_err_for_img,
|
923 | 927 | "l": l_for_err_targ,
|
| 928 | + "pf_1d": list(pf_1d), |
| 929 | + "cum_sum_sum": list(cum_sum_sum), |
924 | 930 | }
|
925 | 931 | return result # percentage_err_for_img, l_for_err_targ, integral_range
|
0 commit comments