Skip to content
Snippets Groups Projects
test_instrument.py 5.87 KiB
Newer Older
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""Test the Instrument class."""

import os
from tempfile import TemporaryDirectory
import numpy as np
import pytest

from lisainstrument import Instrument


def test_run():
    """Test that simulations can run."""
    with TemporaryDirectory() as temp_dir:
        instru = Instrument(size=100)
        instru.simulate()
        path = os.path.join(temp_dir, "measurements.h5")
        instru.write(path, mode="w")
def test_seeded_run():
    """Test that simulations can run with a seed."""

    instru_1 = Instrument(size=100, seed=42)
    instru_1.simulate()
    instru_2 = Instrument(size=100, seed=42)
    instru_2.simulate()
    instru_3 = Instrument(size=100, seed=43)
    instru_3.simulate()

    with TemporaryDirectory() as temp_dir:
        path = os.path.join(temp_dir, "measurements.h5")
        instru_1.write(path, mode="w")
        with h5py.File(path, "r") as f:
            assert f.attrs["seed"] == 42

    for mosa in Instrument.MOSAS:
        np.testing.assert_array_equal(
            instru_1.testmass_noises[mosa], instru_2.testmass_noises[mosa]
        )
        np.testing.assert_array_equal(
            instru_1.isi_carrier_fluctuations[mosa],
            instru_2.isi_carrier_fluctuations[mosa],
        )
        np.testing.assert_array_equal(instru_1.tmi_usbs[mosa], instru_2.tmi_usbs[mosa])

        assert np.all(instru_1.testmass_noises[mosa] != instru_3.testmass_noises[mosa])
        assert np.all(
            instru_1.isi_carrier_fluctuations[mosa]
            != instru_3.isi_carrier_fluctuations[mosa]
        )
        assert np.all(instru_1.tmi_usbs[mosa] != instru_3.tmi_usbs[mosa])


def test_run_no_aafilter():
    """Test that simulations can run with no filter."""
    with TemporaryDirectory() as temp_dir:
        instru = Instrument(size=100, aafilter=None)
        instru.simulate()
        path = os.path.join(temp_dir, "measurements.h5")
        instru.write(path, mode="w")

def test_run_no_upsampling():
    """Test that simulations can run with no filter."""
    with TemporaryDirectory() as temp_dir:
        instru = Instrument(size=100, physics_upsampling=1, aafilter=None)
        instru.simulate()
        path = os.path.join(temp_dir, "measurements.h5")
        instru.write(path, mode="w")
def test_no_orbit_file():
    """Test that simulations fail with an invalid orbit file."""
    with pytest.raises(FileNotFoundError):
        Instrument(size=100, orbits="tests/data/nonexistent-orbits.h5")
    with pytest.raises(FileNotFoundError):
        Instrument(size=100, t0=0, orbits="tests/data/nonexistent-orbits.h5")
def test_keplerian_orbits_1_0_2():
    """Test that simulations can run with Keplerian orbit files v1.0.2."""
    with TemporaryDirectory() as temp_dir:
        instru = Instrument(size=100, orbits="tests/data/keplerian-orbits-1-0-2.h5")
        instru.simulate()
        path = os.path.join(temp_dir, "measurements.h5")
        instru.write(path, mode="w")
def test_esa_orbits_1_0_2():
    """Test that simulations can run with ESA orbit files v1.0.2."""
    with TemporaryDirectory() as temp_dir:
        instru = Instrument(size=100, orbits="tests/data/esa-orbits-1-0-2.h5")
        instru.simulate()
        path = os.path.join(temp_dir, "measurements.h5")
        instru.write(path, mode="w")
def test_keplerian_orbits_2_0():
    """Test that simulations can run with Keplerian orbit files v2.0."""
    with TemporaryDirectory() as temp_dir:
        instru = Instrument(size=100, orbits="tests/data/keplerian-orbits-2-0.h5")
        instru.simulate()
        path = os.path.join(temp_dir, "measurements.h5")
        instru.write(path, mode="w")
def test_esa_trailing_orbits_2_0():
    """Test that simulations can run with ESA trailing orbit files v2.0."""
    with TemporaryDirectory() as temp_dir:
        instru = Instrument(size=100, orbits="tests/data/esa-trailing-orbits-2-0.h5")
        instru.simulate()
        path = os.path.join(temp_dir, "measurements.h5")
        instru.write(path, mode="w")
def test_locking():
    """Test that simulations can run with various lock configurations."""
    with TemporaryDirectory() as temp_dir:
        path = os.path.join(temp_dir, "measurements.h5")
        # Test six free-running lasers
        instru = Instrument(size=100, lock="six")
        instru.simulate()
        instru.write(path, mode="w")
        # Test three free-running lasers
        instru = Instrument(size=100, lock="three")
        instru.simulate()
        instru.write(path, mode="w")
        # Test non-swap configurations
        for i in range(1, 6):
            instru = Instrument(size=100, lock=f"N{i}-12")
            instru.simulate()
            instru.write(path, mode="w")
        # Test that any other raises an error
        with pytest.raises(ValueError):
            Instrument(size=100, lock="whatever")
        with pytest.raises(ValueError):
            Instrument(size=100, lock="N7-12")
        with pytest.raises(ValueError):
            Instrument(size=100, lock="N1-67")

def test_initial_telemetry_size():
    """Test that simulations can run with a nonzero initial telemetry size."""

    orbit_paths = [
        "tests/data/keplerian-orbits-1-0-2.h5",
        "tests/data/esa-orbits-1-0-2.h5",
        "tests/data/keplerian-orbits-2-0.h5",
        "tests/data/esa-trailing-orbits-2-0.h5",
    with TemporaryDirectory() as temp_dir:
        path = os.path.join(temp_dir, "measurements.h5")
        for orbit_path in orbit_paths:
            # Read orbit file t0 and leave room for initial telemetry
            with h5py.File(orbit_path, "r") as orbitf:
                t0 = orbitf.attrs["t0"] + 1e6
            instru = Instrument(
                initial_telemetry_size=10,
                telemetry_downsampling=100,
                orbits=orbit_path,
                size=100,
                t0=t0,
            )
            instru.simulate()
            instru.write(path, mode="w")