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
PEP8 in sims #884 #960
PEP8 in sims #884 #960
Changes from 1 commit
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 |
---|---|---|
@@ -1 +1 @@ | ||
#init for simulations | ||
# init for simulations |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Test callable | ||
from numpy.testing import Tester | ||
test = Tester().test | ||
del Tester | ||
del Tester |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,10 @@ | |
from dipy.core.gradients import gradient_table | ||
|
||
|
||
fimg,fbvals,fbvecs=get_data('small_64D') | ||
bvals=np.load(fbvals) | ||
bvecs=np.load(fbvecs) | ||
bvecs[np.isnan(bvecs)]=0 | ||
fimg, fbvals, fbvecs = get_data('small_64D') | ||
bvals = np.load(fbvals) | ||
bvecs = np.load(fbvecs) | ||
bvecs[np.isnan(bvecs)] = 0 | ||
|
||
gtab = gradient_table(bvals, bvecs) | ||
|
||
|
@@ -29,10 +29,10 @@ def f(t): | |
""" | ||
Helper function used to define a mapping time => xyz | ||
""" | ||
x = np.linspace(-1,1,len(t)) | ||
y = np.linspace(-1,1,len(t)) | ||
z = np.linspace(-1,1,len(t)) | ||
return x,y,z | ||
x = np.linspace(-1, 1, len(t)) | ||
y = np.linspace(-1, 1, len(t)) | ||
z = np.linspace(-1, 1, len(t)) | ||
return x, y, z | ||
|
||
|
||
def test_phantom(): | ||
|
@@ -55,32 +55,32 @@ def test_phantom(): | |
FA[np.isnan(FA)] = 0 | ||
# 686 -> expected FA given diffusivities of [1500, 400, 400] | ||
l1, l2, l3 = 1500e-6, 400e-6, 400e-6 | ||
expected_fa = (np.sqrt(0.5) * np.sqrt((l1 - l2)**2 + (l2-l3)**2 + (l3-l1)**2 )/np.sqrt(l1**2 + l2**2 + l3**2)) | ||
expected_fa = np.sqrt(0.5) * np.sqrt((l1 - l2)**2 + (l2 - l3)**2 + (l3 - l1)**2) / np.sqrt(l1**2 + l2**2 + l3**2)) | ||
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. This line is probably still >80 characters long. A suggestion:
|
||
|
||
assert_array_almost_equal(FA.max(), expected_fa, decimal=2) | ||
assert_array_almost_equal(FA.max(), expected_fa, decimal = 2) | ||
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. No space needed here. |
||
|
||
|
||
def test_add_noise(): | ||
np.random.seed(1980) | ||
|
||
N = 50 | ||
S0 = 100 | ||
N=50 | ||
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. But space would be nice here. |
||
S0=100 | ||
|
||
options = dict(func=f, | ||
t=np.linspace(0, 2 * np.pi, N), | ||
datashape=(10, 10, 10, len(bvals)), | ||
origin=(5, 5, 5), | ||
scale=(3, 3, 3), | ||
angles=np.linspace(0, 2 * np.pi, 16), | ||
radii=np.linspace(0.2, 2, 6), | ||
S0=S0) | ||
options=dict(func = f, | ||
t = np.linspace(0, 2 * np.pi, N), | ||
datashape = (10, 10, 10, len(bvals)), | ||
origin = (5, 5, 5), | ||
scale = (3, 3, 3), | ||
angles = np.linspace(0, 2 * np.pi, 16), | ||
radii = np.linspace(0.2, 2, 6), | ||
S0 = S0) | ||
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. No spaces around "=" sign when assigning to a dict or when passing arguments to a function, please. |
||
|
||
vol = orbital_phantom(gtab, **options) | ||
vol=orbital_phantom(gtab, **options) | ||
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. But space around "=" when used in assignment! |
||
|
||
for snr in [10, 20, 30, 50]: | ||
vol_noise = orbital_phantom(gtab, snr=snr, **options) | ||
vol_noise=orbital_phantom(gtab, snr = snr, **options) | ||
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. Same as above. |
||
|
||
sigma = S0 / snr | ||
sigma=S0 / snr | ||
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. And same here. |
||
|
||
assert_(np.abs(np.var(vol_noise - vol) - sigma ** 2) < 1) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,9 +176,9 @@ def sticks_and_ball(gtab, d=0.0015, S0=100, angles=[(0, 0), (90, 0)], | |
sticks = _check_directions(angles) | ||
|
||
for (i, g) in enumerate(gtab.bvecs[1:]): | ||
S[i + 1] = f0*np.exp(-gtab.bvals[i + 1]*d) + \ | ||
np.sum([fractions[j]*np.exp(-gtab.bvals[i + 1]*d*np.dot(s, g)**2) | ||
for (j, s) in enumerate(sticks)]) | ||
S[i + 1] = f0 * np.exp(-gtab.bvals[i + 1] * d) + \ | ||
np.sum([fractions[j] * np.exp(-gtab.bvals[i + 1] * d * np.dot(s, g)**2) | ||
for (j, s) in enumerate(sticks)]) | ||
|
||
S[i + 1] = S0 * S[i + 1] | ||
|
||
|
@@ -298,16 +298,15 @@ def multi_tensor(gtab, mevals, S0=100, angles=[(0, 0), (90, 0)], | |
sticks = _check_directions(angles) | ||
|
||
for i in range(len(fractions)): | ||
S = S + fractions[i] * single_tensor(gtab, S0=S0, evals=mevals[i], | ||
evecs=all_tensor_evecs( | ||
sticks[i]), snr=None) | ||
S = S + fractions[i] * single_tensor(gtab, S0=S0, evals=mevals[i], | ||
evecs=all_tensor_evecs( | ||
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. put the function and its param on the same line, it's a bit hard to read like that |
||
sticks[i]), snr=None) | ||
|
||
return add_noise(S, snr, S0), sticks | ||
|
||
|
||
def multi_tensor_dki(gtab, mevals, S0=100, angles=[(90., 0.), (90., 0.)], | ||
fractions=[50, 50], snr=20): | ||
|
||
r""" Simulate the diffusion-weight signal, diffusion and kurtosis tensors | ||
based on the DKI model | ||
|
||
|
@@ -386,7 +385,7 @@ def multi_tensor_dki(gtab, mevals, S0=100, angles=[(90., 0.), (90., 0.)], | |
# compute voxel's DT | ||
DT = np.zeros((3, 3)) | ||
for i in range(len(fractions)): | ||
DT = DT + fractions[i]*D_comps[i] | ||
DT = DT + fractions[i] * D_comps[i] | ||
dt = np.array([DT[0][0], DT[0][1], DT[1][1], DT[0][2], DT[1][2], DT[2][2]]) | ||
|
||
# compute voxel's MD | ||
|
@@ -462,7 +461,7 @@ def kurtosis_element(D_comps, frac, ind_i, ind_j, ind_k, ind_l, DT=None, | |
if DT is None: | ||
DT = np.zeros((3, 3)) | ||
for i in range(len(frac)): | ||
DT = DT + frac[i]*D_comps[i] | ||
DT = DT + frac[i] * D_comps[i] | ||
|
||
if MD is None: | ||
MD = (DT[0][0] + DT[1][1] + DT[2][2]) / 3 | ||
|
@@ -471,13 +470,13 @@ def kurtosis_element(D_comps, frac, ind_i, ind_j, ind_k, ind_l, DT=None, | |
|
||
for f in range(len(frac)): | ||
wijkl = wijkl + frac[f] * ( | ||
D_comps[f][ind_i][ind_j]*D_comps[f][ind_k][ind_l] + | ||
D_comps[f][ind_i][ind_k]*D_comps[f][ind_j][ind_l] + | ||
D_comps[f][ind_i][ind_l]*D_comps[f][ind_j][ind_k]) | ||
D_comps[f][ind_i][ind_j] * D_comps[f][ind_k][ind_l] + | ||
D_comps[f][ind_i][ind_k] * D_comps[f][ind_j][ind_l] + | ||
D_comps[f][ind_i][ind_l] * D_comps[f][ind_j][ind_k]) | ||
|
||
wijkl = (wijkl - DT[ind_i][ind_j]*DT[ind_k][ind_l] - | ||
DT[ind_i][ind_k]*DT[ind_j][ind_l] - | ||
DT[ind_i][ind_l]*DT[ind_j][ind_k]) / (MD**2) | ||
wijkl = (wijkl - DT[ind_i][ind_j] * DT[ind_k][ind_l] - | ||
DT[ind_i][ind_k] * DT[ind_j][ind_l] - | ||
DT[ind_i][ind_l] * DT[ind_j][ind_k]) / (MD**2) | ||
|
||
return wijkl | ||
|
||
|
@@ -523,7 +522,7 @@ def DKI_signal(gtab, dt, kt, S0=150, snr=None): | |
|
||
# define vector of DKI parameters | ||
MD = (dt[0] + dt[2] + dt[5]) / 3 | ||
X = np.concatenate((dt, kt*MD*MD, np.array([np.log(S0)])), axis=0) | ||
X = np.concatenate((dt, kt * MD * MD, np.array([np.log(S0)])), axis=0) | ||
|
||
# Compute signals based on the DKI model | ||
S = np.exp(dot(A, X)) | ||
|
@@ -533,7 +532,6 @@ def DKI_signal(gtab, dt, kt, S0=150, snr=None): | |
return S | ||
|
||
|
||
|
||
def single_tensor_odf(r, evals=None, evecs=None): | ||
""" Simulated ODF with a single tensor. | ||
|
||
|
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.
Somebody might want to clean this up at some point, but maybe not on this PR.