def transform(self, X): X = resize_x(X) X_msc = np.zeros_like(X) for i in range(X.shape[0]): fit = np.polyfit(self.reference, X[i, :], 1, full=True) X_msc[i, :] = np.divide((X[i, :] - fit[0][1]), fit[0][0]) return X_msc
def transform(self, X: np.array) -> np.array: """ Remove the mean row wise. :param X: numpy array. :return: numpy array. """ X_val = deepcopy(X) X_val = resize_x(X_val) mean = np.tile(np.mean(X_val, axis=1), (X_val.shape[1], 1)).T X_mean_centering = X_val - mean return X_mean_centering
def transform(self, X: np.array) -> np.array: """ Remove the mean and reduce by the standard deviation row wise. :param X: numpy array. :return: numpy array. """ X_val = deepcopy(X) X_val = resize_x(X_val) mean = np.tile(np.mean(X_val, axis=1), (X_val.shape[1], 1)).T std = np.tile(np.std(X_val, axis=1), (X_val.shape[1], 1)).T X_snv = (X_val - mean) / std return X_snv
def transform(self, X): X_val = resize_x(X) if self.norm == "l1": norm_matrix = np.tile( np.linalg.norm(X_val, ord=1, axis=1, keepdims=True), (1, X_val.shape[1]) ) elif self.norm == "l2": norm_matrix = np.tile( np.linalg.norm(X_val, ord=2, axis=1, keepdims=True), (1, X_val.shape[1]) ) elif self.norm == "inf": norm_matrix = np.tile( np.linalg.norm(X_val, ord=np.inf, axis=1, keepdims=True), (1, X_val.shape[1]), ) X_norm = np.divide(X_val, norm_matrix) return X_norm
def fit(self, X, y=None): """ Set the reference spectrum """ X_val = resize_x(X) if X_val.shape[0] == 1 and y is None: raise ValueError( "A reference spectrum y must be given for X with only one row." ) if y is None: self.reference = np.mean(X, axis=0) else: if y.shape != (1, X_val.shape[1]): raise ValueError( f"The reference must be of shape {(1, X_val.shape[1])} but is ({y.shape})" ) else: self.reference = y return self
def test_resize_x_1d(self): x = np.array([0, 1, 2, 3]) resized = resize_x(x) np.testing.assert_almost_equal(resized, np.array([[0, 1, 2, 3]]))
def test_resize_x_2d(self): x = np.array([[0, 1], [2, 3]]) resized = resize_x(x) np.testing.assert_almost_equal(x, resized)
def test_resize_x_fail(self): with pytest.raises(ArrayDimensionError): resize_x(np.array([[[0], [1], [2]], [[0], [1], [2]]]))