-
Notifications
You must be signed in to change notification settings - Fork 28
Draft MEDIC dynamic distortion correction method (second attempt) #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tsalo
wants to merge
50
commits into
nipreps:main
Choose a base branch
from
tsalo:medic-whole
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
50b5933
Add warpkit to dependencies.
tsalo ba9a65a
Write workflow.
tsalo 49f9f90
Update medic.py
tsalo 92342b5
Update medic.py
tsalo 1468ca5
Add MEDIC test dataset.
tsalo 27d0139
Add TotalReadoutTime field.
tsalo e3a5f99
Install Julia with micromamba, not Docker.
tsalo 662542f
Aspirational doctest.
tsalo 63aa532
Try wrangling MEDIC files.
tsalo 5f84bd2
Merge remote-tracking branch 'upstream/master' into medic-whole
tsalo 5b9814a
Merge branch 'master' into medic-whole
tsalo 7750eaa
Merge remote-tracking branch 'upstream/master' into medic-whole
tsalo e63cd84
Fix connections.
tsalo d2569da
Merge branch 'master' into medic-whole
effigies b535b5f
Try @effigies' recommendation.
tsalo 496fa10
Update sdcflows/utils/wrangler.py
tsalo 79d1fb1
Search for phase to reduce false positives.
tsalo 544ae7d
Address style issues and fix error.
tsalo 8bef670
Update test_find_estimators.py
tsalo b164fa3
Update test_find_estimators.py
tsalo add5d9f
Drop test dataset in favor of dict.
tsalo a7b0f84
Update test_find_estimators.py
tsalo 6fbb1cb
Drop old stuff.
tsalo 6b7b416
Revert changes to failing test.
tsalo d2877b0
Update fieldmaps.py
tsalo 8fa7c39
Just skip the failing test for now.
tsalo 18b8e09
Fix.
tsalo 2b9ff81
Update test_wrangler.py
tsalo 084f199
Add B0Field fields to mock dataset.
tsalo 9f2c089
Replace B0Field with IntendedFor.
tsalo bb660d2
Update wrangler.py
tsalo c2205e1
Add second session to test spec.
tsalo 670ebc8
It needs a third session!
tsalo 9c5fdd8
Alright maybe this'll work?
tsalo d7a59ee
Fix?
tsalo 14e1ff1
Ignore long lines in test files.
tsalo 1ba3e94
Fix long line.
tsalo 45b256a
Change Julia version to make build work.
tsalo 5d13425
Add test for new function.
tsalo 3f48d93
Test collection on real data.
tsalo 2815590
Fix check.
tsalo ee8c38a
Drop B0Field fields from fmaps.
tsalo f54533b
Update build-test-publish.yml
tsalo 9c58221
Update wrangler.py
tsalo e205348
Fix key.
tsalo 8f54fad
Draft medic workflow test.
tsalo 25caaf3
Fix paths to metadata files.
tsalo 54d92c4
Run ruff.
tsalo 7c6cc93
Test both types of annotation.
tsalo 085670d
Update wrangler.py
tsalo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
# | ||
# Copyright 2021 The NiPreps Developers <[email protected]> | ||
# | ||
# 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. | ||
# | ||
# We support and encourage derived works from this project, please read | ||
# about our expectations at | ||
# | ||
# https://www.nipreps.org/community/licensing/ | ||
# | ||
"""Processing of dynamic field maps from complex-valued multi-echo BOLD data.""" | ||
|
||
from nipype.pipeline import engine as pe | ||
from nipype.interfaces import utility as niu | ||
from niworkflows.engine.workflows import LiterateWorkflow as Workflow | ||
|
||
from sdcflows.interfaces.fmap import MEDIC, PhaseMap2rads2 | ||
|
||
INPUT_FIELDS = ("magnitude", "phase") | ||
|
||
|
||
def init_medic_wf(name="medic_wf"): | ||
"""Create the MEDIC dynamic field estimation workflow. | ||
|
||
Workflow Graph | ||
.. workflow :: | ||
:graph2use: orig | ||
:simple_form: yes | ||
|
||
from sdcflows.workflows.fit.medic import init_medic_wf | ||
|
||
wf = init_medic_wf() # doctest: +SKIP | ||
|
||
Parameters | ||
---------- | ||
name : :obj:`str` | ||
Name for this workflow | ||
|
||
Inputs | ||
------ | ||
magnitude : :obj:`list` of :obj:`str` | ||
A list of echo-wise magnitude EPI files that will be fed into MEDIC. | ||
phase : :obj:`list` of :obj:`str` | ||
A list of echo-wise phase EPI files that will be fed into MEDIC. | ||
metadata : :obj:`list` of :obj:`dict` | ||
List of metadata dictionaries corresponding to each of the input magnitude files. | ||
|
||
Outputs | ||
------- | ||
fieldmap : :obj:`str` | ||
The path of the estimated fieldmap time series file. Units are Hertz. | ||
displacement : :obj:`list` of :obj:`str` | ||
Path to the displacement time series files. Units are mm. | ||
method: :obj:`str` | ||
Short description of the estimation method that was run. | ||
|
||
Notes | ||
----- | ||
This is a translation of the MEDIC algorithm, as implemented in ``vandandrew/warpkit`` | ||
(specifically the function ``unwrap_and_compute_field_maps``), into a Nipype workflow. | ||
|
||
""" | ||
workflow = Workflow(name=name) | ||
|
||
workflow.__desc__ = """\ | ||
A dynamic fieldmap was estimated from multi-echo EPI data using the MEDIC algorithm (@medic). | ||
""" | ||
|
||
inputnode = pe.Node(niu.IdentityInterface(fields=INPUT_FIELDS), name="inputnode") | ||
outputnode = pe.Node( | ||
niu.IdentityInterface(fields=["fieldmap", "displacement", "method"]), | ||
name="outputnode", | ||
) | ||
outputnode.inputs.method = "MEDIC" | ||
|
||
# Write metadata dictionaries to JSON files | ||
write_metadata = pe.MapNode( | ||
niu.Function( | ||
input_names=["metadata"], | ||
output_names=["out_file"], | ||
function=write_json, | ||
), | ||
iterfield=["metadata"], | ||
name="write_metadata", | ||
) | ||
workflow.connect([(inputnode, write_metadata, [("metadata", "metadata")])]) | ||
|
||
# Convert phase to radians (-pi to pi, not 0 to 2pi) | ||
phase2rad = pe.MapNode( | ||
PhaseMap2rads2(), | ||
iterfield=["in_file"], | ||
name="phase2rad", | ||
) | ||
workflow.connect([(inputnode, phase2rad, [("phase", "in_file")])]) | ||
|
||
medic = pe.Node( | ||
MEDIC(), | ||
name="medic", | ||
) | ||
workflow.connect([ | ||
(inputnode, medic, [("magnitude", "magnitude")]), | ||
(phase2rad, medic, [("out_file", "metadata")]), | ||
(phase2rad, medic, [("out_file", "phase")]), | ||
(medic, outputnode, [ | ||
("native_field_map", "fieldmap"), | ||
("displacement_map", "displacement"), | ||
]), | ||
]) # fmt:skip | ||
|
||
return workflow | ||
|
||
|
||
def write_json(metadata): | ||
"""Write a dictionary to a JSON file.""" | ||
import json | ||
import os | ||
|
||
out_file = os.path.abspath("metadata.json") | ||
with open(out_file, "w") as fobj: | ||
json.dump(metadata, fobj, sort_keys=True, indent=4) | ||
|
||
return out_file |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.