piaso.preprocessing package#

piaso.preprocessing.getCrossCategories(df, col1, col2, delimiter='@', iterate_by_second_column=True)#

Generates a new categorical column from the cross combinations of two specified columns in a DataFrame, respecting existing categorical orders if present. The iteration order of combination can be controlled, and a custom delimiter can be used to join the column values.

Parameters:
  • df (pd.DataFrame) – The DataFrame containing the columns to be combined.

  • col1 (str) – Name of the first column to combine.

  • col2 (str) – Name of the second column to combine.

  • delimiter (str, optional) – Delimiter used to join the column values. Defaults to ‘@’.

  • iterate_by_second_column (bool, optional) – If set to True, the function iterates by the values of the second column first when generating the combined categories. Defaults to True.

Returns:

A Pandas Categorical series of the combined columns with a defined order.

Return type:

pd.Categorical

piaso.preprocessing.table(values, rank: bool = False, ascending: bool = False, as_dataframe: bool = False)#

Returns the counts of unique values in the given list.

Parameters:
  • values (list) – A list of values for which the counts are to be calculated.

  • rank (bool, optional) – If True, the results are sorted by count. Default is False.

  • ascending (bool, optional) – If True and rank is True, the results are sorted in ascending order. If False and rank is True, the results are sorted in descending order. Default is False.

  • as_dataframe (bool, optional) – If True, the result is returned as a pandas DataFrame with columns ‘value’ and ‘count’. If False, the result is returned as a dictionary. Default is False.

Returns:

A dictionary (or DataFrame, if as_dataframe is True) containing the counts of unique values. If rank is True, the dictionary is sorted by count.

Return type:

dict or pandas.DataFrame

piaso.preprocessing.rotateSpatialCoordinates(adata: AnnData, angle_degrees: float, spatial_key: str = 'X_spatial', clockwise: bool = False, inplace: bool = True, backup_spatial_key: str | None = None) AnnData | None#

Rotates the spatial coordinates in an AnnData object around their center.

This function performs a 2D rotation on the coordinates stored in adata.obsm[spatial_key]. It first calculates the centroid of the coordinates, translates the data to center it at the origin, performs the rotation, and then translates it back.

Parameters:
  • adata – The annotated data matrix of shape (n_obs, n_vars).

  • angle_degrees – The angle of rotation in degrees.

  • spatial_key – The key in adata.obsm where the spatial coordinates are stored. Defaults to ‘X_spatial’.

  • clockwise – If True, performs a clockwise rotation. If False (default), performs a counter-clockwise rotation (standard mathematical convention).

  • inplace – If True (default), modifies the input AnnData object in place and returns None. If False, returns a new AnnData object with rotated coordinates.

  • backup_spatial_key – If specified, the original spatial coordinates will be backed up in adata.obsm[backup_spatial_key] before rotation. If None (default), no backup is created.

Returns:

If inplace=True, returns None and modifies the input adata object. If inplace=False, returns a new AnnData object with the rotated spatial coordinates.

Raises:
  • KeyError – If spatial_key is not found in adata.obsm.

  • ValueError – If the coordinates in adata.obsm[spatial_key] are not 2D or 3D.

Example

# Rotate coordinates in place with backup piaso.pp.rotateSpatialCoordinates(adata, 45, backup_spatial_key=’X_spatial_original’)

# Rotate and create new object without backup adata_rotated = piaso.pp.rotateSpatialCoordinates(adata, 90, inplace=False)