Untitled
unknown
plain_text
a year ago
2.2 kB
1
Indexable
Never
def normalize(self, norm = "highest", shift_to_zero = True, inplace = True): ''' Normalize spectrum by linearly changing values of intensity and eventually shifting spectrum to zero. ARGUMENTS: by - way of normalizing the spectrum. If \"highest\", then the spectrum is scaled to 1, so that its greatest value is 1. If \"intensity\", then spectrum is scaled, so that its integral is equal to 1. shift_to_zero - if \"True\", then spectrum is shifted by X axis, by simply np.rolling it. ''' import numpy as np if norm not in ["sup", "L1", "L2"]: raise Exception("\"norm\" parameter must be either \"sup\", \"L1\" or \"L2\".") X_safe = self.X.copy() Y_safe = self.Y.copy() safe_spectrum = spectrum(X_safe, Y_safe, self.x_type, self.y_type) if norm == "sup": max = np.max(np.abs(Y_safe)) max_idx = np.argmax(np.abs(Y_safe)) Y_safe /= max if shift_to_zero: zero_idx = np.searchsorted(X_safe, 0) shift_idx = max_idx - zero_idx X_safe = np.roll(X_safe, shift_idx) if norm == "L1": integral = np.sum(np.abs(Y_safe))/(X_safe[-1]-X_safe[0]) median = safe_spectrum.quantile(1/2) max_idx = np.searchsorted(np.abs(Y_safe), median) Y_safe /= integral if shift_to_zero: zero_idx = np.searchsorted(X_safe, 0) shift_idx = max_idx - zero_idx X_safe = np.roll(X_safe, shift_idx) if norm == "L2": integral = np.sum(Y_safe**2)/(X_safe[-1]-X_safe[0]) median = safe_spectrum.quantile(1/2) max_idx = np.searchsorted(np.abs(Y_safe), median) Y_safe /= np.sqrt(integral) if shift_to_zero: zero_idx = np.searchsorted(X_safe, 0) shift_idx = max_idx - zero_idx X_safe = np.roll(X_safe, shift_idx) if inplace == True: self.X = X_safe self.Y = Y_safe if inplace == False: return spectrum(X_safe, Y_safe, self.x_type, self.y_type)