lama/registration_pipeline/parallel_average.py
def job_runner(config_path: Path) -> Path:
    """
    Run the registrations specified in the config file

    Returns
    -------
    The path to the final registrered images
    """

    config = LamaConfig(config_path)
    print(common.git_log())


    avg_dir = config.options['average_folder']
    avg_dir.mkdir(exist_ok=True, parents=True)

    elastix_stage_parameters = generate_elx_parameters(config, do_pairwise=config['pairwise_registration'])

    # Set the fixed volume up for the first stage. This will checnge each stage if doing population average
    fixed_vol = config['fixed_volume']

    # Get list of specimens
    inputs_dir = config.options['inputs']
    spec_ids = [Path(x).stem for x in common.get_file_paths(inputs_dir)]

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

        stage_id = reg_stage['stage_id']
        logging.info(stage_id)
        stage_dir = Path(config.stage_dirs[stage_id])

        # Make stage dir if not made by another instance of the script
        stage_dir.mkdir(exist_ok=True, parents=True)

        starting_avg = stage_dir / 'avg_started'
        average_done = stage_dir / "avg_done"

        while True:  # Pick up unstarted speciemens. Only break when reg and avergae complete

            # Check if any specimens left (It's possible the avg is being made but all specimens are registered)
            spec_stage_dirs = [x.name for x in stage_dir.iterdir() if x.is_dir()]
            not_started = set(spec_ids).difference(spec_stage_dirs)

            next_stage = False  # No breaking out yet

            if len(not_started) > 0:
                next_spec_id = list(not_started)[0] # Some specimens left. Pick up spec_id and process

            else:  # All specimens are being processed
                next_stage = True

                #  This block controls what happens if we have all speciemns registered
                while True:
                    if not check_stage_done(stage_dir):
                        print('waiting for stage to finish')
                        time.sleep(5)
                        continue

                    print('stage finished')

                    if average_done.is_file():
                        print('found average done file')
                        break  # Next stage
                    else:
                        if starting_avg.is_file():
                            print('found starting average file')
                            time.sleep(5)
                            continue
                        else:
                            try:
                                open(starting_avg, 'x')
                            except FileExistsError:
                                time.sleep(5)
                                print('cannot write avg starting file')
                                continue
                            else:
                                average_path = avg_dir / f'{stage_id}.nrrd'
                                make_avg(stage_dir, average_path, avg_dir / f'{stage_id}.log')
                                open(average_done, 'x').close()
                                print('making average')
                                break

            if next_stage:
                print('breaking stage')
                break

            # Get the input for this specimen
            if i == 0:  # The first stage
                moving = inputs_dir / f'{next_spec_id}.nrrd'
            else:
                moving = list(config.stage_dirs.values())[i-1] / next_spec_id / f'{next_spec_id}.nrrd'
                fixed_vol = avg_dir / f'{list(config.stage_dirs.keys())[i-1]}.nrrd'
            reg_method = TargetBasedRegistration

            # Make the elastix parameter file for this stage
            elxparam = elastix_stage_parameters[stage_id]
            elxparam_path = stage_dir / f'{ELX_PARAM_PREFIX}{stage_id}.txt'

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

            fixed_mask = None

            logging.info(moving)

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

            registrator.set_target(fixed_vol)

            try:
                registrator.run()  # Do the registrations for a single stage
            except FileExistsError as e:
                # 040620: Bodge as some specimens are picked up twice.
                # Need a better way to make sure each speciemn picked up only once
                continue

            spec_done = stage_dir / next_spec_id / 'spec_done'  # The directory gets created in .run()
            open(spec_done, 'x').close()

    Run the registrations specified in the config file

    Returns
    -------
    The path to the final registrered images
    
Run the registrations specified in the config file
Returns

The path to the final registrered images
Run the registrations specified in the config file Returns The path to the final registrered images
def job_runner(config_path: Path) -> Path:
    config = LamaConfig(config_path)
    print(common.git_log())
    avg_dir = config.options['average_folder']
    avg_dir.mkdir(exist_ok=True, parents=True)
    elastix_stage_parameters = generate_elx_parameters(config, do_pairwise=config['pairwise_registration'])
    fixed_vol = config['fixed_volume']
    inputs_dir = config.options['inputs']
    spec_ids = [Path(x).stem for x in common.get_file_paths(inputs_dir)]
    for i, reg_stage in enumerate(config['registration_stage_params']):
        stage_id = reg_stage['stage_id']
        logging.info(stage_id)
        stage_dir = Path(config.stage_dirs[stage_id])
        stage_dir.mkdir(exist_ok=True, parents=True)
        starting_avg = stage_dir / 'avg_started'
        average_done = stage_dir / "avg_done"
        while True:  
            spec_stage_dirs = [x.name for x in stage_dir.iterdir() if x.is_dir()]
            not_started = set(spec_ids).difference(spec_stage_dirs)
            next_stage = False  
            if len(not_started) > 0:
                next_spec_id = list(not_started)[0] 
            else:  
                next_stage = True
                while True:
                    if not check_stage_done(stage_dir):
                        print('waiting for stage to finish')
                        time.sleep(5)
                        continue
                    print('stage finished')
                    if average_done.is_file():
                        print('found average done file')
                        break  
                    else:
                        if starting_avg.is_file():
                            print('found starting average file')
                            time.sleep(5)
                            continue
                        else:
                            try:
                                open(starting_avg, 'x')
                            except FileExistsError:
                                time.sleep(5)
                                print('cannot write avg starting file')
                                continue
                            else:
                                average_path = avg_dir / f'{stage_id}.nrrd'
                                make_avg(stage_dir, average_path, avg_dir / f'{stage_id}.log')
                                open(average_done, 'x').close()
                                print('making average')
                                break
            if next_stage:
                print('breaking stage')
                break
            if i == 0:  
                moving = inputs_dir / f'{next_spec_id}.nrrd'
            else:
                moving = list(config.stage_dirs.values())[i-1] / next_spec_id / f'{next_spec_id}.nrrd'
                fixed_vol = avg_dir / f'{list(config.stage_dirs.keys())[i-1]}.nrrd'
            reg_method = TargetBasedRegistration
            elxparam = elastix_stage_parameters[stage_id]
            elxparam_path = stage_dir / f'{ELX_PARAM_PREFIX}{stage_id}.txt'
            if not elxparam_path.is_file():
                with open(elxparam_path, 'w') as fh:
                    if elxparam:
                        fh.write(elxparam)
            fixed_mask = None
            logging.info(moving)
            registrator = reg_method(elxparam_path,
                                     moving,
                                     stage_dir,
                                     config['filetype'],
                                     config['threads'],
                                     fixed_mask
                                     )
            registrator.set_target(fixed_vol)
            try:
                registrator.run()   for a single stage
            except FileExistsError as e:
                continue
            spec_done = stage_dir / next_spec_id / 'spec_done'  
            open(spec_done, 'x').close()
def job_runner ( config_path : Path ) -> Path : config = LamaConfig ( config_path ) print ( common . git_log ( ) ) avg_dir = config . options [ 'average_folder' ] avg_dir . mkdir ( exist_ok = True , parents = True ) elastix_stage_parameters = generate_elx_parameters ( config , do_pairwise = config [ 'pairwise_registration' ] ) fixed_vol = config [ 'fixed_volume' ] inputs_dir = config . options [ 'inputs' ] spec_ids = [ Path ( x ) . stem for x in common . get_file_paths ( inputs_dir ) ] for i , reg_stage in enumerate ( config [ 'registration_stage_params' ] ) : stage_id = reg_stage [ 'stage_id' ] logging . info ( stage_id ) stage_dir = Path ( config . stage_dirs [ stage_id ] ) stage_dir . mkdir ( exist_ok = True , parents = True ) starting_avg = stage_dir / 'avg_started' average_done = stage_dir / "avg_done" while True : spec_stage_dirs = [ x . name for x in stage_dir . iterdir ( ) if x . is_dir ( ) ] not_started = set ( spec_ids ) . difference ( spec_stage_dirs ) next_stage = False if len ( not_started ) > 0 : next_spec_id = list ( not_started ) [ 0 ] else : next_stage = True while True : if not check_stage_done ( stage_dir ) : print ( 'waiting for stage to finish' ) time . sleep ( 5 ) continue print ( 'stage finished' ) if average_done . is_file ( ) : print ( 'found average done file' ) break else : if starting_avg . is_file ( ) : print ( 'found starting average file' ) time . sleep ( 5 ) continue else : try : open ( starting_avg , 'x' ) except FileExistsError : time . sleep ( 5 ) print ( 'cannot write avg starting file' ) continue else : average_path = avg_dir / f'{stage_id}.nrrd' make_avg ( stage_dir , average_path , avg_dir / f'{stage_id}.log' ) open ( average_done , 'x' ) . close ( ) print ( 'making average' ) break if next_stage : print ( 'breaking stage' ) break if i == 0 : moving = inputs_dir / f'{next_spec_id}.nrrd' else : moving = list ( config . stage_dirs . values ( ) ) [ i - 1 ] / next_spec_id / f'{next_spec_id}.nrrd' fixed_vol = avg_dir / f'{list(config.stage_dirs.keys())[i-1]}.nrrd' reg_method = TargetBasedRegistration elxparam = elastix_stage_parameters [ stage_id ] elxparam_path = stage_dir / f'{ELX_PARAM_PREFIX}{stage_id}.txt' if not elxparam_path . is_file ( ) : with open ( elxparam_path , 'w' ) as fh : if elxparam : fh . write ( elxparam ) fixed_mask = None logging . info ( moving ) registrator = reg_method ( elxparam_path , moving , stage_dir , config [ 'filetype' ] , config [ 'threads' ] , fixed_mask ) registrator . set_target ( fixed_vol ) try : registrator . run ( ) except FileExistsError as e : continue spec_done = stage_dir / next_spec_id / 'spec_done' open ( spec_done , 'x' ) . close ( )
Run the registrations specified in the config file
Returns
Run the registrations specified in the config file Returns
"""
    Run the registrations specified in the config file

    Returns
    -------
    The path to the final registrered images
    """ # Set the fixed volume up for the first stage. This will checnge each stage if doing population average # Get list of specimens # Make stage dir if not made by another instance of the script # Pick up unstarted speciemens. Only break when reg and avergae complete # Check if any specimens left (It's possible the avg is being made but all specimens are registered) # No breaking out yet # Some specimens left. Pick up spec_id and process # All specimens are being processed #  This block controls what happens if we have all speciemns registered # Next stage # Get the input for this specimen # The first stage # Make the elastix parameter file for this stage # Do the registrations # Do the registrations for a single stage # 040620: Bodge as some specimens are picked up twice. # Need a better way to make sure each speciemn picked up only once # The directory gets created in .run()
config_path
Path