|
| 1 | +""" |
| 2 | +Unit tests for the AFL.double_agent.PipelineOp module. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import numpy as np |
| 7 | +import xarray as xr |
| 8 | +import json |
| 9 | +import os |
| 10 | + |
| 11 | +from tests.utils import MockPipelineOp |
| 12 | +from AFL.double_agent import TreePipeline as tp |
| 13 | +from AFL.double_agent import (Pipeline, LogLogTransform) |
| 14 | +from sklearn.svm import SVC |
| 15 | +from AFL.double_agent.data import ( |
| 16 | + get_data_dir, |
| 17 | + list_datasets, |
| 18 | + load_dataset, |
| 19 | + example_dataset1, |
| 20 | +) |
| 21 | +from TreeHierarchy import ( |
| 22 | + TreeHierarchy, |
| 23 | + json_decoder |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.unit |
| 28 | +class TestClassificationPipeline: |
| 29 | + """Tests for the PipelineOp class.""" |
| 30 | + def test_classifier_creation(self): |
| 31 | + data = load_dataset("example_classification_data") |
| 32 | + classification_def = json.loads(open(os.path.join(get_data_dir(), "example_tree_structure.json"), 'r').read()) |
| 33 | + with Pipeline() as P: |
| 34 | + LogLogTransform("SAS_curves", "log_sas_curves") |
| 35 | + pipe = tp.ClassificationPipeline("SAS_curves", "predicted_labels", classification_def) |
| 36 | + assert isinstance(pipe, tp.ClassificationPipeline) |
| 37 | + assert isinstance(pipe.classifier, TreeHierarchy) |
| 38 | + assert isinstance(pipe.classifier.left, TreeHierarchy) |
| 39 | + assert isinstance(pipe.classifier.right, TreeHierarchy) |
| 40 | + assert isinstance(pipe.classifier.left.left, TreeHierarchy) |
| 41 | + assert isinstance(pipe.classifier.left.right, TreeHierarchy) |
| 42 | + assert isinstance(pipe.classifier.right.left, TreeHierarchy) |
| 43 | + assert isinstance(pipe.classifier.right.right, TreeHierarchy) |
| 44 | + assert isinstance(pipe.classifier.entity, SVC) |
| 45 | + assert isinstance(pipe.classifier.left.entity, SVC) |
| 46 | + assert isinstance(pipe.classifier.right.entity, SVC) |
| 47 | + |
| 48 | +@pytest.mark.unit |
| 49 | +class TestClassificationPipelineLoaded: |
| 50 | + """Tests for the PipelineOp class.""" |
| 51 | + def test_classifier_load(self): |
| 52 | +### data = load_dataset("classification_data") |
| 53 | +### classification_def = json.loads(open(os.path.join(get_data_dir(), "classification_tree.json"), 'r').read()) |
| 54 | +### pipe = tp.ClassificationPipeline("log_sas_curves", "predicted_labels", classification_def) |
| 55 | + save_path = os.path.join(get_data_dir(), "classification_pipeline.json") |
| 56 | + with Pipeline.read_json(str(save_path)) as P: |
| 57 | + assert isinstance(P[1], tp.ClassificationPipeline) |
| 58 | + assert isinstance(P[1].classifier, TreeHierarchy) |
| 59 | + assert isinstance(P[1].classifier.left, TreeHierarchy) |
| 60 | + assert isinstance(P[1].classifier.right, TreeHierarchy) |
| 61 | + assert isinstance(P[1].classifier.left.left, TreeHierarchy) |
| 62 | + assert isinstance(P[1].classifier.left.right, TreeHierarchy) |
| 63 | + assert isinstance(P[1].classifier.right.left, TreeHierarchy) |
| 64 | + assert isinstance(P[1].classifier.right.right, TreeHierarchy) |
| 65 | + assert isinstance(P[1].classifier.entity, SVC) |
| 66 | + assert isinstance(P[1].classifier.left.entity, SVC) |
| 67 | + assert isinstance(P[1].classifier.right.entity, SVC) |
| 68 | + |
| 69 | +@pytest.mark.unit |
| 70 | +class TestClassificationPipelinePerformance: |
| 71 | + """Tests for the PipelineOp class.""" |
| 72 | + def test_classifier_load(self): |
| 73 | +### data = load_dataset("classification_data") |
| 74 | +### classification_def = json.loads(open(os.path.join(get_data_dir(), "classification_tree.json"), 'r').read()) |
| 75 | +### pipe = tp.ClassificationPipeline("log_sas_curves", "predicted_labels", classification_def) |
| 76 | + save_path = os.path.join(get_data_dir(), "classification_pipeline.json") |
| 77 | + data = load_dataset("example_classification_data") |
| 78 | + ref = load_dataset("reference_predictions") |
| 79 | + with Pipeline.read_json(str(save_path)) as P: |
| 80 | + out = P.calculate(data) |
| 81 | + print(P[0].output_variable) |
| 82 | + np.testing.assert_array_equal(out["predicted_test_labels"].data, ref["reference_predictions"].data) |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
0 commit comments