From 8bbadadee02ad6eea176fb933f56e0e026bab3bc Mon Sep 17 00:00:00 2001 From: arjunsavel Date: Thu, 8 Feb 2024 19:36:35 -0500 Subject: [PATCH 1/3] test linalg error for bad cube --- src/cortecs/fit/fit_pca.py | 4 ++-- src/cortecs/tests/test_fit.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/cortecs/fit/fit_pca.py b/src/cortecs/fit/fit_pca.py index d8bc0fe..f429f52 100644 --- a/src/cortecs/fit/fit_pca.py +++ b/src/cortecs/fit/fit_pca.py @@ -97,9 +97,9 @@ def do_pca(cube, nc=3): try: xMat, s, vh, u = do_svd(standardized_cube, nc, nx) - except np.linalg.LinAlgError: + except np.linalg.LinAlgError as e: print("SVD did not converge.") - return + raise e return xMat, standardized_cube, s, vh, u diff --git a/src/cortecs/tests/test_fit.py b/src/cortecs/tests/test_fit.py index 722d80a..86e6b90 100644 --- a/src/cortecs/tests/test_fit.py +++ b/src/cortecs/tests/test_fit.py @@ -7,6 +7,7 @@ from cortecs.fit.fit_pca import * import os import sys +import numpy as np sys.path.insert(0, os.path.abspath(".")) @@ -56,3 +57,36 @@ def test_available_methods_assigned_func(self): fitter = Fitter(self.opac, method="pca") self.assertEqual(fitter.fit_func, fit_pca) + + +class TestFitUtils(unittest.TestCase): + """ + Test the fitter object itself + """ + + T_filename = os.path.abspath(".") + "/src/cortecs/tests/temperatures.npy" + P_filename = os.path.abspath(".") + "/src/cortecs/tests/pressures.npy" + wl_filename = os.path.abspath(".") + "/src/cortecs/tests/wavelengths.npy" + cross_sec_filename = ( + os.path.abspath(".") + "/src/cortecs/tests/absorb_coeffs_C2H4.npy" + ) + + load_kwargs = { + "T_filename": T_filename, + "P_filename": P_filename, + "wl_filename": wl_filename, + } + opac = Opac(cross_sec_filename, loader="platon", load_kwargs=load_kwargs) + + def test_nan_pca_cube_errors(self): + """ + if i pass nans, should fail. + :return: + """ + bad_cube = np.zeros((3, 3, 3)) * np.nan + + self.assertRaises( + np.linalg.LinAlgError, + do_pca, + bad_cube, + ) From c9289e5896d6677d4e9f0718d0b5e1bc3e7772bf Mon Sep 17 00:00:00 2001 From: arjunsavel Date: Thu, 8 Feb 2024 19:43:47 -0500 Subject: [PATCH 2/3] only 2d array --- src/cortecs/tests/test_fit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cortecs/tests/test_fit.py b/src/cortecs/tests/test_fit.py index 86e6b90..bd77357 100644 --- a/src/cortecs/tests/test_fit.py +++ b/src/cortecs/tests/test_fit.py @@ -83,7 +83,7 @@ def test_nan_pca_cube_errors(self): if i pass nans, should fail. :return: """ - bad_cube = np.zeros((3, 3, 3)) * np.nan + bad_cube = np.zeros((3, 3)) * np.nan self.assertRaises( np.linalg.LinAlgError, From f91a886b339722ab7b5547a5123bb9dc01ae817b Mon Sep 17 00:00:00 2001 From: arjunsavel Date: Thu, 8 Feb 2024 19:56:55 -0500 Subject: [PATCH 3/3] check more PCA error --- src/cortecs/tests/test_fit.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cortecs/tests/test_fit.py b/src/cortecs/tests/test_fit.py index bd77357..220b23b 100644 --- a/src/cortecs/tests/test_fit.py +++ b/src/cortecs/tests/test_fit.py @@ -86,7 +86,20 @@ def test_nan_pca_cube_errors(self): bad_cube = np.zeros((3, 3)) * np.nan self.assertRaises( - np.linalg.LinAlgError, + ValueError, + do_pca, + bad_cube, + ) + + def test_nan_pca_cube_errors(self): + """ + i want to make a linalg errror! + :return: + """ + bad_cube = np.zeros((3, 3)) * np.inf + + self.assertRaises( + ValueError, do_pca, bad_cube, )