lama/elastix/reverse_registration.py
def run_registration_schedule(config: LamaConfig, fixed_vol, moving_vol: Path, outdir: Path) -> Path:
    """
    Run the registrations specified in the config file, but flip the moving and fixed images

    Returns
    -------
    The path to the final registrered images
    """
    # egp = {'WriteResultImage': 'false',   # We only need the tform files not the images
    #        'WriteResultImageAfterEachResolution': 'true'}

    elastix_stage_parameters = run_lama.generate_elx_parameters(config)

    # Set the moving volume dir and the fixed image for the first stage
    #  Set the fixed volume up for the first stage. This will checnge each stage if doing population average
    stage_ids = []
    stage_spec_dirs = []

    for i, reg_stage in enumerate(config['registration_stage_params']):

        #  Make the stage output dir
        stage_id = reg_stage['stage_id']
        stage_ids.append(stage_id)
        stage_dir = outdir / stage_id
        stage_dir.mkdir(exist_ok=True)

        logging.info("### Reverse registration - current registration step: {} ###".format(stage_id))

        # Make the elastix parameter file for this stage
        elxparam = elastix_stage_parameters[stage_id]
        elxparam_path = join(stage_dir, ELX_PARAM_PREFIX + stage_id + '.txt')

        with open(elxparam_path, 'w') as fh:
            fh.write(elxparam)

        # maybe we should add fixed mask
        fixed_mask = None

        # Do the registrations
        registrator = TargetBasedRegistration(elxparam_path,
                                 moving_vol,
                                 stage_dir,
                                 config['filetype'],
                                 config['threads'],
                                 fixed_mask
                                 )

        registrator.set_target(fixed_vol)

        # issues/133: switch off fix folding for label propagation until fixed
        registrator.fix_folding = False

        # if reg_stage['elastix_parameters']['Transform'] == 'BSplineTransform':
        #     if config['fix_folding']:
        #         logging.info(f'Folding correction for stage {stage_id} set')
        #         registrator.fix_folding = True

        registrator.run()  # Do the registrations for a single stage
        os.remove(elxparam_path)

        # As the stage output diretory is named as the moving image, but in this case we want it named the same as the
        # fixed image
        stage_spec_dir = next(stage_dir.glob(f'*{moving_vol.stem}'))
        new_stage_spec_dir = stage_dir / fixed_vol.stem
        stage_spec_dir.rename(new_stage_spec_dir)

        stage_spec_dirs.append(new_stage_spec_dir)

        moving_vol = new_stage_spec_dir / moving_vol.name

        src_tform_file = stage_dir / fixed_vol.stem / ELX_TRANSFORM_NAME
        label_tform_file = stage_dir / fixed_vol.stem / PROPAGATE_LABEL_TRANFORM
        image_tform_file = stage_dir / fixed_vol.stem / PROPAGATE_IMAGE_TRANSFORM
        modify_elx_parameter_file(src_tform_file, label_tform_file, LABEL_REPLACEMENTS)
        modify_elx_parameter_file(src_tform_file, image_tform_file, IMAGE_REPLACEMENTS)

        # move_intemediate_volumes(stage_dir)

    logging.info("### Reverse registration finished ###")

    # Now delete everything we don't need
    to_keep = [PROPAGATE_LABEL_TRANFORM, 'elastix.log', PROPAGATE_IMAGE_TRANSFORM]

    for s in stage_spec_dirs:
        for f in s.iterdir():
            if f.name not in to_keep:
                try:
                    shutil.rmtree(f)
                except NotADirectoryError:
                    f.unlink()

    d = {'label_propagation_order': stage_ids}
    with open(outdir / PROPAGATE_CONFIG, 'w') as fh:
        yaml.dump(d, fh)

    Run the registrations specified in the config file, but flip the moving and fixed images

    Returns
    -------
    The path to the final registrered images
    
Run the registrations specified in the config file, but flip the moving and fixed images
Returns

The path to the final registrered images
Run the registrations specified in the config file but flip the moving and fixed images Returns The path to the final registrered images
def run_registration_schedule(config: LamaConfig, fixed_vol, moving_vol: Path, outdir: Path) -> Path:
    elastix_stage_parameters = run_lama.generate_elx_parameters(config)
    stage_ids = []
    stage_spec_dirs = []
    for i, reg_stage in enumerate(config['registration_stage_params']):
        stage_id = reg_stage['stage_id']
        stage_ids.append(stage_id)
        stage_dir = outdir / stage_id
        stage_dir.mkdir(exist_ok=True)
        logging.info("### Reverse registration - current registration step: {} ###".format(stage_id))
        elxparam = elastix_stage_parameters[stage_id]
        elxparam_path = join(stage_dir, ELX_PARAM_PREFIX + stage_id + '.txt')
        with open(elxparam_path, 'w') as fh:
            fh.write(elxparam)
        fixed_mask = None
        registrator = TargetBasedRegistration(elxparam_path,
                                 moving_vol,
                                 stage_dir,
                                 config['filetype'],
                                 config['threads'],
                                 fixed_mask
                                 )
        registrator.set_target(fixed_vol)
        registrator.fix_folding = False
        registrator.run()   for a single stage
        os.remove(elxparam_path)
        stage_spec_dir = next(stage_dir.glob(f'*{moving_vol.stem}'))
        new_stage_spec_dir = stage_dir / fixed_vol.stem
        stage_spec_dir.rename(new_stage_spec_dir)
        stage_spec_dirs.append(new_stage_spec_dir)
        moving_vol = new_stage_spec_dir / moving_vol.name
        src_tform_file = stage_dir / fixed_vol.stem / ELX_TRANSFORM_NAME
        label_tform_file = stage_dir / fixed_vol.stem / PROPAGATE_LABEL_TRANFORM
        image_tform_file = stage_dir / fixed_vol.stem / PROPAGATE_IMAGE_TRANSFORM
        modify_elx_parameter_file(src_tform_file, label_tform_file, LABEL_REPLACEMENTS)
        modify_elx_parameter_file(src_tform_file, image_tform_file, IMAGE_REPLACEMENTS)
    logging.info("### Reverse registration finished ###")
    to_keep = [PROPAGATE_LABEL_TRANFORM, 'elastix.log', PROPAGATE_IMAGE_TRANSFORM]
    for s in stage_spec_dirs:
        for f in s.iterdir():
            if f.name not in to_keep:
                try:
                    shutil.rmtree(f)
                except NotADirectoryError:
                    f.unlink()
    d = {'label_propagation_order': stage_ids}
    with open(outdir / PROPAGATE_CONFIG, 'w') as fh:
        yaml.dump(d, fh)
def run_registration_schedule ( config : LamaConfig , fixed_vol , moving_vol : Path , outdir : Path ) -> Path : elastix_stage_parameters = run_lama . generate_elx_parameters ( config ) stage_ids = [ ] stage_spec_dirs = [ ] for i , reg_stage in enumerate ( config [ 'registration_stage_params' ] ) : stage_id = reg_stage [ 'stage_id' ] stage_ids . append ( stage_id ) stage_dir = outdir / stage_id stage_dir . mkdir ( exist_ok = True ) logging . info ( "### Reverse registration - current registration step: {} ###" . format ( stage_id ) ) elxparam = elastix_stage_parameters [ stage_id ] elxparam_path = join ( stage_dir , ELX_PARAM_PREFIX + stage_id + '.txt' ) with open ( elxparam_path , 'w' ) as fh : fh . write ( elxparam ) fixed_mask = None registrator = TargetBasedRegistration ( elxparam_path , moving_vol , stage_dir , config [ 'filetype' ] , config [ 'threads' ] , fixed_mask ) registrator . set_target ( fixed_vol ) registrator . fix_folding = False registrator . run ( ) os . remove ( elxparam_path ) stage_spec_dir = next ( stage_dir . glob ( f'*{moving_vol.stem}' ) ) new_stage_spec_dir = stage_dir / fixed_vol . stem stage_spec_dir . rename ( new_stage_spec_dir ) stage_spec_dirs . append ( new_stage_spec_dir ) moving_vol = new_stage_spec_dir / moving_vol . name src_tform_file = stage_dir / fixed_vol . stem / ELX_TRANSFORM_NAME label_tform_file = stage_dir / fixed_vol . stem / PROPAGATE_LABEL_TRANFORM image_tform_file = stage_dir / fixed_vol . stem / PROPAGATE_IMAGE_TRANSFORM modify_elx_parameter_file ( src_tform_file , label_tform_file , LABEL_REPLACEMENTS ) modify_elx_parameter_file ( src_tform_file , image_tform_file , IMAGE_REPLACEMENTS ) logging . info ( "### Reverse registration finished ###" ) to_keep = [ PROPAGATE_LABEL_TRANFORM , 'elastix.log' , PROPAGATE_IMAGE_TRANSFORM ] for s in stage_spec_dirs : for f in s . iterdir ( ) : if f . name not in to_keep : try : shutil . rmtree ( f ) except NotADirectoryError : f . unlink ( ) d = { 'label_propagation_order' : stage_ids } with open ( outdir / PROPAGATE_CONFIG , 'w' ) as fh : yaml . dump ( d , fh )
Run the registrations specified in the config file, but flip the moving and fixed images
Returns
Run the registrations specified in the config file but flip the moving and fixed images Returns
"""
    Run the registrations specified in the config file, but flip the moving and fixed images

    Returns
    -------
    The path to the final registrered images
    """ # egp = {'WriteResultImage': 'false',   # We only need the tform files not the images #        'WriteResultImageAfterEachResolution': 'true'} # Set the moving volume dir and the fixed image for the first stage #  Set the fixed volume up for the first stage. This will checnge each stage if doing population average #  Make the stage output dir # Make the elastix parameter file for this stage # maybe we should add fixed mask # Do the registrations # issues/133: switch off fix folding for label propagation until fixed # if reg_stage['elastix_parameters']['Transform'] == 'BSplineTransform': #     if config['fix_folding']: #         logging.info(f'Folding correction for stage {stage_id} set') #         registrator.fix_folding = True # Do the registrations for a single stage # As the stage output diretory is named as the moving image, but in this case we want it named the same as the # fixed image # move_intemediate_volumes(stage_dir) # Now delete everything we don't need
config
LamaConfig
fixed_vol
moving_vol
Path
outdir
Path