File size: 2,289 Bytes
e87a462 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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 |