def saveAsPickle(self, fileName, fileCollisionMethod='rename'): """Basically just saves a copy of the handler (with data) to a pickle file. This can be reloaded if necessary and further analyses carried out. :Parameters: fileCollisionMethod: Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision` """ if self.thisTrialN < 1 and self.thisRepN < 1: # if both are < 1 we haven't started if self.autoLog: logging.info('.saveAsPickle() called but no trials completed.' ' Nothing saved') return -1 # otherwise use default location if not fileName.endswith('.psydat'): fileName += '.psydat' f = openOutputFile(fileName, append=False, fileCollisionMethod=fileCollisionMethod) pickle.dump(self, f) f.close() logging.info('saved data to %s' % f.name)
def saveAsPickle(self, fileName, fileCollisionMethod='rename'): """Basically just saves a copy of the handler (with data) to a pickle file. This can be reloaded if necessary and further analyses carried out. :Parameters: fileCollisionMethod: Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision` """ fileName = pathToString(fileName) if self.thisTrialN < 1 and self.thisRepN < 1: # if both are < 1 we haven't started if self.autoLog: logging.info('.saveAsPickle() called but no trials completed.' ' Nothing saved') return -1 if not fileName.endswith('.psydat'): fileName += '.psydat' with openOutputFile(fileName=fileName, append=False, fileCollisionMethod=fileCollisionMethod) as f: pickle.dump(self, f) 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. 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 saveAsJson(self, fileName=None, encoding='utf-8', fileCollisionMethod='rename'): """ Serialize the object to the JSON format. Parameters ---------- fileName: string, or None the name of the file to create or append. Can include a relative or absolute path. If `None`, will not write to a file, but return an in-memory JSON object. encoding : string, optional The encoding to use when writing the file. This parameter will be ignored if `append` is `False` and `fileName` ends with `.psydat` or `.npy` (i.e. if a binary file is to be written). fileCollisionMethod : string Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision`. Can be either of `'rename'`, `'overwrite'`, or `'fail'`. Notes ----- Currently, a copy of the object is created, and the copy's .origin attribute is set to an empty string before serializing because loading the created JSON file would sometimes fail otherwise. """ self_copy = copy.deepcopy(self) self_copy.origin = '' msg = ('Setting attribute .origin to empty string during JSON ' 'serialization.') logging.warn(msg) if fileName is None: return json_tricks.np.dumps(self_copy) else: f = openOutputFile(fileName, fileCollisionMethod=fileCollisionMethod, encoding=encoding) json_tricks.np.dump(self_copy, f) if f != sys.stdout: f.close() logging.info('Saved JSON data to %s' % f.name)
def saveAsJson(self, fileName=None, encoding='utf-8', fileCollisionMethod='rename'): """ Serialize the object to the JSON format. Parameters ---------- fileName: string, or None the name of the file to create or append. Can include a relative or absolute path. If `None`, will not write to a file, but return an in-memory JSON object. encoding : string, optional The encoding to use when writing the file. This parameter will be ignored if `append` is `False` and `fileName` ends with `.psydat` or `.npy` (i.e. if a binary file is to be written). fileCollisionMethod : string Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision`. Can be either of `'rename'`, `'overwrite'`, or `'fail'`. Notes ----- Currently, a copy of the object is created, and the copy's .origin attribute is set to an empty string before serializing because loading the created JSON file would sometimes fail otherwise. """ fileName = pathToString(fileName) self_copy = copy.deepcopy(self) self_copy.origin = '' msg = ('Setting attribute .origin to empty string during JSON ' 'serialization.') logging.warn(msg) if (fileName is None) or (fileName == 'stdout'): return json_tricks.dumps(self_copy) else: with openOutputFile(fileName=fileName, fileCollisionMethod=fileCollisionMethod, encoding=encoding) as f: json_tricks.dump(self_copy, f) logging.info('Saved JSON data to %s' % f.name)
def saveAsJson(self, fileName=None, encoding='utf-8', fileCollisionMethod='rename'): """ Serialize the object to the JSON format. Parameters ---------- fileName: string, or None the name of the file to create or append. Can include a relative or absolute path. If `None`, will not write to a file, but return an in-memory JSON object. encoding : string, optional The encoding to use when writing the file. fileCollisionMethod : string Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision`. Can be either of `'rename'`, `'overwrite'`, or `'fail'`. Notes ----- Currently, a copy of the object is created, and the copy's .origin attribute is set to an empty string before serializing because loading the created JSON file would sometimes fail otherwise. """ fileName = pathToString(fileName) self_copy = copy.deepcopy(self) self_copy.origin = '' msg = ('Setting attribute .origin to empty string during JSON ' 'serialization.') logging.warn(msg) if (fileName is None) or (fileName == 'stdout'): return json_tricks.dumps(self_copy) else: with openOutputFile(fileName=fileName, fileCollisionMethod=fileCollisionMethod, encoding=encoding) as f: json_tricks.dump(self_copy, f) logging.info('Saved JSON data to %s' % f.name)
def saveAsPickle(self, fileName, fileCollisionMethod='rename'): """Basically just saves a copy of self (with data) to a pickle file. This can be reloaded if necessary and further analyses carried out. :Parameters: fileCollisionMethod: Collision method passed to :func:`~psychopy.tools.fileerrortools.handleFileCollision` """ # Store the current state of self.savePickle and self.saveWideText # for later use: # We are going to set both to False before saving, # so PsychoPy won't try to save again after loading the pickled # .psydat file from disk. # # After saving, the initial state of self.savePickle and # self.saveWideText is restored. # # See # https://groups.google.com/d/msg/psychopy-dev/Z4m_UX88q8U/UGuh1eeyjMEJ savePickle = self.savePickle saveWideText = self.saveWideText self.savePickle = False self.saveWideText = False origEntries = self.entries self.entries = self.getAllEntries() # otherwise use default location if not fileName.endswith('.psydat'): fileName += '.psydat' with openOutputFile(fileName=fileName, append=False, fileCollisionMethod=fileCollisionMethod) as f: pickle.dump(self, f) if (fileName is not None) and (fileName != 'stdout'): logging.info('saved data to %s' % f.name) self.entries = origEntries # revert list of completed entries post-save self.savePickle = savePickle self.saveWideText = saveWideText
def test_stdout(self): f = openOutputFile(None) assert f is sys.stdout f = openOutputFile('stdout') assert f is sys.stdout
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 test_default_parameters(self): self.f = openOutputFile(self.baseFileName)
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 test_append(self): self.f = openOutputFile(self.baseFileName, append=True)
def test_delim_comma(self): self.f = openOutputFile(self.baseFileName, delim=',')
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)
def test_delim_tab(self): self.f = openOutputFile(self.baseFileName, delim='\t')
def test_delim_delim_empty(self): self.f = openOutputFile(self.baseFileName, delim='')
def test_append(self): with openOutputFile(self.baseFileName, append=True) as f: assert f.encoding == 'utf-8-sig' assert f.closed is False assert f.stream.mode == 'ab'
def test_default_parameters(self): with openOutputFile(self.baseFileName) as f: assert f.encoding == 'utf-8-sig' assert f.closed is False assert f.stream.mode == 'wb'
def test_append_and_delim(self): self.f = openOutputFile(self.baseFileName, append=True, delim=',')