"""Python script to convert the HDF5 data file in the Electronic
Supplemental Material for the following paper to a text file:

Weidemann, C. T. & Kahana, M. J. (2016). Assessing recognition
memory using confidence ratings and response times. Royal Society
Open Science, 3, 150670.

Not all information from the HDF5 file is transferred to the .tsv
file with the current script, but the below script can be adapted
to extract further information from the HDF5 file.

This script was written by Christoph T. Weidemann (ctw@cogsci.info),
and is released into the public domain (see comment below for details
and disclaimer).

"""

############################################################################
############################################################################
############################################################################
####
#### This is free and unencumbered software released into the public domain.
####
#### Anyone is free to copy, modify, publish, use, compile, sell, or
#### distribute this software, either in source code form or as a compiled
#### binary, for any purpose, commercial or non-commercial, and by any
#### means.
####
#### In jurisdictions that recognize copyright laws, the author or authors
#### of this software dedicate any and all copyright interest in the
#### software to the public domain. We make this dedication for the benefit
#### of the public at large and to the detriment of our heirs and
#### successors. We intend this dedication to be an overt act of
#### relinquishment in perpetuity of all present and future rights to this
#### software under copyright law.
####
#### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#### EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#### MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#### IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
#### OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#### ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#### OTHER DEALINGS IN THE SOFTWARE.
####
#### For more information, please refer to <http://unlicense.org/>
####
############################################################################
############################################################################
############################################################################



import numpy as np
import h5py

# path to input data file in HDF5 format:
hdf5_file = 'WeidemannKahana2016_data.hdf5'

# path to output data file in text format:
txt_file = 'WeidemannKahana2016_data.tsv'

# Formats for the columns in the text file:
fmts = ['%s', '%d', '%d', '%f', '%s', '%s', '%f',  '%f', '%s',
        '%d', '%s', '%s']

# Names of the columns in the text file:
colnames = ['participant', 'session', 'test_list', 'study_list', 'type',
            'recog_resp', 'recog_conf', 'recog_rt', 'item', 'itemno',
            'recalled', 'finalrecalled']

# Character to delimit the columns:
delimchar = '\t'

np.savetxt(txt_file, h5py.File(hdf5_file)['WeidemannKahana2016_data'],
           fmt=fmts, delimiter=delimchar, header=delimchar.join(colnames))
