Skip to content

Commit

Permalink
add apply_range() and replace load_profile() by preprocess()
Browse files Browse the repository at this point in the history
  • Loading branch information
patquem committed Aug 7, 2024
1 parent bc62557 commit 64005aa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
4 changes: 3 additions & 1 deletion examples/ex_nogui_auto_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def auto_decomposition(verbosity=True, show_plots=False):
spectra_list = []
for fname in fnames:
spectrum = Spectrum()
spectrum.load_profile(fname, xmin=55)
spectrum.load_profile(fname)
spectrum.apply_range(range_min=55)
spectrum.auto_baseline()
spectrum.subtract_baseline()
spectrum.auto_peaks(model_name="Lorentzian")
Expand Down Expand Up @@ -54,6 +55,7 @@ def auto_decomposition(verbosity=True, show_plots=False):

# Fitted spectra
ax1.set_title('Flattened + Attractors + Fitted')
spectrum.preprocess()
spectrum.plot(ax=ax1)
ax1.legend()

Expand Down
13 changes: 6 additions & 7 deletions fitspy/app/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,8 @@ def set_spectrum_range(self, delete_tabview=True):
self.current_spectrum.range_max = float(self.range_max.get())

self.remove(delete_tabview=delete_tabview)
self.current_spectrum.load_profile(self.current_spectrum.fname)
self.update_attractors()
self.current_spectrum.preprocess()
self.plot()

def set_range(self):
""" Set range from the spectrum to the appli """
Expand All @@ -766,9 +766,7 @@ def apply_range_to_all(self):
for spectrum in self.spectra.all:
spectrum.range_max = range_max

self.current_spectrum.load_profile(self.current_spectrum.fname)
self.update_attractors()

self.current_spectrum.preprocess()
self.paramsview.delete()
self.statsview.delete()
self.ax.clear()
Expand Down Expand Up @@ -942,8 +940,9 @@ def add_items(self, fnames=None):
fname_first_item = fname

spectrum = Spectrum()
spectrum.load_profile(fname)
spectrum.fname = fname
spectrum.attractors_params = attractors_params
spectrum.preprocess()
self.spectra.append(spectrum)

self.update(fname=fname_first_item or self.fileselector.filenames[0])
Expand Down Expand Up @@ -988,7 +987,7 @@ def update(self, fname=None):
self.update_markers(fname)

self.current_spectrum, _ = self.spectra.get_objects(fname)
self.current_spectrum.load_profile(fname)
self.current_spectrum.preprocess()

self.show_plot = False
self.set_range()
Expand Down
41 changes: 22 additions & 19 deletions fitspy/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,18 @@ def set_attributes(self, model_dict):

def preprocess(self):
""" Preprocess the spectrum: call successively load_profile(),
subtract_baseline() and normalize() """

apply_range(), attractors_calculation(), subtract_baseline() and
normalize() """
self.load_profile(self.fname)
self.apply_range()
self.attractors_calculation()
self.baseline.is_subtracted = False
self.subtract_baseline()
self.normalize()

def load_profile(self, fname, xmin=None, xmax=None):
def load_profile(self, fname):
""" Load profile from 'fname' with 1 header line and 2 (x,y) columns"""

# raw profile loading
if self.x0 is None:
x0, y0 = get_1d_profile(fname)

Expand All @@ -247,22 +248,26 @@ def load_profile(self, fname, xmin=None, xmax=None):

self.fname = fname

if xmin is not None:
self.range_min = xmin
if xmax is not None:
self.range_max = xmax
self.x = self.x0.copy()
self.y = self.y0.copy()

if self.range_min is None and self.range_max is None:
self.x = self.x0.copy()
self.y = self.y0.copy()
else:
ind_min, ind_max = 0, len(self.x0)
def apply_range(self, range_min=None, range_max=None):
""" Apply range to the raw spectrum (and the baseline.y_eval) """

self.range_min = range_min or self.range_min
self.range_max = range_max or self.range_max

if self.range_min is not None or self.range_max is not None:
ind_min, ind_max = 0, len(self.x)
if self.range_min is not None:
ind_min = closest_index(self.x0, self.range_min)
ind_min = closest_index(self.x, self.range_min)
if self.range_max is not None:
ind_max = closest_index(self.x0, self.range_max)
self.x = self.x0[ind_min:ind_max + 1].copy()
self.y = self.y0[ind_min:ind_max + 1].copy()
ind_max = closest_index(self.x, self.range_max)

self.x = self.x[ind_min:ind_max + 1]
self.y = self.y[ind_min:ind_max + 1]
if self.baseline.y_eval is not None:
self.baseline.y_eval = self.baseline.y_eval[ind_min:ind_max + 1]

def calculate_outliers(self):
""" Return outliers points (x,y) coordinates """
Expand Down Expand Up @@ -695,8 +700,6 @@ def plot(self, ax,
linestyles='dashed', lw=0.5, label="Noise level")

if show_baseline and self.baseline.is_subtracted:
if self.baseline.y_eval is None:
self.baseline.eval(x, self.y_no_outliers)
ax.plot(x, self.baseline.y_eval, 'g', label="Baseline")

y_bkg = np.zeros_like(x)
Expand Down

0 comments on commit 64005aa

Please sign in to comment.