[solved] Error... TypeError: unsupported operand type(s) for /: 'str' and 'int'
Hello all!
Having trouble (as usual) with my latest endeavour into Python. I got the following error when I ran the experiment:
experiment.run(): experiment started at Thu Aug 29 17:17:37 2013
Traceback (most recent call last):
File "dist\libopensesame\inline_script.py", line 130, in prepare
File "<string>", line 8, in <module>
File "dist\openexp\_canvas\xpyriment.py", line 237, in polygon
TypeError: unsupported operand type(s) for /: 'str' and 'int'
Traceback (most recent call last):
File "dist\libqtopensesame\qtopensesame.py", line 1393, in run_experiment
File "dist\libopensesame\experiment.py", line 287, in run
File "dist\libopensesame\sequence.py", line 55, in run
File "dist\libopensesame\loop.py", line 147, in run
File "dist\libopensesame\sequence.py", line 55, in run
File "dist\libopensesame\loop.py", line 146, in run
File "dist\libopensesame\sequence.py", line 107, in prepare
File "dist\libopensesame\inline_script.py", line 132, in prepare
inline_error: Error: Inline script error
In: ongoing_script (prepare phase)
File "dist\openexp\_canvas\xpyriment.py", line 237, in polygon
Python traceback:
TypeError: unsupported operand type(s) for /: 'str' and 'int'
Full traceback in debug window
Sequence is supposed to generate 2 random shapes based on .csv files containing 'valid' coordinates.
Overview snapshot (block_loop is set to 50, sequential):
'read_coordinate_files' script:
# Specify paths to files containing valid coordinate points using built-in OS function exp.get_file()
path_points1 = exp.get_file("image1_points.csv")
path_points2 = exp.get_file("image2_points.csv")
# Import numpy to read coordinate .csv files
import numpy as np
# ... and load the files:
image1_points = np.loadtxt(path_points1, dtype = str)
image2_points = np.loadtxt(path_points2, dtype = str)
# Make the two global for future use:
global image1_points, image2_points
'ongoing_task' script:
# Import built-in OS module random and shuffle 'image1_points' and 'image2_points'...
import random
random.shuffle(image1_points)
random.shuffle(image2_points)
# Define a list of numerical values representing an 'n' sided-shape
n = [8,6,4,3]
# Take a random value from 'n' (code found in cogsci.nl thread http://forum.cogsci.nl/index.php?p=/
# discussion/325/solved-question-randomization-on-one-page-using-forms/p1):
random.shuffle(n) # Shuffle the list order
while len(n) > 0: # While the list is not empty
nss = n.pop() # 'Pop' the first item
# ... each group of 'image_points' totals 24 points, so divide by value of 'nss'
# and take a random sample to generate various 3, 4, 6 and 8 sided shapes:
sample_image1 = random.sample(image1_points, len(image1_points)/nss)
sample_image2 = random.sample(image2_points, len(image2_points)/nss)
# Make items global for future use:
global sample_image1, sample_image2
'ongoing_script' script:
# Import built-in OS module canvas for the polygon function
from openexp.canvas import canvas
trial_display = canvas(exp)
# Use 'sample_image1' and 'sample_image2' with polygon function
# to create 'shape1' and 'shape2':
shape1 = trial_display.polygon(sample_image1)
shape2 = trial_display.polygon(sample_image2)
# Make the variables 'shape1' and 'shape2'
# available in the GUI (and therefore the sketchpad item):
exp.set("shape1", shape1)
exp.set("shape2", shape2)
I feel I'm getting better at this Python lark, but apparently still falling short!
Once I have the shape generation working the intention is to make 'shape2': (a) rotate 90 degrees for all 50 pairs and (b) match for 50% of pairs and not match for 50% of pairs in a session. Tricky... any directional hints are welcome!
Thanks all in advance...
Comments
Hi Lee,
I suspect that the problem is that you are loading the coordinate points as
str
objects:This will cause
canvas.polygon()
to crash, because it expects numeric values. Simply changing thedtype
tofloat
should fix the problem, provided that the data files do indeed contain numeric values.Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Sebastiaan,
Oh dear, embarrassingly obvious! I've changed the dtype to 'int' as they are all whole values, but not sure how to account for -/+ values; having chosen 24 points either side of the y-axis to create a 'mirror image' it would make quite a difference in the location of the generated shape if there were no orienting signs...
Thanks again!
Lee
Hi,
With regards to the above comment, still trying to figure out how to make inline scripts differentiate between negative and positive integers (on a canvas object in both x and y axis). I have tried various configurations of code, but I always come up with an error of some sort... have gone back to the drawing board on this one (no pun intended).
So:
x-values are: 96, 160, 224, 288, 352, 416 and
y-values are: 32, 96
(add negative signs as appropriate to create symmetrical areas relative to the y-axis)
The aim is to create a shape on the LHS of the canvas (evenly distributed above/below the x-axis) and a corresponding shape on the RHS of the canvas.
There needs to be 50 iterations in one trial; 25 where the shapes match (but the RHS one is inverted 180 degrees) and 25 where the shapes do not match.
Shapes will be 'n-sided' where n = 4,6,8, or 12 (there are 24 possible points either side of the y-axis) - shapes do not have to be regular ones and lines may cross, the emphasis is on generating 50 random images.
My initial thoughts were to use the x/y points in independent lists and randomly draw 'n' coordinates out and combine to make respective shapes. Unfortunately I came unstuck as I can't figure out how to add the negative signs in the 'right' places. The 180 degree inversion isn't even under contemplation at the moment!
Any thoughts would be greatly appreciated, it seems a tricky problem to me but a more experienced eye may be needed...
Thanks in advance!
Lee
Hi Lee,
Ok, so the problem is simply that you want to read negative numbers from a text file, is that correct? This is straightforward with the function
np.loadtxt()
. Let's say that you have a file calledinput.txt
, which looks like this:Then you can read and print the file like this:
Does that help at all or did I misunderstand the problem?
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Sebastiaan,
That's exactly what I've done, something is still amiss...
pastebin here
Coordinate files on Dropbox:
image1_values here
image2 values here
I can see what the error is referring to in the debug, but I'm not sure why as I can't se what's wrong with the script!
Thanks again,
Lee
Hi Lee,
Right, I see. The problem is not really related to negative numbers, but to the way that you shuffle the lists. Basically,
random.shuffle()
shuffles a list (or numpy array in your case) in place. This means that the list that you pass as an argument is changed itself, and the function does not provide a return value. In other words,random.shuffle()
always returnsNone
, which is why your script doesn't work as desired:The script above will not make
image1
andimage2
be shuffled copies orimage1_points
andimage2_points
, but simply assign the valueNone
to them, while the original lists are shuffled. Does that make sense? This causes problems later on in the experiment, when you try to determinelen(image1)
.So what you should do is for example:
See also:
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Sebastiaan,
I've changed the script as per your suggestions and the experiment now runs... however the sketchpad merely presents 'none' 'none' instead of generating random shapes as I thought it would.
I presume there is some problem with my logic somewhere, which I am looking into but a fresh eye is always welcome if you have the time.
Thanks as always!
Lee
You are trying to use a text element in a
sketchpad
to show a polygon that you have created in aninline_script
!So what you should do instead is construct and show the
canvas
entirely using a inline scripting. You cannot combine inline scripting with asketchpad
item in the way that you're trying to do now. Does that make sense?See the
canvas
documentation for many examples:Check out SigmundAI.eu for our OpenSesame AI assistant!
Sebastiaan,
Thanks - I had thought that the canvas/sketchpad were somehow linked, misinterpretation on my part.
Let's see if I can sort it out now.,...
Lee
I have SO sorted it, woohoo! Also just remembered that programming graphics work from 'upside down' axes so once I had transposed all the coordinates (not needing negatives after all) the generating images just popped onto screen...
It is probable that I will bother you less and less as I get more confident in my [really slowly] growing programming ability, but at the moment a second opinion is often useful.
Thanks!
Lee
Never celebrate too soon... Having a little issue organising matching pairs in the script.
(sorry for the giant script copy!):
The point that I can't work is the pairing of points between the 'matching' shapes:
I would then need to have 25 matching occurrences and 25 non-matching occurrences in a randomised block of 50... I've set the block loop to 50 cycles, but I'm not sure on how to utilise that as a value (i.e. to tell the script that cycles\2 = m_img2 and cycles\2 = nm_img2... or similar).
I'm going to keep trying different things as I think of them, but I figured an internets-worth of heads is better than one!
Thanks,
Lee
Hi Lee,
About the matching part, does something like this help?
Best wishes,
Lotje
Did you like my answer? Feel free to
![:) :)](https://forum.cogsci.nl/plugins/emojiextender/emoji/twitter/smile.png)
Lotje,
Thanks for the reply! It sort of helps, gives me pause for thought, but if I'm drawing random 3,4,6 and 8 sided shapes ('nss') then the script has to be made to figure out 'l1' all by its lonesome before comparing to 'l2'.
Would it be possible, given that I've already shuffled and defined 'points1' as variable 'img1':
img1 = random.sample(points1, len(points1)/nss)
to index 'img1' (and thereby 'points1')? I just want be indexing each sample and then reproducing those positions from 'points2'.
Thanks,
Lee
P.S. where > m_img2 = img1 ... 'img1' has no function I just included it so I could test the rest of the script.
I've just seen the flaw in my own logic... If I could index 'img1' for the sampled values of 'points1', they would be part of a new list (at least I think they would) and an index of 'points2' would not return the corresponding points correctly...
Back to the start!
...I gave it a go anyway:
Returned an error saying: ValueError: 24 is not in list There are 24 points per list so the number has some significance, but not being familiar with indexing function, I don't know why its told me off!
Thanks,
Lee
The method
index()
gives the position (starting from 0) of an element from that list. For exampleIn your case you are passing a number (i.e. 24), to
index()
, which gives an error because this number is not present in the list. After all,points1
is a list of (x,y) tuples, not a list of numbers. So that explains the error. But I'm not sure how to fix it, because it's not clear to me what the script is supposed to do?Check out SigmundAI.eu for our OpenSesame AI assistant!
Okay that makes sense.
The script is supposed to generate two images side by side. 50 presentations is ensured by having the block loop set at 50 cycles. 25 pairs of images have to be non-matching; easy enough, just pull necessary numbers of random points independently for image1 and image2.
The other 25 pairs have to be matching which is where I'm stuck. image1 can remain random, but the script must recognise image1's points and reproduce those points for image2.
Making image2 work is the problem.
Thanks,
Lee
Ah right, I think I see what you're trying to do. Basically, you could solve the problem as follows:
points1
andpoints2
. This is just a list that runs from 0 (the first index) to the length ofpoints1
- 1 (the last index).points1
andpoints2
.This idea is demonstrated in the script below (using
points1
andpoints2
posted above), which you should be able to adapt to fit into your experiment.Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Thanks! It worked in terms of creating matching pairs of polygons... One question:
Can I create a variable that reflects the number of cycles of my block_loop? I'm trying to make the 50 cycles be a combination of 25 presentations of matching:non-matching. I had thought of using something like:
... but I can't see how to use this to establish a 50:50 split of match:non-match, with random appearance to the participant...
Confused,
Lee
If I understand correctly, you could simply add a variable called 'match' to the
block_loop
and set this to 'yes' for half the trials and to 'no' for the rest of the trials. Then you use this variable in theinline_script
to check whether you want the polygons to match or not:Would that solve the problem?
Check out SigmundAI.eu for our OpenSesame AI assistant!
It was helpful for the adaptation below:
I just added 25xy and 25xn as a list 'match', to a Python shell and shuffled-printed repeatedly until I had one per session - logic suddenly kicked in and told me it doesn't have to be truly random, just appear random....
Solved... Thanks!
Lee
Great to hear!![:) :)](https://forum.cogsci.nl/plugins/emojiextender/emoji/twitter/smile.png)
Did you like my answer? Feel free to
![:) :)](https://forum.cogsci.nl/plugins/emojiextender/emoji/twitter/smile.png)