Skip to content

find_registration_consensus

Applies the multiplexing translation to all ROI tables

find_registration_consensus(*, zarr_url, init_args, roi_table='FOV_ROI_table', new_roi_table=None)

Applies pre-calculated registration to ROI tables.

Apply pre-calculated registration such that resulting ROIs contain the consensus align region between all acquisitions.

Parallelization level: well

PARAMETER DESCRIPTION
zarr_url

Path or url to the individual OME-Zarr image to be processed. Refers to the zarr_url of the reference acquisition. (standard argument for Fractal tasks, managed by Fractal server).

TYPE: str

init_args

Intialization arguments provided by init_group_by_well_for_multiplexing. It contains the zarr_url_list listing all the zarr_urls in the same well as the zarr_url of the reference acquisition that are being processed. (standard argument for Fractal tasks, managed by Fractal server).

TYPE: InitArgsRegistrationConsensus

roi_table

Name of the ROI table over which the task loops to calculate the registration. Examples: FOV_ROI_table => loop over the field of views, well_ROI_table => process the whole well as one image.

TYPE: str DEFAULT: 'FOV_ROI_table'

new_roi_table

Optional name for the new, registered ROI table. If no name is given, it will default to "registered_" + roi_table

TYPE: Optional[str] DEFAULT: None

Source code in fractal_tasks_core/tasks/find_registration_consensus.py
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
@validate_call
def find_registration_consensus(
    *,
    # Fractal parameters
    zarr_url: str,
    init_args: InitArgsRegistrationConsensus,
    # Core parameters
    roi_table: str = "FOV_ROI_table",
    # Advanced parameters
    new_roi_table: Optional[str] = None,
):
    """
    Applies pre-calculated registration to ROI tables.

    Apply pre-calculated registration such that resulting ROIs contain
    the consensus align region between all acquisitions.

    Parallelization level: well

    Args:
        zarr_url: Path or url to the individual OME-Zarr image to be processed.
            Refers to the zarr_url of the reference acquisition.
            (standard argument for Fractal tasks, managed by Fractal server).
        init_args: Intialization arguments provided by
            `init_group_by_well_for_multiplexing`. It contains the
            zarr_url_list listing all the zarr_urls in the same well as the
            zarr_url of the reference acquisition that are being processed.
            (standard argument for Fractal tasks, managed by Fractal server).
        roi_table: Name of the ROI table over which the task loops to
            calculate the registration. Examples: `FOV_ROI_table` => loop over
            the field of views, `well_ROI_table` => process the whole well as
            one image.
        new_roi_table: Optional name for the new, registered ROI table. If no
            name is given, it will default to "registered_" + `roi_table`

    """
    if not new_roi_table:
        new_roi_table = "registered_" + roi_table
    logger.info(
        f"Running for {zarr_url=} & the other acquisitions in that well. \n"
        f"Applying translation registration to {roi_table=} and storing it as "
        f"{new_roi_table=}."
    )

    # Collect all the ROI tables
    roi_tables = {}
    roi_tables_attrs = {}
    for acq_zarr_url in init_args.zarr_url_list:
        curr_ROI_table = ad.read_zarr(f"{acq_zarr_url}/tables/{roi_table}")
        curr_ROI_table_group = zarr.open_group(
            f"{acq_zarr_url}/tables/{roi_table}", mode="r"
        )
        curr_ROI_table_attrs = curr_ROI_table_group.attrs.asdict()

        # For reference_acquisition, handle the fact that it doesn't
        # have the shifts
        if acq_zarr_url == zarr_url:
            curr_ROI_table = add_zero_translation_columns(curr_ROI_table)
        # Check for valid ROI tables
        are_ROI_table_columns_valid(table=curr_ROI_table)
        translation_columns = [
            "translation_z",
            "translation_y",
            "translation_x",
        ]
        if curr_ROI_table.var.index.isin(translation_columns).sum() != 3:
            raise ValueError(
                f"{roi_table=} in {acq_zarr_url} does not contain the "
                f"translation columns {translation_columns} necessary to use "
                "this task."
            )
        roi_tables[acq_zarr_url] = curr_ROI_table
        roi_tables_attrs[acq_zarr_url] = curr_ROI_table_attrs

    # Check that all acquisitions have the same ROIs
    rois = roi_tables[list(roi_tables.keys())[0]].obs.index
    for acq_zarr_url, acq_roi_table in roi_tables.items():
        if not (acq_roi_table.obs.index == rois).all():
            raise ValueError(
                f"Acquisition {acq_zarr_url} does not contain the same ROIs "
                f"as the reference acquisition {zarr_url}:\n"
                f"{acq_zarr_url}: {acq_roi_table.obs.index}\n"
                f"{zarr_url}: {rois}"
            )

    roi_table_dfs = [
        roi_table.to_df().loc[:, translation_columns]
        for roi_table in roi_tables.values()
    ]
    logger.info("Calculating min & max translation across acquisitions.")
    max_df, min_df = calculate_min_max_across_dfs(roi_table_dfs)
    shifted_rois = {}

    # Loop over acquisitions
    for acq_zarr_url in init_args.zarr_url_list:
        shifted_rois[acq_zarr_url] = apply_registration_to_single_ROI_table(
            roi_tables[acq_zarr_url], max_df, min_df
        )

        # TODO: Drop translation columns from this table?

        logger.info(
            f"Write the registered ROI table {new_roi_table} for "
            "{acq_zarr_url=}"
        )
        # Save the shifted ROI table as a new table
        image_group = zarr.group(acq_zarr_url)
        write_table(
            image_group,
            new_roi_table,
            shifted_rois[acq_zarr_url],
            table_attrs=roi_tables_attrs[acq_zarr_url],
            overwrite=True,
        )