From 1841f6572f62f26968f8c04201217f70ac32c0e1 Mon Sep 17 00:00:00 2001 From: Anthony Gitter Date: Thu, 13 Jun 2024 21:47:18 -0500 Subject: [PATCH] Switch error type and raise error when file missing --- spras/analysis/ml.py | 9 ++++----- test/ml/test_ml.py | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/spras/analysis/ml.py b/spras/analysis/ml.py index 1c85d3da..a1571988 100644 --- a/spras/analysis/ml.py +++ b/spras/analysis/ml.py @@ -61,9 +61,8 @@ def summarize_networks(file_paths: Iterable[Union[str, PathLike]]) -> pd.DataFra p = PurePath(file) edge_tuples.append((p.parts[-2], edges)) - except FileNotFoundError: - print(file, ' not found during ML analysis') # should not hit this - continue + except FileNotFoundError as exc: + raise FileNotFoundError(str(file) + ' not found during ML analysis') from exc # initially construct separate dataframes per algorithm edge_dataframes = [] @@ -84,10 +83,10 @@ def summarize_networks(file_paths: Iterable[Union[str, PathLike]]) -> pd.DataFra # don't do ml post-processing if there is an empty dataframe or the number of samples is <= 1 if concated_df.empty: - raise OSError("ML post-processing cannot proceed because the summarize network dataframe is empty.\nWe " + raise ValueError("ML post-processing cannot proceed because the summarize network dataframe is empty.\nWe " "suggest setting ml include: false in the configuration file to avoid this error.") if min(concated_df.shape) <= 1: - raise OSError(f"ML post-processing cannot proceed because the available number of pathways is insufficient. " + raise ValueError(f"ML post-processing cannot proceed because the available number of pathways is insufficient. " f"The ml post-processing requires more than one pathway, but currently " f"there are only {min(concated_df.shape)} pathways.") diff --git a/test/ml/test_ml.py b/test/ml/test_ml.py index 95768817..93a9cc43 100644 --- a/test/ml/test_ml.py +++ b/test/ml/test_ml.py @@ -27,11 +27,11 @@ def test_summarize_networks(self): assert filecmp.cmp(OUT_DIR + 'dataframe.csv', EXPECT_DIR + 'expected-dataframe.csv', shallow=False) def test_summarize_networks_empty(self): - with pytest.raises(OSError): #raises error if empty dataframe is used for post processing - ml.summarize_networks([INPUT_DIR + 'test-data-empty/empty.txt']) + with pytest.raises(ValueError): #raises error if empty dataframe is used for post processing + ml.summarize_networks([INPUT_DIR + 'test-data-empty/emptya.txt']) def test_single_line(self): - with pytest.raises(OSError): #raises error if single line in file s.t. single row in dataframe is used for post processing + with pytest.raises(ValueError): #raises error if single line in file s.t. single row in dataframe is used for post processing ml.summarize_networks([INPUT_DIR + 'test-data-single/single.txt']) def test_pca(self):