From c6c087426ca97d0c3294314bc4c4ecdab3711199 Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Fri, 3 May 2024 14:39:47 -0700 Subject: [PATCH 1/9] add tests for filenotfound error --- tests/test_series.py | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/test_series.py b/tests/test_series.py index 207f438..cf1061c 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -4,6 +4,8 @@ from sleap_roots.series import Series, find_all_series from pathlib import Path from typing import Literal +from contextlib import redirect_stdout +import io @pytest.fixture @@ -56,6 +58,67 @@ def csv_path(tmp_path): ) return csv_path +def test_primary_prediction_not_found(tmp_path): + dummy_video_path = tmp_path / "dummy_video.mp4" + dummy_video_path.write_text("This is a dummy video file.") + + # Create a dummy Series instance with a non-existent primary prediction file + output = io.StringIO() + with redirect_stdout(output): + Series.load(h5_path=str(dummy_video_path), primary_name="nonexistent") + + # format file path string for assert statement + path_obj = Path(dummy_video_path) + parent_dir = path_obj.parent + filename_without_extension = path_obj.stem + new_file_path = parent_dir / filename_without_extension + + assert output.getvalue() == f"Primary prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + +def test_lateral_prediction_not_found(tmp_path): + dummy_video_path = tmp_path / "dummy_video.mp4" + dummy_video_path.write_text("This is a dummy video file.") + + # Create a dummy Series instance with a non-existent primary prediction file + output = io.StringIO() + with redirect_stdout(output): + Series.load(h5_path=str(dummy_video_path), lateral_name="nonexistent") + + # format file path string for assert statement + path_obj = Path(dummy_video_path) + parent_dir = path_obj.parent + filename_without_extension = path_obj.stem + new_file_path = parent_dir / filename_without_extension + + assert output.getvalue() == f"Lateral prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + +def test_crown_prediction_not_found(tmp_path): + dummy_video_path = tmp_path / "dummy_video.mp4" + dummy_video_path.write_text("This is a dummy video file.") + + # Create a dummy Series instance with a non-existent primary prediction file + output = io.StringIO() + with redirect_stdout(output): + Series.load(h5_path=str(dummy_video_path), crown_name="nonexistent") + + # format file path string for assert statement + path_obj = Path(dummy_video_path) + parent_dir = path_obj.parent + filename_without_extension = path_obj.stem + new_file_path = parent_dir / filename_without_extension + + assert output.getvalue() == f"Crown prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + +def test_video_loading_error(tmp_path): + # Create a dummy Series instance with an invalid video file path + invalid_video_path = tmp_path / "invalid_video.mp4" + + output = io.StringIO() + with redirect_stdout(output): + Series.load(h5_path=str(invalid_video_path)) + + # Check if the correct error message is output + assert output.getvalue() == f"Error loading video file {invalid_video_path}: File not found\n" def test_series_name(dummy_series): expected_name = "dummy_video" # Based on the dummy_video_path fixture @@ -85,6 +148,16 @@ def test_expected_count(series_instance, csv_path): assert series_instance.expected_count == 10 +def test_expected_count_error(series_instance, tmp_path): + series_instance.csv_path = tmp_path / "invalid" + + output = io.StringIO() + with redirect_stdout(output): + series_instance.expected_count + # Check if the correct error message is output + assert output.getvalue() == "CSV path is not set or the file does not exist.\n" + + def test_qc_cylinder(series_instance, csv_path): series_instance.csv_path = csv_path assert series_instance.qc_fail == 0 From 461e78c10687174f8da093a23f5a62d00baf5f5b Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Fri, 3 May 2024 14:40:40 -0700 Subject: [PATCH 2/9] if video is not found produce error --- sleap_roots/series.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sleap_roots/series.py b/sleap_roots/series.py index 5b9e4d6..c769fe4 100644 --- a/sleap_roots/series.py +++ b/sleap_roots/series.py @@ -114,7 +114,9 @@ def load( # Attempt to load the video, with error handling video = None try: - video = sio.Video.from_filename(h5_path) if Path(h5_path).exists() else None + if not Path(h5_path).exists(): + raise FileNotFoundError(f"File not found") + video = sio.Video.from_filename(h5_path) except Exception as e: print(f"Error loading video file {h5_path}: {e}") From 88fbc3f6e1fbeaf28090373179755ebdc99f8a1e Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Fri, 3 May 2024 14:48:17 -0700 Subject: [PATCH 3/9] black --- tests/test_series.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index cf1061c..35b4b98 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -58,6 +58,7 @@ def csv_path(tmp_path): ) return csv_path + def test_primary_prediction_not_found(tmp_path): dummy_video_path = tmp_path / "dummy_video.mp4" dummy_video_path.write_text("This is a dummy video file.") @@ -73,7 +74,11 @@ def test_primary_prediction_not_found(tmp_path): filename_without_extension = path_obj.stem new_file_path = parent_dir / filename_without_extension - assert output.getvalue() == f"Primary prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + assert ( + output.getvalue() + == f"Primary prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + ) + def test_lateral_prediction_not_found(tmp_path): dummy_video_path = tmp_path / "dummy_video.mp4" @@ -90,7 +95,11 @@ def test_lateral_prediction_not_found(tmp_path): filename_without_extension = path_obj.stem new_file_path = parent_dir / filename_without_extension - assert output.getvalue() == f"Lateral prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + assert ( + output.getvalue() + == f"Lateral prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + ) + def test_crown_prediction_not_found(tmp_path): dummy_video_path = tmp_path / "dummy_video.mp4" @@ -107,7 +116,11 @@ def test_crown_prediction_not_found(tmp_path): filename_without_extension = path_obj.stem new_file_path = parent_dir / filename_without_extension - assert output.getvalue() == f"Crown prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + assert ( + output.getvalue() + == f"Crown prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + ) + def test_video_loading_error(tmp_path): # Create a dummy Series instance with an invalid video file path @@ -118,7 +131,11 @@ def test_video_loading_error(tmp_path): Series.load(h5_path=str(invalid_video_path)) # Check if the correct error message is output - assert output.getvalue() == f"Error loading video file {invalid_video_path}: File not found\n" + assert ( + output.getvalue() + == f"Error loading video file {invalid_video_path}: File not found\n" + ) + def test_series_name(dummy_series): expected_name = "dummy_video" # Based on the dummy_video_path fixture From 887cbe4785045e82b25e04b831f67496090f756a Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Mon, 6 May 2024 13:18:28 -0700 Subject: [PATCH 4/9] condensed file formatting --- tests/test_series.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index 35b4b98..ad4f94c 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -69,10 +69,7 @@ def test_primary_prediction_not_found(tmp_path): Series.load(h5_path=str(dummy_video_path), primary_name="nonexistent") # format file path string for assert statement - path_obj = Path(dummy_video_path) - parent_dir = path_obj.parent - filename_without_extension = path_obj.stem - new_file_path = parent_dir / filename_without_extension + new_file_path = Path(dummy_video_path).with_suffix('') assert ( output.getvalue() @@ -90,10 +87,7 @@ def test_lateral_prediction_not_found(tmp_path): Series.load(h5_path=str(dummy_video_path), lateral_name="nonexistent") # format file path string for assert statement - path_obj = Path(dummy_video_path) - parent_dir = path_obj.parent - filename_without_extension = path_obj.stem - new_file_path = parent_dir / filename_without_extension + new_file_path = Path(dummy_video_path).with_suffix('') assert ( output.getvalue() @@ -111,10 +105,7 @@ def test_crown_prediction_not_found(tmp_path): Series.load(h5_path=str(dummy_video_path), crown_name="nonexistent") # format file path string for assert statement - path_obj = Path(dummy_video_path) - parent_dir = path_obj.parent - filename_without_extension = path_obj.stem - new_file_path = parent_dir / filename_without_extension + new_file_path = Path(dummy_video_path).with_suffix('') assert ( output.getvalue() From 297f4ba9c2f61e44b2f56e7a9245340317a2fe65 Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Mon, 6 May 2024 13:55:26 -0700 Subject: [PATCH 5/9] black --- tests/test_series.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index ad4f94c..0b34f2d 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -69,7 +69,7 @@ def test_primary_prediction_not_found(tmp_path): Series.load(h5_path=str(dummy_video_path), primary_name="nonexistent") # format file path string for assert statement - new_file_path = Path(dummy_video_path).with_suffix('') + new_file_path = Path(dummy_video_path).with_suffix("") assert ( output.getvalue() @@ -87,7 +87,7 @@ def test_lateral_prediction_not_found(tmp_path): Series.load(h5_path=str(dummy_video_path), lateral_name="nonexistent") # format file path string for assert statement - new_file_path = Path(dummy_video_path).with_suffix('') + new_file_path = Path(dummy_video_path).with_suffix("") assert ( output.getvalue() @@ -105,7 +105,7 @@ def test_crown_prediction_not_found(tmp_path): Series.load(h5_path=str(dummy_video_path), crown_name="nonexistent") # format file path string for assert statement - new_file_path = Path(dummy_video_path).with_suffix('') + new_file_path = Path(dummy_video_path).with_suffix("") assert ( output.getvalue() From bdcf3913696783b99adc9e8ae8ef1f24fc5b1bd3 Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Wed, 8 May 2024 14:33:06 -0700 Subject: [PATCH 6/9] Path lib --- tests/test_series.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index 0b34f2d..b0d7a6b 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -60,7 +60,7 @@ def csv_path(tmp_path): def test_primary_prediction_not_found(tmp_path): - dummy_video_path = tmp_path / "dummy_video.mp4" + dummy_video_path = Path(tmp_path) / "dummy_video.mp4" dummy_video_path.write_text("This is a dummy video file.") # Create a dummy Series instance with a non-existent primary prediction file @@ -78,7 +78,7 @@ def test_primary_prediction_not_found(tmp_path): def test_lateral_prediction_not_found(tmp_path): - dummy_video_path = tmp_path / "dummy_video.mp4" + dummy_video_path = Path(tmp_path) / "dummy_video.mp4" dummy_video_path.write_text("This is a dummy video file.") # Create a dummy Series instance with a non-existent primary prediction file @@ -96,7 +96,7 @@ def test_lateral_prediction_not_found(tmp_path): def test_crown_prediction_not_found(tmp_path): - dummy_video_path = tmp_path / "dummy_video.mp4" + dummy_video_path = Path(tmp_path) / "dummy_video.mp4" dummy_video_path.write_text("This is a dummy video file.") # Create a dummy Series instance with a non-existent primary prediction file @@ -115,7 +115,7 @@ def test_crown_prediction_not_found(tmp_path): def test_video_loading_error(tmp_path): # Create a dummy Series instance with an invalid video file path - invalid_video_path = tmp_path / "invalid_video.mp4" + invalid_video_path = Path(tmp_path) / "invalid_video.mp4" output = io.StringIO() with redirect_stdout(output): From 6eb8a075558618e049897b032c692972e5ab6379 Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Wed, 8 May 2024 14:42:28 -0700 Subject: [PATCH 7/9] path string --- tests/test_series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_series.py b/tests/test_series.py index b0d7a6b..c6a930e 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -66,7 +66,7 @@ def test_primary_prediction_not_found(tmp_path): # Create a dummy Series instance with a non-existent primary prediction file output = io.StringIO() with redirect_stdout(output): - Series.load(h5_path=str(dummy_video_path), primary_name="nonexistent") + Series.load(h5_path=dummy_video_path, primary_name="nonexistent") # format file path string for assert statement new_file_path = Path(dummy_video_path).with_suffix("") From 6492de720f7ef4932b7dfee350416ab840870ecf Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Wed, 8 May 2024 14:43:39 -0700 Subject: [PATCH 8/9] path string --- tests/test_series.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index c6a930e..4d63924 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -84,7 +84,7 @@ def test_lateral_prediction_not_found(tmp_path): # Create a dummy Series instance with a non-existent primary prediction file output = io.StringIO() with redirect_stdout(output): - Series.load(h5_path=str(dummy_video_path), lateral_name="nonexistent") + Series.load(h5_path=dummy_video_path, lateral_name="nonexistent") # format file path string for assert statement new_file_path = Path(dummy_video_path).with_suffix("") @@ -102,7 +102,7 @@ def test_crown_prediction_not_found(tmp_path): # Create a dummy Series instance with a non-existent primary prediction file output = io.StringIO() with redirect_stdout(output): - Series.load(h5_path=str(dummy_video_path), crown_name="nonexistent") + Series.load(h5_path=dummy_video_path, crown_name="nonexistent") # format file path string for assert statement new_file_path = Path(dummy_video_path).with_suffix("") @@ -119,7 +119,7 @@ def test_video_loading_error(tmp_path): output = io.StringIO() with redirect_stdout(output): - Series.load(h5_path=str(invalid_video_path)) + Series.load(h5_path=invalid_video_path) # Check if the correct error message is output assert ( From ceb9570c14a279b71de81040ede21fb88e8a6e6b Mon Sep 17 00:00:00 2001 From: emdavis02 Date: Wed, 8 May 2024 15:11:32 -0700 Subject: [PATCH 9/9] as_possix --- tests/test_series.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index 4d63924..6c861b2 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -69,7 +69,8 @@ def test_primary_prediction_not_found(tmp_path): Series.load(h5_path=dummy_video_path, primary_name="nonexistent") # format file path string for assert statement - new_file_path = Path(dummy_video_path).with_suffix("") + new_file_path = Path(dummy_video_path).with_suffix("").as_posix() + print(new_file_path) assert ( output.getvalue() @@ -87,7 +88,7 @@ def test_lateral_prediction_not_found(tmp_path): Series.load(h5_path=dummy_video_path, lateral_name="nonexistent") # format file path string for assert statement - new_file_path = Path(dummy_video_path).with_suffix("") + new_file_path = Path(dummy_video_path).with_suffix("").as_posix() assert ( output.getvalue() @@ -105,7 +106,7 @@ def test_crown_prediction_not_found(tmp_path): Series.load(h5_path=dummy_video_path, crown_name="nonexistent") # format file path string for assert statement - new_file_path = Path(dummy_video_path).with_suffix("") + new_file_path = Path(dummy_video_path).with_suffix("").as_posix() assert ( output.getvalue()