Skip to content

Commit

Permalink
#16
Browse files Browse the repository at this point in the history
  • Loading branch information
sapetnioc committed Feb 16, 2022
1 parent c3a41b5 commit 1618f7f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
35 changes: 18 additions & 17 deletions bv_use_cases/tiny_morphologist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class BiasCorrection(Process):
input: field(type_=file())
strength: float = 0.8
output: field(type_=file(), output=True)
output: field(type_=file(), write=True)

def execute(self, context):
with open(self.input) as f:
Expand All @@ -23,10 +23,10 @@ def execute(self, context):
brainvisa={'output': {'prefix': 'nobias'}}
)

class SPMNormalization(Process):
class FakeSPMNormalization(Process):
input: field(type_=file())
template: field(type_=file())
output: field(type_=file(), output=True)
output: field(type_=file(), write=True)

requirements = {
'fakespm': {
Expand All @@ -40,18 +40,18 @@ class SPMNormalization(Process):
)

def execute(self, context):
spmdir = Path(context.spm.directory)
real_version = (spmdir / 'spm').read_text().strip()
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 {spmdir} using template "{self.template}"'
content = f'{content}\nNormalization with fakespm {real_version} installed in {fakespmdir} using template "{self.template}"'
with open(self.output, 'w') as f:
f.write(content)

class AimsNormalization(Process):
input: field(type_=file())
origin: field(type_=list[float], default_factory=lambda: [1.2, 3.4, 5.6])
output: field(type_=file(), output=True)
output: field(type_=file(), write=True)

path_layout = dict(
bids={'output': {'part': 'normalized'}},
Expand All @@ -67,8 +67,8 @@ def execute(self, context):

class SplitBrain(Process):
input: field(type_=file())
right_output: field(type_=file(), output=True)
left_output: field(type_=file(), output=True)
right_output: field(type_=file(), write=True)
left_output: field(type_=file(), write=True)

path_layout = dict(
bids={'output': {'part': 'split'}},
Expand All @@ -85,7 +85,7 @@ def execute(self, context):

class ProcessHemisphere(Process):
input: field(type_=file())
output: field(type_=file(), output=True)
output: field(type_=file(), write=True)

def execute(self, context):
with open(self.input) as f:
Expand All @@ -98,18 +98,17 @@ class TinyMorphologist(Pipeline):
def pipeline_definition(self):
self.add_process('nobias', BiasCorrection)

self.add_switch('normalization', ['none', 'spm', 'aims'], ['output'])
self.add_process('spm_normalization', SPMNormalization)
self.add_switch('normalization', ['none', 'fakespm', 'aims'], ['output'])
self.add_process('fakespm_normalization', FakeSPMNormalization)
self.add_process('aims_normalization', AimsNormalization)
self.add_process('split', SplitBrain)
self.add_process('right_hemi', ProcessHemisphere)
self.add_process('left_hemi', ProcessHemisphere)


self.add_link('nobias.output->normalization.none_switch_output')

self.add_link('nobias.output->spm_normalization.input')
self.add_link('spm_normalization.output->normalization.spm_switch_output')
self.add_link('nobias.output->fakespm_normalization.input')
self.add_link('fakespm_normalization.output->normalization.fakespm_switch_output')

self.add_link('nobias.output->aims_normalization.input')
self.add_link('aims_normalization.output->normalization.aims_switch_output')
Expand All @@ -126,11 +125,13 @@ def pipeline_definition(self):
path_layout = dict(
bids={
'*': {'pipeline': 'tinymorphologist'},
'right_hemi': {'part': 'right_hemi'}
'left_hemisphere': {'part': 'left_hemi'},
'right_hemisphere': {'part': 'right_hemi'},
},
brainvisa={
'*': {'process': 'tinymorphologist'},
'left_hemi': {'prefix': 'left_hemi'}
'left_hemisphere': {'prefix': 'left_hemi'},
'right_hemisphere': {'prefix': 'right_hemi'},
}
)

Expand Down
23 changes: 16 additions & 7 deletions bv_use_cases/tiny_morphologist/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,23 @@
'default': {
'label': 'Local computer',
'modules': {}
},
'remote': {
'label': 'triscotte',
'type': 'ssh',
'host': 'triscotte.cea.fr',
'modules': {}
}
}
# Create fake SPM directories
for version in ('8', '12'):
spm = tmp / 'software' / f'fakespm-{version}'
spm.mkdir(parents=True, exist_ok=True)
fakespm = tmp / 'software' / f'fakespm-{version}'
fakespm.mkdir(parents=True, exist_ok=True)
# Write a file containing only the version string that will be used
# by fakespm module to check installation.
(spm / 'spm').write_text(version)
(fakespm / 'fakespm').write_text(version)
fakespm_config = {
'directory': str(spm),
'directory': str(fakespm),
'version': version,
}
config['default']['modules'].setdefault('fakespm', []).append(fakespm_config)
Expand Down Expand Up @@ -125,7 +131,8 @@
count = 0
for t1_mri in input_dataset.find(suffix='T1w'):
# Create a TinyMorphologist pipeline
tiny_morphologist = capsul.executable('bv_use_cases.tiny_morphologist.TinyMorphologist')
tiny_morphologist = capsul.executable('bv_use_cases.tiny_morphologist.TinyMorphologist',
normalization='aims')
# Set the input data
tiny_morphologist.input = t1_mri['path']
# Complete outputs following BraiVISA organization
Expand All @@ -143,8 +150,10 @@
for field in tiny_morphologist.fields():
value = getattr(tiny_morphologist, field.name, None)
print(' ', ('<-' if tiny_morphologist.is_output(field) else '->'), field.name, '=', value)

count = count + 1
# # Finally execute all the TinyMorphologist instances
# capsul.run(processing_pipeline)
with capsul.engine() as ce:
# Finally execute all the TinyMorphologist instances
ce.run(processing_pipeline)
finally:
shutil.rmtree(tmp)

0 comments on commit 1618f7f

Please sign in to comment.