cytopy.flow.cell_classifier.sklearn_classifier¶
This module contains the SklearnCellClassifier for using supervised classification methods, trained on some labeled FileGroup (has existing Populations) to predict single cell classifications.
Copyright 2020 Ross Burton
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Classes:
|
Use supervised machine learning to predict the classification of single cell data. |
-
class
cytopy.flow.cell_classifier.sklearn_classifier.
SklearnCellClassifier
(model, params: dict, multi_label: bool = False, **kwargs)¶ Use supervised machine learning to predict the classification of single cell data. This class allows the user to apply an Scikit-Learn classifier or classifier that follows the conventions of Scikit-Learn i.e. contains the methods ‘fit’, ‘fit_predict’ and ‘predict’. Training data should be provided in the form of a FileGroup with existing Populations. Supports multi-class and multi-label classification; if multi-label classification is chosen, the tree structure of training data is NOT conserved - all resulting populations will have the same parent population.
- Parameters
model (Scikit-Learn Classifier) – Should be a valid Scikit-Learn class or similar e.g. XGBClassifier
params (dict) – Parameters to initiate class with
features (list) – List of channels/markers to use as features in prediction
target_populations (list) – List of populations from training data to predict
multi_label (bool (default=False)) – If True, single cells can belong to more than one population. The tree structure of training data is NOT conserved - all resulting populations will have the same parent population.
logging_level (int (default=logging.INFO)) – Level to log events at
log (str, optional) – Path to log output to; if not given, will log to stdout
population_prefix (str (default=”CellClassifier_”)) – Prefix applied to populations generated
-
transformer
¶ Transformer object
- Type
-
class_weights
¶ Sample class weights; key is sample index, value is weight. Set by calling compute_class_weights.
- Type
dict
-
x
¶ Training feature space
- Type
Pandas.DataFrame
-
y
¶ Target labels
- Type
numpy.ndarray
-
logger
¶ - Type
logging.Logger
-
features
¶ - Type
list
-
target_populations
¶ - Type
list
Methods:
load_model
(path, **kwargs)Load a pickled model from disk.
plot_confusion_matrix
([cmap, figsize, x, y])Wraps cytopy.flow.supervised.confusion_matrix_plots (see for more details).
plot_learning_curve
([experiment, …])This method will generate a learning curve using the Scikit-Learn utility function sklearn.model_selection.learning_curve.
save_model
(path, **kwargs)Pickle the associated model and save to disk.
-
load_model
(path: str, **kwargs)¶ Load a pickled model from disk. WARNING: be aware of continuity issues. Compatibility with new releases of Scikit-Learn and cytopy are not guaranteed. The loaded model must correspond to the expected method for this CellClassifier.
- Parameters
path (str) – Where to save on disk
kwargs – Additional keyword arguments passed to pickle.dump call
- Returns
- Return type
None
-
plot_confusion_matrix
(cmap: Optional[str] = None, figsize: tuple = (10, 5), x: Optional[pandas.core.frame.DataFrame] = None, y: Optional[numpy.ndarray] = None, **kwargs)¶ Wraps cytopy.flow.supervised.confusion_matrix_plots (see for more details). Given some feature space and target labels, use the model to generate a confusion matrix heatmap. If x and y are not provided, will use associated training data.
- Parameters
cmap (str (optional)) – Colour scheme
figsize (tuple (default=(10, 5))) – Figure size
x (Pandas.DataFrame (optional)) – Feature space. If not given, will use associated training data. To use a validation dataset, use the ‘load_validation’ method to get relevant data.
y (numpy.ndarray (optional)) – Target labels. If not given, will use associated training data. To use a validation dataset, use the ‘load_validation’ method to get relevant data.
kwargs – Additional keyword arguments passed to cytopy.flow.supervised.confusion_matrix_plots
- Returns
- Return type
Matplotlib.Figure
- Raises
AssertionError – Invalid x, y input
-
plot_learning_curve
(experiment: Optional[cytopy.data.experiment.Experiment] = None, validation_id: Optional[str] = None, root_population: Optional[str] = None, ax: Optional[matplotlib.axes._axes.Axes] = None, x_label: str = 'Training examples', y_label: str = 'Score', train_sizes: Optional[numpy.array] = None, verbose: int = 1, **kwargs)¶ This method will generate a learning curve using the Scikit-Learn utility function sklearn.model_selection.learning_curve. Either use the associated training data or a validation FileGroup by providing the Experiment object and the ID for the validation sample (validation_id). This validation sample should contain the same populations as the training data, which must be downstream of the ‘root_population’.
- Parameters
experiment (Experiment (optional)) – If provided, should be the same Experiment training data was derived from
validation_id (str (optional)) – Name of the sample to use for validation
root_population (str (optional)) – If not given, will use the same root_population as training data
ax (Matplotlib.Axes (optional)) – Axes object to use to draw plot
x_label (str (default="Training examples")) – X-axis labels
y_label (str (default="Score")) – Y-axis labels
train_sizes (numpy.ndarray (optional)) – Defaults to linear range between 0.1 and 1.0, with 10 steps
verbose (int (default=1)) – Passed to learning_curve function
kwargs – Additional keyword arguments passed to sklearn.model_selection.learning_curve
- Returns
- Return type
Matplotlib.Axes
- Raises
AssertionError – If plotting learning curve for validation and Experiment, validation_id or root_population not provided
-
save_model
(path: str, **kwargs)¶ Pickle the associated model and save to disk. WARNING: be aware of continuity issues. Compatibility with new releases of Scikit-Learn and cytopy are not guaranteed.
- Parameters
path (str) – Where to save on disk
kwargs – Additional keyword arguments passed to pickle.dump call
- Returns
- Return type
None