diff --git a/lisainstrument/instrument.py b/lisainstrument/instrument.py index 3c9f4acb05b8cb9623fc20e639edf532ce159781..bce2bbc72997f42eaf1a5a9fefc47920cc6e7848 100644 --- a/lisainstrument/instrument.py +++ b/lisainstrument/instrument.py @@ -17,6 +17,7 @@ from h5py import File from scipy.signal import lfilter, kaiserord, firwin from scipy.interpolate import InterpolatedUnivariateSpline from scipy.integrate import cumulative_trapezoid +from packaging.specifiers import SpecifierSet from lisaconstants import c @@ -487,14 +488,12 @@ class Instrument: self.orbit_file = orbits self.neglect_tps = neglect_tps with File(self.orbit_file, 'r') as orbitf: - if 'version' not in orbitf.attrs: - raise ValueError(f"cannot read version of orbit file '{self.orbit_file}'") - orbits_version = orbitf.attrs['version'] - logger.debug("Using orbit file version %s", orbits_version) - if orbits_version in ['1.0', '1.0.1', '1.0.2']: + version = orbitf.attrs['version'] + logger.debug("Using orbit file version %s", version) + if version in SpecifierSet('~= 1.0'): self.init_orbits_file_1_0(orbitf) else: - raise ValueError(f"unsupported orbit file version '{orbits_version}'") + raise ValueError(f"unsupported orbit file version '{version}'") else: logger.info("Using user-provided proper pseudo-ranges and derivatives thereof") if not self.neglect_tps: @@ -502,13 +501,13 @@ class Instrument: self.orbit_file = None self.neglect_tps = True self.pprs = ForEachMOSA(orbits) - self.d_pprs = self.pprs.transformed(lambda sc, x: + self.d_pprs = self.pprs.transformed(lambda _, x: 0 if np.isscalar(x) else np.gradient(x, self.physics_dt) ) self.tps_proper_time_deviations = ForEachSC(0) def init_orbits_file_1_0(self, orbitf): - """Initialize orbits from an orbit file version 1.0, 1.0.1 or 1.0.2.""" + """Initialize orbits from an orbit file version ~= 1.0.""" def pprs(mosa): if self.neglect_tps: @@ -550,16 +549,14 @@ class Instrument: logger.info("Interpolating gravitational-wave responses from GW file '%s'", gws) self.gw_file = gws with File(self.gw_file, 'r') as gwf: - try: - gwf_version = gwf.attrs['version'] - except KeyError as error: - raise ValueError(f"cannot read version of GW file '{self.gw_file}'") from error - if gwf_version in ['0.1', '1.0']: + version = gwf.attrs['version'] + logger.debug("Using GW file version %s", version) + if version in SpecifierSet('~= 0.1') or version in SpecifierSet('~= 1.0'): self.gws = ForEachMOSA(lambda mosa: InterpolatedUnivariateSpline( gwf['t'][:], gwf[f'l_{mosa}'][:], k=5, ext='zeros')(self.physics_t) ) else: - raise ValueError(f"unsupported GW file version '{gwf_version}'") + raise ValueError(f"unsupported GW file version '{version}'") elif gws is None: logger.debug("No gravitational-wave responses") self.gw_file = None @@ -582,16 +579,21 @@ class Instrument: self.glitch_file = glitches logger.info("Interpolating glitch signals from glitch file '%s'", self.glitch_file) with File(self.glitch_file, 'r') as glitchf: - self.glitch_tms = ForEachMOSA(lambda mosa: - 0 if f'tm_{mosa}' not in glitchf else \ - InterpolatedUnivariateSpline( - glitchf['t'][:], glitchf[f'tm_{mosa}'][:], k=5, ext='const')(self.physics_t) - ) - self.glitch_lasers = ForEachMOSA(lambda mosa: - 0 if f'laser_{mosa}' not in glitchf else \ - InterpolatedUnivariateSpline( - glitchf['t'][:], glitchf[f'laser_{mosa}'][:], k=5, ext='const')(self.physics_t) - ) + version = glitchf.attrs['version'] + logger.debug("Using glitch file version %s", version) + if version in SpecifierSet('~= 1.0'): + self.glitch_tms = ForEachMOSA(lambda mosa: + 0 if f'tm_{mosa}' not in glitchf else \ + InterpolatedUnivariateSpline( + glitchf['t'][:], glitchf[f'tm_{mosa}'][:], k=5, ext='const')(self.physics_t) + ) + self.glitch_lasers = ForEachMOSA(lambda mosa: + 0 if f'laser_{mosa}' not in glitchf else \ + InterpolatedUnivariateSpline( + glitchf['t'][:], glitchf[f'laser_{mosa}'][:], k=5, ext='const')(self.physics_t) + ) + else: + raise ValueError(f"unsupported glitch file version '{version}'") elif glitches is None: logger.debug("No glitches") self.glitch_file = None diff --git a/setup.py b/setup.py index c7421de54d59f3918c5e720bc78058ea2399ef22..f4401b6f276af892fc55c1dea4c9d5490d9387b6 100644 --- a/setup.py +++ b/setup.py @@ -29,6 +29,7 @@ setuptools.setup( 'pyplnoise', 'matplotlib', 'lisaconstants', + 'packaging', ], python_requires='>=3.7', )