Skip to content

Commit 39dd4bc

Browse files
author
rmdocherty
committed
changes to work with module structure
1 parent 9f0d3f5 commit 39dd4bc

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

representativity/correction_fitting/prediction_error.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import json
22
import numpy as np
33
import time
4-
from representativity import util
4+
from representativity import util, core, slicegan
55
from scipy.stats import norm
66
import matplotlib.pyplot as plt
77
from scipy.optimize import minimize
88
from scipy.optimize import curve_fit
99
from functools import partial
1010

11+
from os import getcwd, path, mkdir
12+
13+
CWD = getcwd()
14+
DATA_PATH = path.join(CWD, "representativity/correction_fitting")
15+
1116
"""
1217
File: prediction_error.py
1318
@@ -34,10 +39,10 @@ def error_by_size_estimation(dim, run_number=0, std_not_cls=True):
3439

3540

3641
def data_micros(dim):
37-
with open("microlib_statistics_periodic.json", "r") as fp:
42+
with open(f"{DATA_PATH}/microlib_statistics_periodic.json", "r") as fp:
3843
datafull = json.load(fp)
3944

40-
with open("micro_names.json", "r") as fp:
45+
with open(f"{DATA_PATH}/micro_names.json", "r") as fp:
4146
micro_names = json.load(fp)
4247

4348
dim_data = datafull[f"data_gen_{dim}"]
@@ -145,6 +150,11 @@ def plot_pred_vs_fit(dim, edge_length, num_runs=5, std_not_cls=True):
145150
plt.title(f"Prediction vs. Fit Data {dim}")
146151
ax = plt.gca()
147152
ax.set_aspect("equal", adjustable="box")
153+
154+
try:
155+
mkdir("pred_vs_fit_all_runs_periodic_cut")
156+
except FileExistsError:
157+
pass
148158
plt.savefig(
149159
f"pred_vs_fit_all_runs_periodic_cut/pred_vs_fit_{dim}_{edge_length}.png"
150160
)
@@ -159,14 +169,14 @@ def std_error_by_size(dim, edge_lengths, num_runs=5, start_idx=0, std_not_cls=Tr
159169
stds = np.array(stds).sum(axis=0) / num_runs
160170
n_voxels = np.array([edge ** int(dim[0]) for edge in edge_lengths])
161171
stds, n_voxels = stds[start_idx:], n_voxels[start_idx:]
162-
popt, pcov = curve_fit(partial(util.fit_to_errs_function, dim), n_voxels, stds)
172+
popt, pcov = curve_fit(partial(core.fit_to_errs_function, dim), n_voxels, stds)
163173
# img_sizes = [(l,)*2 for l in edge_lengths]
164174
# pfs, irs = [0.1, 0.2, 0.4], [40, 40, 40]
165175
# for i in range(len(pfs)):
166176
# erros_inherent = util.bernouli(pfs[i], util.ns_from_dims(img_sizes, irs[i]),conf=0.95)
167177
# plt.plot(edge_lengths, erros_inherent, label=f'Inherent error IR = {irs[i]}, pf = {pfs[i]}')
168178
print(f"popt: {popt}")
169-
prediction_error = util.fit_to_errs_function(dim, n_voxels, *popt)
179+
prediction_error = core.fit_to_errs_function(dim, n_voxels, *popt)
170180
return prediction_error, stds
171181

172182

@@ -197,8 +207,8 @@ def mape(slope):
197207

198208

199209
def optimal_slopes(dim, num_runs=5):
200-
data_micros = data_micros(dim)
201-
edge_lengths_pred = data_micros[0]["edge_lengths_pred"]
210+
data_micro = data_micros(dim)
211+
edge_lengths_pred = data_micro[0]["edge_lengths_pred"]
202212
slopes = []
203213
stds = []
204214
for edge_length in edge_lengths_pred:

0 commit comments

Comments
 (0)