aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/DataFeatures.py
blob: 3e372fb147ef57d1532332b92ea2f4fa73b1a5b5 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python

"""Timestamped data features."""

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

from typing import TypeVar, Tuple, Any
from dataclasses import dataclass, field
import importlib
from inspect import getmembers
import collections
import json
import ast
import bisect
import threading
import math

import pandas
import numpy
import matplotlib.pyplot as mpyplot
import matplotlib.patches as mpatches

TimeStampType = TypeVar('TimeStamp', int, float)
"""Type definition for timestamp as integer or float values."""

DataType = TypeVar('Data')
"""Type definition for data to store anything in time."""

TimeStampedBufferType = TypeVar('TimeStampedBuffer', bound="TimeStampedBuffer")
# Type definition for type annotation convenience

def as_dict(dataclass_object) -> dict:
    """
    Get dataclass object fields's values as a dictionary.

    Returns:
        values: dictionary of dataclass fields's values
    """

    # Get data class fields names
    fields_names = []
    for member_name, member_value in getmembers(dataclass_object):
        if member_name == '__dataclass_fields__':
            fields_names = member_value.keys()

    # Copy fields values
    return {name: vars(dataclass_object)[name] for name in fields_names}

def module_path(obj) -> str:
    """
    Get object module path.

    Returns:
        module path
    """
    return obj.__class__.__module__

class JsonEncoder(json.JSONEncoder):
    """Specific ArGaze JSON Encoder."""

    def default(self, obj):
        """default implementation to serialize object."""

        # numpy cases
        if isinstance(obj, numpy.integer):
            return int(obj)

        elif isinstance(obj, numpy.floating):
            return float(obj)

        elif isinstance(obj, numpy.ndarray):
            return obj.tolist()

        # default case
        try:

            return json.JSONEncoder.default(self, obj)

        # class case
        except:

            # ignore attribute starting with _
            public_dict = {}

            for k, v in vars(obj).items():
                
                if not k.startswith('_'):
                    
                    # numpy cases
                    if isinstance(v, numpy.integer):
                        v = int(v)

                    elif isinstance(v, numpy.floating):
                        v = float(v)

                    elif isinstance(v, numpy.ndarray):
                        v = v.tolist()

                    public_dict[k] = v

            return public_dict

class SharedObject():
    """Enable multiple threads sharing."""

    def __init__(self):
        self._lock = threading.Lock()
        self._timestamp = math.nan
        self._token = None

    def acquire(self):
        self._lock.acquire()

    def release(self):
        self._lock.release()

    def locked(self) -> bool:
        return self._lock.locked()

    @property
    def timestamp(self) -> int|float:
        """Get timestamp"""

        self._lock.acquire()
        timestamp = self._timestamp
        self._lock.release()

        return timestamp

    @timestamp.setter
    def timestamp(self, timestamp: int|float):
        """Set timestamp"""

        self._lock.acquire()
        self._timestamp = timestamp
        self._lock.release()

    def untimestamp(self):
        """Reset timestamp"""

        self._lock.acquire()
        self._timestamp = math.nan
        self._lock.release()

    @property
    def timestamped(self) -> bool:
        """Is the object timestamped?"""

        self._lock.acquire()
        timestamped = not math.isnan(self._timestamp)
        self._lock.release()

        return timestamped

    @property
    def token(self) -> any:
        """Get token"""

        self._lock.acquire()
        token = self._token
        self._lock.release()

        return token

    @token.setter
    def token(self, token: any):
        """Set token"""

        self._lock.acquire()
        self._token = token
        self._lock.release()

class TimeStampedBuffer(collections.OrderedDict):
    """Ordered dictionary to handle timestamped data.
    ```
        {
            timestamp1: data1,
            timestamp2: data2,
            ...
        }
    ```

    !!! warning
    
        Timestamps must be numbers.

    !!! warning "Timestamps are not sorted internally"
        
        Data are considered to be stored according at their coming time.
    """

    def __new__(cls, args = None):
        """Inheritance"""

        return super(TimeStampedBuffer, cls).__new__(cls)

    def __setitem__(self, ts: TimeStampType, data: DataType):
        """Store data at given timestamp."""

        assert(type(ts) == int or type(ts) == float)

        super().__setitem__(ts, data)

    def __repr__(self):
        """String representation"""

        return json.dumps(self, ensure_ascii=False, cls=JsonEncoder)

    def __str__(self):
        """String representation"""

        return json.dumps(self, ensure_ascii=False, cls=JsonEncoder)

    def append(self, timestamped_buffer: TimeStampedBufferType) -> TimeStampedBufferType:
        """Append a timestamped buffer."""

        for ts, value in timestamped_buffer.items():
            self[ts] = value

        return self

    @property
    def first(self) -> Tuple[TimeStampType, DataType]:
        """Easing access to first item."""

        return list(self.items())[0]

    def pop_first(self) -> Tuple[TimeStampType, DataType]:
        """Easing FIFO access mode."""

        return self.popitem(last=False)

    def pop_last_until(self, ts: TimeStampType) -> Tuple[TimeStampType, DataType]:
        """Pop all item until a given timestamped value and return the first after."""

        # get last item before given timestamp
        earliest_ts, earliest_value = self.get_last_until(ts)

        first_ts, first_value = self.first

        while first_ts < earliest_ts:
            self.pop_first()
            first_ts, first_value = self.first
            
        return first_ts, first_value

    def pop_last_before(self, ts: TimeStampType) -> Tuple[TimeStampType, DataType]:
        """Pop all item before a given timestamped value and return the last one."""

        # get last item before given timestamp
        earliest_ts, earliest_value = self.get_last_before(ts)

        popep_ts, poped_value = self.pop_first()

        while popep_ts != earliest_ts:
            popep_ts, poped_value = self.pop_first()

        return popep_ts, poped_value

    @property
    def last(self) -> Tuple[TimeStampType, DataType]:
        """Easing access to last item."""

        return list(self.items())[-1]

    def pop_last(self) -> Tuple[TimeStampType, DataType]:
        """Easing FIFO access mode."""

        return self.popitem(last=True)

    def get_first_from(self, ts) -> Tuple[TimeStampType, DataType]:
        """Retreive first item timestamp from a given timestamp value."""

        ts_list = list(self.keys())
        first_from_index = bisect.bisect_left(ts_list, ts)

        if first_from_index < len(self):

            first_from_ts = ts_list[first_from_index]
            
            return first_from_ts, self[first_from_ts]
            
        else:
            
            raise KeyError(f'No data stored after {ts} timestamp.')

    def get_last_before(self, ts) -> Tuple[TimeStampType, DataType]:
        """Retreive last item timestamp before a given timestamp value."""

        ts_list = list(self.keys())
        last_before_index = bisect.bisect_left(ts_list, ts) - 1

        if last_before_index >= 0:

            last_before_ts = ts_list[last_before_index]
            
            return last_before_ts, self[last_before_ts]
            
        else:
            
            raise KeyError(f'No data stored before {ts} timestamp.')
        

    def get_last_until(self, ts) -> Tuple[TimeStampType, DataType]:
        """Retreive last item timestamp until a given timestamp value."""

        ts_list = list(self.keys())
        last_until_index = bisect.bisect_right(ts_list, ts) - 1

        if last_until_index >= 0:

            last_until_ts = ts_list[last_until_index]
            
            return last_until_ts, self[last_until_ts]
            
        else:
            
            raise KeyError(f'No data stored until {ts} timestamp.')

    @classmethod
    def from_json(self, json_filepath: str) -> TimeStampedBufferType:
        """Create a TimeStampedBuffer from .json file."""

        with open(json_filepath, encoding='utf-8') as ts_buffer_file:

            json_buffer = json.load(ts_buffer_file)

            return TimeStampedBuffer({ast.literal_eval(ts_str): json_buffer[ts_str] for ts_str in json_buffer})

    def to_json(self, json_filepath: str):
        """Save a TimeStampedBuffer to .json file."""

        with open(json_filepath, 'w', encoding='utf-8') as ts_buffer_file:

            json.dump(self, ts_buffer_file, ensure_ascii=False, cls=JsonEncoder)

    @classmethod
    def from_dataframe(self, dataframe: pandas.DataFrame, exclude=[]) -> TimeStampedBufferType:
        """Create a TimeStampedBuffer from [Pandas DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html)."""

        dataframe.drop(exclude, inplace=True, axis=True)

        assert(dataframe.index.name == 'timestamp')

        return TimeStampedBuffer(dataframe.to_dict('index'))

    def as_dataframe(self, exclude=[], split={}) -> pandas.DataFrame:
        """Convert as [Pandas DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html).  
        
        The optional *split* argument allows tuple values to be stored in dedicated columns.  
        For example: to convert {"point": (0, 0)} data as two separated "x" and "y" columns, use split={"point": ["x", "y"]}  

        !!! warning "Values must be dictionaries"
        
            Each key is stored as a column name.

        !!! note

            Timestamps are stored as index column called 'timestamp'.
        """

        df = pandas.DataFrame.from_dict(self.values())

        # Exclude columns
        df.drop(exclude, inplace=True, axis=True)

        # Split columns
        if len(split) > 0:

            splited_columns = []
            
            for column in df.columns:

                if column in split.keys():

                    df[split[column]] = pandas.DataFrame(df[column].tolist(), index=df.index)
                    df.drop(column, inplace=True, axis=True)

                    for new_column in split[column]:

                        splited_columns.append(new_column)

                else:

                    splited_columns.append(column)

            # Reorder splited columns
            df = df[splited_columns]

        # Append timestamps as index column
        df['timestamp'] = self.keys()
        df.set_index('timestamp', inplace=True)

        return df

    def plot(self, names=[], colors=[], split={}, samples=None) -> list:
        """Plot as [matplotlib](https://matplotlib.org/) time chart."""

        df = self.as_dataframe(split=split)
        legend_patches = []

        # decimate data
        if samples != None:

            if samples < len(df):

                step = int(len(df) / samples) + 1
                df = df.iloc[::step, :]

        for name, color in zip(names, colors):

            markerline, stemlines, baseline = mpyplot.stem(df.index, df[name])
            mpyplot.setp(markerline, color=color, linewidth=1, markersize = 1)
            mpyplot.setp(stemlines, color=color, linewidth=1)
            mpyplot.setp(baseline, color=color, linewidth=1)

            legend_patches.append(mpatches.Patch(color=color, label=name.upper()))

        return legend_patches

class DataDictionary(dict):
    """Enable dot.notation access to dictionary attributes"""

    __getattr__ = dict.get
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

# Import libraries that can be used in selector or formatter codes
from argaze import GazeFeatures

TimeStampedDataLoggerType = TypeVar('TimeStampedDataLogger', bound="TimeStampedDataLogger")
# Type definition for type annotation convenience

@dataclass
class TimeStampedDataLogger():
    """Abstract class to define what should provide a timestamped data logger."""

    selector: str = field(default='True')
    """Code evaluated to handle log under a condition. Default 'True' string means that all incoming data will be accepted."""

    formatter: str = field(default='')
    """Code evaluated to edit the log."""

    @classmethod
    def from_dict(self, logger_data: dict) -> TimeStampedDataLoggerType:
        """Load timestamped data logger from dictionary.

        Parameters:
            logger_module_path: class name to load
            logger_parameters: attributes to load
        """

        logger_module_path, logger_parameters = logger_data.popitem()

        # Prepend argaze.DataLog path when a single name is provided
        if len(logger_module_path.split('.')) == 1:
            logger_module_path = f'argaze.DataLog.{logger_module_path}'

        logger_module = importlib.import_module(logger_module_path)
        return logger_module.TimeStampedDataLogger(**logger_parameters)

    def emit(self, timestamp: TimeStampType, data: DataDictionary) -> Any:
        """Apply selector code to decide if data have to be logged, then apply formatter code before to call specific logger handle method."""

        data['timestamp'] = timestamp

        if eval(self.selector, globals(), data):

            self.handle(eval(self.formatter, globals(), data))

    def handle(self, formatted_log: any):
        """Handle formatted log emission to destination."""

        raise NotImplementedError('handle() method not implemented')