-
Notifications
You must be signed in to change notification settings - Fork 10
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
hastily-written homework solutions... #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,11 @@ | |
""" | ||
|
||
# Add any extra imports here | ||
import sys | ||
|
||
import sys, os | ||
import nibabel as nib | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from scipy.interpolate import InterpolatedUnivariateSpline | ||
|
||
def slice_time_image(img, slice_times, TR): | ||
"""Run slice-timing correction on nibabel image 'img'. | ||
|
@@ -47,8 +48,13 @@ def slice_time_image(img, slice_times, TR): | |
A new copy of the input image with slice-time interpolation applied | ||
""" | ||
# Get the image data as an array; | ||
data = img.get_data() | ||
|
||
# Make a new empty array "interp_data" to hold the interpolated data; | ||
interp_data = np.array(data.shape) | ||
|
||
# Do the interpolation; | ||
|
||
# This is how to make a new image with the interpolated data | ||
new_img = nib.Nifti1Image(interp_data, img.affine, img.header) | ||
return new_img | ||
|
@@ -71,23 +77,57 @@ def slice_time_file(fname, slice_times, TR): | |
TR: double | ||
Repetition time in seconds. | ||
""" | ||
img = nib.load(fname) | ||
fdir, fn = os.path.split(fname) | ||
fslug, ext = os.path.splitext(fn) | ||
new_fname = os.path.join(fdir, fslug+'_st'+ext) | ||
# Hint: use os.path.split and os.path.join to make the new filename | ||
nib.save(interp_img, new_fname) | ||
|
||
data = img.get_data() | ||
# discard first tp (???) | ||
fixed_data = data[..., 1:] | ||
new_data = fixed_data.copy() | ||
n_tps = fixed_data.shape[3] | ||
s0_times = TR * np.asarray(range(n_tps)) | ||
for x in range(fixed_data.shape[0]): | ||
for y in range(fixed_data.shape[1]): | ||
for z in range(fixed_data.shape[2]): | ||
ts = fixed_data[x, y, z, :] | ||
xs = (TR * np.asarray(range(n_tps))) + slice_times[z] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use |
||
interp = InterpolatedUnivariateSpline(xs, ts, k=1) | ||
new_series = interp(s0_times) | ||
new_data[x,y,z,:] = new_series | ||
|
||
new_img = nib.Nifti1Image(new_data, img.affine, img.header) | ||
nib.save(new_img, new_fname) | ||
|
||
def main(): | ||
""" This function will be called when this file is run as a script | ||
""" | ||
# Get the filename from the command line parameters | ||
fname = sys.argv[1] | ||
img = nib.load(fname) | ||
n_slices = img.shape[2] | ||
# Assume the TR | ||
TR = 2.0 | ||
slice_duration = TR/n_slices | ||
|
||
e_slices = range(0, n_slices, 2) # 0 2 4 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do all this with:
|
||
o_slices = range(1, n_slices, 2) # 1 3 5 | ||
ind_order = [] | ||
ind_order.extend(e_slices) | ||
ind_order.extend(o_slices) | ||
|
||
offsets = slice_duration * np.asarray(range(n_slices)) | ||
print offsets | ||
slice_times = np.zeros(n_slices) | ||
slice_times[ind_order] = offsets | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice algorithm. |
||
print(slice_times) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to remove debug prints from the final code. |
||
|
||
# Assume the slices were acquired even slices first, inferior to | ||
# superior, then odd slices, inferior to superior, where the most inferior | ||
# slice is index 0 and 0 is an even number. What are the slice acquisition | ||
# times in seconds, where the first value is the acquisition time of slice | ||
# 0, the second is acquisition time of slice 1, etc? | ||
slice_times = ? | ||
slice_time_file(fname, slice_times, TR) | ||
|
||
|
||
|
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.
Typical convention is to put less common imports lower down than more common. So, numpy is more common than scipy which is about the same popularity as matplotlib, and more common than nibabel.