From 96c816cb585a9684dc6a1c26dd7154ba44343bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Tue, 12 Mar 2024 10:56:05 +0100 Subject: [PATCH 1/5] UX: improve error message in case an invalid directory name is used --- nonos/api/from_simulation.py | 9 ++++++++- tests/test_load_data.py | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/nonos/api/from_simulation.py b/nonos/api/from_simulation.py index 4a3bfd8f..afba294b 100644 --- a/nonos/api/from_simulation.py +++ b/nonos/api/from_simulation.py @@ -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: diff --git a/tests/test_load_data.py b/tests/test_load_data.py index 557504e9..257b4624 100644 --- a/tests/test_load_data.py +++ b/tests/test_load_data.py @@ -66,7 +66,7 @@ def test_api_vtk_by_name(test_data_dir): assert ds.on == on 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") From 72fb02d91efac69f2fe3295674948e7a11a24f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Tue, 12 Mar 2024 10:56:25 +0100 Subject: [PATCH 2/5] fix a typo in error message --- nonos/api/from_simulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nonos/api/from_simulation.py b/nonos/api/from_simulation.py index afba294b..42135977 100644 --- a/nonos/api/from_simulation.py +++ b/nonos/api/from_simulation.py @@ -448,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.... From cce5b61203260acabdf0318721f265b38aa7f271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Tue, 12 Mar 2024 11:34:17 +0100 Subject: [PATCH 3/5] TST: add regression tests for loading with absolute path --- tests/test_load_data.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/test_load_data.py b/tests/test_load_data.py index 257b4624..5873d95c 100644 --- a/tests/test_load_data.py +++ b/tests/test_load_data.py @@ -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=r"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): From ec8f16ad56df929fa377a2e9ca90f7f257cd1f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Tue, 12 Mar 2024 11:45:23 +0100 Subject: [PATCH 4/5] BUG: fix a bug where output number parsing could include false positives from directory name --- nonos/api/from_simulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nonos/api/from_simulation.py b/nonos/api/from_simulation.py index 42135977..e9043751 100644 --- a/nonos/api/from_simulation.py +++ b/nonos/api/from_simulation.py @@ -318,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()) From 4d680555dc3da651fe12382e2d093b594cc3d86d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Tue, 12 Mar 2024 11:59:52 +0100 Subject: [PATCH 5/5] BUG: fix loading from absolute paths --- nonos/api/analysis.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/nonos/api/analysis.py b/nonos/api/analysis.py index 70f77812..044d8ed3 100644 --- a/nonos/api/analysis.py +++ b/nonos/api/analysis.py @@ -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 @@ -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, @@ -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