From ea65ead1887e44f7206fb05b3562dd0f8b5fb864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Sierro?= Date: Mon, 29 Apr 2024 08:55:23 +0200 Subject: [PATCH] formatting --- src/scgenerator/cache.py | 1 + src/scgenerator/noise.py | 16 ++++++++++++---- src/scgenerator/physics/fiber.py | 18 +++--------------- src/scgenerator/spectra.py | 20 ++++++++------------ 4 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/scgenerator/cache.py b/src/scgenerator/cache.py index 4f0f51a..61ff949 100644 --- a/src/scgenerator/cache.py +++ b/src/scgenerator/cache.py @@ -107,6 +107,7 @@ class Cache: return data wrapped.cached_only = cached_only + wrapped.func = func return wrapped diff --git a/src/scgenerator/noise.py b/src/scgenerator/noise.py index 071d83a..3cb273f 100644 --- a/src/scgenerator/noise.py +++ b/src/scgenerator/noise.py @@ -259,20 +259,28 @@ class NoiseMeasurement: time = np.arange(len(signal)) / fs return time, signal - def root_integrated(self) -> np.ndarray: + def root_integrated(self, from_right: bool = True) -> np.ndarray: """ returns the sqrt of the integrated spectrum The 0th component is the total RIN in the frequency range covered by the measurement Caution! this may be 0 frequency + + Parameters + ---------- + from_right : bool, optional + start integration at f[-1], by default True. """ - return integrated_noise(self.freq, self.psd) + return integrated_noise(self.freq, self.psd, from_right) -def integrated_noise(freq: np.ndarray, psd: np.ndarray) -> float: +def integrated_noise(freq: np.ndarray, psd: np.ndarray, from_right: bool = True) -> np.ndarray: """ given a normalized spectrum, computes the total rms RIN in the provided frequency window """ - return np.sqrt(cumulative_trapezoid(np.abs(psd)[::-1], -freq[::-1], initial=0)[::-1]) + s = slice(None, None, -1) if from_right else slice(None) + return np.sqrt( + cumulative_trapezoid(np.abs(psd)[s], (1 - 2 * from_right) * freq[s], initial=0)[s] + ) def quantum_noise_limit(wavelength: float, power: float) -> float: diff --git a/src/scgenerator/physics/fiber.py b/src/scgenerator/physics/fiber.py index 0e774ca..c1d3912 100644 --- a/src/scgenerator/physics/fiber.py +++ b/src/scgenerator/physics/fiber.py @@ -58,9 +58,7 @@ def dispersion_parameter_to_beta2(dispersion_parameter: float, wavelength: T) -> def dispersion_slope_to_beta3( dispersion_parameter: float, dispersion_slope: float, wavelength ) -> float: - return (wavelength**2 / pi2c) ** 2 * ( - dispersion_slope + 2 * dispersion_parameter / wavelength - ) + return (wavelength**2 / pi2c) ** 2 * (dispersion_slope + 2 * dispersion_parameter / wavelength) def handle_dispersion_parameter( @@ -598,18 +596,8 @@ def saitoh_paramters(pcf_pitch_ratio: float) -> tuple[float, float]: di2 = np.array([9, 6.58, 10, 0.41]) di3 = np.array([10, 24.8, 15, 6]) - A = ( - ai0 - + ai1 * pcf_pitch_ratio**bi1 - + ai2 * pcf_pitch_ratio**bi2 - + ai3 * pcf_pitch_ratio**bi3 - ) - B = ( - ci0 - + ci1 * pcf_pitch_ratio**di1 - + ci2 * pcf_pitch_ratio**di2 - + ci3 * pcf_pitch_ratio**di3 - ) + A = ai0 + ai1 * pcf_pitch_ratio**bi1 + ai2 * pcf_pitch_ratio**bi2 + ai3 * pcf_pitch_ratio**bi3 + B = ci0 + ci1 * pcf_pitch_ratio**di1 + ci2 * pcf_pitch_ratio**di2 + ci3 * pcf_pitch_ratio**di3 return A, B diff --git a/src/scgenerator/spectra.py b/src/scgenerator/spectra.py index 072d953..27dd5b6 100644 --- a/src/scgenerator/spectra.py +++ b/src/scgenerator/spectra.py @@ -179,20 +179,13 @@ class Spectrum(np.ndarray): @property def wl_amp(self): - return ( - np.sqrt( - units.to_WL( - math.abs2(self), - self.l, - ) - ) - * self - / np.abs(self) - )[..., self.l_order] + return (np.sqrt(units.to_WL(math.abs2(self), self.l)) * self / np.abs(self))[ + ..., self.l_order + ] @property def afreq_amp(self): - return self[..., self.w_order[::-1]] * self.spectrum_factor + return self[..., self.w_order] * self.spectrum_factor @property def time_amp(self): @@ -242,7 +235,10 @@ class Spectrum(np.ndarray): band = peak * np.exp(-(((self.l - pos) / (pulse.fwhm_to_T0_fac["gaussian"] * width)) ** 2)) if shot_noise: sn = np.reshape( - [pulse.shot_noise(self.w, *shot_noise) for _ in range(np.prod(self.shape[:-1]))], + [ + pulse.shot_noise(self.w, *shot_noise) + for _ in range(np.prod(self.shape[:-1] + (1,))) + ], self.shape, ) return self * band + np.sqrt(1 - band**2) * sn