[open] Inline Script won't work with loop-element
Hi,
I never used OS before and don't know all functions of Python. Now I wanted to run an experiment, where the data presented is in a .csv-file. With an Inline Script I could read the data and (to test, if the script itself works) write it into a textfile (and yes, it works). But if I try to print the data within a loop-element, there's an error-message that the variables would not exist. (I tried it with "[Phrase1]" (without quotes) as content of a variable of the loop-element.)
Abbridged: I want to read the data line by line from the .csv-file and present it phrase to phrase to the test subjects. After the last phrase is presented, the persons should press a button to decide, if the sentence was correct.
Please help me out. Thanks a lot.
Here's the code (so far):
import codecs
handle_read = open('sentences.csv').readlines()
handle_write = open("sentences.txt","w")
for line in handle_read:
data= line.split(",")
id = data[0]
corr = data[1]
Phrase1 = data[2]
Phrase2 = data[3]
Phrase3 = data[4]
Phrase4 = data[5]
Phrase5 = data[6]
Phrase6 = data[7]
if corr == "1":
print Phrase1
handle_write.write(id+": "+Phrase1+" "+Phrase2+" "+Phrase3+" "+Phrase4+" "+Phrase5+" "+Phrase6+" = correct")
print 'id+": "+Phrase1+" "+Phrase2+" "+Phrase3+" "+Phrase4+" "+Phrase5+" "+Phrase6+" = correct"'
else:
handle_write.write(id+": "+Phrase1+" "+Phrase2+" "+Phrase3+" "+Phrase4+" "+Phrase5+" "+Phrase6+" = not correct")
print 'id+": "+Phrase1+" "+Phrase2+" "+Phrase3+" "+Phrase4+" "+Phrase5+" "+Phrase6+" = not correct"'
def print_out(id,Phrase1,Phrase2,Phrase3,Phrase4,Phrase5,Phrase6,corr):
sentence= id+Phrase1+Phrase2+Phrase3+Phrase4+Phrase5+Phrase6+corr
return sentence
Comments
Sry, I don't know, why the code is disarranged
Hi Kiri,
To keep your pasted code readible you could use Markdown syntax as described here:
When you write your collection of variables to your text file by using
everything goes well because
Phrase1
is a variable that has a certain value assigned to it. This value, for example"This is my first phrase"
, will be written to your text file.In your print statement, however, you (accidentally) make the whole collection a string by starting and ending it with single quotes. A string is not variable (in fact it is a so-called literal constant).
In short:
will print, for example,
This is my first phrase
, whereaswill obviously simply print
Phrase1
.So by changing
into
(and doing the same for your other print statement) your problem should be solved.
I would advise you to become more familiar with those things if you want to use Python inline coding for your experiments, for example by doing the 'A Byte of Python' tutorial that can be found here:
Does this answer your question? Best wishes,
Lotje
Did you like my answer? Feel free to
Thanks a lot for the fast answer.
I'll test your suggestion tomorrow.
Hi,
it still doesn't print out anything and just gives an (Runtime) error message that "Variable Phrase1" wouldn't exist?
Hey Kiri,
I'm afraid we're going to need a lot more information! In particular,
We need to know where the error occurs (is it in this inline script item? this piece of code? If so, what does it currently look like? If not, where does the error come from?)
Could you give some more insight into what your experiment currently looks like?
As a side note ( but I'm not sure if anything like this is at play here), you said:
Please have a look at the osdoc items on variables in opensesame - items vs 'pure' python inline scripting
You should realize that there's a major difference between opensesame scripting (where the brackets denote an experiment variable that is generally set in a loop-item or so ) and python scripting (which is done in an inline script item, simply to allow for more flexibility in designing experiments, and which uses pure python syntax..).
For example in python, brackets are not used to denote variables -- so in an inline script,
would be syntactically incorrect
Hi Wouter,
the error occurs at the end of the experiment. Nothing is printed out during the experiment (neither by inline_script nor by loop). The inline_script works, though. But when then the experiment seems to be interrupted trying to go through the loop-element (which follows the inline_script). Then the runtime error occurs.
The loop-element obviously can't find the variable "Phrase1" (which is declared in the inline_script, see the code above). So it can't print it out.
I used the brackets only within the loop-element, because I wanted the loop to print out the content of the variable declared in the inline_script. But even the variable inspector can't find "Phrase1" (or another Phrase) of the inline_script. The brackets are not used in the inline_script itself.
I hope, my explanation helped you to understand my problem.
Hi Kiri,
By declaring/defining a variable within an inline script the way you have done above, i.e. within a python inline script, it is only visible/accessible within that particular inline script.
To make it visible to other inline scripts somewhere else in the experiment, you could either declare the variable global (the link is about functions, but it works the same for variables)
Another (possibly better) option would be, to store them within the experiment object using:
OR (which seems to be what you need here) :
The latter option has the main advantage that you can not only retrieve the variable in inline_script items, but also from gui items using the bracket notation [Phrase], as well as it being logged by the logger item (...and probably found by the variable inspector as well?).
For that reason, it also has to be a variable that can be logged by the logger item. That's why the exp.get() way of doing things is only used for primitive variable types, such as numbers and strings. It is not suitable for passing around lists and dictionaries and such (but for those you can use global or the first method)
I hope this helps!
For more info, you could read :
http://osdoc.cogsci.nl/python-inline-code/experiment-functions/
http://osdoc.cogsci.nl/usage/variables-and-conditional-qifq-statements/#defining-variables
EDIT: as I typed the above, it still wasn't fully clear to me what you wanted to do with the variable within the loop element: but do you mean you want to use one of the phrases read in from a file as the value of a particular variable within the loop table? In that case, the above still holds, but a lot of my explanation may have been a bit unclear. Is this what you want?
Hi Wouter,
yes, I wanted (all) the phrases (1-6) to be read within the loop table (I couldn't explain it better, sry for that). Thank you very much for your answer. I'll test it as soon as possible but it seems to be the right way. I'll tell you if it worked.
Hi,
I tested it. Now the variables are finally shown in the variable inspector - so far so good, thank you very much for your help.
But unfortunately they still aren't printed out?
Hi Kiri,
What do you mean exactly by 'printed out'?
In Wouter's example experiment, the in-the-loop-item defined variable 'bla' is printed out, isn't it? This is done by the line
which he placed in the Run phase of his second inline_script item.
As a result, the value of 'bla', in this case "This is a phrase read in from a file", is printed to the debug window. To view the debug window, simply click the red ladybug item in the main toolbar.
The value of the variable 'bla' is also presented to the participant by the sketchpad item, where Wouter used the square-bracket method (
[bla]
) to display the phrase ("This is a ...") on screen.Is one of those two what you meant by 'printed out'?
I would advise you to walk through the step-by-step tutorial to familiarise yourself with OpenSesame:
Also, if you're planning to use Python inline coding for your experiments, walking through a Python tutorial might be a good idea as well:
Have a good evening!
Lotje
Did you like my answer? Feel free to
Hi Lotje,
I meant that the experiment starts, doesn't do anything, ends and shows the error message. Thanks for the links. I think they'll help me too.
Have a good weekend.
Kiri
Hi Kiri,
Does this happen when you run Wouter's experiment? Or your own script?
Are you sure you set your variable (say, 'sentence') like so:
exp.set("sentence', sentence)
and then refer to it in your loop item like so:
[sentence]
?And did you set your variable in the prepare phase tab of the inline_script item? (Otherwise the variable is indeed not yet available when the loop item is ran.)
If those suggestions don't solve your problem, perhaps you could upload your experiment here or copy your up-to-date inline_script, such that we could have a closer look. Also, could you please provide us with the error message as shown in the debug window? (To view the debug window, simply click the red ladybug icon in the main toolbar.)
Best,
Lotje
Did you like my answer? Feel free to
Hi Lotje,
I set the variables as described (with exp.set("abc", abc)) within the prepare phase tab of the inline_script item and within the loop item also as described. The variable inspector now shows the variables declared by me, but I can't use their content. I'll load up the error message on Friday (I'm using another PC at the moment).
Greetings,
Kiri
Hi,
as promised, here is the error message from Debug Window:
Additionally there's the message (which pops up) telling me that theres the
Error: Inline script error
In: inline_script (prepare phase)
Line: 17 (which is the line where I typed "print Phrase1" to test the variable)
Python traceback:
NameError: name 'Phrase1' is not defined.
I also tried to comment out this line, but it gives me the same error message with other lines. Except for the error message at the end, nothing is shown during the 'running' experiment.
Hope, that's what you wanted.
Greetings,
Kiri
Hi Kiri,
Are you trying to print the variables in a different inline_script than the inline_script where you defined them? (Note that even the Run phase tab of the same item is considered as a different inline_script.)
To make variables available in other inline_script items, you should make them global like so:
After doing so, you should be able to use the variables in another inline_script, for example to print them to the debug window:
So, the code where you define the variables should look something like this:
I uploaded an example experiment here:
This example should give you an idea of how to read sentences from a text file and use them for stimulus presentation in a loop item. To download the example experiment:
I hope this helps!
Best,
Lotje
Did you like my answer? Feel free to
Hi Lotje,
thank you very very much. The example experiment was very helpful. Now the experiment is doing what I wanted it to do.
Have a good weekend and Happy Easter.
Greetings,
Kiri
Hi Kiri,
I'm glad to hear! Feel free to post any further questions you may have.
A happy Easter to you too!
Lotje
Did you like my answer? Feel free to