본문 바로가기
Brain Engineering/MNE Python

[MNE-Python] 원시 데이터에서 이벤트 구문 분석 (2)

by goatlab 2022. 3. 23.
728x90
반응형
SMALL

Reading embedded events as Annotations

 

일부 EEG / MEG 시스템은 이벤트가 하나 이상의 STIM 채널의 펄스가 아닌 별도의 데이터 어레이에 저장되는 파일을 생성한다. 예를 들어, EEGLAB 형식은 이벤트를 set 파일의 배열 모음으로 저장한다. Annotations 이러한 파일을 읽을 때 MNE-Python은 저장된 이벤트를 객체로 자동 변환하고 객체의 annotations 속성으로 저장한다.

 

testing_data_folder = mne.datasets.testing.data_path()
eeglab_raw_file = os.path.join(testing_data_folder, 'EEGLAB', 'test_raw.set')
eeglab_raw = mne.io.read_raw_eeglab(eeglab_raw_file)
print(eeglab_raw.annotations)
---
Reading /home/circleci/mne_data/MNE-testing-data/EEGLAB/test_raw.fdt
<Annotations | 154 segments: rt (74), square (80)>

 

개체 내의 핵심 데이터 Annotations는 해당 속성 중 세 가지를 통해 액세스할  있다. 여기서 EEGLAB 파일에 154개의 이벤트가 저장되어 있고 모두 지속 시간이 0초이고 두 가지 유형의 이벤트가 있으며 첫 번째 이벤트가 기록이 시작된 후 약 1초 후에 발생했음을 알 수 있다.

 

print(len(eeglab_raw.annotations))
print(set(eeglab_raw.annotations.duration))
print(set(eeglab_raw.annotations.description))
print(eeglab_raw.annotations.onset[0])
---
154
{0.0}
{'square', 'rt'}
1.000068

 

Converting between Events arrays and Annotations objects

 

실험 이벤트가 MNE-Python (이벤트 배열 또는 Annotations 객체로)으로 읽혀지면 필요에 따라 두 형식 간에 쉽게 변환할 수 있다. 예를 들어, 연속 데이터를 에포칭하는 데 이벤트 배열이 필요하거나 특정 주석과 겹치는 경우 데이터 범위를 자동으로 생략하는 일부 기능의 "주석 인식" 기능을 활용하고 싶기 때문에 이 작업을 수행할 수 있다.

 

Annotations 객체를 이벤트 배열로 변환하려면 주석이 포함된 파일에서 함수 mne.events_from_annotations를 사용한다. Raw 함수는 각 고유 요소에 정수 이벤트 ID를 할당 (raw.annotations.description)하고 파생된 이벤트 배열과 함께 정수 이벤트 ID에 대한 설명 매핑을 반환한다. 기본적으로 각 주석이 시작될 때 하나의 이벤트가 생성된다. 이것은 각 주석 범위 내에서 균등한 간격의 이벤트를 생성하기 위해 chunk_duration 매개변수를 통해 수정할 수 있다 (주석당 여러 이벤트 만들기 참조 또는 균등한 간격의 이벤트의 이벤트 배열 직접 생성을 위한 균등한 간격의 이벤트 배열 만들기 참조).

 

events_from_annot, event_dict = mne.events_from_annotations(eeglab_raw)
print(event_dict)
print(events_from_annot[:5])
---
Used Annotations descriptions: ['rt', 'square']
{'rt': 1, 'square': 2}
[[128   0   2]
 [217   0   2]
 [267   0   1]
 [602   0   2]
 [659   0   1]]

 

dict 각각의 고유한 설명 값에 매핑되는 정수를 제어하려는 경우 매핑을 지정하는 a를 event_id 매개변수로 전달할 수 있다 (events_from_annotations). 이것은 dict 수정되지 않은 상태로 반환된다.

 

custom_mapping = {'rt': 77, 'square': 42}
(events_from_annot,
 event_dict) = mne.events_from_annotations(eeglab_raw, event_id=custom_mapping)
print(event_dict)
print(events_from_annot[:5])
---
Used Annotations descriptions: ['rt', 'square']
{'rt': 77, 'square': 42}
[[128   0  42]
 [217   0  42]
 [267   0  77]
 [602   0  42]
 [659   0  77]]

 

반대 변환 (이벤트 배열에서 Annotations 객체로)을 수행하려면 정수 이벤트 ID에서 문자열 설명으로의 매핑을 만들고 annotations_from_events 사용하여 객체를 구성하고 메서드 Annotations 호출하여 객체 set_annotations에 주석을 추가할 수 있다.

 

샘플 데이터가 Neuromag 시스템에 기록 되었기 때문에 (여기서 샘플 번호 매기기는 기록 이 시작될 때가 아니라 수집 시스템이 시작될 때 시작됨) 시작 orig_time이 시작을 기준으로 적절하게 정렬되도록 매개변수도 전달해야 한다.

 

mapping = {1: 'auditory/left', 2: 'auditory/right', 3: 'visual/left',
           4: 'visual/right', 5: 'smiley', 32: 'buttonpress'}
annot_from_events = mne.annotations_from_events(
    events=events, event_desc=mapping, sfreq=raw.info['sfreq'],
    orig_time=raw.info['meas_date'])
raw.set_annotations(annot_from_events)

 

주석은 원시 데이터를 플로팅할 때 자동으로 나타나며 레이블 값에 따라 색상으로 구분된다.

 

raw.plot(start=5, duration=5)

 

Making multiple events per annotation

 

매개변수를 Annotations 사용하여 객체에서 동일한 간격의 이벤트를 생성할 수 있다 . 예를 들어, 대상이 REM 수면에 있을 때를 나타내는 주석이 객체에 있고 해당 데이터 범위에 대해 휴식 상태 분석을 수행하려고 한다고 가정한다. 각 "REM" 범위 내에서 동일한 간격의 일련의 이벤트가 있는 이벤트 배열을 만든 다음 해당 이벤트를 사용하여 추가로 분석할 수 있는 (잠재적으로 겹치는) 에포크를 생성할 수 있다.

 

# create the REM annotations
rem_annot = mne.Annotations(onset=[5, 41],
                            duration=[16, 11],
                            description=['REM'] * 2)
raw.set_annotations(rem_annot)
(rem_events,
 rem_event_dict) = mne.events_from_annotations(raw, chunk_duration=1.5)
 Out : Used Annotations descriptions: ['REM']

 

이벤트가 실제로 5 - 21초 및 41 - 52초 범위에 속하고 ~1.5초 차이가 나는지 확인할 수 있다 (샘플링 주파수로 인한 약간의 지터 모듈로). 다음은 가장 가까운 밀리초로 반올림된 이벤트 시간이다.

 

print(np.round((rem_events[:, 0] - raw.first_samp) / raw.info['sfreq'], 3))
---
[ 5.     6.5    8.     9.5   11.    12.501 14.001 15.501 16.999 18.499
 41.    42.5   44.    45.5   47.    48.5   50.   ]

 

https://mne.tools/stable/auto_tutorials/intro/20_events_from_raw.html

 

Parsing events from raw data — MNE 1.0.0 documentation

This tutorial describes how to read experimental events from raw recordings, and how to convert between the two different representations of events within MNE-Python (Events arrays and Annotations objects). In the introductory tutorial we saw an example of

mne.tools

 

728x90
반응형
LIST