Cyto utilities¶
Functions enabling smooth interaction with CellProfiler and DeepProfiler output formats.
A variety of utility functions for working with cytominer data.
pycytominer.cyto_utils.DeepProfiler_processing
¶
Utility function to load and process the output files of a DeepProfiler run.
AggregateDeepProfiler
¶
Class that holds all functions needed to aggregate the DeepProfiler (DP) run.
Attributes:
| Name | Type | Description |
|---|---|---|
deep_data |
DeepProfilerData
|
DeepProfilerData object to load data from DeepProfiler project |
aggregated_profiles |
DataFrame
|
df to hold the metadata and profiles. |
file_aggregate |
dict
|
dict that holds the file names and metadata. Is used to load in the npz files in the correct order and grouping. |
output_file |
str
|
If provided, will write annotated profiles to folder. Defaults to None. |
Methods:
| Name | Description |
|---|---|
aggregate_deep |
Given an initialized AggregateDeepProfiler() class, run this function to output level 3 profiles (aggregated profiles with annotated metadata). |
Example
import pathlib from pycytominer.cyto_utils import DeepProfiler_processing
index_file = pathlib.Path("path/to/index.csv") profile_dir = pathlib.Path("path/to/features/")
deep_data = DeepProfiler_processing.DeepProfilerData(index_file, profile_dir, filename_delimiter="/", file_extension=".npz") deep_aggregate = DeepProfiler_processing.AggregateDeepProfiler(deep_data) deep_aggregate = aggregate.aggregate_deep()
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
__init__(deep_data, aggregate_operation='median', aggregate_on='well', output_file=None)
¶
init function for this class.
Arguments
See above for all parameters.
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
aggregate_deep()
¶
Aggregate the DeepProfiler profiles into a pandas dataframe.
For each key in file_aggregate, the profiles are loaded, concatenated and then aggregated. If files are missing, we throw a warning but continue the code. After aggregation, the metadata is concatenated back onto the dataframe.
Returns:
| Name | Type | Description |
|---|---|---|
df_out |
dataframe
|
dataframe with all metadata and the feature space. This is the input to any further pycytominer or pycytominer-eval processing |
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
setup_aggregate()
¶
Set up the file_aggregate attribute.
A helper function to aggregate_deep that aggregate the file_aggregate dictionary contains the file locations and metadata for each grouping.
If for example we are grouping by well then the keys of self.file_aggregate would be:
plate1/well1, plate1/well2, plate2/well1, etc.
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
DeepProfilerData
¶
Class that holds all functions needed to load and annotate the DeepProfiler (DP) run.
Attributes:
| Name | Type | Description |
|---|---|---|
profile_dir |
str
|
file location of the output profiles from DeepProfiler
(e.g. |
filename_delimiter |
default = '_'
|
delimiter for the filenames of the profiles (e.g. B02_4.npz). |
file_extension |
default = '.npz'
|
extension of the profile file. |
index_df |
DataFrame
|
load in the index.csv file from DeepProfiler, provided by an input index file. |
filenames |
list of paths
|
list of Purepaths that point to the npz files. |
Methods:
| Name | Description |
|---|---|
build_filenames |
build filenames from index_df |
extract_filename_metadata |
get site, well, plate info for npz file |
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 | |
__init__(index_file, profile_dir, filename_delimiter='_', file_extension='.npz')
¶
init function for this class.
Arguments
index_file : str file location of the index.csv from DP
See above for all other parameters.
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
build_filename_from_index(row)
¶
Build the name of the profile files.
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
75 76 77 78 79 80 81 82 | |
build_filenames()
¶
Create file names indicated by plate, well, and site information.
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
66 67 68 69 70 71 72 73 | |
extract_filename_metadata(npz_file, delimiter='_')
¶
Extract metadata (site, well and plate) from the filename.
This function is used to extract the metadata from the filename of the npz files. It expects a naming convetion of path/plate/well{delimiter}site.npz.
Arguments
npz_file : str file path
delimiter : str the delimiter used in the naming convention of the files. default = '_'
Returns:
| Name | Type | Description |
|---|---|---|
loc |
dict
|
dict with metadata |
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
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 | |
SingleCellDeepProfiler
¶
Class that holds functions needed to analyze single cells from the DeepProfiler (DP) run.
Only pycytominer.normalization() is implemented.
Attributes:
| Name | Type | Description |
|---|---|---|
deep_data |
DeepProfilerData
|
DeepProfilerData object to load data from DeepProfiler project |
aggregated_profiles |
DataFrame
|
df to hold the metadata and profiles. |
file_aggregate |
dict
|
dict that holds the file names and metadata. Is used to load in the npz files in the correct order and grouping. |
output_file |
str
|
If provided, will write annotated profiles to folder. Defaults to None. |
Methods:
| Name | Description |
|---|---|
normalize |
|
float_format, mad_robustize_epsilon, spherize_center, spherize_method, spherize_epsilon) |
normalize profiling features from DeepProfiler run with pycytominer.normalize() |
Example
import pathlib from pycytominer.cyto_utils import DeepProfiler_processing
index_file = pathlib.Path("path/to/index.csv") profile_dir = pathlib.Path("path/to/features/")
deep_data = DeepProfiler_processing.DeepProfilerData(index_file, profile_dir, filename_delimiter="/", file_extension=".npz") deep_single_cell = DeepProfiler_processing.SingleCellDeepProfiler(deep_data) normalized = deep_single_cell.normalize_deep_single_cells()
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
__init__(deep_data)
¶
init function for this class.
Arguments
See above for all parameters.
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
341 342 343 344 345 346 347 348 349 350 351 352 | |
get_single_cells(output=False, location_x_col_index=0, location_y_col_index=1)
¶
Set up a single_cells dataframe in the format expected by pycytominer.normalize().
Helper function to normalize_deep_single_cells() that sets up the single_cells attribute or outputs it as a dataframe.
Arguments
output : bool If true, will output the single cell dataframe instead of setting to self attribute location_x_col_index: int index of the x location column (which column in DP output has X coords) location_y_col_index: int index of the y location column (which column in DP output has Y coords)
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
normalize_deep_single_cells(location_x_col_index=0, location_y_col_index=1, image_features=False, meta_features='infer', samples='all', method='standardize', output_file=None, compression_options=None, float_format=None, mad_robustize_epsilon=1e-18, spherize_center=True, spherize_method='ZCA-cor', spherize_epsilon=1e-06)
¶
Normalize all cells into a pandas dataframe.
For each file in the DP project features folder, the features from each cell are loaded. These features are put into a profiles dataframe for use in pycytominer.normalize. A features list is also compiled for use in pycytominer.normalize.
Returns:
| Name | Type | Description |
|---|---|---|
df_out |
dataframe
|
dataframe with all metadata and the feature space. This is the input to any further pycytominer or pycytominer-eval processing |
Source code in pycytominer/cyto_utils/DeepProfiler_processing.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
pycytominer.cyto_utils.annotate_custom
¶
Functions to annotate data frames with custom options according to CMAP specifications.
annotate_cmap(annotated, annotate_join_on, cell_id='unknown', perturbation_mode='none')
¶
Annotates data frame with custom options according to CMAP specifications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotated |
DataFrame
|
DataFrame of profiles. |
required |
annotate_join_on |
str
|
Typically the well metadata, but how to join external data |
required |
cell_id |
str
|
provide a string to annotate cell id column |
"unknown"
|
perturbation_mode |
str
|
How to annotate CMAP specific data (options = ["chemical" , "genetic"]) |
"none"
|
Returns:
| Type | Description |
|---|---|
annotated
|
CMAP annotated data |
Source code in pycytominer/cyto_utils/annotate_custom.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 | |
cp_clean(profiles)
¶
Specifically clean certain column names derived from different CellProfiler versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profiles |
DataFrame
|
DataFrame of profiles. |
required |
Returns:
| Type | Description |
|---|---|
profiles
|
Renamed to standard metadata |
Source code in pycytominer/cyto_utils/annotate_custom.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
pycytominer.cyto_utils.cell_locations
¶
Utility function to augment a metadata file with X,Y locations of cells in each image.
CellLocation
¶
Class holding all the functions augment a metadata file with X,Y locations of cells in each image.
In the metadata file, which is either a CSV or a Parquet file,
- Each row is single multi-channel image
- Each image is indexed by multiple columns, e.g., Metadata_Plate, Metadata_Well,Metadata_Site
The single_cell SQLite file contains at least two tables
- Nuclei, which has the single-cell-level readouts, including location information
- Image, which has the image-level readouts, as well metadata to link to the metadata file
In the Nuclei table,
- Each row is a cell
- Each cell has at least 3 columns: Nuclei_Location_Center_X, Nuclei_Location_Center_Y, ImageNumber
In the Image table,
- Each row is an image
- Each image has at least the same columns as the images in the metadata file are indexed by, e.g., Metadata_Plate,Metadata_Well,Metadata_Site
The methods in this class do the following - Read the metadata file - Read the single_cell file - For each image in the metadata file, find the corresponding image in the single_cell file - For each cell in the corresponding image, find the X,Y location - Add the X,Y locations of all cells to the metadata file in the corresponding row, packed into a single column
Attributes:
| Name | Type | Description |
|---|---|---|
metadata_input |
str or Pandas DataFrame
|
Path to the input metadata file or a Pandas DataFrame |
single_cell_input |
str or Engine
|
Path to the single_cell file or a sqlalchemy.engine.Engine object |
augmented_metadata_output |
str
|
Path to the output file. If None, the metadata file is not saved to disk |
image_column |
default = 'ImageNumber'
|
Name of the column in the metadata file that links to the single_cell file, in combination with |
image_key |
default = ['Metadata_Plate', 'Metadata_Well', 'Metadata_Site']
|
Names of the columns in the metadata file that uniquely identify each image |
object_column |
default = 'ObjectNumber'
|
Name of the column in the single_cell file that identifies each cell |
cell_x_loc |
default = 'Nuclei_Location_Center_X'
|
Name of the column in the single_cell file that contains the X location of each cell |
cell_y_loc |
default = 'Nuclei_Location_Center_Y'
|
Name of the column in the single_cell file that contains the Y location of each cell |
table_column |
default = 'TableNumber'
|
Name of the column in the metadata file that links to the single_cell file, in combination with |
Methods:
| Name | Description |
|---|---|
add_cell_location |
Augment the metadata file and optionally save it to a file |
Source code in pycytominer/cyto_utils/cell_locations.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
add_cell_location()
¶
Add the X,Y locations of all cells to the metadata file in the corresponding row, packed into a single column.
Optionally, save the augmented metadata file as a Parquet file.
Returns:
| Type | Description |
|---|---|
Pandas DataFrame
|
Either a data frame or the path to a Parquet file with the X,Y locations of all cells packed into a single column |
Source code in pycytominer/cyto_utils/cell_locations.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
pycytominer.cyto_utils.cell_locations_cmd
¶
CLI for cell location calculations.
pycytominer.cyto_utils.cells
¶
Module containing the SingleCells class, which is used to interact with single cell morphological profiles.
SingleCells
¶
Class to interact with single cell morphological profiles including aggregation, normalization, and output.
Attributes:
| Name | Type | Description |
|---|---|---|
sql_file |
str
|
SQLite connection pointing to the single cell database. The string prefix must be "sqlite:///". |
strata |
list of str, default ["Metadata_Plate", "Metadata_Well"]
|
The columns to groupby and aggregate single cells. |
aggregation_operation |
str, default "median"
|
Operation to perform single cell aggregation. |
output_file |
str, default None
|
If specified, the location to write the file. |
compartments |
list of str, default ["cells", "cytoplasm", "nuclei"]
|
List of compartments to process. |
compartment_linking_cols |
dict, default noted below
|
Dictionary identifying how to merge columns across tables. |
merge_cols |
list of str, default ["TableNumber", "ImageNumber"]
|
Columns indicating how to merge image and compartment data. |
image_cols |
list of str, default ["TableNumber", "ImageNumber", "Metadata_Site"]
|
Columns to select from the image table. |
add_image_features |
bool, default False
|
Whether to add image features to the profiles. |
image_feature_categories |
list of str, optional
|
List of categories of features from the image table to add to the profiles. |
features |
str or list of str, default "infer"
|
List of features that should be loaded or aggregated. |
load_image_data |
bool, default True
|
Whether or not the image data should be loaded into memory. |
image_table_name |
str, default "image"
|
The name of the table inside the SQLite file of image measurements. |
subsample_frac |
float, default 1
|
The percentage of single cells to select (0 < subsample_frac <= 1). |
subsample_n |
str or int, default "all"
|
How many samples to subsample - do not specify both subsample_frac and subsample_n. |
subsampling_random_state |
str or int, default None
|
The random state to init subsample. |
fields_of_view |
list of int, str, default "all"
|
List of fields of view to aggregate. |
fields_of_view_feature |
str, default "Metadata_Site"
|
Name of the fields of view feature. |
object_feature |
str, default "Metadata_ObjectNumber"
|
Object number feature. |
default_datatype_float |
type
|
Numpy floating point datatype to use for load_compartment and resulting dataframes. This parameter may be used to assist with performance-related issues by reducing the memory required for floating-point data. For example, using np.float32 instead of np.float64 for this parameter will reduce memory consumed by float columns by roughly 50%. Please note: using any besides np.float64 are experimentally unverified. |
Notes
.. note:: the argument compartment_linking_cols is designed to work with CellProfiler output, as curated by cytominer-database. The default is: { "cytoplasm": { "cells": "Cytoplasm_Parent_Cells", "nuclei": "Cytoplasm_Parent_Nuclei", }, "cells": {"cytoplasm": "ObjectNumber"}, "nuclei": {"cytoplasm": "ObjectNumber"}, }
Source code in pycytominer/cyto_utils/cells.py
29 30 31 32 33 34 35 36 37 38 39 40 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
__init__(sql_file, strata=['Metadata_Plate', 'Metadata_Well'], aggregation_operation='median', output_file=None, compartments=default_compartments, compartment_linking_cols=default_linking_cols, merge_cols=['TableNumber', 'ImageNumber'], image_cols=['TableNumber', 'ImageNumber', 'Metadata_Site'], add_image_features=False, image_feature_categories=None, features='infer', load_image_data=True, image_table_name='image', subsample_frac=1, subsample_n='all', subsampling_random_state=None, fields_of_view='all', fields_of_view_feature='Metadata_Site', object_feature='Metadata_ObjectNumber', default_datatype_float=np.float64)
¶
Construct a SingleCells object.
Source code in pycytominer/cyto_utils/cells.py
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
aggregate_compartment(compartment, compute_subsample=False, compute_counts=False, add_image_features=False, n_aggregation_memory_strata=1)
¶
Aggregate morphological profiles. Uses pycytominer.aggregate().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compartment |
str
|
Compartment to aggregate. |
required |
compute_subsample |
bool
|
Whether or not to subsample. |
False
|
compute_counts |
bool
|
Whether or not to compute the number of objects in each compartment and the number of fields of view per well. |
False
|
add_image_features |
bool
|
Whether or not to add image features. |
False
|
n_aggregation_memory_strata |
int
|
Number of unique strata to pull from the database into working memory at once. Typically 1 is fastest. A larger number uses more memory. For example, if aggregating by "well", then n_aggregation_memory_strata=1 means that one "well" will be pulled from the SQLite database into memory at a time. |
1
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame of aggregated profiles. |
Source code in pycytominer/cyto_utils/cells.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |
aggregate_profiles(compute_subsample=False, output_file=None, compression_options=None, float_format=None, n_aggregation_memory_strata=1, **kwargs)
¶
Aggregate and merge compartments. This is the primary entry to this class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compute_subsample |
bool
|
Whether or not to compute subsample. compute_subsample must be specified to perform subsampling. The function aggregate_profiles(compute_subsample=True) will apply subsetting even if subsample is initialized. |
False
|
output_file |
str
|
The name of a file to output. We recommended that, if provided, the output file be suffixed with "_augmented". |
None
|
compression_options |
str
|
Compression arguments as input to pandas.to_csv() with pandas version >= 1.2. |
None
|
float_format |
str
|
Decimal precision to use in writing output file. |
None
|
n_aggregation_memory_strata |
int
|
Number of unique strata to pull from the database into working memory at once. Typically 1 is fastest. A larger number uses more memory. |
1
|
Returns:
| Type | Description |
|---|---|
DataFrame or str
|
if output_file=None) returns a Pandas dataframe else will write to file and return the filepath of the file |
Source code in pycytominer/cyto_utils/cells.py
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
count_cells(compartment='cells', count_subset=False)
¶
Determine how many cells are measured per well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compartment |
str
|
Compartment to subset. |
"cells"
|
count_subset |
bool
|
Whether or not count the number of cells as specified by the strata groups. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame of cell counts in the experiment. |
Source code in pycytominer/cyto_utils/cells.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
count_sql_table_rows(table)
¶
Count total number of rows for a table.
Source code in pycytominer/cyto_utils/cells.py
414 415 416 417 | |
get_sql_table_col_names(table)
¶
Get column names from the database.
Source code in pycytominer/cyto_utils/cells.py
419 420 421 422 423 424 | |
get_subsample(df=None, compartment='cells', rename_col=True)
¶
Apply the subsampling procedure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
DataFrame of a single cell profile. |
None
|
compartment |
str
|
The compartment to process. |
"cells"
|
rename_col |
bool
|
Whether or not to rename the columns. |
True
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/cells.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
load_compartment(compartment)
¶
Create the compartment dataframe.
Note: makes use of default_datatype_float attribute for setting a default floating point datatype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compartment |
str
|
The compartment to process. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Compartment dataframe. |
Source code in pycytominer/cyto_utils/cells.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
load_image(image_table_name=None)
¶
Load image table from sqlite file.
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/cells.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
merge_single_cells(compute_subsample=False, sc_output_file=None, compression_options=None, float_format=None, single_cell_normalize=False, normalize_args=None, platemap=None, **kwargs)
¶
Given the linking columns, merge single cell data. Normalization is also supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compute_subsample |
bool
|
Whether or not to compute subsample. |
False
|
sc_output_file |
str
|
The name of a file to output. |
None
|
compression_options |
str
|
Compression arguments as input to pandas.to_csv() with pandas version >= 1.2. |
None
|
float_format |
str
|
Decimal precision to use in writing output file. |
None
|
single_cell_normalize |
bool
|
Whether or not to normalize the single cell data. |
False
|
normalize_args |
dict
|
Additional arguments passed as input to pycytominer.normalize(). |
None
|
platemap |
Optional[Union[str, DataFrame]]
|
optional platemap filepath str or pd.DataFrame to be used with results via annotate |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame or str
|
if output_file=None returns a Pandas dataframe else will write to file and return the filepath of the file |
Source code in pycytominer/cyto_utils/cells.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
set_output_file(output_file)
¶
Set or modify output file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_file |
str
|
New output file name. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/cells.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
set_subsample_frac(subsample_frac)
¶
Set or update the subsample fraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subsample_frac |
float
|
Percentage of single cells to select (0 < subsample_frac <= 1). |
1
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/cells.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
set_subsample_n(subsample_n)
¶
Set or update the subsample n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subsample_n |
int
|
Indicate how many sample to subsample - do not specify both subsample_frac and subsample_n. |
"all"
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/cells.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
set_subsample_random_state(random_state)
¶
Set or update the subsample random state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
random_state |
The random state to init subsample. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/cells.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
split_column_categories(col_names)
¶
Split a list of column names into feature and metadata columns lists.
Source code in pycytominer/cyto_utils/cells.py
426 427 428 429 430 431 432 433 434 435 436 | |
subsample_profiles(df, rename_col=True)
¶
Sample a Pandas DataFrame given subsampling information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
DataFrame of a single cell profile. |
required |
rename_col |
bool
|
Whether or not to rename the columns. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A subsampled pandas dataframe of single cell profiles. |
Source code in pycytominer/cyto_utils/cells.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
pycytominer.cyto_utils.collate
¶
Module that provides functions for collating CellProfiler-created CSVs into a single SQLite file.
collate(batch, config, plate, base_directory='../..', column=None, munge=False, csv_dir='analysis', aws_remote=None, aggregate_only=False, tmp_dir='/tmp', overwrite=False, add_image_features=True, image_feature_categories=['Granularity', 'Texture', 'ImageQuality', 'Threshold'], printtoscreen=True)
¶
Collate the CellProfiler-created CSVs into a single SQLite file by calling cytominer-database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch |
str
|
Batch name to process |
required |
config |
str
|
Config file to pass to cytominer-database |
required |
plate |
str
|
Plate name to process |
required |
base_directory |
str
|
Base directory for subdirectories containing CSVs, backends, etc; in our preferred structure, this is the "workspace" directory |
"../.."
|
column |
str
|
An existing column to be explicitly copied to a new column called Metadata_Plate if no Metadata_Plate column already explicitly exists |
None
|
munge |
bool
|
Whether munge should be passed to cytominer-database, if True cytominer-database will expect a single all-object CSV; it will split each object into its own table |
False
|
csv_dir |
str
|
The directory under the base directory where the analysis CSVs will be found. If running the analysis pipeline, this should nearly always be "analysis" |
'analysis'
|
aws_remote |
str
|
A remote AWS prefix, if set CSV files will be synced down from at the beginning and to which SQLite files will be synced up at the end of the run |
None
|
aggregate_only |
bool
|
Whether to perform only the aggregation of existent SQLite files and bypass previous collation steps |
False
|
tmp_dir |
The temporary directory to be used by cytominer-databases for output |
'/tmp'
|
|
overwrite |
Whether or not to overwrite an sqlite that exists in the temporary directory if it already exists |
False
|
|
add_image_features |
Whether or not to add the image features to the profiles |
True
|
|
image_feature_categories |
The list of image feature groups to be used by add_image_features during aggregation |
['Granularity', 'Texture', 'ImageQuality', 'Threshold']
|
|
printtoscreen |
Whether or not to print output to the terminal |
True
|
Source code in pycytominer/cyto_utils/collate.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
run_check_errors(cmd)
¶
Run a system command, and exit if an error occurred, otherwise continue.
Source code in pycytominer/cyto_utils/collate.py
10 11 12 13 14 15 16 17 18 19 20 | |
pycytominer.cyto_utils.collate_cmd
¶
Command line interface for collate function in pycytominer.cyto_utils.collate.
pycytominer.cyto_utils.cp_image_features
¶
Functions for counting the number of fields and aggregating other images features.
aggregate_fields_count(image_df, strata, fields_of_view_feature)
¶
Compute the number of fields per well and create a new column called Metadata_Site_Count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_df |
DataFrame
|
Image table dataframe which includes the strata and fields of view feature as columns. |
required |
strata |
list of str
|
The columns to groupby and aggregate single cells. |
required |
fields_of_view_feature |
Name of the fields of the view column. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fields_count_df |
DataFrame
|
DataFrame with the Metadata_Site_Count column. |
Source code in pycytominer/cyto_utils/cp_image_features.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
aggregate_image_count_features(df, image_features_df, image_cols, strata, count_prefix='Count')
¶
Aggregate the Count features in the Image table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
Dataframe of aggregated profiles. |
required |
image_features_df |
DataFrame
|
Image table dataframe with Count features |
required |
image_cols |
list of str
|
Columns to select from the image table. |
required |
strata |
list of str
|
The columns to groupby and aggregate single cells. |
required |
count_prefix |
str
|
Prefix of the count columns in the image table. |
"Count"
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
DataFrame with aggregated Count features in the Image table. |
remove_cols |
list of str
|
Columns to remove from the image table before aggregating using aggregate_image_features() |
Source code in pycytominer/cyto_utils/cp_image_features.py
37 38 39 40 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 | |
aggregate_image_features(df, image_features_df, image_feature_categories, image_cols, strata, aggregation_operation, count_prefix='Count')
¶
Aggregate the non-Count image features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
Dataframe of aggregated profiles. |
required |
image_features_df |
DataFrame
|
Image table dataframe with all the image_feature_category features. |
required |
image_feature_categories |
list of str
|
List of categories of features from the image table to add to the profiles. |
required |
image_cols |
list of str
|
Columns to select from the image table. |
required |
strata |
list of str
|
The columns to groupby and aggregate single cells. |
required |
aggregation_operation |
str
|
Operation to perform image table feature aggregation. |
required |
count_prefix |
str
|
Prefix of the count columns in the image table. |
"Count"
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
DataFrame of aggregated image features. |
Source code in pycytominer/cyto_utils/cp_image_features.py
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 | |
pycytominer.cyto_utils.features
¶
Utility function to manipulate cell profiler features.
convert_compartment_format_to_list(compartments)
¶
Convert cell painting compartments to a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compartments |
list of str or str
|
Cell Painting compartment(s). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
compartments |
list of str
|
List of Cell Painting compartments. |
Source code in pycytominer/cyto_utils/features.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
count_na_features(population_df, features)
¶
Given a population dataframe and features, count how many nas per feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population_df |
DataFrame
|
DataFrame of profiles. |
required |
features |
list of str
|
Features present in the population dataframe. |
required |
Returns:
| Type | Description |
|---|---|
Dataframe of NA counts per feature
|
|
Source code in pycytominer/cyto_utils/features.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
drop_outlier_features(population_df, features='infer', samples='all', outlier_cutoff=500)
¶
Exclude a feature if its min or max absolute value is greater than the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population_df |
DataFrame
|
DataFrame that includes metadata and observation features. |
required |
features |
list of str or str
|
Features present in the population dataframe. If "infer", then assume Cell Painting features are those that start with "Cells_", "Nuclei_", or "Cytoplasm_" |
"infer"
|
samples |
str
|
List of samples to perform operation on. The function uses a pd.DataFrame.query() function, so you should structure samples in this fashion. An example is "Metadata_treatment == 'control'" (include all quotes). If "all", use all samples to calculate. |
"all"
|
outlier_cutoff |
int or float
|
see https://github.com/cytomining/pycytominer/issues/237 for details. Threshold to remove features if absolute values is greater |
500
|
Returns:
| Name | Type | Description |
|---|---|---|
outlier_features |
list of str
|
Features greater than the threshold. |
Source code in pycytominer/cyto_utils/features.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
get_blocklist_features(blocklist_file=blocklist_file, population_df=None)
¶
Get a list of blocklist features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocklist_file |
path-like object
|
Location of the dataframe with features to exclude. |
blocklist_file
|
population_df |
DataFrame
|
Profile dataframe used to subset blocklist features. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
blocklist_features |
list of str
|
Features to exclude from downstream analysis. |
Source code in pycytominer/cyto_utils/features.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
infer_cp_features(population_df, compartments=['Cells', 'Nuclei', 'Cytoplasm'], metadata=False, image_features=False)
¶
Given a dataframe, output features that we expect to be Cell Painting features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population_df |
DataFrame
|
DataFrame from which features are to be inferred. |
required |
compartments |
list of str
|
Compartments from which Cell Painting features were extracted. |
["Cells", "Nuclei", "Cytoplasm"]
|
metadata |
bool
|
Whether or not to infer metadata features. |
False
|
image_features |
bool
|
Whether or not the profiles contain image features. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
features |
list of str
|
List of Cell Painting features. |
Source code in pycytominer/cyto_utils/features.py
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 | |
label_compartment(cp_features, compartment, metadata_cols)
¶
Assign compartment label to each features as a prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cp_features |
list of str
|
All features being used. |
required |
compartment |
str
|
Measured compartment. |
required |
metadata_cols |
list
|
Columns that should be considered metadata. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
cp_features |
list of str
|
Recoded column names with appropriate metadata and compartment labels. |
Source code in pycytominer/cyto_utils/features.py
40 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 | |
pycytominer.cyto_utils.load
¶
Module for loading data from various file formats.
infer_delim(file)
¶
Sniff the delimiter in the given file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file |
str
|
File name |
required |
Return
the delimiter used in the dataframe (typically either tab or commas)
Source code in pycytominer/cyto_utils/load.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
is_path_a_parquet_file(file)
¶
Check if the provided file path is a parquet file.
Identify parquet files by inspecting the file extensions.
If the file does not end with parquet, this will return False, else True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file |
Union[str, PurePath]
|
path to parquet file |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Returns True if the file path contains |
Raises:
| Type | Description |
|---|---|
TypeError
|
Raised if a non str or non-path object is passed in the |
FileNotFoundError
|
Raised if the provided path in the |
Source code in pycytominer/cyto_utils/load.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
load_npz_features(npz_file, fallback_feature_prefix='DP', metadata=True)
¶
Load an npz file storing features and, sometimes, metadata.
The function will first search the .npz file for a metadata column called "Metadata_Model". If the field exists, the function uses this entry as the feature prefix. If it doesn't exist, use the fallback_feature_prefix.
If the npz file does not exist, this function returns an empty dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
npz_file |
str
|
file path to the compressed output (typically DeepProfiler output) |
required |
fallback_feature_prefix |
a string to prefix all features [default: "DP"]. |
'DP'
|
Return
df : pandas.core.frame.DataFrame pandas DataFrame of profiles
Source code in pycytominer/cyto_utils/load.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
load_npz_locations(npz_file, location_x_col_index=0, location_y_col_index=1)
¶
Load an npz file storing locations and, sometimes, metadata.
The function will first search the .npz file for a metadata column called "locations". If the field exists, the function uses this entry as the feature prefix.
If the npz file does not exist, this function returns an empty dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
npz_file |
str
|
file path to the compressed output (typically DeepProfiler output) |
required |
location_x_col_index |
index of the x location column (which column in DP output has X coords) |
0
|
|
location_y_col_index |
index of the y location column (which column in DP output has Y coords) |
1
|
Return
df : pandas.core.frame.DataFrame pandas DataFrame of profiles
Source code in pycytominer/cyto_utils/load.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
load_platemap(platemap, add_metadata_id=True)
¶
Unless a dataframe is provided, load the given platemap dataframe from path or string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
platemap |
pandas dataframe
|
location or actual pandas dataframe of platemap file |
required |
add_metadata_id |
bool
|
boolean if "Metadata_" should be appended to all platemap columns |
True
|
Return
platemap : pandas.core.frame.DataFrame pandas DataFrame of profiles
Source code in pycytominer/cyto_utils/load.py
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 | |
load_profiles(profiles)
¶
Unless a dataframe is provided, load the given profile dataframe from path or string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profiles |
(str, Path, DataFrame)
|
file location or actual pandas dataframe of profiles |
str
|
Return
pandas DataFrame of profiles
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
Raised if the provided profile does not exists |
Source code in pycytominer/cyto_utils/load.py
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 | |
pycytominer.cyto_utils.modz
¶
Module for performing a modified z score transformation.
modz(population_df, replicate_columns, features='infer', method='spearman', min_weight=0.01, precision=4)
¶
Collapse replicates into a consensus signature using a weighted transformation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population_df |
DataFrame
|
DataFrame that includes metadata and observation features. |
required |
replicate_columns |
(str, list)
|
a string or list of column(s) in the population dataframe that indicate replicate level information |
required |
features |
list
|
List of features present in the population dataframe [default: "infer"] if "infer", then assume cell painting features are those that start with "Cells_", "Nuclei_", or "Cytoplasm_". |
"infer"
|
method |
str
|
indicating which correlation metric to use. |
"spearman"
|
min_weight |
float
|
the minimum correlation to clip all non-negative values lower to |
0.01
|
precision |
int
|
how many significant digits to round weights to |
4
|
Returns:
| Name | Type | Description |
|---|---|---|
modz_df |
DataFrame
|
Consensus signatures with metadata for all replicates in the given DataFrame |
Source code in pycytominer/cyto_utils/modz.py
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 | |
modz_base(population_df, method='spearman', min_weight=0.01, precision=4)
¶
Perform a modified z score transformation.
This code is modified from cmapPy. (see https://github.com/cytomining/pycytominer/issues/52). Note that this will apply the transformation to the FULL population_df. See modz() for replicate level procedures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population_df |
DataFrame
|
DataFrame that includes metadata and observation features. |
required |
method |
str
|
indicating which correlation metric to use. |
"spearman"
|
min_weight |
float
|
the minimum correlation to clip all non-negative values lower to |
0.01
|
precision |
int
|
how many significant digits to round weights to |
4
|
Returns:
| Name | Type | Description |
|---|---|---|
modz_df |
DataFrame
|
modz transformed dataframe - a consensus signature of the input data weighted by replicate correlation |
Source code in pycytominer/cyto_utils/modz.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 | |
pycytominer.cyto_utils.output
¶
Utility function to compress output data.
check_compression_method(compression)
¶
Ensure compression options are set properly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compression |
str
|
The category of compression options available |
required |
Returns:
| Type | Description |
|---|---|
None
|
Asserts available options |
Source code in pycytominer/cyto_utils/output.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
output(df, output_filename, output_type='csv', sep=',', float_format=None, compression_options={'method': 'gzip', 'mtime': 1}, **kwargs)
¶
Given an output file and compression options, write file to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
pandas.core.frame.DataFrame
|
a pandas dataframe that will be written to file |
required |
output_filename |
str
|
location of file to write |
required |
output_type |
str
|
type of output file to create |
"csv"
|
sep |
str
|
file delimiter |
','
|
float_format |
str
|
Decimal precision to use in writing output file as input to pd.DataFrame.to_csv(float_format=float_format). For example, use "%.3g" for 3 decimal precision. |
None
|
compression_options |
str or dict
|
Contains compression options as input to pd.DataFrame.to_csv(compression=compression_options). pandas version >= 1.2. |
{"method": "gzip", "mtime": 1}
|
Returns:
| Type | Description |
|---|---|
str
|
returns output_filename |
Examples:
import pandas as pd from pycytominer.cyto_utils import output
data_df = pd.concat( [ pd.DataFrame( { "Metadata_Plate": "X", "Metadata_Well": "a", "Cells_x": [0.1, 0.3, 0.8], "Nuclei_y": [0.5, 0.3, 0.1], } ), pd.DataFrame( { "Metadata_Plate": "X", "Metadata_Well": "b", "Cells_x": [0.4, 0.2, -0.5], "Nuclei_y": [-0.8, 1.2, -0.5], } ), ] ).reset_index(drop=True)
output_file = "test.csv.gz" output( df=data_df, output_filename=output_file, sep=",", compression_options={"method": "gzip", "mtime": 1}, float_format=None, )
Source code in pycytominer/cyto_utils/output.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 | |
set_compression_method(compression)
¶
Set the compression options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compression |
str or dict
|
Contains compression options as input to pd.DataFrame.to_csv(compression=compression_options). pandas version >= 1.2. |
required |
Returns:
| Type | Description |
|---|---|
(compression, dict)
|
A formated dictionary expected by output() |
Source code in pycytominer/cyto_utils/output.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
pycytominer.cyto_utils.single_cell_ingest_utils
¶
Utility functions for single cell ingest.
assert_linking_cols_complete(linking_cols='default', compartments='default')
¶
Confirm that the linking cols and compartments are compatible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
linking_cols |
str or dict
|
Specify how to link objects |
"default"
|
compartments |
str or list
|
Which compartments used in the experiment. |
"default"
|
Returns:
| Type | Description |
|---|---|
None
|
Asserts linking columns are appropriately defined |
.. note::
|
assert_linking_cols_complete() does not check if columns are present |
Source code in pycytominer/cyto_utils/single_cell_ingest_utils.py
30 31 32 33 34 35 36 37 38 39 40 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 | |
get_default_linking_cols()
¶
Define the standard experiment linking columns between tables.
Returns:
| Type | Description |
|---|---|
(linking_cols, dict)
|
A dictionary mapping columns that links together CellProfiler objects |
.. note::
|
every dictionary pair has a 1 to 1 correspondence (e.g. cytoplasm-cells and cells-cytoplasm both must exist) |
Source code in pycytominer/cyto_utils/single_cell_ingest_utils.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
provide_linking_cols_feature_name_update(linking_cols='default')
¶
Output a dictionary to use to update pandas dataframe column names from linking cols in the Metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
linking_cols |
str or dict
|
Specify how to link objects |
"default"
|
Returns:
| Type | Description |
|---|---|
(update_name, dict)
|
Dictionary of the linking column names to update after they are used |
Source code in pycytominer/cyto_utils/single_cell_ingest_utils.py
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 | |
pycytominer.cyto_utils.util
¶
Miscellaneous utility functions.
check_aggregate_operation(operation)
¶
Confirm that the input operation for aggregation is currently supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation |
str
|
Aggregation operation to provide. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Correctly formatted operation method. |
Source code in pycytominer/cyto_utils/util.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
check_compartments(compartments)
¶
Check if the input compartments are noncanonical compartments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compartments |
list of str
|
Input compartments. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/util.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
check_consensus_operation(operation)
¶
Confirm that the input operation for consensus is currently supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation |
Consensus operation to provide. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Correctly formatted operation method. |
Source code in pycytominer/cyto_utils/util.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
check_correlation_method(method)
¶
Confirm that the input method is currently supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method |
str
|
The correlation metric to use. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Correctly formatted correlation method. |
Source code in pycytominer/cyto_utils/util.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
check_fields_of_view(data_fields_of_view, input_fields_of_view)
¶
Confirm that the input list of fields of view is a subset of the list of fields of view in the image table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_fields_of_view |
list of int
|
Fields of view in the image table. |
required |
input_fields_of_view |
list of int
|
Input fields of view. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/util.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
check_fields_of_view_format(fields_of_view)
¶
Confirm that the input fields of view is valid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields_of_view |
list of int
|
List of integer fields of view. |
required |
Returns:
| Type | Description |
|---|---|
str or list of int
|
Correctly formatted fields_of_view variable. |
Source code in pycytominer/cyto_utils/util.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
check_image_features(image_features, image_columns)
¶
Confirm that the input list of image features are present in the image table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_features |
Input image features to extract from the image table. |
required | |
image_columns |
Columns in the image table |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in pycytominer/cyto_utils/util.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
extract_image_features(image_feature_categories, image_df, image_cols, strata)
¶
Confirm that the input list of image features categories are present in the image table and then extract those features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_feature_categories |
list of str
|
Input image feature groups to extract from the image table. |
required |
image_df |
DataFrame
|
Image dataframe. |
required |
image_cols |
list of str
|
Columns to select from the image table. |
required |
strata |
list of str
|
The columns to groupby and aggregate single cells. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
image_features_df |
DataFrame
|
Dataframe with extracted image features. |
image_feature_categories |
list of str
|
Correctly formatted image feature categories. |
Source code in pycytominer/cyto_utils/util.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
get_default_compartments()
¶
Return default compartments.
Returns:
| Type | Description |
|---|---|
list of str
|
Default compartments. |
Source code in pycytominer/cyto_utils/util.py
16 17 18 19 20 21 22 23 24 25 | |
get_pairwise_correlation(population_df, method='pearson')
¶
Given a population dataframe, calculate all pairwise correlations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population_df |
DataFrame
|
Includes metadata and observation features. |
required |
method |
str
|
Which correlation matrix to use to test cutoff. |
"pearson"
|
Returns:
| Type | Description |
|---|---|
list of str
|
Features to exclude from the population_df. |
Source code in pycytominer/cyto_utils/util.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
load_known_metadata_dictionary(metadata_file=default_metadata_file)
¶
Load previously known metadata columns per compartment from metadata text file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_file |
str
|
File location of the metadata text file which should be a tab-separated file with two columns: ["compartment", "feature"]. If not provided, the default metadata file will be used. |
default_metadata_file
|
Returns:
| Type | Description |
|---|---|
dict
|
Compartment (keys) mappings to previously known metadata (values). |
Source code in pycytominer/cyto_utils/util.py
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 | |
pycytominer.cyto_utils.write_gct
¶
Module to write a gct file from a pandas DataFrame.
Transform profiles into a gct (Gene Cluster Text) file A gct is a tab deliminted text file that traditionally stores gene expression data File Format Description: https://clue.io/connectopedia/gct_format.
Modified from cytominer_scripts "write_gcg" written in R https://github.com/broadinstitute/cytominer_scripts/blob/master/write_gct.R
write_gct(profiles, output_file, features='infer', meta_features='infer', feature_metadata=None, version='#1.3')
¶
Convert profiles to a .gct file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profiles |
DataFrame
|
DataFrame of profiles. |
required |
output_file |
str
|
If provided, will write gct to file. |
required |
features |
list
|
A list of strings corresponding to feature measurement column names in the
|
'infer'
|
meta_features |
list
|
A list of strings corresponding to metadata column names in the |
'infer'
|
feature_metadata |
DataFrame
|
|
None
|
version |
str
|
Important for gct loading into Morpheus |
"#1.3"
|
Returns:
| Type | Description |
|---|---|
None
|
Writes gct to file |
Source code in pycytominer/cyto_utils/write_gct.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 | |