|
a |
|
b/Adj_Logic.py |
|
|
1 |
import numpy as np |
|
|
2 |
from tqdm import tqdm |
|
|
3 |
|
|
|
4 |
def Calculate_Index(i: int, j: int, n_cols: int) -> int: |
|
|
5 |
return n_cols * i + j * (i % 2 == 0) + (n_cols - 1 - j) * (i % 2 == 1) |
|
|
6 |
|
|
|
7 |
def Generate_Adj_Matrix(n_cols: int) -> np.ndarray: |
|
|
8 |
row_array = np.zeros(8 * 512 * n_cols, dtype = np.uint32) |
|
|
9 |
col_array = np.zeros(8 * 512 * n_cols, dtype = np.uint32) |
|
|
10 |
|
|
|
11 |
for i in range(0, 512): |
|
|
12 |
index = 8 * i * n_cols if i % 2 == 0 else 8 * (i + 1) * n_cols - 1 |
|
|
13 |
|
|
|
14 |
for j in range(0, n_cols): |
|
|
15 |
mask = list(range(index, index + 8)) if i % 2 == 0 else list(range(index, index - 8, -1)) |
|
|
16 |
mask.sort() |
|
|
17 |
|
|
|
18 |
row_array[mask] = Calculate_Index(i, j, n_cols) |
|
|
19 |
|
|
|
20 |
i_m = 511 if i == 0 else i - 1 |
|
|
21 |
i_p = 0 if i == 511 else i + 1 |
|
|
22 |
|
|
|
23 |
j_m = n_cols - 1 if j == 0 else j - 1 |
|
|
24 |
j_p = 0 if j == n_cols - 1 else j + 1 |
|
|
25 |
|
|
|
26 |
con_indices = np.array([Calculate_Index(i, j_m, n_cols), |
|
|
27 |
Calculate_Index(i_m, j_m, n_cols), |
|
|
28 |
Calculate_Index(i_m, j, n_cols), |
|
|
29 |
Calculate_Index(i_m, j_p, n_cols), |
|
|
30 |
Calculate_Index(i, j_p, n_cols), |
|
|
31 |
Calculate_Index(i_p, j_p, n_cols), |
|
|
32 |
Calculate_Index(i_p, j, n_cols), |
|
|
33 |
Calculate_Index(i_p, j_m, n_cols)], |
|
|
34 |
dtype = np.uint32) |
|
|
35 |
con_indices.sort() |
|
|
36 |
|
|
|
37 |
col_array[mask] = con_indices |
|
|
38 |
index = index + 8 if i % 2 == 0 else index - 8 |
|
|
39 |
|
|
|
40 |
return np.array([row_array, col_array], dtype = np.uint32) |
|
|
41 |
|
|
|
42 |
def Generate_All_Adj_Matrices(n_cols_arr: np.ndarray) -> bool: |
|
|
43 |
try: |
|
|
44 |
for n_cols in tqdm(n_cols_arr): |
|
|
45 |
adj_mat = Generate_Adj_Matrix(n_cols = n_cols) |
|
|
46 |
np.save(file = "C:\\Users\\leotu\\OneDrive\\Documents\\ImageCHD_dataset\\ADJACENCY\\Adj_Mat_" \ |
|
|
47 |
+ str(n_cols) + ".npy", arr = adj_mat) |
|
|
48 |
return True |
|
|
49 |
except Exception as e: |
|
|
50 |
print(e) |
|
|
51 |
return False |