added Spectrum export/import to bytes

This commit is contained in:
Benoît Sierro
2024-01-12 11:36:02 +01:00
parent db77bef2af
commit 19acb550e7
2 changed files with 76 additions and 1 deletions

View File

@@ -6,6 +6,33 @@ from scgenerator.physics.units import m_rads
from scgenerator.spectra import Spectrum
def test_export():
t = np.linspace(-1e-12, 1e-12, 1024)
w = wspace(t) + m_rads(800e-9)
spec = np.fft.fft(np.exp(-((t / 1e-13) ** 2)))
spec = Spectrum(spec, w, t)
new_spec = Spectrum.from_bytes(bytes(spec))
assert np.all(new_spec.w == spec.w)
assert np.all(new_spec.t == spec.t)
assert np.all(new_spec.l == spec.l)
assert np.all(new_spec == spec)
assert new_spec.ifft is spec.ifft
t = np.linspace(-1e-12, 1e-12, 512)
w = np.fft.rfftfreq(len(t), d=t[1] - t[0]) * 2 * np.pi + m_rads(800e-9)
spec = np.exp(2j * np.pi * np.random.rand(4, 2, 5, 257))
spec = Spectrum(spec, w, t, np.fft.irfft)
new_spec = Spectrum.from_bytes(bytes(spec))
assert np.all(new_spec.w == spec.w)
assert np.all(new_spec.t == spec.t)
assert np.all(new_spec.l == spec.l)
assert np.all(new_spec == spec)
assert new_spec.ifft is spec.ifft
def test_center_gravity():
t = np.linspace(-1e-12, 1e-12, 1024)
w = wspace(t) + m_rads(800e-9)