def read_datafile(filename, only_header_and_variable_names=False, encoding=None,
                  read_variables=None):
    """Read an Expyriment data file.

    Returns the data, the variable names, the subject info & the comments:

    Parameters
    ----------
    filename : str
        name (fullpath) of the Expyriment data file
    only_header_and_variable_names : bool, optional
        if True the function reads only the header and variable names
        (default=False)
    encoding : str, optional
        the encoding with which the contents of the file will be read
    read_variables : array of str, optional
        array of variable names, read only the specified variables

    Returns
    -------
    data : list of list
        data array
    variables : list of str
        variable names list
    subject_info : dict
        dictionary with subject information (incl. date and between
        subject factors)
    comments : str
        string with remaining comments

    """

    delimiter = ","
    variables = None
    subject_info = {}
    comments = ""
    data = []

    if encoding is None:
        with open(filename, 'r') as fl:
            first_line = fl.readline()
            encoding = _re.findall("coding[:=]\s*([-\w.]+)", first_line)
            if encoding == []:
                second_line = fl.readline()
                encoding = _re.findall("coding[:=]\s*([-\w.]+)",
                                       second_line)
                if encoding == []:
                    encoding = [None]
    else:
        encoding = [encoding]

    read_in_columns = None
    fl = _codecs.open(filename, 'rb', encoding[0], errors='replace')
    for ln in fl:
        # parse infos
        ln = _str2unicode(ln.strip())
        if not(ln.startswith("#")):
            if variables is None:
                variables = ln.split(delimiter)
                if only_header_and_variable_names:
                    break
                if read_variables is not None:
                    read_in_columns = map(lambda x:variables.index(x),
                                                            read_variables)
                    variables = map(lambda x:variables[x], read_in_columns)
            else:
                row =ln.split(delimiter)
                if read_in_columns is not None:
                    row = map(lambda x:row[x], read_in_columns)
                data.append(row)
        else:
            if ln.startswith("#s"):
                ln = ln.replace("#s", "")
                tmp = ln.replace("=", ":")
                tmp = tmp.split(":")
                if len(tmp) == 2:
                    subject_info[tmp[0].strip()] = tmp[1].strip()
                else:
                    subject_info["#s{0}".format(len(subject_info))] = ln.strip()
            elif ln.startswith("#date:"):
                ln = ln.replace("#date:", "")
                subject_info["date"] = ln.strip()
            else:
                comments = comments + "\n" + ln
    fl.close()
    # strip variables
    variables = map(lambda x:x.strip(), variables)
    return data, variables, subject_info, comments
Example #2
0
K_SYSREQ = _pygame.K_SYSREQ
K_BREAK = _pygame.K_BREAK
K_MENU = _pygame.K_MENU
K_POWER = _pygame.K_POWER
K_EURO = _pygame.K_EURO
K_ALL_LETTERS = range(K_a, K_z + 1)
K_ALL_DIGITS = range(K_0, K_9 + 1)
K_ALL_KEYPAD_DIGITS = range(K_KP0, K_KP9 + 1)

# Colours
C_BLACK = (0, 0, 0)
C_WHITE = (255, 255, 255)
C_RED = (255, 0, 0)
C_GREEN = (0, 255, 0)
C_BLUE = (0, 0, 255)
C_YELLOW = (255, 255, 0)
C_GREY = (200, 200, 200)
C_DARKGREY = (150, 150, 150)
C_EXPYRIMENT_ORANGE = (255, 150, 50)
C_EXPYRIMENT_PURPLE = (160, 70, 250)

# Permutation types
P_BALANCED_LATIN_SQUARE = 'balanced-latin-square'
P_CYCLED_LATIN_SQUARE = 'cycled-latin-square'
P_RANDOM = 'random'

# Misc
_tmp = _os.path.abspath(
    _os.path.join(_os.path.dirname(__file__), "..", "expyriment_logo.png"))
EXPYRIMENT_LOGO_FILE = _str2unicode(_tmp)
Example #3
0
def read_datafile(filename,
                  only_header_and_variable_names=False,
                  encoding=None,
                  read_variables=None):
    """Read an Expyriment data file.

    Returns the data, the variable names, the subject info & the comments:

    Parameters
    ----------
    filename : str
        name (fullpath) of the Expyriment data file
    only_header_and_variable_names : bool, optional
        if True the function reads only the header and variable names
        (default=False)
    encoding : str, optional
        the encoding with which the contents of the file will be read
    read_variables : array of str, optional
        array of variable names, read only the specified variables

    Returns
    -------
    data : list of list
        data array
    variables : list of str
        variable names list
    subject_info : dict
        dictionary with subject information (incl. date and between
        subject factors)
    comments : str
        string with remaining comments

    """

    delimiter = ","
    variables = None
    subject_info = {}
    comments = ""
    data = []

    if encoding is None:
        with open(filename, 'r') as fl:
            first_line = fl.readline()
            encoding = _re.findall("coding[:=]\s*([-\w.]+)", first_line)
            if encoding == []:
                second_line = fl.readline()
                encoding = _re.findall("coding[:=]\s*([-\w.]+)", second_line)
                if encoding == []:
                    encoding = [None]
    else:
        encoding = [encoding]

    read_in_columns = None
    fl = _codecs.open(filename, 'rb', encoding[0], errors='replace')
    for ln in fl:
        # parse infos
        ln = _str2unicode(ln.strip())
        if not (ln.startswith("#")):
            if variables is None:
                variables = ln.split(delimiter)
                if only_header_and_variable_names:
                    break
                if read_variables is not None:
                    read_in_columns = map(lambda x: variables.index(x),
                                          read_variables)
                    variables = map(lambda x: variables[x], read_in_columns)
            else:
                row = ln.split(delimiter)
                if read_in_columns is not None:
                    row = map(lambda x: row[x], read_in_columns)
                data.append(row)
        else:
            if ln.startswith("#s"):
                ln = ln.replace("#s", "")
                tmp = ln.replace("=", ":")
                tmp = tmp.split(":")
                if len(tmp) == 2:
                    subject_info[tmp[0].strip()] = tmp[1].strip()
                else:
                    subject_info["#s{0}".format(
                        len(subject_info))] = ln.strip()
            elif ln.startswith("#date:"):
                ln = ln.replace("#date:", "")
                subject_info["date"] = ln.strip()
            else:
                comments = comments + "\n" + ln
    fl.close()
    # strip variables
    variables = map(lambda x: x.strip(), variables)
    return data, variables, subject_info, comments
Example #4
0
K_BREAK = _pygame.K_BREAK
K_MENU = _pygame.K_MENU
K_POWER = _pygame.K_POWER
K_EURO = _pygame.K_EURO
K_ALL_LETTERS = range(K_a, K_z + 1)
K_ALL_DIGITS = range(K_0, K_9 + 1)
K_ALL_KEYPAD_DIGITS = range(K_KP0, K_KP9 + 1)

# Colours
C_BLACK = (0, 0, 0)
C_WHITE = (255, 255, 255)
C_RED = (255, 0, 0)
C_GREEN = (0, 255, 0)
C_BLUE = (0, 0, 255)
C_YELLOW = (255, 255, 0)
C_GREY = (200, 200, 200)
C_DARKGREY = (150, 150, 150)
C_EXPYRIMENT_ORANGE = (255, 150, 50)
C_EXPYRIMENT_PURPLE = (160, 70, 250)

# Permutation types
P_BALANCED_LATIN_SQUARE = 'balanced-latin-square'
P_CYCLED_LATIN_SQUARE = 'cycled-latin-square'
P_RANDOM = 'random'

# Misc
_tmp = _os.path.abspath(
    _os.path.join(_os.path.dirname(__file__),
                  "..", "expyriment_logo.png"))
EXPYRIMENT_LOGO_FILE = _str2unicode(_tmp)