forked from NVIDIA/modulus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_graphs.py
357 lines (292 loc) · 11.5 KB
/
generate_graphs.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# ignore_header_test
# Copyright 2023 Stanford University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import numpy as np
import dgl
from tqdm import tqdm
import json
import shutil
import copy
import vtk_tools as vtkt
import graph_tools as grpt
import scipy
import torch as th
def add_field(graph, field, field_name, offset=0, pad=10):
"""
Add time-dependent fields to a DGL graph.
Add time-dependent scalar fields as graph node features. The time-dependent
fields are stored as n x 1 x m Pytorch tensors, where n is the number of
graph nodes and m the number of timesteps.
Arguments:
graph: DGL graph
field: dictionary with (key: timestep, value: field value)
field_name (string): name of the field
offset (int): number of timesteps to skip.
Default: 0 -> keep all timesteps
pad (int): number of timesteps to add for interpolation from zero
zero initial conditions. Default: 0 -> start from actual
initial condition
"""
timesteps = [float(t) for t in field]
timesteps.sort()
dt = timesteps[1] - timesteps[0]
T = timesteps[-1]
# we use the third dimension for time
field_t = th.zeros(
(list(field.values())[0].shape[0], 1, len(timesteps) - offset + pad)
)
times = [t for t in field]
times.sort()
times = times[offset:]
loading_t = th.zeros(
(list(field.values())[0].shape[0], 1, len(timesteps) - offset + pad),
dtype=th.bool,
)
if pad > 0:
inc = th.tensor(field[times[0]], dtype=th.float32)
deft = inc * 0
if field_name == "pressure":
minp = np.infty
for t in field:
minp = np.min((minp, np.min(field[t])))
deft = deft + minp
for i in range(pad):
field_t[:, 0, i] = deft * (pad - i) / pad + inc * (i / pad)
loading_t[:, 0, i] = True
for i, t in enumerate(times):
f = th.tensor(field[t], dtype=th.float32)
field_t[:, 0, i + pad] = f
loading_t[:, 0, i + pad] = False
graph.ndata[field_name] = field_t
graph.ndata["loading"] = loading_t
graph.ndata["dt"] = th.reshape(
th.ones(graph.num_nodes(), dtype=th.float32) * dt, (-1, 1, 1)
)
graph.ndata["T"] = th.reshape(
th.ones(graph.num_nodes(), dtype=th.float32) * T, (-1, 1, 1)
)
def load_vtp(file, input_dir):
"""
Load vtp file.
Arguments:
file (string): file name
input_dir (string): path to input_dir
Returns:
dictionary containing point data (key: name, value: data)
n x 3 numpy array of point coordinates
numpy array containing indices of source nodes for every edge
numpy array containing indices of dest nodes for every edge
"""
soln = vtkt.read_geo(input_dir + "/" + file)
point_data, _, points = vtkt.get_all_arrays(soln.GetOutput())
edges1, edges2 = vtkt.get_edges(soln.GetOutput())
# lets check for nans and delete points if they appear
ni = np.argwhere(np.isnan(point_data["area"]))
if ni.size > 0:
for i in ni[0]:
indices = np.where(edges1 >= i)[0]
edges1[indices] = edges1[indices] - 1
indices = np.where(edges2 >= i)[0]
edges2[indices] = edges2[indices] - 1
indices = np.where(edges1 == edges2)[0]
edges1 = np.delete(edges1, indices)
edges2 = np.delete(edges2, indices)
points = np.delete(points, i, axis=0)
for ndata in point_data:
point_data[ndata] = np.delete(point_data[ndata], i)
return point_data, points, edges1, edges2
def resample_time(field, timestep, period, shift=0):
"""
Resample timesteps.
Given a time-dependent field distributed over graph nodes, this function
resamples the field in time using B-spline interpolation at every node.
Arguments:
field: dictionary containing the field for all timesteps
(key: timestep, value: n-dimensional numpy array)
timestep (float): the new timestep
period (float): period of the simulation. We restrict to one cardiac
cycle
shift (float): apply shift (s) to start at the beginning of the systole.
Default value -> 0
Returns:
dictionary containing the field for all resampled timesteps
(key: timestep, value: n-dimensional numpy array)
"""
original_timesteps = [t for t in field]
original_timesteps.sort()
t0 = original_timesteps[0]
T = original_timesteps[-1]
t = [t0 + shift]
nnodes = field[t0].size
resampled_field = {t0 + shift: np.zeros(nnodes)}
while t[-1] < T and t[-1] <= t[0] + period:
t.append(t[-1] + timestep)
resampled_field[t[-1]] = np.zeros(nnodes)
for inode in range(nnodes):
values = []
for time in original_timesteps:
values.append(field[time][inode])
tck, _ = scipy.interpolate.splprep([values], u=original_timesteps, s=0)
values_interpolated = scipy.interpolate.splev(t, tck)[0]
for i, time in enumerate(t):
resampled_field[time][inode] = values_interpolated[i]
return resampled_field
def generate_datastructures(vtp_data, resample_perc):
"""
Generate data structures for graph generation from vtp data.
Arguments:
vtp_data: tuple containing data extracted from the vtp using load_vtp
resample_perc: percentage of points in the original vtp file we keep
(between 0 and 1)
Returns:
dictionary containing graph data (key: field name, value: data)
"""
point_data, points, edges1, edges2 = vtp_data
point_data["tangent"] = grpt.generate_tangents(points, point_data["BranchIdTmp"])
# first node is the inlet by convention
inlet = [0]
outlets = grpt.find_outlets(edges1, edges2)
indices = {"inlet": inlet, "outlets": outlets}
success = False
while not success:
try:
sampled_indices, points, edges1, edges2, _ = grpt.resample_points(
points.copy(),
edges1.copy(),
edges2.copy(),
indices,
resample_perc,
remove_caps=3,
)
success = True
except Exception as e:
print(e)
resample_perc = np.min([resample_perc * 2, 1])
for ndata in point_data:
point_data[ndata] = point_data[ndata][sampled_indices]
inlet = [0]
outlets = grpt.find_outlets(edges1, edges2)
indices = {"inlet": inlet, "outlets": outlets}
pressure = vtkt.gather_array(point_data, "pressure")
flowrate = vtkt.gather_array(point_data, "flow")
if len(flowrate) == 0:
flowrate = vtkt.gather_array(point_data, "velocity")
times = [t for t in pressure]
timestep = float(dataset_info[file.replace(".vtp", "")]["dt"])
for t in times:
pressure[t * timestep] = pressure[t]
flowrate[t * timestep] = flowrate[t]
del pressure[t]
del flowrate[t]
# scale pressure to be mmHg
for t in pressure:
pressure[t] = pressure[t] / 1333.2
times = [t for t in pressure]
sampling_indices = np.arange(points.shape[0])
graph_data = {
"point_data": point_data,
"points": points,
"edges1": edges1,
"edges2": edges2,
"sampling_indices": sampling_indices,
"pressure": pressure,
"flowrate": flowrate,
"timestep": timestep,
"times": times,
}
return graph_data
def add_time_dependent_fields(
graph, graph_data, do_resample_time=False, dt=0.01, copies=1
):
"""
Add time-dependent data to a graph containing static data. This function
can be used to create multiple graphs from a single trajectory by
specifying do_resample_time and providing a number of copies > 1. In this
case, every graph trajectories starts at a different offset from the
starting time.
Arguments:
graph: a DGL graph.
graph_data: dictionary containing graph_data (created using
generate_datastructures)
do_resample_time (bool): specify whether we should resample the
the timesteps. Default -> False
dt (double): timestep size used for resampling. Default -> 0.01
copies: number of copies to generate from a single trajectory (for
data augmentation). Default -> 1
Returns:
list of 'copies' graphs.
"""
ncopies = 1
if do_resample_time:
ncopies = copies
dt = 0.01
offset = int(np.floor((dt / graph_data["timestep"]) / ncopies))
graphs = []
intime = 0
for icopy in range(ncopies):
c_pressure = {}
c_flowrate = {}
si = graph_data["sampling_indices"]
for t in graph_data["times"][intime:]:
c_pressure[t] = graph_data["pressure"][t][si]
c_flowrate[t] = graph_data["flowrate"][t][si]
if do_resample_time:
period = dataset_info[fname]["T"]
shift = dataset_info[fname]["time_shift"]
c_pressure = resample_time(
c_pressure, timestep=dt, period=period, shift=shift
)
c_flowrate = resample_time(
c_flowrate, timestep=dt, period=period, shift=shift
)
intime = intime + offset
padt = 0.1
new_graph = copy.deepcopy(graph)
add_field(new_graph, c_pressure, "pressure", pad=int(padt / dt))
add_field(new_graph, c_flowrate, "flowrate", pad=int(padt / dt))
graphs.append(new_graph)
return graphs
"""
The main function reads all vtps files from the folder specified in input_dir
and generates DGL graphs. The graphs are saved in output_dir.
"""
if __name__ == "__main__":
input_dir = "raw_dataset/vtps"
output_dir = "raw_dataset/graphs/"
dataset_info = json.load(open(input_dir + "/dataset_info.json"))
files = os.listdir(input_dir)
print("Processing all files in {}".format(input_dir))
print("File list:")
print(files)
for file in tqdm(files, desc="Generating graphs", colour="green"):
if ".vtp" in file and "s" in file:
vtp_data = load_vtp(file, input_dir)
graph_data = generate_datastructures(vtp_data, resample_perc=0.06)
fname = file.replace(".vtp", "")
static_graph = grpt.generate_graph(
graph_data["point_data"],
graph_data["points"],
graph_data["edges1"],
graph_data["edges2"],
add_boundary_edges=True,
rcr_values=dataset_info[fname],
)
graphs = add_time_dependent_fields(
static_graph, graph_data, do_resample_time=True, dt=0.1, copies=4
)
for i, graph in enumerate(graphs):
filename = file.replace(".vtp", "." + str(i) + ".grph")
dgl.save_graphs(output_dir + filename, graph)
shutil.copy(input_dir + "/dataset_info.json", output_dir + "/dataset_info.json")