Make flake8 linting

Add gitignore
This commit is contained in:
Jakob Lass 2025-04-09 17:27:28 +02:00
parent c689f35b76
commit e57a01b9a1
4 changed files with 83 additions and 77 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
tests/__pycache__/*
*.pyc

View File

@ -2,12 +2,13 @@
import numpy as np import numpy as np
import scipy import scipy
import torch import torch
from scipy.sparse import lil_matrix, block_diag,csr_array,diags,csr_matrix 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
---------- ----------
@ -24,23 +25,23 @@ def create_laplacian_matrix(nx, ny=None):
""" """
if ny is None: if ny is None:
ny = nx ny = nx
assert(nx>1) assert (nx > 1)
assert(ny>1) assert (ny > 1)
#Blocks corresponding to the corner of the image (linking row elements) # Blocks corresponding to the corner of the image (linking row elements)
top_block=lil_matrix((ny,ny),dtype=np.float32) top_block = lil_matrix((ny, ny), dtype=np.float32)
top_block.setdiag([2]+[3]*(ny-2)+[2]) top_block.setdiag([2] + [3] * (ny - 2) + [2])
top_block.setdiag(-1,k=1) top_block.setdiag(-1, k=1)
top_block.setdiag(-1,k=-1) top_block.setdiag(-1, k=-1)
#Blocks corresponding to the middle of the image (linking row elements) # Blocks corresponding to the middle of the image (linking row elements)
mid_block=lil_matrix((ny,ny),dtype=np.float32) mid_block = lil_matrix((ny, ny), dtype=np.float32)
mid_block.setdiag([3]+[4]*(ny-2)+[3]) mid_block.setdiag([3] + [4]*(ny - 2) + [3])
mid_block.setdiag(-1,k=1) mid_block.setdiag(-1, k=1)
mid_block.setdiag(-1,k=-1) mid_block.setdiag(-1, k=-1)
#Construction of the diagonal of blocks # Construction of the diagonal of blocks
list_blocks=[top_block]+[mid_block]*(nx-2)+[top_block] list_blocks = [top_block] + [mid_block]*(nx-2) + [top_block]
blocks=block_diag(list_blocks) blocks = block_diag(list_blocks)
#Diagonals linking different rows # Diagonals linking different rows
blocks.setdiag(-1,k=ny) blocks.setdiag(-1, k=ny)
return blocks return blocks
@ -64,7 +65,7 @@ def delete_from_csr(mat, row_indices=[], col_indices=[]):
row_mask[rows] = False row_mask[rows] = False
col_mask = np.ones(mat.shape[1], dtype=bool) col_mask = np.ones(mat.shape[1], dtype=bool)
col_mask[cols] = False col_mask[cols] = False
return mat[row_mask][:,col_mask] return mat[row_mask][:, col_mask]
elif len(rows) > 0: elif len(rows) > 0:
mask = np.ones(mat.shape[0], dtype=bool) mask = np.ones(mat.shape[0], dtype=bool)
mask[rows] = False mask[rows] = False
@ -72,11 +73,12 @@ def delete_from_csr(mat, row_indices=[], col_indices=[]):
elif len(cols) > 0: elif len(cols) > 0:
mask = np.ones(mat.shape[1], dtype=bool) mask = np.ones(mat.shape[1], dtype=bool)
mask[cols] = False mask[cols] = False
return mat[:,mask] return mat[:, mask]
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,10 +87,10 @@ 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.
Args: Args:
@ -96,16 +98,17 @@ 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())
# create Laplacian # create Laplacian
L = create_laplacian_matrix(nx, ny=ny) L = create_laplacian_matrix(nx, ny=ny)
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.
@ -114,11 +117,11 @@ def unnormalized_laplacian(y, nx, ny=None, method='inverse'):
- method: str indicating how to compute the weight of the unnormalized laplacian - method: str indicating how to compute the weight of the unnormalized laplacian
""" """
# create laplacian matrix # create laplacian matrix
lapl_tmp = laplacian(y,nx,ny) lapl_tmp = laplacian(y, nx, ny)
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])
@ -129,7 +132,7 @@ def unnormalized_laplacian(y, nx, ny=None, method='inverse'):
# construction of the non zeros weights # construction of the non zeros weights
if method == 'inverse': if method == 'inverse':
nnz_w = 1/(np.abs(y_tmp[idx_rows] - y_tmp[idx_cols])+1e-4) nnz_w = 1/(np.abs(y_tmp[idx_rows] - y_tmp[idx_cols]) + 1e-4)
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]))
@ -144,15 +147,16 @@ 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.
""" """
L = np.zeros((nb_vertices,nb_vertices)) L = np.zeros((nb_vertices, nb_vertices))
# First vertex # First vertex
L[0,0] = 1 L[0, 0] = 1
L[0,1] = -1 L[0, 1] = -1
# Toeplitz matrix # Toeplitz matrix
if nb_vertices > 2: if nb_vertices > 2:
@ -166,14 +170,15 @@ def laplacian_chain(nb_vertices):
D = scipy.linalg.toeplitz(first_col, r=first_row) D = scipy.linalg.toeplitz(first_col, r=first_row)
L[1:nb_vertices-1,:] = D L[1:nb_vertices-1, :] = D
# Last vertex # Last vertex
L[-1,-2] = -1 L[-1, -2] = -1
L[-1,-1] = 1 L[-1, -1] = 1
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)

View File

@ -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()

View File

@ -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()