immuneML.workflows.instructions package¶
Subpackages¶
- immuneML.workflows.instructions.dataset_generation package
- immuneML.workflows.instructions.exploratory_analysis package
- Submodules
- immuneML.workflows.instructions.exploratory_analysis.ExploratoryAnalysisInstruction module
- immuneML.workflows.instructions.exploratory_analysis.ExploratoryAnalysisState module
- immuneML.workflows.instructions.exploratory_analysis.ExploratoryAnalysisUnit module
ExploratoryAnalysisUnit
ExploratoryAnalysisUnit.dataset
ExploratoryAnalysisUnit.dim_reduction
ExploratoryAnalysisUnit.encoder
ExploratoryAnalysisUnit.example_weighting
ExploratoryAnalysisUnit.label_config
ExploratoryAnalysisUnit.number_of_processes
ExploratoryAnalysisUnit.preprocessing_sequence
ExploratoryAnalysisUnit.report
ExploratoryAnalysisUnit.report_result
- Module contents
- immuneML.workflows.instructions.ml_model_application package
- immuneML.workflows.instructions.subsampling package
Submodules¶
immuneML.workflows.instructions.Instruction module¶
immuneML.workflows.instructions.MLProcess module¶
- class immuneML.workflows.instructions.MLProcess.MLProcess(train_dataset: Dataset, test_dataset: Dataset, label: Label, metrics: set, optimization_metric: ClassificationMetric, path: Path, ml_reports: List[MLReport] = None, encoding_reports: list = None, data_reports: list = None, number_of_processes: int = 2, label_config: LabelConfiguration = None, report_context: dict = None, hp_setting: HPSetting = None, example_weighting: ExampleWeightingStrategy = None, sequence_type: SequenceType = SequenceType.AMINO_ACID, region_type: RegionType = RegionType.IMGT_CDR3)[source]¶
Bases:
object
- Class that implements the machine learning process:
encodes the training dataset
encodes the test dataset (using parameters learnt in step 1 if there are any such parameters)
trains the ML method on encoded training dataset
assesses the method’s performance on encoded test dataset
It performs the task for a given label configuration, and given list of metrics (used only in the assessment step).
immuneML.workflows.instructions.SimulationInstruction module¶
immuneML.workflows.instructions.TrainMLModelInstruction module¶
- class immuneML.workflows.instructions.TrainMLModelInstruction.TrainMLModelInstruction(dataset, hp_strategy: HPOptimizationStrategy, hp_settings: list, assessment: SplitConfig, selection: SplitConfig, metrics: set, optimization_metric: ClassificationMetric, label_configuration: LabelConfiguration, path: Path = None, context: dict = None, number_of_processes: int = 1, reports: dict = None, name: str = None, refit_optimal_model: bool = False, sequence_type: SequenceType = None, region_type: RegionType = None, export_all_ml_settings: bool = False, example_weighting: ExampleWeightingStrategy = None)[source]¶
Bases:
Instruction
Class implementing hyperparameter optimization and training and assessing the model through nested cross-validation (CV). The process is defined by two loops:
the outer loop over defined splits of the dataset for performance assessment
the inner loop over defined hyperparameter space and with cross-validation or train & validation split to choose the best hyperparameters.
Optimal model chosen by the inner loop is then retrained on the whole training dataset in the outer loop.
Note
If you are interested in plotting the performance of all combinations of encodings and ML methods on the test set, consider running the MLSettingsPerformance report as hyperparameter report in the assessment loop.
Specification arguments:
dataset (Dataset): dataset to use for training and assessing the classifier
hp_strategy (HPOptimizationStrategy): how to search different hyperparameters; common options include grid search, random search. Valid values are objects of any class inheriting
HPOptimizationStrategy
.hp_settings (list): a list of combinations of preprocessing_sequence, encoding and ml_method. preprocessing_sequence is optional, while encoding and ml_method are mandatory. These three options (and their parameters) can be optimized over, choosing the highest performing combination.
assessment (SplitConfig): description of the outer loop (for assessment) of nested cross-validation. It describes how to split the data, how many splits to make, what percentage to use for training and what reports to execute on those splits. See SplitConfig.
selection (SplitConfig): description of the inner loop (for selection) of nested cross-validation. The same as assessment argument, just to be executed in the inner loop. See SplitConfig.
metrics (list): a list of metrics to compute for all splits and settings created during the nested cross-validation. These metrics will be computed only for reporting purposes. For choosing the optimal setting, optimization_metric will be used.
optimization_metric (Metric): a metric to use for optimization and assessment in the nested cross-validation.
example_weighting: which example weighting strategy to use. Example weighting can be used to up-weight or down-weight the importance of each example in the dataset. These weights will be applied when computing (optimization) metrics, and are used by some encoders and ML methods.
label_configuration (LabelConfiguration): a list of labels for which to train the classifiers. The goal of the nested CV is to find the setting which will have best performance in predicting the given label (e.g., if a subject has experienced an immune event or not). Performance and optimal settings will be reported for each label separately. If a label is binary, instead of specifying only its name, one should explicitly set the name of the positive class as well under parameter positive_class. If positive class is not set, one of the label classes will be assumed to be positive.
number_of_processes (int): how many processes should be created at once to speed up the analysis. For personal machines, 4 or 8 is usually a good choice.
reports (list): a list of report names to be executed after the nested CV has finished to show the overall performance or some statistic; the reports to be specified here have to be
TrainMLModelReport
reports.refit_optimal_model (bool): if the final combination of preprocessing-encoding-ML model should be refitted on the full dataset thus providing the final model to be exported from instruction; alternatively, train combination from one of the assessment folds will be used
export_all_models (bool): if set to True, all trained models in the assessment split are exported as .zip files. If False, only the optimal model is exported. By default, export_all_models is False.
sequence_type (str): whether to perform the analysis on amino acid or nucleotide sequences
region_type (str): which part of the sequence to analyze, e.g., IMGT_CDR3
YAML specification:
instructions: my_nested_cv_instruction: # user-defined name of the instruction type: TrainMLModel # which instruction should be executed settings: # a list of combinations of preprocessing, encoding and ml_method to optimize over - preprocessing: seq1 # preprocessing is optional encoding: e1 # mandatory field ml_method: simpleLR # mandatory field - preprocessing: seq1 # the second combination encoding: e2 ml_method: simpleLR assessment: # outer loop of nested CV split_strategy: random # perform Monte Carlo CV (randomly split the data into train and test) split_count: 1 # how many train/test datasets to generate training_percentage: 0.7 # what percentage of the original data should be used for the training set reports: # reports to execute on training/test datasets, encoded datasets and trained ML methods data_splits: # list of reports to execute on training/test datasets (before they are encoded) - rep1 encoding: # list of reports to execute on encoded training/test datasets - rep2 models: # list of reports to execute on trained ML methods for each assessment CV split - rep3 selection: # inner loop of nested CV split_strategy: k_fold # perform k-fold CV split_count: 5 # how many fold to create: here these two parameters mean: do 5-fold CV reports: data_splits: # list of reports to execute on training/test datasets (in the inner loop, so these are actually training and validation datasets) - rep1 models: # list of reports to execute on trained ML methods for each selection CV split - rep2 encoding: # list of reports to execute on encoded training/test datasets (again, it is training/validation here) - rep3 labels: # list of labels to optimize the classifier for, as given in the metadata for the dataset - celiac: positive_class: + # if it's binary classification, positive class parameter should be set - T1D # this is not binary label, so no need to specify positive class dataset: d1 # which dataset to use for the nested CV strategy: GridSearch # how to choose the combinations which to test from settings (GridSearch means test all) metrics: # list of metrics to compute for all settings, but these do not influence the choice of optimal model - accuracy - auc reports: # list of reports to execute when nested CV is finished to show overall performance - rep4 number_of_processes: 4 # number of parallel processes to create (could speed up the computation) optimization_metric: balanced_accuracy # the metric to use for choosing the optimal model and during training refit_optimal_model: False # use trained model, do not refit on the full dataset export_all_ml_settings: False # only export the optimal setting region_type: IMGT_CDR3 sequence_type: AMINO_ACID
- print_performances(state: TrainMLModelState)[source]¶