Exemplo n.º 1
0
 def test_finds_subclass(self, sample_cal):
     """If the wrong subclass is used it can not find the correct calibration."""
     with tempfile.NamedTemporaryFile(mode="w+") as f:
         sample_cal.to_json_file(f.name)
         if isinstance(sample_cal, CustomFerraris):
             assert load_calibration_info(
                 f.name, file_type="json",
                 base_class=CustomFerraris) == sample_cal
         else:
             with pytest.raises(ValueError) as e:
                 load_calibration_info(f.name,
                                       file_type="json",
                                       base_class=CustomFerraris)
             assert sample_cal.CAL_TYPE in str(e)
Exemplo n.º 2
0
 def test_auto_loader(self, file_type, sample_cal):
     method = dict(json="to_json_file", hdf="to_hdf5")
     with tempfile.NamedTemporaryFile(mode="w+",
                                      suffix="." + file_type) as f:
         getattr(sample_cal, method[file_type])(f.name)
         out = load_calibration_info(f.name)
     assert sample_cal == out
Exemplo n.º 3
0
    def test_find_calibration_type_filter(self, sample_cal_folder_recursive):
        cals = find_calibration_info_for_sensor("test1",
                                                sample_cal_folder_recursive,
                                                recursive=True,
                                                filter_cal_type="ferraris")

        assert len(cals) == 10
        assert all(["test1" in str(x) for x in cals])
        assert all([
            load_calibration_info(c).CAL_TYPE.lower() == "ferraris"
            for c in cals
        ])
Exemplo n.º 4
0
def load_and_check_cal_info(
        calibration: Union["CalibrationInfo", path_t]) -> "CalibrationInfo":
    """Load a calibration from path or check if the provided object is already a valid calibration."""
    from imucal import CalibrationInfo  # noqa: import-outside-toplevel

    if isinstance(calibration, (Path, str)):
        from imucal.management import load_calibration_info  # noqa: F401

        calibration = load_calibration_info(calibration)
    if not isinstance(calibration, CalibrationInfo):
        raise ValueError("No valid CalibrationInfo object provided")
    return calibration
Exemplo n.º 5
0
    def test_custom_validator(self, sample_cal_folder_recursive):
        # We simulate the caltype filter with a custom validator
        validator = lambda x: x.CAL_TYPE.lower() == "ferraris"

        cals = find_calibration_info_for_sensor("test1",
                                                sample_cal_folder_recursive,
                                                recursive=True,
                                                filter_cal_type=None,
                                                custom_validator=validator)

        assert len(cals) == 10
        assert all(["test1" in str(x) for x in cals])
        assert all([
            load_calibration_info(c).CAL_TYPE.lower() == "ferraris"
            for c in cals
        ])
Exemplo n.º 6
0
file_path

# %%
# In the latter case, we can use the helper functions :func:`~imucal.management.find_calibration_info_for_sensor`
# and :func:`~imucal.management.find_closest_calibration_info_to_date` to find the calibration again.
from imucal.management import find_calibration_info_for_sensor

cals = find_calibration_info_for_sensor("imu1", Path(d.name))
cals

# %%
# In any case, we can use :func:`~imucal.management.load_calibration_info` to load the calibration if we know the file
# path.
from imucal.management import load_calibration_info

loaded_cal_info = load_calibration_info(cals[0])
print(loaded_cal_info.to_json())

# %%
# After loading the calibration file, we will apply it to a "new" recording (we will just use the calibration session
# as example here).

calibrated_data = loaded_cal_info.calibrate_df(data, "m/s^2", "deg/s")

# %%
# We can see the effect of the calibration, when we plot the acc norm in the beginning of the recording.
# The calibrated values are now much closer to 9.81 m/s^2 compared to before the calibration.
import matplotlib.pyplot as plt
from numpy.linalg import norm

plt.figure()
Exemplo n.º 7
0
import pandas as pd

cal_data = ferraris_regions_from_df(
    pd.read_csv(EXAMPLE_PATH / "annotated_session.csv",
                header=0,
                index_col=[0, 1]))

cal_info = cal.compute(cal_data,
                       sampling_rate_hz=204.8,
                       from_acc_unit="a.u.",
                       from_gyr_unit="a.u.",
                       new_meta_info="my value")

cal_info.new_meta_info

# %%
# And of course we can simply export and reimport the new calibration info to json or hdf5 (we will use a tempfile
# here to not clutter the example folder).
import tempfile
from pathlib import Path
from imucal.management import load_calibration_info

with tempfile.TemporaryDirectory() as d:
    file_name = Path(d) / "my_cal_info.json"
    cal_info.to_json_file(file_name)

    # An load it again
    loaded_cal_info = load_calibration_info(file_name)

loaded_cal_info.new_meta_info
Exemplo n.º 8
0
    def test_invalid_loader(self):
        with pytest.raises(ValueError) as e:
            load_calibration_info("invalid_file.txt", file_type="invalid")

        assert "`format` must be one of" in str(e)
Exemplo n.º 9
0
    def test_invalid_file(self):
        with pytest.raises(ValueError) as e:
            load_calibration_info("invalid_file.txt")

        assert "The loader format could not be determined" in str(e)
Exemplo n.º 10
0
def test_error_raised(legacy_pre_2_0_cal):
    with pytest.raises(ValueError) as e:
        load_calibration_info(legacy_pre_2_0_cal)

    assert "`imucal.legacy`" in str(e)
def test_hdf5_file_roundtrip(sample_cal):
    with tempfile.NamedTemporaryFile(mode="w+") as f:
        sample_cal.to_hdf5(f.name)
        out = load_calibration_info(f.name, file_type="hdf")
    assert sample_cal == out