-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink_Nodes.py
More file actions
149 lines (138 loc) · 4.86 KB
/
Copy pathLink_Nodes.py
File metadata and controls
149 lines (138 loc) · 4.86 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import numpy as np;
from Utils import isint ,isfloat
def cauclate_node_pos(brainareanodes:list[tuple[float,float,float]|tuple[float,float,float,float]]):
"""
Calculate the position of the nodes in the brain area
:param brainareanodes: list of tuples, each tuple contains the x,y,z coordinate of the node
:return: list of tuples, each tuple contains the x,y,z coordinate of the node
"""
mean_node=np.mean(np.array(brainareanodes[:,0:3]),axis=0);
return mean_node;
def cauclate_node_value(brainareanodes:list[tuple[float,float,float,float]],valueaxis=3):
"""
Calculate the value of the nodes in the brain area
:param brainareanodes: list of tuples, each tuple contains the x,y,z coordinate of the node and the value
:return: float, the value of the node
"""
values=brainareanodes[:,valueaxis];
mean_value=np.mean(values);
return mean_value;
def cauclate_node_size(brainareanodes:list[tuple[float,float,float,float]],fac=0.01):
"""
Calculate the size of the nodes in the brain area
:param brainareanodes: list of tuples, each tuple contains the x,y,z coordinate of the node and the value
:param fac: factory to the size
:return: float, the size of the node
"""
l=len(brainareanodes);
return l*fac;
def parse_node_text(path:str,mode="continue"):
"""
Parse the node text file
:param path: the node file path
:return: node lists
"""
f=open(path,"r");
lines=f.readlines();
outnodes=[];
for line in lines:
data=line.strip().split("\t");
#data.remove
try:
x,y,z,value,size,label=data;
node={"x":float(x),"y":float(y),"z":float(z),"value":float(value),"size":float(size),"label":label};
outnodes.append(node);
except:
print(f"Data error at {data}");
if(mode=="continue"):
continue;
else:
f.close();
return;
f.close();
return outnodes;
def parse_edge_text(path:str,mode="continue"):
"""
Parse the edge text file
:param path: the edge file path
:return: edge matrix
"""
f=open(path,"r");
lines=f.readlines();
outedges=[];
for line in lines:
data=line.strip().split("\t");
#data.remove("");
try:
data=list(map(lambda x: float(x),data));
outedges.append(data);
except:
print(f"Data error at {data}");
if(mode=="continue"):
continue;
else:
f.close();
return;
outedges=np.array(outedges);
f.close()
return outedges;
def make_edge_from_nodes(size:tuple[int,int],outpath:str,pairnodesl:list[tuple[int,int]|tuple[int,int,float]]):
arr=np.array([]);
try:
arr=np.zeros(size);
except:
print( f"shape error at size{size}");
return;
for pairnodes in pairnodesl:
if(len(pairnodes)==2):
try:
arr[pairnodes[0],pairnodes[1]]=1;
except:
print( f"nodes position error{pairnodes}");
return;
else:
try:
arr[pairnodes[0],pairnodes[1]]=pairnodes[2];
if(not isfloat(pairnodes[2])):
print( f"nodes value error at value{pairnodes[2]}");
return;
except:
print( f"nodes position error{pairnodes}");
return;
arr=list(arr);
f=open(outpath,"w");
for vec in arr:
f.write(" ".join([str(i) for i in vec])+"\n");
f.close();
def roi_compute(roinodes:list[tuple[int,int,int,float]],scalfac=1,mode="average"):
if(mode=="average"):
return np.mean(np.array(roinodes)[:,3])*scalfac;
elif(mode=="max"):
return np.max(np.array(roinodes)[:,3])*scalfac;
elif(mode=="min"):
return np.min(np.array(roinodes)[:,3])*scalfac;
else:
print("mode is not supported!")
from copy import deepcopy;
def node2connect(nodelist,connectmatrix):
lin=np.sum(connectmatrix,axis=1)/np.sum(connectmatrix);
outnodelist=[];
for i,node in enumerate(nodelist):
outnode=deepcopy(node);
outnode["value"]=lin[i];
outnodelist.append(outnode);
return outnodelist;
def nodevaluecauclate(image,regionmask,type="average"):
nodevoxels=image[regionmask];
if(type=="average"):
return np.mean(nodevoxels);
elif(type=="max"):
return np.max(nodevoxels);
elif(type=="min"):
return np.min(nodevoxels);
elif(type=="filteraverage"):
m=np.mean(nodevoxels);
t=np.std(nodevoxels);
nodevoxels=nodevoxels[nodevoxels>m-t];
nodevoxels=nodevoxels[nodevoxels<m+t];
return np.mean(nodevoxels);