From 9169b1565714fbba14a14cf900f6e2277fc14a18 Mon Sep 17 00:00:00 2001 From: Sosnowsky Date: Fri, 23 Feb 2024 18:16:58 +0100 Subject: [PATCH] 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