Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas to_csv creates empty, unspecified file

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.

  • I can correctly import the underlying csv file for stimulus creation
  • if I include print df, I can see the dataframe in the console, so the data exists somewhere, at least temporarily
  • My system (Mac) is german, but the Windows 10 and 7 computers are set to french
  • There is no error message at all.
  • I have successfully done this experiment on a (belgian) french Mac before.



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:

  • different versions of psychopy, except the one using python 3
  • I suspected an encoding problem b/c of the different keyboard layouts, so I changed that, but no change (and hadn't been a problem on the belgian french computers)
  • The solutions proposed here: Changing default encoding of Python?
  • There are no special characters in the file names, and not in all of the csv files read in, but they all aren't saved.
  • used admin privileges on the computer

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!

like image 465
JuliaC Avatar asked Nov 08 '22 05:11

JuliaC


1 Answers

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:

Close psychopy before script terminates

Add this to the end of the script:

w.close()
core.quit()

Close csv file object before script terminates

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()
like image 79
Jonas Lindeløv Avatar answered Nov 14 '22 23:11

Jonas Lindeløv