diff --git a/RATapi/examples/convert_rascal_project/convert_rascal.py b/RATapi/examples/convert_rascal_project/convert_rascal.py index d5d54e1a..9fbe1de8 100644 --- a/RATapi/examples/convert_rascal_project/convert_rascal.py +++ b/RATapi/examples/convert_rascal_project/convert_rascal.py @@ -1,3 +1,4 @@ +import pathlib from pprint import pp import RATapi as RAT @@ -5,7 +6,8 @@ # convert R1 project to Project class def convert_rascal(): - project = RAT.utils.convert.r1_to_project_class("R1monolayerVolumeModel.mat") + project_path = pathlib.Path(__file__).parent / "R1monolayerVolumeModel.mat" + project = RAT.utils.convert.r1_to_project_class(project_path) # change values if you like, including ones not supported by R1 project.parameters["Head Thickness"].prior_type = "gaussian" diff --git a/RATapi/utils/convert.py b/RATapi/utils/convert.py index 4c40dc10..2aeba655 100644 --- a/RATapi/utils/convert.py +++ b/RATapi/utils/convert.py @@ -2,6 +2,7 @@ import json from collections.abc import Iterable +from os import PathLike from pathlib import Path from typing import Union @@ -14,12 +15,12 @@ from RATapi.utils.enums import Geometries, Languages, LayerModels -def r1_to_project_class(filename: str) -> Project: +def r1_to_project_class(filename: Union[str, PathLike]) -> Project: """Read a RasCAL1 project struct as a Python `Project`. Parameters ---------- - filename : str + filename : str, PathLike The path to a .mat file containing project data. Returns @@ -28,7 +29,7 @@ def r1_to_project_class(filename: str) -> Project: A RAT `Project` equivalent to the RasCAL1 project struct. """ - mat_project = loadmat(filename, simplify_cells=True)["problem"] + mat_project = loadmat(str(filename), simplify_cells=True)["problem"] mat_module = mat_project["module"] if mat_module["experiment_type"] == "Air / Liquid (or solid)": @@ -271,7 +272,7 @@ def read_param(names, constrs, values, fits): def project_class_to_r1( - project: Project, filename: str = "RAT_project", return_struct: bool = False + project: Project, filename: Union[str, PathLike] = "RAT_project", return_struct: bool = False ) -> Union[dict, None]: """Convert a RAT Project to a RasCAL1 project struct. @@ -279,7 +280,7 @@ def project_class_to_r1( ---------- project : Project The RAT Project to convert. - filename : str, default "RAT_project" + filename : str or PathLike, default "RAT_project" If given, saves as a .mat file with the given filename. return_struct : bool, default False If True, do not save and instead return the R1 struct. @@ -503,7 +504,7 @@ def convert_parameters( if eng is None: raise ImportError("matlabengine is not installed.") eng.workspace["problem"] = r1 - eng.save(filename, "problem", nargout=0) + eng.save(str(filename), "problem", nargout=0) eng.exit() return None diff --git a/tests/test_convert.py b/tests/test_convert.py index 3e4690c6..ee97832e 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -2,6 +2,7 @@ import importlib import os +import pathlib import tempfile import pytest @@ -56,9 +57,10 @@ def dspc_bilayer(): ["R1motofitBenchMark.mat", "r1_motofit_bench_mark"], ], ) -def test_r1_to_project_class(file, project, request): +@pytest.mark.parametrize("path_type", [os.path.join, pathlib.Path]) +def test_r1_to_project_class(file, project, path_type, request): """Test that R1 to Project class conversion returns the expected Project.""" - output_project = r1_to_project_class(os.path.join(TEST_DIR_PATH, file)) + output_project = r1_to_project_class(path_type(TEST_DIR_PATH, file)) expected_project = request.getfixturevalue(project) # assert statements have to be more careful due to R1 missing features @@ -127,11 +129,12 @@ def test_json_involution(project, request): @pytest.mark.skipif(importlib.util.find_spec("matlab") is None, reason="Matlab not installed") -def test_matlab_save(request): +@pytest.mark.parametrize("path_type", [os.path.join, pathlib.Path]) +def test_matlab_save(path_type, request): """Test that MATLAB correctly saves the .mat file.""" project = request.getfixturevalue("r1_default_project") with tempfile.TemporaryDirectory() as temp: - matfile = os.path.join(temp, "testfile.mat") + matfile = path_type(temp, "testfile.mat") project_class_to_r1(project, filename=matfile) converted_project = r1_to_project_class(matfile)