def sample_cal_folder(sample_cal): with tempfile.TemporaryDirectory() as f: for sid in ["test1", "test2", "test3"]: for min in range(10, 30, 2): date = datetime.datetime(2000, 10, 3, 13, min) save_calibration_info(sample_cal, sid, date, f, folder_structure="") yield f
def sample_cal_folder_recursive(sample_cal_dict): with tempfile.TemporaryDirectory() as f: for s in [ FerrarisCalibrationInfo, TurntableCalibrationInfo, CustomFerraris ]: for sid in ["test1", "test2", "test3"]: new_cal = s(**sample_cal_dict) for min in range(10, 30, 2): date = datetime.datetime(2000, 10, 3, 13, min) save_calibration_info(new_cal, sid, date, Path(f)) yield f
def test_empty_folder_structure(self, sample_cal): out = save_calibration_info(sample_cal, "test", datetime.datetime(2000, 10, 3, 13, 22), self.temp_folder, folder_structure="") assert out.parent == self.temp_folder
def test_custom_folder_path(self, sample_cal, str_in, folder_path): out = save_calibration_info(sample_cal, "test", datetime.datetime(2000, 10, 3, 13, 22), self.temp_folder, folder_structure=str_in) match = out.parts[-len(folder_path) - 1:-1] for e, o in zip(match, folder_path): assert e == o
def test_default_filename(self, sample_cal): out = save_calibration_info(sample_cal, "test", datetime.datetime(2000, 10, 3, 13, 22), self.temp_folder) expected_out = next( (self.temp_folder / "test" / sample_cal.CAL_TYPE).glob("*")) assert out == expected_out assert expected_out.name == "test_2000-10-03_13-22.json"
def save_calibration( calibration: "CalibrationInfo", sensor_id: str, cal_time: datetime.datetime, folder: path_t, folder_structure: str = "", ) -> Path: """Save a calibration info object in the correct format and file name for NilsPods. The files will be saved in the format `folder/{sensor_id}_%Y-%m-%d_%H-%M.json` by default. If you want to recreate the default folder structure of `imucal`, pass `folder_structure="{sensor_id}/{cal_info.CAL_TYPE}"` to the function. The naming schema and format is of course just a suggestion, and any structure can be used as long as it can be converted back into a CalibrationInfo object. However, following the naming convention will allow to use other calibration utils to search for suitable calibration files. .. note:: If the folder does not exist it will be created. Parameters ---------- calibration : The CalibrationInfo object ot be saved sensor_id : The for 4 letter/digit identifier of a sensor_type, as obtained from :py:meth:`nilspodlib.header.Header.sensor_id` cal_time : The date and time (min precision) when the calibration was performed. It is preferable to pass this value in UTC timezone, as this is in line with the time handling in the rest of the library. folder : Basepath of the folder, where the file will be stored. folder_structure : A valid formatted Python string using the `{}` syntax. `sensor_id`, calibration as the name `cal_info` and kwargs will be passed to the `str.format` as keyword arguments and can be used in the string. Returns ------- output_file_name The name under which the calibration file was saved """ if not re.fullmatch(r"\w{4}", sensor_id): raise ValueError( "The sensor_id is expected to be a 4 symbols string only containing numbers or letters, not {}" .format(sensor_id)) from imucal.management import save_calibration_info # noqa: F401 return save_calibration_info(cal_info=calibration, sensor_id=sensor_id, cal_time=cal_time, folder=folder, folder_structure=folder_structure)
def test_kwargs(self, sample_cal): out = save_calibration_info( sample_cal, "test", datetime.datetime(2000, 10, 3, 13, 22), self.temp_folder, folder_structure="{sensor_id}/{my_custom}", my_custom="my_custom_val", ) assert out.parts[-2] == "my_custom_val" assert out.parts[-3] == "test"
d = tempfile.TemporaryDirectory() d.name # %% # You can either use the `to_json_file` or `to_hdf5` methods of :class:`~imucal.FerrarisCalibration` directly ... cal_info.to_json_file(Path(d.name) / "my_sensor_cal.json") # %% # ... or use the provided management tools to save the file in a predefined folder structure. # Read more about this in the our :ref:`guide on that topic <cal_store_guide>`. from imucal.management import save_calibration_info from datetime import datetime file_path = save_calibration_info(cal_info, sensor_id="imu1", cal_time=datetime(2020, 8, 12, 13, 21), folder=Path(d.name)) 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
def test_valid_s_id(self, sample_cal, sensor_id): with pytest.raises(ValueError): save_calibration_info(sample_cal, sensor_id, datetime.datetime(2000, 10, 3, 13, 22), self.temp_folder)