1
1
import json
2
2
import numpy as np
3
3
import time
4
- from representativity import util
4
+ from representativity import util , core , slicegan
5
5
from scipy .stats import norm
6
6
import matplotlib .pyplot as plt
7
7
from scipy .optimize import minimize
8
8
from scipy .optimize import curve_fit
9
9
from functools import partial
10
10
11
+ from os import getcwd , path , mkdir
12
+
13
+ CWD = getcwd ()
14
+ DATA_PATH = path .join (CWD , "representativity/correction_fitting" )
15
+
11
16
"""
12
17
File: prediction_error.py
13
18
@@ -34,10 +39,10 @@ def error_by_size_estimation(dim, run_number=0, std_not_cls=True):
34
39
35
40
36
41
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 :
38
43
datafull = json .load (fp )
39
44
40
- with open (" micro_names.json" , "r" ) as fp :
45
+ with open (f" { DATA_PATH } / micro_names.json" , "r" ) as fp :
41
46
micro_names = json .load (fp )
42
47
43
48
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):
145
150
plt .title (f"Prediction vs. Fit Data { dim } " )
146
151
ax = plt .gca ()
147
152
ax .set_aspect ("equal" , adjustable = "box" )
153
+
154
+ try :
155
+ mkdir ("pred_vs_fit_all_runs_periodic_cut" )
156
+ except FileExistsError :
157
+ pass
148
158
plt .savefig (
149
159
f"pred_vs_fit_all_runs_periodic_cut/pred_vs_fit_{ dim } _{ edge_length } .png"
150
160
)
@@ -159,14 +169,14 @@ def std_error_by_size(dim, edge_lengths, num_runs=5, start_idx=0, std_not_cls=Tr
159
169
stds = np .array (stds ).sum (axis = 0 ) / num_runs
160
170
n_voxels = np .array ([edge ** int (dim [0 ]) for edge in edge_lengths ])
161
171
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 )
163
173
# img_sizes = [(l,)*2 for l in edge_lengths]
164
174
# pfs, irs = [0.1, 0.2, 0.4], [40, 40, 40]
165
175
# for i in range(len(pfs)):
166
176
# erros_inherent = util.bernouli(pfs[i], util.ns_from_dims(img_sizes, irs[i]),conf=0.95)
167
177
# plt.plot(edge_lengths, erros_inherent, label=f'Inherent error IR = {irs[i]}, pf = {pfs[i]}')
168
178
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 )
170
180
return prediction_error , stds
171
181
172
182
@@ -197,8 +207,8 @@ def mape(slope):
197
207
198
208
199
209
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" ]
202
212
slopes = []
203
213
stds = []
204
214
for edge_length in edge_lengths_pred :
0 commit comments