ngaggion's picture
First push
e87a462
import numpy as np
import scipy.sparse as sp
import torch
def scipy_to_torch_sparse(scp_matrix):
values = scp_matrix.data
indices = np.vstack((scp_matrix.row, scp_matrix.col))
i = torch.LongTensor(indices)
v = torch.FloatTensor(values)
shape = scp_matrix.shape
sparse_tensor = torch.sparse.FloatTensor(i, v, torch.Size(shape))
return sparse_tensor
## Adjacency Matrix
def mOrgan(N):
sub = np.zeros([N, N])
for i in range(0, N):
sub[i, i-1] = 1
sub[i, (i+1)%N] = 1
return sub
## Downsampling Matrix
def mOrganD(N):
N2 = int(np.ceil(N/2))
sub = np.zeros([N2, N])
for i in range(0, N2):
if (2*i+1) == N:
sub[i, 2*i] = 1
else:
sub[i, 2*i] = 1/2
sub[i, 2*i+1] = 1/2
return sub
def mOrganU(N):
N2 = int(np.ceil(N/2))
sub = np.zeros([N, N2])
for i in range(0, N):
if i % 2 == 0:
sub[i, i//2] = 1
else:
sub[i, i//2] = 1/2
sub[i, (i//2 + 1) % N2] = 1/2
return sub
def genMatrixesLungsHeart():
RLUNG = 44
LLUNG = 50
HEART = 26
Asub1 = mOrgan(RLUNG)
Asub2 = mOrgan(LLUNG)
Asub3 = mOrgan(HEART)
ADsub1 = mOrgan(int(np.ceil(RLUNG / 2)))
ADsub2 = mOrgan(int(np.ceil(LLUNG / 2)))
ADsub3 = mOrgan(int(np.ceil(HEART / 2)))
Dsub1 = mOrganD(RLUNG)
Dsub2 = mOrganD(LLUNG)
Dsub3 = mOrganD(HEART)
Usub1 = mOrganU(RLUNG)
Usub2 = mOrganU(LLUNG)
Usub3 = mOrganU(HEART)
p1 = RLUNG
p2 = p1 + LLUNG
p3 = p2 + HEART
p1_ = int(np.ceil(RLUNG / 2))
p2_ = p1_ + int(np.ceil(LLUNG / 2))
p3_ = p2_ + int(np.ceil(HEART / 2))
A = np.zeros([p3, p3])
A[:p1, :p1] = Asub1
A[p1:p2, p1:p2] = Asub2
A[p2:p3, p2:p3] = Asub3
AD = np.zeros([p3_, p3_])
AD[:p1_, :p1_] = ADsub1
AD[p1_:p2_, p1_:p2_] = ADsub2
AD[p2_:p3_, p2_:p3_] = ADsub3
D = np.zeros([p3_, p3])
D[:p1_, :p1] = Dsub1
D[p1_:p2_, p1:p2] = Dsub2
D[p2_:p3_, p2:p3] = Dsub3
U = np.zeros([p3, p3_])
U[:p1, :p1_] = Usub1
U[p1:p2, p1_:p2_] = Usub2
U[p2:p3, p2_:p3_] = Usub3
return A, AD, D, U