tra-algorithm 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tests/__init__.py ADDED
File without changes
tests/test_core.py ADDED
@@ -0,0 +1,145 @@
1
+ """
2
+ Unit tests for TRA algorithm core functionality.
3
+ """
4
+ import unittest
5
+ import numpy as np
6
+ from sklearn.datasets import make_classification, make_regression
7
+ from sklearn.model_selection import train_test_split
8
+
9
+ from tra_algorithm.core import OptimizedTRA
10
+
11
+
12
+ class TestOptimizedTRA(unittest.TestCase):
13
+ """Test cases for OptimizedTRA class."""
14
+
15
+ def setUp(self):
16
+ """Set up test fixtures."""
17
+ # Classification dataset
18
+ self.X_clf, self.y_clf = make_classification(
19
+ n_samples=200, n_features=10, n_classes=3, n_informative=3, random_state=42
20
+ )
21
+ self.X_train_clf, self.X_test_clf, self.y_train_clf, self.y_test_clf = \
22
+ train_test_split(self.X_clf, self.y_clf, test_size=0.3, random_state=42)
23
+
24
+ # Regression dataset
25
+ self.X_reg, self.y_reg = make_regression(
26
+ n_samples=200, n_features=10, random_state=42
27
+ )
28
+ self.X_train_reg, self.X_test_reg, self.y_train_reg, self.y_test_reg = \
29
+ train_test_split(self.X_reg, self.y_reg, test_size=0.3, random_state=42)
30
+
31
+ def test_classification_basic(self):
32
+ """Test basic classification functionality."""
33
+ tra = OptimizedTRA(task_type="classification", n_tracks=3, random_state=42)
34
+ tra.fit(self.X_train_clf, self.y_train_clf)
35
+
36
+ # Test predictions
37
+ y_pred = tra.predict(self.X_test_clf)
38
+ self.assertEqual(len(y_pred), len(self.y_test_clf))
39
+
40
+ # Test probability predictions
41
+ y_proba = tra.predict_proba(self.X_test_clf)
42
+ self.assertEqual(y_proba.shape[0], len(self.y_test_clf))
43
+ self.assertEqual(y_proba.shape[1], len(np.unique(self.y_clf)))
44
+
45
+ # Test scoring
46
+ score = tra.score(self.X_test_clf, self.y_test_clf)
47
+ self.assertIsInstance(score, float)
48
+ self.assertGreaterEqual(score, 0.0)
49
+ self.assertLessEqual(score, 1.0)
50
+
51
+ def test_regression_basic(self):
52
+ """Test basic regression functionality."""
53
+ tra = OptimizedTRA(task_type="regression", n_tracks=3, random_state=42)
54
+ tra.fit(self.X_train_reg, self.y_train_reg)
55
+
56
+ # Test predictions
57
+ y_pred = tra.predict(self.X_test_reg)
58
+ self.assertEqual(len(y_pred), len(self.y_test_reg))
59
+
60
+ # Test scoring (negative MSE)
61
+ score = tra.score(self.X_test_reg, self.y_test_reg)
62
+ self.assertIsInstance(score, float)
63
+ self.assertLessEqual(score, 0.0) # Negative MSE
64
+
65
+ def test_invalid_task_type(self):
66
+ """Test invalid task type handling."""
67
+ with self.assertRaises(ValueError):
68
+ tra = OptimizedTRA(task_type="invalid")
69
+ tra.fit(self.X_train_clf, self.y_train_clf)
70
+
71
+ def test_predict_proba_regression_error(self):
72
+ """Test that predict_proba raises error for regression."""
73
+ tra = OptimizedTRA(task_type="regression", random_state=42)
74
+ tra.fit(self.X_train_reg, self.y_train_reg)
75
+
76
+ with self.assertRaises(ValueError):
77
+ tra.predict_proba(self.X_test_reg)
78
+
79
+ def test_feature_selection(self):
80
+ """Test feature selection functionality."""
81
+ tra = OptimizedTRA(
82
+ task_type="classification",
83
+ feature_selection=True,
84
+ random_state=42
85
+ )
86
+ tra.fit(self.X_train_clf, self.y_train_clf)
87
+
88
+ # Check that feature selector was created
89
+ self.assertIsNotNone(tra.feature_selector_)
90
+
91
+ # Test predictions work with feature selection
92
+ y_pred = tra.predict(self.X_test_clf)
93
+ self.assertEqual(len(y_pred), len(self.y_test_clf))
94
+
95
+ def test_statistics_generation(self):
96
+ """Test statistics generation."""
97
+ tra = OptimizedTRA(task_type="classification", random_state=42)
98
+ tra.fit(self.X_train_clf, self.y_train_clf)
99
+ tra.predict(self.X_test_clf[:10]) # Make some predictions
100
+
101
+ stats = tra.get_track_statistics()
102
+
103
+ # Check required keys
104
+ required_keys = ['n_tracks', 'n_signals', 'total_predictions', 'track_details']
105
+ for key in required_keys:
106
+ self.assertIn(key, stats)
107
+
108
+ # Check track details
109
+ self.assertGreater(stats['n_tracks'], 0)
110
+ self.assertGreaterEqual(stats['total_predictions'], 0)
111
+
112
+ def test_performance_report(self):
113
+ """Test performance report generation."""
114
+ tra = OptimizedTRA(task_type="classification", random_state=42)
115
+ tra.fit(self.X_train_clf, self.y_train_clf)
116
+
117
+ report = tra.get_performance_report()
118
+ self.assertIsInstance(report, str)
119
+ self.assertIn("PERFORMANCE REPORT", report)
120
+ self.assertIn("TRACK PERFORMANCE DETAILS", report)
121
+
122
+
123
+ class TestUtils(unittest.TestCase):
124
+ """Test utility functions."""
125
+
126
+ def test_example_dataset_creation(self):
127
+ """Test example dataset creation."""
128
+ # Classification dataset
129
+ X_clf, y_clf = OptimizedTRA.create_example_dataset(
130
+ task_type="classification", n_samples=100, n_features=5
131
+ )
132
+ self.assertEqual(X_clf.shape, (100, 5))
133
+ self.assertEqual(len(y_clf), 100)
134
+ self.assertGreater(len(np.unique(y_clf)), 1)
135
+
136
+ # Regression dataset
137
+ X_reg, y_reg = OptimizedTRA.create_example_dataset(
138
+ task_type="regression", n_samples=150, n_features=8
139
+ )
140
+ self.assertEqual(X_reg.shape, (150, 8))
141
+ self.assertEqual(len(y_reg), 150)
142
+
143
+
144
+ if __name__ == '__main__':
145
+ unittest.main()
tests/test_utils.py ADDED
@@ -0,0 +1,80 @@
1
+ """
2
+ Unit tests for utility functions.
3
+ """
4
+ import unittest
5
+ import numpy as np
6
+ import tempfile
7
+ import os
8
+
9
+ from tra_algorithm.core import OptimizedTRA
10
+ from tra_algorithm.utils import validate_input, format_performance_metrics
11
+
12
+
13
+ class TestUtilityFunctions(unittest.TestCase):
14
+ """Test utility functions."""
15
+
16
+ def test_validate_input(self):
17
+ """Test input validation function."""
18
+ # Valid inputs
19
+ X_valid = np.array([[1, 2], [3, 4], [5, 6]])
20
+ y_valid = np.array([0, 1, 0])
21
+
22
+ X_validated, y_validated = validate_input(X_valid, y_valid)
23
+ np.testing.assert_array_equal(X_validated, X_valid)
24
+ np.testing.assert_array_equal(y_validated, y_valid)
25
+
26
+ # Invalid inputs
27
+ with self.assertRaises(ValueError):
28
+ validate_input(None, y_valid)
29
+
30
+ with self.assertRaises(ValueError):
31
+ validate_input(X_valid, None)
32
+
33
+ # Mismatched shapes
34
+ X_mismatch = np.array([[1, 2]])
35
+ with self.assertRaises(ValueError):
36
+ validate_input(X_mismatch, y_valid)
37
+
38
+ def test_format_performance_metrics(self):
39
+ """Test performance metrics formatting."""
40
+ metrics = {
41
+ 'accuracy': 0.8567,
42
+ 'f1_score': 0.7234,
43
+ 'precision': 0.9012
44
+ }
45
+
46
+ formatted = format_performance_metrics(metrics)
47
+ self.assertIsInstance(formatted, str)
48
+ self.assertIn('accuracy', formatted.lower())
49
+ self.assertIn('0.857', formatted) # Rounded to 3 decimal places
50
+
51
+ def test_model_save_load_integration(self):
52
+ """Test model save/load functionality."""
53
+ # Create and train a simple model
54
+ X, y = OptimizedTRA.create_example_dataset(
55
+ task_type="classification", n_samples=50
56
+ )
57
+
58
+ tra = OptimizedTRA(task_type="classification", n_tracks=2, random_state=42)
59
+ tra.fit(X, y)
60
+
61
+ # Test saving and loading (Windows-safe)
62
+ tmp = tempfile.NamedTemporaryFile(suffix='.joblib', delete=False)
63
+ tmp_path = tmp.name
64
+ tmp.close() # Close so joblib can use it
65
+ try:
66
+ tra.save_model(tmp_path)
67
+ loaded_tra = OptimizedTRA.load_model(tmp_path)
68
+
69
+ # Test that loaded model works
70
+ y_pred_original = tra.predict(X[:10])
71
+ y_pred_loaded = loaded_tra.predict(X[:10])
72
+
73
+ np.testing.assert_array_equal(y_pred_original, y_pred_loaded)
74
+
75
+ finally:
76
+ os.unlink(tmp_path)
77
+
78
+
79
+ if __name__ == '__main__':
80
+ unittest.main()
@@ -0,0 +1,98 @@
1
+ """
2
+ TRA Algorithm Package
3
+ ====================
4
+
5
+ Track/Rail Algorithm (TRA) - A novel machine learning algorithm for dynamic model selection.
6
+
7
+ The TRA algorithm uses multiple "tracks" (models) and "signals" (switching conditions) to
8
+ dynamically route data through the most appropriate model, providing improved performance
9
+ for both classification and regression tasks.
10
+
11
+ Example Usage:
12
+ -------------
13
+ from tra_algorithm import OptimizedTRA
14
+ from sklearn.datasets import make_classification
15
+ from sklearn.model_selection import train_test_split
16
+
17
+ # Create dataset
18
+ X, y = make_classification(n_samples=1000, n_features=20, n_classes=3)
19
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
20
+
21
+ # Initialize and train TRA
22
+ tra = OptimizedTRA(task_type="classification", n_tracks=3)
23
+ tra.fit(X_train, y_train)
24
+
25
+ # Make predictions
26
+ y_pred = tra.predict(X_test)
27
+
28
+ # Get performance report
29
+ print(tra.get_performance_report())
30
+
31
+ Classes:
32
+ --------
33
+ OptimizedTRA: Main TRA algorithm implementation
34
+ Track: Individual model track
35
+ Signal: Track switching signal
36
+ Record: Data record for tracking
37
+ EnhancedSignalCondition: Advanced signal logic
38
+
39
+ Utilities:
40
+ ----------
41
+ create_example_dataset: Generate sample datasets for testing
42
+ evaluate_model_performance: Model evaluation utilities
43
+ plot_learning_curves: Learning curve visualization
44
+ compare_with_baselines: Baseline model comparison
45
+
46
+ Examples:
47
+ ---------
48
+ basic_classification_example, basic_regression_example, model_comparison_example
49
+ """
50
+
51
+ from .version import __version__
52
+ from .core import (
53
+ OptimizedTRA,
54
+ Track,
55
+ Signal,
56
+ Record,
57
+ EnhancedSignalCondition
58
+ )
59
+ from .utils import (
60
+ create_example_dataset,
61
+ evaluate_model_performance,
62
+ plot_learning_curves,
63
+ compare_with_baselines
64
+ )
65
+ from .examples import (
66
+ basic_classification_example,
67
+ basic_regression_example,
68
+ model_comparison_example
69
+ )
70
+
71
+ __all__ = [
72
+ # Main classes
73
+ 'OptimizedTRA',
74
+ 'Track',
75
+ 'Signal',
76
+ 'Record',
77
+ 'EnhancedSignalCondition',
78
+
79
+ # Utilities
80
+ 'create_example_dataset',
81
+ 'evaluate_model_performance',
82
+ 'plot_learning_curves',
83
+ 'compare_with_baselines',
84
+
85
+ # Examples
86
+ 'basic_classification_example',
87
+ 'basic_regression_example',
88
+ 'model_comparison_example',
89
+
90
+ # Version
91
+ '__version__',
92
+ ]
93
+
94
+ # Package metadata
95
+ __author__ = "TRA Algorithm Team"
96
+ __email__ = "contact@tra-algorithm.com"
97
+ __license__ = "MIT"
98
+ __description__ = "Track/Rail Algorithm (TRA) - A novel machine learning algorithm for dynamic model selection"