Ejemplo n.º 1
0
    def test_connect(self):
        rp = ResourcePool()

        mod2 = NipypeJob(EngineTestInterface(), reference="mod2")
        mod1 = NipypeJob(EngineTestInterface(), reference="mod1")
        mod2.input1 = mod1.output1

        rp[ResourceKey('T1w')] = mod2.output1

        g = DependencySolver(rp).graph

        self.assertIn(id(mod1), g.nodes)
        self.assertIn(id(mod2), g.nodes)
        self.assertTrue(
            networkx.algorithms.bidirectional_dijkstra(g, id(mod1), id(mod2)))
Ejemplo n.º 2
0
 def test_mock_nipype(self):
     with mock_nipype():
         from radiome.core.jobs import NipypeJob
         job = NipypeJob(interface=afni.Refit(deoblique=True),
                         reference='deoblique')
         in_file = data_path(__file__, 'mocks/sub-0050682_T1w.nii.gz')
         res = job(in_file=in_file)
         self.assertEqual(str(res['out_file']), in_file)
Ejemplo n.º 3
0
def create_workflow(config: AttrDict, resource_pool: ResourcePool,
                    context: Context):
    for _, rp in resource_pool[['label-reorient_T1w']]:
        anat_image = rp[R('T1w', label='reorient')]
        anat_skullstrip = NipypeJob(interface=afni.SkullStrip(
            outputtype='NIFTI_GZ',
            args=create_3dskullstrip_arg_string(**config)),
                                    reference='anat_skullstrip')
        anat_skullstrip.in_file = anat_image
        anat_brain_mask = NipypeJob(interface=afni.Calc(expr='step(a)',
                                                        outputtype='NIFTI_GZ'),
                                    reference='anat_brain_mask')
        anat_skullstrip_orig_vol = NipypeJob(
            interface=afni.Calc(expr='a*step(b)', outputtype='NIFTI_GZ'),
            reference='anat_skullstrip_orig_vol')
        anat_brain_mask.in_file_a = anat_skullstrip.out_file
        anat_skullstrip_orig_vol.in_file_a = anat_image
        anat_skullstrip_orig_vol.in_file_b = anat_brain_mask.out_file
        rp[R('T1w', desc='skullstrip-afni',
             suffix='mask')] = anat_brain_mask.out_file
        rp[R('T1w', desc='skullstrip-afni',
             suffix='brain')] = anat_skullstrip_orig_vol.out_file
Ejemplo n.º 4
0
def create_workflow(config: AttrDict, resource_pool: ResourcePool,
                    context: Context):
    for _, rp in resource_pool[['label-reorient_T1w']]:
        anat = rp[R('T1w', label='reorient')]
        anat_skullstrip = NipypeJob(interface=fsl.BET(output_type='NIFTI_GZ',
                                                      **config),
                                    reference='anat_skullstrip')
        anat_skullstrip.in_file = anat
        anat_skullstrip_orig_vol = NipypeJob(
            interface=afni.Calc(expr='a*step(b)', outputtype='NIFTI_GZ'),
            reference='anat_skullstrip_orig_vol')
        anat_skullstrip_orig_vol.in_file_a = anat
        anat_skullstrip_orig_vol.in_file_b = anat_skullstrip.out_file
        rp[R('T1w', desc='skullstrip-fsl',
             suffix='mask')] = anat_skullstrip.mask_file
        rp[R('T1w', desc='skullstrip-fsl',
             suffix='brain')] = anat_skullstrip_orig_vol.out_file
Ejemplo n.º 5
0
def lesion_preproc(lesion_mask: str):
    lesion_deoblique = NipypeJob(interface=afni.Refit(deoblique=True),
                                 reference='lesion_deoblique')
    lesion_inverted = PythonJob(function=inverse_lesion,
                                reference='inverse_lesion')
    lesion_reorient = NipypeJob(interface=afni.Resample(orientation='RPI',
                                                        outputtype='NIFTI_GZ'),
                                reference='lesion_reorient')

    lesion_inverted.lesion_path = S3Resource(lesion_mask) if lesion_mask.lower(
    ).startswith('s3://') else lesion_mask
    lesion_deoblique.in_file = lesion_inverted.lesion_out
    lesion_reorient.in_file = lesion_deoblique.out_file
    return lesion_reorient.out_file
Ejemplo n.º 6
0
def create_workflow(config: AttrDict, resource_pool: ResourcePool, context: Context):
    for _, rp in resource_pool[['T1w']]:
        anat_image = rp[R('T1w')]
        anat_deoblique = NipypeJob(
            interface=afni.Refit(deoblique=True),
            reference='deoblique'
        )
        anat_deoblique.in_file = anat_image
        output_node = anat_deoblique.out_file

        anat_reorient = NipypeJob(
            interface=afni.Resample(orientation='RPI', outputtype='NIFTI_GZ'),
            reference='reorient'
        )
        anat_reorient.in_file = output_node
        rp[R('T1w', label='reorient')] = anat_reorient.out_file
Ejemplo n.º 7
0
def create_workflow(config: AttrDict, resource_pool: ResourcePool,
                    context: Context):
    for _, rp in resource_pool[['brain', 'label-reorient_T1w']]:
        # TODO: disable skullstrip
        linear_reg = NipypeJob(interface=fsl.FLIRT(cost='corratio'),
                               reference='linear_reg_0')
        inv_flirt_xfm = NipypeJob(
            interface=fsl.utils.ConvertXFM(invert_xfm=True),
            reference='inv_linear_reg0_xfm')

        linear_reg.in_file = rp[R('brain')]
        linear_reg.reference = config.template_brain
        linear_reg.interp = config.interpolation

        inv_flirt_xfm.in_file = linear_reg.out_matrix_file

        if config.linear_only:
            rp[R('brain', space='MNI')] = linear_reg.out_file
            # other xfm
            return
        else:
            nonlinear_reg = NipypeJob(interface=fsl.FNIRT(fieldcoeff_file=True,
                                                          jacobian_file=True),
                                      reference='nonlinear_reg_1')
            brain_warp = NipypeJob(interface=fsl.ApplyWarp(),
                                   reference='brain_warp')

            nonlinear_reg.in_file = rp[R('T1w', label='reorient')]
            nonlinear_reg.ref_file = config.template_skull
            nonlinear_reg.refmask_file = config.ref_mask
            nonlinear_reg.config_file = config.fnirt_config
            nonlinear_reg.affine_file = linear_reg.out_matrix_file

            brain_warp.interp = config.interpolation
            brain_warp.in_file = rp[R('brain')]
            brain_warp.field_file = nonlinear_reg.fieldcoeff_file
            brain_warp.ref_file = config.template_brain
            rp[R('brain', space='MNI')] = brain_warp.out_file
Ejemplo n.º 8
0
def create_workflow(config: AttrDict, resource_pool: ResourcePool,
                    context: Context):
    for _, rp in resource_pool[['label-reorient_T1w']]:
        anat = rp[R('T1w', label='reorient')]
        train_model = UNet2d(dim_in=config.dim_in,
                             num_conv_block=config.num_conv_block,
                             kernel_root=config.kernel_root)
        if config.unet_model.lower().startswith('s3://'):
            unet_path = S3Resource(config.unet_model,
                                   working_dir=tempfile.mkdtemp())()
        else:
            unet_path = config.unet_model
        checkpoint = torch.load(unet_path, map_location={'cuda:0': 'cpu'})
        train_model.load_state_dict(checkpoint['state_dict'])
        model = nn.Sequential(train_model, nn.Softmax2d())

        # create a node called unet_mask
        unet_mask = PythonJob(function=predict_volumes, reference='unet_mask')
        unet_mask.model = Resource(model)
        unet_mask.cimg_in = anat
        """
        Revised mask with ANTs
        """
        # fslmaths <whole head> -mul <mask> brain.nii.gz
        unet_masked_brain = NipypeJob(
            interface=fsl.MultiImageMaths(op_string="-mul %s"),
            reference='unet_masked_brain')
        unet_masked_brain.in_file = anat
        unet_masked_brain.operand_files = unet_mask.output_path

        # flirt -v -dof 6 -in brain.nii.gz -ref NMT_SS_0.5mm.nii.gz -o brain_rot2atl -omat brain_rot2atl.mat -interp sinc
        # TODO change it to ANTs linear transform
        native_brain_to_template_brain = NipypeJob(
            interface=fsl.FLIRT(reference=config.template_brain_only_for_anat,
                                dof=6,
                                interp='sinc'),
            reference='native_brain_to_template_brain')
        native_brain_to_template_brain.in_file = unet_masked_brain.out_file

        # flirt -in head.nii.gz -ref NMT_0.5mm.nii.gz -o head_rot2atl -applyxfm -init brain_rot2atl.mat
        # TODO change it to ANTs linear transform
        native_head_to_template_head = NipypeJob(
            interface=fsl.FLIRT(reference=config.template_skull_for_anat,
                                apply_xfm=True),
            reference='native_head_to_template_head')
        native_head_to_template_head.in_file = anat
        native_head_to_template_head.in_matrix_file = native_brain_to_template_brain.out_matrix_file

        # fslmaths NMT_SS_0.5mm.nii.gz -bin templateMask.nii.gz
        template_brain_mask = NipypeJob(
            interface=fsl.maths.MathsCommand(args='-bin'),
            reference='template_brain_mask')
        template_brain_mask.in_file = config.template_brain_only_for_anat

        # ANTS 3 -m  CC[head_rot2atl.nii.gz,NMT_0.5mm.nii.gz,1,5] -t SyN[0.25] -r Gauss[3,0] -o atl2T1rot -i 60x50x20 --use-Histogram-Matching  --number-of-affine-iterations 10000x10000x10000x10000x10000 --MI-option 32x16000
        ants_template_head_to_template = NipypeJob(
            interface=ants.Registration(),
            reference='template_head_to_template')
        ants_template_head_to_template.metric = ['CC']
        ants_template_head_to_template.metric_weight = [1, 5]
        ants_template_head_to_template.moving_image = config.template_skull_for_anat
        ants_template_head_to_template.transforms = ['SyN']
        ants_template_head_to_template.transform_parameters = [(0.25, )]
        ants_template_head_to_template.interpolation = 'NearestNeighbor'
        ants_template_head_to_template.number_of_iterations = [[60, 50, 20]]
        ants_template_head_to_template.smoothing_sigmas = [[0.6, 0.2, 0.0]]
        ants_template_head_to_template.shrink_factors = [[4, 2, 1]]
        ants_template_head_to_template.convergence_threshold = [1.e-8]

        ants_template_head_to_template.fixed_image = native_head_to_template_head.out_file

        # antsApplyTransforms -d 3 -i templateMask.nii.gz -t atl2T1rotWarp.nii.gz atl2T1rotAffine.txt -r brain_rot2atl.nii.gz -o brain_rot2atl_mask.nii.gz
        template_head_transform_to_template = NipypeJob(
            interface=ants.ApplyTransforms(dimension=3),
            reference='template_head_transform_to_template')
        template_head_transform_to_template.input_image = template_brain_mask.out_file
        template_head_transform_to_template.reference_image = native_brain_to_template_brain.out_file
        template_head_transform_to_template.transforms = ants_template_head_to_template.forward_transforms

        # convert_xfm -omat brain_rot2native.mat -inverse brain_rot2atl.mat
        invt = NipypeJob(interface=fsl.ConvertXFM(invert_xfm=True),
                         reference='convert_xfm')
        invt.in_file = native_brain_to_template_brain.out_matrix_file

        # flirt -in brain_rot2atl_mask.nii.gz -ref brain.nii.gz -o brain_mask.nii.gz -applyxfm -init brain_rot2native.mat
        template_brain_to_native_brain = NipypeJob(
            interface=fsl.FLIRT(apply_xfm=True),
            reference='template_brain_to_native_brain')
        template_brain_to_native_brain.in_file = template_head_transform_to_template.output_image
        template_brain_to_native_brain.reference = unet_masked_brain.out_file
        template_brain_to_native_brain.in_matrix_file = invt.out_file

        # fslmaths brain_mask.nii.gz -thr .5 -bin brain_mask_thr.nii.gz
        refined_mask = NipypeJob(interface=fsl.Threshold(thresh=0.5,
                                                         args='-bin'),
                                 reference='refined_mask')
        refined_mask.in_file = template_brain_to_native_brain.out_file

        # get a new brain with mask
        refined_brain = NipypeJob(
            interface=fsl.MultiImageMaths(op_string="-mul %s"),
            reference='refined_brain')
        refined_brain.in_file = anat
        refined_brain.operand_files = refined_mask.out_file

        rp[R('T1w', desc='skullstrip-unet',
             suffix='mask')] = refined_mask.out_file
        rp[R('T1w', desc='skullstrip-unet',
             suffix='brain')] = refined_brain.out_file
Ejemplo n.º 9
0
def create_workflow(config: AttrDict, resource_pool: ResourcePool,
                    context: Context):
    for _, rp in resource_pool[['T1w']]:
        anat_image = rp[R('T1w')]
        anat_deoblique = NipypeJob(interface=afni.Refit(deoblique=True),
                                   reference='anat_deoblique')
        anat_deoblique.in_file = anat_image
        output_node = anat_deoblique.out_file

        if config.non_local_means_filtering:
            denoise = NipypeJob(interface=ants.DenoiseImage(),
                                reference='anat_denoise')
            denoise.input_image = output_node
            output_node = denoise.output_image

        if config.n4_bias_field_correction:
            n4 = NipypeJob(interface=ants.N4BiasFieldCorrection(
                dimension=3, shrink_factor=2, copy_header=True),
                           reference='anat_n4')
            n4.input_image = output_node
            output_node = n4.output_image

        anat_reorient = NipypeJob(interface=afni.Resample(
            orientation='RPI', outputtype='NIFTI_GZ'),
                                  reference='anat_reorient')
        anat_reorient.in_file = output_node
        rp[R('T1w', label='reorient')] = anat_reorient.out_file