Пример #1
0
    def test_wrong_mag_dimensions(self):
        # Call prelude phase with wrong dimensions
        mag_e1 = np.ones([4, 4, 4])

        try:
            prelude(self.phase_e1, self.affine_phase_e1, mag=mag_e1)
        except RuntimeError:
            # If an exception occurs, this is the desired behaviour
            return 0

        # If there isn't an error, then there is a problem
        print('\nWrong dimensions for mag input')
        assert False
Пример #2
0
    def test_default_works(self):
        """
        Runs prelude and check output integrity.
        """
        # default prelude call
        unwrapped_phase_e1 = prelude(self.phase_e1, self.affine_phase_e1)

        assert (unwrapped_phase_e1.shape == self.phase_e1.shape)
Пример #3
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.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

        # If there isn't an error, then there is a problem
        print('\nWrong dimensions for mask does not throw an error')
        assert False
Пример #4
0
 def test_threshold(self):
     """
     Call prelude with a threshold for masking
     """
     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)
Пример #5
0
    def test_phase_2d(self):
        """
        Call prelude with a 2D phase and mag array
        """
        # Get first slice
        phase_e1_2d = self.phase_e1[:, :, 0]
        mag_e1_2d = self.mag_e1[:, :, 0]
        unwrapped_phase_e1 = prelude(phase_e1_2d,
                                     self.affine_phase_e1,
                                     mag=mag_e1_2d)

        assert (unwrapped_phase_e1.shape == phase_e1_2d.shape)
Пример #6
0
    def test_2nd_dim_singleton(self):
        """
        Call prelude on data with a singleton on the 2nd dimension
        """

        # Prepare singleton
        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,
                                            self.affine_phase_e1,
                                            mag=mag_singleton)

        assert unwrapped_phase_singleton.ndim == 2
Пример #7
0
    def test_non_default_mask(self):
        """
        Check prelude function with input binary mask.
        """
        # Create mask with all ones
        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.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, 0] != 0)
Пример #8
0
def general_demo(path_output=os.path.join(os.path.curdir, 'output_dir')):
    """
    Args:
        path_output (str): Output directory to store data and results.

    Returns:
        str: File name and path of output figure
    """

    logging.basicConfig(level='INFO')

    # Download example data
    path_testing_data = os.path.join(path_output, 'testing_data')
    run_subprocess(f'st_download_data testing_data --output {path_testing_data}')

    # Transfer from dicom to nifti
    path_dicom_unsorted = os.path.join(path_testing_data, 'dicom_unsorted')
    path_nifti = os.path.join(path_output, 'niftis')
    dicom_to_nifti(path_dicom_unsorted, path_nifti, subject_id='sub-01')

    # Open phase data
    fname_phases = glob.glob(os.path.join(path_nifti, 'sub-01', 'fmap', '*phase*.nii.gz'))

    nii_phase_e1 = nib.load(fname_phases[0])
    nii_phase_e2 = nib.load(fname_phases[1])

    # Scale phase to radians
    phase_e1 = np.interp(nii_phase_e1.get_fdata(), [0, 4096], [-np.pi, np.pi])
    phase_e2 = np.interp(nii_phase_e2.get_fdata(), [0, 4096], [-np.pi, np.pi])

    # Open mag data
    fname_mags = glob.glob(os.path.join(path_nifti, 'sub-01', 'fmap', '*magnitude*.nii.gz'))

    nii_mag_e1 = nib.load(fname_mags[0])
    nii_mag_e2 = nib.load(fname_mags[1])

    # Call prelude to unwrap the phase
    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))
    # FigureCanvas(fig)
    ax = fig.add_subplot(3, 2, 1)
    im = ax.imshow(nii_mag_e1.get_fdata()[:-1, :-1, 0])
    fig.colorbar(im)
    ax.set_title("Mag e1")
    ax = fig.add_subplot(3, 2, 2)
    im = ax.imshow(nii_mag_e2.get_fdata()[:-1, :-1, 0])
    fig.colorbar(im)
    ax.set_title("Mag e2")
    ax = fig.add_subplot(3, 2, 3)
    im = ax.imshow(phase_e1[:-1, :-1, 0])
    fig.colorbar(im)
    ax.set_title("Wrapped e1")
    ax = fig.add_subplot(3, 2, 4)
    im = ax.imshow(phase_e2[:-1, :-1, 0])
    fig.colorbar(im)
    ax.set_title("Wrapped e2")
    ax = fig.add_subplot(3, 2, 5)
    im = ax.imshow(unwrapped_phase_e1[:-1, :-1, 0])
    fig.colorbar(im)
    ax.set_title("Unwrapped e1")
    ax = fig.add_subplot(3, 2, 6)
    im = ax.imshow(unwrapped_phase_e2[:-1, :-1, 0])
    fig.colorbar(im)
    ax.set_title("Unwrapped e2")

    fname_figure = os.path.join(path_output, 'unwrap_phase_plot.png')
    fig.savefig(fname_figure)

    return fname_figure