def test_inferr_file_format_no_extension(tmp_path: Path): """Raise error if file has no extension.""" file_path = tmp_path / "dummy" file_path.touch() with pytest.raises( ValueError, match= "Cannot determine format of file .+?, please provide an explicit format" ): inferr_file_format(file_path)
def load_parameters(file_name: StrOrPath, format_name: str = None, **kwargs) -> ParameterGroup: """Create a :class:`ParameterGroup` instance from the specs defined in a file. Parameters ---------- file_name : StrOrPath File containing the parameter specs. format_name : str Format the file is in, if not provided it will be inferred from the file extension. **kwargs: Any Additional keyword arguments passes to the ``load_parameters`` implementation of the project io plugin. Returns ------- ParameterGroup :class:`ParameterGroup` instance created from the file. """ io = get_project_io(format_name or inferr_file_format(file_name)) parameters = io.load_parameters( # type: ignore[call-arg] Path(file_name).as_posix(), **kwargs, ) parameters.source_path = Path(file_name).as_posix() return parameters
def test_inferr_file_format_allow_folder(tmp_path: Path, is_file: bool): """If there is no extension, return folder.""" file_path = tmp_path / "dummy" if is_file: file_path.touch() assert inferr_file_format(file_path, allow_folder=True) == "folder"
def save_dataset( file_name: str, dataset: xr.Dataset | xr.DataArray, format_name: str = None, *, allow_overwrite: bool = False, **kwargs: Any, ) -> None: """Save data from :xarraydoc:`Dataset` or :xarraydoc:`DataArray` to a file. Parameters ---------- file_name : str File to write the data to. format_name : str Format the file should be in, if not provided it will be inferred from the file extension. dataset: xr.Dataset|xr.DataArray Data to be written to file. allow_overwrite : bool Whether or not to allow overwriting existing files, by default False **kwargs: Any Additional keyword arguments passes to the ``write_dataset`` implementation of the data io plugin. If you aren't sure about those use ``get_datawriter`` to get the implementation with the proper help and autocomplete. """ protect_from_overwrite(file_name, allow_overwrite=allow_overwrite) io = get_data_io(format_name or inferr_file_format(file_name, needs_to_exist=False)) io.save_dataset( # type: ignore[call-arg] file_name=file_name, dataset=dataset, **kwargs, )
def save_result( result_path: str, result: Result, format_name: str = None, *, allow_overwrite: bool = False, **kwargs: Any, ) -> None: """Write a :class:`Result` instance to a spec file. Parameters ---------- result_path : str Path to write the result data to. result : Result :class:`Result` instance to write. format_name : str Format the result should be saved in, if not provided and it is a file it will be inferred from the file extension. allow_overwrite : bool Whether or not to allow overwriting existing files, by default False **kwargs: Any Additional keyword arguments passes to the ``save_result`` implementation of the project io plugin. """ protect_from_overwrite(result_path, allow_overwrite=allow_overwrite) io = get_project_io(format_name or inferr_file_format( result_path, needs_to_exist=False, allow_folder=True)) io.save_result( # type: ignore[call-arg] result_path=result_path, result=result, **kwargs, )
def save_scheme( file_name: str, scheme: Scheme, format_name: str = None, *, allow_overwrite: bool = False, **kwargs: Any, ) -> None: """Save a :class:`Scheme` instance to a spec file. Parameters ---------- file_name : str File to write the scheme specs to. scheme : Scheme :class:`Scheme` instance to save to specs file. format_name : str Format the file should be in, if not provided it will be inferred from the file extension. allow_overwrite : bool Whether or not to allow overwriting existing files, by default False **kwargs: Any Additional keyword arguments passes to the ``save_scheme`` implementation of the project io plugin. """ protect_from_overwrite(file_name, allow_overwrite=allow_overwrite) io = get_project_io(format_name or inferr_file_format(file_name, needs_to_exist=False)) io.save_scheme(file_name=file_name, scheme=scheme, **kwargs) # type: ignore[call-arg]
def load_dataset(file_name: StrOrPath, format_name: str = None, **kwargs: Any) -> xr.Dataset: """Read data from a file to :xarraydoc:`Dataset` or :xarraydoc:`DataArray`. Parameters ---------- file_name : StrOrPath File containing the data. format_name : str Format the file is in, if not provided it will be inferred from the file extension. **kwargs : Any Additional keyword arguments passes to the ``read_dataset`` implementation of the data io plugin. If you aren't sure about those use ``get_dataloader`` to get the implementation with the proper help and autocomplete. Returns ------- xr.Dataset Data loaded from the file. """ io = get_data_io(format_name or inferr_file_format(file_name)) dataset = io.load_dataset(Path(file_name).as_posix(), **kwargs) # type: ignore[call-arg] if isinstance(dataset, xr.DataArray): dataset = dataset.to_dataset(name="data") dataset.attrs["loader"] = load_dataset dataset.attrs["source_path"] = Path(file_name).as_posix() return dataset
def save_result( result: Result, result_path: StrOrPath, format_name: str = None, *, allow_overwrite: bool = False, update_source_path: bool = True, saving_options: SavingOptions = SAVING_OPTIONS_DEFAULT, **kwargs: Any, ) -> list[str]: """Write a :class:`Result` instance to a spec file. Parameters ---------- result : Result :class:`Result` instance to write. result_path : StrOrPath Path to write the result data to. format_name : str Format the result should be saved in, if not provided and it is a file it will be inferred from the file extension. allow_overwrite : bool Whether or not to allow overwriting existing files, by default False update_source_path: bool Whether or not to update the ``source_path`` attribute to ``result_path`` when saving. by default True saving_options : SavingOptions Options for the saved result. **kwargs : Any Additional keyword arguments passes to the ``save_result`` implementation of the project io plugin. Returns ------- list[str] | None List of file paths which were saved. """ protect_from_overwrite(result_path, allow_overwrite=allow_overwrite) io = get_project_io( format_name or inferr_file_format(result_path, needs_to_exist=False, allow_folder=True) ) paths = io.save_result( # type: ignore[call-arg] result_path=Path(result_path).as_posix(), result=result, saving_options=saving_options, **kwargs, ) if update_source_path is True: result.source_path = Path(result_path).as_posix() return paths
def save_dataset( dataset: xr.Dataset | xr.DataArray, file_name: StrOrPath, format_name: str = None, *, data_filters: list[str] | None = None, allow_overwrite: bool = False, update_source_path: bool = True, **kwargs: Any, ) -> None: """Save data from :xarraydoc:`Dataset` or :xarraydoc:`DataArray` to a file. Parameters ---------- dataset : xr.Dataset | xr.DataArray Data to be written to file. file_name : StrOrPath File to write the data to. format_name : str Format the file should be in, if not provided it will be inferred from the file extension. data_filters : list[str] | None Optional list of items in the dataset to be saved. allow_overwrite : bool Whether or not to allow overwriting existing files, by default False update_source_path: bool Whether or not to update the ``source_path`` attribute to ``file_name`` when saving. by default True **kwargs : Any Additional keyword arguments passes to the ``write_dataset`` implementation of the data io plugin. If you aren't sure about those use ``get_datawriter`` to get the implementation with the proper help and autocomplete. """ protect_from_overwrite(file_name, allow_overwrite=allow_overwrite) io = get_data_io(format_name or inferr_file_format(file_name, needs_to_exist=False)) if "loader" in dataset.attrs: del dataset.attrs["loader"] if "source_path" in dataset.attrs: orig_source_path: str = dataset.attrs["source_path"] del dataset.attrs["source_path"] io.save_dataset( # type: ignore[call-arg] file_name=Path(file_name).as_posix(), dataset=dataset, **kwargs, ) dataset.attrs["loader"] = load_dataset if update_source_path is True or "orig_source_path" not in locals(): dataset.attrs["source_path"] = Path(file_name).as_posix() else: dataset.attrs["source_path"] = Path(orig_source_path).as_posix()
def load_model(file_name: StrOrPath, format_name: str = None, **kwargs: Any) -> Model: """Create a Model instance from the specs defined in a file. Parameters ---------- file_name : StrOrPath File containing the model specs. format_name : str Format the file is in, if not provided it will be inferred from the file extension. **kwargs: Any Additional keyword arguments passes to the ``load_model`` implementation of the project io plugin. Returns ------- Model Model instance created from the file. """ io = get_project_io(format_name or inferr_file_format(file_name)) model = io.load_model(Path(file_name).as_posix(), **kwargs) # type: ignore[call-arg] model.source_path = Path(file_name).as_posix() return model
def load_scheme(file_name: str, format_name: str = None, **kwargs: Any) -> Scheme: """Create a :class:`Scheme` instance from the specs defined in a file. Parameters ---------- file_name : str File containing the parameter specs. format_name : str Format the file is in, if not provided it will be inferred from the file extension. **kwargs: Any Additional keyword arguments passes to the ``load_scheme`` implementation of the project io plugin. Returns ------- Scheme :class:`Scheme` instance created from the file. """ io = get_project_io(format_name or inferr_file_format(file_name)) return io.load_scheme(file_name, **kwargs) # type: ignore[call-arg]
def load_dataset( file_name: str, format_name: str = None, **kwargs: Any ) -> xr.Dataset | xr.DataArray: """Read data from a file to :xarraydoc:`Dataset` or :xarraydoc:`DataArray`. Parameters ---------- file_name : str File containing the data. format_name : str Format the file is in, if not provided it will be inferred from the file extension. **kwargs: Any Additional keyword arguments passes to the ``read_dataset`` implementation of the data io plugin. If you aren't sure about those use ``get_dataloader`` to get the implementation with the proper help and autocomplete. Returns ------- xr.Dataset|xr.DataArray Data loaded from the file. """ io = get_data_io(format_name or inferr_file_format(file_name)) return io.load_dataset(file_name, **kwargs) # type: ignore[call-arg]
def load_result(result_path: str, format_name: str = None, **kwargs: Any) -> Result: """Create a :class:`Result` instance from the specs defined in a file. Parameters ---------- result_path : str Path containing the result data. format_name : str Format the result is in, if not provided and it is a file it will be inferred from the file extension. **kwargs: Any Additional keyword arguments passes to the ``load_result`` implementation of the project io plugin. Returns ------- Result :class:`Result` instance created from the saved format. """ io = get_project_io(format_name or inferr_file_format(result_path)) return io.load_result(result_path, **kwargs) # type: ignore[call-arg]
def save_scheme( scheme: Scheme, file_name: StrOrPath, format_name: str = None, *, allow_overwrite: bool = False, update_source_path: bool = True, **kwargs: Any, ) -> None: """Save a :class:`Scheme` instance to a spec file. Parameters ---------- scheme : Scheme :class:`Scheme` instance to save to specs file. file_name : StrOrPath File to write the scheme specs to. format_name : str Format the file should be in, if not provided it will be inferred from the file extension. allow_overwrite : bool Whether or not to allow overwriting existing files, by default False update_source_path: bool Whether or not to update the ``source_path`` attribute to ``file_name`` when saving. by default True **kwargs : Any Additional keyword arguments passes to the ``save_scheme`` implementation of the project io plugin. """ protect_from_overwrite(file_name, allow_overwrite=allow_overwrite) io = get_project_io(format_name or inferr_file_format(file_name, needs_to_exist=False)) io.save_scheme( # type: ignore[call-arg] file_name=Path(file_name).as_posix(), scheme=scheme, **kwargs, ) if update_source_path is True: scheme.source_path = Path(file_name).as_posix()
def test_inferr_file_format_none_existing_file(): """Raise error if file does not exists.""" with pytest.raises(ValueError, match="There is no file "): inferr_file_format("none-existing-file.yml")
def test_inferr_file_format(tmp_path: Path, extension: str, expected: str): """Inferr type from existing files with extension.""" file_path = tmp_path / f"dummy.{extension}" file_path.touch() assert inferr_file_format(file_path) == expected