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

tonegas / nnodely / 12162489906

04 Dec 2024 03:04PM UTC coverage: 93.934% (-0.2%) from 94.155%
12162489906

push

github

web-flow
Merge pull request #6 from tonegas/release/0.15.0

Release/0.15.0

37 of 42 new or added lines in 8 files covered. (88.1%)

59 existing lines in 7 files now uncovered.

8625 of 9182 relevant lines covered (93.93%)

0.94 hits per line

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

98.87
/tests/test_model_predict_recurrent.py
1
import unittest, sys, os, torch
1✔
2
import numpy as np
1✔
3

4
from nnodely import *
1✔
5
from nnodely.relation import NeuObj
1✔
6
from nnodely import relation
1✔
7
relation.CHECK_NAMES = False
1✔
8

9
from nnodely.logger import logging, nnLogger
1✔
10
log = nnLogger(__name__, logging.CRITICAL)
1✔
11
log.setAllLevel(logging.CRITICAL)
1✔
12

13
sys.path.append(os.getcwd())
1✔
14

15
# 24 Tests
16
# This file test the model prediction when closed loop or connect are present in particular the output value
17
# Test the state variables and the connect_values
18

19
# Dimensions
20
# The first dimension must indicate the time dimension i.e. how many time samples I asked for
21
# The second dimension indicates the output time dimension for each sample.
22
# The third is the size of the signal
23

24
def myfun(x, P):
1✔
25
    out = x*P
×
26
    return out[:,1:,:]
×
27

28
def myfunsum(x, P):
1✔
29
    out = x + P
1✔
30
    return out
1✔
31

32
def matmul(x,y):
1✔
33
    import torch
1✔
34
    return torch.matmul(torch.transpose(x,1,2),y)
1✔
35

36
# def myfun2(a, b ,c):
37
#     import torch
38
#     return torch.sin(a + b) * c
39
#
40
# def myfun3(a, b, p1, p2):
41
#     import torch
42
#     at = torch.transpose(a[:, :, 0:2],1,2)
43
#     bt = torch.transpose(b, 1, 2)
44
#     return torch.matmul(p1,at+bt)+p2.t()
45

46
class ModelyRecurrentPredictTest(unittest.TestCase):
1✔
47
    
48
    def TestAlmostEqual(self, data1, data2, precision=4):
1✔
49
        assert np.asarray(data1, dtype=np.float32).ndim == np.asarray(data2, dtype=np.float32).ndim, f'Inputs must have the same dimension! Received {type(data1)} and {type(data2)}'
×
NEW
50
        if type(data1) == type(data2) == list:
×
NEW
51
            self.assertEqual(len(data1), len(data2))
×
52
            for pred, label in zip(data1, data2):
×
53
                self.TestAlmostEqual(pred, label, precision=precision)
×
54
        else:
55
            self.assertAlmostEqual(data1, data2, places=precision)
×
56

57
    def test_predict_and_states_values_fir_simple_closed_loop(self):
1✔
58
        x = Input('x')
1✔
59
        x_state = State('x_state')
1✔
60
        p = Parameter('p', dimensions=1, sw=1, values=[[1.0]])
1✔
61
        rel_x = Fir(parameter=p)(x_state.last())
1✔
62
        rel_x = ClosedLoop(rel_x, x_state)
1✔
63
        out = Output('out', rel_x)
1✔
64

65
        test = Modely(visualizer=None, seed=42)
1✔
66
        test.addModel('out',out)
1✔
67
        test.addMinimize('pos_x', x.next(), out)
1✔
68
        test.neuralizeModel(0.01)
1✔
69

70
        result = test(inputs={'x': [2], 'x_state':[1]})
1✔
71
        self.assertEqual(test.model.states['x_state'], torch.tensor(result['out']))
1✔
72
        self.assertEqual({'out': [1]}, result)
1✔
73
        result = test(inputs={'x': [2]})
1✔
74
        self.assertEqual(test.model.states['x_state'], torch.tensor(1.0))
1✔
75
        self.assertEqual({'out': [1.0]}, result)
1✔
76
        test.resetStates()
1✔
77
        result = test(inputs={'x': [2]})
1✔
78
        self.assertEqual(test.model.states['x_state'], torch.tensor(0.0))
1✔
79
        self.assertEqual({'out': [0.0]}, result)
1✔
80

81
    def test_predict_values_fir_simple_closed_loop_predict(self):
1✔
82
        x = Input('x')
1✔
83
        x_in = Input('x_in')
1✔
84
        p = Parameter('p', dimensions=1, sw=1, values=[[1.0]])
1✔
85
        rel_x = Fir(parameter=p)(x_in.last())
1✔
86
        #rel_x = ClosedLoop(rel_x, x_state)
87
        out = Output('out', rel_x)
1✔
88

89
        test = Modely(visualizer=None, seed=42)
1✔
90
        test.addModel('out',out)
1✔
91
        test.addMinimize('pos_x', x.next(), out)
1✔
92
        test.neuralizeModel(0.01)
1✔
93

94
        result = test(inputs={'x': [2], 'x_in':[1]},closed_loop={'x_in':'out'})
1✔
95
        self.assertEqual({'out':[1]}, result)
1✔
96
        result = test(inputs={'x': [2]}, closed_loop={'x_in':'out'})
1✔
97
        self.assertEqual({'out': [0.0]}, result)
1✔
98

99
    def test_predict_values_fir_closed_loop(self):
1✔
100
        ## the memory is not shared between different calls
101
        x = Input('x')
1✔
102
        F = State('F')
1✔
103
        p = Parameter('p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
104
        x_out = Fir(parameter=p)(x.tw(0.5))+F.last()
1✔
105
        out = Output('out',x_out)
1✔
106
        test = Modely(visualizer=None, seed=42)
1✔
107
        test.addClosedLoop(x_out,F)
1✔
108
        test.addModel('out',out)
1✔
109
        test.neuralizeModel(0.1)
1✔
110

111
        ## one sample prediction with F initialized with zeros
112
        test.resetStates()
1✔
113
        result = test(inputs={'x':[1,2,3,4,5]})
1✔
114
        self.assertEqual(result['out'], [15.0])
1✔
115
        ## 5 samples prediction with F initialized with zero only the first time
116
        test.resetStates()
1✔
117
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9]})
1✔
118
        self.assertEqual(result['out'], [15.0, 35.0, 60.0, 90.0, 125.0])
1✔
119
        ## one sample prediction with F initialized with [1]
120
        test.resetStates()
1✔
121
        result = test(inputs={'x':[1,2,3,4,5], 'F':[1]})
1✔
122
        self.assertEqual(result['out'], [16.0])
1✔
123
        ## 5 samples prediction with F initialized with [1] only the first time
124
        test.resetStates()
1✔
125
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'F':[1]})
1✔
126
        self.assertEqual(result['out'], [16.0, 36.0, 61.0, 91.0, 126.0])
1✔
127
        ## 5 samples prediction with F initialized with [1] the first time, [2] the second time and [3] the third time
128
        test.resetStates()
1✔
129
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'F':[1,2,3]})
1✔
130
        self.assertEqual(result['out'], [16.0, 22.0, 28.0, 58.0, 93.0])
1✔
131
        ## one sample prediction with F initialized with [1] (the other values are ignored)
132
        test.resetStates()
1✔
133
        result = test(inputs={'x':[1,2,3,4,5], 'F':[1,2,3]})
1✔
134
        self.assertEqual(result['out'], [16.0])
1✔
135

136
    def test_predict_values_fir_closed_loop_predict(self):
1✔
137
        ## the memory is not shared between different calls
138
        x = Input('x') 
1✔
139
        F = Input('F')
1✔
140
        p = Parameter('p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
141
        x_out = Fir(parameter=p)(x.tw(0.5))+F.last()
1✔
142
        out = Output('out',x_out)
1✔
143
        test = Modely(visualizer=None, seed=42)
1✔
144
        test.addModel('out',out)
1✔
145
        test.neuralizeModel(0.1)
1✔
146

147
        ## one sample prediction with F initialized with zeros
148
        result = test(inputs={'x':[1,2,3,4,5]}, closed_loop={'F':'out'})
1✔
149
        self.assertEqual(result['out'], [15.0])
1✔
150
        ## 5 samples prediction with F initialized with zero only the first time
151
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9]}, closed_loop={'F':'out'})
1✔
152
        self.assertEqual(result['out'], [15.0, 35.0, 60.0, 90.0, 125.0])
1✔
153
        ## one sample prediction with F initialized with [1]
154
        result = test(inputs={'x':[1,2,3,4,5], 'F':[1]}, closed_loop={'F':'out'})
1✔
155
        self.assertEqual(result['out'], [16.0])
1✔
156
        ## 5 samples prediction with F initialized with [1] only the first time
157
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'F':[1]}, closed_loop={'F':'out'})
1✔
158
        self.assertEqual(result['out'], [16.0, 36.0, 61.0, 91.0, 126.0])
1✔
159
        ## 5 samples prediction with F initialized with [1] the first time, [2] the second time and [3] the third time
160
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'F':[1,2,3]}, closed_loop={'F':'out'})
1✔
161
        self.assertEqual(result['out'], [16.0, 22.0, 28.0, 58.0, 93.0])
1✔
162
        ## one sample prediction with F initialized with [1] (the other values are ignored)
163
        result = test(inputs={'x':[1,2,3,4,5], 'F':[1,2,3]}, closed_loop={'F':'out'})
1✔
164
        self.assertEqual(result['out'], [16.0])
1✔
165

166
    def test_predict_values_2fir_closed_loop(self):
1✔
167
        ## the memory is not shared between different calls
168
        x = State('x')
1✔
169
        y = State('y')
1✔
170
        p = Parameter('p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
171
        n = Parameter('n', tw=0.5, dimensions=1, values=[[-1.0],[-1.0],[-1.0],[-1.0],[-1.0]])
1✔
172
        fir_pos = Fir(parameter=p)(x.tw(0.5))
1✔
173
        fir_neg = Fir(parameter=n)(y.tw(0.5))
1✔
174
        out_pos = Output('out_pos', fir_pos)
1✔
175
        out_neg = Output('out_neg', fir_neg)
1✔
176
        out = Output('out',fir_neg+fir_pos)
1✔
177
        test = Modely(visualizer=None, seed=42)
1✔
178
        test.addClosedLoop(fir_pos, x)
1✔
179
        test.addClosedLoop(fir_neg, y)
1✔
180
        test.addModel('out', out)
1✔
181
        test.addModel('out_pos',out_pos)
1✔
182
        test.addModel('out_neg',out_neg)
1✔
183
        test.neuralizeModel(0.1)
1✔
184

185
        ## one sample prediction with both close loops
186
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5]})
1✔
187
        self.assertEqual(result['out'], [0.0])
1✔
188
        self.assertEqual(result['out_pos'], [15.0])
1✔
189
        self.assertEqual(result['out_neg'], [-15.0])
1✔
190
        ## three sample prediction due to the max dimensions of inputs + prediction_samples
191
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5]}, prediction_samples=2, num_of_samples=3)
1✔
192
        self.assertEqual(result['out'], [0.0, 30.0, 58.0])
1✔
193
        self.assertEqual(result['out_pos'], [15.0, 29.0, 56.0])
1✔
194
        self.assertEqual(result['out_neg'], [-15.0, 1.0, 2.0])
1✔
195
        ## three sample prediction with both close loops but y gets initialized for 3 steps
196
        ## (!! since all the inputs are recurrent we must specify the prediction horizon (defualt=1))
197
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5,6,7]}, prediction_samples=2, num_of_samples=3)
1✔
198
        self.assertEqual(result['out'], [0.0, 30.0, 58.0])
1✔
199
        self.assertEqual(result['out_pos'], [15.0, 29.0, 56.0])
1✔
200
        self.assertEqual(result['out_neg'], [-15.0, 1.0, 2.0])
1✔
201

202
        test.resetStates()
1✔
203
        result = test(inputs={'x': [1, 2, 3, 4, 5], 'y': [1, 2, 3, 4, 5, 6, 7]}, num_of_samples=3)
1✔
204
        self.assertEqual(result['out'], [0.0, 9.0, 31.0])
1✔
205
        self.assertEqual(result['out_pos'], [15.0, 29.0, 56.0])
1✔
206
        self.assertEqual(result['out_neg'], [-15.0, -20.0, -25.0])
1✔
207

208
    def test_predict_values_2fir_closed_loop_predict(self):
1✔
209
        ## the memory is not shared between different calls
210
        x = Input('x') 
1✔
211
        y = Input('y')
1✔
212
        p = Parameter('p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
213
        n = Parameter('n', tw=0.5, dimensions=1, values=[[-1.0],[-1.0],[-1.0],[-1.0],[-1.0]])
1✔
214
        fir_pos = Fir(parameter=p)(x.tw(0.5))
1✔
215
        fir_neg = Fir(parameter=n)(y.tw(0.5))
1✔
216
        out_pos = Output('out_pos', fir_pos)
1✔
217
        out_neg = Output('out_neg', fir_neg)
1✔
218
        out = Output('out',fir_neg+fir_pos)
1✔
219
        test = Modely(visualizer=None, seed=42)
1✔
220
        test.addModel('out', out)
1✔
221
        test.addModel('out_pos',out_pos)
1✔
222
        test.addModel('out_neg',out_neg)
1✔
223
        test.neuralizeModel(0.1)
1✔
224

225
        ## two sample one prediction for x in close loop
226
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5,6]}, closed_loop={'x':'out_pos'})
1✔
227
        self.assertEqual(result['out'], [0.0, 9.0])
1✔
228
        self.assertEqual(result['out_pos'], [15.0, 29.0])
1✔
229
        self.assertEqual(result['out_neg'], [-15.0, -20.0])
1✔
230
        ## two sample one prediction for y in close loop
231
        result = test(inputs={'x':[1,2,3,4,5,6], 'y':[1,2,3,4,5]}, closed_loop={'y':'out_pos'})
1✔
232
        self.assertEqual(result['out'], [0.0, -9.0])
1✔
233
        self.assertEqual(result['out_pos'], [15.0, 20.0])
1✔
234
        self.assertEqual(result['out_neg'], [-15.0, -29.0])
1✔
235
        ## one sample prediction with both close loops
236
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5]}, closed_loop={'x':'out_pos', 'y':'out_neg'})
1✔
237
        self.assertEqual(result['out'], [0.0])
1✔
238
        self.assertEqual(result['out_pos'], [15.0])
1✔
239
        self.assertEqual(result['out_neg'], [-15.0])
1✔
240
        ## three sample prediction due to the max dimensions of inputs + prediction_samples
241
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5]}, closed_loop={'x':'out_pos', 'y':'out_neg'}, prediction_samples=2, num_of_samples=3)
1✔
242
        self.assertEqual(result['out'], [0.0, 30.0, 58.0])
1✔
243
        self.assertEqual(result['out_pos'], [15.0, 29.0, 56.0])
1✔
244
        self.assertEqual(result['out_neg'], [-15.0, 1.0, 2.0])
1✔
245
        ## three sample prediction with both close loops but y gets initialized for 3 steps
246
        ## (!! since all the inputs are recurrent we must specify the prediction horizon (defualt=1))
247
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5,6,7]}, closed_loop={'x':'out_pos', 'y':'out_neg'}, prediction_samples=2, num_of_samples=3)
1✔
248
        ## 1+2+3+4+5 -1-2-3-4-5 2+3+4+5+15 -2-3-4-5+15
249
        #self.assertEqual(result['out'], [0.0, 9.0, 31.0])
250
        self.assertEqual(result['out_pos'], [15.0, 29.0, 56.0])
1✔
251
        self.assertEqual(result['out_neg'], [-15.0, 1.0, 2.0])
1✔
252

253
    def test_predict_values_3states_closed_loop(self):
1✔
254
        ## the state is saved inside the model so the memory is shared between different calls
255
        x = Input('x') 
1✔
256
        F_state = State('F')
1✔
257
        y_state = State('y')
1✔
258
        z_state = State('z')
1✔
259
        p = Parameter('p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
260
        x_out = Fir(parameter=p)(x.tw(0.5))+F_state.last()+y_state.last()+z_state.last()
1✔
261
        x_out = ClosedLoop(x_out, F_state)
1✔
262
        x_out = ClosedLoop(x_out, y_state)
1✔
263
        x_out = ClosedLoop(x_out, z_state)
1✔
264
        out = Output('out',x_out)
1✔
265

266
        test = Modely(visualizer=None, seed=42)
1✔
267
        test.addModel('out',out)
1✔
268
        test.neuralizeModel(0.1)
1✔
269

270
        ## one sample prediction with state variables not initialized
271
        ## (they will have the last valid state)
272
        result = test(inputs={'x':[1,2,3,4,5]})
1✔
273
        self.assertEqual(result['out'], [15.0])
1✔
274
        ## 5 sample prediction with state variables not initialized
275
        ## (the first prediction will preserve the state of the previous test [15.0])
276
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9]})
1✔
277
        self.assertEqual(result['out'], [60.0, 200.0, 625.0, 1905.0, 5750.0])
1✔
278
        ## one sample prediction with state variables initialized with zero
279
        test.resetStates()
1✔
280
        result = test(inputs={'x':[1,2,3,4,5]})
1✔
281
        self.assertEqual(result['out'], [15.0])
1✔
282
        ## one sample prediction with F initialized with [1] and the others not initialized (so they will have 15.0 in the memory)
283
        result = test(inputs={'x':[1,2,3,4,5], 'F':[1]})
1✔
284
        self.assertEqual(result['out'], [46.0])
1✔
285
        ## one sample prediction with all the state variables initialized
286
        result = test(inputs={'x':[1,2,3,4,5], 'F':[1], 'y':[2], 'z':[3]})
1✔
287
        self.assertEqual(result['out'], [21.0])
1✔
288
        ## 5 samples prediction with state variables initialized as many times as they have values to take
289
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'F':[1,2,3], 'y':[2,3], 'z':[3]})
1✔
290
        self.assertEqual(result['out'], [21.0, 46.0, 120.0, 390.0, 1205.0])
1✔
291
        # 2 samples prediction with state variables inizialized only at %prediction_samples
292
        result = test(inputs={'F': [1,2,3,4], 'y': [1,2], 'z': [1,2,3,4,5]}, prediction_samples=2, num_of_samples=4)
1✔
293
        # 1+1+1 = 3, 3+3+3 = 9, 9+9+9 = 27, 4+0+4 = 8, 8+8+8 = 24
294
        self.assertEqual(result['out'], [3.0, 9.0, 27.0, 8.0])
1✔
295
        #self.assertEqual(result['out'], [3.0,9.0,27.0,8.0])
296
        #self.assertEqual(result['out'], [3.0, 6.0, 12.0, 20.0])
297

298
    def test_predict_values_3states_closed_loop_predict(self):
1✔
299
        ## the state is saved inside the model so the memory is shared between different calls
300
        x = Input('x')
1✔
301
        F_state = Input('F')
1✔
302
        y_state = Input('y')
1✔
303
        z_state = Input('z')
1✔
304
        p = Parameter('p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
305
        x_out = Fir(parameter=p)(x.tw(0.5))+F_state.last()+y_state.last()+z_state.last()
1✔
306
        out = Output('out',x_out)
1✔
307

308
        test = Modely(visualizer=None, seed=42)
1✔
309
        test.addModel('out',out)
1✔
310
        test.neuralizeModel(0.1)
1✔
311

312
        ## one sample prediction with state variables not initialized
313
        ## (they will have the last valid state)
314
        result = test(inputs={'x':[1,2,3,4,5]},closed_loop={'F':'out','x':'out','z':'out'})
1✔
315
        self.assertEqual(result['out'], [15.0])
1✔
316
        ## 5 sample prediction with state variables not initialized
317
        ## (the first prediction will preserve the state of the previous test [15.0])
318
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9]}, closed_loop={'F':'out','x':'out','z':'out'})
1✔
319
        #self.assertEqual(result['out'], [60.0, 200.0, 625.0, 1905.0, 5750.0]) #TODO
320
        ## one sample prediction with state variables initialized with zero
321
        test.resetStates()
1✔
322
        result = test(inputs={'x':[1,2,3,4,5]}, closed_loop={'F':'out','x':'out','z':'out'})
1✔
323
        self.assertEqual(result['out'], [15.0])
1✔
324
        ## one sample prediction with F initialized with [1] and the others not initialized (so they will have 15.0 in the memory)
325
        result = test(inputs={'x':[1,2,3,4,5], 'F':[1]}, closed_loop={'F':'out','x':'out','z':'out'})
1✔
326
        self.assertEqual(result['out'], [16.0])
1✔
327
        ## one sample prediction with all the state variables initialized
328
        result = test(inputs={'x':[1,2,3,4,5], 'F':[1], 'y':[2], 'z':[3]}, closed_loop={'F':'out','x':'out','z':'out'})
1✔
329
        self.assertEqual(result['out'], [21.0])
1✔
330
        ## 5 samples prediction with state variables initialized as many times as they have values to take
331
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'F':[1,2,3], 'y':[2,3], 'z':[3]}, closed_loop={'F':'out','x':'out','z':'out'})
1✔
332
        #self.assertEqual(result['out'], [21.0, 46.0, 120.0, 390.0, 1205.0])
333
        # 2 samples prediction with state variables inizialized only at %prediction_samples
334
        result = test(inputs={'F': [1,2,3,4], 'y': [1,2], 'z': [1,2,3,4,5]}, closed_loop={'F':'out','x':'out','z':'out'}, prediction_samples=2)
1✔
335
        # 1+1+1 = 3, 3+3+3 = 9, 9+9+9 = 27, 4+0+4 = 8, 8+8+8 = 24
336
        #self.assertEqual(result['out'], [3.0,9.0,27.0,8.0])
337
        #self.assertEqual(result['out'], [3.0, 6.0, 12.0, 20.0]) #TODO
338

339
    def test_predict_values_and_states_3states_more_window_closed_loop(self):
1✔
340
        ## the state is saved inside the model so the memory is shared between different calls
341
        x = Input('x') 
1✔
342
        y_state = State('y')
1✔
343
        z_state = State('z')
1✔
344
        x_p = Parameter('x_p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
345
        y_p = Parameter('y_p', tw=0.5, dimensions=1, values=[[2.0],[2.0],[2.0],[2.0],[2.0]])
1✔
346
        z_p = Parameter('z_p', tw=0.5, dimensions=1, values=[[3.0],[3.0],[3.0],[3.0],[3.0]])
1✔
347
        x_fir = Fir(parameter=x_p)(x.tw(0.5))
1✔
348
        y_fir = Fir(parameter=y_p)(y_state.tw(0.5))
1✔
349
        z_fir = Fir(parameter=z_p)(z_state.tw(0.5))
1✔
350
        y_fir = ClosedLoop(y_fir, y_state)
1✔
351
        z_fir = ClosedLoop(z_fir, z_state)
1✔
352
        out_x = Output('out_x', x_fir)
1✔
353
        out_y = Output('out_y', y_fir)
1✔
354
        out_z = Output('out_z', z_fir)
1✔
355
        out = Output('out',x_fir+y_fir+z_fir)
1✔
356

357
        test = Modely(visualizer=None, seed=42)
1✔
358
        test.addModel('out_all',[out, out_x, out_y, out_z])
1✔
359
        test.neuralizeModel(0.1)
1✔
360

361
        ## one sample prediction with state variables not initialized
362
        ## (they will have the last valid state)
363
        result = test(inputs={'x':[1,2,3,4,5]})
1✔
364
        self.assertEqual(result['out'], [15.0])
1✔
365
        self.assertEqual(result['out_x'], [15.0])
1✔
366
        self.assertEqual(result['out_y'], [0.0])
1✔
367
        self.assertEqual(result['out_z'], [0.0])
1✔
368
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
1✔
369
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
1✔
370
        ## 1 sample prediction with state variables all initialized
371
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5], 'z':[1,2,3,4,5]})
1✔
372
        self.assertEqual(result['out'], [90.0])
1✔
373
        self.assertEqual(result['out_x'], [15.0])
1✔
374
        self.assertEqual(result['out_y'], [30.0])
1✔
375
        self.assertEqual(result['out_z'], [45.0])
1✔
376
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[2.0], [3.0], [4.0], [5.0], [30.0]]])
1✔
377
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[2.0], [3.0], [4.0], [5.0], [45.0]]])
1✔
378
        ## clear state of y
379
        test.resetStates({'y'})
1✔
380
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
1✔
381
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[2.0], [3.0], [4.0], [5.0], [45.0]]])
1✔
382
        ## multi-sample prediction with states initialized as many times as they have values
383
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'y':[1,2,3,4,5,6,7], 'z':[1,2,3,4,5,6]})
1✔
384
        self.assertEqual(result['out'], [90.0, 120.0, 309.0, 1101.0, 4155.0])
1✔
385
        self.assertEqual(result['out_x'], [15.0, 20.0, 25.0, 30.0, 35.0])
1✔
386
        self.assertEqual(result['out_y'], [30.0, 40.0, 50.0, 144.0, 424.0])
1✔
387
        self.assertEqual(result['out_z'], [45.0, 60.0, 234.0, 927.0, 3696.0])
1✔
388
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[6.0], [7.0], [50.0], [144.0], [424.0]]])
1✔
389
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[6.0], [60.0], [234.0], [927.0], [3696.0]]])
1✔
390
        ## Clear all states
391
        test.resetStates()
1✔
392
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
1✔
393
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
1✔
394

395
    def test_predict_values_and_states_3states_more_window_closed_loop_predict(self):
1✔
396
        ## the state is saved inside the model so the memory is shared between different calls
397
        x = Input('x')
1✔
398
        y_state = Input('y')
1✔
399
        z_state = Input('z')
1✔
400
        x_p = Parameter('x_p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
401
        y_p = Parameter('y_p', tw=0.5, dimensions=1, values=[[2.0],[2.0],[2.0],[2.0],[2.0]])
1✔
402
        z_p = Parameter('z_p', tw=0.5, dimensions=1, values=[[3.0],[3.0],[3.0],[3.0],[3.0]])
1✔
403
        x_fir = Fir(parameter=x_p)(x.tw(0.5))
1✔
404
        y_fir = Fir(parameter=y_p)(y_state.tw(0.5))
1✔
405
        z_fir = Fir(parameter=z_p)(z_state.tw(0.5))
1✔
406
        out_x = Output('out_x', x_fir)
1✔
407
        out_y = Output('out_y', y_fir)
1✔
408
        out_z = Output('out_z', z_fir)
1✔
409
        out = Output('out',x_fir+y_fir+z_fir)
1✔
410

411
        test = Modely(visualizer=None, seed=42)
1✔
412
        test.addModel('out_all',[out, out_x, out_y, out_z])
1✔
413
        test.neuralizeModel(0.1)
1✔
414

415
        ## one sample prediction with state variables not initialized
416
        ## (they will have the last valid state)
417
        result = test(inputs={'x':[1,2,3,4,5]}, closed_loop={'y':'out_y', 'z':'out_z'})
1✔
418
        self.assertEqual(result['out'], [15.0])
1✔
419
        self.assertEqual(result['out_x'], [15.0])
1✔
420
        self.assertEqual(result['out_y'], [0.0])
1✔
421
        self.assertEqual(result['out_z'], [0.0])
1✔
422
        ## 1 sample prediction with state variables all initialized
423
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5], 'z':[1,2,3,4,5]}, closed_loop={'y':'out_y', 'z':'out_z'})
1✔
424
        self.assertEqual(result['out'], [90.0])
1✔
425
        self.assertEqual(result['out_x'], [15.0])
1✔
426
        self.assertEqual(result['out_y'], [30.0])
1✔
427
        self.assertEqual(result['out_z'], [45.0])
1✔
428
        ## multi-sample prediction with states initialized as many times as they have values
429
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'y':[1,2,3,4,5,6,7], 'z':[1,2,3,4,5,6]}, closed_loop={'y':'out_y', 'z':'out_z'})
1✔
430
        self.assertEqual(result['out'], [90.0, 120.0, 309.0, 1101.0, 4155.0])
1✔
431
        self.assertEqual(result['out_x'], [15.0, 20.0, 25.0, 30.0, 35.0])
1✔
432
        self.assertEqual(result['out_y'], [30.0, 40.0, 50.0, 144.0, 424.0])
1✔
433
        self.assertEqual(result['out_z'], [45.0, 60.0, 234.0, 927.0, 3696.0])
1✔
434

435
    def test_predict_values_and_states_2states_more_window_connect(self):
1✔
436
        ## the state is saved inside the model so the memory is shared between different calls
437
        x = Input('x') 
1✔
438
        y_state = State('y')
1✔
439
        z_state = State('z')
1✔
440
        x_p = Parameter('x_p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
441
        y_p = Parameter('y_p', tw=0.5, dimensions=1, values=[[2.0],[2.0],[2.0],[2.0],[2.0]])
1✔
442
        z_p = Parameter('z_p', tw=0.5, dimensions=1, values=[[3.0],[3.0],[3.0],[3.0],[3.0]])
1✔
443
        x_fir = Fir(parameter=x_p)(x.tw(0.5))
1✔
444
        y_fir = Fir(parameter=y_p)(y_state.tw(0.5))
1✔
445
        z_fir = Fir(parameter=z_p)(z_state.tw(0.5))
1✔
446
        x_fir = Connect(x_fir, y_state)
1✔
447
        x_fir = Connect(x_fir, z_state)
1✔
448
        out_x = Output('out_x', x_fir)
1✔
449
        out_y = Output('out_y', y_fir)
1✔
450
        out_z = Output('out_z', z_fir)
1✔
451
        out = Output('out',x_fir+y_fir+z_fir)
1✔
452

453
        test = Modely(visualizer=None, seed=42)
1✔
454
        test.addModel('out_all',[out, out_x, out_y, out_z])
1✔
455
        test.neuralizeModel(0.1)
1✔
456

457
        ## one sample prediction with state variables not initialized
458
        ## (they will have the last valid state)
459
        result = test(inputs={'x':[1,2,3,4,5]})
1✔
460
        self.assertEqual(result['out'], [90.0])
1✔
461
        self.assertEqual(result['out_x'], [15.0])
1✔
462
        self.assertEqual(result['out_y'], [30.0])
1✔
463
        self.assertEqual(result['out_z'], [45.0])
1✔
464
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [15.0]]])
1✔
465
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [15.0]]])
1✔
466
        # Replace insead of rolling
467
        # self.assertEqual(test.model.states['y'].numpy().tolist(), [[[0.0], [0.0], [0.0], [15.0], [0.0]]])
468
        # self.assertEqual(test.model.states['z'].numpy().tolist(), [[[0.0], [0.0], [0.0], [15.0], [0.0]]])
469
        ## 1 sample prediction with state variables all initialized
470
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5], 'z':[1,2,3,4,5]})
1✔
471
        self.assertEqual(result['out_x'], [15.0])
1✔
472
        #(1+2+3+4+5)+(2+3+4+5+(1+2+3+4+5))*2+(2+3+4+5+(1+2+3+4+5))*3
473
        self.assertEqual(result['out'], [160.0])
1✔
474
        self.assertEqual(result['out_y'], [58.0])
1✔
475
        self.assertEqual(result['out_z'], [87.0])
1✔
476
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[2.0], [3.0], [4.0], [5.0], [15.0]]])
1✔
477
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[2.0], [3.0], [4.0], [5.0], [15.0]]])
1✔
478
        # Replace instead of rolling
479
        #(1+2+3+4+5)+(1+2+3+4+(1+2+3+4+5))*2+(1+2+3+4+(1+2+3+4+5))*3
480
        # self.assertEqual(result['out'], [140.0])
481
        # self.assertEqual(result['out_y'], [50.0])
482
        # self.assertEqual(result['out_z'], [75.0])
483
        # self.assertEqual(test.model.states['y'].numpy().tolist(), [[[2.0], [3.0], [4.0], [15.0], [1.0]]])
484
        # self.assertEqual(test.model.states['z'].numpy().tolist(), [[[2.0], [3.0], [4.0], [15.0], [1.0]]])
485
        ## clear state of y
486
        test.resetStates({'y'})
1✔
487
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
1✔
488
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[2.0], [3.0], [4.0], [5.0], [15.0]]])
1✔
489
        # # Replace insead of rolling
490
        # ## clear state of y
491
        # test.resetStates({'y'})
492
        # self.assertEqual(test.model.states['y'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
493
        # self.assertEqual(test.model.states['z'].numpy().tolist(), [[[2.0], [3.0], [4.0], [15.0], [1.0]]])
494
        ## multi-sample prediction with states initialized as many times as they have values
495
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'y':[1,2,3,4,5,6,7], 'z':[1,2,3,4,5,6]})
1✔
496
        self.assertEqual(result['out_x'], [15.0, 20.0, 25.0, 30.0, 35.0])
1✔
497
        self.assertEqual(result['out_y'], [2*(2+3+4+5+15), 2*(3+4+5+6+20), 2*(4+5+6+7+25), 2*(5+6+7+25+30), 2*(6+7+25+30+35)])
1✔
498
        self.assertEqual(result['out_z'], [3*(2+3+4+5+15), 3*(3+4+5+6+20), 3*(4+5+6+20+25), 3*(5+6+20+25+30), 3*(6+20+25+30+35)])
1✔
499
        self.assertEqual(result['out'], [sum(x) for x in zip(result['out_x'],result['out_y'],result['out_z'])])
1✔
500
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[6.0], [7.0], [25.0], [30.0], [35.0]]])
1✔
501
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[6.0], [20.0], [25.0], [30.0], [35.0]]])
1✔
502
        # Replace instead of rolling
503
        # self.assertEqual(result['out_y'], [2*(1+2+3+4+15), 2*(2+3+4+5+20), 2*(3+4+5+6+25), 2*(4+5+6+25+30), 2*(5+6+25+30+35)])
504
        # self.assertEqual(result['out_z'], [3*(1+2+3+4+15), 3*(2+3+4+5+20), 3*(3+4+5+20+25), 3*(4+5+20+25+30), 3*(5+20+25+30+35)])
505
        # self.assertEqual(result['out'], [sum(x) for x in zip(result['out_x'],result['out_y'],result['out_z'])])
506
        # self.assertEqual(test.model.states['y'].numpy().tolist(), [[[6.0], [25.0], [30.0], [35.0], [5.0]]])
507
        # self.assertEqual(test.model.states['z'].numpy().tolist(), [[[20.0], [25.0], [30.0], [35.0], [5.0]]])
508
        ## Clear all states
509
        test.resetStates()
1✔
510
        self.assertEqual(test.model.states['y'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
1✔
511
        self.assertEqual(test.model.states['z'].numpy().tolist(), [[[0.0], [0.0], [0.0], [0.0], [0.0]]])
1✔
512

513
    def test_predict_values_and_states_2states_more_window_connect_predict(self):
1✔
514
        ## the state is saved inside the model so the memory is shared between different calls
515
        x = Input('x')
1✔
516
        y_state = Input('y')
1✔
517
        z_state = Input('z')
1✔
518
        x_p = Parameter('x_p', tw=0.5, dimensions=1, values=[[1.0],[1.0],[1.0],[1.0],[1.0]])
1✔
519
        y_p = Parameter('y_p', tw=0.5, dimensions=1, values=[[2.0],[2.0],[2.0],[2.0],[2.0]])
1✔
520
        z_p = Parameter('z_p', tw=0.5, dimensions=1, values=[[3.0],[3.0],[3.0],[3.0],[3.0]])
1✔
521
        x_fir = Fir(parameter=x_p)(x.tw(0.5))
1✔
522
        y_fir = Fir(parameter=y_p)(y_state.tw(0.5))
1✔
523
        z_fir = Fir(parameter=z_p)(z_state.tw(0.5))
1✔
524
        out_x = Output('out_x', x_fir)
1✔
525
        out_y = Output('out_y', y_fir)
1✔
526
        out_z = Output('out_z', z_fir)
1✔
527
        out = Output('out',x_fir+y_fir+z_fir)
1✔
528

529
        test = Modely(visualizer=None, seed=42)
1✔
530
        test.addModel('out_all',[out, out_x, out_y, out_z])
1✔
531
        test.neuralizeModel(0.1)
1✔
532

533
        ## one sample prediction with state variables not initialized
534
        ## (they will have the last valid state)
535
        result = test(inputs={'x':[1,2,3,4,5]}, connect={'y':'out_x','z':'out_x'})
1✔
536
        self.assertEqual(result['out'], [90.0])
1✔
537
        self.assertEqual(result['out_x'], [15.0])
1✔
538
        self.assertEqual(result['out_y'], [30.0])
1✔
539
        self.assertEqual(result['out_z'], [45.0])
1✔
540
        ## 1 sample prediction with state variables all initialized
541
        result = test(inputs={'x':[1,2,3,4,5], 'y':[1,2,3,4,5], 'z':[1,2,3,4,5]}, connect={'y':'out_x','z':'out_x'})
1✔
542
        self.assertEqual(result['out_x'], [15.0])
1✔
543
        #(1+2+3+4+5)+(2+3+4+5+(1+2+3+4+5))*2+(2+3+4+5+(1+2+3+4+5))*3
544
        self.assertEqual(result['out'], [160.0])
1✔
545
        self.assertEqual(result['out_y'], [58.0])
1✔
546
        self.assertEqual(result['out_z'], [87.0])
1✔
547
        # Replace instead of rolling
548
        #(1+2+3+4+5)+(1+2+3+4+(1+2+3+4+5))*2+(1+2+3+4+(1+2+3+4+5))*3
549
        # self.assertEqual(result['out'], [140.0])
550
        # self.assertEqual(result['out_y'], [50.0])
551
        # self.assertEqual(result['out_z'], [75.0])
552
        ## multi-sample prediction with states initialized as many times as they have values
553
        result = test(inputs={'x':[1,2,3,4,5,6,7,8,9], 'y':[1,2,3,4,5,6,7], 'z':[1,2,3,4,5,6]}, connect={'y':'out_x','z':'out_x'})
1✔
554
        self.assertEqual(result['out_x'], [15.0, 20.0, 25.0, 30.0, 35.0])
1✔
555
        self.assertEqual(result['out_y'], [2*(2+3+4+5+15), 2*(3+4+5+6+20), 2*(4+5+6+7+25), 2*(5+6+7+25+30), 2*(6+7+25+30+35)])
1✔
556
        self.assertEqual(result['out_z'], [3*(2+3+4+5+15), 3*(3+4+5+6+20), 3*(4+5+6+20+25), 3*(5+6+20+25+30), 3*(6+20+25+30+35)])
1✔
557
        # Reaplce instead of rolling
558
        # self.assertEqual(result['out_y'], [2*(1+2+3+4+15), 2*(2+3+4+5+20), 2*(3+4+5+6+25), 2*(4+5+6+25+30), 2*(5+6+25+30+35)])
559
        # self.assertEqual(result['out_z'], [3*(1+2+3+4+15), 3*(2+3+4+5+20), 3*(3+4+5+20+25), 3*(4+5+20+25+30), 3*(5+20+25+30+35)])
560
        self.assertEqual(result['out'], [sum(x) for x in zip(result['out_x'],result['out_y'],result['out_z'])])
1✔
561

562
    def test_predict_values_and_connect_variables_2models_more_window_connect(self):
1✔
563
        ## Model1
564
        input1 = Input('in1')
1✔
565
        a = Parameter('a', dimensions=1, tw=0.05, values=[[1],[1],[1],[1],[1]])
1✔
566
        output1 = Output('out1', Fir(parameter=a)(input1.tw(0.05)))
1✔
567

568
        test = Modely(visualizer=None, seed=42)
1✔
569
        test.addModel('model1', output1)
1✔
570
        test.addMinimize('error1', input1.next(), output1)
1✔
571
        test.neuralizeModel(0.01)
1✔
572

573
        ## Model2
574
        input2 = Input('in2')
1✔
575
        input3 = State('in3')
1✔
576
        b = Parameter('b', dimensions=1, tw=0.05, values=[[1],[1],[1],[1],[1]])
1✔
577
        c = Parameter('c', dimensions=1, tw=0.03, values=[[1],[1],[1]])
1✔
578
        output2 = Output('out2', Fir(parameter=b)(input2.tw(0.05))+Fir(parameter=c)(input3.tw(0.03)))
1✔
579

580
        test.addModel('model2', output2)
1✔
581
        test.addConnect(output1,input3)
1✔
582
        test.addMinimize('error2', input2.next(), output2)
1✔
583
        test.neuralizeModel(0.01)
1✔
584

585
        ## Without connect
586
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in3':[[1],[2],[3],[4],[5],[6]]}, prediction_samples=None)
1✔
587
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
588
        self.assertEqual(results['out2'], [21.0, 29.0, 37.0, 45.0])
1✔
589

590
        ## connect out1 to in3 for 4 samples
591
        test.resetStates()
1✔
592
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]]}, prediction_samples=3)
1✔
593
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
594
        self.assertEqual(results['out2'], [30.0, 55.0, 85.0, 105.0])
1✔
595
        self.assertEqual(test.model.states['in3'].detach().numpy().tolist(), [[[20.], [25.], [30.]]])
1✔
596
        # Replace insead of rolling
597
        # self.assertEqual(test.model.states['in3'].detach().numpy().tolist(), [[[25.], [30.], [20.]]])
598

599
        ## connect out1 to in3 for 3 samples
600
        test.resetStates()
1✔
601
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]]}, prediction_samples=2)
1✔
602
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
603
        self.assertEqual(results['out2'], [30.0, 55.0, 85.0, 60.0])
1✔
604
        self.assertEqual(test.model.states['in3'].detach().numpy().tolist(), [[[0.0], [0.], [30.]]])
1✔
605
        # Replace insead of rolling
606
        # self.assertEqual(test.model.states['in3'].detach().numpy().tolist(), [[[0.], [30.], [0.]]])
607

608
        ## connect out1 to in3 for 4 samples (initialize in3 with data)
609
        test.resetStates()
1✔
610
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in3':[[1],[2],[3],[4],[5],[6]]}, prediction_samples=3)
1✔
611
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
612
        #(1+2+3+4+5)+(2+3+15)
613
        #(2+3+4+5+6)+(3+15+20)
614
        self.assertEqual(results['out2'], [35.0, 58.0, 85.0, 105.0])
1✔
615
        self.assertEqual(test.model.states['in3'].detach().numpy().tolist(), [[[20.], [25.], [30.]]])
1✔
616
        # Replace insead of rolling
617
        #(1+2+3+4+5)+(1+2+15)
618
        #(2+3+4+5+6)+(2+15+20)
619
        # self.assertEqual(results['out2'], [33.0, 57.0, 85.0, 105.0])
620
        # self.assertEqual(test.model.states['in3'].detach().numpy().tolist(), [[[25.], [30.], [20.]]])
621

622
        ## connect out1 to in3 for 3 samples (initialize in3 with data)
623
        test.resetStates()
1✔
624
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in3':[[1],[2],[3],[4],[5],[6]]}, prediction_samples=2)
1✔
625
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
626
        # (4+5+6+7+8)+(5+6+30)
627
        self.assertEqual(results['out2'], [35.0, 58.0, 85.0, 71.0])
1✔
628
        self.assertEqual(test.model.states['in3'].detach().numpy().tolist(), [[[5.], [6.], [30.]]])
1✔
629
        # Replace insead of rolling
630
        # (4+5+6+7+8)+(4+5+30)
631
        # self.assertEqual(results['out2'], [33.0, 57.0, 85.0, 69.0])
632
        # self.assertEqual(test.model.states['in3'].detach().numpy().tolist(), [[[5.], [30.], [4.]]])
633

634
    def test_predict_values_and_connect_variables_2models_more_window_connect_predict(self):
1✔
635
        ## Model1
636
        input1 = Input('in1')
1✔
637
        a = Parameter('a', dimensions=1, tw=0.05, values=[[1],[1],[1],[1],[1]])
1✔
638
        output1 = Output('out1', Fir(parameter=a)(input1.tw(0.05)))
1✔
639

640
        test = Modely(visualizer=None, seed=42)
1✔
641
        test.addModel('model1', output1)
1✔
642
        test.addMinimize('error1', input1.next(), output1)
1✔
643
        test.neuralizeModel(0.01)
1✔
644

645
        ## Model2
646
        input2 = Input('in2')
1✔
647
        input3 = Input('in3')
1✔
648
        b = Parameter('b', dimensions=1, tw=0.05, values=[[1],[1],[1],[1],[1]])
1✔
649
        c = Parameter('c', dimensions=1, tw=0.03, values=[[1],[1],[1]])
1✔
650
        output2 = Output('out2', Fir(parameter=b)(input2.tw(0.05))+Fir(parameter=c)(input3.tw(0.03)))
1✔
651

652
        test.addModel('model2', output2)
1✔
653
        test.addMinimize('error2', input2.next(), output2)
1✔
654
        test.neuralizeModel(0.01)
1✔
655

656
        ## Without connect
657
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in3':[[1],[2],[3],[4],[5],[6]]})
1✔
658
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
659
        self.assertEqual(results['out2'], [21.0, 29.0, 37.0, 45.0])
1✔
660

661
        ## connect out1 to in3 for 4 samples
662
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]]}, prediction_samples=3, connect={'in3':'out1'})
1✔
663
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
664
        self.assertEqual(results['out2'], [30.0, 55.0, 85.0, 105.0])
1✔
665
        self.assertEqual(test.model.connect_variables['in3'].detach().numpy().tolist(), [[[20.], [25.], [30.]]])
1✔
666
        # Replace insead of rolling
667
        # self.assertEqual(test.model.connect_variables['in3'].detach().numpy().tolist(), [[[25.], [30.], [20.]]])
668

669
        ## connect out1 to in3 for 3 samples
670
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]]}, prediction_samples=2, connect={'in3':'out1'})
1✔
671
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
672
        self.assertEqual(results['out2'], [30.0, 55.0, 85.0, 60.0])
1✔
673
        self.assertEqual(test.model.connect_variables['in3'].detach().numpy().tolist(), [[[0.0], [0.], [30.]]])
1✔
674
        # Replace insead of rolling
675
        # self.assertEqual(test.model.connect_variables['in3'].detach().numpy().tolist(), [[[0.], [30.], [0.]]])
676

677
        ## connect out1 to in3 for 4 samples (initialize in3 with data)
678
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in3':[[1],[2],[3],[4],[5],[6]]}, prediction_samples=3, connect={'in3':'out1'})
1✔
679
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
680
        #(1+2+3+4+5)+(2+3+15)
681
        #(2+3+4+5+6)+(3+15+20)
682
        self.assertEqual(results['out2'], [35.0, 58.0, 85.0, 105.0])
1✔
683
        self.assertEqual(test.model.connect_variables['in3'].detach().numpy().tolist(), [[[20.], [25.], [30.]]])
1✔
684
        # Replace insead of rolling
685
        #(1+2+3+4+5)+(1+2+15)
686
        #(2+3+4+5+6)+(2+15+20)
687
        # self.assertEqual(results['out2'], [33.0, 57.0, 85.0, 105.0])
688
        # self.assertEqual(test.model.connect_variables['in3'].detach().numpy().tolist(), [[[25.], [30.], [20.]]])
689

690
        ## connect out1 to in3 for 3 samples (initialize in3 with data)
691
        results = test(inputs={'in1':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in2':[[1],[2],[3],[4],[5],[6],[7],[8],[9]], 'in3':[[1],[2],[3],[4],[5],[6]]}, prediction_samples=2, connect={'in3':'out1'})
1✔
692
        self.assertEqual(results['out1'], [15.0, 20.0, 25.0, 30.0])
1✔
693
        # (4+5+6+7+8)+(5+6+30)
694
        self.assertEqual(results['out2'], [35.0, 58.0, 85.0, 71.0])
1✔
695
        self.assertEqual(test.model.connect_variables['in3'].detach().numpy().tolist(), [[[5.], [6.], [30.]]])
1✔
696
        # Replace insead of rolling
697
        # (4+5+6+7+8)+(4+5+30)
698
        # self.assertEqual(results['out2'], [33.0, 57.0, 85.0, 69.0])
699
        # self.assertEqual(test.model.connect_variables['in3'].detach().numpy().tolist(), [[[5.], [30.], [4.]]])
700

701
    def test_predict_values_and_states_only_state_variables_more_window_closed_loop(self):
1✔
702
        x_state = State('x_state')
1✔
703
        p = Parameter('p', dimensions=1, tw=0.03, values=[[1.0], [1.0], [1.0]])
1✔
704
        rel_x = Fir(parameter=p)(x_state.tw(0.03))
1✔
705
        rel_x = ClosedLoop(rel_x, x_state)
1✔
706
        out = Output('out', rel_x)
1✔
707

708
        test = Modely(visualizer = None, seed=42)
1✔
709
        test.addModel('out',out)
1✔
710
        test.neuralizeModel(0.01)
1✔
711

712
        result = test(inputs={'x_state':[1, 2, 3]})
1✔
713
        self.assertEqual(test.model.states['x_state'].numpy().tolist(), [[[2.],[3.],[6.]]])
1✔
714
        result = test()
1✔
715
        self.assertEqual(test.model.states['x_state'].numpy().tolist(), [[[3.],[6.],[11.]]])
1✔
716

717
    def test_predict_values_linear_and_fir_2models_same_window_connect(self):
1✔
718
        NeuObj.reset_count()
1✔
719
        input1 = Input('in1',dimensions=2)
1✔
720
        W = Parameter('W', values=[[[-1],[-5]]])
1✔
721
        b = Parameter('b', values=[[1]])
1✔
722
        lin_out = Linear(W=W, b=b)(input1.sw(2))
1✔
723
        output1 = Output('out1', lin_out)
1✔
724

725
        inout = State('inout')
1✔
726
        a = Parameter('a', values=[[4],[5]])
1✔
727
        output2 = Output('out2', Fir(parameter=a)(inout.sw(2)))
1✔
728
        output3 = Output('out3', Fir(parameter=a)(lin_out))
1✔
729

730
        test = Modely(visualizer=None, seed=42)
1✔
731
        test.addModel('model', [output1,output2,output3])
1✔
732
        test.addConnect(output1,inout)
1✔
733
        test.neuralizeModel()
1✔
734
        # [[1,2],[2,3]]*[-1,-5] = [[1*-1+2*-5=-11],[2*-1+3*-5=-17]]+[1] = [-10,-16] -> [-10,-16]*[4,5] -> [-16*5+-10*4=-120] <------
735
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3':[-120.0]}, test({'in1': [[1.0, 2.0], [2.0, 3.0]],'inout':[-10,-16]}))
1✔
736
        self.assertEqual({'out1': [[-10.0,-16.0]], 'out2': [-120.0], 'out3':[-120.0]}, test({'in1': [[1.0,2.0],[2.0,3.0]]}))
1✔
737

738
    def test_predict_values_linear_and_fir_2models_same_window_connect_predict(self):
1✔
739
        NeuObj.reset_count()
1✔
740
        input1 = Input('in1',dimensions=2)
1✔
741
        W = Parameter('W', values=[[[-1],[-5]]])
1✔
742
        b = Parameter('b', values=[[1]])
1✔
743
        lin_out = Linear(W=W, b=b)(input1.sw(2))
1✔
744
        output1 = Output('out1', lin_out)
1✔
745

746
        inout = Input('inout')
1✔
747
        a = Parameter('a', values=[[4],[5]])
1✔
748
        output2 = Output('out2', Fir(parameter=a)(inout.sw(2)))
1✔
749
        output3 = Output('out3', Fir(parameter=a)(lin_out))
1✔
750

751
        test = Modely(visualizer=None, seed=42)
1✔
752
        test.addModel('model', [output1,output2,output3])
1✔
753
        test.neuralizeModel()
1✔
754
        # [[1,2],[2,3]]*[-1,-5] = [[1*-1+2*-5=-11],[2*-1+3*-5=-17]]+[1] = [-10,-16] -> [-10,-16]*[4,5] -> [-16*5+-10*4=-120] <------
755
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3':[-120.0]}, test({'in1': [[1.0, 2.0], [2.0, 3.0]],'inout':[-10,-16]}))
1✔
756
        self.assertEqual({'out1': [[-10.0,-16.0]], 'out2': [-120.0], 'out3':[-120.0]}, test({'in1': [[1.0,2.0],[2.0,3.0]]},connect={'inout': 'out1'}))
1✔
757
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3': [-120.0]},
1✔
758
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]],'inout':[-30,-30]}, connect={'inout': 'out1'}))
759

760
    def test_predict_values_linear_and_fir_2models_more_window_connect(self):
1✔
761
        NeuObj.reset_count()
1✔
762
        input1 = Input('in1',dimensions=2)
1✔
763
        W = Parameter('W', values=[[[-1],[-5]]])
1✔
764
        b = Parameter('b', values=[[1]])
1✔
765
        lin_out = Linear(W=W, b=b)(input1.sw(2))
1✔
766
        output1 = Output('out1', lin_out)
1✔
767

768
        inout = State('inout')
1✔
769
        a = Parameter('a', values=[[4], [5]])
1✔
770
        a_big = Parameter('ab', values=[[1], [2], [3], [4], [5]])
1✔
771
        output2 = Output('out2', Fir(parameter=a)(inout.sw(2)))
1✔
772
        output3 = Output('out3', Fir(parameter=a_big)(inout.sw(5)))
1✔
773
        output4 = Output('out4', Fir(parameter=a)(lin_out))
1✔
774

775
        test = Modely(visualizer=None, seed=42)
1✔
776
        test.addModel('model', [output1,output2,output3,output4])
1✔
777
        test.addConnect(output1, inout)
1✔
778
        test.neuralizeModel()
1✔
779
        # [[1,2],[2,3]]*[-1,-5] = [[1*-1+2*-5=-11],[2*-1+3*-5=-17]]+[1] = [-10,-16] -> [-10,-16]*[4,5] -> [-16*5+-10*4=-120] <------
780
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3': [-120.0], 'out4': [-120.0]}, test({'in1': [[1.0, 2.0], [2.0, 3.0]]}))
1✔
781
        test.resetStates()
1✔
782
        # out2 # = [[-10,-16]] -> 1) [-10,-16]*[4,5] -> [-16*5+-10*4=-120]
783
        # out3 # = [[-10,-16]] -> 1) [0,0,-10,-10,-16]*[1,2,3,4,5] -> [-10*3+-16*5+-10*4=-150]
784
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3': [-150.0], 'out4': [-120.0]}, test({'in1': [[1.0, 2.0], [2.0, 3.0]],'inout':[0,0,0,-10,-16]}))
1✔
785
        # Replace instead of rolling
786
        # self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3': [-120.0], 'out4': [-120.0]},
787
        #                  test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'inout': [0, 0, 0, -10, -16]}))
788
        test.resetStates()
1✔
789

790
        # out2 # = [[-10,-16],[-16,-10]] -> 1) [-10,-16]*[4,5] -> [-16*5+-10*4=-120]             2) [-16,-10]*[4,5] -> [-16*4+-10*5=-114] -> [-120,-114]
791
        # out3 # = [[-10,-16],[-16,-10]] -> 1) [0,0,0,-10,-16]*[1,2,3,4,5] -> [-16*5+-10*4=-120] 2) [0,0,-10,-16,-10]*[1,2,3,4,5] -> [-10*3+-16*4+-10*5 = -144] -> [-120,-144]
792
        self.assertEqual({'out1': [[-10.0, -16.0],[-16.0, -10.0]], 'out2': [-120.0,-114.0], 'out3': [-120.0,-144], 'out4': [-120.0,-114.0]},
1✔
793
                         test({'in1': [[1.0, 2.0], [2.0, 3.0], [1.0,2.0]]}))
794

795
    def test_predict_values_linear_and_fir_2models_more_window_connect_predict(self):
1✔
796
        NeuObj.reset_count()
1✔
797
        input1 = Input('in1',dimensions=2)
1✔
798
        W = Parameter('W', values=[[[-1],[-5]]])
1✔
799
        b = Parameter('b', values=[[1]])
1✔
800
        lin_out = Linear(W=W, b=b)(input1.sw(2))
1✔
801
        output1 = Output('out1', lin_out)
1✔
802

803
        inout = Input('inout')
1✔
804
        a = Parameter('a', values=[[4], [5]])
1✔
805
        output2 = Output('out2', Fir(parameter=a)(inout.sw(2)))
1✔
806
        a_big = Parameter('ab', values=[[1], [2], [3], [4], [5]])
1✔
807
        output3 = Output('out3', Fir(parameter=a_big)(inout.sw(5)))
1✔
808
        output4 = Output('out4', Fir(parameter=a)(lin_out))
1✔
809

810
        test = Modely(visualizer=None, seed=42)
1✔
811
        test.addModel('model', [output1,output2,output3,output4])
1✔
812
        test.neuralizeModel()
1✔
813
        # [[1,2],[2,3]]*[-1,-5] = [[1*-1+2*-5=-11],[2*-1+3*-5=-17]]+[1] = [-10,-16] -> [-10,-16]*[4,5] -> [-16*5+-10*4=-120] <------
814
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3': [-120.0], 'out4': [-120.0]}, test({'in1': [[1.0, 2.0], [2.0, 3.0]],'inout':[0,0,0,-10,-16]}))
1✔
815
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3': [-120.0], 'out4': [-120.0]},
1✔
816
                        test({'in1': [[1.0, 2.0], [2.0, 3.0]]}, connect={'inout': 'out1'}))
817
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [-120.0], 'out3': [-150.0], 'out4': [-120.0]},
1✔
818
                        test({'in1': [[1.0, 2.0], [2.0, 3.0]],'inout':[0,0,0,-10,-16]}, connect={'inout': 'out1'}))
819
        with self.assertRaises(StopIteration):
1✔
820
            self.assertEqual({}, test())
1✔
821
        with self.assertRaises(StopIteration):
1✔
822
            self.assertEqual({}, test(prediction_samples=0))
1✔
823
        with self.assertRaises(StopIteration):
1✔
824
            self.assertEqual({}, test(prediction_samples=4))
1✔
825

826
        self.assertEqual({'out1': [[1.0, 1.0]], 'out2': [9.0], 'out3': [9.0], 'out4': [9.0]},
1✔
827
                        test(connect={'inout': 'out1'}))
828
        self.assertEqual({'out1': [[1.0, 1.0]], 'out2': [9.0], 'out3': [9.0], 'out4': [9.0]},
1✔
829
                        test(connect={'inout': 'out1'}, prediction_samples=0))
830
        self.assertEqual({'out1': [[1.0, 1.0],[1.0, 1.0]], 'out2': [9.0,9.0], 'out3': [9.0,12.0], 'out4': [9.0,9.0]},
1✔
831
                        test(connect={'inout': 'out1'}, prediction_samples=1, num_of_samples=2))
832

833
        # [[1,2],[2,3]]*[-1,-5] = [[1*-1+2*-5=-11],[2*-1+3*-5=-17]]+[1]
834
        # [[2,3],[1,2]]*[-1,-5] = [[2*-1+3*-5=-17],[1*-1+2*-5=-11]]+[1]
835
        # out2 # = [[-10,-16],[-16,-10]] -> 1) [-10,-16]*[4,5] -> [-16*5+-10*4=-120]             2) [-16,-10]*[4,5] -> [-16*4+-10*5=-114] -> [-120,-114]
836
        # out3 # = [[-10,-16],[-16,-10]] -> 1) [0,0,0,-10,-16]*[1,2,3,4,5] -> [-16*5+-10*4=-120] 2) [0,0,-10,-16,-10]*[1,2,3,4,5] -> [-10*3+-16*4+-10*5 = -144] -> [-120,-144]
837
        self.assertEqual({'out1': [[-10.0, -16.0],[-16.0, -10.0]], 'out2': [-120.0,-114.0], 'out3': [-120.0,-144], 'out4': [-120.0,-114.0]},
1✔
838
                         test({'in1': [[1.0, 2.0], [2.0, 3.0], [1.0,2.0]]},
839
                              connect={'inout': 'out1'}))
840

841
    def test_predict_values_linear_and_fir_2models_more_window_closed_loop(self):
1✔
842
        NeuObj.reset_count()
1✔
843
        input1 = State('in1',dimensions=2)
1✔
844
        W = Parameter('W', values=[[[-1],[-5]]])
1✔
845
        b = Parameter('b', values=[[1]])
1✔
846
        output1 = Output('out1', Linear(W=W, b=b)(input1.sw(2)))
1✔
847

848
        # input2 = State('inout') #TODO loop forever
849
        # test.addConnect(output1, input1) # With this
850
        input2 = State('in2')
1✔
851
        a = Parameter('a', values=[[1,3],[2,4],[3,5],[4,6],[5,7]])
1✔
852
        output2 = Output('out2', Fir(output_dimension=2,parameter=a)(input2.sw(5)))
1✔
853

854
        test = Modely(visualizer=None, seed=42)
1✔
855
        test.addModel('model', [output1,output2])
1✔
856
        test.addClosedLoop(output1, input2)
1✔
857
        test.addClosedLoop(output2, input1)
1✔
858
        test.neuralizeModel()
1✔
859
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [[[-34.0, -86.0]]]},
1✔
860
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]}))
861
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [[[-34.0, -86.0]]]},
1✔
862
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]},prediction_samples=0))
863

864
        self.assertEqual({'out1': [[-10.0, -16.0],[-16.0,465.0]], 'out2': [[[-34.0, -86.0]],[[-140.0,-230.0]]]},
1✔
865
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]}, prediction_samples=1, num_of_samples=2))
866
        self.assertEqual({'out1': [[465.0,1291.0]], 'out2': [[[2230.0, 3102.0]]]}, test())
1✔
867
        test.resetStates()
1✔
868
        self.assertEqual({'out1': [[-10.0, -16.0],[-16.0,465.0],[465.0,1291.0]], 'out2': [[[-34.0, -86.0]],[[-140.0,-230.0]],[[2230.0, 3102.0]]]},
1✔
869
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]}, prediction_samples=2, num_of_samples=3))
870

871
    def test_predict_values_linear_and_fir_2models_more_window_closed_loop_predict(self):
1✔
872
        NeuObj.reset_count()
1✔
873
        input1 = Input('in1',dimensions=2)
1✔
874
        W = Parameter('W', values=[[[-1],[-5]]])
1✔
875
        b = Parameter('b', values=[[1]])
1✔
876
        output1 = Output('out1', Linear(W=W, b=b)(input1.sw(2)))
1✔
877

878
        # input2 = State('inout') #TODO loop forever
879
        # test.addConnect(output1, input1) # With this
880
        input2 = Input('in2')
1✔
881
        a = Parameter('a', values=[[1,3],[2,4],[3,5],[4,6],[5,7]])
1✔
882
        output2 = Output('out2', Fir(output_dimension=2,parameter=a)(input2.sw(5)))
1✔
883

884
        test = Modely(visualizer=None, seed=42)
1✔
885
        test.addModel('model', [output1,output2])
1✔
886
        test.neuralizeModel()
1✔
887
        # 1*-1+2*-5+1 = -10 2*-1+3*-5+1 = -16 -10*1+-16*2+-5*3+2*4+3*5 = -34 -16*2+-10*3+-5*4+2*5+3*6 = -86
888
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [[[-34.0, -86.0]]]},
1✔
889
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]}, closed_loop={'in1':'out2', 'in2':'out1'}))
890
        self.assertEqual({'out1': [[-10.0, -16.0]], 'out2': [[[-34.0, -86.0]]]},
1✔
891
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]}, prediction_samples=0, closed_loop={'in1':'out2', 'in2':'out1'}))
892
        self.assertEqual({'out1': [[-10.0, -16.0], [-16.0,465.0]], 'out2': [[[-34.0, -86.0]],[[-140.0,-230.0]]]},
1✔
893
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]}, num_of_samples=2, prediction_samples=2, closed_loop={'in1':'out2', 'in2':'out1'}))
894

895
        with self.assertRaises(StopIteration):
1✔
896
             self.assertEqual({'out1': [[465.0, 1291.0]], 'out2': [[[2230.0, 3102.0]]]}, test())
1✔
897
        with self.assertRaises(StopIteration):
1✔
898
             self.assertEqual({'out1': [[465.0, 1291.0]], 'out2': [[[2230.0, 3102.0]]]}, test(prediction_samples=0))
1✔
899
        with self.assertRaises(StopIteration):
1✔
900
             self.assertEqual({'out1': [[465.0, 1291.0]], 'out2': [[[2230.0, 3102.0]]]}, test(prediction_samples=3))
1✔
901
        self.assertEqual({'out1': [[1.0, 1.0]], 'out2': [[[0.0, 0.0]]]},
1✔
902
                          test(closed_loop={'in1': 'out2', 'in2': 'out1'}))
903
        self.assertEqual({'out1': [[1.0, 1.0]], 'out2': [[[0.0, 0.0]]]},
1✔
904
                          test(closed_loop={'in1': 'out2', 'in2': 'out1'},prediction_samples=0))
905
        self.assertEqual({'out1': [[1.0, 1.0],[1.0,1.0]], 'out2': [[[0.0, 0.0]],[[9.0,13.0]]]},
1✔
906
                          test(closed_loop={'in1': 'out2', 'in2': 'out1'},prediction_samples=1, num_of_samples=2))
907
        self.assertEqual({'out1': [[1.0, 1.0],[1.0,1.0],[1.0,-73.0]], 'out2': [[[0.0, 0.0]],[[9.0,13.0]],[[12.0,18.0]]]},
1✔
908
                          test(closed_loop={'in1': 'out2', 'in2': 'out1'},prediction_samples=2, num_of_samples=3))
909

910
        self.assertEqual({'out1': [[-10.0, -16.0], [-16.0,1.0], [1.0,1.0], [1.0,1.0], [1.0,1.0]],
1✔
911
                          'out2': [[[-34.0, -86.0]],[[-8.0,-40.0]],[[8.0,8.0]],[[8.0,18.0]],[[3.0,9.0]]]},
912
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]},  num_of_samples=5))
913
        self.assertEqual({'out1': [[-10.0, -16.0], [-16.0,1.0], [1.0,1.0], [1.0,1.0], [1.0,1.0]],
1✔
914
                          'out2': [[[-34.0, -86.0]],[[-8.0,-40.0]],[[8.0,8.0]],[[8.0,18.0]],[[3.0,9.0]]]},
915
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]},  prediction_samples=None, num_of_samples=5))
916

917
        #-34*-1+ -86*-5+1 = 465.0
918
        #-140*-1+ -230*-5+1 = 465.0
919
        #8*-1 + 18*-5+1 = -97.0
920
        #[[1, 3], [2, 4], [3, 5], [4, 6], [5, 7]] * [465.0, 1291.0]
921
        #-16*1+-5*2+2*3+-10.0*4-16.0*5 = 140
922
        #-5*1+2*2-10*3+-16*4+465*5 = 2230 , -5*3+2*4-10*5+-16*6+465*7 = 3102
923
        #2*1+3*2 = 8, 2*3+3*4 = 18
924
        #3*1+1*4+1*5 = 12.0, 3*3+1*6+1*7 = 22.0
925
        self.assertEqual({'out1': [[-10.0, -16.0], [-16.0, 465.0], [465.0, 1291.0], [1.0, 1.0], [1.0, -97.0]],
1✔
926
                          'out2': [[[-34.0, -86.0]], [[-140.0, -230.0]],[[2230.0, 3102.0]], [[8.0, 18.0]], [[12.0, 22.0]]]},
927
                         test({'in1': [[1.0, 2.0], [2.0, 3.0]], 'in2': [-10, -16, -5, 2, 3]}, num_of_samples=5,
928
                              prediction_samples=2, closed_loop={'in1': 'out2', 'in2': 'out1'}))
929

930
    def test_predict_parameters(self):
1✔
931
        NeuObj.reset_count()
1✔
932
        input1 = Input('in1')
1✔
933
        cl1 = State('cl1')
1✔
934
        co1 = State('co1')
1✔
935
        W = Parameter('W', values=[[1], [2], [3]])
1✔
936
        parfun = ParamFun(myfunsum, parameters=[W])
1✔
937
        matmulfun = ParamFun(matmul)
1✔
938
        parfun_out = parfun(input1.sw(3))
1✔
939
        output = Output('out', parfun_out)
1✔
940
        matmul_outcl = matmulfun(parfun_out, cl1.sw(3))+1.0
1✔
941
        matmul_outcl.connect(co1)
1✔
942
        matmul_outcl.closedLoop(cl1)
1✔
943
        outputCl = Output('outCl', matmul_outcl)
1✔
944
        outputCo = Output('outCo', matmulfun(parfun_out, co1.sw(3)))
1✔
945

946
        test = Modely(visualizer=None, seed=42)
1✔
947
        test.addModel('model', [output,outputCl,outputCo])
1✔
948
        test.neuralizeModel()
1✔
949

950
        # Test only one input
951
        result = test({'in1':[1,2,3]})
1✔
952
        self.assertEqual((1,3), np.array(result['out']).shape)
1✔
953
        self.assertEqual((1,), np.array(result['outCl']).shape)
1✔
954
        self.assertEqual((1,), np.array(result['outCo']).shape)
1✔
955
        self.assertEqual([[2.0,4.0,6.0]], result['out'])
1✔
956
        self.assertEqual([1.0], result['outCl'])
1✔
957
        self.assertEqual([6.0],result['outCo'])
1✔
958
        self.assertEqual(test.model.states['cl1'].detach().numpy().tolist(), [[[0.], [0.], [1.]]])
1✔
959
        self.assertEqual(test.model.states['co1'].detach().numpy().tolist(), [[[0.], [0.], [1.]]])
1✔
960

961
        # Test two input
962
        test.resetStates()
1✔
963
        result = test({'in1':[1,2,3,4]})
1✔
964
        self.assertEqual((2,3), np.array(result['out']).shape)
1✔
965
        self.assertEqual((2,), np.array(result['outCl']).shape)
1✔
966
        self.assertEqual((2,), np.array(result['outCo']).shape)
1✔
967
        self.assertEqual([[2.0,4.0,6.0],[3.0,5.0,7.0]], result['out'])
1✔
968
        self.assertEqual([1.0,1*7+1], result['outCl'])
1✔
969
        self.assertEqual([1 * 6.0, 1. * 5 + 7. * 8.], result['outCo'])
1✔
970
        self.assertEqual(test.model.states['co1'].detach().numpy().tolist(), [[[0.], [1.], [8.]]])
1✔
971
        self.assertEqual(test.model.states['cl1'].detach().numpy().tolist(), [[[0.], [1.], [8.]]])
1✔
972

973
        # Test two input
974
        test.resetStates()
1✔
975
        result = test({'in1':[1,2,3,4], 'cl1':[2,2,2,2,2,2]})
1✔
976
        self.assertEqual((2,3), np.array(result['out']).shape)
1✔
977
        self.assertEqual((2,), np.array(result['outCl']).shape)
1✔
978
        self.assertEqual((2,), np.array(result['outCo']).shape)
1✔
979
        self.assertEqual([[2.0,4.0,6.0],[3.0,5.0,7.0]], result['out'])
1✔
980
        # 2*2+4*2+6*2+1, 2*3+5*2+7*2+1
981
        self.assertEqual([25.0, 31.], result['outCl'])
1✔
982
        self.assertEqual([150.0, 342.0], result['outCo'])
1✔
983
        self.assertEqual(test.model.states['cl1'].detach().numpy().tolist(), [[[2.], [2.], [31.]]])
1✔
984
        self.assertEqual(test.model.states['co1'].detach().numpy().tolist(), [[[0.], [25.], [31.]]])
1✔
985

986
        # Test two input
987
        test.resetStates()
1✔
988
        result = test({'in1':[1,2,3,4], 'co1':[2,2,2,2,2,2]})
1✔
989
        self.assertEqual((2,3), np.array(result['out']).shape)
1✔
990
        self.assertEqual((2,), np.array(result['outCl']).shape)
1✔
991
        self.assertEqual((2,), np.array(result['outCo']).shape)
1✔
992
        self.assertEqual([[2.0,4.0,6.0],[3.0,5.0,7.0]], result['out'])
1✔
993
        # 2*0+4*0+6*0+1
994
        self.assertEqual([1.0, 7*1+1.], result['outCl'])
1✔
995
        # 2*2+4*2+6*1, 2*3+2*5+7*8
996
        self.assertEqual([18.0, 72.0], result['outCo'])
1✔
997
        self.assertEqual(test.model.states['cl1'].detach().numpy().tolist(), [[[0.], [1.], [8.]]])
1✔
998
        self.assertEqual(test.model.states['co1'].detach().numpy().tolist(), [[[2.], [2.], [8.]]])
1✔
999

1000
        test.resetStates()
1✔
1001
        result = test({'co1':[2,2,2,2,2,2]})
1✔
1002
        self.assertEqual((4,3), np.array(result['out']).shape)
1✔
1003
        self.assertEqual((4,), np.array(result['outCl']).shape)
1✔
1004
        self.assertEqual((4,), np.array(result['outCo']).shape)
1✔
1005
        self.assertEqual([[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]], result['out'])
1✔
1006
        self.assertEqual(test.model.states['cl1'].detach().numpy().tolist(), [[[4.], [15.], [55.]]])
1✔
1007
        self.assertEqual(test.model.states['co1'].detach().numpy().tolist(), [[[2.], [2.], [55.]]])
1✔
1008

1009
        test.resetStates()
1✔
1010
        result = test({'co1':[2,2,2,2,2,2]}, prediction_samples = 2)
1✔
1011
        self.assertEqual((4,3), np.array(result['out']).shape)
1✔
1012
        self.assertEqual((4,), np.array(result['outCl']).shape)
1✔
1013
        self.assertEqual((4,), np.array(result['outCo']).shape)
1✔
1014

1015

1016
        # Test output recurrent
1017

1018
        # prediction_samples set the dimension of the prediction samples every prediction_samples the recurrent variable is read from input (as in train)
1019
        # default value = 'auto' means the network is connected and is closed loop for all the available samples
1020
        # None means that the network is not anymore closed loop or connected
1021
        # 0 means that every sample the state is reset or with zero or with the input but the connect works
1022
        # 1 means that for 1 sample the network use the inside value
1023

1024
        # num_of_samples is the numer of sample return from the call if the input is missing is filled with zero
1025
        # default value = 'auto' means that the network choose the best number of samples to generate
1026
        # at least one sample is generate if the network is called without values
1027
        # or the maximum number of samples if it is called without aligned_input = True
1028
        # otherwise generate the same number of the dataset if it is called with aligned_inputs = True
1029

1030
        # aligned_inputs is a flag for align the inputs as the raw of the dataset
1031
        # default value = False
1032

1033
        # Cases with aligned_input = False
1034
        # Case predict 1 step using the states or (fill the input with zero with a warning)
1035
        #   test()
1036
        #
1037
        # Case prediction using partial inputs. The states and inputs are filled using a rolling window reaching the correct dimension.
1038
        # The network predict the maximum number of samples (considering the bigger input) uses a rolling window.
1039
        # in this way a partial initialization of the state is possible. (if some inputs are missing for generate an input return a warning).
1040
        #   test(data)
1041
        #
1042
        # Case predict using the dataset or inputs without aligned_inputs the network predict the maximum number of samples (considering the bigger input) uses a rolling window
1043
        # The states are reset only if the input is present. it is used a rolling window for the input.
1044
        #   test(dataset)
1045
        #
1046
        # Case predict using the dataset or inputs without aligned_inputs the network predict the maximum number of samples (considering the bigger input) uses a rolling window
1047
        # The states are reset every prediction_sample using inputs or zeros
1048
        #   test(dataset, prediction_sample=N)
1049
        #
1050
        # Case predict M samples using the dataset or inputs
1051
        # the states are reset only if the input is present. if the input are not present are fill with zero. it is used a rolling window for the input
1052
        #   test(dataset, num_of_samples=M)
1053
        #
1054
        # Case predict M samples using the dataset or inputs
1055
        # the states are reset every prediction_sample using inputs or zeros. if the input are not present are fill with zero. it is used a rolling window for the input
1056
        #   test(dataset, prediction_sample=N, num_of_samples=M)
1057
        #
1058
        # Cases with aligned_input=True
1059
        # If the input are messing the network return an arror.
1060
        # The number of the input or states max be equal.
1061
        # The num_of_samples must be less than the number of sample of the data.
1062
        # The states are reset only if the input is present.
1063
        # The network predict the same sample of training.
1064
        # Case with no inputs test(aligned_input=True) the output is the same of before.
1065
        # Case partial inputs test(data, aligned_input=True) the output is the same of before.
1066
        # Case with a data or dataset test(dataset, aligned_input=True) the output is the same of before.
1067
        # Case predict using the dataset as the training does the network predict the same sample of training
1068
        # The states are reset every prediction_sample using inputs or zeros
1069
        #   test(dataset, prediction_sample=N, aligned_input=True)
1070

1071
        # Case predict fewer samples using the dataset
1072
        # The states are reset only if the input is present.
1073
        #   test(dataset, num_of_samples=M, aligned_input=True)
1074

1075
        # Case predict fewer samples and reset every prediction_sample
1076
        # The states are reset only if the input is present.
1077
        #   test(dataset, prediction_sample=N, num_of_samples=M, aligned_input=True)
1078

1079
    def test_parameters_predict_closed_loop_perdict(self):
1✔
1080
        NeuObj.reset_count()
1✔
1081
        input1 = Input('in1')
1✔
1082
        W = Parameter('W', values=[[1], [2], [3]])
1✔
1083
        out = Output('out',Fir(parameter=W)(input1.sw(3)))
1✔
1084

1085
        test = Modely(visualizer=None, seed=42)
1✔
1086
        test.addModel('model', [out])
1✔
1087
        test.neuralizeModel()
1✔
1088

1089
        result = test({'in1':[1,2,3]})
1✔
1090
        self.assertEqual((1,), np.array(result['out']).shape)
1✔
1091
        self.assertEqual([14.0], result['out'])
1✔
1092

1093
        result = test({'in1': [1, 2, 3]},closed_loop={'in1':'out'})
1✔
1094
        self.assertEqual((1,), np.array(result['out']).shape)
1✔
1095
        self.assertEqual([14.0], result['out'])
1✔
1096

1097
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'})
1✔
1098
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1099
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1], result['out'])
1✔
1100

1101
        result = test({'in1': [1, 2, 3, 4, 5]}, closed_loop = {'in1':'out'}, num_of_samples = 5)
1✔
1102
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1103
        self.assertEqual([14.0, 3*4+2*3+2*1, 5*3+4*2+3*1, 26*3+5*2+4*1, 92*3+26*2+1*5], result['out'])
1✔
1104

1105
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=None)
1✔
1106
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1107
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1], result['out'])
1✔
1108

1109
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=0)
1✔
1110
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1111
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1], result['out'])
1✔
1112

1113
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=1)
1✔
1114
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1115
        self.assertEqual([14.0,3*14+2*3+2*1,5*3+4*2+3*1], result['out'])
1✔
1116

1117
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=2)
1✔
1118
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1119
        self.assertEqual([14.0,3*14+2*3+2*1,50*3+14*2+3*1], result['out'])
1✔
1120

1121
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=3)
1✔
1122
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1123
        self.assertEqual([14.0,3*14+2*3+2*1,50*3+14*2+3*1], result['out'])
1✔
1124

1125
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=None, num_of_samples = 5)
1✔
1126
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1127
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1,0*3+5*2+4*1,0*3+0*2+5*1], result['out'])
1✔
1128

1129
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=0, num_of_samples = 5)
1✔
1130
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1131
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1,0*3+5*2+4*1,0*3+0*2+5*1], result['out'])
1✔
1132

1133
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=1, num_of_samples = 5)
1✔
1134
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1135
        self.assertEqual([14.0,3*14+2*3+2*1,5*3+4*2+3*1,26*3+5*2+4*1,0*3+0*2+5*1], result['out'])
1✔
1136

1137
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=2, num_of_samples = 5)
1✔
1138
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1139
        self.assertEqual([14.0,3*14+2*3+2*1,50*3+14*2+3*1,0*3+5*2+4*1,14*3+0*2+5*1], result['out'])
1✔
1140

1141
        result = test({'in1': [1, 2, 3, 4, 5]},closed_loop={'in1':'out'}, prediction_samples=3, num_of_samples = 5)
1✔
1142
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1143
        self.assertEqual([14.0,3*14+2*3+2*1,50*3+14*2+3*1,181*3+50*2+14*1,0*3+0*2+5*1], result['out'])
1✔
1144

1145
    def test_parameters_predict_closed_loop(self):
1✔
1146
        NeuObj.reset_count()
1✔
1147
        input1 = State('in1')
1✔
1148
        W = Parameter('W', values=[[1], [2], [3]])
1✔
1149
        out = Output('out',Fir(parameter=W)(input1.sw(3)))
1✔
1150

1151
        test = Modely(visualizer=None, seed=42)
1✔
1152
        test.addModel('model', [out])
1✔
1153
        test.addClosedLoop(out, input1)
1✔
1154
        test.neuralizeModel()
1✔
1155

1156
        result = test({'in1':[1,2,3]})
1✔
1157
        self.assertEqual((1,), np.array(result['out']).shape)
1✔
1158
        self.assertEqual([14.0], result['out'])
1✔
1159

1160
        test.resetStates()
1✔
1161
        result = test({'in1': [1, 2, 3]})
1✔
1162
        self.assertEqual((1,), np.array(result['out']).shape)
1✔
1163
        self.assertEqual([14.0], result['out'])
1✔
1164

1165
        test.resetStates()
1✔
1166
        result = test({'in1': [1, 2, 3, 4, 5]})
1✔
1167
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1168
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1], result['out'])
1✔
1169

1170
        test.resetStates()
1✔
1171
        result = test({'in1': [1, 2, 3, 4, 5]}, num_of_samples = 5)
1✔
1172
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1173
        self.assertEqual([14.0, 3*4+2*3+2*1, 5*3+4*2+3*1, 26*3+5*2+4*1, 92*3+26*2+1*5], result['out'])
1✔
1174

1175
        test.resetStates()
1✔
1176
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=None)
1✔
1177
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1178
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1], result['out'])
1✔
1179

1180
        test.resetStates()
1✔
1181
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=0)
1✔
1182
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1183
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1], result['out'])
1✔
1184

1185
        test.resetStates()
1✔
1186
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=1)
1✔
1187
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1188
        self.assertEqual([14.0,3*14+2*3+2*1,5*3+4*2+3*1], result['out'])
1✔
1189

1190
        test.resetStates()
1✔
1191
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=2)
1✔
1192
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1193
        self.assertEqual([14.0,3*14+2*3+2*1,50*3+14*2+3*1], result['out'])
1✔
1194

1195
        test.resetStates()
1✔
1196
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=3)
1✔
1197
        self.assertEqual((3,), np.array(result['out']).shape)
1✔
1198
        self.assertEqual([14.0,3*14+2*3+2*1,50*3+14*2+3*1], result['out'])
1✔
1199

1200
        test.resetStates()
1✔
1201
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=None, num_of_samples = 5)
1✔
1202
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1203
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1,0*3+5*2+4*1,0*3+0*2+5*1], result['out'])
1✔
1204

1205
        test.resetStates()
1✔
1206
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=0, num_of_samples = 5)
1✔
1207
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1208
        self.assertEqual([14.0,3*4+2*3+2*1,5*3+4*2+3*1,0*3+5*2+4*1,0*3+0*2+5*1], result['out'])
1✔
1209

1210
        test.resetStates()
1✔
1211
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=1, num_of_samples = 5)
1✔
1212
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1213
        self.assertEqual([14.0,3*14+2*3+2*1,5*3+4*2+3*1,26*3+5*2+4*1,0*3+0*2+5*1], result['out'])
1✔
1214

1215
        test.resetStates()
1✔
1216
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=2, num_of_samples = 5)
1✔
1217
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1218
        self.assertEqual([14.0,3*14+2*3+2*1,50*3+14*2+3*1,0*3+5*2+4*1,14*3+0*2+5*1], result['out'])
1✔
1219

1220
        test.resetStates()
1✔
1221
        result = test({'in1': [1, 2, 3, 4, 5]}, prediction_samples=3, num_of_samples = 5)
1✔
1222
        self.assertEqual((5,), np.array(result['out']).shape)
1✔
1223
        self.assertEqual([14.0,3*14+2*3+2*1,50*3+14*2+3*1,181*3+50*2+14*1,0*3+0*2+5*1], result['out'])
1✔
1224

1225

1226
if __name__ == '__main__':
1✔
1227
    unittest.main()
×
1228

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

© 2025 Coveralls, Inc