fix to boris events

This commit is contained in:
2025-10-03 16:58:49 -07:00
parent 3f38f5a978
commit 3d0fbd5c5e

43
main.py
View File

@@ -960,16 +960,51 @@ class UpdateEventsWindow(QWidget):
durations = []
descriptions = []
label_counts = {}
used_times = set()
sfreq = raw.info['sfreq'] # sampling frequency in Hz
min_shift = 1.0 / sfreq
max_attempts = 10
for event in boris_events:
if not isinstance(event, list) or len(event) < 3:
continue
orig_time = event[0]
desc = event[2]
shifted_time = orig_time + time_shift
onsets.append(shifted_time)
durations.append(0.0) # durations shouldn't matter?
descriptions.append(desc)
# Count occurrences per event label
count = label_counts.get(desc, 0)
label_counts[desc] = count + 1
# Only use 1st, 3rd, 5th... (odd occurrences)
if (count % 2) == 0:
shifted_time = orig_time + time_shift
# Ensure unique timestamp by checking and adjusting slightly
adjusted_time = shifted_time
# Try to find a unique timestamp
attempts = 0
while round(adjusted_time, 6) in used_times and attempts < max_attempts:
adjusted_time += min_shift
attempts += 1
if attempts == max_attempts:
print(f"Warning: Could not find unique timestamp for event '{desc}' at original time {orig_time:.3f}s. Skipping.")
continue # Skip problematic event
adjusted_time = round(adjusted_time, 6)
used_times.add(adjusted_time)
print(f"Applying event: {desc} @ {adjusted_time:.3f}s (original: {orig_time:.3f}s)")
onsets.append(adjusted_time)
durations.append(0.0)
descriptions.append(desc)
new_annotations = Annotations(onset=onsets, duration=durations, description=descriptions)