@@ -34,16 +34,16 @@ def __hash__(self):
34
34
35
35
@dataclass
36
36
class PrimaryData :
37
- # ts_uuid -> ts
38
- time_series : ComplexPowerDict [TimeSeriesKey ]
37
+ # ts_key -> ts
38
+ _time_series : ComplexPowerDict [TimeSeriesKey ]
39
39
# participant_uuid -> ts_uuid
40
- participant_mapping : dict [str , str ]
40
+ _participant_mapping : dict [str , str ]
41
41
42
42
def __init__ (
43
43
self , time_series : "ComplexPowerDict" , participant_mapping : dict [str , str ]
44
44
):
45
- self .time_series = time_series
46
- self .participant_mapping = participant_mapping
45
+ self ._time_series = time_series
46
+ self ._participant_mapping = participant_mapping
47
47
48
48
def __eq__ (self , other ):
49
49
try :
@@ -53,46 +53,52 @@ def __eq__(self, other):
53
53
return False
54
54
55
55
def __len__ (self ):
56
- return len (self .time_series )
56
+ return len (self ._time_series )
57
57
58
58
def __contains__ (self , uuid ):
59
- return uuid in self .time_series or uuid in self .participant_mapping
59
+ return uuid in self ._time_series or uuid in self ._participant_mapping
60
60
61
61
def __getitem__ (self , get : str | TimeSeriesKey ) -> ComplexPower :
62
62
match get :
63
63
case str ():
64
64
get_key = TimeSeriesKey (get , None )
65
- if get in self .participant_mapping :
66
- ts_uuid = self .participant_mapping [get ]
65
+ if get in self ._participant_mapping :
66
+ ts_uuid = self ._participant_mapping [get ]
67
67
key = TimeSeriesKey (ts_uuid , None )
68
- return self .time_series [key ]
69
- elif get_key in self .time_series :
70
- return self .time_series [get_key ]
68
+ return self ._time_series [key ]
69
+ elif get_key in self ._time_series :
70
+ return self ._time_series [get_key ]
71
71
else :
72
72
raise KeyError (
73
73
f"{ get } neither a valid time series nor a participant uuid."
74
74
)
75
75
case TimeSeriesKey ():
76
- return self .time_series [get ]
76
+ return self ._time_series [get ]
77
77
case _:
78
78
raise ValueError (
79
79
"Only str uuid of either time series or participant or TimeSeriesKey of time series are allowed as key for PrimaryData."
80
80
)
81
81
82
82
def p (self , ffill = True ):
83
- return self .time_series .p (ffill )
83
+ return self ._time_series .p (ffill )
84
84
85
85
def q (self , ffill = True ):
86
- return self .time_series .q (ffill )
86
+ return self ._time_series .q (ffill )
87
87
88
88
def p_sum (self ) -> Series :
89
- return self .time_series .p_sum ()
89
+ return self ._time_series .p_sum ()
90
90
91
91
def q_sum (self ):
92
- return self .time_series .q_sum ()
92
+ return self ._time_series .q_sum ()
93
93
94
94
def sum (self ) -> ComplexPower :
95
- return self .time_series .sum ()
95
+ return self ._time_series .sum ()
96
+
97
+ def add_time_series (
98
+ self , ts_key : TimeSeriesKey , ts : ComplexPower , participant : str
99
+ ):
100
+ self ._time_series [ts_key ] = ts
101
+ self ._participant_mapping [participant ] = ts_key .ts_uuid
96
102
97
103
def get_for_participants (self , participants ) -> list [ComplexPower ]:
98
104
time_series = []
@@ -106,9 +112,9 @@ def filter_by_participants(
106
112
self , participants : list [str ], skip_missing : bool = False
107
113
):
108
114
if skip_missing :
109
- participants = [p for p in participants if p in self .participant_mapping ]
115
+ participants = [p for p in participants if p in self ._participant_mapping ]
110
116
try :
111
- pm = {p : self .participant_mapping [p ] for p in participants }
117
+ pm = {p : self ._participant_mapping [p ] for p in participants }
112
118
ts = ComplexPowerDict ({ts_uuid : self [ts_uuid ] for ts_uuid in pm .values ()}) # type: ignore
113
119
return PrimaryData (ts , pm )
114
120
except KeyError as e :
@@ -118,27 +124,27 @@ def filter_by_participants(
118
124
) from e
119
125
120
126
def get_for_participant (self , participant : str ) -> ComplexPower | None :
121
- ts_uuid = self .participant_mapping .get (participant )
127
+ ts_uuid = self ._participant_mapping .get (participant )
122
128
if ts_uuid :
123
129
return self [ts_uuid ] # type: ignore
124
130
else :
125
131
return None
126
132
127
133
def filter_by_date_time (self , time : Union [datetime , list [datetime ]]):
128
- ts = self .time_series .filter_by_date_time (time )
129
- return PrimaryData (ts , self .participant_mapping )
134
+ ts = self ._time_series .filter_by_date_time (time )
135
+ return PrimaryData (ts , self ._participant_mapping )
130
136
131
137
def interval (self , start : datetime , end : datetime ):
132
- ts = self .time_series .interval (start , end )
133
- return PrimaryData (ts , self .participant_mapping )
138
+ ts = self ._time_series .interval (start , end )
139
+ return PrimaryData (ts , self ._participant_mapping )
134
140
135
141
def to_csv (self , path : str , mkdirs = False , delimiter = "," ):
136
142
write_ts = partial (PrimaryData ._write_ts_df , path , mkdirs , delimiter )
137
143
138
144
with concurrent .futures .ProcessPoolExecutor () as executor :
139
145
futures = [
140
146
executor .submit (write_ts , ts , key )
141
- for key , ts in list (self .time_series .items ())
147
+ for key , ts in list (self ._time_series .items ())
142
148
]
143
149
144
150
for future in concurrent .futures .as_completed (futures ):
@@ -147,11 +153,11 @@ def to_csv(self, path: str, mkdirs=False, delimiter=","):
147
153
raise maybe_exception
148
154
149
155
# write mapping data
150
- index = [str (uuid .uuid4 ()) for _ in range (len (self .participant_mapping ))]
156
+ index = [str (uuid .uuid4 ()) for _ in range (len (self ._participant_mapping ))]
151
157
mapping_data = pd .DataFrame (
152
158
{
153
- "participant" : self .participant_mapping .keys (),
154
- "time_series" : self .participant_mapping .values (),
159
+ "participant" : self ._participant_mapping .keys (),
160
+ "time_series" : self ._participant_mapping .values (),
155
161
},
156
162
index = index ,
157
163
)
@@ -200,9 +206,11 @@ def compare(self, other):
200
206
errors = []
201
207
202
208
# Compare participant mapping
203
- participant_ts_self = set ([(p , t ) for p , t in self .participant_mapping .items ()])
209
+ participant_ts_self = set (
210
+ [(p , t ) for p , t in self ._participant_mapping .items ()]
211
+ )
204
212
participant_ts_other = set (
205
- [(p , t ) for p , t in other .participant_mapping .items ()]
213
+ [(p , t ) for p , t in other ._participant_mapping .items ()]
206
214
)
207
215
mapping_differences = participant_ts_self .symmetric_difference (
208
216
participant_ts_other
@@ -217,7 +225,7 @@ def compare(self, other):
217
225
218
226
# Compare time series
219
227
try :
220
- self .time_series .compare (other .time_series )
228
+ self ._time_series .compare (other ._time_series )
221
229
except ComparisonError as e :
222
230
errors .extend (e .differences )
223
231
0 commit comments