From 6f3a8e0a27c319d77188945bf8376c59f5614eaa Mon Sep 17 00:00:00 2001 From: Sosnowsky Date: Fri, 23 Feb 2024 17:42:16 +0100 Subject: [PATCH 1/5] black --- tests/test_blob.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_blob.py b/tests/test_blob.py index c2a2260..db91ae4 100644 --- a/tests/test_blob.py +++ b/tests/test_blob.py @@ -101,9 +101,7 @@ def test_single_point(): x=mesh_x, y=mesh_y, t=0, periodic_y=True, Ly=10 ) - expected_values = ( - 1 / (2 * np.pi) * np.exp(-(mesh_x**2) / 2) * np.exp(-(4**2) / 2) - ) + expected_values = 1 / (2 * np.pi) * np.exp(-(mesh_x**2) / 2) * np.exp(-(4**2) / 2) error = np.max(abs(expected_values - blob_values)) assert error < 1e-8, "Numerical error too big" From 24fb4359fc4e8b24f45ba46db6784f29684e9af6 Mon Sep 17 00:00:00 2001 From: Sosnowsky Date: Fri, 23 Feb 2024 18:01:54 +0100 Subject: [PATCH 2/5] Add selves --- blobmodel/blob_shape.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/blobmodel/blob_shape.py b/blobmodel/blob_shape.py index df09a3e..a2772d6 100644 --- a/blobmodel/blob_shape.py +++ b/blobmodel/blob_shape.py @@ -77,7 +77,7 @@ def get_blob_shape_perp(self, theta: np.ndarray, **kwargs) -> np.ndarray: """ raise NotImplementedError - def _get_exponential_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + def _get_exponential_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: """Compute the exponential pulse shape. Parameters @@ -94,7 +94,7 @@ def _get_exponential_shape(theta: np.ndarray, **kwargs) -> np.ndarray: """ return np.exp(theta) * np.heaviside(-1.0 * theta, 1) - def _get_lorentz_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + def _get_lorentz_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: """Compute the Lorentzian pulse shape. Parameters @@ -111,7 +111,7 @@ def _get_lorentz_shape(theta: np.ndarray, **kwargs) -> np.ndarray: """ return 1 / (np.pi * (1 + theta**2)) - def _get_double_exponential_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + def _get_double_exponential_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: """Compute the double-exponential pulse shape. Parameters @@ -135,7 +135,7 @@ def _get_double_exponential_shape(theta: np.ndarray, **kwargs) -> np.ndarray: kern[theta >= 0] = np.exp(-theta[theta >= 0] / (1 - lam)) return kern - def _get_gaussian_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + def _get_gaussian_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: """Compute the Gaussian pulse shape. Parameters @@ -152,7 +152,7 @@ def _get_gaussian_shape(theta: np.ndarray, **kwargs) -> np.ndarray: """ return 1 / np.sqrt(2 * np.pi) * np.exp(-(theta**2) / 2) - def _get_secant_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + def _get_secant_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: """Compute the secant pulse shape. Parameters @@ -169,7 +169,7 @@ def _get_secant_shape(theta: np.ndarray, **kwargs) -> np.ndarray: """ return 2 / np.pi / (np.exp(theta) + np.exp(-theta)) - def _get_dipole_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + def _get_dipole_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: """Compute the diople pulse shape as a derivative of a gaussian pulse shape. Parameters From 9169b1565714fbba14a14cf900f6e2277fc14a18 Mon Sep 17 00:00:00 2001 From: Sosnowsky Date: Fri, 23 Feb 2024 18:16:58 +0100 Subject: [PATCH 3/5] Try this --- blobmodel/blob_shape.py | 224 +++++++++++++++++++------------------- tests/test_pulse_shape.py | 2 +- 2 files changed, 116 insertions(+), 110 deletions(-) diff --git a/blobmodel/blob_shape.py b/blobmodel/blob_shape.py index a2772d6..f62f623 100644 --- a/blobmodel/blob_shape.py +++ b/blobmodel/blob_shape.py @@ -20,6 +20,121 @@ def get_blob_shape_perp(self, theta: np.ndarray, **kwargs) -> np.ndarray: raise NotImplementedError +def _get_exponential_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + """Compute the exponential pulse shape. + + Parameters + ---------- + theta : np.ndarray + Array of theta values. + kwargs + Additional keyword arguments. + + Returns + ------- + np.ndarray + Array representing the exponential pulse shape. + """ + return np.exp(theta) * np.heaviside(-1.0 * theta, 1) + + +def _get_lorentz_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + """Compute the Lorentzian pulse shape. + + Parameters + ---------- + theta : np.ndarray + Array of theta values. + kwargs + Additional keyword arguments. + + Returns + ------- + np.ndarray + Array representing the Lorentzian pulse shape. + """ + return 1 / (np.pi * (1 + theta**2)) + + +def _get_double_exponential_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + """Compute the double-exponential pulse shape. + + Parameters + ---------- + theta : np.ndarray + Array of theta values. + kwargs + Additional keyword arguments. + lam : float + Asymmetry parameter controlling the shape. + + Returns + ------- + np.ndarray + Array representing the double-exponential pulse shape. + """ + lam = kwargs["lam"] + assert (lam > 0.0) & (lam < 1.0) + kern = np.zeros(shape=np.shape(theta)) + kern[theta < 0] = np.exp(theta[theta < 0] / lam) + kern[theta >= 0] = np.exp(-theta[theta >= 0] / (1 - lam)) + return kern + + +def _get_gaussian_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + """Compute the Gaussian pulse shape. + + Parameters + ---------- + theta : np.ndarray + Array of theta values. + kwargs + Additional keyword arguments. + + Returns + ------- + np.ndarray + Array representing the Gaussian pulse shape. + """ + return 1 / np.sqrt(2 * np.pi) * np.exp(-(theta**2) / 2) + + +def _get_secant_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + """Compute the secant pulse shape. + + Parameters + ---------- + theta : np.ndarray + Array of theta values. + kwargs + Additional keyword arguments. + + Returns + ------- + np.ndarray + Array representing the secant pulse shape. + """ + return 2 / np.pi / (np.exp(theta) + np.exp(-theta)) + + +def _get_dipole_shape(theta: np.ndarray, **kwargs) -> np.ndarray: + """Compute the diople pulse shape as a derivative of a gaussian pulse shape. + + Parameters + ---------- + theta : np.ndarray + Array of theta values. + kwargs + Additional keyword arguments. + + Returns + ------- + np.ndarray + Array representing the dipole pulse shape. + """ + return -2 * theta / np.sqrt(2 * np.pi) * np.exp(-(theta**2) / 2) + + class BlobShapeImpl(AbstractBlobShape): """Implementation of the AbstractPulseShape class.""" @@ -77,115 +192,6 @@ def get_blob_shape_perp(self, theta: np.ndarray, **kwargs) -> np.ndarray: """ raise NotImplementedError - def _get_exponential_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: - """Compute the exponential pulse shape. - - Parameters - ---------- - theta : np.ndarray - Array of theta values. - kwargs - Additional keyword arguments. - - Returns - ------- - np.ndarray - Array representing the exponential pulse shape. - """ - return np.exp(theta) * np.heaviside(-1.0 * theta, 1) - - def _get_lorentz_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: - """Compute the Lorentzian pulse shape. - - Parameters - ---------- - theta : np.ndarray - Array of theta values. - kwargs - Additional keyword arguments. - - Returns - ------- - np.ndarray - Array representing the Lorentzian pulse shape. - """ - return 1 / (np.pi * (1 + theta**2)) - - def _get_double_exponential_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: - """Compute the double-exponential pulse shape. - - Parameters - ---------- - theta : np.ndarray - Array of theta values. - kwargs - Additional keyword arguments. - lam : float - Asymmetry parameter controlling the shape. - - Returns - ------- - np.ndarray - Array representing the double-exponential pulse shape. - """ - lam = kwargs["lam"] - assert (lam > 0.0) & (lam < 1.0) - kern = np.zeros(shape=np.shape(theta)) - kern[theta < 0] = np.exp(theta[theta < 0] / lam) - kern[theta >= 0] = np.exp(-theta[theta >= 0] / (1 - lam)) - return kern - - def _get_gaussian_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: - """Compute the Gaussian pulse shape. - - Parameters - ---------- - theta : np.ndarray - Array of theta values. - kwargs - Additional keyword arguments. - - Returns - ------- - np.ndarray - Array representing the Gaussian pulse shape. - """ - return 1 / np.sqrt(2 * np.pi) * np.exp(-(theta**2) / 2) - - def _get_secant_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: - """Compute the secant pulse shape. - - Parameters - ---------- - theta : np.ndarray - Array of theta values. - kwargs - Additional keyword arguments. - - Returns - ------- - np.ndarray - Array representing the secant pulse shape. - """ - return 2 / np.pi / (np.exp(theta) + np.exp(-theta)) - - def _get_dipole_shape(self, theta: np.ndarray, **kwargs) -> np.ndarray: - """Compute the diople pulse shape as a derivative of a gaussian pulse shape. - - Parameters - ---------- - theta : np.ndarray - Array of theta values. - kwargs - Additional keyword arguments. - - Returns - ------- - np.ndarray - Array representing the dipole pulse shape. - """ - return -2 * theta / np.sqrt(2 * np.pi) * np.exp(-(theta**2) / 2) - __GENERATORS = { "exp": _get_exponential_shape, "gauss": _get_gaussian_shape, diff --git a/tests/test_pulse_shape.py b/tests/test_pulse_shape.py index 688bdcc..2c2dc95 100644 --- a/tests/test_pulse_shape.py +++ b/tests/test_pulse_shape.py @@ -1,5 +1,5 @@ import pytest -from blobmodel import BlobShapeImpl, AbstractBlobShape, BlobShapeImpl +from blobmodel import AbstractBlobShape, BlobShapeImpl import numpy as np From 4936346a72668191b02c795eec1c4718b8ad64ff Mon Sep 17 00:00:00 2001 From: Sosnowsky Date: Fri, 23 Feb 2024 18:22:29 +0100 Subject: [PATCH 4/5] Fix test --- tests/test_pulse_shape.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pulse_shape.py b/tests/test_pulse_shape.py index 2c2dc95..a79a948 100644 --- a/tests/test_pulse_shape.py +++ b/tests/test_pulse_shape.py @@ -47,9 +47,9 @@ def test__get_double_exponential_shape(): theta = np.array([-1, 0, 1]) lam = 0.5 expected_result = np.array([0.13533528, 1.0, 0.13533528]) - assert np.allclose( - BlobShapeImpl._get_double_exponential_shape(theta, lam=lam), expected_result - ) + ps = BlobShapeImpl("2-exp", "2-exp") + values = ps.get_blob_shape_perp(theta, lam=lam) + assert np.max(np.abs(values - expected_result)) < 1e-5, "Wrong shape" def test__get_secant_shape(): From b3d668e5a56a746de23300127d0fb2305912e81e Mon Sep 17 00:00:00 2001 From: Sosnowsky Date: Fri, 23 Feb 2024 18:27:07 +0100 Subject: [PATCH 5/5] Fiux --- tests/test_pulse_shape.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/test_pulse_shape.py b/tests/test_pulse_shape.py index a79a948..1d077ab 100644 --- a/tests/test_pulse_shape.py +++ b/tests/test_pulse_shape.py @@ -55,16 +55,25 @@ def test__get_double_exponential_shape(): def test__get_secant_shape(): theta = np.array([1, 2, 3]) expected_result = np.array([0.20628208, 0.08460748, 0.03161706]) - assert np.allclose(BlobShapeImpl._get_secant_shape(theta), expected_result) + + ps = BlobShapeImpl("secant", "secant") + values = ps.get_blob_shape_perp(theta) + assert np.max(np.abs(values - expected_result)) < 1e-5, "Wrong shape" def test__get_lorentz_shape(): theta = np.array([1, 2, 3]) expected_result = np.array([0.15915494, 0.06366198, 0.03183099]) - assert np.allclose(BlobShapeImpl._get_lorentz_shape(theta), expected_result) + + ps = BlobShapeImpl("lorentz", "lorentz") + values = ps.get_blob_shape_perp(theta) + assert np.max(np.abs(values - expected_result)) < 1e-5, "Wrong shape" def test__get_dipole_shape(): theta = np.array([1, 2, 3]) expected_result = np.array([-0.48394145, -0.21596387, -0.02659109]) - assert np.allclose(BlobShapeImpl._get_dipole_shape(theta), expected_result) + + ps = BlobShapeImpl("dipole", "dipole") + values = ps.get_blob_shape_perp(theta) + assert np.max(np.abs(values - expected_result)) < 1e-5, "Wrong shape"