Skip to content

Commit 269a7b1

Browse files
committed
I made the iterator respect the dlamda_min/dlamda_max when you pass them with lamda_list
1 parent 45d86ea commit 269a7b1

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

helpers.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,25 @@ def extrap(lamda, n, kind = 'linear'):
1919
'''Requires that lamda be in increasing order'''
2020
upper_value = n[-1]
2121
lower_value = n[0]
22-
from scipy.interpolate import interp1d
23-
interp1d.upper_bound = 0.0
24-
interp1d.lower_bound = 0.0
22+
from scipy.interpolate import interp1d, BSpline
2523
def is_in_bounds(self,lamda):
2624
if (self.lower_bound <= lamda) and (lamda <= self.upper_bound):
2725
return True
2826
else:
2927
return False
30-
interp1d.is_in_bounds = is_in_bounds
28+
3129
# now we instantiate
32-
func = interp1d(lamda, n, kind=kind, bounds_error = False, fill_value = (lower_value, upper_value))
30+
if kind != 'cubic_bspline':
31+
interp1d.upper_bound = 0
32+
interp1d.lower_bound = 0
33+
interp1d.is_in_bounds = is_in_bounds
34+
func = interp1d(lamda, n, kind=kind, bounds_error = False, fill_value = (lower_value, upper_value))
35+
else:
36+
BSpline.upper_bound = 0
37+
BSpline.lower_bound = 0
38+
BSpline.is_in_bounds = is_in_bounds
39+
func = BSpline(lamda, n, k = 3, extrapolate = True)
40+
3341
func.upper_bound = max(lamda)
3442
func.lower_bound = min(lamda)
3543
return func

iterator.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def error_adaptive_iterative_fit_spectra(
2828

2929
from TRANK import (fit_spectra_nk_sqr, fit_spectra_nk_sqr_KK_compliant,
3030
rms_error_spectrum, reducible_rms_error_spectrum, nk_plot, error_plot, try_mkdir)
31+
from TRANK import compute_coarse_and_fine_grid
3132
from time import time
3233
from numpy import floor, log2, ceil, linspace, diff, sqrt, mean, array, savetxt, percentile, argsort
3334
from copy import deepcopy
@@ -37,7 +38,6 @@ def error_adaptive_iterative_fit_spectra(
3738
from matplotlib.pylab import show
3839

3940
if reuse_mode == False: #picks lambda points accordingly
40-
from TRANK import compute_coarse_and_fine_grid
4141
lamda_list, lamda_fine = compute_coarse_and_fine_grid(dlamda_max, dlamda_min, lamda_max, lamda_min)
4242
dlamda_min = lamda_fine[1]-lamda_fine[0]
4343
dlamda_max = lamda_list[1]-lamda_list[0]
@@ -51,19 +51,14 @@ def error_adaptive_iterative_fit_spectra(
5151

5252

5353
else:
54-
54+
lamda_coarse, lamda_fine = compute_coarse_and_fine_grid(dlamda_max, dlamda_min, lamda_max, lamda_min)
5555
# Determines the fine grid based on the smallest delta lambda you have
5656
dlamda_min_found = min(diff(lamda_list))
5757
power_of_2 = int(round( log2(dlamda_min_found/dlamda_min) ))
5858
#print( log2(dlamda_min_found/dlamda_min) )
59-
dlamda_min = dlamda_min_found/(2**power_of_2) # finds power of two spacing to match your dlamda_min
60-
61-
### use what we find
62-
dlamda_max = dlamda_max_found = max(diff(lamda_list))
63-
64-
nfine = ceil((lamda_max - lamda_min)/dlamda_min)
65-
lamda_fine = linspace(lamda_min, lamda_max, nfine+1)
66-
#print ('lamda_fine', lamda_fine)
59+
if False: # in the past it made sense to retrive the dlamda_min and max from data
60+
dlamda_min = dlamda_min_found/(2**power_of_2) # finds power of two spacing to match your dlamda_min
61+
dlamda_max = dlamda_max_found = max(diff(lamda_list))
6762

6863
if max_passes == 0:
6964
# this part guesses how many passes are required to reach the finest grid level
@@ -172,7 +167,7 @@ def error_adaptive_iterative_fit_spectra(
172167
adaptation_threshold_max = adaptation_threshold_max,
173168
reducible_error_spectrum = reducible_error_spectrum,
174169
file_name = data_directory + rms_spectrum_file_format % pass_number,
175-
title_string = 'Pass %i: Net RMS Error = %.3f %%' %( pass_number, net_rms*100),
170+
title_string = 'Pass %i\nNon-Uniform RMS Error = %.3f %%\nUniform Fine RMS Error = %.3f %%' %( pass_number, net_rms*100, net_rms_fine*100),
176171
show_plots = show_plots )
177172

178173
nk_fig = nk_plot(lamda_list = lamda_list, lamda_fine = lamda_fine, nkf = fit_nk_f,
@@ -189,7 +184,7 @@ def error_adaptive_iterative_fit_spectra(
189184
adaptation_spectrum = rms_spectrum
190185

191186

192-
refinement_method = 'interpolate_and_check_all'
187+
refinement_method = 'near_worst'
193188
#### with our adaptation selection method set, we find new points
194189
if refinement_method == 'near_worst':
195190
new_lamda_list = []
@@ -231,9 +226,17 @@ def error_adaptive_iterative_fit_spectra(
231226
if ( (num_new_points + len(lamda_list)) > max_points):
232227
n_delete = num_new_points+len(lamda_list) - max_points
233228
sorted_indices = argsort(adaptation_spectrum)
229+
### remove edge indices
234230
sorted_indices_without_edge_values = list(sorted_indices)
235231
sorted_indices_without_edge_values.remove(0)
236232
sorted_indices_without_edge_values.remove(len(adaptation_spectrum)-1)
233+
234+
# now we remove any that would make a gap that is too large
235+
for index_index in range(len(sorted_indices_without_edge_values)-1,-1,-1):
236+
index_to_check = sorted_indices_without_edge_values[index_index]
237+
if (lamda_list[index_to_check+1] - lamda_list[index_to_check-1]) > dlamda_max:
238+
del sorted_indices_without_edge_values[index_index] # we can't consider it, would make a large gap
239+
237240
indicies_to_delete = sorted_indices_without_edge_values[0:n_delete]
238241
indicies_to_delete.sort(reverse = True)
239242

0 commit comments

Comments
 (0)