Skip to content

Commit 2e0a0a4

Browse files
committed
more faffing
1 parent eeca06e commit 2e0a0a4

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

gempy/library/wavecal.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -803,28 +803,38 @@ def find_solution(init_models, config, peaks=None, peak_weights=None,
803803
min_lines_per_fit=min_lines_per_fit,
804804
k=k, bounds_setter=bounds_setter)
805805

806+
wavemin, wavemax = sorted(model(model.domain))
807+
nlines_expected = sum(wavemin <= wline <= wavemax for wline in arc_lines)
808+
806809
# We perform a regular least-squares fit to all the matches
807810
# we've made. This allows a high polynomial order to be
808811
# used without the risk of it going off the rails
809812
matched = np.where(matches > -1)[0]
810813
fit_it = fitting.LinearLSQFitter()
811-
if len(matched) > 1: # need at least 2 lines, right?
812-
m_init = models.Chebyshev1D(degree=min(config["order"], len(matched)-1),
813-
domain=domain)
814+
815+
# We allow this continue if we only matched 1 line but only 1 line is
816+
# expected
817+
if len(matched) > 1: # or (len(matched) * nlines_expected == 1 and len(peaks) <= 2):
818+
m_init = models.Chebyshev1D(
819+
degree=min(config["order"], (len(matched) + 1) // 2),
820+
domain=domain)
814821
for p, v in zip(model.param_names, model.parameters):
815822
if p in m_init.param_names:
816823
setattr(m_init, p, v)
817824
model_bounds = bounds_setter(m_init)
825+
if len(matched) == 1:
826+
m_init.c1.fixed = True
818827
#for i in range(len(matched), m_init.degree + 1):
819828
# m_init.fixed[f"c{i}"] = True
820829
matched_peaks = peaks[matched]
821830
matched_arc_lines = arc_lines[matches[matched]]
822831
m_final = fit_it(m_init, matched_peaks, matched_arc_lines)
832+
print(repr(m_final))
823833
#for p, l in zip(matched_peaks, matched_arc_lines):
824834
# print(f"{p:.2f} => {l:.2f}")
825835

826836
# We're close to the correct solution, perform a KDFit
827-
m_init = models.Chebyshev1D(degree=config["order"], domain=domain)
837+
m_init = models.Chebyshev1D(m_final.degree, domain=domain)
828838
for p, v in zip(m_final.param_names, m_final.parameters):
829839
setattr(m_init, p, v)
830840
dw = abs(np.diff(m_final(m_final.domain))[0] / np.diff(m_final.domain)[0])
@@ -865,12 +875,12 @@ def find_solution(init_models, config, peaks=None, peak_weights=None,
865875
np.array([-0.5, 0.5]) * np.diff(bounds)[0])
866876
assert new_bounds[0] <= v <= new_bounds[1]
867877
except AssertionError:
868-
# for p, v in zip(fit1d.model.param_names, fit1d.model.parameters):
869-
# bounds = model_bounds.get(p, (-np.inf, np.inf))
870-
# new_bounds = (np.asarray(bounds) +
871-
# np.array([-0.5, 0.5]) * np.diff(bounds)[0])
872-
# print(p, v, new_bounds)
873-
# print("*** REJECTED ***")
878+
for p, v in zip(fit1d.model.param_names, fit1d.model.parameters):
879+
bounds = model_bounds.get(p, (-np.inf, np.inf))
880+
new_bounds = (np.asarray(bounds) +
881+
np.array([-0.5, 0.5]) * np.diff(bounds)[0])
882+
print(p, v, new_bounds)
883+
print("*** REJECTED ***")
874884
continue
875885

876886
# Wavelength solution models need to be monotonic. Make that check.
@@ -1013,7 +1023,7 @@ def perform_piecewise_fit(model, peaks, arc_lines, pixel_start, kdsigma,
10131023
# automatically removes old (bad) match
10141024
matches[i] = m
10151025
found_new_matches = True
1016-
#print(f"Pixel {p} => was {m_init(p):.4f} now {m_this(p):.4f} {arc_lines[m]}")
1026+
print(f"Pixel {p} => was {m_init(p):.4f} now {m_this(p):.4f} {arc_lines[m]}")
10171027
try:
10181028
p_lo = peaks[matches > -1].min()
10191029
except ValueError:

0 commit comments

Comments
 (0)