-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathneuromorphovis.py
executable file
·405 lines (307 loc) · 16.2 KB
/
neuromorphovis.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/python
####################################################################################################
# Copyright (c) 2016 - 2024, EPFL / Blue Brain Project
# Author(s): Marwan Abdellah <marwan.abdellah@epfl.ch>
#
# This file is part of NeuroMorphoVis <https://github.com/BlueBrain/NeuroMorphoVis>
#
# This program is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, version 3 of the License.
#
# This Blender-based tool is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
####################################################################################################
__author__ = "Marwan Abdellah"
__copyright__ = "Copyright (c) 2016 - 2024, Blue Brain Project / EPFL"
__credits__ = ["Ahmet Bilgili", "Juan Hernando", "Stefan Eilemann"]
__version__ = "1.9.0"
__maintainer__ = "Marwan Abdellah"
__email__ = "marwan.abdellah@epfl.ch"
__status__ = "Production"
####################################################################################################
# System imports
import os
import sys
import subprocess
# Append the internal modules into the system paths to avoid Blender importing conflicts
import_paths = ['nmv/interface/cli', 'nmv/file/ops', 'nmv/slurm']
for import_path in import_paths:
sys.path.append(('%s/%s' % (os.path.dirname(os.path.realpath(__file__)), import_path)))
# Internal imports
import arguments_parser
####################################################################################################
# @execute_shell_command
####################################################################################################
def execute_shell_command(shell_command):
subprocess.call(shell_command, shell=True)
####################################################################################################
# @create_shell_commands
####################################################################################################
def create_shell_commands_for_local_execution(arguments,
arguments_string):
"""Creates a list of all the shell commands that are needed to run the different tasks set
in the configuration file.
Notes:
# -b : Blender background mode
# --verbose : Turn off all the verbose messages
# -- : Separate the framework arguments from those given to Blender
:param arguments:
Input arguments.
:param arguments_string:
A string that will be given to each CLI command.
:return:
A list of commands to be appended to the SLURM scripts or directly executed on a local node.
"""
shell_commands = list()
# Retrieve the path to the CLIs
cli_interface_path = os.path.dirname(os.path.realpath(__file__)) + '/nmv/interface/cli'
cli_soma_reconstruction = '%s/soma_reconstruction.py' % cli_interface_path
cli_morphology_reconstruction = '%s/neuron_morphology_reconstruction.py' % cli_interface_path
cli_morphology_analysis = '%s/morphology_analysis.py' % cli_interface_path
cli_mesh_reconstruction = '%s/neuron_mesh_reconstruction.py' % cli_interface_path
# Morphology analysis task
if arguments.analyze_morphology:
# Add this command to the list
shell_commands.append('%s -b --verbose 0 --python %s -- %s' %
(arguments.blender, cli_morphology_analysis, arguments_string))
# Morphology reconstruction task: call the @cli_morphology_reconstruction interface
if arguments.render_neuron_morphology or \
arguments.render_neuron_morphology_360 or \
arguments.render_neuron_morphology_progressive or \
arguments.export_morphology_swc or \
arguments.export_morphology_segments or \
arguments.export_morphology_blend:
# Add this command to the list
shell_command = '%s -b --verbose 0 --python %s -- %s' % (arguments.blender,
cli_morphology_reconstruction,
arguments_string)
shell_commands.append(shell_command)
# Soma-related task: call the @cli_soma_reconstruction interface
if arguments.render_soma_mesh or \
arguments.render_soma_mesh_360 or \
arguments.render_soma_mesh_progressive or \
arguments.export_soma_mesh_ply or \
arguments.export_soma_mesh_obj or \
arguments.export_soma_mesh_stl or \
arguments.export_soma_mesh_blend:
# Add this command to the list
shell_command = '%s -b --verbose 0 --python %s -- %s' % (arguments.blender,
cli_soma_reconstruction,
arguments_string)
print(shell_command)
shell_commands.append(shell_command)
# Neuron mesh reconstruction related task: call the @cli_mesh_reconstruction interface
if arguments.render_neuron_mesh or \
arguments.render_neuron_mesh_360 or \
arguments.export_neuron_mesh_ply or \
arguments.export_neuron_mesh_obj or \
arguments.export_neuron_mesh_stl or \
arguments.export_neuron_mesh_blend:
# Add this command to the list
shell_commands.append('%s -b --verbose 0 --python %s -- %s' %
(arguments.blender, cli_mesh_reconstruction, arguments_string))
# Return a list of commands
return shell_commands
####################################################################################################
# @execute_command
####################################################################################################
def execute_command(shell_command):
subprocess.call(shell_command, shell=True)
####################################################################################################
# @execute_commands
####################################################################################################
def execute_commands(shell_commands):
for shell_command in shell_commands:
print('RUNNING: **********************************************************************')
print(shell_command)
print('*******************************************************************************')
execute_command(shell_command)
####################################################################################################
# @execute_commands_parallel
####################################################################################################
def execute_commands_parallel(shell_commands):
from joblib import Parallel, delayed
Parallel(n_jobs=1)(delayed(execute_command)(i) for i in shell_commands)
####################################################################################################
# @run_local_neuromorphovis
####################################################################################################
def run_local_neuromorphovis(arguments):
"""Run the framework on a local node, basically your machine.
:param arguments:
Command line arguments.
"""
# Use a specific circuit target
if arguments.input == 'target':
# Log
print('Loading a target [%s] in circuit [%s]' % (arguments.target, arguments.blue_config))
# Ensure a valid blue config and a target
if arguments.blue_config is None or arguments.target is None:
print('ERROR: Empty circuit configuration file or target')
exit(0)
# Import BluePy
try:
import bluepy
except ImportError:
print('ERROR: Cannot import [BluePy], please install it')
exit(0)
from bluepy import Circuit
# Loading a circuit
circuit = Circuit(arguments.blue_config)
# Loading the GIDs of the sample target within the circuit
gids = circuit.cells.ids(arguments.target)
shell_commands = list()
for gid in gids:
# Get the argument string for an individual file
arguments_string = arguments_parser.get_arguments_string_for_individual_gid(
arguments=arguments, gid=gid)
# Construct the shell command to run the workflow
shell_commands.extend(
create_shell_commands_for_local_execution(arguments, arguments_string))
# Run NeuroMorphoVis from Blender in the background mode
for shell_command in shell_commands:
subprocess.call(shell_command, shell=True)
# Use a single GID
elif arguments.input == 'gid':
print('Loading a gid [%s] in circuit [%s]' % (str(arguments.gid), arguments.blue_config))
# Ensure a valid blue config and a GID
if arguments.blue_config is None or arguments.gid is None:
print('ERROR: Empty circuit configuration file or GID')
exit(0)
# Get the argument string for an individual file
arguments_string = arguments_parser.get_arguments_string_for_individual_gid(
arguments=arguments, gid=arguments.gid)
# Construct the shell command to run the workflow
shell_commands = create_shell_commands_for_local_execution(arguments, arguments_string)
# Run NeuroMorphoVis from Blender in the background mode
for shell_command in shell_commands:
subprocess.call(shell_command, shell=True)
# Run the jobs on the cluster
slurm.run_gid_jobs_on_cluster(arguments=arguments, gids=[str(arguments.gid)])
# Load morphology files (.H5 or .SWC)
elif arguments.input == 'file':
# Get the arguments string list
arguments_string = arguments_parser.get_arguments_string(arguments=arguments)
# NOTE: Using a morphology file, either in .H5 or .SWC formats is straightforward and
# therefore the arguments will not change at all. In this case it is safe to pass the
# arguments as they were received without any change.
# Construct the shell command to run the workflow
shell_commands = create_shell_commands_for_local_execution(arguments, arguments_string)
# Run NeuroMorphoVis from Blender in the background mode
for shell_command in shell_commands:
subprocess.call(shell_command, shell=True)
# Load a directory morphology files (.H5 or .SWC)
elif arguments.input == 'directory':
import file_ops
import slurm
# Get all the morphology files in this directory
morphology_files = file_ops.get_files_in_directory(arguments.morphology_directory, '.h5')
morphology_files.extend(file_ops.get_files_in_directory(
arguments.morphology_directory, '.swc'))
# If the directory is empty, give an error message
if len(morphology_files) == 0:
print('ERROR: The directory [%s] does NOT contain any morphology files' %
arguments.morphology_directory)
# A list of all the commands to be executed
shell_commands = list()
# Construct the commands for every individual morphology file
for morphology_file in morphology_files:
# Get the argument string for an individual file
arguments_string = arguments_parser.get_arguments_string_for_individual_file(
arguments=arguments, morphology_file=morphology_file)
# Construct the shell command to run the workflow
shell_commands.extend(
create_shell_commands_for_local_execution(arguments, arguments_string))
execute_commands(shell_commands=shell_commands)
exit(0)
# execute_commands_parallel(shell_commands=shell_commands)
else:
print('ERROR: Input data source, use \'file, gid, target or directory\'')
exit(0)
####################################################################################################
# @run_cluster_neuromorphovis
####################################################################################################
def run_cluster_neuromorphovis(arguments):
"""Run the NeuroMorphoVis framework on the BBP visualization cluster using SLURM.
:param arguments:
Command line arguments.
"""
# Use a specific circuit target
if arguments.input == 'target':
# Log
print('Loading a target [%s] in circuit [%s]' % (arguments.target, arguments.blue_config))
# Ensure a valid blue config and a target
if arguments.blue_config is None or arguments.target is None:
print('ERROR: Empty circuit configuration file or target')
exit(0)
# Import BluePy
try:
import bluepy
except ImportError:
print('ERROR: Cannot import [BluePy], please install it')
exit(0)
from bluepy import Circuit
import file_ops
import slurm
# Loading a circuit
circuit = Circuit(arguments.blue_config)
# Loading the GIDs of the sample target within the circuit
gids = circuit.cells.ids(arguments.target)
# Run the jobs on the cluster
slurm.run_gid_jobs_on_cluster(arguments=arguments, gids=gids)
# Use a single GID
elif arguments.input == 'gid':
print('Loading a gid [%s] in circuit [%s]' % (str(arguments.gid), arguments.blue_config))
# Ensure a valid blue config and a GID
if arguments.blue_config is None or arguments.gid is None:
print('ERROR: Empty circuit configuration file or GID')
exit(0)
import file_ops
import slurm
# Run the jobs on the cluster
slurm.run_gid_jobs_on_cluster(arguments=arguments, gids=[str(arguments.gid)])
# Use the morphology file (.H5 or .SWC)
elif arguments.input == 'file':
import file_ops
import slurm
# Get the arguments string list
arguments_string = arguments_parser.get_arguments_string(arguments=arguments)
# Run the job on the cluster
slurm.run_morphology_files_jobs_on_cluster(
arguments=arguments, morphology_files=arguments.morphology_file)
# Operate on a directory
elif arguments.input == 'directory':
import file_ops
import slurm
# Get all the morphology files in this directory
morphology_files = file_ops.get_files_in_directory(arguments.morphology_directory, '.h5')
# Run the jobs on the cluster
slurm.run_morphology_files_jobs_on_cluster(
arguments=arguments, morphology_files=morphology_files)
else:
print('ERROR: Input data source, use [file, gid, target or directory]')
exit(0)
####################################################################################################
# @ Run the main function if invoked from the command line.
####################################################################################################
if __name__ == "__main__":
# Parse the command line arguments
arguments = arguments_parser.parse_command_line_arguments()
import file_ops
import slurm
# Verify the output directory before screwing things !
if not file_ops.path_exists(arguments.output_directory):
print('ERROR: Please set the output directory to a valid path')
exit(0)
# Otherwise, create the output tree
else:
file_ops.create_output_tree(arguments.output_directory)
# LOCAL EXECUTION: Compile the corresponding command and launch it on the current machine
if arguments.execution_node == 'local':
run_local_neuromorphovis(arguments=arguments)
# BBP CLUSTER EXECUTION: Create the SLURM scripts and run them on the cluster (only @ BBP)
else:
run_cluster_neuromorphovis(arguments=arguments)