Need some help.

import mne
from mne.datasets.eyelink import data_path
from mne.preprocessing.eyetracking import read_eyelink_calibration
from mne.viz.eyetracking import plot_gaze

import mne.preprocessing.eyetracking as et

et_fpath = data_path() / "eeg-et" / "sub-01_task-plr_eyetrack.asc"
eeg_fpath = data_path() / "eeg-et" / "sub-01_task-plr_eeg.mff"

raw_et = mne.io.read_raw_eyelink(et_fpath, create_annotations=["blinks"])
raw_eeg = mne.io.read_raw_egi(eeg_fpath, events_as_annotations=True).load_data()
raw_eeg.filter(1, 30)


print(raw_et.info["chs"])
for ci, ch_info in enumerate(raw_et.info["chs"]):
    print(ci, ch_info["ch_name"])
cals = read_eyelink_calibration(et_fpath)
print(f"number of calibrations: {len(cals)}")
first_cal = cals[0]  # let's access the first (and only in this case) calibration
print(first_cal)
print(f"offset for each calibration point: {first_cal['offsets']}")
print(f"x-coordinate for each calibration point: {first_cal['positions'].T[0]}")

first_cal.plot()

first_cal["screen_resolution"] = (1920, 1080)
first_cal["screen_size"] = (0.53, 0.3)
first_cal["screen_distance"] = 0.9
mne.preprocessing.eyetracking.convert_units(raw_et, calibration=first_cal, to="radians")

ps_scalings = dict(pupil=1e3)
print("these are the channels names ----------------------------", raw_et.ch_names)
raw_et.plot(scalings=ps_scalings, block=True)
raw_et = et.interpolate_blinks(raw_et, interpolate_gaze=False)
raw_et.plot(scalings=ps_scalings, block=True)
print("These are the annotations", raw_et.annotations)
my_annot = mne.Annotations(
    onset=[*raw_et.annotations.onset, 3],
    duration=[*raw_et.annotations.duration, 1],
    description=[*raw_et.annotations.description, "BAD_foo"],
)
print(my_annot)
raw_et.set_annotations(my_annot)

raw_et.plot(scalings=ps_scalings, block=True)
raw_et = et.interpolate_blinks(raw_et, interpolate_gaze=False, match="BAD_foo")
raw_et.plot(scalings=ps_scalings, block=True)

How can I remove the existing annotations description and ā€œrenameā€ them? Right now it looks something like this:

As we can clearly see, there’s a ā€œBAD_blinkā€ but what if I want to rename it as ā€œBAD_somethingā€?

You can’t modify annotation descriptions in-place, since Annotations are immutable. The usual approach is to create a new Annotations object with updated descriptions and then set it back on the raw object

For example

annot = raw_et.annotations

new_desc = [
    "BAD_something" if d == "BAD_blink" else d
    for d in annot.description
]

raw_et.set_annotations(
    mne.Annotations(
        onset=annot.onset,
        duration=annot.duration,
        description=new_desc,
        orig_time=annot.orig_time,
    )
)

This will effectively ā€œrenameā€ BAD_blink to BAD_something while keeping timing and metadata intact

1 Like

Actually, Annotations objects do have the rename method that can be used to rename all instances of ā€œBAD_blinkā€ to ā€œBAD_somethingā€.

But if only specific ā€œBAD_blinkā€ annotations should be renamed, reconstructing the object after making the modifications is a good route.

1 Like

Thanks for the correction @tsbinns! Good to know about the .rename() method

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.