def saveAsWideText(self, fileName, delim=None, matrixOnly=False, appendFile=False, encoding='utf-8', fileCollisionMethod='rename'): """Saves a long, wide-format text file, with one line representing the attributes and data for a single trial. Suitable for analysis in R and SPSS. If `appendFile=True` then the data will be added to the bottom of an existing file. Otherwise, if the file exists already it will be overwritten If `matrixOnly=True` then the file will not contain a header row, which can be handy if you want to append data to an existing file of the same format. encoding: The encoding to use when saving a the file. Defaults to `utf-8`. fileCollisionMethod: Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision` """ # set default delimiter if none given if delim is None: delim = genDelimiter(fileName) # create the file or send to stdout f = openOutputFile( fileName, append=appendFile, delim=delim, fileCollisionMethod=fileCollisionMethod, encoding=encoding) names = self._getAllParamNames() names.extend(self.dataNames) # names from the extraInfo dictionary names.extend(self._getExtraInfo()[0]) # write a header line if not matrixOnly: for heading in names: f.write(u'%s%s' % (heading, delim)) f.write('\n') # write the data for each entry for entry in self.entries: for name in names: if name in entry: ename = str(entry[name]) if ',' in ename or '\n' in ename: fmt = u'"%s"%s' else: fmt = u'%s%s' f.write(fmt % (entry[name], delim)) else: f.write(delim) f.write('\n') if f != sys.stdout: f.close() logging.info('saved data to %r' % f.name)
def test_genDelimiter(): baseName = 'testfile' extensions = ['.csv', '.CSV', '.tsv', '.txt', '.unknown', ''] correctDelimiters = [',', ',', '\t', '\t', '\t', '\t'] for extension, correctDelimiter in zip(extensions, correctDelimiters): fileName = baseName + extension delimiter = genDelimiter(fileName) assert delimiter == correctDelimiter
def saveAsWideText(self, fileName, delim='auto', matrixOnly=False, appendFile=None, encoding='utf-8-sig', fileCollisionMethod='rename', sortColumns=False): """Saves a long, wide-format text file, with one line representing the attributes and data for a single trial. Suitable for analysis in R and SPSS. If `appendFile=True` then the data will be added to the bottom of an existing file. Otherwise, if the file exists already it will be kept and a new file will be created with a slightly different name. If you want to overwrite the old file, pass 'overwrite' to ``fileCollisionMethod``. If `matrixOnly=True` then the file will not contain a header row, which can be handy if you want to append data to an existing file of the same format. :Parameters: fileName: if extension is not specified, '.csv' will be appended if the delimiter is ',', else '.tsv' will be appended. Can include path info. delim: allows the user to use a delimiter other than the default tab ("," is popular with file extension ".csv") matrixOnly: outputs the data with no header row. appendFile: will add this output to the end of the specified file if it already exists. encoding: The encoding to use when saving a the file. Defaults to `utf-8-sig`. fileCollisionMethod: Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision` sortColumns: will sort columns alphabetically by header name if True """ # set default delimiter if none given delimOptions = {'comma': ",", 'semicolon': ";", 'tab': "\t"} if delim == 'auto': delim = genDelimiter(fileName) elif delim in delimOptions: delim = delimOptions[delim] if appendFile is None: appendFile = self.appendFiles # create the file or send to stdout fileName = genFilenameFromDelimiter(fileName, delim) f = openOutputFile(fileName, append=appendFile, fileCollisionMethod=fileCollisionMethod, encoding=encoding) names = self._getAllParamNames() names.extend(self.dataNames) # names from the extraInfo dictionary names.extend(self._getExtraInfo()[0]) if len(names) < 1: logging.error( "No data was found, so data file may not look as expected.") # sort names if requested if sortColumns: names.sort() # write a header line if not matrixOnly: for heading in names: f.write(u'%s%s' % (heading, delim)) f.write('\n') # write the data for each entry for entry in self.getAllEntries(): for name in names: if name in entry: ename = str(entry[name]) if ',' in ename or '\n' in ename: fmt = u'"%s"%s' else: fmt = u'%s%s' f.write(fmt % (entry[name], delim)) else: f.write(delim) f.write('\n') if f != sys.stdout: f.close() logging.info('saved data to %r' % f.name)
def saveAsText(self, fileName, stimOut=None, dataOut=('n', 'all_mean', 'all_std', 'all_raw'), delim=None, matrixOnly=False, appendFile=True, summarised=True, fileCollisionMethod='rename', encoding='utf-8'): """ Write a text file with the data and various chosen stimulus attributes :Parameters: fileName: will have .tsv appended and can include path info. stimOut: the stimulus attributes to be output. To use this you need to use a list of dictionaries and give here the names of dictionary keys that you want as strings dataOut: a list of strings specifying the dataType and the analysis to be performed,in the form `dataType_analysis`. The data can be any of the types that you added using trialHandler.data.add() and the analysis can be either 'raw' or most things in the numpy library, including; 'mean','std','median','max','min'... The default values will output the raw, mean and std of all datatypes found delim: allows the user to use a delimiter other than tab ("," is popular with file extension ".csv") matrixOnly: outputs the data with no header row or extraInfo attached appendFile: will add this output to the end of the specified file if it already exists fileCollisionMethod: Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision` encoding: The encoding to use when saving a the file. Defaults to `utf-8`. """ fileName = pathToString(fileName) if stimOut is None: stimOut = [] if self.thisTrialN < 1 and self.thisRepN < 1: # if both are < 1 we haven't started if self.autoLog: logging.info('TrialHandler.saveAsText called but no trials' ' completed. Nothing saved') return -1 dataArray = self._createOutputArray(stimOut=stimOut, dataOut=dataOut, matrixOnly=matrixOnly) # set default delimiter if none given if delim is None: delim = genDelimiter(fileName) # create the file or send to stdout fileName = genFilenameFromDelimiter(fileName, delim) with openOutputFile(fileName=fileName, append=appendFile, fileCollisionMethod=fileCollisionMethod, encoding=encoding) as f: # loop through lines in the data matrix for line in dataArray: for cellN, entry in enumerate(line): # surround in quotes to prevent effect of delimiter if delim in str(entry): f.write(u'"%s"' % str(entry)) else: f.write(str(entry)) if cellN < (len(line) - 1): f.write(delim) f.write("\n") # add an EOL at end of each line if (fileName is not None) and (fileName != 'stdout') and self.autoLog: logging.info('saved data to %s' % f.name)
def saveAsWideText(self, fileName, delim=None, matrixOnly=False, appendFile=False, encoding='utf-8', fileCollisionMethod='rename'): """Saves a long, wide-format text file, with one line representing the attributes and data for a single trial. Suitable for analysis in R and SPSS. If `appendFile=True` then the data will be added to the bottom of an existing file. Otherwise, if the file exists already it will be overwritten If `matrixOnly=True` then the file will not contain a header row, which can be handy if you want to append data to an existing file of the same format. :Parameters: fileName: if extension is not specified, '.csv' will be appended if the delimiter is ',', else '.tsv' will be appended. Can include path info. delim: allows the user to use a delimiter other than the default tab ("," is popular with file extension ".csv") matrixOnly: outputs the data with no header row. appendFile: will add this output to the end of the specified file if it already exists. encoding: The encoding to use when saving a the file. Defaults to `utf-8`. fileCollisionMethod: Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision` """ # set default delimiter if none given if delim is None: delim = genDelimiter(fileName) # create the file or send to stdout fileName = genFilenameFromDelimiter(fileName, delim) f = openOutputFile(fileName, append=appendFile, fileCollisionMethod=fileCollisionMethod, encoding=encoding) names = self._getAllParamNames() names.extend(self.dataNames) # names from the extraInfo dictionary names.extend(self._getExtraInfo()[0]) # write a header line if not matrixOnly: for heading in names: f.write(u'%s%s' % (heading, delim)) f.write('\n') # write the data for each entry for entry in self.entries: for name in names: if name in entry: ename = str(entry[name]) if ',' in ename or '\n' in ename: fmt = u'"%s"%s' else: fmt = u'%s%s' f.write(fmt % (entry[name], delim)) else: f.write(delim) f.write('\n') if f != sys.stdout: f.close() logging.info('saved data to %r' % f.name)
def saveAsText(self, fileName, stimOut=None, dataOut=('n', 'all_mean', 'all_std', 'all_raw'), delim=None, matrixOnly=False, appendFile=True, summarised=True, fileCollisionMethod='rename', encoding='utf-8'): """ Write a text file with the data and various chosen stimulus attributes :Parameters: fileName: will have .tsv appended and can include path info. stimOut: the stimulus attributes to be output. To use this you need to use a list of dictionaries and give here the names of dictionary keys that you want as strings dataOut: a list of strings specifying the dataType and the analysis to be performed,in the form `dataType_analysis`. The data can be any of the types that you added using trialHandler.data.add() and the analysis can be either 'raw' or most things in the numpy library, including; 'mean','std','median','max','min'... The default values will output the raw, mean and std of all datatypes found delim: allows the user to use a delimiter other than tab ("," is popular with file extension ".csv") matrixOnly: outputs the data with no header row or extraInfo attached appendFile: will add this output to the end of the specified file if it already exists fileCollisionMethod: Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision` encoding: The encoding to use when saving a the file. Defaults to `utf-8`. """ if stimOut is None: stimOut = [] if self.thisTrialN < 1 and self.thisRepN < 1: # if both are < 1 we haven't started if self.autoLog: logging.info('TrialHandler.saveAsText called but no trials' ' completed. Nothing saved') return -1 dataArray = self._createOutputArray(stimOut=stimOut, dataOut=dataOut, matrixOnly=matrixOnly) # set default delimiter if none given if delim is None: delim = genDelimiter(fileName) # create the file or send to stdout f = openOutputFile( fileName, append=appendFile, delim=delim, fileCollisionMethod=fileCollisionMethod, encoding=encoding) # loop through lines in the data matrix for line in dataArray: for cellN, entry in enumerate(line): # surround in quotes to prevent effect of delimiter if delim in str(entry): f.write(u'"%s"' % str(entry)) else: f.write(str(entry)) if cellN < (len(line) - 1): f.write(delim) f.write("\n") # add an EOL at end of each line if f != sys.stdout: f.close() if self.autoLog: logging.info('saved data to %s' % f.name)