-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensorplot.py
More file actions
352 lines (293 loc) · 10.6 KB
/
sensorplot.py
File metadata and controls
352 lines (293 loc) · 10.6 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
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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnnotationBbox, OffsetImage
from PIL import Image
import actipy
from datetime import datetime
from pathlib import Path
class TimeScale:
def __init__(self, times):
self.min_time = np.min(times)
self.max_time = np.max(times)
self.scale = self.max_time - self.min_time
def to_unit(self, times):
"""
datetime -> [0,1]
"""
return (times - self.min_time) / self.scale
def to_scale(self, floats):
"""
[0,1] -> datetime
"""
return floats * self.scale + self.min_time
def to_str(self, floats, string_slice=slice(11, 19)):
"""
[0,1] -> str
"""
times = self.to_scale(floats)
time_strings = np.datetime_as_string(times)
return [time_string[string_slice] for time_string in time_strings]
def mask_times(
start_time: np.datetime64,
duration: np.timedelta64,
times: np.ndarray[np.datetime64],
*arrays: np.ndarray,
):
stop_time = start_time + duration
mask = np.logical_and(times >= start_time, times < stop_time)
to_return = []
for array in arrays:
to_return.append(array[mask])
return (times[mask], *to_return)
class SensorData:
def __init__(
self,
name: str,
times: np.ndarray[np.datetime64],
data: np.ndarray,
# common plot params
plot_x_ticks: bool | int,
# type specific plot params
plot_params: dict,
) -> None:
self.name = name
self.times = times
self.data = data
self.plot_x_ticks = plot_x_ticks
self.plot_params = plot_params
def select(self, start_time: np.datetime64, duration: np.timedelta64):
sel_time, sel_data = mask_times(start_time, duration, self.times, self.data)
return self.__class__(
self.name,
sel_time,
sel_data,
self.plot_x_ticks,
**self.plot_params,
)
def handle_xticks(
plot_x_ticks: bool | int, ax: plt.Axes, ts: TimeScale, unit_times: np.ndarray
) -> None:
if plot_x_ticks:
# if plot_x_ticks is an integer, plot that many ticks
if isinstance(plot_x_ticks, bool): # else plot where data was actually sampled
ax.set_xticks(unit_times)
ax.set_xticklabels(ts.to_str(unit_times))
elif isinstance(plot_x_ticks, int):
ticks = np.linspace(0, 1, plot_x_ticks)
ax.set_xticks(ticks)
ax.set_xticklabels(ts.to_str(ticks))
else:
raise ValueError("plot_x_ticks must be bool or int")
else:
ax.set_xticks([])
class ImageData(SensorData):
def __init__(
self,
name: str,
times: np.ndarray[np.datetime64],
data: np.ndarray,
# common plot params
plot_x_ticks: bool | int = True,
# image specific plot params
img_zoom: float = 0.23,
) -> None:
super().__init__(name, times, data, plot_x_ticks, {"img_zoom": img_zoom})
def plot(self, ts: TimeScale, ax: plt.Axes):
unit_times = ts.to_unit(self.times)
for img_path, x in zip(self.data, unit_times):
ax.add_artist(
AnnotationBbox(
OffsetImage(
Image.open(img_path), zoom=self.plot_params["img_zoom"]
),
xy=(x, 0.5),
frameon=False,
)
)
# Change y axis
ax.set_ylabel(self.name, rotation=0, ha="right")
ax.set_yticks([]) # remove y ticks
# Optionally, plot x ticks
handle_xticks(self.plot_x_ticks, ax, ts, unit_times)
class TextData(SensorData):
def __init__(
self,
name: str,
times: np.ndarray[np.datetime64],
text_data: np.ndarray,
# common plot params
plot_x_ticks: bool | int = True,
# text specific plot params
fontsize: int = 10,
) -> None:
super().__init__(name, times, text_data, plot_x_ticks, {"fontsize": fontsize})
def plot(self, ts: TimeScale, ax: plt.Axes):
unit_times = ts.to_unit(self.times)
for text, x in zip(self.data, unit_times):
ax.text(
x,
0.5,
text,
fontsize=self.plot_params["fontsize"],
ha="center",
va="center",
)
# Change y axis
ax.set_ylabel(self.name, rotation=0, ha="right")
ax.set_yticks([])
# Optionally, plot x ticks
handle_xticks(self.plot_x_ticks, ax, ts, unit_times)
class ScalarData(SensorData):
def __init__(
self,
name: str,
times: np.ndarray[np.datetime64],
sensor_data: np.ndarray,
# common plot params
plot_x_ticks: bool | int = True,
) -> None:
super().__init__(name, times, sensor_data, plot_x_ticks, {})
def plot(self, ts: TimeScale, ax: plt.Axes):
unit_times = ts.to_unit(self.times)
ax.plot(unit_times, self.data)
ax.set_ylabel(self.name, rotation=0, ha="right")
# Optionally, plot x ticks
handle_xticks(self.plot_x_ticks, ax, ts, unit_times)
class VectorData(SensorData):
def __init__(
self,
name: str,
times: np.ndarray[np.datetime64],
sensor_data: np.ndarray,
# common plot params
plot_x_ticks: bool | int = True,
# vector specific plot params
dim_names: list[str] | None = None,
) -> None:
super().__init__(
name, times, sensor_data, plot_x_ticks, {"dim_names": dim_names}
)
def plot(self, ts: TimeScale, ax: plt.Axes):
unit_times = ts.to_unit(self.times)
for i, dim in enumerate(self.data.T):
if self.plot_params["dim_names"] is None:
ax.plot(unit_times, dim, label=f"{self.name}_{i}")
else:
ax.plot(unit_times, dim, label=self.plot_params["dim_names"][i])
ax.set_ylabel(self.name, rotation=0, ha="right")
ax.legend()
# Optionally, plot x ticks
handle_xticks(self.plot_x_ticks, ax, ts, unit_times)
class SensorPlot:
def __init__(
self,
sensor_data: list[SensorData],
figsize: list[int] = [20, 10],
height_ratios: list[int] | None = None,
) -> None:
self.sensor_data = sensor_data
# Save plot parameters
if height_ratios is None:
height_ratios = [1 for _ in sensor_data]
self.height_ratios = height_ratios
self.figsize = figsize
def __repr__(self) -> None:
"""
Go through self.sensor_data and print the name, type, and start, stop times for each modality, as well as the number of readings.
"""
to_return = "SensorPlot object with the following data:\n"
for data in self.sensor_data:
to_return += f"- {data.name} ({data.__class__.__name__})\n"
to_return += f" {len(data.times)} readings\n"
to_return += f" {min(data.times)} -> {max(data.times)}\n"
return to_return
def add_data(
self, sensor_data: SensorData, index: int | None = None, height_ratio: int = 1
):
# check it is not alreadt in self.sensor_data
for data in self.sensor_data:
if data.name == sensor_data.name:
raise ValueError(
f"Data with name {sensor_data.name} already exists in self.sensor_data"
)
# add to self.sensor_data
if index is None:
index = len(self.sensor_data)
self.sensor_data.insert(index, sensor_data)
self.height_ratios.insert(index, height_ratio)
def plot_window(self, start_time: np.datetime64, duration: np.timedelta64):
# ====== Select data by going through self.sensor data and calling the select method
selected_data = [data.select(start_time, duration) for data in self.sensor_data]
all_times = np.concatenate([data.times for data in selected_data])
if len(all_times) < 2:
raise ValueError(
f"Selected time range contains a single, or no data points."
)
ts = TimeScale(all_times) # fit timescale to all data
# ====== Plot parameters
fig, axes = plt.subplots(
len(self.sensor_data),
figsize=self.figsize,
gridspec_kw={"height_ratios": self.height_ratios},
)
# ====== Plot data
for data, ax in zip(selected_data, axes):
data.plot(ts, ax)
# Adjusting x-axis limits
for ax in axes:
ax.set_xlim(-0.1, 1.1)
return fig, axes
def main():
# List of image paths and the times from these paths
# "20231114_182809"
time_format = "%Y%m%d_%H%M%S"
def get_img_times(paths):
return [datetime.strptime(path.parts[-1][17:32], time_format) for path in paths]
small_img_paths = list(Path("raw_data/camera/small").glob("*.JPG"))
small_img_times = get_img_times(small_img_paths)
tuples = list(zip(small_img_paths, small_img_times))
tuples.sort(key=lambda x: x[0])
small_img_paths, small_img_times = zip(*tuples)
# Accelerometer reading in
ax3_data, info = actipy.read_device(
"raw_data/accelerometer/CWA-DATA.CWA",
lowpass_hz=20,
calibrate_gravity=True,
detect_nonwear=True,
resample_hz=30,
)
# img
image_datetimes = np.array(small_img_times, dtype=np.datetime64)
image_paths = np.array(small_img_paths)
# axivity
sensor_datetimes = ax3_data.index.to_numpy()
accelerometer_readings = ax3_data[["x", "y", "z"]].to_numpy()
light_readings = ax3_data["light"].to_numpy()
temperature_readings = ax3_data["temperature"].to_numpy()
start_time = image_datetimes[0]
duration = np.timedelta64(30, "s")
print(
f"Looking at data from {str(start_time)[11:19]} to {str(start_time + duration)[11:19] }"
)
sensor_data = [
ImageData(
"Camera", image_datetimes, image_paths, plot_x_ticks=True, img_zoom=0.22
),
ScalarData(
"Temperature", sensor_datetimes, temperature_readings, plot_x_ticks=False
),
ScalarData("Light", sensor_datetimes, light_readings, plot_x_ticks=False),
VectorData(
"Accelerometer",
sensor_datetimes,
accelerometer_readings,
plot_x_ticks=10,
dim_names=["x", "y", "z"],
),
]
sv = SensorPlot(sensor_data)
print(sv)
fig, ax = sv.plot_window(start_time, duration)
plt.show()
if __name__ == "__main__":
main()