Beispiel #1
0
def test_write_struct():

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        # Trigger a warning
        pfo_study_in = os.path.join(root_dir, 'test_data', 'bru_banana')
        scan2struct(pfo_study_in)
        # manage warning in python 2 or python 3:
        if sys.version_info[0] == 3:
            assert len(w) == 1
            assert issubclass(w[-1].category, UserWarning)
        else:
            import exceptions
            assert len(w) == 1
            assert issubclass(w[-1].category, exceptions.UserWarning)

    pfo_scan_in = os.path.join(root_dir, 'test_data', 'bru_banana', '1')
    banana_struct = scan2struct(pfo_scan_in)

    pfo_output = os.path.join(root_dir, 'test_data', 'nifti_banana', 'write_struct_here')
    if not os.path.exists(pfo_output):
        os.system('mkdir {}'.format(pfo_output))

    write_struct(banana_struct, pfo_output, fin_scan='test')

    assert os.path.exists(os.path.join(pfo_output, 'acquisition_method.txt'))
    assert os.path.exists(os.path.join(pfo_output, 'test.nii.gz'))
Beispiel #2
0
def test_scan2struct_with_banana_data_slope_no_slope():

    pfo_scan_in = os.path.join(root_dir, 'test_data', 'bru_banana', '1')

    struct_no_slope_corrected = scan2struct(pfo_scan_in, correct_slope=False)

    struct_yes_slope_corrected = scan2struct(pfo_scan_in, correct_slope=True)

    parsed_slope_from_no = struct_no_slope_corrected['visu_pars_list'][0]['VisuCoreDataSlope']
    parsed_slope_from_yes = struct_yes_slope_corrected['visu_pars_list'][0]['VisuCoreDataSlope']
    ground_slope = np.array([11.02701887, ] * 5)

    # check general structure:
    assert_equal(struct_no_slope_corrected['nib_scans_list'][0].shape,
                 struct_yes_slope_corrected['nib_scans_list'][0].shape)
    assert_array_equal(parsed_slope_from_no, parsed_slope_from_yes)
    np.testing.assert_array_almost_equal(parsed_slope_from_no, ground_slope)

    assert_equal(struct_no_slope_corrected['acqp'], {})
    assert_equal(struct_no_slope_corrected['reco'], {})
    assert_equal(struct_no_slope_corrected['method'], {})

    # check data of the nibabel images:
    np.testing.assert_array_almost_equal(ground_slope[0] * struct_no_slope_corrected['nib_scans_list'][0].get_data(),
                                         struct_yes_slope_corrected['nib_scans_list'][0].get_data(), decimal=4)
Beispiel #3
0
def test_scan2struct_with_banana_data_slope_no_slope():

    pfo_scan_in = os.path.join(root_dir, "test_data", "bru_banana", "1")

    struct_no_slope_corrected = scan2struct(pfo_scan_in, correct_slope=False)

    struct_yes_slope_corrected = scan2struct(pfo_scan_in, correct_slope=True)

    parsed_slope_from_no = struct_no_slope_corrected["visu_pars_list"][0][
        "VisuCoreDataSlope"]
    parsed_slope_from_yes = struct_yes_slope_corrected["visu_pars_list"][0][
        "VisuCoreDataSlope"]
    ground_slope = np.array([11.02701887] * 5)

    # check general structure:
    assert_equal(
        struct_no_slope_corrected["nib_scans_list"][0].shape,
        struct_yes_slope_corrected["nib_scans_list"][0].shape,
    )
    assert_array_equal(parsed_slope_from_no, parsed_slope_from_yes)
    np.testing.assert_array_almost_equal(parsed_slope_from_no, ground_slope)

    assert_equal(struct_no_slope_corrected["acqp"], {})
    assert_equal(struct_no_slope_corrected["reco"], {})
    assert_equal(struct_no_slope_corrected["method"], {})

    # check data of the nibabel images:
    np.testing.assert_array_almost_equal(
        ground_slope[0] *
        struct_no_slope_corrected["nib_scans_list"][0].get_data(),
        struct_yes_slope_corrected["nib_scans_list"][0].get_data(),
        decimal=4,
    )
Beispiel #4
0
    def convert_scan(self,
                     pfo_input_scan,
                     pfo_output_converted,
                     nifti_file_name=None,
                     create_output_folder_if_not_exists=True):
        """
        :param pfo_input_scan: path to folder (pfo) containing a scan from Bruker, see documentation for the difference
         between Bruker 'scan' and Bruker 'study'.
        :param pfo_output_converted: path to the folder where the converted scan will be stored.
        :param create_output_folder_if_not_exists: [True] if the output folder does not exist will be created.
        :param nifti_file_name: [None] filename of the nifti image that will be saved into the pfo_output folder.
         If None, the filename will be obtained from the parameter file of the study.
        :return: [None] save the data parsed from the raw Bruker scan into a folder, including the nifti image.
        """

        if not os.path.isdir(pfo_input_scan):
            raise IOError('Input folder does not exist.')

        if create_output_folder_if_not_exists:
            os.makedirs(pfo_output_converted)

        print('FRAME BODY {}'.format(self.frame_body_as_frame_head))
        print('UPSIDE DOWN {}'.format(self.sample_upside_down))

        struct_scan = scan2struct(
            pfo_input_scan,
            correct_slope=self.correct_slope,
            correct_offset=self.correct_offset,
            sample_upside_down=self.sample_upside_down,
            nifti_version=self.nifti_version,
            qform_code=self.qform_code,
            sform_code=self.sform_code,
            get_acqp=self.get_acqp,
            get_method=self.get_method,
            get_reco=self.get_reco,
            frame_body_as_frame_head=self.frame_body_as_frame_head)

        if struct_scan is not None:
            write_struct(
                struct_scan,
                pfo_output_converted,
                fin_scan=nifti_file_name,
                save_human_readable=self.save_human_readable,
                save_b0_if_dwi=self.save_b0_if_dwi,
                verbose=self.verbose,
            )