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
2 changes: 1 addition & 1 deletion RATapi/examples/absorption/absorption.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def absorption():
name="DPPC absorption",
filename="volume_thiol_bilayer.py",
language="python",
path=pathlib.Path(__file__).parent.resolve(),
path=pathlib.Path(__file__).parent,
)

# Finally add the contrasts
Expand Down
2 changes: 1 addition & 1 deletion RATapi/examples/absorption/volume_thiol_bilayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def volume_thiol_bilayer(params, bulk_in, bulk_out, contrast):

CW = [cwThick, bulk_out[contrast], 0, bilayerRough]

if contrast == 0 or contrast == 2:
if contrast == 1 or contrast == 3:
output = [alloyUp, gold, SAMTAILS, SAMHEAD, CW, *BILAYER]
else:
output = [alloyDown, gold, SAMTAILS, SAMHEAD, CW, *BILAYER]
Expand Down
2 changes: 1 addition & 1 deletion RATapi/examples/domains/domains_custom_XY.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def domains_custom_XY():
name="Domain Layer",
filename="domains_XY_model.py",
language="python",
path=pathlib.Path(__file__).parent.resolve(),
path=pathlib.Path(__file__).parent,
)

# Make contrasts
Expand Down
2 changes: 1 addition & 1 deletion RATapi/examples/domains/domains_custom_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def domains_custom_layers():
name="Alloy domains",
filename="alloy_domains.py",
language="python",
path=pathlib.Path(__file__).parent.resolve(),
path=pathlib.Path(__file__).parent,
)

# Make a contrast
Expand Down
2 changes: 1 addition & 1 deletion RATapi/examples/languages/run_custom_file_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import RATapi as RAT

path = pathlib.Path(__file__).parent.resolve()
path = pathlib.Path(__file__).parent

project = setup_problem.make_example_problem()
controls = RAT.Controls()
Expand Down
2 changes: 1 addition & 1 deletion RATapi/examples/languages/setup_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def make_example_problem():
name="DSPC Model",
filename="custom_bilayer.py",
language="python",
path=pathlib.Path(__file__).parent.resolve(),
path=pathlib.Path(__file__).parent,
)

# Also, add the relevant background parameters - one each for each contrast:
Expand Down
2 changes: 1 addition & 1 deletion RATapi/examples/normal_reflectivity/DSPC_custom_XY.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def DSPC_custom_XY():
name="DSPC Model",
filename="custom_XY_DSPC.py",
language="python",
path=pathlib.Path(__file__).parent.resolve(),
path=pathlib.Path(__file__).parent,
)

# Also, add the relevant background parameters - one each for each contrast:
Expand Down
2 changes: 1 addition & 1 deletion RATapi/examples/normal_reflectivity/DSPC_custom_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def DSPC_custom_layers():
name="DSPC Model",
filename="custom_bilayer_DSPC.py",
language="python",
path=pathlib.Path(__file__).parent.resolve(),
path=pathlib.Path(__file__).parent,
)

# Also, add the relevant background parameters - one each for each contrast:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def DSPC_function_background():
name="D2O Background Function",
filename="background_function.py",
language="python",
path=pathlib.Path(__file__).parent.resolve(),
path=pathlib.Path(__file__).parent,
)

problem.background_parameters.append(name="Fn Ao", min=5e-7, value=8e-6, max=5e-5)
Expand Down
8 changes: 7 additions & 1 deletion RATapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,13 @@ class CustomFile(RATModel):
filename: str = ""
function_name: str = ""
language: Languages = Languages.Python
path: pathlib.Path = pathlib.Path(".")
path: pathlib.Path = pathlib.Path(".").resolve()

@field_validator("path")
@classmethod
def resolve_relative_paths(cls, path: pathlib.Path) -> pathlib.Path:
"""Return the absolute path of the given path."""
return path.resolve()

def model_post_init(self, __context: Any) -> None:
"""Autogenerate the function name from the filename if not set.
Expand Down
9 changes: 5 additions & 4 deletions RATapi/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,8 @@ def write_item(item):
+ f"'filename': '{item.filename}', "
+ f"'function_name': '{item.function_name}', "
+ f"'language': '{str(item.language)}', "
+ f"'path': '{str(item.path)}'"
# Raw string to ensure backslash is not interpreted as escape
+ f"'path': r'{str(try_relative_to(item.path, script_path.parent))}'"
+ "}"
)
return item_str
Expand Down Expand Up @@ -932,7 +933,7 @@ def make_custom_file_dict(item):
"name": item.name,
"filename": item.filename,
"language": item.language,
"path": try_relative_to(item.path, filepath),
"path": try_relative_to(item.path, filepath.parent),
}

json_dict["custom_files"] = [make_custom_file_dict(file) for file in attr]
Expand Down Expand Up @@ -968,7 +969,7 @@ def load(cls, path: Union[str, Path]) -> "Project":
# file paths are saved as relative to the project directory
for file in model_dict["custom_files"]:
if not Path(file["path"]).is_absolute():
file["path"] = Path(path, file["path"])
file["path"] = Path(path.parent, file["path"]).resolve()

return cls.model_validate(model_dict)

Expand Down Expand Up @@ -1038,7 +1039,7 @@ def try_relative_to(path: Path, relative_to: Path) -> str:
return str(path.relative_to(relative_to))
else:
warnings.warn(
"Could not save custom file path as relative to the project directory, "
"Could not write custom file path as relative to the project directory, "
"which means that it may not work on other devices. If you would like to share your project, "
"make sure your custom files are in a subfolder of the project save location.",
stacklevel=2,
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6858,7 +6858,7 @@ def r1_monolayer():
),
custom_files=RATapi.ClassList(
RATapi.models.CustomFile(
name="Model_IIb", filename="Model_IIb.m", function_name="Model_IIb", language="matlab", path=""
name="Model_IIb", filename="Model_IIb.m", function_name="Model_IIb", language="matlab"
)
),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def test_append_data_background_error():


def test_get_python_handle():
path = pathlib.Path(__file__).parent.resolve()
path = pathlib.Path(__file__).parent
assert RATapi.inputs.get_python_handle("utils.py", "dummy_function", path).__code__ == dummy_function.__code__


Expand Down
8 changes: 8 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test the pydantic models."""

import pathlib
import re
from typing import Callable

Expand Down Expand Up @@ -100,6 +101,13 @@ def test_initialise_with_extra_fields(self, model: Callable, model_params: dict)
model(new_field=1, **model_params)


def test_custom_file_path_is_absolute() -> None:
"""If we use provide a relative path to the custom file model, it should be converted to an absolute path."""
relative_path = pathlib.Path("./relative_path")
custom_file = RATapi.models.CustomFile(path=relative_path)
assert custom_file.path.is_absolute()


def test_data_eq() -> None:
"""If we use the Data.__eq__ method with an object that is not a pydantic BaseModel, we should return
"NotImplemented".
Expand Down
6 changes: 1 addition & 5 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,10 +1590,6 @@ def test_save_load(project, request):
original_project.save(path)
converted_project = RATapi.Project.load(path)

# resolve custom files in case the original project had unresolvable relative paths
for file in original_project.custom_files:
file.path = file.path.resolve()

for field in RATapi.Project.model_fields:
assert getattr(converted_project, field) == getattr(original_project, field)

Expand All @@ -1614,7 +1610,7 @@ def test_relative_paths_warning():
relative_path = "/tmp/project/project_path/myproj.dat"

with pytest.warns(
match="Could not save custom file path as relative to the project directory, "
match="Could not write custom file path as relative to the project directory, "
"which means that it may not work on other devices. If you would like to share your project, "
"make sure your custom files are in a subfolder of the project save location.",
):
Expand Down
Loading