Commit from local

This commit is contained in:
Jakob Lass
2025-04-09 16:46:45 +02:00
parent 19e0cc92af
commit 6865e2cbff
7 changed files with 544 additions and 5 deletions

177
tests/test_background.py Normal file
View File

@ -0,0 +1,177 @@
import unittest
import torch
import numpy as np
import os
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):
def setUp(self):
self.bg = background(data_path='/mydata/ds4ms/victor/Data/', str_dataset='VanadiumOzide', str_option='6p9T')
self.bg.load_data(verbose=True)
self.bg.set_grid_volume(dqx=0.03, dqy=0.03, dE=0.08)
self.bg.set_binned_data()
self.bg.set_radial_bins(max_radius=6.0, n_bins=10
self.bg.Ygrid = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
def test_load_data(self):
self.bg.load_data(verbose=True)
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):
lambda_range = torch.tensor([1.0])
alpha_range = torch.tensor([1.0])
beta_range = torch.tensor([1.0])
mu_range = torch.tensor([1.0])
result = self.bg.cross_validation(lambda_range=lambda_range, alpha_range=alpha_range, beta_range=beta_range, mu_range=mu_range, n_epochs=1, verbose=False)
self.assertIsNotNone(result)
def test_compute_mask(self):
result = self.bg.compute_mask(q=0.75, e_cut=None)
self.assertIsNotNone(result)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,70 @@
import unittest
import numpy as np
import torch
from scipy.sparse import lil_matrix, csr_matrix, coo_matrix
import os
import sys
maindir = os.getcwd()
main_path = maindir[:maindir.find('ds4ms/code')]
sys.path.append(main_path+"/ds4ms/code/src")
from graph_laplacian import create_laplacian_matrix, delete_from_csr, remove_vertex, laplacian, unnormalized_laplacian, laplacian_chain, unnormalized_laplacian_chain
class TestGraphLaplacian(unittest.TestCase):
def test_create_laplacian_matrix(self):
nx, ny = 3, 3
L = create_laplacian_matrix(nx, ny)
self.assertEqual(L.shape, (nx*ny, nx*ny))
self.assertIsInstance(L, coo_matrix)
def test_delete_from_csr(self):
mat = lil_matrix((4, 4), dtype=np.float32)
mat.setdiag([1, 2, 3, 4])
mat = mat.tocsr()
print(mat)
mat = delete_from_csr(mat, row_indices=[1], col_indices=[2])
print(mat)
self.assertEqual(mat.shape, (3, 3))
self.assertEqual(mat[1, 1], 0.0)
def test_remove_vertex(self):
nx, ny = 3, 3
L = create_laplacian_matrix(nx, ny)
L_cut = remove_vertex(L, lst_rows=[0], lst_cols=[0])
self.assertEqual(L_cut.shape, (nx*ny-1, nx*ny-1))
self.assertTrue((L_cut.sum(axis=1).A1 == np.zeros(L_cut.shape[0])).all())
def test_laplacian(self):
Y = torch.tensor([[1.0, np.nan, 3.0], [4.0, 5.0, np.nan], [7.0, 8.0, 9.0]])
nx, ny = Y.shape
L = laplacian(Y, nx, ny)
self.assertEqual(L.shape, (nx*ny-2, nx*ny-2))
self.assertTrue((L.sum(axis=1).A1 == np.zeros(L.shape[0])).all())
def test_unnormalized_laplacian(self):
y = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
nx, ny = y.shape
L = unnormalized_laplacian(y, nx, ny, method='inverse')
self.assertEqual(L.shape, (nx*ny, nx*ny))
self.assertIsInstance(L, csr_matrix)
def test_laplacian_chain(self):
nb_vertices = 5
L = laplacian_chain(nb_vertices)
self.assertEqual(L.shape, (nb_vertices, nb_vertices))
self.assertEqual(L[0, 0], 1)
self.assertEqual(L[0, 1], -1)
self.assertEqual(L[-1, -2], -1)
self.assertEqual(L[-1, -1], 1)
def test_unnormalized_laplacian_chain(self):
y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
nx = len(y)
L = unnormalized_laplacian_chain(y, nx, method='inverse')
self.assertEqual(L.shape, (nx, nx))
self.assertIsInstance(L, csr_matrix)
if __name__ == '__main__':
unittest.main()