Skip to content

Commit e57af59

Browse files
committed
added test for single ensemble_model
1 parent 98f1eaf commit e57af59

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/progpy/ensemble_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class EnsembleModel(PrognosticsModel):
3535
def __init__(self, models: list, **kwargs):
3636
if not isinstance(models, Sequence):
3737
raise TypeError(f'EnsembleModel must be initialized with a list of models, got {type(models)}')
38-
if len(models) < 2:
39-
raise ValueError('EnsembleModel requires at least two models')
38+
if len(models) < 1:
39+
raise ValueError('EnsembleModel requires at least one model')
4040
for i, m in enumerate(models):
4141
if not isinstance(m, PrognosticsModel):
4242
raise TypeError(f'EnsembleModel requires all models to be PrognosticsModel instances. models[{i}] was {type(m)}')

tests/test_ensemble.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def test_single_model(self):
2727

2828
m = OneInputOneOutputOneEventLM()
2929

30-
with self.assertRaises(ValueError):
31-
EnsembleModel([m])
30+
31+
EnsembleModel([m])
3232

3333
def test_wrong_type(self):
3434
# An ensemble model with a non-model should raise an exception
@@ -43,6 +43,48 @@ def test_wrong_type(self):
4343
with self.assertRaises(TypeError):
4444
EnsembleModel([m, m, m, 77])
4545

46+
def test_single_model(self):
47+
"""
48+
This tests that the ensemble model works with a single model, ensuring that inputs, states, outputs, and events are correctly handled.
49+
"""
50+
m = OneInputOneOutputOneEventLM()
51+
em = EnsembleModel([m])
52+
53+
# inputs, states, outputs, and events should be the same as the single model
54+
self.assertSetEqual(set(em.inputs), {'u1'})
55+
self.assertSetEqual(set(em.states), {'x1'})
56+
self.assertSetEqual(set(em.outputs), {'z1'})
57+
self.assertSetEqual(set(em.events), {'x1 == 10'})
58+
59+
# Initialize
60+
x_t0 = em.initialize()
61+
self.assertEqual(x_t0['x1'], 0)
62+
63+
# State transition
64+
u = em.InputContainer({'u1': 1})
65+
x_t1 = em.next_state(x_t0, u, 1)
66+
self.assertEqual(x_t1['x1'], 1)
67+
68+
# Output
69+
z = em.output(x_t1)
70+
self.assertEqual(z['z1'], 1)
71+
72+
# Event state
73+
es = em.event_state(x_t1)
74+
self.assertEqual(es['x1 == 10'], 0.9)
75+
76+
# Threshold met
77+
self.assertFalse(em.threshold_met(x_t1)['x1 == 10'])
78+
79+
# Transition again
80+
x_t2 = em.next_state(x_t1, u, 2)
81+
82+
# Threshold met
83+
# x1 == 3
84+
self.assertFalse(em.threshold_met(x_t2)['x1 == 10'])
85+
86+
87+
4688
def test_two_models_identical(self):
4789
"""
4890
This tests that the ensemble model works with two identical model-types with identical states, inputs, etc., one with slightly altered parameters.

0 commit comments

Comments
 (0)