Update tests
This commit is contained in:
parent
03899fc52f
commit
c8728bba4d
@ -1,176 +1,88 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import torch
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import os
|
from AMBER.background import background
|
||||||
import sys
|
|
||||||
maindir = os.getcwd()
|
|
||||||
main_path = maindir[:maindir.find('ds4ms/code')]
|
|
||||||
sys.path.append(main_path+"/ds4ms/code/src")
|
|
||||||
from background import background
|
|
||||||
|
|
||||||
|
|
||||||
class TestBackground(unittest.TestCase):
|
class TestBackground(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.bg = background(data_path='/mydata/ds4ms/victor/Data/', str_dataset='VanadiumOzide', str_option='6p9T')
|
"""Set up mock data for testing."""
|
||||||
self.bg.load_data(verbose=True)
|
self.bg = background(dtype=np.float32)
|
||||||
self.bg.set_grid_volume(dqx=0.03, dqy=0.03, dE=0.08)
|
self.bg.set_gridcell_size(dqx=0.03, dqy=0.03, dE=0.1)
|
||||||
self.bg.set_binned_data()
|
Qx = np.linspace(-1, 1, 10)
|
||||||
self.bg.set_radial_bins(max_radius=6.0, n_bins=10)
|
Qy = np.linspace(-1, 1, 10)
|
||||||
self.bg.Ygrid = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
E = np.linspace(0, 1, 5)
|
||||||
|
Int = np.random.rand(len(Qx), len(Qy), len(E))
|
||||||
def test_load_data(self):
|
self.bg.set_binned_data(Qx, Qy, E, Int)
|
||||||
self.bg.load_data(verbose=True)
|
self.bg.set_radial_bins(max_radius=3.0, n_bins=100)
|
||||||
self.assertIsNotNone(self.bg.ds)
|
|
||||||
|
|
||||||
def test_set_dataset(self):
|
|
||||||
ds = "dummy_dataset"
|
|
||||||
self.bg.set_dataset(ds)
|
|
||||||
self.assertEqual(self.bg.ds, ds)
|
|
||||||
|
|
||||||
def test_mask_preprocessing(self):
|
|
||||||
self.bg.mask_preprocessing()
|
|
||||||
self.assertIsNotNone(self.bg.ds.mask)
|
|
||||||
|
|
||||||
def test_set_grid_volume(self):
|
|
||||||
self.bg.set_grid_volume(dqx=0.03, dqy=0.03, dE=0.08)
|
|
||||||
self.assertEqual(self.bg.dqx, 0.03)
|
|
||||||
self.assertEqual(self.bg.dqy, 0.03)
|
|
||||||
self.assertEqual(self.bg.dE, 0.08)
|
|
||||||
|
|
||||||
def test_set_binned_data(self):
|
|
||||||
self.bg.set_binned_data()
|
|
||||||
self.assertIsNotNone(self.bg.data)
|
|
||||||
self.assertIsNotNone(self.bg.bins)
|
|
||||||
|
|
||||||
def test_set_variables(self):
|
|
||||||
self.bg.set_variables()
|
|
||||||
self.assertIsNotNone(self.bg.X)
|
|
||||||
self.assertIsNotNone(self.bg.b)
|
|
||||||
self.assertIsNotNone(self.bg.b_grid)
|
|
||||||
|
|
||||||
def test_R_operator(self):
|
|
||||||
Y_r = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
b = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
result = self.bg.R_operator(Y_r, b)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_Rstar_operator(self):
|
|
||||||
Y_r = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
X = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
result = self.bg.Rstar_operator(Y_r, X)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_mask_nans(self):
|
|
||||||
x = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
result = self.bg.mask_nans(x)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_S_lambda(self):
|
|
||||||
x = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
lambda_ = torch.tensor(np.random.rand(10), dtype=torch.float64)
|
|
||||||
result = self.bg.S_lambda(x, lambda_)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_L_b(self):
|
|
||||||
self.bg.set_radial_nans()
|
|
||||||
result = self.bg.L_b(e_cut=0)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_gamma_matrix(self):
|
|
||||||
self.bg.set_radial_nans()
|
|
||||||
result = self.bg.gamma_matrix(e_cut=0)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_set_radial_nans(self):
|
|
||||||
self.bg.set_radial_nans()
|
|
||||||
self.assertIsNotNone(self.bg.u)
|
|
||||||
|
|
||||||
def test_set_b_design_matrix(self):
|
|
||||||
beta_ = torch.tensor(1.0, dtype=torch.float64)
|
|
||||||
alpha_ = torch.tensor(np.random.rand(10), dtype=torch.float64)
|
|
||||||
result = self.bg.set_b_design_matrix(beta_, alpha_, e_cut=0)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_compute_laplacian(self):
|
|
||||||
Y_r = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
result = self.bg.compute_laplacian(Y_r)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_compute_all_laplacians(self):
|
|
||||||
Y_r = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
self.bg.compute_all_laplacians(Y_r)
|
|
||||||
self.assertIsNotNone(self.bg.L_list)
|
|
||||||
|
|
||||||
def test_TV_denoising(self):
|
|
||||||
gamma_ = torch.tensor(np.random.rand(10), dtype=torch.float64)
|
|
||||||
result = self.bg.TV_denoising(gamma_, n_epochs=1, verbose=False)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_L_e(self):
|
|
||||||
result = self.bg.L_e()
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_set_e_design_matrix(self):
|
|
||||||
result = self.bg.set_e_design_matrix(mu_=1.0)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_MAD_lambda(self):
|
|
||||||
result = self.bg.MAD_lambda()
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_mu_estimator(self):
|
|
||||||
result = self.bg.mu_estimator()
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_denoising(self):
|
|
||||||
Y_r = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
lambda_ = torch.tensor(np.random.rand(10), dtype=torch.float64)
|
|
||||||
beta_ = torch.tensor(np.random.rand(10), dtype=torch.float64)
|
|
||||||
alpha_ = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
self.bg.denoising(Y_r, lambda_, beta_, alpha_, mu_=1.0, n_epochs=1, verbose=False)
|
|
||||||
self.assertIsNotNone(self.bg.X)
|
|
||||||
self.assertIsNotNone(self.bg.b)
|
|
||||||
|
|
||||||
def test_applyBackground(self):
|
|
||||||
self.bg.applyBackground(median=False)
|
|
||||||
self.assertIsNotNone(self.bg.ds.backgroundModel)
|
|
||||||
|
|
||||||
def test_median_bg(self):
|
|
||||||
result = self.bg.median_bg(self.bg.Ygrid)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_compute_signal_to_noise(self):
|
|
||||||
b = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
result = self.bg.compute_signal_to_noise(b)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_plot_snr(self):
|
|
||||||
b = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
self.bg.plot_snr(b, e_cut=range(2, 4), fmin=0.0, fmax=0.1)
|
|
||||||
|
|
||||||
def test_save_arrays(self):
|
|
||||||
self.bg.save_arrays(median=True)
|
|
||||||
self.assertTrue(os.path.exists('arrays'))
|
|
||||||
|
|
||||||
def test_compute_signal_to_obs(self):
|
|
||||||
b = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
|
||||||
result = self.bg.compute_signal_to_obs(b)
|
|
||||||
self.assertIsNotNone(result)
|
|
||||||
|
|
||||||
def test_cross_validation(self):
|
def test_cross_validation(self):
|
||||||
lambda_range = torch.tensor([1.0])
|
"""Test the cross_validation method."""
|
||||||
alpha_range = torch.tensor([1.0])
|
q = 0.75
|
||||||
beta_range = torch.tensor([1.0])
|
beta_range = np.array([1.0, 2.0])
|
||||||
mu_range = torch.tensor([1.0])
|
lambda_ = 0.01
|
||||||
result = self.bg.cross_validation(lambda_range=lambda_range, alpha_range=alpha_range,
|
mu_ = 0.001
|
||||||
beta_range=beta_range, mu_range=mu_range, n_epochs=1, verbose=False)
|
n_epochs = 5
|
||||||
self.assertIsNotNone(result)
|
rmse = self.bg.cross_validation(q, beta_range, lambda_, mu_, n_epochs, verbose=False)
|
||||||
|
self.assertEqual(len(rmse), len(beta_range))
|
||||||
|
self.assertTrue(np.all(rmse >= 0))
|
||||||
|
|
||||||
def test_compute_mask(self):
|
def test_set_gridcell_size(self):
|
||||||
result = self.bg.compute_mask(q=0.75, e_cut=None)
|
"""Test set_gridcell_size method."""
|
||||||
self.assertIsNotNone(result)
|
self.bg.set_gridcell_size(dqx=0.05, dqy=0.05, dE=0.2)
|
||||||
|
self.assertEqual(self.bg.dqx, 0.05)
|
||||||
|
self.assertEqual(self.bg.dqy, 0.05)
|
||||||
|
self.assertEqual(self.bg.dE, 0.2)
|
||||||
|
|
||||||
|
def test_set_binned_data(self):
|
||||||
|
"""Test set_binned_data method."""
|
||||||
|
Qx = np.linspace(-2, 2, 20)
|
||||||
|
Qy = np.linspace(-2, 2, 20)
|
||||||
|
E = np.linspace(0, 2, 10)
|
||||||
|
Int = np.random.rand(len(Qx), len(Qy), len(E))
|
||||||
|
self.bg.set_binned_data(Qx, Qy, E, Int)
|
||||||
|
self.assertEqual(self.bg.Qx_size, len(Qx))
|
||||||
|
self.assertEqual(self.bg.Qy_size, len(Qy))
|
||||||
|
self.assertEqual(self.bg.E_size, len(E))
|
||||||
|
|
||||||
|
def test_set_radial_bins(self):
|
||||||
|
"""Test set_radial_bins method."""
|
||||||
|
self.bg.set_radial_bins(max_radius=5.0, n_bins=50)
|
||||||
|
self.assertEqual(self.bg.max_radius, 5.0)
|
||||||
|
self.assertEqual(self.bg.n_bins, 50)
|
||||||
|
self.assertEqual(len(self.bg.r_range), 51)
|
||||||
|
|
||||||
|
def test_R_operator(self):
|
||||||
|
"""Test R_operator method."""
|
||||||
|
b = np.random.rand(self.bg.E_size, self.bg.n_bins)
|
||||||
|
b_grid = self.bg.R_operator(b)
|
||||||
|
self.assertEqual(b_grid.shape, (self.bg.E_size, self.bg.Qx_size * self.bg.Qy_size))
|
||||||
|
|
||||||
|
def test_Rstar_operator(self):
|
||||||
|
"""Test Rstar_operator method."""
|
||||||
|
X = np.random.rand(self.bg.E_size, self.bg.Qx_size * self.bg.Qy_size)
|
||||||
|
v_agg = self.bg.Rstar_operator(X)
|
||||||
|
self.assertEqual(v_agg.shape, (self.bg.E_size, self.bg.n_bins))
|
||||||
|
|
||||||
|
def test_gamma_matrix(self):
|
||||||
|
"""Test gamma_matrix method."""
|
||||||
|
gamma_mat = self.bg.gamma_matrix()
|
||||||
|
self.assertEqual(gamma_mat.shape, (self.bg.n_bins, self.bg.n_bins))
|
||||||
|
|
||||||
|
def test_mask_nans(self):
|
||||||
|
"""Test mask_nans method."""
|
||||||
|
x = np.random.rand(self.bg.E_size, self.bg.Qx_size * self.bg.Qy_size)
|
||||||
|
x[0, 0] = np.nan
|
||||||
|
masked_x = self.bg.mask_nans(x)
|
||||||
|
self.assertTrue(np.isnan(masked_x[0, 0]))
|
||||||
|
|
||||||
|
def test_S_lambda(self):
|
||||||
|
"""Test S_lambda method."""
|
||||||
|
x = np.array([1.0, -2.0, 0.5])
|
||||||
|
lambda_ = 1.0
|
||||||
|
result = self.bg.S_lambda(x, lambda_)
|
||||||
|
expected = np.array([0.0, -1.0, 0.0])
|
||||||
|
np.testing.assert_array_almost_equal(result, expected)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
@ -4,10 +4,10 @@ import torch
|
|||||||
from scipy.sparse import lil_matrix, csr_matrix, coo_matrix
|
from scipy.sparse import lil_matrix, csr_matrix, coo_matrix
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
maindir = os.getcwd()
|
#maindir = os.getcwd()
|
||||||
main_path = maindir[:maindir.find('ds4ms/code')]
|
#main_path = maindir[:maindir.find('ds4ms/code')]
|
||||||
sys.path.append(main_path+"/ds4ms/code/src")
|
#sys.path.append(main_path+"/ds4ms/code/src")
|
||||||
from graph_laplacian import create_laplacian_matrix, delete_from_csr, \
|
from AMBER.graph_laplacian import create_laplacian_matrix, delete_from_csr, \
|
||||||
remove_vertex, laplacian, unnormalized_laplacian, laplacian_chain, \
|
remove_vertex, laplacian, unnormalized_laplacian, laplacian_chain, \
|
||||||
unnormalized_laplacian_chain
|
unnormalized_laplacian_chain
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user