Make flake8 linting
Add gitignore
This commit is contained in:
parent
c689f35b76
commit
e57a01b9a1
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
tests/__pycache__/*
|
||||||
|
*.pyc
|
@ -7,7 +7,8 @@ from scipy.sparse import lil_matrix, block_diag,csr_array,diags,csr_matrix
|
|||||||
|
|
||||||
def create_laplacian_matrix(nx, ny=None):
|
def create_laplacian_matrix(nx, ny=None):
|
||||||
"""
|
"""
|
||||||
Helper method to create the laplacian matrix for the laplacian regularization
|
Helper method to create the laplacian matrix for the laplacian
|
||||||
|
regularization
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
@ -76,6 +77,7 @@ def delete_from_csr(mat, row_indices=[], col_indices=[]):
|
|||||||
else:
|
else:
|
||||||
return mat
|
return mat
|
||||||
|
|
||||||
|
|
||||||
def remove_vertex(L, lst_rows=[], lst_cols=[]):
|
def remove_vertex(L, lst_rows=[], lst_cols=[]):
|
||||||
"""
|
"""
|
||||||
Function that removes a vertex and adjust the graph laplacian matrix.
|
Function that removes a vertex and adjust the graph laplacian matrix.
|
||||||
@ -85,9 +87,9 @@ def remove_vertex(L,lst_rows=[],lst_cols=[]):
|
|||||||
L_cut = L_cut - diags(L_cut.sum(axis=1).A1)
|
L_cut = L_cut - diags(L_cut.sum(axis=1).A1)
|
||||||
assert (L_cut.sum(axis=1).A1 == np.zeros(L_cut.shape[0])).all()
|
assert (L_cut.sum(axis=1).A1 == np.zeros(L_cut.shape[0])).all()
|
||||||
|
|
||||||
|
|
||||||
return L_cut
|
return L_cut
|
||||||
|
|
||||||
|
|
||||||
def laplacian(Y, nx, ny=None):
|
def laplacian(Y, nx, ny=None):
|
||||||
"""
|
"""
|
||||||
Function that removes the vertices corresponding to Nan locations of tensor Y.
|
Function that removes the vertices corresponding to Nan locations of tensor Y.
|
||||||
@ -96,7 +98,7 @@ def laplacian(Y,nx,ny=None):
|
|||||||
- nx,ny: dimensions of the image (Int64)
|
- nx,ny: dimensions of the image (Int64)
|
||||||
"""
|
"""
|
||||||
# find Nan indices
|
# find Nan indices
|
||||||
Nan_indices = torch.where(torch.isnan(Y.ravel())==True)[0]
|
Nan_indices = torch.where(torch.isnan(Y.ravel()) is True)[0]
|
||||||
|
|
||||||
# get list of indices
|
# get list of indices
|
||||||
list_idx = list(Nan_indices.detach().numpy())
|
list_idx = list(Nan_indices.detach().numpy())
|
||||||
@ -106,6 +108,7 @@ def laplacian(Y,nx,ny=None):
|
|||||||
L = remove_vertex(L, list_idx, list_idx)
|
L = remove_vertex(L, list_idx, list_idx)
|
||||||
return L
|
return L
|
||||||
|
|
||||||
|
|
||||||
def unnormalized_laplacian(y, nx, ny=None, method='inverse'):
|
def unnormalized_laplacian(y, nx, ny=None, method='inverse'):
|
||||||
"""Construct numpy array with non zeros weights and non zeros indices.
|
"""Construct numpy array with non zeros weights and non zeros indices.
|
||||||
|
|
||||||
@ -118,7 +121,7 @@ def unnormalized_laplacian(y, nx, ny=None, method='inverse'):
|
|||||||
lapl_tmp.setdiag(np.zeros(nx*ny))
|
lapl_tmp.setdiag(np.zeros(nx*ny))
|
||||||
|
|
||||||
# select the non nan indices
|
# select the non nan indices
|
||||||
y_tmp = y[torch.isnan(y)==False]
|
y_tmp = y[torch.isnan(y) is False]
|
||||||
|
|
||||||
# store non zero indices
|
# store non zero indices
|
||||||
idx_rows = np.array(lapl_tmp.nonzero()[0])
|
idx_rows = np.array(lapl_tmp.nonzero()[0])
|
||||||
@ -144,6 +147,7 @@ def unnormalized_laplacian(y, nx, ny=None, method='inverse'):
|
|||||||
|
|
||||||
return L
|
return L
|
||||||
|
|
||||||
|
|
||||||
def laplacian_chain(nb_vertices):
|
def laplacian_chain(nb_vertices):
|
||||||
"""
|
"""
|
||||||
Construct the Laplacian matrix of a chain.
|
Construct the Laplacian matrix of a chain.
|
||||||
@ -174,6 +178,7 @@ def laplacian_chain(nb_vertices):
|
|||||||
|
|
||||||
return L
|
return L
|
||||||
|
|
||||||
|
|
||||||
def unnormalized_laplacian_chain(y, nx, method='inverse'):
|
def unnormalized_laplacian_chain(y, nx, method='inverse'):
|
||||||
"""Construct numpy array with non zeros weights and non zeros indices.
|
"""Construct numpy array with non zeros weights and non zeros indices.
|
||||||
|
|
||||||
@ -192,7 +197,6 @@ def unnormalized_laplacian_chain(y, nx, method='inverse'):
|
|||||||
idx_rows = np.array(lapl_tmp.nonzero()[0])
|
idx_rows = np.array(lapl_tmp.nonzero()[0])
|
||||||
idx_cols = np.array(lapl_tmp.nonzero()[1])
|
idx_cols = np.array(lapl_tmp.nonzero()[1])
|
||||||
|
|
||||||
|
|
||||||
# construct the set of weights
|
# construct the set of weights
|
||||||
nnz_w = np.zeros_like(idx_rows, dtype=np.float32)
|
nnz_w = np.zeros_like(idx_rows, dtype=np.float32)
|
||||||
|
|
||||||
@ -202,7 +206,6 @@ def unnormalized_laplacian_chain(y, nx, method='inverse'):
|
|||||||
else:
|
else:
|
||||||
nnz_w = np.exp(-np.abs(y_tmp[idx_rows] - y_tmp[idx_cols]))
|
nnz_w = np.exp(-np.abs(y_tmp[idx_rows] - y_tmp[idx_cols]))
|
||||||
|
|
||||||
|
|
||||||
# construct the non diagonal terms of the Laplacian
|
# construct the non diagonal terms of the Laplacian
|
||||||
lapl_nondiag = csr_array((nnz_w, (idx_rows, idx_cols)), shape=(lapl_tmp.shape[0], lapl_tmp.shape[0]), dtype=np.float32)
|
lapl_nondiag = csr_array((nnz_w, (idx_rows, idx_cols)), shape=(lapl_tmp.shape[0], lapl_tmp.shape[0]), dtype=np.float32)
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import torch
|
import torch
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
maindir = os.getcwd()
|
maindir = os.getcwd()
|
||||||
@ -11,7 +9,6 @@ sys.path.append(main_path+"/ds4ms/code/src")
|
|||||||
from background import background
|
from background import background
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestBackground(unittest.TestCase):
|
class TestBackground(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -19,7 +16,7 @@ class TestBackground(unittest.TestCase):
|
|||||||
self.bg.load_data(verbose=True)
|
self.bg.load_data(verbose=True)
|
||||||
self.bg.set_grid_volume(dqx=0.03, dqy=0.03, dE=0.08)
|
self.bg.set_grid_volume(dqx=0.03, dqy=0.03, dE=0.08)
|
||||||
self.bg.set_binned_data()
|
self.bg.set_binned_data()
|
||||||
self.bg.set_radial_bins(max_radius=6.0, n_bins=10
|
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)
|
self.bg.Ygrid = torch.tensor(np.random.rand(10, 10), dtype=torch.float64)
|
||||||
|
|
||||||
def test_load_data(self):
|
def test_load_data(self):
|
||||||
@ -166,12 +163,14 @@ class TestBackground(unittest.TestCase):
|
|||||||
alpha_range = torch.tensor([1.0])
|
alpha_range = torch.tensor([1.0])
|
||||||
beta_range = torch.tensor([1.0])
|
beta_range = torch.tensor([1.0])
|
||||||
mu_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)
|
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)
|
self.assertIsNotNone(result)
|
||||||
|
|
||||||
def test_compute_mask(self):
|
def test_compute_mask(self):
|
||||||
result = self.bg.compute_mask(q=0.75, e_cut=None)
|
result = self.bg.compute_mask(q=0.75, e_cut=None)
|
||||||
self.assertIsNotNone(result)
|
self.assertIsNotNone(result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
@ -2,14 +2,15 @@ import unittest
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
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, remove_vertex, laplacian, unnormalized_laplacian, laplacian_chain, unnormalized_laplacian_chain
|
from graph_laplacian import create_laplacian_matrix, delete_from_csr, \
|
||||||
|
remove_vertex, laplacian, unnormalized_laplacian, laplacian_chain, \
|
||||||
|
unnormalized_laplacian_chain
|
||||||
|
|
||||||
|
|
||||||
class TestGraphLaplacian(unittest.TestCase):
|
class TestGraphLaplacian(unittest.TestCase):
|
||||||
|
|
||||||
@ -66,5 +67,6 @@ class TestGraphLaplacian(unittest.TestCase):
|
|||||||
self.assertEqual(L.shape, (nx, nx))
|
self.assertEqual(L.shape, (nx, nx))
|
||||||
self.assertIsInstance(L, csr_matrix)
|
self.assertIsInstance(L, csr_matrix)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user