-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_brain_code.py
82 lines (64 loc) · 2.89 KB
/
generate_brain_code.py
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
import os
brain_files = os.listdir(os.getcwd()+os.sep+'brain_testing')
if not os.path.isdir(os.getcwd()+os.sep+'brain_data'):
os.mkdir('brain_data')
CODE ="""
# Code to generate vascular networks for Third Ventricle
import svcco
import os
import numpy as np
from pickle import dump
######################
number = 100
######################
FOLDER = '{}'
OBJ_FILE = '{}'
NAME = '{}_8000_vessels_'
if not os.path.isdir(os.getcwd()+os.sep+FOLDER):
os.mkdir(os.getcwd()+os.sep+FOLDER)
os.mkdir(os.getcwd()+os.sep+FOLDER+os.sep+'times')
os.mkdir(os.getcwd()+os.sep+FOLDER+os.sep+'data')
FILE_TIME_PREFIX = os.getcwd()+os.sep+FOLDER+os.sep+'times'
FILE_DATA_PREFIX = os.getcwd()+os.sep+FOLDER+os.sep+'data'
s = svcco.surface()
s.load(OBJ_FILE)
s.solve()
s.build()
t = svcco.tree()
t.set_boundary(s)
t.set_root()
t.n_add(500)
EXISTING_TIMES = len(os.listdir(os.getcwd()+os.sep+FOLDER+os.sep+'times'))
EXISTING_DATA = len(os.listdir(os.getcwd()+os.sep+FOLDER+os.sep+'data'))
EXISTING_PERFUSION = len(list(filter(lambda n: 'perfusion' in n,os.listdir(os.getcwd()+os.sep+FOLDER+os.sep+'data'))))
EXISTING_MESH = len(list(filter(lambda n: 'mesh' in n,os.listdir(os.getcwd()+os.sep+FOLDER+os.sep+'data'))))
EXISTING_TREE = len(list(filter(lambda n: 'tree' in n,os.listdir(os.getcwd()+os.sep+FOLDER+os.sep+'data'))))
EXISTING_RAW = len(list(filter(lambda n: 'raw' in n,os.listdir(os.getcwd()+os.sep+FOLDER+os.sep+'data'))))
for i in range(number):
t = svcco.tree()
t.set_boundary(s)
t.set_assumptions(convex=True)
t.set_root()
t.n_add(8000)
file = open(os.getcwd() + os.sep + FOLDER +os.sep+'times'+os.sep+NAME+'{{}}.pkl'.format(i+EXISTING_TIMES),"wb")
dump(t.time,file)
file.close()
id,vol = svcco.perfusion_territory(t,mesh_file=os.getcwd()+os.sep+FOLDER+os.sep+'data'+os.sep+NAME+'mesh_{{}}.vtu'.format(i+EXISTING_MESH),
tree_file=os.getcwd()+os.sep+FOLDER+os.sep+'data'+os.sep+NAME+'tree_{{}}.vtp'.format(i+EXISTING_TREE))
file = open(os.getcwd() + os.sep + FOLDER +os.sep+'data'+os.sep+NAME+'perfusion_{{}}.pkl'.format(i+EXISTING_PERFUSION),"wb")
dump(vol,file)
file.close()
np.savetxt(os.getcwd() + os.sep + FOLDER +os.sep+'data'+os.sep+NAME+'raw_{{}}.csv'.format(i+EXISTING_RAW),t.data,delimiter=',')
grid = s.pv_polydata.delaunay_3d()
surf = grid.extract_surface()
stats = [s.pv_polydata.area,s.pv_polydata.volume,surf.area,grid.volume]
file = open(os.getcwd() + os.sep + FOLDER +os.sep+'data'+os.sep+'stats.pkl',"wb")
dump(stats,file)
file.close()
"""
for file in brain_files:
part_name = file.split('_')[-1].split('.')[0].replace(' ','_')
full_part_path = (os.getcwd()+os.sep+'brain_testing'+os.sep+file).replace(os.sep,os.sep+os.sep)
f = open(os.getcwd()+os.sep+'brain_data'+os.sep+part_name+'.py','w+')
f.writelines([CODE.format(part_name,full_part_path,part_name)])
f.close()