|
| 1 | +""" |
| 2 | +Test cases for CSAI classification model. |
| 3 | +""" |
| 4 | + |
| 5 | +# Created by Linglong Qian <[email protected]> |
| 6 | +# License: BSD-3-Clause |
| 7 | + |
| 8 | +import os |
| 9 | +import unittest |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from pypots.classification import CSAI |
| 14 | +from pypots.optim import Adam |
| 15 | +from pypots.utils.logging import logger |
| 16 | +from pypots.utils.metrics import calc_binary_classification_metrics |
| 17 | +from tests.global_test_config import ( |
| 18 | + DATA, |
| 19 | + EPOCHS, |
| 20 | + DEVICE, |
| 21 | + TRAIN_SET, |
| 22 | + VAL_SET, |
| 23 | + TEST_SET, |
| 24 | + GENERAL_H5_TRAIN_SET_PATH, |
| 25 | + GENERAL_H5_VAL_SET_PATH, |
| 26 | + GENERAL_H5_TEST_SET_PATH, |
| 27 | + RESULT_SAVING_DIR_FOR_CLASSIFICATION, |
| 28 | + check_tb_and_model_checkpoints_existence, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +class TestCSAI(unittest.TestCase): |
| 33 | + logger.info("Running tests for a classification model CSAI...") |
| 34 | + |
| 35 | + # Set the log and model saving path |
| 36 | + saving_path = os.path.join(RESULT_SAVING_DIR_FOR_CLASSIFICATION, "CSAI") |
| 37 | + model_save_name = "saved_CSAI_model.pypots" |
| 38 | + |
| 39 | + # Initialize an Adam optimizer |
| 40 | + optimizer = Adam(lr=0.001, weight_decay=1e-5) |
| 41 | + |
| 42 | + # Initialize the CSAI model for classification |
| 43 | + csai = CSAI( |
| 44 | + n_steps=DATA["n_steps"], |
| 45 | + n_features=DATA["n_features"], |
| 46 | + n_classes=DATA["n_classes"], |
| 47 | + rnn_hidden_size=32, |
| 48 | + imputation_weight=0.7, |
| 49 | + consistency_weight=0.3, |
| 50 | + classification_weight=1.0, |
| 51 | + removal_percent=10, |
| 52 | + increase_factor=0.1, |
| 53 | + compute_intervals=True, |
| 54 | + step_channels=16, |
| 55 | + batch_size=64, |
| 56 | + epochs=EPOCHS, |
| 57 | + dropout=0.5, |
| 58 | + optimizer=optimizer, |
| 59 | + num_workers=4, |
| 60 | + device=DEVICE, |
| 61 | + saving_path=saving_path, |
| 62 | + model_saving_strategy="better", |
| 63 | + verbose=True, |
| 64 | + ) |
| 65 | + |
| 66 | + @pytest.mark.xdist_group(name="classification-csai") |
| 67 | + def test_0_fit(self): |
| 68 | + # Fit the CSAI model on the training and validation datasets |
| 69 | + self.csai.fit(TRAIN_SET, VAL_SET) |
| 70 | + |
| 71 | + @pytest.mark.xdist_group(name="classification-csai") |
| 72 | + def test_1_classify(self): |
| 73 | + # Classify test set using the trained CSAI model |
| 74 | + results = self.csai.classify(TEST_SET) |
| 75 | + |
| 76 | + # Calculate binary classification metrics |
| 77 | + metrics = calc_binary_classification_metrics( |
| 78 | + results, DATA["test_y"] |
| 79 | + ) |
| 80 | + |
| 81 | + logger.info( |
| 82 | + f'CSAI ROC_AUC: {metrics["roc_auc"]}, ' |
| 83 | + f'PR_AUC: {metrics["pr_auc"]}, ' |
| 84 | + f'F1: {metrics["f1"]}, ' |
| 85 | + f'Precision: {metrics["precision"]}, ' |
| 86 | + f'Recall: {metrics["recall"]}' |
| 87 | + ) |
| 88 | + |
| 89 | + assert metrics["roc_auc"] >= 0.5, "ROC-AUC < 0.5" |
| 90 | + |
| 91 | + @pytest.mark.xdist_group(name="classification-csai") |
| 92 | + def test_2_parameters(self): |
| 93 | + # Ensure that CSAI model parameters are properly initialized and trained |
| 94 | + assert hasattr(self.csai, "model") and self.csai.model is not None |
| 95 | + |
| 96 | + assert hasattr(self.csai, "optimizer") and self.csai.optimizer is not None |
| 97 | + |
| 98 | + assert hasattr(self.csai, "best_loss") |
| 99 | + self.assertNotEqual(self.csai.best_loss, float("inf")) |
| 100 | + |
| 101 | + assert ( |
| 102 | + hasattr(self.csai, "best_model_dict") |
| 103 | + and self.csai.best_model_dict is not None |
| 104 | + ) |
| 105 | + |
| 106 | + @pytest.mark.xdist_group(name="classification-csai") |
| 107 | + def test_3_saving_path(self): |
| 108 | + # Ensure the root saving directory exists |
| 109 | + assert os.path.exists( |
| 110 | + self.saving_path |
| 111 | + ), f"file {self.saving_path} does not exist" |
| 112 | + |
| 113 | + # Check if the tensorboard file and model checkpoints exist |
| 114 | + check_tb_and_model_checkpoints_existence(self.csai) |
| 115 | + |
| 116 | + # Save the trained model to file, and verify the file existence |
| 117 | + saved_model_path = os.path.join(self.saving_path, self.model_save_name) |
| 118 | + self.csai.save(saved_model_path) |
| 119 | + |
| 120 | + # Test loading the saved model |
| 121 | + self.csai.load(saved_model_path) |
| 122 | + |
| 123 | + @pytest.mark.xdist_group(name="classification-csai") |
| 124 | + def test_4_lazy_loading(self): |
| 125 | + # Fit the CSAI model using lazy-loading datasets from H5 files |
| 126 | + self.csai.fit(GENERAL_H5_TRAIN_SET_PATH, GENERAL_H5_VAL_SET_PATH) |
| 127 | + |
| 128 | + # Perform classification using lazy-loaded data |
| 129 | + results = self.csai.classify(GENERAL_H5_TEST_SET_PATH) |
| 130 | + |
| 131 | + # Calculate binary classification metrics |
| 132 | + metrics = calc_binary_classification_metrics( |
| 133 | + results, DATA["test_y"] |
| 134 | + ) |
| 135 | + |
| 136 | + logger.info( |
| 137 | + f'Lazy-loading CSAI ROC_AUC: {metrics["roc_auc"]}, ' |
| 138 | + f'PR_AUC: {metrics["pr_auc"]}, ' |
| 139 | + f'F1: {metrics["f1"]}, ' |
| 140 | + f'Precision: {metrics["precision"]}, ' |
| 141 | + f'Recall: {metrics["recall"]}' |
| 142 | + ) |
| 143 | + |
| 144 | + assert metrics["roc_auc"] >= 0.5, "ROC-AUC < 0.5" |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + unittest.main() |
0 commit comments