File write in wrong order
Hi, I'm using version 3.3.14, Legacy backend. On each trial I'm tracking the mouse trajectory, stored in mouse_data_array (a list of strings). At the end of each trial I then save this array to a .csv file using inline, like this:
mouse_file = open(mouse_filename, "a")
for i in range(0, len(mouse_data_array)):
#print(mouse_data_array[i]) #solely for checking purposes, see below
mouse_file.write(str(var.datetime) + ', ' + mouse_data_array[i] + '\n')
mouse_file.close
So after each trial, file gets opened, data gets dumped, and file gets closed. This is all that happens to the file, nothing more.
What I then would expect is that the datafile shows the trials in order, so first the data of trial 1, then of trial 2 etc. However, this is not the case. Frquently, trials are swapped. Sometimes midayw through the data of trial n, the data of trial n+1 is stored, after which it goes back to trial n, then n+2, and so on.
So the data looks for example like this:
trial_nr x y
1 0 0
1 10 5
1 12 7
2 0 0
2 -4 -20
2 -10 -25
1 15 8
1 19 12
3 0 0
3 4 -6
....
The structure and integrity of mouse_data_array itself is fine, all in the correct order, as checked through e.g. the print(mouse_data_array) command. Also, using writelines instead of write does not matter, still same type of error. The data in the file is complete (none missing), just in the wrong order.
Interestingly, if I include the print command, or merely a 5ms delay (sleep) in the above loop, so after each write command, the problem disappears. It seems that somehow each write command is assigned a process, but some processes get delayed, even across trials and file.close() commands, and so future trials can overtake.... I have not tried this outside OpenSesame, but experienced python programmers in our tech department have never seen it before, so my hunch is that it is OS-specific.
Any idea?
cheers,
Chris
Comments
Foget it.... I found the problem myself (you'll always see, after days of trying to find the fault, I find it just after asking for help - thanks to Joshua Snell).
mouse_file.close should have had a '()', i.e. mouse_file.close()
My bad, not an OS issue. Still, good to know I guess, f.close does not give an error, but does mess up your file.