WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   TypeError: only length-1 arrays can be converted to Python scalars (https://forum.worldviz.com/showthread.php?t=3980)

new_horizon 10-31-2011 09:54 AM

TypeError: only length-1 arrays can be converted to Python scalars
 
Hi All,

I will not paste my code here as it is extremely long, but I have included a snippet where the error is appearing.

TypeError: only length-1 arrays can be converted to Python scalars

Code:

# ----------------------------------------------#
#      MAIN LOOP ANIMATING THE OBJECTS          #
# ----------------------------------------------#

def animate():
        global rng,plgit, mlgit,prob_N, nv, trialsplt,nv_p,sgn,sgn_p,reverse, X_motion,Obs_rotn, L_inc,Min_loom,Obj_sz,Obj_scale, displaytime, Z_dist,Z_far, Speed
        global Vert_screen, Aspect, Z_observer, pixelcm, pixeldg,Lpixels,selectM, Dummycar, Pedestrian, condition, male, female, vehicle_endpos, pedestrian_endpos
        from calc_reversals import *
        stim=0
        falsep = 0
        falserate = 0
        nulltrials = 0
        nv[0] = 999; nv[1] = 999 ; R = 1  # Set up for 2FC
        trialtype = [0,1,1,1]  # use to specify ratio of dummy trials [0] to test trials
        # Also use to set movement to zero or 1*scaling
        Motion_conditions = [0,1]
        for tr in range(0, 2*(ntrials-1)):
                object1 = vehicles[tr]
                if Motion_conditions == 0:
                        object1 = vehicle0
                if Motion_conditions ==1:
                        object1 = vehicle1
                import random
                Condition = 0; Dummycar = 0; Pedestrian = 0
                random.shuffle(Motion_conditions)
                random.shuffle(trialtype)
                on_off = trialtype[1]
#### NEW NEW
                probe = Motion_conditions[0]
                # Check if that PEST has already completed reversals and switch to other one if it has
                if trial_ch[probe]< 0 :
                        print 'Series:',probe,'has completed, switching to:', Motion_conditions[1]
                        probe = Motion_conditions[1]
                local_motion = probe
#### END of NEW
                print 'Trial:',tr, 'Stimulus:', nv, 'set to:',probe*nv[probe]
                Loom_level = Min_loom+nv[probe]*L_inc
                Lpixels = 57.3*pixeldg*Loom_level
                if on_off == 1 and nv_p[probe] > nv[probe]:
                        Loom_level = Min_loom+nv_p[probe]*L_inc # use previous distance setting for null trials
                        if nv_p[probe] == 0:
                                Loom_level = Min_loom # if reached zero point set to 1 inc above
                Z_dist = Obj_sz/(Loom_level*TTC)
#                if selectc <2 :
#                        if Z_dist > Z_far:
#                                Z_dist = Z_far-5  # max setting for cars
#                if selectc == 2 :
#                        if Z_dist > 500:
#                                Z_dist = 500  # max setting for balls
                view.goto(xobs,yobs,Z_observer-zobs,0.01,viz.TIME)
#
        # Reset object position & scale
#                object1.setPosition(0,0,Z_dist-zobs)
                object1.setScale(Obj_scale[0],Obj_scale[1],1)
                Speed = Obj_sz/(Loom_level*TTC*TTC)
                screencms = 100*Obj_scale[0]*Z_observer/(Z_dist+Z_observer)
                Opixels = pixelcm*screencms
                KPH = 3600*Speed/1000
                Angular_size_start = math.atan(Obj_sz/Z_dist)*57.2957795
                theta_start = math.atan(Obj_sz/Z_dist)
                Angular_size_end = (theta_start + Loom_level*displaytime)*57.2957795
                Loom_level_degrees = Loom_level*57.2957795
#
                if local_motion ==1:
                        print 'LOCAL MOTION with on_off =',on_off
                if local_motion ==0:       
                        print 'STATIC DISTRACTION with on_off =',on_off
                #       
                if on_off > 0:
                        print 'Kph:', round(KPH,1),'m/s:',round(Speed,1), 'Dist:', round(Z_dist,1), 'Loom(deg):', round(57.3*Loom_level,4), 'Pixels:', round(Lpixels,1), 'Size Start', round(Angular_size_start,4), 'Size End', round(Angular_size_end,4)
                if on_off == 0:
                        print 'Dist:', round(Z_dist,1), 'Loom:', 0
                updatelabel(probe,nv[probe]*on_off,reverse[probe],falserate)
                yield viztask.waitMouseDown(viz.MOUSEBUTTON_LEFT)
                view.setEuler(0,0,0, 0.01, viz.TIME)
                yield viztask.waitTime(1.0) # wait before dummy motion begins
                #
                if local_motion ==1:
                        move_with_local_motion(on_off,Loom_level)
                if local_motion ==0:       
                        move_with_static_distractors(on_off,Loom_level)
                #       
                yield viztask.waitTime(0.3) # wait before removing
                vehicledummy1.visible(viz.OFF), object1.visible(viz.OFF), vehicledummy3.visible(viz.OFF), male.visible(viz.OFF), vehicledummy4.visible(viz.OFF)
                yield viztask.waitTime(0.5) # wait before prompt
                grid.visible(viz.ON)
                zone1button.visible(viz.ON)
                zone2button.visible(viz.ON)
                zone3button.visible(viz.ON)
                zone4button.visible(viz.ON)
                zone5button.visible(viz.ON)
                nonebutton.visible(viz.ON)
                yield viztask.waitAny( [ viztask.waitButtonDown(zone1button),viztask.waitButtonDown(zone2button),viztask.waitButtonDown(zone3button),viztask.waitButtonDown(zone4button),viztask.waitButtonDown(zone5button),viztask.waitButtonDown(nonebutton)])
                m_pos1 = viz.mouse.getPosition()
                yield viztask.waitAny( [ viztask.waitButtonDown(zone1button),viztask.waitButtonDown(zone2button),viztask.waitButtonDown(zone3button),viztask.waitButtonDown(zone4button),viztask.waitButtonDown(zone5button),viztask.waitButtonDown(nonebutton)])
                m_pos2 = viz.mouse.getPosition()
                yield viztask.waitAny( [ viztask.waitButtonDown(zone1button),viztask.waitButtonDown(zone2button),viztask.waitButtonDown(zone3button),viztask.waitButtonDown(zone4button),viztask.waitButtonDown(zone5button),viztask.waitButtonDown(nonebutton)])
                m_pos3 = viz.mouse.getPosition()
                print m_pos1, m_pos2, m_pos3
#                m_position = [m_pos1,m_pos2,m_pos3]
                mpos = 0
                if m_pos1[0] > 0.4 and m_pos1[0] < 0.6 and m_pos1[1] > 0.1:
                        mpos = 1
                if m_pos2[0] > 0.4 and m_pos2[0] < 0.6 and m_pos2[1] > 0.1:
                        mpos = 1
                if m_pos3[0] > 0.4 and m_pos3[0] < 0.6 and m_pos3[1] > 0.1:
                        mpos = 1
#                mousepress = viz.ask('Where were the objects that moved towards you?')
                grid.visible(viz.OFF)
                zone1button.visible(viz.OFF)
                zone2button.visible(viz.OFF)
                zone3button.visible(viz.OFF)
                zone4button.visible(viz.OFF)
                zone5button.visible(viz.OFF)
                nonebutton.visible(viz.OFF)
### MOVE THIS section to after
                if on_off == 1:
                        stim=nv[probe]
                        R = mpos
                        prob_calc_N(R,probe,rng,plgit, mlgit,prob_N, nv,dbrng) # should be using last valid nv[0] & R for next nv[0]
                        calc_reversals(probe,trialsprb, reverse, nv, nv_p, sgn_p, rng)
                        print 'Reversals: ', reverse[probe]
                        print '--NEXT--'
                if on_off == 0:
                        stim = 0
                        nulltrials = nulltrials + 1
                        if mpos == 1:
                                falsep = falsep+1
                        falserate = 100*falsep/nulltrials
                response1 = 0 # No Motion
                response2 = 0 # Car
                response3 = 0 # Pedestrian
                #  Zone 1 = x 0-0.2
                #  Zone 2 = x 0.2-0.4
                #  Zone 3 = x 0.4-0.6
                #  Zone 4 = x 0.6-0.8
                #  Zone 5 = x 0.8-1.0
                #  None Button = x 0.4-0.6 and y 0-0.1
                #  1 = No Motion; 2 = Car; 3 = Pedestrian
                NB = 0
                zone1 = 0
                zone2 = 0
                zone3 = 0
                zone4 = 0
                zone5 = 0
                if m_pos1[0] > 0.4 and m_pos1[0] < 0.6 and m_pos1[1] > 0.1:
                        zone3 = 1
                if m_pos2[0] > 0.4 and m_pos2[0] < 0.6 and m_pos2[1] > 0.1:
                        zone3 = 1
                if m_pos3[0] > 0.4 and m_pos3[0] < 0.6 and m_pos3[1] > 0.1:
                        zone3 = 1
                if m_pos1[0] > 0.4 and m_pos1[0] < 0.6 and m_pos1[1] < 0.1:
                        NB = 1
                if m_pos2[0] > 0.4 and m_pos2[0] < 0.6 and m_pos2[1] < 0.1:
                        NB = 1
                if m_pos3[0] > 0.4 and m_pos3[0] < 0.6 and m_pos3[1] < 0.1:
                        NB = 1
                if m_pos1[0] > 0.2 and m_pos1[0] < 0.4:
                        zone2 = 1
                if m_pos2[0] > 0.2 and m_pos2[0] < 0.4:
                        zone2 = 1
                if m_pos3[0] > 0.2 and m_pos3[0] < 0.4:
                        zone2 = 1
                if m_pos1[0] > 0.6 and m_pos1[0] < 0.8:
                        zone4 = 1
                if m_pos2[0] > 0.6 and m_pos2[0] < 0.8:
                        zone4 = 1
                if m_pos3[0] > 0.6 and m_pos3[0] < 0.8:
                        zone4 = 1
                if m_pos1[0] > 0.0 and m_pos3[0] < 0.2:
                        zone1 = 1
                if m_pos2[0] > 0.0 and m_pos3[0] < 0.2:
                        zone1 = 1
                if m_pos3[0] > 0.0 and m_pos3[0] < 0.2:
                        zone1 = 1
                if m_pos1[0] > 0.8 and m_pos3[0] < 1.0:
                        zone5 = 1
                if m_pos2[0] > 0.8 and m_pos3[0] < 1.0:
                        zone5 = 1
                if m_pos3[0] > 0.8 and m_pos3[0] < 1.0:
                        zone5 = 1
                print 'Trial:',tr,'Reversals: ', reverse[probe], 'False pos rate:', falserate, 'Probe:', probe, 'Mpos:', mpos, 'Condition', Condition, 'DummyCar', Dummycar, 'Pedestrian', Pedestrian, 'Zone1', zone1, 'Zone2', zone2, 'Zone3', zone3, 'Zone4', zone4, 'Zone5', zone5, 'NoneButton', NB
                print '--NEXT--'
        #                yield viztask.ButtonDown(nonebutton)
                res[tr,0] = tr; res[tr,1]= stim; res[tr,2] = mpos; res[tr,3] = reverse[probe]
                SaveData(tr, stim, local_motion, reverse[probe], on_off, mpos, probe, Dummycar, vehicle_endpos, Pedestrian, pedestrian_endpos, zone1, zone2, zone3, zone4, zone5, NB, falserate,Loom_level,Loom_level_degrees, Lpixels,KPH,Speed, Z_dist,Z_observer,Angular_size_start, Angular_size_end )
                updatelabel(probe,stim,reverse,falserate)
#########  NEW NEW NEW ################
        #  THIS CHECKS IF MAX REVERSALS HAVE BEEN REACHED FOR ANY SERIES
                if reverse[probe]>maxreversals:
                        trial_ch[probe] = -1
                        # this will allow all future selections of this series to be skipped
                        ###  NOTE ADD IN AN ADDITIONAL SAVE OF VARIABLES at end of PEST to save the NEXT nv variable = MLE
                        SaveData(tr, nv[probe], local_motion, reverse[probe], on_off, mpos, probe, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, falserate,999,999, 999,999,999, 999,999,999,999 )
                checktotal=sum(trial_ch)
                print checktotal
                # This checks if the max has been reached for all series
                if checktotal <= -Npest :
                        break       
#########  NEW NEW NEW ################

# ----------------------------------------------#
#      END OF THE PEST PROCEDURE                #
# ----------------------------------------------#

Can anyone provide any advice as to what "TypeError: only length-1 arrays can be converted to Python scalars" is referring to?

Best wishes,

Mark


All times are GMT -7. The time now is 07:06 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC