Repository: psychopy/psychopy

Issue:
extraInfo vars not recorded using TrialHandler.saveAsWideText()
I've been having trouble getting the extra variables I pass as `TrialHandler`'s
`extraInfo` argument to actually show up in the data file when using
`saveAsWideText()`. I think I've now
tracked down the bug to the lines in `data.py -> saveAsWideText()` where the entries get created:

``` python
# create a dictionary representing each trial:
# this is wide format, so we want fixed information (e.g. subject ID, date, etc) repeated every line if it exists:
if (self.extraInfo != None):
    nextEntry = self.extraInfo.copy()
else:
    nextEntry = {}

# etc.

# now collect the value from each trial of the variables named in the header:
for parameterName in header:
    # the header includes both trial and data variables, so need to check before accessing:
    if self.trialList[trialTypeIndex] and parameterName in self.trialList[trialTypeIndex]:
        nextEntry[parameterName] = self.trialList[trialTypeIndex][parameterName]
    elif parameterName in self.data:
        nextEntry[parameterName] = self.data[parameterName][trialTypeIndex][repThisType]
    else: # allow a null value if this parameter wasn't explicitly stored on this trial:
        if parameterName == "TrialNumber":
            nextEntry[parameterName] = trialCount
        else:
            nextEntry[parameterName] = ''
```

Because the variables in `extraInfo` are also in `header`, that loop through the
header's parameter names overwrites them with empty strings. Should
be pretty easy to fix, I'll try to get a pull request going now.



Discussion:

Resolving patch:
diff --git a/psychopy/data.py b/psychopy/data.py
--- a/psychopy/data.py
+++ b/psychopy/data.py
@@ -1187,7 +1187,9 @@ def saveAsWideText(self,fileName,
         header.extend(self.data.dataTypes)
         # get the extra 'wide' parameter names into the header line:
         header.insert(0,"TrialNumber")
-        if (self.extraInfo != None):
+        # this is wide format, so we want fixed information
+        # (e.g. subject ID, date, etc) repeated every line if it exists:
+        if self.extraInfo is not None:
             for key in self.extraInfo:
                 header.insert(0, key)
         df = DataFrame(columns = header)
@@ -1210,11 +1212,7 @@ def saveAsWideText(self,fileName,
                 repThisType=repsPerType[trialTypeIndex]#what repeat are we on for this trial type?
 
                 # create a dictionary representing each trial:
-                # this is wide format, so we want fixed information (e.g. subject ID, date, etc) repeated every line if it exists:
-                if (self.extraInfo != None):
-                    nextEntry = self.extraInfo.copy()
-                else:
-                    nextEntry = {}
+                nextEntry = {}
 
                 # add a trial number so the original order of the data can always be recovered if sorted during analysis:
                 trialCount += 1
@@ -1226,6 +1224,8 @@ def saveAsWideText(self,fileName,
                         nextEntry[parameterName] = self.trialList[trialTypeIndex][parameterName]
                     elif parameterName in self.data:
                         nextEntry[parameterName] = self.data[parameterName][trialTypeIndex][repThisType]
+                    elif self.extraInfo is not None and parameterName in self.extraInfo:
+                        nextEntry[parameterName] = self.extraInfo[parameterName]
                     else: # allow a null value if this parameter wasn't explicitly stored on this trial:
                         if parameterName == "TrialNumber":
                             nextEntry[parameterName] = trialCount


Test patch: