-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathevidence.py
More file actions
452 lines (375 loc) · 16.5 KB
/
Copy pathevidence.py
File metadata and controls
452 lines (375 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"""Evidence evaluation module for BCI task control.
This module provides classes and functions for extracting evidence from raw device data,
including EEG, gaze tracking, and switch input data. The module supports different types
of evidence evaluation based on the input data type and desired output evidence type.
"""
# mypy: disable-error-code="override"
import logging
from typing import Any, List, Optional, Type
import numpy as np
from bcipy.acquisition.multimodal import ContentType
from bcipy.config import SESSION_LOG_FILENAME
from bcipy.core.parameters import Parameters
from bcipy.core.stimuli import GazeReshaper, TrialReshaper
from bcipy.display.main import ButtonPressMode
from bcipy.helpers.acquisition import analysis_channels
from bcipy.signal.model import SignalModel
from bcipy.signal.process import extract_eye_info
from bcipy.task.data import EvidenceType
from bcipy.task.exceptions import MissingEvidenceEvaluator
log = logging.getLogger(SESSION_LOG_FILENAME)
class EvidenceEvaluator:
"""Base class for evaluating raw device data using a signal model.
This class defines the interface for evidence evaluators, which are responsible
for performing necessary preprocessing steps such as filtering and reshaping
before evaluating the evidence.
Attributes:
symbol_set: List of possible symbols that can be presented.
signal_model: Model trained using calibration session data.
device_spec: Specification of the input device.
"""
def __init__(
self,
symbol_set: List[str],
signal_model: SignalModel,
parameters: Optional[Parameters] = None) -> None:
"""Initialize the evidence evaluator.
Args:
symbol_set: List of possible symbols that can be presented.
signal_model: Model trained using calibration session data.
parameters: Optional configuration parameters.
Raises:
AssertionError: If signal model metadata is missing or incompatible.
"""
assert signal_model.metadata, "Metadata missing from signal model."
device_spec = signal_model.metadata.device_spec
assert ContentType(
signal_model.metadata.device_spec.content_type
) == self.consumes, "evaluator is not compatible with the given model"
self.symbol_set = symbol_set
self.signal_model = signal_model
self.device_spec = device_spec
self.parameters = parameters
@property
def consumes(self) -> ContentType:
"""Get the type of data this evaluator consumes.
Returns:
ContentType: Type of input data required.
"""
raise NotImplementedError()
@property
def produces(self) -> EvidenceType:
"""Get the type of evidence this evaluator produces.
Returns:
EvidenceType: Type of evidence output.
"""
raise NotImplementedError()
def evaluate(self, *args: Any, **kwargs: Any) -> np.ndarray:
"""Evaluate the evidence from raw data.
Args:
**kwargs: Arbitrary keyword arguments for evaluation.
Returns:
np.ndarray: Evaluated evidence data.
"""
raise NotImplementedError()
class EEGEvaluator(EvidenceEvaluator):
"""Evidence evaluator for extracting symbol likelihoods from EEG data.
This evaluator processes raw EEG data to compute likelihood ratios for
different symbols based on the ERP response.
Attributes:
consumes: Type of input data (EEG).
produces: Type of evidence output (ERP).
channel_map: Mapping of EEG channels.
transform: Signal transformation function.
reshape: Trial reshaping function.
"""
consumes = ContentType.EEG
produces = EvidenceType.ERP
def __init__(
self,
symbol_set: List[str],
signal_model: SignalModel,
parameters: Optional[Parameters] = None) -> None:
"""Initialize the EEG evaluator.
Args:
symbol_set: List of possible symbols that can be presented.
signal_model: Model trained using calibration session data.
parameters: Optional configuration parameters.
"""
super().__init__(symbol_set, signal_model, parameters)
self.channel_map = analysis_channels(self.device_spec.channels,
self.device_spec)
self.transform = signal_model.metadata.transform
self.reshape = TrialReshaper()
def preprocess(
self,
raw_data: np.ndarray,
times: List[float],
target_info: List[str],
window_length: float) -> np.ndarray:
"""Preprocess the inquiry EEG data.
Args:
raw_data: C x L EEG data where C is number of channels and L is
signal length.
times: Timestamps associated with each symbol.
target_info: Target information about the stimuli
(e.g. ['nontarget', 'nontarget', ...]).
window_length: Length of time between stimuli presentation.
Returns:
np.ndarray: Preprocessed EEG data.
"""
transformed_data, transform_sample_rate = self.transform(
raw_data, self.device_spec.sample_rate)
# The data from DAQ is assumed to have offsets applied
reshaped_data, _lbls = self.reshape(
trial_targetness_label=target_info,
timing_info=times,
eeg_data=transformed_data,
sample_rate=transform_sample_rate,
channel_map=self.channel_map,
poststimulus_length=window_length)
return reshaped_data
def evaluate(
self,
raw_data: np.ndarray,
symbols: List[str],
times: List[float],
target_info: List[str],
window_length: float,
*args: Any,
**kwargs: Any) -> np.ndarray:
"""Evaluate EEG evidence.
Args:
raw_data: C x L EEG data where C is number of channels and L is
signal length.
symbols: Symbols displayed in the inquiry.
times: Timestamps associated with each symbol.
target_info: Target information about the stimuli
(e.g. ['nontarget', 'nontarget', ...]).
window_length: Length of time between stimuli presentation.
*args: Additional arguments.
Returns:
np.ndarray: Likelihood ratios for each symbol.
"""
data = self.preprocess(raw_data, times, target_info, window_length)
return self.signal_model.compute_likelihood_ratio(
data, symbols, self.symbol_set)
class GazeEvaluator(EvidenceEvaluator):
"""Evidence evaluator for extracting symbol likelihoods from gaze data.
This evaluator processes raw eye tracking data to compute likelihoods
for different symbols based on gaze patterns.
Attributes:
consumes: Type of input data (EYETRACKER).
produces: Type of evidence output (EYE).
channel_map: Mapping of eye tracking channels.
transform: Signal transformation function.
reshape: Gaze data reshaping function.
"""
consumes = ContentType.EYETRACKER
produces = EvidenceType.EYE
def __init__(
self,
symbol_set: List[str],
signal_model: SignalModel,
parameters: Optional[Parameters] = None) -> None:
"""Initialize the gaze evaluator.
Args:
symbol_set: List of possible symbols that can be presented.
signal_model: Model trained using calibration session data.
parameters: Optional configuration parameters.
"""
super().__init__(symbol_set, signal_model, parameters)
self.channel_map = analysis_channels(self.device_spec.channels,
self.device_spec)
self.transform = signal_model.metadata.transform
self.reshape = GazeReshaper()
def preprocess(
self,
raw_data: np.ndarray,
times: List[float],
flash_time: float) -> np.ndarray:
"""Preprocess the inquiry gaze data.
The preprocessing is functionally different than Gaze Reshaper, since
the raw data contains only one inquiry. start_idx is determined as the
start time of first symbol flashing multiplied by the sampling rate
of eye tracker. stop_idx is the index indicating the end of last
symbol flashing.
Args:
raw_data: C x L data where C is number of channels and L is signal
length. Includes all channels in devices.json.
times: Timestamps associated with each symbol.
flash_time: Duration (in seconds) of each stimulus.
Returns:
np.ndarray: Preprocessed gaze data (4, N_samples).
"""
if self.transform:
transformed_data, transform_sample_rate = self.transform(
raw_data, self.device_spec.sample_rate)
else:
transformed_data = raw_data
transform_sample_rate = self.device_spec.sample_rate
start_idx = int(self.device_spec.sample_rate * times[0])
stop_idx = start_idx + int(
(times[-1] - times[0] + flash_time) * self.device_spec.sample_rate)
data_all_channels = transformed_data[:, start_idx:stop_idx]
# Extract left and right eye from all channels. Remove/replace nan values
left_eye, right_eye, _, _, _, _ = extract_eye_info(data_all_channels)
reshaped_data = np.vstack(
(np.array(left_eye).T, np.array(right_eye).T))
return reshaped_data
def evaluate(
self,
raw_data: np.ndarray,
symbols: List[str],
times: List[float],
flash_time: float,
*args: Any,
**kwargs: Any) -> np.ndarray:
"""Evaluate gaze evidence.
Args:
raw_data: C x L data where C is number of channels and L is signal
length.
symbols: Symbols displayed in the inquiry.
times: Timestamps associated with each symbol.
target_info: Target information about the stimuli.
window_length: Length of time between stimuli presentation.
flash_time: Duration of each stimulus.
stim_length: Length of stimulus sequence.
Returns:
np.ndarray: Likelihood values for each symbol.
"""
data = self.preprocess(raw_data, times, flash_time)
# We need the likelihoods in the form of p(label | gaze).
# predict returns the argmax of the likelihoods.
# Therefore we need predict_proba method to get the likelihoods.
likelihood = self.signal_model.evaluate_likelihood(
data, symbols, self.symbol_set)
return likelihood
class SwitchEvaluator(EvidenceEvaluator):
"""Evidence evaluator for extracting symbol likelihoods from switch data.
This evaluator processes raw switch input data to compute likelihoods
for different symbols based on button press patterns.
Attributes:
consumes: Type of input data (MARKERS).
produces: Type of evidence output (BTN).
button_press_mode: Mode of button press interpretation.
trial_count: Number of trials in stimulus sequence.
"""
consumes = ContentType.MARKERS
produces = EvidenceType.BTN
def __init__(
self,
symbol_set: List[str],
signal_model: SignalModel,
parameters: Optional[Parameters] = None) -> None:
"""Initialize the switch evaluator.
Args:
symbol_set: List of possible symbols that can be presented.
signal_model: Model trained using calibration session data.
parameters: Optional configuration parameters.
Raises:
AssertionError: If button press mode is not supported.
"""
super().__init__(symbol_set, signal_model, parameters)
if not parameters:
raise ValueError("Parameters required for SwitchEvaluator")
self.button_press_mode = ButtonPressMode(
parameters.get('preview_inquiry_progress_method'))
self.trial_count = parameters.get('stim_length')
if self.button_press_mode == ButtonPressMode.NOTHING:
raise AssertionError((
"Button press mode not supported.",
"To run without button press evidence set the acq_mode to exclude MARKERS."
))
def preprocess(
self,
raw_data: np.ndarray,
times: List[float],
target_info: List[str],
window_length: float) -> np.ndarray:
"""Preprocess the inquiry switch data.
Determines the return data based on whether the switch was pressed
during the inquiry and the configured ButtonPressMode.
Args:
raw_data: Switch input data.
times: Timestamps associated with each symbol.
target_info: Target information about the stimuli.
window_length: Length of time between stimuli presentation.
Returns:
np.ndarray: Preprocessed switch data.
"""
switch_was_pressed = np.any(raw_data)
# shape: (channels/1, trials/trial_count, samples/1)
data_shape = (1, self.trial_count, 1)
ones = np.ones(data_shape)
zeros = np.zeros(data_shape)
# Inquiries with 1.0s will be upgraded/supported by the model probabilities.
rules = {
ButtonPressMode.ACCEPT:
lambda: ones if switch_was_pressed else zeros,
ButtonPressMode.REJECT:
lambda: ones if not switch_was_pressed else zeros
}
return rules[self.button_press_mode]()
# pylint: disable=arguments-differ
def evaluate(self, raw_data: np.ndarray, symbols: List[str],
times: List[float], target_info: List[str],
window_length: float, *args: Any, **kwargs: Any) -> np.ndarray:
"""Evaluate the evidence.
Parameters
----------
raw_data - C x L eeg data where C is number of channels and L is the
signal length
symbols - symbols displayed in the inquiry
times - timestamps associated with each symbol
target_info - target information about the stimuli;
ex. ['nontarget', 'nontarget', ...]
window_length - The length of the time between stimuli presentation
"""
data = self.preprocess(raw_data, times, target_info, window_length)
probs = self.signal_model.compute_likelihood_ratio(
data, symbols, self.symbol_set)
return probs
def get_evaluator(
data_source: ContentType,
evidence_type: Optional[EvidenceType] = None
) -> Type[EvidenceEvaluator]:
"""Returns the matching evaluator class.
Parameters
----------
data_source - type of data that the evaluator should consume
evidence_type - type of evidence that the evaluator should produce.
"""
matches = [
cls for cls in EvidenceEvaluator.__subclasses__()
if cls.consumes == data_source and (
evidence_type is None or cls.produces == evidence_type)
]
if matches:
return matches[0]
else:
msg = f"Evidence Evaluator not found for {data_source.name}"
if evidence_type:
msg += f" -> {evidence_type.name}"
raise MissingEvidenceEvaluator(msg)
def find_matching_evaluator(
signal_model: SignalModel) -> Type[EvidenceEvaluator]:
"""Find the first EvidenceEvaluator compatible with the given signal model."""
content_type = ContentType(signal_model.metadata.device_spec.content_type)
# Metadata may provide an EvidenceType with a model so the same data source can
# be used to produce multiple types of evidence (ex. alpha)
evidence_type = None
model_output = signal_model.metadata.evidence_type
if model_output:
try:
evidence_type = EvidenceType(model_output.upper())
except ValueError:
log.error(f"Unsupported evidence type: {model_output}")
return get_evaluator(content_type, evidence_type)
def init_evidence_evaluator(
symbol_set: List[str],
signal_model: SignalModel,
parameters: Optional[Parameters] = None) -> EvidenceEvaluator:
"""Find an EvidenceEvaluator that matches the given signal_model and initialize it."""
evaluator_class = find_matching_evaluator(signal_model)
return evaluator_class(symbol_set, signal_model, parameters)