bidsaid.events.PresentationBlockExtractor
- class bidsaid.events.PresentationBlockExtractor(log_or_df, trial_types, scanner_event_type, scanner_trigger_code, trial_column_name='Code', convert_to_seconds=None, initial_column_headers=('Trial', 'Event Type'), n_discarded_volumes=0, tr=None, rest_block_codes=None, rest_code_pattern='fixed', quit_code=None, split_cue_from_block=False, unsplit_trial_types=None, cue_suffix='_cue', drop_cue_rows=False)[source]
Extract onsets, durations, and trial types from Presentation logs using a block design.
Parameters
- log_or_df
str,Path,pandas.DataFrame The Presentation log as a file path or the Presentation DataFrame returned by
bidsaid.parsers.load_presentation_log.Important
If a text file is used, data are assumed to have at least one element that is an digit or float during parsing.
- trial_types
Iterable[str] The names of the trial types (i.e., “Face”, “Place”). Serves to identify the boundaries of a trial type of interest. Regex can be used.
- trial_column_name
str, default=”Code” Name of the column containing the trial types.
- scanner_event_type
str The event type in the “Event Type” column the scanner trigger is listed under (e.g., “Pulse”, “Response”, “Picture”, etc).
- scanner_trigger_code
str Code listed under “Code” for the scanner start (e.g., “54”, “99”, “trigger). Used with
scanner_event_typeto compute the onset times of the trials relative to the scanner start time then clip the dataframe to ensure that no trials before the start of the scanner is initiated.Note
Uses the first index of the rows in the dataframe with values provided for
scanner_event_typeandscanner_trigger_code.- convert_to_seconds
list[str]orNone, default=None Convert the time resolution of the specified columns from 0.1 ms to seconds. See Presentation Timing.
Important
Recommend time resolution of the “Time” and “Duration” column to be converted.
- initial_column_headers
Iterable[str], default=(“Trial”, “Event Type”) The initial column headers for data. Only used when
log_or_dfis a file path.- n_discarded_volumes
int, default=0 Number of non-steady state scans discarded by the scanner at the start of the sequence.
Important
Only used when
trigger_column_nameis specified.Only set this parameter if scanner trigger is sent before these volumes are acquired so that the start time of the first retained volume is shifted forward by (
n_discarded_volumes * tr). If the scanner sends trigger after discarding the volumes, do not set this parameter. Explanation from Neurostars.
- tr
float,int, orNone, default=None The repetition time provided in seconds if data was converted to seconds or in ten thousand seconds if not converted.
- rest_block_codes
str,Iterable[str]orNone, default=None The code name(s) for the rest block(s). Used when a resting state block is between the events to compute the correct block duration. If None, the block duration will be computed based on the starting index of the trial types given by
trial_types. If specified andrest_code_patternis “variable”, will be used withtrial_typesto compute the correct duration. Regex can be used.- rest_code_pattern
Literal["fixed", "variable"], default=”fixed” Pattern of the rest blocks. For “fixed”, the rest code is assumed to appear between each block. For “variable”, it is assumed that the rest code(s) inconsistently appears after each block.
- quit_code
strorNone, default=None The quit code. Suggest to use in cases when a quit code, as opposed to a rest code, is preceded by a trial block. Ideally, this should be a unique code.
- split_cue_from_block
bool, default=False Whether to split cue from block. If True, the row index containing the trial type name will have a separate onset and duration time. In addition, only the row indices proceeding the row index of the cue row will be used for computing the mean reaction time and accuracy for a block.
Important
Use this when your log structure has the following stucture, where
trial_typesare followed by their first stimulus:Row
Code
0
Face
1
stim1
2
response
3
stim2
4
response
5
Face
6
stim1
…
…
Do not use this when every row in the block shares the same code:
0
Face
1
response
2
Face
3
response
…
…
- unsplit_trial_types
Iterable[str]orNone, default=None Block cue names that should not have their cue split from the block. Only used when
split_cue_from_blockis True. Blocks specified here will always start at the cue row.- cue_suffix
str, default=”_cue” Suffix for cue trial type names.
- drop_cue_rows
bool, defaut=False Whether to drop cue rows from the output.
Attributes
- df
pandas.DataFrame DataFrame containing the log data. If the scanner trigger is identified using
scanner_event_typeandscanner_trigger_code, then rows preceding the first scanner are dropped and the index is reset.- trial_types
list[str] The names of the trial types (i.e., Face, Place).
- scanner_event_type
str Event type of scanner trigger.
- scanner_trigger_code
str Code for the scanner trigger.
- trial_column_name
str Name of column containing the trial types.
- n_discarded_scans
int Number of non-steady state scans discarded by scanner.
- tr
float,int, orNone The repetition time.
- scanner_start_time
floatorNone Time when scanner sends the pulse. If
n_discarded_volumesis not 0 andtris specified, then this time will be shifted forward (scanner_start_time = scanner_start_time + n_discarded_volumes * tr) to reflect the time when the first steady state volume was retained. Otherwise, the time extracted from the log data is assumed to be the time when the first steady state volume was retained.- starting_block_indices
list[int] The indices of when each trial block of interest (specified by
trial_types) begins.- rest_block_codesobj:list[str] or
None, default=None The rest block code(s).
- rest_code_pattern
Literal["fixed", "variable"], default=”fixed” Pattern of the rest blocks.
- quit_code
strorNone The quit code.
- split_cue_from_block
bool Whether to split cue from block.
- unsplit_trial_types
Iterable[str] Names of the blocks that should not have their cue split from the block. Regex can be used.
- cue_suffix
str Suffix for cue trial type names.
- drop_cue_rows
bool Whether to drop cue rows from the output.
Example
>>> import pandas as pd >>> from bidsaid.events import PresentationBlockExtractor >>> extractor = PresentationBlockExtractor( ... log_file, ... trial_types=("Face", "Place"), ... scanner_event_type="Pulse", ... scanner_trigger_code="99", ... convert_to_seconds=["Time"], ... rest_block_codes="crosshair", ... ) >>> events = {} >>> events["onset"] = extractor.extract_onsets() >>> events["duration"] = extractor.extract_durations() >>> events["trial_type"] = extractor.extract_trial_types() >>> # Mean reaction time for all trials with a response >>> events["mean_rt"] = extractor.extract_mean_reaction_times() >>> # Mean reaction time for correct trials only >>> response_map = {"hit": 1, "miss": 0} >>> events["mean_rt_correct"] = extractor.extract_mean_reaction_times( ... response_map=response_map, ... response_type="correct", ... ) >>> # Mean accuracy >>> events["mean_accuracy"] = extractor.extract_mean_accuracies(response_map=response_map) >>> df = pd.DataFrame(events)
Methods
Extract the duration for each block.
extract_mean_accuracies(response_map[, ...])Extract mean accuracy for each block.
extract_mean_reaction_times([response_map, ...])Extract mean reaction times for each block.
extract_onsets([scanner_start_time])Extract the onset times for each event.
extract_response_counts([response_map, ...])Extract the number of trials with a response for each block.
Extract the trial type for each block.
- log_or_df