Skip to content

Commit

Permalink
Merge pull request #250 from neutrinoceros/ux/better_errors
Browse files Browse the repository at this point in the history
BUG: fix loading from absolute paths
  • Loading branch information
volodia99 authored Mar 12, 2024
2 parents f43d766 + 4d68055 commit e01d814
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
18 changes: 16 additions & 2 deletions nonos/api/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def __init__(
*,
inifile: str = "",
code: str = "",
directory="",
directory: Optional[str] = None,
rotate_by: Optional[float] = None,
rotate_with: Optional[str] = None,
rotate_grid: int = -1, # deprecated
Expand All @@ -462,6 +462,8 @@ def __init__(

self.inifile = inifile
self.code = code
if directory is None:
directory = os.getcwd()
self.directory = directory
self._rotate_by = _parse_rotation_angle(
rotate_by=rotate_by,
Expand Down Expand Up @@ -1533,9 +1535,21 @@ def __init__(
inifile: str = "",
code: str = "",
geometry: str = "unknown",
directory: str = "",
directory: Optional[str] = None,
fluid: Optional[str] = None,
) -> None:
if isinstance(input_dataset, str):
directory_from_input = os.path.dirname(input_dataset)
if directory is None:
directory = directory_from_input
elif os.path.abspath(directory_from_input) != os.path.abspath(directory):
raise ValueError(
f"directory value {directory!r} does not match "
f"directory name from input_dataset ({directory_from_input!r})"
)
del directory_from_input
input_dataset = os.path.basename(input_dataset)

self.params = Parameters(inifile=inifile, code=code, directory=directory)
self._read = self.params.loadSimuFile(
input_dataset, geometry=geometry, cell="edges", fluid=fluid
Expand Down
13 changes: 10 additions & 3 deletions nonos/api/from_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ def __init__(
code: Optional[str] = None,
directory: Optional[str] = None,
) -> None:
self.directory = directory or ""
if not directory:
directory = os.getcwd()
elif not os.path.exists(directory):
raise FileNotFoundError(f"No such file or directory {directory}")
elif not os.path.isdir(directory):
raise ValueError(f"{directory} is not a directory")

self.directory = directory
self.paramfile = inifile or ""
self.code: Code
if code:
Expand Down Expand Up @@ -311,7 +318,7 @@ def funnel_on_type(
elif _code is Code.IDEFIX or _code is Code.PLUTO:
if isinstance(input_dataset, str):
filename = os.path.join(directory, input_dataset)
if (m := re.search(r"\d+", filename)) is None:
if (m := re.search(r"\d+", input_dataset)) is None:
raise ValueError("filename format is not correct")
else:
on = int(m.group())
Expand Down Expand Up @@ -441,7 +448,7 @@ def idfxReadVTK(
if V.geometry == "unknown":
raise RuntimeError(
"Geometry couldn't be determined from data. "
"Try to set the geometry keyword argument explicitely."
"Try to set the geometry keyword argument explicitly."
)

slist = s.split() # DIMENSIONS....
Expand Down
28 changes: 19 additions & 9 deletions tests/test_load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,28 @@ def test_roundtrip_other_dir(test_data_dir, tmp_path):
assert dsnpy.nfields == 1


def test_api_vtk_by_name(test_data_dir):
os.chdir(test_data_dir / "idefix_spherical_planet3d")

on = 500

ds = GasDataSet(f"data.{on:04d}.vtk")
assert ds.on == on
@pytest.mark.parametrize(
"from_abs_path",
[
pytest.param(True, id="from absolute path"),
pytest.param(False, id="from relative path"),
],
)
def test_api_vtk_by_name(test_data_dir, from_abs_path):
data_dir = test_data_dir / "idefix_spherical_planet3d"
if from_abs_path:
input_ = str((data_dir / "data.0500.vtk").absolute())
else:
os.chdir(data_dir)
input_ = "data.0500.vtk"

ds = GasDataSet(input_)
assert ds.on == 500

with pytest.raises(
FileNotFoundError, match="In idfxReadVTK: datawrong.0500.vtk not found."
FileNotFoundError, match=r"In idfxReadVTK: .*datawrong\.0500\.vtk not found\."
):
GasDataSet(f"datawrong.{on:04d}.vtk")
GasDataSet(input_.replace("data.0500", "datawrong.0500"))


def test_api_vtk_by_name_fargo(test_data_dir):
Expand Down

0 comments on commit e01d814

Please sign in to comment.