spatialdata.concatenate

Contents

spatialdata.concatenate#

spatialdata.concatenate(sdatas, region_key=None, instance_key=None, concatenate_tables=False, obs_names_make_unique=True, modify_tables_inplace=False, **kwargs)#

Concatenate a list of spatial data objects.

Parameters:
  • sdatas (Iterable[SpatialData] | dict[str, SpatialData]) – The spatial data objects to concatenate. The names of the elements across the SpatialData objects must be unique. If they are not unique, you can pass a dictionary with the suffixes as keys and the spatial data objects as values. This will rename the names of each SpatialElement to ensure uniqueness of names across SpatialData objects. See more on the notes.

  • region_key (Optional[str] (default: None)) – The key to use for the region column in the concatenated object. If None and all region_keys are the same, the region_key is used.

  • instance_key (Optional[str] (default: None)) – The key to use for the instance column in the concatenated object. If None and all instance_keys are the same, the instance_key is used.

  • concatenate_tables (bool (default: False)) – Whether to merge the tables in case of having the same element name.

  • obs_names_make_unique (bool (default: True)) – Whether to make the obs_names unique by calling AnnData.obs_names_make_unique() on each table of the concatenated object. If you passed a dictionary with the suffixes as keys and the SpatialData objects as values and if concatenate_tables is True, the obs_names will be made unique by adding the corresponding suffix instead.

  • modify_tables_inplace (bool (default: False)) – Whether to modify the tables in place. If True, the tables will be modified in place. If False, the tables will be copied before modification. Copying is enabled by default but can be disabled for performance reasons.

  • kwargs (Any) – See anndata.concat() for more details.

Return type:

SpatialData

Returns:

: The concatenated spatialdata.SpatialData object.

Notes

If you pass a dictionary with the suffixes as keys and the SpatialData objects as values, the names of each SpatialElement will be renamed to ensure uniqueness of names across SpatialData objects by adding the corresponding suffix. To ensure the matching between existing table annotations, the region metadata of each table, and the values of the region_key column in each table, will be altered by adding the suffix. In addition, the obs_names of each table will be altered (a suffix will be added). Finally, a suffix will be added to the name of each table iff rename_tables is False.

If you need more control in the renaming, please give us feedback, as we are still trying to find the right balance between ergonomics and control. Also, you are welcome to copy and adjust the code of _fix_ensure_unique_element_names() directly.