Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion RATapi/examples/convert_rascal_project/convert_rascal.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import pathlib
from pprint import pp

import RATapi as RAT


# 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"
Expand Down
13 changes: 7 additions & 6 deletions RATapi/utils/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
from collections.abc import Iterable
from os import PathLike
from pathlib import Path
from typing import Union

Expand All @@ -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
Expand All @@ -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)":
Expand Down Expand Up @@ -271,15 +272,15 @@ 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.

Parameters
----------
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.
Expand Down Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import importlib
import os
import pathlib
import tempfile

import pytest
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Loading