I am new to python and programming in general, so this is probably a stupid question.
I am currently preparing a psychology experiment using PsychoPy v1.90.2 standalone, python 2.7, the coder version, based on both an existing script and a tutorial from a colleague. It works perfectly on my computer (Mac) and on a Windows 10 computer, but on a computer using Windows7, it does not save the output (i.e. the participant's responses) to a csv file. An empty file is created, without any file specification, but the correct file name and in the correct location.
from psychopy import visual, core, event, gui, info, data
import pandas as pd
import numpy as np
import os
import random
import sys
import time
GUI = gui.Dlg(title = "example")
GUI.addField('participant:')
GUI.show()
if GUI.OK:
metadata = GUI.data
else:
sys.exit('participant cancelled')
w = visual.Window([1000, 600], color='black', units='pix')
df = pd.DataFrame([[1, 2], [5, 3], [4, 6]], columns=['stim_a', 'stim_b'])
df['participant'] = int(metadata[0])
df['ans'] = ''
df = df.iloc[random.sample(df.index, len(df))]
df.index = range(len(df))
output_df = "S_" + str(df.participant) + "_df" + "_" + time.strftime("%Y-%m:%d_%Hh%M") + ".csv"
for j in range(df.shape[0]):
event.clearEvents()
stim_a = visual.TextStim(w, df.stim_a[j])
stim_a.draw()
w.flip()
resp = event.waitKeys()
df.ans[j] = resp
print df
df.to_csv(output_df, index = False, header = True, sep = ',', encoding = 'utf-8')
What should be there in the end: a csv file containing all the information contained in df initially, plus the "participant" column containing the participant number and the "ans" column containing the key presses performed by the participant.
What I tried:
I know there is a better way to save data than "pure" pandas by using the experimentHandler, but as it worked before, and my knowledge so far is limited to my colleague's way of working with psychopy, i kept that method. I had other compatibility issues which I managed to solve, and I am afraid given the time pressure I am currently in, it is a sunken-cost situation...
EDIT: the working example should now work, sorry for that!
The csv data is written ("flushed") when the script terminates. The problem is likely that it hangs for OS-specific reasons in Windows 7. I have seen this before and solved it in two different ways:
Add this to the end of the script:
w.close()
core.quit()
Copied from an answer here: replace the df.to_csv
line with the following where the file object is explicitly closed before the script terminates.
outfile = open(output_df, 'wb')
df.to_csv(outfile, index = False, header = True, sep = ',', encoding = 'utf-8')
outfile.close()