corebridge 0.2.1__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: corebridge
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Bridge for Stactics AICore
5
5
  Home-page: https://github.com/fenke/corebridge
6
6
  Author: Fenke Meijer
@@ -0,0 +1,157 @@
1
+ __version__ = "0.2.1"
2
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_aicorebridge.ipynb.
3
+
4
+ # %% auto 0
5
+ __all__ = ['syslog', 'AICoreModule']
6
+
7
+ # %% ../nbs/01_aicorebridge.ipynb 4
8
+ import typing
9
+ import logging
10
+ import inspect
11
+ import datetime
12
+ import json
13
+ import pandas as pd
14
+ import numpy as np
15
+
16
+ from fastcore.basics import patch_to, patch
17
+
18
+
19
+ # %% ../nbs/01_aicorebridge.ipynb 5
20
+ syslog = logging.getLogger(__name__)
21
+
22
+ # %% ../nbs/01_aicorebridge.ipynb 6
23
+ try:
24
+ print(f"Loading {__name__} from {__file__}")
25
+ except:
26
+ pass
27
+
28
+ # %% ../nbs/01_aicorebridge.ipynb 7
29
+ class AICoreModule(): pass
30
+
31
+ # %% ../nbs/01_aicorebridge.ipynb 8
32
+ @patch
33
+ def __init__(self:AICoreModule,
34
+ processor:typing.Callable, # data processing function
35
+ save_dir:str, # path where the module can keep files
36
+ assets_dir:str,
37
+ *args, **kwargs):
38
+
39
+ self.init_time = datetime.datetime.utcnow()
40
+ self.save_dir = save_dir
41
+ self.assets_dir = assets_dir
42
+ self._init_processor(processor)
43
+
44
+ self.init_args = args
45
+ self.init_kwargs = kwargs
46
+
47
+
48
+
49
+ # %% ../nbs/01_aicorebridge.ipynb 9
50
+ @patch
51
+ def _init_processor(
52
+ self:AICoreModule,
53
+ processor:typing.Callable):
54
+ """Initializes processor related variables on self"""
55
+
56
+ self.processor = processor
57
+ self.processor_signature = inspect.signature(self.processor)
58
+ self.processor_params = dict(self.processor_signature.parameters)
59
+ self.return_param = self.processor_params.pop('return', None)
60
+ self.data_param, *self.call_params = list(self.processor_params.keys())
61
+
62
+
63
+ # %% ../nbs/01_aicorebridge.ipynb 10
64
+ @patch
65
+ def call_processor(self:AICoreModule, calldata, **callargs):
66
+ return self.processor(calldata, **callargs)
67
+
68
+
69
+ # %% ../nbs/01_aicorebridge.ipynb 11
70
+ @patch
71
+ def infer(self:AICoreModule, data:dict, *_, **kwargs):
72
+ try:
73
+
74
+ msg=[
75
+ f"{self.processor.__name__}({self.processor_signature})",
76
+ f"init_args: {self.init_args}, init_kwargs: {self.init_kwargs}",
77
+ ]
78
+
79
+ lastSeen = kwargs.pop('lastSeen', False)
80
+ recordformat = kwargs.pop('format', "records").lower()
81
+ reversed = kwargs.pop('reversed', False)
82
+ timezone = kwargs.get('timezone', 'UTC')
83
+ msg.append(f"lastSeen: {lastSeen}, recordformat: {recordformat}, timezone: {timezone}")
84
+
85
+ calldata = self.get_call_data(
86
+ data,
87
+ recordformat=recordformat,
88
+ timezone=timezone,
89
+ reversed=reversed)
90
+
91
+ msg.append(f"calldata shape: {calldata.shape}")
92
+
93
+ callargs = self.get_callargs(**kwargs)
94
+
95
+ for arg, val in callargs.items():
96
+ msg.append(f"{arg}: {val}")
97
+
98
+ result = self.call_processor(calldata, **callargs)
99
+ msg.append(f"result shape: {result.shape}")
100
+
101
+ return {
102
+ 'msg':msg,
103
+ 'data': timeseries_dataframe_to_datadict(
104
+ result if not lastSeen else result[-1:],
105
+ recordformat=recordformat,
106
+ timezone=timezone,
107
+ reversed=reversed)
108
+ }
109
+ except Exception as err:
110
+ return {
111
+ 'msg': f"Unexpected {err=}, {type(err)=}",
112
+ 'data': []
113
+ }
114
+
115
+
116
+ # %% ../nbs/01_aicorebridge.ipynb 12
117
+ @patch
118
+ def get_callargs(self:AICoreModule, **kwargs):
119
+ "Get arguments for the processor call"
120
+
121
+ # Remove null / None values
122
+ kwargs = {k:v for k,v in kwargs.items() if v is not None}
123
+
124
+ metadata = kwargs.pop('metadata', {}) # TODO: historic metadata
125
+
126
+ return {
127
+ K:self.processor_signature.parameters[K].annotation(kwargs.get(K,metadata.get(K, self.init_kwargs.get(K, self.processor_signature.parameters[K].default))))
128
+ for K in self.call_params
129
+ }
130
+
131
+
132
+ # %% ../nbs/01_aicorebridge.ipynb 13
133
+ @patch
134
+ def get_call_data(
135
+ self:AICoreModule,
136
+ data:dict,
137
+ recordformat='records',
138
+ timezone='UTC',
139
+ reversed=False):
140
+
141
+ "Convert data to the processor signature"
142
+
143
+ df = set_time_index_zone(timeseries_dataframe_from_datadict(
144
+ data, ['datetimemeasure', 'time'], recordformat), timezone)
145
+
146
+ if reversed:
147
+ df = df[::-1]
148
+
149
+ if self.processor_params[self.data_param].annotation == pd.DataFrame:
150
+ return df
151
+ elif len(df.columns) > 1:
152
+ df.index = (df.index - datetime.datetime(1970,1,1, tzinfo=datetime.timezone.utc)) / datetime.timedelta(seconds=1)
153
+ return df.to_records(index=True)
154
+ else:
155
+ df.index = (df.index - datetime.datetime(1970,1,1, tzinfo=datetime.timezone.utc)) / datetime.timedelta(seconds=1)
156
+ return df.reset_index().to_numpy()
157
+
@@ -13,7 +13,7 @@ import pandas as pd
13
13
  import numpy as np
14
14
 
15
15
  from fastcore.basics import patch_to, patch
16
- import corebridge
16
+ from core import *
17
17
 
18
18
 
19
19
  # %% ../nbs/01_aicorebridge.ipynb 5
@@ -100,7 +100,7 @@ def infer(self:AICoreModule, data:dict, *_, **kwargs):
100
100
 
101
101
  return {
102
102
  'msg':msg,
103
- 'data': corebridge.core.timeseries_dataframe_to_datadict(
103
+ 'data': timeseries_dataframe_to_datadict(
104
104
  result if not lastSeen else result[-1:],
105
105
  recordformat=recordformat,
106
106
  timezone=timezone,
@@ -140,7 +140,7 @@ def get_call_data(
140
140
 
141
141
  "Convert data to the processor signature"
142
142
 
143
- df = corebridge.core.set_time_index_zone(corebridge.core.timeseries_dataframe_from_datadict(
143
+ df = set_time_index_zone(timeseries_dataframe_from_datadict(
144
144
  data, ['datetimemeasure', 'time'], recordformat), timezone)
145
145
 
146
146
  if reversed:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: corebridge
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Bridge for Stactics AICore
5
5
  Home-page: https://github.com/fenke/corebridge
6
6
  Author: Fenke Meijer
@@ -5,7 +5,7 @@
5
5
  ### Python library ###
6
6
  repo = corebridge
7
7
  lib_name = %(repo)s
8
- version = 0.2.1
8
+ version = 0.2.2
9
9
  min_python = 3.7
10
10
  license = apache2
11
11
  black_formatting = False
@@ -1 +0,0 @@
1
- __version__ = "0.2.1"
File without changes
File without changes
File without changes
File without changes
File without changes