|
#!/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) |