-
Notifications
You must be signed in to change notification settings - Fork 4
Draft NORDIC example #27
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
Open
tsalo
wants to merge
1
commit into
paquiteau:master
Choose a base branch
from
tsalo:doc-nordic
base: master
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.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
""" | ||
=========================================================== | ||
Apply NORDIC to complex-valued fMRI data with noise volumes | ||
=========================================================== | ||
|
||
This example shows how to apply NORDIC to complex-valued fMRI data with noise | ||
volumes. | ||
""" | ||
|
||
############################################################################### | ||
# Load Data | ||
# ----------------------------------------------------------------------------- | ||
|
||
import nibabel as nb | ||
import numpy as np | ||
|
||
magnitude_file = 'data/sub-01_task-rest_part-mag_bold.nii.gz' | ||
phase_file = 'data/sub-01_task-rest_part-phase_bold.nii.gz' | ||
magnitude_noise_file = 'data/sub-01_task-rest_part-mag_noRF.nii.gz' | ||
phase_noise_file = 'data/sub-01_task-rest_part-phase_noRF.nii.gz' | ||
|
||
magnitude_img = nb.load(magnitude_file) | ||
phase_img = nb.load(phase_file) | ||
magnitude_noise_img = nb.load(magnitude_noise_file) | ||
phase_noise_img = nb.load(phase_noise_file) | ||
|
||
magnitude_data = magnitude_img.get_fdata() | ||
phase_data = phase_img.get_fdata() | ||
magnitude_noise_data = magnitude_noise_img.get_fdata() | ||
phase_noise_data = phase_noise_img.get_fdata() | ||
|
||
n_noise_volumes = magnitude_noise_data.shape[3] | ||
|
||
magnitude_data = np.concatenate((magnitude_data, magnitude_noise_data), axis=3) | ||
phase_data = np.concatenate((phase_data, phase_noise_data), axis=3) | ||
|
||
# Rescale phase data to [-pi, pi] | ||
phase_range = np.max(phase_data) | ||
phase_range_min = np.min(phase_data) | ||
range_norm = phase_range - phase_range_min | ||
range_center = (phase_range + phase_range_min) / range_norm * 1 / 2 | ||
phase_data = (phase_data / range_norm - range_center) * 2 * np.pi | ||
|
||
# Combine magnitude and phase into complex-valued data | ||
complex_data = magnitude_data * np.exp(1j * phase_data) | ||
|
||
# TODO: Filter complex data and calculate g-factor map | ||
|
||
############################################################################### | ||
# Calculate Noise Level | ||
# ----------------------------------------------------------------------------- | ||
|
||
# Split out the noise volumes | ||
noise_data = complex_data[:, :, :, -n_noise_volumes:] | ||
noise_data[np.isnan(noise_data)] = 0 | ||
noise_data[np.isinf(noise_data)] = 0 | ||
noise_level = np.std(noise_data[noise_data != 0]) | ||
|
||
# Scale by sqrt(2) because the data are complex-valued | ||
noise_level = noise_level / np.sqrt(2) | ||
|
||
############################################################################### | ||
# Run NORDIC | ||
# ----------------------------------------------------------------------------- | ||
from patch_denoise.denoise import nordic | ||
|
||
n_vols = complex_data.shape[3] | ||
patch_shape = np.ones(3, dtype=int) * int(np.round(np.cbrt(n_vols * 11))) | ||
denoised_data, patch_weights, noise, dofs = nordic( | ||
input_data=complex_data, | ||
patch_shape=patch_shape, | ||
patch_overlap=2, | ||
noise_std=noise_level, | ||
recombination='average', | ||
n_iter_threshold=10, | ||
) | ||
denoised_data = denoised_data[:, :, :, :-n_noise_volumes] | ||
denoised_magnitude_data = np.abs(denoised_data) | ||
denoised_phase_data = np.angle(denoised_data) | ||
denoised_magnitude_img = nb.Nifti1Image( | ||
denoised_magnitude_data, | ||
magnitude_img.affine, | ||
magnitude_img.header, | ||
) | ||
denoised_phase_img = nb.Nifti1Image( | ||
denoised_phase_data, | ||
phase_img.affine, | ||
phase_img.header, | ||
) | ||
|
||
dofs_img = nb.Nifti1Image( | ||
dofs, | ||
magnitude_img.affine, | ||
magnitude_img.header, | ||
) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paquiteau would adding these steps be useful for an example, or should they go into a function/class within patch-denoising?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this is already done in the
load_complex_nifti
function, which is what people would use in practice. I don't think it's needed to refactor that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you referring to combining the complex-valued data or the phase filtering and g-factor map creation?
load_complex_nifti
doesn't do the latter. Also, it doesn't rescale the phase data to -pi to pi, right? So this example definitely needs to separately rescale the data.