Skip to content

Commit

Permalink
Latest fixes before D4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Barton authored and kerberizer committed Mar 9, 2022
1 parent 07db4f6 commit feb2e08
Show file tree
Hide file tree
Showing 14 changed files with 10,469 additions and 14 deletions.
Binary file added documentation/ramanchada cheat sheet.pdf
Binary file not shown.
Binary file added documentation/ramanchada cheat sheet.pptx
Binary file not shown.
Binary file modified documents/Raman data formats.xlsx
Binary file not shown.
19 changes: 13 additions & 6 deletions ramanchada/src/ramanchada/calibration/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
from copy import deepcopy
from scipy.interpolate import interp1d
from scipy.ndimage import gaussian_filter1d
from ramanchada.file_io.io import read_chada

from ramanchada.analysis.peaks import fit_spectrum_peaks_pos
from ramanchada.utilities import lims


def raman_x_calibration(target_spectrum, reference_peak_list, fitmethod, interpolate=False):
def raman_x_calibration(target_spectrum, reference_peak_list, fitmethod, poly_degree=3, interpolate=False):
# spectrum is a RamanSpectrum
# reference is a list of precise reference x peak positions
# pos must be in x range an non-nan
Expand All @@ -26,9 +27,9 @@ def raman_x_calibration(target_spectrum, reference_peak_list, fitmethod, interpo
target_pos = [target_pos[ii] for ii, cond in enumerate(ind) if not cond]
reference_peak_list = [reference_peak_list[ii] for ii, cond in enumerate(ind) if not cond]
# construct calibration
return construct_calibration(target_pos, np.array(reference_peak_list) - np.array(target_pos), interpolate=interpolate)
return construct_calibration(target_pos, np.array(reference_peak_list) - np.array(target_pos), poly_degree=poly_degree, interpolate=interpolate)

def raman_x_calibration_from_spectrum(target_spectrum, reference_spectrum, fitmethod, peak_pos=[]):
def raman_x_calibration_from_spectrum(target_spectrum, reference_spectrum, fitmethod, peak_pos=[], poly_degree=3, interpolate=False):
# Find peaks in target
# if np peak list is given, locate peaks
if peak_pos == []:
Expand All @@ -40,7 +41,7 @@ def raman_x_calibration_from_spectrum(target_spectrum, reference_spectrum, fitme
# Fit corresponding peaks in reference
reference_pos = fit_spectrum_peaks_pos(reference_spectrum.x,
reference_spectrum.y, peak_pos, fitmethod)[0].tolist()
return raman_x_calibration(target_spectrum, reference_pos, fitmethod)
return raman_x_calibration(target_spectrum, reference_pos, fitmethod, poly_degree=poly_degree, interpolate=interpolate)

def raman_y_calibration_from_spectrum(target_spectrum, reference_spectrum, x_min=-1e9, x_max=1e9):
ref, tar = deepcopy(reference_spectrum), deepcopy(target_spectrum)
Expand All @@ -58,12 +59,18 @@ def raman_y_calibration_from_spectrum(target_spectrum, reference_spectrum, x_min
g, x = gain[ok], tar.x[ok]
return construct_calibration(x, g, x_col_name='Raman shift', y_col_name='y gain', interpolate=False)

def construct_calibration(pos, shifts, x_col_name='Raman shift', y_col_name='RS correction'):
def construct_calibration(pos, shifts, poly_degree=3, x_col_name='Raman shift', y_col_name='RS correction', interpolate=False):
from ramanchada.classes import RamanCalibration
caldata = pd.DataFrame()
caldata[x_col_name] = pos
caldata[y_col_name] = shifts
return RamanCalibration(caldata, interpolate=interpolate)
return RamanCalibration(caldata, poly_degree=poly_degree, interpolate=interpolate)

def read_x_calibration(file_path):
pos, shifts, calibration_metadata, _, _ = read_chada(file_path)
calibration = construct_calibration(pos, shifts, poly_degree=calibration_metadata['Polynomial order'])
calibration.time = calibration_metadata['Calibration time']
return calibration

# def raman_x_calibration_from_spectrum(target_spectrum, reference_spectrum, fitmethod, peak_pos=[]):
# # Find peaks in target
Expand Down
29 changes: 21 additions & 8 deletions ramanchada/src/ramanchada/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
from ramanchada.pre_processing.baseline import baseline_model, xrays
from ramanchada.pre_processing.denoise import smooth_curve
from ramanchada.file_io.io import import_native,\
read_chada, create_chada_from_native, commit_chada
read_chada, create_chada_from_native, commit_chada, write_new_chada
from ramanchada.analysis.peaks import find_spectrum_peaks, fit_spectrum_peaks_pos, find_spectrum_peaks_cwt
from ramanchada.analysis.signal import snr
from ramanchada.utilities import hqi, lims, interpolation_within_bounds, labels_from_filenames
from ramanchada.calibration.calibration import raman_x_calibration, raman_x_calibration_from_spectrum, raman_y_calibration_from_spectrum,\
deconvolve_mtf, relative_ctf, apply_relative_ctf, raman_mtf_from_psfs, extract_xrays
deconvolve_mtf, relative_ctf, apply_relative_ctf, raman_mtf_from_psfs, extract_xrays, construct_calibration


class Curve:
Expand Down Expand Up @@ -330,6 +330,10 @@ def fit_baseline(self, method='als', lam=1e5, p=0.001, niter=100, smooth=7):
"""
self.baseline = baseline_model(self.y, method=method, lam=lam, p=p, niter=niter, smooth=smooth)
@specstyle
def plot_baseline(self):
plt.plot(self.x, self.baseline, 'r-')
plt.plot(self.x, self.y, 'k:')
@log
def remove_baseline(self, method='als', lam=1e5, p=0.001, niter=100, smooth=7):
"""
Expand Down Expand Up @@ -494,7 +498,7 @@ def calibrate(self, calibration):
# Make sure x values remain sorted
inds = self.x.argsort()
self.x, self.y = self.x[inds], self.y[inds]
def make_x_calibration(self, reference, fitmethod = 'voigt', peak_pos=[]):
def make_x_calibration(self, reference, fitmethod = 'voigt', peak_pos=[], poly_degree=3, interpolate=False):
"""
Generate an x axis calibration, either to a RamanSpectrum or a list of exact reference peak positions.
Expand Down Expand Up @@ -525,11 +529,12 @@ def make_x_calibration(self, reference, fitmethod = 'voigt', peak_pos=[]):
# Returns a RamanCalibration
# If a Raman spectrum is given as refernece
if reference.__class__.__name__ in ['RamanSpectrum', 'RamanChada']:
return raman_x_calibration_from_spectrum(self, reference, fitmethod=fitmethod, peak_pos=peak_pos)
return raman_x_calibration_from_spectrum(self, reference, fitmethod=fitmethod, peak_pos=peak_pos,
poly_degree=poly_degree, interpolate=interpolate)
# If a list of peak positions is given
if reference.__class__.__name__ == 'list':
print('list')
return raman_x_calibration(self, reference, fitmethod=fitmethod)
return raman_x_calibration(self, reference, fitmethod=fitmethod, poly_degree=poly_degree, interpolate=interpolate)
else:
return None
@change_y
Expand Down Expand Up @@ -662,6 +667,10 @@ def fit_xrays(self):
"""
self.xrays = xrays(self)
@specstyle
def plot_xrays(self):
plt.plot(self.x, self.xrays, 'r-')
plt.plot(self.x, self.y, 'k:')
@change_y
@log
def remove_xrays(self):
Expand Down Expand Up @@ -1203,10 +1212,8 @@ class RamanCalibration(Curve):
"""
Object containing a Raman x axis calibration.
"""
test_x = np.linspace(0, 3500, 100)
def __init__(self, data, poly_degree=3, interpolate=False):
"""
Parameters
----------
data : DataFrame
Expand Down Expand Up @@ -1237,6 +1244,8 @@ def __init__(self, data, poly_degree=3, interpolate=False):
# Only apply shift within x limits of calibration data
# Set shifts outside the limits to boundary values
self.interp_x = interpolation_within_bounds(self.x, self.y, poly_degree)
self.poly_degree = poly_degree
self.interpolate = interpolate
def show(self):
"""
Plots the calibration data points and the associated model.
Expand All @@ -1247,11 +1256,15 @@ def show(self):
"""
plt.figure()
plt.plot(self.test_x, self.interp_x(self.test_x))
test_x = np.linspace(0, self.x.max(), 100)
plt.plot(test_x, self.interp_x(test_x))
plt.plot(self.x, self.y, 'ko')
plt.xlabel(self.x_label)
plt.ylabel(self.y_label)
plt.show()
def save(self, file_path):
calibration_metadata = {'Calibration time': self.time, 'Polynomial order': self.interp_x.order}
write_new_chada(file_path, self.x, self.y, calibration_metadata)


class RamanMTF(Curve):
Expand Down
Binary file modified ramanchada/src/ramanchada/testdata/200218-17.cha
Binary file not shown.
Loading

0 comments on commit feb2e08

Please sign in to comment.