• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SMTorg / smt-optim / 28806559135

06 Jul 2026 04:23PM UTC coverage: 87.169% (+0.6%) from 86.529%
28806559135

push

github

web-flow
  Fix: Resolve RSCV constraint bug, correct variance margins for relaxation, and fix CI Coveralls (#21)

109 of 110 new or added lines in 4 files covered. (99.09%)

360 existing lines in 43 files now uncovered.

3757 of 4310 relevant lines covered (87.17%)

1.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.12
/src/tests/test_unit/test_sample.py
1
import unittest
2✔
2
import numpy as np
2✔
3

4
from smt_optim.core import Sample, OptimizationDataset
2✔
5

6
from smt_optim.core.sample import sample_func
2✔
7

8
class TestSample(unittest.TestCase):
2✔
9

10
    def test_sample_initialization(self):
2✔
11
        x = np.array([1.0, 2.0])
2✔
12
        obj = np.array([0.5, 1.5])
2✔
13
        cstr = np.array([0.0])
2✔
14
        eval_time = np.array([0.01, 0.02, 0.03])
2✔
15

16
        s = Sample(
2✔
17
            x=x,
18
            fidelity=0,
19
            obj=obj,
20
            cstr=cstr,
21
            eval_time=eval_time,
22
        )
23

24
        self.assertTrue(np.array_equal(s.x, x))
2✔
25
        self.assertEqual(s.fidelity, 0)
2✔
26
        self.assertTrue(np.array_equal(s.obj, obj))
2✔
27
        self.assertTrue(np.array_equal(s.cstr, cstr))
2✔
28
        self.assertTrue(np.array_equal(s.eval_time, eval_time))
2✔
29
        self.assertIsInstance(s.metadata, dict)
2✔
30

31

32
class TestOptimizationDataset(unittest.TestCase):
2✔
33

34
    def setUp(self):
2✔
35
        self.dataset = OptimizationDataset()
2✔
36

37
        # Two objectives, one constraint
38
        self.s1 = Sample(
2✔
39
            x=np.array([0.0, 1.0]),
40
            fidelity=0,
41
            obj=np.array([1.0, 2.0]),
42
            cstr=np.array([10.0]),
43
            eval_time=None,
44
        )
45

46
        self.s2 = Sample(
2✔
47
            x=np.array([2.0, 3.0]),
48
            fidelity=0,
49
            obj=np.array([3.0, 4.0]),
50
            cstr=np.array([20.0]),
51
            eval_time=None,
52
        )
53

54
        self.s3 = Sample(
2✔
55
            x=np.array([4.0, 5.0]),
56
            fidelity=1,
57
            obj=np.array([5.0, 6.0]),
58
            cstr=np.array([30.0]),
59
            eval_time=None,
60
        )
61

62
        self.s4 = Sample(
2✔
63
            x=np.array([4.1, 5.1]),
64
            fidelity=1,
65
            obj=np.array([5.0, 6.0]),
66
            cstr=np.array([30.0]),
67
            eval_time=None,
68
            metadata={
69
                "scalar": np.pi,
70
                "array": np.full((2,), np.pi),
71
                "alpha": "word",
72
            }
73
        )
74

75

76
    def test_add_initializes_dimensions(self):
2✔
77
        self.dataset.add(self.s1)
2✔
78

79
        self.assertEqual(self.dataset.num_obj, 2)
2✔
80
        self.assertEqual(self.dataset.num_cstr, 1)
2✔
81
        self.assertEqual(self.dataset.num_fidelity, 1)
2✔
82
        self.assertIn(0, self.dataset.fidelities)
2✔
83

84
    def test_add_dimension_mismatch_raises(self):
2✔
85
        self.dataset.add(self.s1)
2✔
86

87
        bad_sample = Sample(
2✔
88
            x=np.array([1.0, 2.0]),
89
            fidelity=0,
90
            obj=np.array([1.0]),   # wrong size
91
            cstr=np.array([10.0]),
92
            eval_time=None,
93
        )
94

95
        with self.assertRaises(Exception):
2✔
96
            self.dataset.add(bad_sample)
2✔
97

98
    def test_add_multiple_fidelities(self):
2✔
99
        self.dataset.add(self.s1)
2✔
100
        self.dataset.add(self.s3)
2✔
101

102
        self.assertEqual(self.dataset.num_fidelity, 2)
2✔
103
        self.assertCountEqual(self.dataset.fidelities, [0, 1])
2✔
104

105

106
    def test_get_by_fidelity(self):
2✔
107
        self.dataset.add(self.s1)
2✔
108
        self.dataset.add(self.s2)
2✔
109
        self.dataset.add(self.s3)
2✔
110

111
        lvl0 = self.dataset.get_by_fidelity(0)
2✔
112
        lvl1 = self.dataset.get_by_fidelity(1)
2✔
113

114
        self.assertEqual(len(lvl0), 2)
2✔
115
        self.assertEqual(len(lvl1), 1)
2✔
116
        self.assertTrue(all(s.fidelity == 0 for s in lvl0))
2✔
117
        self.assertTrue(all(s.fidelity == 1 for s in lvl1))
2✔
118

119

120
    def test_export_single_objective(self):
2✔
121
        self.dataset.add(self.s1)
2✔
122
        self.dataset.add(self.s2)
2✔
123

124
        result = self.dataset.export_data(idx=0, lvl=0)
2✔
125

126
        expected = np.array([
2✔
127
            [1.0],
128
            [3.0],
129
        ])
130

131
        self.assertTrue(np.array_equal(result, expected))
2✔
132

133
    def test_export_multiple_objectives(self):
2✔
134
        self.dataset.add(self.s1)
2✔
135
        self.dataset.add(self.s2)
2✔
136

137
        result = self.dataset.export_data(idx=[0, 1], lvl=0)
2✔
138

139
        expected = np.array([
2✔
140
            [1.0, 2.0],
141
            [3.0, 4.0],
142
        ])
143

144
        self.assertTrue(np.array_equal(result, expected))
2✔
145

146
    def test_export_constraint(self):
2✔
147
        self.dataset.add(self.s1)
2✔
148
        self.dataset.add(self.s2)
2✔
149

150
        # constraint index is num_obj + 0 = 2
151
        result = self.dataset.export_data(idx=2, lvl=0)
2✔
152

153
        expected = np.array([
2✔
154
            [10.0],
155
            [20.0],
156
        ])
157

158
        self.assertTrue(np.array_equal(result, expected))
2✔
159

160
    def test_export_mixed_obj_and_constraint(self):
2✔
161
        self.dataset.add(self.s1)
2✔
162
        self.dataset.add(self.s2)
2✔
163

164
        # objective 1 and constraint 0
165
        result = self.dataset.export_data(idx=[1, 2], lvl=0)
2✔
166

167
        expected = np.array([
2✔
168
            [2.0, 10.0],
169
            [4.0, 20.0],
170
        ])
171

172
        self.assertTrue(np.array_equal(result, expected))
2✔
173

174
    def test_export_empty_fidelity(self):
2✔
175
        self.dataset.add(self.s1)
2✔
176

177
        result = self.dataset.export_data(idx=0, lvl=999)
2✔
178

179
        self.assertEqual(result.shape, (0,))
2✔
180
        self.assertEqual(result.size, 0)
2✔
181

182

183
    def test_export_as_dict(self):
2✔
184

185
        self.dataset.add(self.s1)
2✔
186
        self.dataset.add(self.s2)
2✔
187
        self.dataset.add(self.s3)
2✔
188
        self.dataset.add(self.s4)
2✔
189

190
        data_as_dict = self.dataset.export_as_dict()
2✔
191

192

193
        keys = set(data_as_dict.keys())
2✔
194

195
        self.assertIn("scalar", keys)
2✔
196
        # test that there is a single scalar value in data_as_dict["scalar"],
197
        # all other values in the array should be np.nan
198
        self.assertEqual(data_as_dict["scalar"].shape, (4,))
2✔
199
        finite_idx = np.where(~np.isnan(data_as_dict["scalar"]))[0]
2✔
200
        self.assertEqual(len(finite_idx), 1)
2✔
201
        self.assertEqual(finite_idx[0], 3)
2✔
202
        self.assertEqual(data_as_dict["scalar"][3], self.s4.metadata["scalar"])
2✔
203

204
        self.assertIn("array", keys)
2✔
205
        # test that the dimension of data_as_dict["array"], should be (4, 2)
206
        self.assertEqual(data_as_dict["array"].shape, (4, 2))
2✔
207
        self.assertTrue(np.all(np.isnan(data_as_dict["array"][:3, :])))
2✔
208
        # test that only the last row is numerical, all the other value should be np.nan
209
        np.testing.assert_array_equal(
2✔
210
            data_as_dict["array"][3, :],
211
            self.s4.metadata["array"]
212
        )
213

214
        self.assertNotIn("alpha", keys)
2✔
215

216

217

218
class TestEvaluator(unittest.TestCase):
2✔
219

220

221
    def test_float_output(self):
2✔
222

223
        def float_output(x):
2✔
224
            return 0.0
2✔
225

226
        value, time = sample_func(np.array([0.0]), float_output)
2✔
227

228
        self.assertEqual(value, 0.0)
2✔
229

230

231
    def test_array_output(self):
2✔
232

233
        def array_1d_output(x):
2✔
234
            return np.array([0.0])
2✔
235

236
        value, time = sample_func(np.array([0.0]), array_1d_output)
2✔
237
        self.assertEqual(value, 0.0)
2✔
238

239
        def array_2d_output(x):
2✔
240
            return np.array([[0.0]])
2✔
241

242
        value, time = sample_func(np.array([0.0]), array_2d_output)
2✔
243
        self.assertEqual(value, 0.0)
2✔
244

245

246

247
if __name__ == "__main__":
2✔
UNCOV
248
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc