From b70628b3b9a1969dd6de67fc29ceab992624586e Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Fri, 2 Aug 2024 11:52:05 -0400 Subject: [PATCH 1/4] Avoid lamdbda flake8 erorr with private function --- diffpy/snmf/containers.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/diffpy/snmf/containers.py b/diffpy/snmf/containers.py index 41bf03f..ed8c967 100644 --- a/diffpy/snmf/containers.py +++ b/diffpy/snmf/containers.py @@ -25,6 +25,23 @@ def __init__(self, grid, number_of_signals, id_number, perturbation=1e-3): self.stretching_factors = np.ones(number_of_signals) + np.random.randn(number_of_signals) * perturbation self.id = int(id_number) + def _interpolate_stretching_factor(self, stretching_factor): + """Interpolates the intensity values over a normalized grid scaled by a given stretching factor. + + Parameters + ---------- + stretching_factor : float + The factor by which to stretch the grid. + + Returns + ------- + NDArray[float64] + The interpolated values over the stretched grid. + """ + + normalized_grid = np.arange(len(self.grid)) + return np.interp(normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0) + def apply_stretch(self, m): """Applies a stretching factor to a component @@ -39,19 +56,11 @@ def apply_stretch(self, m): The tuple of vectors where one vector is the stretched component, one vector is the 1st derivative of the stretching operation, and one vector is the second derivative of the stretching operation. """ - normalized_grid = np.arange(len(self.grid)) - # func = lambda stretching_factor: np.interp( - # normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0 - # ) - - # E731 do not assign a lambda expression, use a def - def func(stretching_factor): - return np.interp(normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0) - derivative_func = numdifftools.Derivative(func) + derivative_func = numdifftools.Derivative(self._interpolate_stretching_factor) second_derivative_func = numdifftools.Derivative(derivative_func) - stretched_component = func(self.stretching_factors[m]) + stretched_component = self._interpolate_stretching_factor(self.stretching_factors[m]) stretched_component_gra = derivative_func(self.stretching_factors[m]) stretched_component_hess = second_derivative_func(self.stretching_factors[m]) From 9ca7caf84ac951e3e36cc176c664359ef119091a Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Fri, 2 Aug 2024 11:55:39 -0400 Subject: [PATCH 2/4] Fix return description of private func --- diffpy/snmf/containers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffpy/snmf/containers.py b/diffpy/snmf/containers.py index ed8c967..b85bf70 100644 --- a/diffpy/snmf/containers.py +++ b/diffpy/snmf/containers.py @@ -36,7 +36,7 @@ def _interpolate_stretching_factor(self, stretching_factor): Returns ------- NDArray[float64] - The interpolated values over the stretched grid. + Interpolated values of the self.iq values over the grid that has been adjusted by the stretching_factor """ normalized_grid = np.arange(len(self.grid)) From 1243f1a40d7530b44d656fc2d6789a8f309cba39 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Tue, 6 Aug 2024 21:51:38 -0400 Subject: [PATCH 3/4] Add noqa: E731 for lambda func --- diffpy/snmf/containers.py | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/diffpy/snmf/containers.py b/diffpy/snmf/containers.py index b85bf70..0440515 100644 --- a/diffpy/snmf/containers.py +++ b/diffpy/snmf/containers.py @@ -8,7 +8,7 @@ class ComponentSignal: ---------- grid: 1d array of floats The vector containing the grid points of the component. - + iq: 1d array of floats The intensity/g(r) values of the component. weights: 1d array of floats The vector containing the weight of the component signal for each signal. @@ -25,23 +25,6 @@ def __init__(self, grid, number_of_signals, id_number, perturbation=1e-3): self.stretching_factors = np.ones(number_of_signals) + np.random.randn(number_of_signals) * perturbation self.id = int(id_number) - def _interpolate_stretching_factor(self, stretching_factor): - """Interpolates the intensity values over a normalized grid scaled by a given stretching factor. - - Parameters - ---------- - stretching_factor : float - The factor by which to stretch the grid. - - Returns - ------- - NDArray[float64] - Interpolated values of the self.iq values over the grid that has been adjusted by the stretching_factor - """ - - normalized_grid = np.arange(len(self.grid)) - return np.interp(normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0) - def apply_stretch(self, m): """Applies a stretching factor to a component @@ -53,14 +36,17 @@ def apply_stretch(self, m): Returns ------- tuple of 1d arrays - The tuple of vectors where one vector is the stretched component, one vector is the 1st derivative - of the stretching operation, and one vector is the second derivative of the stretching operation. + The tuple of vectors where one vector is the stretched component, one vector is the 1st derivative of the + stretching operation, and one vector is the second derivative of the stretching operation. """ - - derivative_func = numdifftools.Derivative(self._interpolate_stretching_factor) + normalized_grid = np.arange(len(self.grid)) + func = lambda stretching_factor: np.interp( # noqa: E731 + normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0 + ) + derivative_func = numdifftools.Derivative(func) second_derivative_func = numdifftools.Derivative(derivative_func) - stretched_component = self._interpolate_stretching_factor(self.stretching_factors[m]) + stretched_component = func(self.stretching_factors[m]) stretched_component_gra = derivative_func(self.stretching_factors[m]) stretched_component_hess = second_derivative_func(self.stretching_factors[m]) From ac2d944e170a9d4f81d54c07797c1a2560c1f200 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Tue, 6 Aug 2024 21:53:48 -0400 Subject: [PATCH 4/4] Clarify lambda func name --- diffpy/snmf/containers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/diffpy/snmf/containers.py b/diffpy/snmf/containers.py index 0440515..1af960a 100644 --- a/diffpy/snmf/containers.py +++ b/diffpy/snmf/containers.py @@ -40,13 +40,13 @@ def apply_stretch(self, m): stretching operation, and one vector is the second derivative of the stretching operation. """ normalized_grid = np.arange(len(self.grid)) - func = lambda stretching_factor: np.interp( # noqa: E731 + interpolate_intensity = lambda stretching_factor: np.interp( # noqa: E731 normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0 ) - derivative_func = numdifftools.Derivative(func) + derivative_func = numdifftools.Derivative(interpolate_intensity) second_derivative_func = numdifftools.Derivative(derivative_func) - stretched_component = func(self.stretching_factors[m]) + stretched_component = interpolate_intensity(self.stretching_factors[m]) stretched_component_gra = derivative_func(self.stretching_factors[m]) stretched_component_hess = second_derivative_func(self.stretching_factors[m])