Skip to content

Commit

Permalink
#16 first version of TinyMorphologist that can run
Browse files Browse the repository at this point in the history
  • Loading branch information
sapetnioc committed Mar 9, 2022
1 parent f854e9a commit fef879a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 60 deletions.
30 changes: 17 additions & 13 deletions bv_use_cases/tiny_morphologist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class BiasCorrection(Process):
output: field(type_=File, write=True, extensions=('.nii',))

def execute(self, context):
boom
with open(self.input) as f:
content = self.read()
content = f'{content}\nBias correction with strength={self.strength}'
content = f.read()
content = f'{content}Bias correction with strength={self.strength}\n'
Path(self.output).parent.mkdir(parents=True, exist_ok=True)
with open(self.output, 'w') as f:
f.write(content)

Expand Down Expand Up @@ -44,8 +44,8 @@ def execute(self, context):
fakespmdir = Path(context.fakespm.directory)
real_version = (fakespmdir / 'fakespm').read_text().strip()
with open(self.input) as f:
content = self.read()
content = f'{content}\nNormalization with fakespm {real_version} installed in {fakespmdir} using template "{self.template}"'
content = f.read()
content = f'{content}Normalization with fakespm {real_version} installed in {fakespmdir} using template "{self.template}"\n'
with open(self.output, 'w') as f:
f.write(content)

Expand All @@ -61,8 +61,9 @@ class AimsNormalization(Process):

def execute(self, context):
with open(self.input) as f:
content = self.read()
content = f'{content}\nNormalization with Aims, origin={self.origin}'
content = f.read()
content = f'{content}Normalization with Aims, origin={self.origin}\n'
Path(self.output).parent.mkdir(parents=True, exist_ok=True)
with open(self.output, 'w') as f:
f.write(content)

Expand All @@ -78,10 +79,13 @@ class SplitBrain(Process):

def execute(self, context):
with open(self.input) as f:
content = self.read()
content = f'{content}\nBias correction with strength={self.strength}'
with open(self.output, 'w') as f:
f.write(content)
content = f.read()
for side in ('left', 'right'):
side_content = f'{content}Split brain side={side}\n'
output = getattr(self, f'{side}_output')
Path(output).parent.mkdir(parents=True, exist_ok=True)
with open(output, 'w') as f:
f.write(side_content)


class ProcessHemisphere(Process):
Expand All @@ -90,8 +94,8 @@ class ProcessHemisphere(Process):

def execute(self, context):
with open(self.input) as f:
content = self.read()
content = f'{content}\nProcess hemisphere'
content = f.read()
content = f'{content}Process hemisphere\n'
with open(self.output, 'w') as f:
f.write(content)

Expand Down
106 changes: 59 additions & 47 deletions bv_use_cases/tiny_morphologist/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
from pathlib import Path
from pprint import pprint
import shutil
import tempfile

Expand Down Expand Up @@ -38,31 +39,31 @@
'christophorus',
'christy',
'conchobhar',
# 'cornelia',
# 'dakila',
# 'demosthenes',
# 'devin',
# 'ferit',
# 'gautam',
# 'hikmat',
# 'isbel',
# 'ivona',
# 'jordana',
# 'justyn',
# 'katrina',
# 'lyda',
# 'melite',
# 'mina',
# 'odalric',
# 'rainbow',
# 'rashn',
# 'shufen',
# 'simona',
# 'svanhildur',
# 'thilini',
# 'til',
# 'vanessza',
# 'victoria'
'cornelia',
'dakila',
'demosthenes',
'devin',
'ferit',
'gautam',
'hikmat',
'isbel',
'ivona',
'jordana',
'justyn',
'katrina',
'lyda',
'melite',
'mina',
'odalric',
'rainbow',
'rashn',
'shufen',
'simona',
'svanhildur',
'thilini',
'til',
'vanessza',
'victoria'
)

# Create temporary directory for the use case
Expand Down Expand Up @@ -147,7 +148,7 @@
output_dataset = capsul.dataset(brainvisa)
# Create a main pipeline that will contain all the morphologist pipelines
# we want to execute
processing_pipeline = capsul.custom_pipeline(autoexport_nodes_parameters=False)
processing_pipeline = capsul.custom_pipeline()

completion = Completion()
completion.add_dataset_items({
Expand Down Expand Up @@ -181,10 +182,13 @@
# pipeline that will be executed
processing_pipeline.add_process(f'pipeline_{count}', tiny_morphologist)

pipeline_files.append(tiny_morphologist.nobias)
pipeline_files.append(tiny_morphologist.normalized)
pipeline_files.append(tiny_morphologist.right_hemisphere)
pipeline_files.append(tiny_morphologist.left_hemisphere)

print('Created', f'pipeline_{count}')

# for field in tiny_morphologist.fields():
# value = getattr(tiny_morphologist, field.name, None)
# print(' ', ('<-' if field.is_output() else '->'), field.name, '=', value)
Expand All @@ -196,35 +200,43 @@
# for field in node.fields():
# value = getattr(node, field.name, None)
# print(' ', ('<-' if field.is_output() else '->'), field.name, '=', value)

# import sys
# sys.stdout.flush()
# from soma.qt_gui.qt_backend import QtGui
# from capsul.qt_gui.widgets import PipelineDeveloperView

# app = QtGui.QApplication.instance()
# if not app:
# app = QtGui.QApplication(sys.argv)
# view1 = PipelineDeveloperView(tiny_morphologist)
# view1.show()
# app.exec_()
# del view1
# sys.exit()

count = count + 1

with capsul.engine() as ce:
# Finally execute all the TinyMorphologist instances
execution_id = ce.run(processing_pipeline)
for debug in ce.status(execution_id)['debug_messages']:
print('!', debug)

# for node_name, node in processing_pipeline.nodes.items():
# if not node_name:
# continue
# print(' ', node_name, ':', getattr(node ,'definition', node.__class__.__name__))
# if isinstance(node,Process):
# for field in node.fields():
# value = getattr(node, field.name, None)
# print(' ', ('<-' if field.is_output() else '->'), field.name, '=', value)

try:
with capsul.engine() as ce:
# Finally execute all the TinyMorphologist instances
execution_id = ce.run(processing_pipeline)
except Exception:
import traceback
traceback.print_exc()

for f in pipeline_files:
if os.path.exists(f):
print('-' * 40)
print(f)
print('-' * 40)
with open(f) as file:
print(file.read())

import sys
sys.stdout.flush()
from soma.qt_gui.qt_backend import QtGui
from capsul.qt_gui.widgets import PipelineDeveloperView
app = QtGui.QApplication.instance()
if not app:
app = QtGui.QApplication(sys.argv)
view1 = PipelineDeveloperView(processing_pipeline, show_sub_pipelines=True)
view1.show()
app.exec_()
del view1
finally:
shutil.rmtree(tmp)

0 comments on commit fef879a

Please sign in to comment.