phenotypic.post package#
Post-measurement DataFrame transforms for measurement pipelines.
Provides operations that reshape or enrich measurement DataFrames after feature extraction. These run as the final stage of ImagePipeline.measure().
- class phenotypic.post.AppendString(column: str = '', value: str = '')[source]#
Bases:
PostMeasurementAppend a string to every value in a metadata column.
Converts each cell to a string and concatenates the given value to the end. Useful for adding suffixes like units or experimental labels.
- Parameters:
- Returns:
A copy of the input DataFrame with the modified column.
- Return type:
pd.DataFrame
- Raises:
KeyError – If the column does not exist in the DataFrame.
Examples
Append a unit suffix to a temperature column:
>>> import pandas as pd >>> from phenotypic.post import AppendString >>> df = pd.DataFrame({ ... "Metadata_Temp": ["30", "37"], ... "ObjectLabel": [1, 2], ... }) >>> op = AppendString(column="Temp", value="C") >>> result = op.apply(df) >>> list(result["Metadata_Temp"]) ['30C', '37C']
- __del__()#
Automatically stop tracemalloc when the object is deleted.
- apply(df: pandas.DataFrame) pandas.DataFrame#
Apply the post-measurement transform.
- Parameters:
df (pandas.DataFrame) – The merged measurement DataFrame.
- Returns:
The transformed DataFrame.
- Return type:
- class phenotypic.post.ExpandMetadata(column: str = '', labels: List[str] | None = None, delimiter: str = '_', regex: bool = False)[source]#
Bases:
PostMeasurementSplit a metadata column into multiple new metadata columns.
Takes a single delimited metadata column (e.g., a filename encoding experimental conditions) and expands it into separate labeled columns. Every row must produce exactly len(labels) parts or a ValueError is raised.
- Parameters:
column (str) – Name of the metadata column to split.
Metadata_prefix is added automatically if missing.labels (List[str] | None) – Names for the resulting columns, one per split part.
Metadata_prefix is added automatically if missing.delimiter (str) – String or regex pattern to split on. Defaults to
"_".regex (bool) – If True, treat delimiter as a regex pattern. Defaults to False.
- Returns:
- The input DataFrame with new columns inserted
adjacent to the source column. The source column is always kept.
- Return type:
pd.DataFrame
- Raises:
ValueError – If labels is empty, or if any row produces a different number of parts than len(labels).
KeyError – If the source column does not exist in the DataFrame.
Examples
Split a filename into experimental conditions:
>>> import pandas as pd >>> from phenotypic.post import ExpandMetadata >>> df = pd.DataFrame({ ... "Metadata_ImageName": ["WT_30C_24h", "mut_37C_48h"], ... "ObjectLabel": [1, 2], ... }) >>> expand = ExpandMetadata( ... column="ImageName", ... labels=["Strain", "Condition", "Time"], ... delimiter="_", ... ) >>> result = expand.apply(df) >>> list(result["Metadata_Strain"]) ['WT', 'mut']
- __del__()#
Automatically stop tracemalloc when the object is deleted.
- apply(df: pandas.DataFrame) pandas.DataFrame#
Apply the post-measurement transform.
- Parameters:
df (pandas.DataFrame) – The merged measurement DataFrame.
- Returns:
The transformed DataFrame.
- Return type:
- class phenotypic.post.MergeMetadata(columns: List[str] | None = None, label: str = '', delimiter: str = '_')[source]#
Bases:
PostMeasurementMerge multiple metadata columns into a single new metadata column.
Concatenates the string values of two or more metadata columns using a delimiter to produce a combined identifier. Useful for creating composite keys (e.g., combining Strain and Condition into SampleID).
- Parameters:
columns (List[str] | None) – Names of the metadata columns to merge.
Metadata_prefix is added automatically if missing. Must contain at least 2 names.label (str) – Name for the new merged column.
Metadata_prefix is added automatically if missing.delimiter (str) – String used to join the column values. Defaults to
"_".
- Returns:
- The input DataFrame with the new merged column
inserted after the last source column. All source columns are kept.
- Return type:
pd.DataFrame
- Raises:
ValueError – If columns contains fewer than 2 names.
KeyError – If any source column does not exist in the DataFrame.
Examples
Merge strain and condition into a sample ID:
>>> import pandas as pd >>> from phenotypic.post import MergeMetadata >>> df = pd.DataFrame({ ... "Metadata_Strain": ["WT", "mut"], ... "Metadata_Condition": ["30C", "37C"], ... "ObjectLabel": [1, 2], ... }) >>> merge = MergeMetadata( ... columns=["Strain", "Condition"], ... label="SampleID", ... delimiter="_", ... ) >>> result = merge.apply(df) >>> list(result["Metadata_SampleID"]) ['WT_30C', 'mut_37C']
- __del__()#
Automatically stop tracemalloc when the object is deleted.
- apply(df: pandas.DataFrame) pandas.DataFrame#
Apply the post-measurement transform.
- Parameters:
df (pandas.DataFrame) – The merged measurement DataFrame.
- Returns:
The transformed DataFrame.
- Return type:
- class phenotypic.post.PrependString(column: str = '', value: str = '')[source]#
Bases:
PostMeasurementPrepend a string to every value in a metadata column.
Converts each cell to a string and concatenates the given value to the beginning. Useful for adding prefixes like identifiers or labels.
- Parameters:
- Returns:
A copy of the input DataFrame with the modified column.
- Return type:
pd.DataFrame
- Raises:
KeyError – If the column does not exist in the DataFrame.
Examples
Prepend a strain prefix to an ID column:
>>> import pandas as pd >>> from phenotypic.post import PrependString >>> df = pd.DataFrame({ ... "Metadata_ID": ["001", "002"], ... "ObjectLabel": [1, 2], ... }) >>> op = PrependString(column="ID", value="WT-") >>> result = op.apply(df) >>> list(result["Metadata_ID"]) ['WT-001', 'WT-002']
- __del__()#
Automatically stop tracemalloc when the object is deleted.
- apply(df: pandas.DataFrame) pandas.DataFrame#
Apply the post-measurement transform.
- Parameters:
df (pandas.DataFrame) – The merged measurement DataFrame.
- Returns:
The transformed DataFrame.
- Return type: