Skip to content

Commit 25f115c

Browse files
committed
Refactors matlab save in "project_to_r1"
1 parent 0354a53 commit 25f115c

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

RATapi/examples/convert_rascal_project/convert_rascal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def convert_rascal(mat_filename="lipid_bilayer.mat"):
2626
2727
"""
2828
project_path = pathlib.Path(__file__).parent / "R1monolayerVolumeModel.mat"
29-
project = RAT.utils.convert.r1_to_project_class(project_path)
29+
project = RAT.utils.convert.r1_to_project(project_path)
3030

3131
# change values if you like, including ones not supported by R1
3232
project.parameters["Head Thickness"].prior_type = "gaussian"
@@ -35,10 +35,10 @@ def convert_rascal(mat_filename="lipid_bilayer.mat"):
3535

3636
# convert DSPC standard layers example to a struct and save as file
3737
lipid_bilayer_project = RAT.examples.DSPC_standard_layers()[0]
38-
RAT.utils.convert.project_class_to_r1(lipid_bilayer_project, filename=mat_filename)
38+
RAT.utils.convert.project_to_r1(lipid_bilayer_project, filename=mat_filename)
3939

4040
# convert and return as a Python dictionary
41-
struct = RAT.utils.convert.project_class_to_r1(lipid_bilayer_project, return_struct=True)
41+
struct = RAT.utils.convert.project_to_r1(lipid_bilayer_project, return_struct=True)
4242

4343
return project, struct
4444

RATapi/utils/convert.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from RATapi.utils.enums import Geometries, Languages, LayerModels
1616

1717

18-
def r1_to_project_class(filename: Union[str, PathLike]) -> Project:
18+
def r1_to_project(filename: Union[str, PathLike]) -> Project:
1919
"""Read a RasCAL1 project struct as a Python `Project`.
2020
2121
Parameters
@@ -318,7 +318,7 @@ def fix_invalid_constraints(name: str, constrs: tuple[float, float], value: floa
318318
return project
319319

320320

321-
def project_class_to_r1(
321+
def project_to_r1(
322322
project: Project, filename: Union[str, PathLike] = "RAT_project", return_struct: bool = False
323323
) -> Union[dict, None]:
324324
"""Convert a RAT Project to a RasCAL1 project struct.
@@ -549,11 +549,10 @@ def convert_parameters(
549549
# scipy.io.savemat doesn't do cells properly:
550550
# https://github.com/scipy/scipy/issues/3756
551551
# rather than fiddling we just use matlab
552-
eng = wrappers.start_matlab()
553-
if eng is None:
552+
loader = wrappers.MatlabWrapper.loader
553+
if loader is None:
554554
raise ImportError("matlabengine is not installed.")
555-
eng = eng.result()
555+
eng = loader.result()
556556
eng.workspace["problem"] = r1
557557
eng.save(str(filename), "problem", nargout=0)
558-
eng.exit()
559558
return None

tests/test_convert.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
import RATapi
11-
from RATapi.utils.convert import project_class_to_r1, r1_to_project_class
11+
from RATapi.utils.convert import project_to_r1, r1_to_project
1212

1313
TEST_DIR_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data")
1414

@@ -58,9 +58,9 @@ def dspc_bilayer():
5858
],
5959
)
6060
@pytest.mark.parametrize("path_type", [os.path.join, pathlib.Path])
61-
def test_r1_to_project_class(file, project, path_type, request):
61+
def test_r1_to_project(file, project, path_type, request):
6262
"""Test that R1 to Project class conversion returns the expected Project."""
63-
output_project = r1_to_project_class(path_type(TEST_DIR_PATH, file))
63+
output_project = r1_to_project(path_type(TEST_DIR_PATH, file))
6464
expected_project = request.getfixturevalue(project)
6565

6666
# assert statements have to be more careful due to R1 missing features
@@ -83,7 +83,7 @@ def test_r1_to_project_class(file, project, path_type, request):
8383
def test_r1_involution(project, request, monkeypatch):
8484
"""Test that converting a Project to an R1 struct and back returns the same project."""
8585
original_project = request.getfixturevalue(project)
86-
r1_struct = project_class_to_r1(original_project, return_struct=True)
86+
r1_struct = project_to_r1(original_project, return_struct=True)
8787

8888
# rather than writing the struct to a file and reading the file, just directly
8989
# hand the struct over
@@ -93,7 +93,7 @@ def mock_load(ignored_filename, **ignored_settings):
9393

9494
monkeypatch.setattr("RATapi.utils.convert.loadmat", mock_load, raising=True)
9595

96-
converted_project = r1_to_project_class(project)
96+
converted_project = r1_to_project(project)
9797

9898
for class_list in RATapi.project.class_lists:
9999
assert getattr(converted_project, class_list) == getattr(original_project, class_list)
@@ -105,7 +105,7 @@ def test_invalid_constraints():
105105
match=r"The parameter (.+) has invalid constraints,"
106106
" these have been adjusted to satisfy the current value of the parameter."
107107
):
108-
output_project = r1_to_project_class(pathlib.Path(TEST_DIR_PATH, "R1DoubleBilayerVolumeModel.mat"))
108+
output_project = r1_to_project(pathlib.Path(TEST_DIR_PATH, "R1DoubleBilayerVolumeModel.mat"))
109109

110110
assert output_project.background_parameters[0].min == output_project.background_parameters[0].value
111111

@@ -117,7 +117,7 @@ def test_matlab_save(path_type, request):
117117
project = request.getfixturevalue("r1_default_project")
118118
with tempfile.TemporaryDirectory() as temp:
119119
matfile = path_type(temp, "testfile.mat")
120-
project_class_to_r1(project, filename=matfile)
121-
converted_project = r1_to_project_class(matfile)
120+
project_to_r1(project, filename=matfile)
121+
converted_project = r1_to_project(matfile)
122122

123123
assert project == converted_project

0 commit comments

Comments
 (0)