Skip to content

Commit cca93e0

Browse files
authored
converter now works with pathlib and fixed example (#108)
1 parent 850cac0 commit cca93e0

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

RATapi/examples/convert_rascal_project/convert_rascal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import pathlib
12
from pprint import pp
23

34
import RATapi as RAT
45

56

67
# convert R1 project to Project class
78
def convert_rascal():
8-
project = RAT.utils.convert.r1_to_project_class("R1monolayerVolumeModel.mat")
9+
project_path = pathlib.Path(__file__).parent / "R1monolayerVolumeModel.mat"
10+
project = RAT.utils.convert.r1_to_project_class(project_path)
911

1012
# change values if you like, including ones not supported by R1
1113
project.parameters["Head Thickness"].prior_type = "gaussian"

RATapi/utils/convert.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
from collections.abc import Iterable
5+
from os import PathLike
56
from pathlib import Path
67
from typing import Union
78

@@ -14,12 +15,12 @@
1415
from RATapi.utils.enums import Geometries, Languages, LayerModels
1516

1617

17-
def r1_to_project_class(filename: str) -> Project:
18+
def r1_to_project_class(filename: Union[str, PathLike]) -> Project:
1819
"""Read a RasCAL1 project struct as a Python `Project`.
1920
2021
Parameters
2122
----------
22-
filename : str
23+
filename : str, PathLike
2324
The path to a .mat file containing project data.
2425
2526
Returns
@@ -28,7 +29,7 @@ def r1_to_project_class(filename: str) -> Project:
2829
A RAT `Project` equivalent to the RasCAL1 project struct.
2930
3031
"""
31-
mat_project = loadmat(filename, simplify_cells=True)["problem"]
32+
mat_project = loadmat(str(filename), simplify_cells=True)["problem"]
3233

3334
mat_module = mat_project["module"]
3435
if mat_module["experiment_type"] == "Air / Liquid (or solid)":
@@ -271,15 +272,15 @@ def read_param(names, constrs, values, fits):
271272

272273

273274
def project_class_to_r1(
274-
project: Project, filename: str = "RAT_project", return_struct: bool = False
275+
project: Project, filename: Union[str, PathLike] = "RAT_project", return_struct: bool = False
275276
) -> Union[dict, None]:
276277
"""Convert a RAT Project to a RasCAL1 project struct.
277278
278279
Parameters
279280
----------
280281
project : Project
281282
The RAT Project to convert.
282-
filename : str, default "RAT_project"
283+
filename : str or PathLike, default "RAT_project"
283284
If given, saves as a .mat file with the given filename.
284285
return_struct : bool, default False
285286
If True, do not save and instead return the R1 struct.
@@ -503,7 +504,7 @@ def convert_parameters(
503504
if eng is None:
504505
raise ImportError("matlabengine is not installed.")
505506
eng.workspace["problem"] = r1
506-
eng.save(filename, "problem", nargout=0)
507+
eng.save(str(filename), "problem", nargout=0)
507508
eng.exit()
508509
return None
509510

tests/test_convert.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import importlib
44
import os
5+
import pathlib
56
import tempfile
67

78
import pytest
@@ -56,9 +57,10 @@ def dspc_bilayer():
5657
["R1motofitBenchMark.mat", "r1_motofit_bench_mark"],
5758
],
5859
)
59-
def test_r1_to_project_class(file, project, request):
60+
@pytest.mark.parametrize("path_type", [os.path.join, pathlib.Path])
61+
def test_r1_to_project_class(file, project, path_type, request):
6062
"""Test that R1 to Project class conversion returns the expected Project."""
61-
output_project = r1_to_project_class(os.path.join(TEST_DIR_PATH, file))
63+
output_project = r1_to_project_class(path_type(TEST_DIR_PATH, file))
6264
expected_project = request.getfixturevalue(project)
6365

6466
# assert statements have to be more careful due to R1 missing features
@@ -127,11 +129,12 @@ def test_json_involution(project, request):
127129

128130

129131
@pytest.mark.skipif(importlib.util.find_spec("matlab") is None, reason="Matlab not installed")
130-
def test_matlab_save(request):
132+
@pytest.mark.parametrize("path_type", [os.path.join, pathlib.Path])
133+
def test_matlab_save(path_type, request):
131134
"""Test that MATLAB correctly saves the .mat file."""
132135
project = request.getfixturevalue("r1_default_project")
133136
with tempfile.TemporaryDirectory() as temp:
134-
matfile = os.path.join(temp, "testfile.mat")
137+
matfile = path_type(temp, "testfile.mat")
135138
project_class_to_r1(project, filename=matfile)
136139
converted_project = r1_to_project_class(matfile)
137140

0 commit comments

Comments
 (0)