Skip to content

Commit

Permalink
Modify calls to prelude, make tests faster
Browse files Browse the repository at this point in the history
  • Loading branch information
po09i committed Nov 20, 2020
1 parent b1d208c commit 007fecd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions examples/general_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def general_demo(path_output=os.path.join(os.path.curdir, 'output_dir')):
nii_mag_e2 = nib.load(fname_mags[1])

# Call prelude to unwrap the phase
unwrapped_phase_e1 = prelude(phase_e1, nii_mag_e1.get_fdata(), nii_phase_e1.affine)
unwrapped_phase_e2 = prelude(phase_e2, nii_mag_e2.get_fdata(), nii_phase_e2.affine, threshold=200)
unwrapped_phase_e1 = prelude(phase_e1, nii_phase_e1.affine, mag=nii_mag_e1.get_fdata())
unwrapped_phase_e2 = prelude(phase_e2, nii_phase_e2.affine, mag=nii_mag_e2.get_fdata(), threshold=200)

# Plot results
fig = Figure(figsize=(10, 10))
Expand Down
34 changes: 18 additions & 16 deletions test/test_prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ def get_phases_mags_affines(self):
nii_mag_e1 = nib.load(fname_mags[0])
nii_mag_e2 = nib.load(fname_mags[1])

self.phase_e1 = phase_e1
self.phase_e2 = phase_e2
self.mag_e1 = nii_mag_e1.get_fdata()
self.mag_e2 = nii_mag_e2.get_fdata()
# Make tests fater by having the last dim only be 1
self.phase_e1 = np.expand_dims(phase_e1[..., 0], -1)
self.phase_e2 = np.expand_dims(phase_e2[..., 0], -1)
self.mag_e1 = np.expand_dims(nii_mag_e1.get_fdata()[..., 0], -1)
self.mag_e2 = np.expand_dims(nii_mag_e2.get_fdata()[..., 0], -1)
self.affine_phase_e1 = nii_phase_e1.affine
self.affine_phase_e2 = nii_phase_e2.affine

Expand All @@ -76,7 +77,7 @@ def test_default_works(self):
Runs prelude and check output integrity.
"""
# default prelude call
unwrapped_phase_e1 = prelude(self.phase_e1, self.mag_e1, self.affine_phase_e1)
unwrapped_phase_e1 = prelude(self.phase_e1, self.affine_phase_e1)

assert (unwrapped_phase_e1.shape == self.phase_e1.shape)

Expand All @@ -88,18 +89,19 @@ def test_non_default_mask(self):
mask = np.ones(self.phase_e1.shape)

# Call prelude with mask (is_unwrapping_in_2d is also used because it is significantly faster)
unwrapped_phase_e1 = prelude(self.phase_e1, self.mag_e1, self.affine_phase_e1, mask, is_unwrapping_in_2d=True)
unwrapped_phase_e1 = prelude(self.phase_e1, self.affine_phase_e1, mag=self.mag_e1, mask=mask,
is_unwrapping_in_2d=True)

# Make sure the phase is not 0. When there isn't a mask, the phase is 0
assert(unwrapped_phase_e1[5, 5, 5] != 0)
assert(unwrapped_phase_e1[5, 5, 0] != 0)

def test_wrong_size_mask(self):
# Create mask with wrong dimensions
mask = np.ones([4, 4, 4])

# Call prelude with mask
try:
prelude(self.phase_e1, self.mag_e1, self.affine_phase_e1, mask)
prelude(self.phase_e1, self.affine_phase_e1, mag=self.mag_e1, mask=mask)
except RuntimeError:
# If an exception occurs, this is the desired behaviour since the mask is the wrong dimensions
return 0
Expand All @@ -113,7 +115,7 @@ def test_wrong_phase_dimensions(self):
phase_e1 = np.ones([4, 4])

try:
prelude(phase_e1, self.mag_e1, self.affine_phase_e1)
prelude(phase_e1, self.affine_phase_e1, mag=self.mag_e1)
except RuntimeError:
# If an exception occurs, this is the desired behaviour
return 0
Expand All @@ -127,7 +129,7 @@ def test_wrong_mag_dimensions(self):
mag_e1 = np.ones([4, 4, 4])

try:
prelude(self.phase_e1, mag_e1, self.affine_phase_e1)
prelude(self.phase_e1, self.affine_phase_e1, mag=mag_e1)
except RuntimeError:
# If an exception occurs, this is the desired behaviour
return 0
Expand All @@ -142,7 +144,7 @@ def test_wrong_mag_and_phase_dimensions(self):
phase_e1 = np.ones([4])

try:
prelude(phase_e1, mag_e1, self.affine_phase_e1)
prelude(phase_e1, self.affine_phase_e1, mag=mag_e1)
except RuntimeError:
# If an exception occurs, this is the desired behaviour
return 0
Expand All @@ -158,15 +160,15 @@ def test_phase_2d(self):
# Get first slice
phase_e1_2d = self.phase_e1[:, :, 0]
mag_e1_2d = self.mag_e1[:, :, 0]
unwrapped_phase_e1 = prelude(phase_e1_2d, mag_e1_2d, self.affine_phase_e1)
unwrapped_phase_e1 = prelude(phase_e1_2d, self.affine_phase_e1, mag=mag_e1_2d)

assert(unwrapped_phase_e1.shape == phase_e1_2d.shape)

def test_threshold(self):
"""
Call prelude with a threshold for masking
"""
unwrapped_phase_e1 = prelude(self.phase_e1, self.mag_e1, self.affine_phase_e1, threshold=200)
unwrapped_phase_e1 = prelude(self.phase_e1, self.affine_phase_e1, mag=self.mag_e1, threshold=200)
assert(unwrapped_phase_e1.shape == self.phase_e1.shape)

def test_3rd_dim_singleton(self):
Expand All @@ -178,7 +180,7 @@ def test_3rd_dim_singleton(self):
phase_singleton = np.expand_dims(self.phase_e1[..., 0], -1)
mag_singleton = np.expand_dims(self.mag_e1[..., 0], -1)

unwrapped_phase_singleton = prelude(phase_singleton, mag_singleton, self.affine_phase_e1)
unwrapped_phase_singleton = prelude(phase_singleton, self.affine_phase_e1, mag=mag_singleton)

assert unwrapped_phase_singleton.ndim == 3

Expand All @@ -191,7 +193,7 @@ def test_2nd_dim_singleton(self):
phase_singleton = np.expand_dims(self.phase_e1[:, 0, 0], -1)
mag_singleton = np.expand_dims(self.mag_e1[:, 0, 0], -1)

unwrapped_phase_singleton = prelude(phase_singleton, mag_singleton, self.affine_phase_e1)
unwrapped_phase_singleton = prelude(phase_singleton, self.affine_phase_e1, mag=mag_singleton)

assert unwrapped_phase_singleton.ndim == 2

Expand All @@ -204,6 +206,6 @@ def test_2nd_and_3rd_dim_singleton(self):
phase_singleton = np.expand_dims(np.expand_dims(self.phase_e1[:, 0, 0], -1), -1)
mag_singleton = np.expand_dims(np.expand_dims(self.mag_e1[:, 0, 0], -1), -1)

unwrapped_phase_singleton = prelude(phase_singleton, mag_singleton, self.affine_phase_e1)
unwrapped_phase_singleton = prelude(phase_singleton, self.affine_phase_e1, mag=mag_singleton)

assert unwrapped_phase_singleton.ndim == 3

0 comments on commit 007fecd

Please sign in to comment.