Esempio n. 1
0
    def _run_interface(self, runtime):
        from rabies.preprocess_pkg.utils import flatten_list
        merged = flatten_list(list(self.inputs.mask_dict_list))
        mask_dict = merged[0]  # all mask files are assumed to be identical
        brain_mask_file = mask_dict['brain_mask_file']
        WM_mask_file = mask_dict['WM_mask_file']
        CSF_mask_file = mask_dict['CSF_mask_file']

        # resample the template to the EPI dimensions
        from rabies.preprocess_pkg.utils import resample_image_spacing
        resampled = resample_image_spacing(
            sitk.ReadImage(mask_dict['preprocess_anat_template']),
            sitk.ReadImage(brain_mask_file).GetSpacing(),
            resampling_interpolation='BSpline')
        template_file = os.path.abspath('display_template.nii.gz')
        sitk.WriteImage(resampled, template_file)

        if self.inputs.DSURQE_regions:
            right_hem_mask_file = resample_mask(
                os.environ['RABIES'] +
                '/template_files/DSURQE_100micron_right_hem_mask.nii.gz',
                brain_mask_file)
            left_hem_mask_file = resample_mask(
                os.environ['RABIES'] +
                '/template_files/DSURQE_100micron_left_hem_mask.nii.gz',
                brain_mask_file)
        else:
            right_hem_mask_file = ''
            left_hem_mask_file = ''

        prior_maps = resample_IC_file(self.inputs.prior_maps, brain_mask_file)

        edge_mask_file = os.path.abspath('edge_mask.nii.gz')
        compute_edge_mask(brain_mask_file, edge_mask_file, num_edge_voxels=1)
        mask_file_dict = {
            'template_file': template_file,
            'brain_mask': brain_mask_file,
            'WM_mask': WM_mask_file,
            'CSF_mask': CSF_mask_file,
            'edge_mask': edge_mask_file,
            'right_hem_mask': right_hem_mask_file,
            'left_hem_mask': left_hem_mask_file,
            'prior_maps': prior_maps
        }

        setattr(self, 'mask_file_dict', mask_file_dict)
        return runtime
Esempio n. 2
0
def run_group_ICA(bold_file_list, mask_file, dim, tr):
    import os
    import pandas as pd

    # create a filelist.txt
    file_path = os.path.abspath('filelist.txt')
    from rabies.preprocess_pkg.utils import flatten_list
    merged = flatten_list(list(bold_file_list))
    df = pd.DataFrame(data=merged)
    df.to_csv(file_path, header=False, sep=',', index=False)

    from rabies.preprocess_pkg.utils import run_command
    out_dir = os.path.abspath('group_melodic.ica')
    command = 'melodic -i %s -m %s -o %s --tr=%s -d %s --report' % (
        file_path, mask_file, out_dir, tr, dim)
    rc = run_command(command)
    IC_file = out_dir + '/melodic_IC.nii.gz'
    return out_dir, IC_file
Esempio n. 3
0
    def _run_interface(self, runtime):
        import os
        import pandas as pd
        import pathlib
        from rabies.preprocess_pkg.utils import run_command

        cwd = os.getcwd()
        template_folder = self.inputs.output_folder + '/ants_dbm_outputs/'

        if os.path.isdir(template_folder):
            # remove previous run
            command = 'rm -r %s' % (template_folder, )
            rc = run_command(command)
        command = 'mkdir -p %s' % (template_folder, )
        rc = run_command(command)

        # create a csv file of the input image list
        csv_path = cwd + '/commonspace_input_files.csv'
        from rabies.preprocess_pkg.utils import flatten_list
        merged = flatten_list(list(self.inputs.moving_image))

        if len(merged) == 1:
            import logging
            log = logging.getLogger('root')
            log.info(
                "Only a single scan was provided as input for commonspace registration. Commonspace registration "
                "won't be run, and the output template will be the input scan."
            )

            # create an identity transform as a surrogate for the commonspace transforms
            import SimpleITK as sitk
            dimension = 3
            identity = sitk.Transform(dimension, sitk.sitkIdentity)

            file = merged[0]
            filename_template = pathlib.Path(file).name.rsplit(".nii")[0]
            transform_file = template_folder + filename_template + '_identity.mat'
            sitk.WriteTransform(identity, transform_file)

            setattr(self, 'warped_image', file)
            setattr(self, 'affine_list', [transform_file])
            setattr(self, 'warp_list', [transform_file])
            setattr(self, 'inverse_warp_list', [transform_file])
            setattr(self, 'warped_anat_list', [file])

            return runtime

        df = pd.DataFrame(data=merged)
        df.to_csv(csv_path, header=False, sep=',', index=False)

        command = 'cd %s ; ants_dbm.sh %s %s %s %s %s %s' % (
            template_folder, csv_path, self.inputs.template_anat,
            self.inputs.cluster_type, self.inputs.walltime,
            self.inputs.memory_request, self.inputs.local_threads)
        rc = run_command(command)

        # verify that all outputs are present
        ants_dbm_template = template_folder + \
            '/output/secondlevel/secondlevel_template0.nii.gz'
        if not os.path.isfile(ants_dbm_template):
            raise ValueError(ants_dbm_template + " doesn't exists.")

        affine_list = []
        warp_list = []
        inverse_warp_list = []
        warped_anat_list = []

        i = 0
        for file in merged:
            file = str(file)
            filename_template = pathlib.Path(file).name.rsplit(".nii")[0]
            anat_to_template_inverse_warp = '%s/output/secondlevel/secondlevel_%s%s1InverseWarp.nii.gz' % (
                template_folder,
                filename_template,
                str(i),
            )
            if not os.path.isfile(anat_to_template_inverse_warp):
                raise ValueError(anat_to_template_inverse_warp +
                                 " file doesn't exists.")
            anat_to_template_warp = '%s/output/secondlevel/secondlevel_%s%s1Warp.nii.gz' % (
                template_folder,
                filename_template,
                str(i),
            )
            if not os.path.isfile(anat_to_template_warp):
                raise ValueError(anat_to_template_warp +
                                 " file doesn't exists.")
            anat_to_template_affine = '%s/output/secondlevel/secondlevel_%s%s0GenericAffine.mat' % (
                template_folder,
                filename_template,
                str(i),
            )
            if not os.path.isfile(anat_to_template_affine):
                raise ValueError(anat_to_template_affine +
                                 " file doesn't exists.")
            warped_anat = '%s/output/secondlevel/secondlevel_template0%s%sWarpedToTemplate.nii.gz' % (
                template_folder,
                filename_template,
                str(i),
            )
            if not os.path.isfile(warped_anat):
                raise ValueError(warped_anat + " file doesn't exists.")
            inverse_warp_list.append(anat_to_template_inverse_warp)
            warp_list.append(anat_to_template_warp)
            affine_list.append(anat_to_template_affine)
            warped_anat_list.append(warped_anat)
            i += 1

        setattr(self, 'warped_image', ants_dbm_template)
        setattr(self, 'affine_list', affine_list)
        setattr(self, 'warp_list', warp_list)
        setattr(self, 'inverse_warp_list', inverse_warp_list)
        setattr(self, 'warped_anat_list', warped_anat_list)

        return runtime
Esempio n. 4
0
    def _run_interface(self, runtime):
        import os
        import pandas as pd
        import pathlib

        cwd = os.getcwd()
        template_folder = self.inputs.output_folder + '/ants_dbm_outputs/'

        if os.path.isdir(template_folder):
            print(
                'Previous ants_dbm_outputs/ folder detected. Inputs from a previous run may cause issues for the commonspace registration, so consider removing the previous folder before running again.'
            )
        command = 'mkdir -p %s' % (template_folder, )
        from rabies.preprocess_pkg.utils import run_command
        rc = run_command(command)

        # create a csv file of the input image list
        csv_path = cwd + '/commonspace_input_files.csv'
        from rabies.preprocess_pkg.utils import flatten_list
        merged = flatten_list(list(self.inputs.moving_image))

        if len(merged) == 1:
            print(
                "Only a single scan was provided as input for commonspace registration. Commonspace registration "
                "won't be run, and the output template will be the input scan."
            )

            # create an identity transform as a surrogate for the commonspace transforms
            import SimpleITK as sitk
            dimension = 3
            identity = sitk.Transform(dimension, sitk.sitkIdentity)

            file = merged[0]
            filename_template = pathlib.Path(file).name.rsplit(".nii")[0]
            transform_file = template_folder + filename_template + '_identity.mat'
            sitk.WriteTransform(identity, transform_file)

            setattr(self, 'warped_image', file)
            setattr(self, 'affine_list', [transform_file])
            setattr(self, 'warp_list', [transform_file])
            setattr(self, 'inverse_warp_list', [transform_file])
            setattr(self, 'warped_anat_list', [file])

            return runtime

        df = pd.DataFrame(data=merged)
        df.to_csv(csv_path, header=False, sep=',', index=False)

        import rabies
        dir_path = os.path.dirname(os.path.realpath(rabies.__file__))
        model_script_path = dir_path + '/shell_scripts/ants_dbm.sh'

        command = 'cd %s ; bash %s %s %s %s %s %s %s' % (
            template_folder, model_script_path, csv_path,
            self.inputs.template_anat, self.inputs.cluster_type,
            self.inputs.walltime, self.inputs.memory_request,
            self.inputs.local_threads)
        import subprocess
        try:
            process = subprocess.run(
                command,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                check=True,
                shell=True,
            )
        except Exception as e:
            print(e.output.decode("utf-8"))
            #log.warning(e.output.decode("utf-8"))

        #rc = run_command(command)

        # verify that all outputs are present
        ants_dbm_template = template_folder + \
            '/output/secondlevel/secondlevel_template0.nii.gz'
        if not os.path.isfile(ants_dbm_template):
            raise ValueError(ants_dbm_template + " doesn't exists.")

        affine_list = []
        warp_list = []
        inverse_warp_list = []
        warped_anat_list = []

        i = 0
        for file in merged:
            file = str(file)
            filename_template = pathlib.Path(file).name.rsplit(".nii")[0]
            anat_to_template_inverse_warp = '%s/output/secondlevel/secondlevel_%s%s1InverseWarp.nii.gz' % (
                template_folder,
                filename_template,
                str(i),
            )
            if not os.path.isfile(anat_to_template_inverse_warp):
                raise ValueError(anat_to_template_inverse_warp +
                                 " file doesn't exists.")
            anat_to_template_warp = '%s/output/secondlevel/secondlevel_%s%s1Warp.nii.gz' % (
                template_folder,
                filename_template,
                str(i),
            )
            if not os.path.isfile(anat_to_template_warp):
                raise ValueError(anat_to_template_warp +
                                 " file doesn't exists.")
            anat_to_template_affine = '%s/output/secondlevel/secondlevel_%s%s0GenericAffine.mat' % (
                template_folder,
                filename_template,
                str(i),
            )
            if not os.path.isfile(anat_to_template_affine):
                raise ValueError(anat_to_template_affine +
                                 " file doesn't exists.")
            warped_anat = '%s/output/secondlevel/secondlevel_template0%s%sWarpedToTemplate.nii.gz' % (
                template_folder,
                filename_template,
                str(i),
            )
            if not os.path.isfile(warped_anat):
                raise ValueError(warped_anat + " file doesn't exists.")
            inverse_warp_list.append(anat_to_template_inverse_warp)
            warp_list.append(anat_to_template_warp)
            affine_list.append(anat_to_template_affine)
            warped_anat_list.append(warped_anat)
            i += 1

        setattr(self, 'warped_image', ants_dbm_template)
        setattr(self, 'affine_list', affine_list)
        setattr(self, 'warp_list', warp_list)
        setattr(self, 'inverse_warp_list', inverse_warp_list)
        setattr(self, 'warped_anat_list', warped_anat_list)

        return runtime
Esempio n. 5
0
    def _run_interface(self, runtime):
        from rabies.preprocess_pkg.utils import flatten_list
        merged = flatten_list(list(self.inputs.spatial_info_list))
        if len(merged) < 3:
            import logging
            log = logging.getLogger('root')
            log.warning(
                "Cannot run statistics on a sample size smaller than 3, so an empty figure is generated."
            )
            fig, axes = plt.subplots()
            fig.savefig(os.path.abspath('empty_dataset_diagnosis.png'),
                        bbox_inches='tight')

            setattr(self, 'figure_dataset_diagnosis',
                    os.path.abspath('empty_dataset_diagnosis.png'))
            return runtime

        dict_keys = [
            'temporal_std', 'VE_spatial', 'GS_corr', 'DVARS_corr', 'FD_corr',
            'DR_BOLD', 'prior_modeling_maps'
        ]

        voxelwise_list = []
        for spatial_info in merged:
            sub_list = [spatial_info[key] for key in dict_keys]
            voxelwise_sub = np.array(sub_list[:5])
            if len(sub_list[6]) > 0:
                voxelwise_sub = np.concatenate(
                    (voxelwise_sub, np.array(sub_list[5]), np.array(
                        sub_list[6])),
                    axis=0)
            else:
                voxelwise_sub = np.concatenate(
                    (voxelwise_sub, np.array(sub_list[5])), axis=0)
            voxelwise_list.append(voxelwise_sub)
            num_DR_maps = len(sub_list[5])
            num_prior_maps = len(sub_list[6])
        voxelwise_array = np.array(voxelwise_list)

        label_name = [
            'temporal_std', 'VE_spatial', 'GS_corr', 'DVARS_corr', 'FD_corr'
        ]
        label_name += [
            'BOLD Dual Regression map %s' % (i) for i in range(num_DR_maps)
        ]
        label_name += [
            'BOLD Dual Convergence map %s' % (i) for i in range(num_prior_maps)
        ]

        template_file = self.inputs.mask_file_dict['template_file']
        mask_file = self.inputs.mask_file_dict['brain_mask']
        from rabies.preprocess_pkg.preprocess_visual_QC import plot_3d, otsu_scaling
        scaled = otsu_scaling(template_file)

        ncols = 5
        fig, axes = plt.subplots(nrows=voxelwise_array.shape[1],
                                 ncols=ncols,
                                 figsize=(12 * ncols,
                                          2 * voxelwise_array.shape[1]))
        for i, x_label in zip(range(voxelwise_array.shape[1]), label_name):
            for j, y_label in zip(range(ncols), label_name[:ncols]):
                ax = axes[i, j]
                if i <= j:
                    ax.axis('off')
                    continue

                X = voxelwise_array[:, i, :]
                Y = voxelwise_array[:, j, :]
                corr = elementwise_corrcoef(X, Y)

                plot_3d([ax],
                        scaled,
                        fig,
                        vmin=0,
                        vmax=1,
                        cmap='gray',
                        alpha=1,
                        cbar=False,
                        num_slices=6,
                        planes=('coronal'))
                analysis_functions.recover_3D(
                    mask_file, corr).to_filename('temp_img.nii.gz')
                sitk_img = sitk.ReadImage('temp_img.nii.gz')
                plot_3d([ax],
                        sitk_img,
                        fig,
                        vmin=-0.7,
                        vmax=0.7,
                        cmap='cold_hot',
                        alpha=1,
                        cbar=True,
                        threshold=0.1,
                        num_slices=6,
                        planes=('coronal'))
                ax.set_title('Cross-correlation for %s and %s' %
                             (x_label, y_label),
                             fontsize=15)
        fig.savefig(os.path.abspath('dataset_diagnosis.png'),
                    bbox_inches='tight')

        setattr(self, 'figure_dataset_diagnosis',
                os.path.abspath('dataset_diagnosis.png'))
        return runtime