aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test/DataFeatures.py
blob: a0031bc001affda422337a97686a2a4b547bfab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
""" 

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
"""

__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

import unittest
from dataclasses import dataclass, field
import os

from argaze import DataFeatures

import pandas
import numpy

class BasicDataClass(DataFeatures.TimestampedObject):
    """Define a basic dataclass for testing purpose."""

    def __init__(self, value: tuple = (), message: str = None, **kwargs):

        DataFeatures.TimestampedObject.__init__(self, **kwargs)

        self.__value = value
        self.__message = message

    @property
    def value(self):
        return self.__value

    @property
    def message(self):
        return self.__message
    
def random_data_list(size):
    """ Generate a random TimestampedObjectsList for testing purpose.
        Timestamps are current time.
        Values are tuples containing an expected value and a random value.
    """
    import random
    import time

    data_list = []
    
    for i in range(0, size):

        # Edit data
        random_data = BasicDataClass((i, random.random()), f'test_{i}')
        
        # Timestamp data
        random_data.timestamp = time.time()

        # Store data
        data_list.append(random_data)

        time.sleep(0.0001)

    return DataFeatures.TimestampedObjectsList(BasicDataClass, data_list)

# DEBUG
'''
print('test_as_dataframe: export ts_data_file.json')
current_directory = os.path.dirname(os.path.abspath(__file__))
json_filepath = os.path.join(current_directory, 'utils/ts_data_file.json')
random_data_list(10).to_json(json_filepath)
'''

class TestTimestampedObjectsListClass(unittest.TestCase):
    """Test TimestampedObjectsList class."""

    def test_new(self):
        """Test TimestampedObjectsList creation."""
        
        # Check TimestampedObjectsList length after creation
        self.assertEqual(len(DataFeatures.TimestampedObjectsList(BasicDataClass)), 0)

        # Check TimestampedObjectsList timestamps after creation
        self.assertEqual(DataFeatures.TimestampedObjectsList(BasicDataClass).timestamps(), [])

        # Check TimestampedObjectsList items after creation
        self.assertEqual(DataFeatures.TimestampedObjectsList(BasicDataClass), [])

        # Check that TimestampedObjectsList creation fails when data are not timestamped
        with self.assertRaises(ValueError):

            data_list = [BasicDataClass((0, 0))]
            DataFeatures.TimestampedObjectsList(BasicDataClass, data_list)
        
    def test_as_dataframe(self):
        """Test TimestampedObjectsList as_dataframe method."""

        data_frame = random_data_list(10).as_dataframe()

        # Check dataframe conversion
        self.assertEqual(data_frame.index.name, "timestamp")
        self.assertEqual(data_frame.index.size, 10)
        self.assertEqual(data_frame.columns.size, 2)
        self.assertEqual(data_frame.index.dtype, 'float64')
        self.assertEqual(data_frame["value"].dtype, 'object')
        self.assertEqual(data_frame["message"].dtype, 'object')

        # Check data exclusion option
        data_frame = random_data_list(10).as_dataframe(exclude=["value"])

        self.assertEqual(data_frame.index.name, "timestamp")
        self.assertEqual(data_frame.index.size, 10)
        self.assertEqual(data_frame.columns.size, 1)

        # Check dataframe split option
        data_frame = random_data_list(10).as_dataframe(split={"value": ["value_0", "value_1"]})

        self.assertEqual(data_frame.index.name, "timestamp")
        self.assertEqual(data_frame.index.size, 10)
        self.assertEqual(data_frame.columns.size, 3)
        self.assertEqual(data_frame["value_0"].dtype, 'int64')
        self.assertEqual(data_frame["value_1"].dtype, 'float64')
    
    def test_from_dataframe(self):
        """Test TimestampedObjectsList creation from pandas dataframe."""

        data_frame = random_data_list(10).as_dataframe()

        # Check dataframe conversion
        data_list = DataFeatures.TimestampedObjectsList.from_dataframe(BasicDataClass, data_frame)

        self.assertEqual(len(data_list), 10)
    
    def test_from_json(self):
        """Test TimestampedObjectsList creation from json file."""

        # Edit dataframe csv file path
        current_directory = os.path.dirname(os.path.abspath(__file__))
        json_filepath = os.path.join(current_directory, 'utils/ts_data_file.json')

        # Load TimestampedObjectsList from json file
        data_list = DataFeatures.TimestampedObjectsList.from_json(BasicDataClass, json_filepath)

        self.assertEqual(len(data_list), 10)
        self.assertEqual(type(data_list[0]), BasicDataClass)
    
    def test___repr__(self):
        """Test TimestampedObjectsList string representation."""

        self.assertEqual(repr(DataFeatures.TimestampedObjectsList(BasicDataClass)), "[]")

        data_list = [BasicDataClass((0, 0), 'test', timestamp=0)]
        self.assertEqual(repr(DataFeatures.TimestampedObjectsList(BasicDataClass, data_list)), "[{\"value\": [0, 0], \"message\": \"test\", \"timestamp\": 0}]")

    def test___str__(self):
        """Test TimestampedObjectsList string representation."""

        self.assertEqual(str(DataFeatures.TimestampedObjectsList(BasicDataClass)), "[]")

        data_list = [BasicDataClass((0, 0), 'test', timestamp=0)]
        self.assertEqual(str(DataFeatures.TimestampedObjectsList(BasicDataClass, data_list)), "[{\"value\": [0, 0], \"message\": \"test\", \"timestamp\": 0}]")

    def test_append(self):
        """Test TimestampedObjectsList append method."""

        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass)
        next_data = BasicDataClass((0, 0), 'test', timestamp=0)

        data_list.append(next_data)

        self.assertEqual(len(data_list), 1)
        self.assertEqual(data_list.timestamps(), [0])
    
    def test_pop_first(self):
        """Test TimestampedObjectsList pop_first method."""

        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
            [
                BasicDataClass(message="A", timestamp=0),
                BasicDataClass(message="B", timestamp=1)
            ])

        first = data_list.pop(0)

        self.assertEqual(len(data_list), 1)
        self.assertEqual(first.message, "A")
        self.assertEqual(first.timestamp, 0)
        self.assertEqual(data_list[0].message, "B")
        self.assertEqual(data_list[0].timestamp, 1)

    def test_pop_last_until(self):
        """Test TimestampedObjectsList pop_last_until method."""

        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
        [
            BasicDataClass(message="A", timestamp=0),
            BasicDataClass(message="B", timestamp=1),
            BasicDataClass(message="C", timestamp=2),
            BasicDataClass(message="D", timestamp=3)
        ])

        # Check pop until an existing timestamp
        pop_last_until_2 = data_list.pop_last_until(2)

        self.assertEqual(pop_last_until_2.message, "C")
        self.assertEqual(pop_last_until_2.timestamp, 2)
        self.assertEqual(len(data_list), 2)
        self.assertEqual(data_list[0].message, "C")
        self.assertEqual(data_list[0].timestamp, 2)

        # Check first until an none existing timestamp
        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
        [
            BasicDataClass(message="A", timestamp=0),
            BasicDataClass(message="B", timestamp=1),
            BasicDataClass(message="C", timestamp=2),
            BasicDataClass(message="D", timestamp=3)
        ])

        pop_last_until_1dot5 = data_list.pop_last_until(1.5)

        self.assertEqual(pop_last_until_1dot5.message, "B")
        self.assertEqual(pop_last_until_1dot5.timestamp, 1)
        self.assertEqual(len(data_list), 3)
        self.assertEqual(data_list[0].message, "B")
        self.assertEqual(data_list[0].timestamp, 1)

    def test_pop_last_before(self):
        """Test TimestampedObjectsList pop_last_before method."""

        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
        [
            BasicDataClass(message="A", timestamp=0),
            BasicDataClass(message="B", timestamp=1),
            BasicDataClass(message="C", timestamp=2),
            BasicDataClass(message="D", timestamp=3)
        ])

        # Check pop until an existing timestamp
        last_before_2 = data_list.pop_last_before(2)

        self.assertEqual(last_before_2.message, "B")
        self.assertEqual(last_before_2.timestamp, 1)
        self.assertEqual(len(data_list), 2)
        self.assertEqual(data_list[0].message, "C")
        self.assertEqual(data_list[0].timestamp, 2)

        # Check pop until an none existing timestamp
        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
        [
            BasicDataClass(message="A", timestamp=0),
            BasicDataClass(message="B", timestamp=1),
            BasicDataClass(message="C", timestamp=2),
            BasicDataClass(message="D", timestamp=3)
        ])

        first_until_1dot5 = data_list.pop_last_before(1.5)

        self.assertEqual(first_until_1dot5.message, "B")
        self.assertEqual(first_until_1dot5.timestamp, 1)
        self.assertEqual(len(data_list), 2)
        self.assertEqual(data_list[0].message, "C")
        self.assertEqual(data_list[0].timestamp, 2)

    def test_pop_last(self):
        """Test TimestampedObjectsList pop_last method."""

        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
            [
                BasicDataClass(message="A", timestamp=0),
                BasicDataClass(message="B", timestamp=1)
            ])

        last = data_list.pop(-1)

        self.assertEqual(len(data_list), 1)
        self.assertEqual(last.message, "B")
        self.assertEqual(last.timestamp, 1)
        self.assertEqual(data_list[0].message, "A")
        self.assertEqual(data_list[0].timestamp, 0)

    def test_get_first_from(self):
        """Test TimestampedObjectsList get_first_from method."""

        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
        [
            BasicDataClass(message="A", timestamp=0),
            BasicDataClass(message="B", timestamp=1),
            BasicDataClass(message="C", timestamp=2),
            BasicDataClass(message="D", timestamp=3)
        ])

        get_first_from_1 = data_list.get_first_from(1)

        self.assertEqual(get_first_from_1.message, "B")
        self.assertEqual(get_first_from_1.timestamp, 1)
        self.assertEqual(len(data_list), 4)

        get_first_from_1dot5 = data_list.get_first_from(1.5)

        self.assertEqual(get_first_from_1dot5.message, "C")
        self.assertEqual(get_first_from_1dot5.timestamp, 2)

        get_first_from_0 = data_list.get_first_from(0)

        self.assertEqual(get_first_from_0.message, "A")
        self.assertEqual(get_first_from_0.timestamp, 0)

        # Check that accessing to lately timestamp fails
        with self.assertRaises(KeyError):

            data_list.get_first_from(4)

    def test_get_last_before(self):
        """Test TimestampedObjectsList get_last_before method."""

        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
        [
            BasicDataClass(message="A", timestamp=0),
            BasicDataClass(message="B", timestamp=1),
            BasicDataClass(message="C", timestamp=2),
            BasicDataClass(message="D", timestamp=3)
        ])

        get_last_before_2 = data_list.get_last_before(2)

        self.assertEqual(get_last_before_2.message, "B")
        self.assertEqual(get_last_before_2.timestamp, 1)
        self.assertEqual(len(data_list), 4)

        get_last_before_1dot5 = data_list.get_last_before(1.5)

        self.assertEqual(get_last_before_1dot5.message,"B")
        self.assertEqual(get_last_before_1dot5.timestamp, 1)

        get_last_before_4 = data_list.get_last_before(4)

        self.assertEqual(get_last_before_4.message, "D")
        self.assertEqual(get_last_before_4.timestamp, 3)

        # Check that accessing to early timestamp fails
        with self.assertRaises(KeyError):

            data_list.get_last_before(-1)  
      
    def test_get_last_until(self):
        """Test TimestampedObjectsList get_last_until method."""

        data_list = DataFeatures.TimestampedObjectsList(BasicDataClass, \
        [
            BasicDataClass(message="A", timestamp=0),
            BasicDataClass(message="B", timestamp=1),
            BasicDataClass(message="C", timestamp=2),
            BasicDataClass(message="D", timestamp=3)
        ])

        get_last_until_2 = data_list.get_last_until(2)

        self.assertEqual(get_last_until_2.message, "C")
        self.assertEqual(get_last_until_2.timestamp, 2)
        self.assertEqual(len(data_list), 4)

        get_last_until_1dot5 = data_list.get_last_until(1.5)

        self.assertEqual(get_last_until_1dot5.message, "B")
        self.assertEqual(get_last_until_1dot5.timestamp, 1)

        get_last_until_4 = data_list.get_last_until(4)

        self.assertEqual(get_last_until_4.message, "D")
        self.assertEqual(get_last_until_4.timestamp, 3)

        # Check that accessing to early timestamp fails
        with self.assertRaises(KeyError):

            data_list.get_last_until(-1)

if __name__ == '__main__':

    unittest.main()