Skip to content

Instantly share code, notes, and snippets.

@louist87

louist87/S01.mat Secret

Last active June 18, 2021 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louist87/cfc754cc5d389e7045b2 to your computer and use it in GitHub Desktop.
Save louist87/cfc754cc5d389e7045b2 to your computer and use it in GitHub Desktop.
Retrospective attention gates discrete conscious access to past sensory stimuli

Authors

  1. Louis Thibault (corresponding author)
  2. Ronald van den Berg
  3. Patrick Cavanagh
  4. Claire Sergent

Laboratoire Psychologie de la Perception, UMR 8242

Manuscript

Description of Data

subnum congruent cuepos normalizedResp response rt soa targetorientation targetpos
int bool int float float float float float int
subject number 1 if cuepos == targetpos 1 if cue left, 2 if cue right, -1 if no cue response error (degrees) absolute response (degrees) response time (s) SOA (s) Orientation of target Gabor (degrees) 1 if target left, 2 if target right
#!/usr/bin/env python3
"""
Convert experimental data from .mat files under raw/ into a single CSV.
"""
import csv
import os.path as osp
from glob import glob
from itertools import chain
from scipy.io import loadmat
HDR = [
'subnum',
'congruent',
'cuepos',
'normalizedResp',
'response',
'rt',
'soa',
'targetorientation',
'targetpos'
]
def get_subnum_from_path(path):
return int(osp.split(path)[0][-2:])
def load_matfile(path):
"""Load a .mat file into a dictionary, squeezing numpy arrays and storing
structs as arrays rather than record types.
"""
d = loadmat(path, struct_as_record=False, squeeze_me=True)
return {k: v for k, v in d.items() if not k.startswith('__')}
def to_tabular(md, subnum):
"""Covert matdict to tuple generator representing tabular data
md : dict
matdict output from `load_matfile`
subnum : int
Subject number
return : generator
Produces tuples, representing rows
"""
for row in zip(*(md[k] for k in sorted(md))):
yield (subnum,) + row
def to_csv(path, it):
"""Convert output of `to_tabular` to a tab-delimited file
Iterators passed via *it are appended in order to a single file.
path : str
Path to output file
it : iterable of tuples
Tuples produced represent rows
"""
with open(path, 'wt') as f:
writer = csv.writer(f, delimiter='\t')
writer.writerow(HDR)
for r in chain(*it):
writer.writerow(r)
def main(src, dst):
"""Aggregate all .mat files matching the glob expression `S??_?_main.mat`,
existing in a directory matching the glob expression `S??` and compile into
a tab-delimited output file, `dst`.
"""
paths = sorted(glob(osp.join(src, 'S??/data.mat')))
mds = map(load_matfile, paths)
rowiters = map(to_tabular, mds, map(get_subnum_from_path, paths))
to_csv(dst, rowiters)
if __name__ == '__main__':
from sys import argv
_, src, dst, *_ = argv
main(src, dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment