coSMicQC in a nutshellΒΆ
This notebook demonstrates various capabilities of coSMicQC using examples.
import pathlib
import pandas as pd
from cytodataframe import CytoDataFrame
import cosmicqc
from importlib.metadata import version
version("cytodataframe")
'0.3.0'
# set a path for the parquet-based dataset
# (in this case, CellProfiler SQLite data processed by CytoTable)
data_path = (
"../../../tests/data/cytotable/NF1_cellpainting_data/"
"Plate_2_with_image_data.parquet"
)
# set a context directory for images associated with the dataset
image_context_dir = pathlib.Path(data_path).parent / "Plate_2_images"
mask_context_dir = pathlib.Path(data_path).parent / "Plate_2_masks"
# create a cosmicqc CytoDataFrame (single-cell DataFrame)
scdf = CytoDataFrame(
data=data_path,
data_context_dir=image_context_dir,
data_mask_context_dir=mask_context_dir,
)
# display the dataframe
scdf
Static snapshot (for non-interactive view)
| Metadata_ImageNumber | Image_Metadata_Plate_x | Metadata_number_of_singlecells | Image_Metadata_Site_x | Image_Metadata_Well_x | Metadata_Cells_Number_Object_Number | Metadata_Cytoplasm_Parent_Cells | Metadata_Cytoplasm_Parent_Nuclei | Metadata_Nuclei_Number_Object_Number | Cytoplasm_AreaShape_Area | ... | Image_Threshold_SumOfEntropies_Cells | Image_Threshold_SumOfEntropies_Nuclei | Image_Threshold_WeightedVariance_Cells | Image_Threshold_WeightedVariance_Nuclei | Image_URL_DAPI | Image_URL_GFP | Image_URL_RFP | Image_Width_DAPI | Image_Width_GFP | Image_Width_RFP | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | Plate_2 | 44 | 1 | A12 | 1 | 1 | 2 | 2 | 21024.0 | ... | -12.181288 | -11.699993 | 0.992624 | 0.657791 | 1224 | 1224 | 1224 | |||
| 1 | 1 | Plate_2 | 44 | 1 | A12 | 4 | 4 | 7 | 7 | 12754.0 | ... | -12.181288 | -11.699993 | 0.992624 | 0.657791 | 1224 | 1224 | 1224 | |||
| 2 | 1 | Plate_2 | 44 | 1 | A12 | 7 | 7 | 10 | 10 | 23976.0 | ... | -12.181288 | -11.699993 | 0.992624 | 0.657791 | 1224 | 1224 | 1224 | |||
| 3 | 1 | Plate_2 | 44 | 1 | A12 | 8 | 8 | 12 | 12 | 19374.0 | ... | -12.181288 | -11.699993 | 0.992624 | 0.657791 | 1224 | 1224 | 1224 | |||
| 4 | 1 | Plate_2 | 44 | 1 | A12 | 9 | 9 | 13 | 13 | 27385.0 | ... | -12.181288 | -11.699993 | 0.992624 | 0.657791 | 1224 | 1224 | 1224 | |||
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1709 | 128 | Plate_2 | 59 | 4 | H7 | 10 | 10 | 14 | 14 | 24942.0 | ... | -12.566582 | -11.633043 | 1.624310 | 0.545186 | 1224 | 1224 | 1224 | |||
| 1710 | 128 | Plate_2 | 59 | 4 | H7 | 11 | 11 | 15 | 15 | 6627.0 | ... | -12.566582 | -11.633043 | 1.624310 | 0.545186 | 1224 | 1224 | 1224 | |||
| 1711 | 128 | Plate_2 | 59 | 4 | H7 | 12 | 12 | 16 | 16 | 11216.0 | ... | -12.566582 | -11.633043 | 1.624310 | 0.545186 | 1224 | 1224 | 1224 | |||
| 1712 | 128 | Plate_2 | 59 | 4 | H7 | 13 | 13 | 17 | 17 | 15279.0 | ... | -12.566582 | -11.633043 | 1.624310 | 0.545186 | 1224 | 1224 | 1224 | |||
| 1713 | 128 | Plate_2 | 59 | 4 | H7 | 14 | 14 | 20 | 20 | 7106.0 | ... | -12.566582 | -11.633043 | 1.624310 | 0.545186 | 1224 | 1224 | 1224 |
1714 rows Γ 2076 columns
bbox_cols = [
col for col in scdf.columns if "bbox" in col.lower() or "box" in col.lower()
]
print("bbox_col:", bbox_cols)
print("bbox_cols:")
for col in bbox_cols:
print(col)
# Identify which rows include outliers for a given threshold definition
# which references a column name and a z-score number which is considered
# the limit.
cosmicqc.analyze.identify_outliers(
df=scdf,
feature_thresholds={"Nuclei_AreaShape_Area": -1},
).sort_values()
# Show the number of outliers given a column name and a specified threshold
# via the `feature_thresholds` parameter and the `find_outliers` function.
cosmicqc.analyze.find_outliers(
df=scdf,
metadata_columns=["Metadata_ImageNumber", "Image_Metadata_Plate_x"],
feature_thresholds={"Nuclei_AreaShape_Area": -1},
)
# create a labeled dataset which includes z-scores and whether those scores
# are interpreted as outliers or inliers. We use pre-defined threshold sets
# loaded from defaults (cosmicqc can accept user-defined thresholds too!).
labeled_scdf = cosmicqc.analyze.label_outliers(
df=scdf, include_threshold_scores=True, feature_thresholds="large_nuclei"
)
labeled_scdf
# show cropped images through CytoDataFrame from the dataset to help analyze outliers
# labeled_scdf._enbable_debug_mode()
labeled_scdf.sort_values(by="Metadata_cqc_large_nuclei_is_outlier", ascending=False)[
[
"Metadata_ImageNumber",
"Metadata_Cells_Number_Object_Number",
"Metadata_cqc_large_nuclei_is_outlier",
"Image_FileName_GFP",
"Image_FileName_RFP",
"Image_FileName_DAPI",
]
]
# One can convert from cosmicqc.CytoDataFrame to pd.DataFrame's
# (when or if needed!)
df = pd.DataFrame(scdf)
print(type(df))
df