fix to boris events
This commit is contained in:
39
main.py
39
main.py
@@ -960,15 +960,50 @@ class UpdateEventsWindow(QWidget):
|
|||||||
durations = []
|
durations = []
|
||||||
descriptions = []
|
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:
|
for event in boris_events:
|
||||||
if not isinstance(event, list) or len(event) < 3:
|
if not isinstance(event, list) or len(event) < 3:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
orig_time = event[0]
|
orig_time = event[0]
|
||||||
desc = event[2]
|
desc = event[2]
|
||||||
|
|
||||||
|
# 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
|
shifted_time = orig_time + time_shift
|
||||||
|
|
||||||
onsets.append(shifted_time)
|
# Ensure unique timestamp by checking and adjusting slightly
|
||||||
durations.append(0.0) # durations shouldn't matter?
|
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)
|
descriptions.append(desc)
|
||||||
|
|
||||||
new_annotations = Annotations(onset=onsets, duration=durations, description=descriptions)
|
new_annotations = Annotations(onset=onsets, duration=durations, description=descriptions)
|
||||||
|
|||||||
Reference in New Issue
Block a user