-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatamodel.py
More file actions
254 lines (191 loc) · 7.31 KB
/
datamodel.py
File metadata and controls
254 lines (191 loc) · 7.31 KB
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
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import List
from functools import reduce
import pandas as pd
import myutils
from servercomm import ServerCommunication
from dataprocessing import DataProcessor
class Subject(ABC):
"""
The Subject interface declares a set of methods for managing subscribers.
"""
@abstractmethod
def attach(self, observer: Observer) -> None:
"""
Attach an observer to the subject.
"""
pass
@abstractmethod
def detach(self, observer: Observer) -> None:
"""
Detach an observer from the subject.
"""
pass
@abstractmethod
def notify(self) -> None:
"""
Notify all observers about an event.
"""
pass
class Observer(ABC):
"""
The Observer interface declares the update method, used by subjects.
"""
@abstractmethod
def update(self, subject: Subject) -> None:
"""
Receive update from subject.
"""
pass
class Model(Subject):
_state: int = None
"""
For the sake of simplicity, the Subject's state, essential to all
subscribers, is stored in this variable.
"""
_observers: List[Observer] = []
"""
List of subscribers. In real life, the list of subscribers can be stored
more comprehensively (categorized by event type, etc.).
"""
def attach(self, observer: Observer) -> None:
print("Subject: Attached an observer.")
self._observers.append(observer)
def detach(self, observer: Observer) -> None:
self._observers.remove(observer)
"""
The subscription management methods.
"""
def notify(self) -> None:
"""
Trigger an update in each subscriber.
"""
for observer in self._observers:
observer.update(self)
def __init__(self, url_seatfinder, printer):
self.server = ServerCommunication(url_seatfinder)
# Reading halls LS
self.mainlib = {'LSG', # Gesellschaftswissenschaften
'LSM', # Medienzentrum
'LST', # Technik
'LSN', # Naturwissenschaften
'LSW', # Wiwi und Informatik
'LBS'} # Lehrbuchsammlung
#'BIB-N'} # KIT-Bibliothek Nord. Am Campus Nord
# Specific Libraires
self.slibs = {'FBC', # Chemie
'FBP', # Physik
'LAF', # Lernzentrum am Fasanenschloesschen}
'FBA', # Architektur
'FBI', # Informatik
'FBM'} # Mathematik
#'FBW'} # Wiwi - zur Zeit geschlossen
self.all_libs = [*(self.mainlib), *(self.slibs)]
self.resampling_interval = '15Min'
self.sampling_methods = ['Mean', 'Gauss']
self.selected_sampling_method = self.sampling_methods[0]
self.libs_metadata = self.server.get_static_lib_data(self.all_libs)
self.data_processor = DataProcessor(self.mainlib, self.slibs, self.libs_metadata)
self.query_progress = 0
# Create Exel File
self.printer = printer
self.printer.set_lib_identifier(self.mainlib, self.slibs)
self.printer.export_lib_metadata(self.libs_metadata)
def set_resampling_interval(self, value):
self.resampling_interval = value
def get_sampling_method_options(self):
return self.sampling_methods
def set_sampling_method(self, value):
self.selected_sampling_method = value
def get_progress(self):
return self.query_progress
def get_info(self, kind, timebegin, timeend):
resampled = {}
location_index = 0
total_locations = len(self.all_libs)
for location_id in self.all_libs:
self.__update_progress((location_index / total_locations) * 95)
oldestTime = timeend
rawdata = pd.DataFrame()
while oldestTime > timebegin:
reload_data = self.server.get_info_for_location_from_server(location_id, kind, timebegin, oldestTime)
newOldestTime = oldestTime
if (reload_data.size > 0):
newOldestTime = reload_data.iloc[0].name
else:
break
# assure strictly monotonous decline
if (not (newOldestTime < oldestTime)):
break
# keep data sorted
rawdata = rawdata.append(reload_data.sort_index(ascending = False))
time_progress = newOldestTime if newOldestTime > timebegin else timebegin
self.__update_progress(self.get_progress() +
((oldestTime - time_progress) / (timeend - timebegin)) * (95 / (total_locations)))
oldestTime = newOldestTime
#rawdata = rawdata[rawdata.index >= timebegin]
rawdata = rawdata.truncate(after = timebegin)
location_data = self.data_processor.resample(rawdata, self.resampling_interval,
self.selected_sampling_method)
#location_data = rawdata.resample(self.resampling_interval).mean()
resampled[location_id] = location_data.round()
location_index += 1
self.__update_progress(95)
combinedData = reduce(lambda left,right:
pd.merge(left,right,on='timestamp', how = 'outer').fillna(0),
resampled.values())
combinedData.sort_index(ascending = False, inplace = True)
return combinedData
def output_data(self, timebegin, timeend):
occupancy = self.get_info('seatestimate', timebegin, timeend)
pressure = self.data_processor.compute_pressure(occupancy)
self.printer.set_ylimits(0,-10)
self.printer.export_data(self.__grouped_seat_info(occupancy),
"Bel. Plätze", "Anzahl belegte Sitzplätze")
self.printer.set_ylimits(0,1.05)
self.printer.export_data(self.__all_main_lib_data(pressure), "Sitzplatzdruck Hauptbib", "Anteil belegter Sitzplätze")
self.printer.export_data(pressure[self.slibs].sort_index(axis=1), "Sitzplatzdruck Fachbibs", "Anteil belegter Sitzplätze")
self.printer.finish_up()
self.__update_progress(100)
def output_experiment_data(self, timebegin, timeend):
occupancy = self.get_info('seatestimate', timebegin, timeend)
mainlib_pressure = self.data_processor.compute_mainlib_pressure(occupancy)
speclib_pressure = self.data_processor.compute_speclibs_pressure(occupancy)
mainlib_pressure_vs_speclib_pressure = pd.merge(mainlib_pressure, speclib_pressure, on='timestamp')
grouped_occupancy = self.__grouped_seat_info(occupancy)
#mask = grouped_occupancy.transform(lambda x: x==x.max()).astype('bool')
#grouped_occupancy.loc[mask]
pressure_ratio_description = "Anteil belegter Sitzplätze"
"""
self.printer.export_data(self.__grouped_seat_info(occupancy),
"Absolute Anzahl belegter Sitzplätze in den Bibliotheken auf dem Campus",
"Absolute Anzahl belegter Sitzplätze")
"""
self.printer.set_ylimits(0,1.05)
self.printer.export_data(mainlib_pressure, "Sitzplatzdruck in der Hauptbibliothek",
pressure_ratio_description)
self.printer.export_data(mainlib_pressure_vs_speclib_pressure,
"Sitzplatzdruck in den Bibliotheken auf dem Campus", pressure_ratio_description)
self.printer.set_ylimits(0,-10)
self.printer.export_data(grouped_occupancy,
"Absolute Anzahl belegter Sitzplätze in den Bibliotheken auf dem Campus",
"Absolute Anzahl belegter Sitzplätze")
#self.printer.export_data(mainlib_pressure_vs_speclib_pressure, "Pressure in campus libraries")
self.printer.finish_up()
self.__update_progress(100)
def __all_main_lib_data(self, data):
ret = data[self.mainlib].sort_index(axis=1)
#print(ret)
if ('KIT-BIB' in data.keys()):
ret = pd.merge(data[['KIT-BIB']], ret, on='timestamp')
return ret
def __grouped_seat_info(self, data):
mainlib = data[self.mainlib].sum(axis=1)
mainlib = mainlib.reset_index(name = "KIT-BIB")
mainlib = mainlib.set_index('timestamp')
merged = pd.merge(mainlib, data[self.slibs].sort_index(axis=1), on='timestamp')
return merged
def __update_progress(self, value):
self.query_progress = value
self.notify()