示例#1
0
    def test_wrong_dim_fieldmap(self):
        """Wrong number of fieldmap dimensions."""

        fieldmap = self.nii_fieldmap.get_fdata()
        nii_fieldmap_3d = nib.Nifti1Image(fieldmap[..., 0], self.nii_fieldmap.affine)

        # This should return an error
        try:
            realtime_zshim(nii_fieldmap_3d, self.nii_anat, self.pmu, self.json)
        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 number of dimensions for fieldmap but does not throw an error.")
        assert False
示例#2
0
def realtime_zshim_cli(fname_fmap,
                       fname_mask_anat,
                       fname_resp,
                       fname_anat,
                       fname_output,
                       verbose=True):
    """ Perform realtime z-shimming. This function will generate textfile containing static and dynamic (due to
    respiration) Gz components based on a fieldmap time series and respiratory trace information obtained from Siemens
    bellows (PMUresp_signal.resp). An additional multi-gradient echo (MGRE) magnitiude image is used to resample the
    static and dynamic Gz component maps to match the MGRE image. Lastly the mean Gz values within the ROI are computed
    for each slice. The mean pressure is also generated in the text file to be used to shim.
    """

    # Load fieldmap
    nii_fmap = nib.load(fname_fmap)

    # Load anat
    nii_anat = nib.load(fname_anat)

    # Load anatomical mask
    if fname_mask_anat is not None:
        nii_mask_anat = nib.load(fname_mask_anat)
    else:
        nii_mask_anat = None

    fname_json = fname_fmap.rsplit('.nii', 1)[0] + '.json'
    with open(fname_json) as json_file:
        json_data = json.load(json_file)

    # Load PMU
    pmu = PmuResp(fname_resp)

    # Look if output directory exists, if not, create it
    if not os.path.exists(fname_output):
        os.makedirs(fname_output)

    static_correction, riro_correction, mean_p, pressure_rms = realtime_zshim(
        nii_fmap,
        nii_anat,
        pmu,
        json_data,
        nii_mask_anat=nii_mask_anat,
        path_output=fname_output)

    # Write to a text file
    fname_corrections = os.path.join(fname_output, 'zshim_gradients.txt')
    file_gradients = open(fname_corrections, 'w')
    for i_slice in range(static_correction.shape[-1]):
        file_gradients.write(
            f'Vector_Gz[0][{i_slice}]= {static_correction[i_slice]:.6f}\n')
        file_gradients.write(
            f'Vector_Gz[1][{i_slice}]= {riro_correction[i_slice] / pressure_rms:.12f}\n'
        )
        file_gradients.write(f'Vector_Gz[2][{i_slice}]= {mean_p:.3f}\n')
    file_gradients.close()

    return fname_corrections
示例#3
0
 def test_output_figure(self):
     """Test realtime_zshim output figures parameter"""
     with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
         static_correction, riro_correction, mean_p, pressure_rms = realtime_zshim(self.nii_fieldmap,
                                                                                   self.nii_anat,
                                                                                   self.pmu,
                                                                                   self.json,
                                                                                   nii_mask_anat=self.nii_mask,
                                                                                   path_output=tmp)
         assert len(os.listdir(tmp)) != 0
示例#4
0
 def test_default(self):
     """Test realtime_zshim default parameters"""
     static_correction, riro_correction, mean_p, pressure_rms = realtime_zshim(self.nii_fieldmap,
                                                                               self.nii_anat,
                                                                               self.pmu,
                                                                               self.json)
     assert np.isclose(static_correction[0], 0.1291926595061463,)
     assert np.isclose(riro_correction[0], -0.00802980555042238)
     assert np.isclose(mean_p, 1326.3179660207873)
     assert np.isclose(pressure_rms, 1493.9468284155396)
示例#5
0
 def test_mask(self):
     """Test realtime_zshim mask parameter"""
     static_correction, riro_correction, mean_p, pressure_rms = realtime_zshim(self.nii_fieldmap,
                                                                               self.nii_anat,
                                                                               self.pmu,
                                                                               self.json,
                                                                               nii_mask_anat=self.nii_mask)
     assert np.isclose(static_correction[0], 0.2766538103967352)
     assert np.isclose(riro_correction[0], -0.051144561917725075)
     assert np.isclose(mean_p, 1326.318)
     assert np.isclose(pressure_rms, 1493.9468284155396)
    def test_wrong_dim_mask_riro(self):
        """Wrong number of riro mask dimensions."""

        mask = self.nii_mask_riro.get_fdata()
        nii_mask_2d = nib.Nifti1Image(mask[..., 0], self.nii_fieldmap.affine)

        # This should return an error
        try:
            realtime_zshim(self.nii_fieldmap,
                           self.nii_anat,
                           self.pmu,
                           self.json,
                           nii_mask_anat_riro=nii_mask_2d)
        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 number of dimensions for riro mask but does not throw an error."
        )
        assert False
    def test_output_figure(self):
        """Test realtime_zshim output figures parameter"""
        with tempfile.TemporaryDirectory(prefix='st_' +
                                         pathlib.Path(__file__).stem) as tmp:
            _, _, _, _ = realtime_zshim(
                self.nii_fieldmap,
                self.nii_anat,
                self.pmu,
                self.json,
                nii_mask_anat_static=self.nii_mask_static,
                nii_mask_anat_riro=self.nii_mask_riro,
                path_output=tmp)

            assert len(os.listdir(tmp)) != 0