From 3d0fbd5c5e20dbdb153717886482079a0069a2a7 Mon Sep 17 00:00:00 2001 From: Tyler Date: Fri, 3 Oct 2025 16:58:49 -0700 Subject: [PATCH] fix to boris events --- main.py | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index e873a09..a44b1b8 100644 --- a/main.py +++ b/main.py @@ -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)