-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabRecorder_Viewer.py
More file actions
1441 lines (1185 loc) · 60 KB
/
LabRecorder_Viewer.py
File metadata and controls
1441 lines (1185 loc) · 60 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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#%%
"""
XDF Explorer for EmotiBit Validation Study
Author: Franck Porteous (lightweight signal & marker plotting)
"""
import pyxdf
import numpy as np
import matplotlib.pyplot as plt
class XDFExplorer:
def __init__(self, xdf_path):
# Load file
self.streams, self.header = pyxdf.load_xdf(xdf_path)
print(f"\n✅ Loaded XDF file: {xdf_path}")
print(f"📊 Found {len(self.streams)} streams")
# Establish global t0 (first timestamp of experiment)
self.t0 = min([min(s['time_stamps']) for s in self.streams if len(s['time_stamps']) > 0])
print(f"🕒 Experiment start (t0): {self.t0:.3f} s")
self.print_summary()
def print_summary(self):
"""Print a quick summary of each stream."""
print("\n--- Stream Summary ---")
for i, s in enumerate(self.streams):
name = s['info']['name'][0]
source_id = s['info']['source_id'][0]
stype = s['info']['type'][0] if s['info']['type'] else "N/A"
nch = int(s['info']['channel_count'][0])
fs = float(s['info']['nominal_srate'][0])
n_samples = len(s['time_series'])
if s['info']['name'][0] in ['LiveAmpSN-054907-0281-STETriggerIn', 'LiveAmpSN-054907-0281-DeviceTrigger']:
print(f"[{i}] source_id: {source_id} \t | {name} \t | type: {stype} \t | channels: {nch} \t | Fs: {fs:.1f} Hz \t | samples: {n_samples} \t | ")
else:
try:
tmin = min(s['time_stamps']) - self.t0
tmax = max(s['time_stamps']) - self.t0
except:
tmin = tmax = 0
print(f"[{i}] source_id: {source_id} \t | {name} \t | type: {stype} \t | channels: {nch} \t | Fs: {fs:.1f} Hz \t | samples: {n_samples} \t | "
f"time: {tmin:.2f}–{tmax:.2f}s")
def print_detailed_summary(self):
"""Print a comprehensive summary with better formatting and more details."""
print("\n" + "="*80)
print("📊 DETAILED STREAM ANALYSIS")
print("="*80)
for i, s in enumerate(self.streams):
name = s['info']['name'][0]
source_id = s['info']['source_id'][0]
stype = s['info']['type'][0] if s['info']['type'] else "N/A"
nch = int(s['info']['channel_count'][0])
fs = float(s['info']['nominal_srate'][0])
n_samples = len(s['time_series'])
# Calculate duration
if len(s['time_stamps']) > 0:
tmin = min(s['time_stamps']) - self.t0
tmax = max(s['time_stamps']) - self.t0
duration = tmax - tmin
else:
duration = 0
tmin = tmax = 0
# Determine stream category
if 'psychopy' in source_id.lower() or 'marker' in name.lower():
category = "🎯 MARKER"
color = "\033[91m" # Red
elif 'md-' in source_id.lower() or 'emotibit' in name.lower():
category = "📡 EMOTIBIT"
color = "\033[92m" # Green
else:
category = "📊 OTHER"
color = "\033[94m" # Blue
print(f"\n{color}[{i}] {category}\033[0m")
print(f" Name: {name}")
print(f" Source ID: {source_id}")
print(f" Type: {stype}")
print(f" Channels: {nch}")
print(f" Sample Rate: {fs:.1f} Hz")
print(f" Samples: {n_samples:,}")
print(f" Duration: {duration:.2f}s")
print(f" Time Range: {tmin:.2f}s - {tmax:.2f}s")
# Show data type and range for continuous streams
if n_samples > 0:
data = np.array(s['time_series'])
if data.dtype.kind not in ['U', 'S', 'O']: # Not string/object
print(f" Data Type: {data.dtype}")
if data.size > 0:
print(f" Value Range: {data.min():.3f} - {data.max():.3f}")
def categorize_streams(self):
"""Categorize streams by type and return organized dictionary."""
categories = {
'emotibit_physiological': [],
'emotibit_magnetometer': [],
'emotibit_ppg': [],
'emotibit_eda': [],
'emotibit_hr': [],
'markers': [],
'other': []
}
for i, s in enumerate(self.streams):
name = s['info']['name'][0].lower()
source_id = s['info']['source_id'][0].lower()
if 'psychopy' in source_id or 'marker' in name:
categories['markers'].append(i)
elif 'md-' in source_id or 'emotibit' in name:
if 'mag' in name:
categories['emotibit_magnetometer'].append(i)
elif 'ppg' in name:
categories['emotibit_ppg'].append(i)
elif 'eda' in name:
categories['emotibit_eda'].append(i)
elif 'hr' in name or 'heart' in name:
categories['emotibit_hr'].append(i)
else:
categories['emotibit_physiological'].append(i)
else:
categories['other'].append(i)
return categories
def get_streams_by_category(self, category):
"""Get stream indices by category."""
categories = self.categorize_streams()
return categories.get(category, [])
def analyze_data_quality(self):
"""Analyze data quality metrics for each stream."""
print("\n🔍 DATA QUALITY ANALYSIS")
print("="*60)
for i, s in enumerate(self.streams):
name = s['info']['name'][0]
ts = s['time_stamps'] - self.t0
data = np.array(s['time_series'])
if len(ts) == 0:
print(f"[{i}] {name}: ❌ EMPTY STREAM")
continue
# Calculate metrics
duration = ts.max() - ts.min()
nominal_srate = float(s['info']['nominal_srate'][0])
actual_samples = len(ts)
# Handle different stream types
if nominal_srate > 0:
# Continuous stream - calculate completeness based on expected samples
expected_samples = duration * nominal_srate
completeness = (actual_samples / expected_samples * 100) if expected_samples > 0 else 0
# Check for gaps in continuous streams
if len(ts) > 1:
intervals = np.diff(ts)
expected_interval = 1.0 / nominal_srate
gaps = np.sum(intervals > expected_interval * 1.5)
else:
gaps = 0
# Quality assessment for continuous streams
if completeness > 95 and gaps == 0:
quality = "✅ EXCELLENT"
elif completeness > 90 and gaps < 5:
quality = "⚠️ GOOD"
elif completeness > 80:
quality = "⚠️ FAIR"
else:
quality = "❌ POOR"
print(f"[{i}] {name}:")
print(f" Duration: {duration:.2f}s")
print(f" Completeness: {completeness:.1f}%")
print(f" Data gaps: {gaps}")
print(f" Quality: {quality}")
else:
# Marker/event stream - different quality metrics
if len(ts) > 1:
intervals = np.diff(ts)
# For markers, check for reasonable timing intervals
min_interval = np.min(intervals) if len(intervals) > 0 else 0
max_interval = np.max(intervals) if len(intervals) > 0 else 0
avg_interval = np.mean(intervals) if len(intervals) > 0 else 0
# Quality assessment for marker streams
if min_interval > 0 and max_interval < 1000: # Reasonable timing
quality = "✅ EXCELLENT"
elif min_interval > 0 and max_interval < 5000:
quality = "⚠️ GOOD"
else:
quality = "⚠️ FAIR"
print(f"[{i}] {name}:")
print(f" Duration: {duration:.2f}s")
print(f" Markers: {actual_samples}")
print(f" Interval range: {min_interval:.3f}s - {max_interval:.3f}s")
print(f" Avg interval: {avg_interval:.3f}s")
print(f" Quality: {quality}")
else:
print(f"[{i}] {name}:")
print(f" Duration: {duration:.2f}s")
print(f" Markers: {actual_samples}")
print(f" Quality: ⚠️ SINGLE MARKER")
def list_streams_interactive(self):
"""List streams with interactive selection capabilities."""
print("\n📋 AVAILABLE STREAMS:")
print("="*50)
for i, s in enumerate(self.streams):
name = s['info']['name'][0]
source_id = s['info']['source_id'][0]
stype = s['info']['type'][0] if s['info']['type'] else "N/A"
n_samples = len(s['time_series'])
# Determine category
if 'psychopy' in source_id.lower():
category = "🎯 MARKER"
elif 'md-' in source_id.lower():
category = "📡 EMOTIBIT"
else:
category = "📊 OTHER"
print(f"[{i:2d}] {category} | {name:<15} | {stype:<15} | {n_samples:>6} samples")
def get_stream_by_name_pattern(self, pattern):
"""Find streams matching a name pattern (case insensitive)."""
matches = []
pattern_lower = pattern.lower()
for i, s in enumerate(self.streams):
name = s['info']['name'][0].lower()
source_id = s['info']['source_id'][0].lower()
if pattern_lower in name or pattern_lower in source_id:
matches.append(i)
return matches
def quick_emotibit_overview(self, max_duration=60):
"""Show a quick overview of all EmotiBit streams."""
categories = self.categorize_streams()
print("\n🔍 QUICK EMOTIBIT OVERVIEW")
print("="*50)
for category, indices in categories.items():
if category.startswith('emotibit') and indices:
print(f"\n📡 {category.upper().replace('_', ' ')}:")
for idx in indices:
s = self.streams[idx]
name = s['info']['name'][0]
n_samples = len(s['time_series'])
fs = float(s['info']['nominal_srate'][0])
print(f" • {name}: {n_samples} samples @ {fs:.1f} Hz")
# Plot all EmotiBit streams
all_emotibit = []
for category, indices in categories.items():
if category.startswith('emotibit'):
all_emotibit.extend(indices)
if all_emotibit:
self.plot_streams_enhanced(all_emotibit, max_duration=max_duration)
def _get_stream_type(self, stream_name):
"""Helper to determine stream type from name."""
name_lower = stream_name.lower()
if 'mag' in name_lower:
return 'magnetometer'
elif 'ppg' in name_lower:
return 'ppg'
elif 'eda' in name_lower:
return 'eda'
elif 'hr' in name_lower or 'heart' in name_lower:
return 'hr'
elif 'marker' in name_lower:
return 'marker'
else:
return 'other'
def plot_markers(self, idx):
"""Plot PsychoPy marker stream (event onsets)."""
s = self.streams[idx]
name = s['info']['name'][0]
ts = s['time_stamps'] - self.t0
labels = [row[0] for row in s['time_series']]
plt.figure(figsize=(14, 2))
for t, lbl in zip(ts, labels):
plt.axvline(x=t, color='r', linestyle='--', alpha=0.6)
plt.text(t, 0, lbl, rotation=90, verticalalignment='bottom', fontsize=8)
plt.title(f"🪧 Marker Stream — {name}")
plt.xlabel("Time (s)")
plt.xlim([0, ts.max()])
plt.yticks([])
plt.tight_layout()
plt.show()
def plot_streams_enhanced(self, indices, channels_per_stream=None, max_duration=None,
labels=None, show_markers=True, figsize=(16, 10)):
"""
Enhanced plotting with better stream differentiation and layout.
"""
if channels_per_stream is None:
channels_per_stream = [None] * len(indices)
if labels is None:
labels = [self.streams[idx]['info']['name'][0] for idx in indices]
# Create subplot layout
n_streams = len(indices)
fig, axes = plt.subplots(n_streams, 1, figsize=figsize, sharex=True)
if n_streams == 1:
axes = [axes]
# Color scheme for different stream types
stream_colors = {
'magnetometer': '#FF6B6B',
'ppg': '#4ECDC4',
'eda': '#45B7D1',
'hr': '#96CEB4',
'marker': '#FFEAA7',
'other': '#DDA0DD'
}
# Block colors for markers
block_colors = {1: '#FF6B6B', 2: '#4ECDC4', 3: '#FFE66D'}
# Get marker stream indices if show_markers is True
marker_stream_indices = []
if show_markers:
marker_stream_indices = self.get_streams_by_category('markers')
max_ts = 0
for i, (idx, chs, lbl) in enumerate(zip(indices, channels_per_stream, labels)):
ax = axes[i]
s = self.streams[idx]
stream_name = s['info']['name'][0]
ts = s['time_stamps'] - self.t0
data = np.array(s['time_series'])
# Determine stream type and color
stream_type = self._get_stream_type(stream_name)
color = stream_colors.get(stream_type, '#DDA0DD')
# Handle empty streams
if len(ts) == 0:
ax.text(0.5, 0.5, f"Empty stream: {stream_name}",
transform=ax.transAxes, ha='center', va='center')
continue
# Crop time
if max_duration is not None:
mask = ts <= max_duration
ts = ts[mask]
data = data[mask]
if len(ts) > 0:
max_ts = max(max_ts, ts.max())
# Determine if continuous or marker stream
is_continuous = data.dtype.kind not in ['U', 'S', 'O']
if is_continuous:
# Reshape if single channel
if data.ndim == 1:
data = data.reshape(-1, 1)
n_channels = data.shape[1]
if chs is None:
chs = range(n_channels)
# Plot channels
for ch in chs:
channel_label = f"{lbl} Ch{ch+1}" if n_channels > 1 else lbl
ax.plot(ts, data[:, ch], label=channel_label, color=color, linewidth=1.5)
ax.set_ylabel(f"{lbl}\n({stream_type.upper()})", color=color, fontweight='bold')
ax.tick_params(axis='y', labelcolor=color)
ax.grid(True, alpha=0.3)
ax.legend(loc='upper right', fontsize=8)
# Overlay markers if requested
if show_markers and len(marker_stream_indices) > 0:
for marker_idx in marker_stream_indices:
marker_stream = self.streams[marker_idx]
marker_ts = marker_stream['time_stamps'] - self.t0
marker_data = marker_stream['time_series']
for t, marker_row in zip(marker_ts, marker_data):
if max_duration is None or t <= max_duration:
marker_text = marker_row[0] if isinstance(marker_row, (list, np.ndarray)) else str(marker_row)
# Extract block_id
block_id = None
if 'block_id:' in marker_text:
try:
block_id = int(marker_text.split('block_id:')[1].split('|')[0])
except (ValueError, IndexError):
pass
marker_color = block_colors.get(block_id, '#999999') if block_id else '#999999'
ax.axvline(x=t, color=marker_color, linestyle=':', alpha=0.5, linewidth=1.2)
else:
# Marker stream
ax.set_ylabel(f"{lbl}\n(MARKERS)", color=color, fontweight='bold')
# Plot markers with block-based coloring
for t, marker_row in zip(ts, data):
marker_text = marker_row[0] if isinstance(marker_row, (list, np.ndarray)) else str(marker_row)
# Extract block_id
block_id = None
if 'block_id:' in marker_text:
try:
block_id = int(marker_text.split('block_id:')[1].split('|')[0])
except (ValueError, IndexError):
pass
marker_color = block_colors.get(block_id, '#999999') if block_id else '#999999'
ax.axvline(x=t, color=marker_color, linestyle='--', alpha=0.7, linewidth=2)
# Add marker text
ax.text(t, 0.5, marker_text.split('|')[0], rotation=90,
verticalalignment='bottom', fontsize=8, color=marker_color)
ax.set_ylim([0, 1])
ax.set_yticks([])
ax.set_xlim([0, max_ts])
# Set common x-axis label
axes[-1].set_xlabel("Time (s)", fontweight='bold')
# Add title with experiment info
fig.suptitle(f"📊 Multi-Stream Analysis - {len(indices)} streams",
fontsize=14, fontweight='bold')
plt.tight_layout()
plt.show()
def plot_continuous(self, idx, channels=None, max_duration=None):
"""
Plot physiological signal(s).
channels: list of indices to plot (default: all)
max_duration: optional crop (in seconds)
"""
s = self.streams[idx]
name = s['info']['name'][0]
ts = s['time_stamps'] - self.t0
data = np.array(s['time_series'])
n_channels = data.shape[1] if data.ndim > 1 else 1
# Handle single channel streams
if n_channels == 1:
data = data.reshape(-1, 1)
# Crop
if max_duration is not None:
mask = ts <= max_duration
ts = ts[mask]
data = data[mask]
# Channel selection
if channels is None:
channels = range(n_channels)
plt.figure(figsize=(14, 6))
for ch in channels:
plt.plot(ts, data[:, ch], label=f"Ch {ch+1}")
plt.title(f"📈 Continuous Stream — {name}")
plt.xlabel("Time (s)")
plt.ylabel("Signal")
plt.xlim([0, ts[-1]])
plt.legend(loc="upper right", fontsize=8)
plt.tight_layout()
plt.show()
def get_stream_index(self, name_substring):
"""Helper to find stream index by name."""
for i, s in enumerate(self.streams):
if name_substring.lower() in s['info']['name'][0].lower():
return i
print(f"⚠️ Stream containing '{name_substring}' not found.")
for i, s in enumerate(self.streams):
if name_substring.lower() in s['info']['source_id'][0].lower():
return i
print(f"⚠️ Source_id containing '{name_substring}' not found.")
return None
def plot_emotibit_all(self, subset=None, max_duration=None):
"""
Plot all EmotiBit continuous streams.
EmotiBit streams are identified by 'MD-' in their source_id or name.
Optionally filter by a substring (e.g., "MAG", "PPG", "TEMP").
"""
subset = subset.lower() if subset else None
emotibit_indices = []
for i, s in enumerate(self.streams):
name = s['info']['name'][0].lower()
source_id = s['info']['source_id'][0].lower()
# Check if it matches EmotiBit identifier and optional subset
is_emotibit = 'md-' in source_id or 'md-' in name or 'emotibit' in name
matches_subset = subset in name or subset in source_id if subset else True
if is_emotibit and matches_subset:
data = np.array(s['time_series'])
if data.dtype.kind not in ['U', 'S', 'O']: # skip string/marker streams
emotibit_indices.append(i)
if not emotibit_indices:
print("⚠️ No matching EmotiBit continuous streams found.")
return
print(f"📦 Found {len(emotibit_indices)} EmotiBit continuous stream(s).")
labels = [self.streams[idx]['info']['name'][0] for idx in emotibit_indices]
self.plot_streams(emotibit_indices, max_duration=max_duration, labels=labels)
def plot_streams(self, indices, channels_per_stream=None, max_duration=None, labels=None,
enhanced=True, figsize=(16, 10)):
"""
Plot multiple streams with enhanced visualization by default.
Args:
indices (list[int]): stream indices to plot
channels_per_stream (list[list[int]] or None): which channels to plot for each stream
max_duration (float or None): crop duration (seconds)
labels (list[str] or None): optional labels for legend per stream
enhanced (bool): use enhanced plotting with separate subplots
figsize (tuple): figure size for plotting
"""
if enhanced:
self.plot_streams_enhanced(indices, channels_per_stream, max_duration, labels, figsize=figsize)
else:
# Original overlay method
if channels_per_stream is None:
channels_per_stream = [None] * len(indices)
if labels is None:
labels = [self.streams[idx]['info']['name'][0] for idx in indices]
# Colors for different streams
colors = plt.cm.tab10(np.linspace(0, 1, len(indices)))
# Block colors (1, 2, 3)
block_colors = {1: '#FF6B6B', 2: '#4ECDC4', 3: '#FFE66D'}
fig, ax_main = plt.subplots(figsize=figsize)
axes = [ax_main]
continuous_count = 0
max_ts = 0
marker_type_colors = {} # Track marker types and their colors
for stream_idx, (idx, chs, lbl, color) in enumerate(zip(indices, channels_per_stream, labels, colors)):
s = self.streams[idx]
stream_name = s['info']['name'][0]
ts = s['time_stamps'] - self.t0
data = np.array(s['time_series'])
# Handle empty streams
if len(ts) == 0:
print(f"⚠️ Stream '{stream_name}' is empty, skipping.")
continue
# Crop time
if max_duration is not None:
mask = ts <= max_duration
ts = ts[mask]
data = data[mask]
if len(ts) > 0:
max_ts = max(max_ts, ts.max())
# Determine if continuous or marker stream
is_continuous = data.dtype.kind not in ['U', 'S', 'O']
if is_continuous:
# Reshape if single channel
if data.ndim == 1:
data = data.reshape(-1, 1)
n_channels = data.shape[1]
if chs is None:
chs = range(n_channels)
# Create new y-axis for each continuous stream (except first)
if continuous_count == 0:
ax = ax_main
else:
ax = ax_main.twinx()
ax.spines['right'].set_position(("axes", 1 + 0.1 * continuous_count))
axes.append(ax)
continuous_count += 1
# Plot channels
for ch in chs:
stream_label = lbl if len(chs) == 1 else f"{lbl} ch {ch+1}"
ax.plot(ts, data[:, ch], label=stream_label, color=color, linewidth=1.5)
ax.set_ylabel(lbl, color=color)
ax.tick_params(axis='y', labelcolor=color)
ax.set_xlim([0, max_ts])
else:
# Marker stream - parse by type and color by block_id
for t, marker_row in zip(ts, data):
marker_text = marker_row[0] if isinstance(marker_row, (list, np.ndarray)) else str(marker_row)
# Extract marker type (first part before |)
marker_type = marker_text.split('|')[0]
# Extract block_id from marker (e.g., "BLOCK_START|block_id:1")
block_id = None
if 'block_id:' in marker_text:
try:
block_id = int(marker_text.split('block_id:')[1].split('|')[0])
except (ValueError, IndexError):
pass
marker_color = block_colors.get(block_id, '#999999') if block_id else '#999999'
# Track marker type for legend (use block color)
if marker_type not in marker_type_colors:
marker_type_colors[marker_type] = marker_color
ax_main.axvline(x=t, color=marker_color, linestyle='--', alpha=0.5, linewidth=1.2)
# Set main axis properties
ax_main.set_xlabel("Time (s)")
ax_main.set_xlim([0, max_ts])
# Build legend for continuous streams
handles, all_labels = [], []
for a in axes:
h, l = a.get_legend_handles_labels()
handles += h
# Shorten labels - remove "idx_emotibit_" prefix
shortened_l = [label.replace('idx_emotibit_', '').replace('idx_', '') for label in l]
all_labels += shortened_l
# Add marker type legend
for marker_type in sorted(marker_type_colors.keys()):
marker_color = marker_type_colors[marker_type]
handles.append(plt.Line2D([0], [0], color=marker_color, linestyle='--', linewidth=1.5))
all_labels.append(marker_type)
if handles:
ax_main.legend(handles, all_labels, loc='upper right', fontsize=9, framealpha=0.9)
plt.title("📊 Overlay of Multiple Streams (Markers colored by block)")
plt.tight_layout()
plt.show()
def plot_by_category(self, category, max_duration=None, figsize=(16, 10)):
"""Plot all streams in a specific category."""
indices = self.get_streams_by_category(category)
if not indices:
print(f"⚠️ No streams found in category: {category}")
return
print(f"📊 Plotting {len(indices)} streams from category: {category}")
self.plot_streams_enhanced(indices, max_duration=max_duration, figsize=figsize)
def plot_emotibit_comparison(self, signal_types=None, max_duration=60):
"""Plot EmotiBit signals for comparison across different types."""
if signal_types is None:
signal_types = ['magnetometer', 'ppg', 'eda', 'hr']
categories = self.categorize_streams()
all_indices = []
labels = []
for signal_type in signal_types:
category_key = f'emotibit_{signal_type}'
if category_key in categories and categories[category_key]:
all_indices.extend(categories[category_key])
labels.extend([self.streams[idx]['info']['name'][0] for idx in categories[category_key]])
if all_indices:
print(f"📊 EmotiBit Comparison: {len(all_indices)} streams")
self.plot_streams_enhanced(all_indices, max_duration=max_duration, labels=labels)
else:
print("⚠️ No EmotiBit streams found for comparison")
def _parse_marker(self, marker_text):
"""
Parse marker text and extract all components.
Returns:
dict: Parsed marker information with keys: type, block_id, trial_id, image_path, raw_text
"""
parts = marker_text.split('|')
marker_info = {
'type': parts[0],
'block_id': None,
'trial_id': None,
'image_path': None,
'raw_text': marker_text
}
for part in parts[1:]:
if part.startswith('block_id:'):
try:
marker_info['block_id'] = int(part.split(':')[1])
except (ValueError, IndexError):
pass
elif part.startswith('trial_id:'):
try:
marker_info['trial_id'] = int(part.split(':')[1])
except (ValueError, IndexError):
pass
elif part.startswith('image:'):
marker_info['image_path'] = part.split(':', 1)[1]
return marker_info
def print_markers_by_block(self, block_id, marker_stream_idx=None):
"""
Print all markers for a specific block with detailed information.
Args:
block_id (int): The block ID to filter markers for
marker_stream_idx (int, optional): Specific marker stream index. If None, searches all marker streams.
"""
print(f"\n🎯 MARKERS FOR BLOCK {block_id}")
print("=" * 60)
# Find marker streams if not specified
if marker_stream_idx is None:
marker_indices = self.get_streams_by_category('markers')
if not marker_indices:
print("⚠️ No marker streams found.")
return
else:
marker_indices = [marker_stream_idx]
total_markers = 0
for idx in marker_indices:
s = self.streams[idx]
name = s['info']['name'][0]
ts = s['time_stamps'] - self.t0
data = s['time_series']
if len(ts) == 0:
print(f"⚠️ Stream '{name}' is empty.")
continue
# Filter markers for the specified block
block_markers = []
for t, marker_row in zip(ts, data):
marker_text = marker_row[0] if isinstance(marker_row, (list, np.ndarray)) else str(marker_row)
marker_info = self._parse_marker(marker_text)
if marker_info['block_id'] == block_id:
block_markers.append((t, marker_info))
if block_markers:
print(f"\n📡 Stream: {name}")
print(f" Found {len(block_markers)} markers for block {block_id}")
print(" " + "-" * 60)
for t, marker_info in block_markers:
print(f" {t:8.2f}s | {marker_info['type']:<15}", end="")
if marker_info['trial_id'] is not None:
print(f" | Trial: {marker_info['trial_id']:<3}", end="")
else:
print(f" | {'':<10}", end="")
if marker_info['image_path'] is not None:
# Extract just the filename from the path
image_name = marker_info['image_path'].split('/')[-1] if '/' in marker_info['image_path'] else marker_info['image_path']
print(f" | Image: {image_name}")
else:
print()
total_markers += len(block_markers)
else:
print(f"\n📡 Stream: {name}")
print(f" No markers found for block {block_id}")
print(f"\n📊 Total markers for block {block_id}: {total_markers}")
if total_markers == 0:
print("💡 Tip: Available block IDs can be found by calling list_all_block_ids()")
def list_all_block_ids(self):
"""List all unique block IDs found in marker streams with additional statistics."""
print("\n🔍 AVAILABLE BLOCK IDs")
print("=" * 40)
marker_indices = self.get_streams_by_category('markers')
if not marker_indices:
print("⚠️ No marker streams found.")
return
all_block_ids = set()
block_stats = {}
for idx in marker_indices:
s = self.streams[idx]
name = s['info']['name'][0]
data = s['time_series']
for marker_row in data:
marker_text = marker_row[0] if isinstance(marker_row, (list, np.ndarray)) else str(marker_row)
marker_info = self._parse_marker(marker_text)
if marker_info['block_id'] is not None:
block_id = marker_info['block_id']
all_block_ids.add(block_id)
if block_id not in block_stats:
block_stats[block_id] = {
'marker_types': set(),
'total_markers': 0,
'trials': set()
}
block_stats[block_id]['marker_types'].add(marker_info['type'])
block_stats[block_id]['total_markers'] += 1
if marker_info['trial_id'] is not None:
block_stats[block_id]['trials'].add(marker_info['trial_id'])
if all_block_ids:
sorted_block_ids = sorted(all_block_ids)
print(f"Found block IDs: {sorted_block_ids}")
print(f"Total blocks: {len(sorted_block_ids)}")
print("\n📊 Block Statistics:")
print("-" * 40)
for block_id in sorted_block_ids:
stats = block_stats[block_id]
trial_count = len(stats['trials']) if stats['trials'] else "N/A"
marker_types = sorted(stats['marker_types'])
print(f"Block {block_id}: {stats['total_markers']} markers, {trial_count} trials")
print(f" Marker types: {', '.join(marker_types)}")
else:
print("⚠️ No block IDs found in marker streams.")
def print_marker_timeline(self, block_id=None, marker_stream_idx=None):
"""
Print a timeline of markers with detailed information, optionally filtered by block.
Args:
block_id (int, optional): Filter by specific block ID. If None, shows all markers.
marker_stream_idx (int, optional): Specific marker stream index. If None, searches all marker streams.
"""
if block_id is not None:
print(f"\n⏰ MARKER TIMELINE FOR BLOCK {block_id}")
else:
print(f"\n⏰ COMPLETE MARKER TIMELINE")
print("=" * 80)
# Find marker streams if not specified
if marker_stream_idx is None:
marker_indices = self.get_streams_by_category('markers')
if not marker_indices:
print("⚠️ No marker streams found.")
return
else:
marker_indices = [marker_stream_idx]
all_markers = []
for idx in marker_indices:
s = self.streams[idx]
name = s['info']['name'][0]
ts = s['time_stamps'] - self.t0
data = s['time_series']
if len(ts) == 0:
continue
for t, marker_row in zip(ts, data):
marker_text = marker_row[0] if isinstance(marker_row, (list, np.ndarray)) else str(marker_row)
marker_info = self._parse_marker(marker_text)
# Filter by block if specified
if block_id is None or marker_info['block_id'] == block_id:
all_markers.append((t, marker_info, name))
# Sort by timestamp
all_markers.sort(key=lambda x: x[0])
if all_markers:
print(f"{'Time':>8} | {'Type':<15} | {'Block':<5} | {'Trial':<5} | {'Image':<20} | {'Stream'}")
print("-" * 80)
for t, marker_info, stream_name in all_markers:
block_str = str(marker_info['block_id']) if marker_info['block_id'] is not None else "N/A"
trial_str = str(marker_info['trial_id']) if marker_info['trial_id'] is not None else "N/A"
# Extract just filename for image
image_str = "N/A"
if marker_info['image_path'] is not None:
image_str = marker_info['image_path'].split('/')[-1] if '/' in marker_info['image_path'] else marker_info['image_path']
if len(image_str) > 20:
image_str = image_str[:17] + "..."
print(f"{t:8.2f}s | {marker_info['type']:<15} | {block_str:<5} | {trial_str:<5} | {image_str:<20} | {stream_name}")
print(f"\n📊 Total markers: {len(all_markers)}")
else:
if block_id is not None:
print(f"⚠️ No markers found for block {block_id}")
else:
print("⚠️ No markers found in any stream")
# This below is where the fun begins
def extract_block_data(self, block_id, include_markers=True, include_emotibit=True, include_other=False):
"""
Extract data from a specific block using BLOCK_START marker.
Args:
block_id (int): The block ID to extract data for
include_markers (bool): Include marker streams in extraction
include_emotibit (bool): Include EmotiBit physiological streams
include_other (bool): Include other non-EmotiBit streams
Returns:
dict: Extracted block data with structure:
{
'block_id': int,
'start_time': float,
'end_time': float,
'duration': float,
'streams': {
stream_idx: {
'name': str,
'type': str,
'time_stamps': array,
'time_series': array,
'sample_rate': float,
'n_samples': int
}
}
}
"""
print(f"\n📦 EXTRACTING DATA FOR BLOCK {block_id}")
print("=" * 50)
# Find BLOCK_START marker for this block
block_start_time = None
block_end_time = None
marker_indices = self.get_streams_by_category('markers')
for idx in marker_indices:
s = self.streams[idx]
ts = s['time_stamps'] - self.t0
data = s['time_series']
for t, marker_row in zip(ts, data):
marker_text = marker_row[0] if isinstance(marker_row, (list, np.ndarray)) else str(marker_row)
marker_info = self._parse_marker(marker_text)
if marker_info['block_id'] == block_id and marker_info['type'] == 'BLOCK_START':
block_start_time = t
print(f"🎯 Found BLOCK_START for block {block_id} at {t:.2f}s")
break
if block_start_time is not None:
break
if block_start_time is None:
print(f"⚠️ BLOCK_START marker not found for block {block_id}")
return None
# Find BLOCK_START for next block to determine end time
next_block_start = None
all_block_starts = []
for idx in marker_indices:
s = self.streams[idx]
ts = s['time_stamps'] - self.t0
data = s['time_series']
for t, marker_row in zip(ts, data):
marker_text = marker_row[0] if isinstance(marker_row, (list, np.ndarray)) else str(marker_row)
marker_info = self._parse_marker(marker_text)