Here is my code:
from psychopy import visual, event, gui, data, core
import random, os
from random import shuffle
from PIL import Image
import glob
a = glob.glob("DDtest/targetimagelist1/*")
b = glob.glob("DDtest/distractorimagelist1/*")
c = glob.glob("DDtest/targetimagelist2/*")
d = glob.glob("DDtest/distractorimagelist3/*")
e = glob.glob("DDtest/targetimagelist4/*")
shuffle(c)
shuffle(d)
ac = a + c
bd = b + d
indices = random.sample(range(len(ac)),len(ac))
ac = list(map(ac.__getitem__, indices))
bd = list(map(bd.__getitem__, indices))
ace = ac+e
shuffle(ace)
target = ac
distractor = bd
recognition = ace
def studyphase():
loc = [1, 2]
location = random.choice(loc)
if location == 1:
pos1 = [-.05,-.05]
pos2 = [.05, .05]
else:
pos1 = [.05, .05]
pos2 = [-.05, -.05]
win = visual.Window(size=(1920, 1080), fullscr=True, screen=0, monitor='testMonitor', color=[-1,-1,-1])
distractorstim = visual.ImageStim(win=win, pos=pos1, size=[0.5,0.5])
distractorstim.autoDraw = True
targetstim = visual.ImageStim(win=win, pos=pos2, size=[0.5,0.5])
targetstim.autoDraw = True
targetstim.image = target[i]
distractorstim.image = distractor[i]
win.flip()
core.wait(.1)
def testphase():
win = visual.Window(size=(1920, 1080 ), fullscr=True, screen=0, monitor='testMonitor', color=[-1,-1,-1])
recognitionstim = visual.ImageStim(win=win, pos=[0,0], size=[0.5,0.5])
recognitionstim.autoDraw = True
recognitionstim.image = recognition[k]
old = visual.TextStim(win,text='OLD',pos=[-0.5,-0.5],font='Lucida Console')
new = visual.TextStim(win,text='NEW', pos=[0.5,-0.5],font='Lucida Console')
old.draw()
new.draw()
win.flip()
core.wait(.1)
for i in range(len(ac)):
studyphase()
for k in range(len(ace)):
testphase()
What this is supposed to do is take a bunch of pictures and display them in two different phases (study and test), however, when I run this code the program crashes about half way through the second loop, and I get the following error message:
python(55762,0xa0afe1a8) malloc: *** mach_vm_map(size=8388608) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Fatal Python error: (pygame parachute) Bus Error
However, if I run either the study loop or the test loop independently they run fine. Anyone know what might be causing this error? Any help will be greatly appreciated. :)
Edit: so apparently, if I move the win command outside the loop, it works.
This issue was raised on the psychopy-users list a few years ago. It is quite likely caused by the images being too large (in pixels, not megabyte). So a solution would be to downscale them to approximately the resolution that you're going to display them, if possible. I found this by googling the error message.
You generate a new window and several new stimuli on every trial/presentation, since they are initiated within the functions and the functions are called in every iteration of the loop(s). Please see my answer to your earlier question for a strategy to create window/stimuli first and then update the properties that needs to change. This may even solve the problem on its own since creating new stimuli may fill up memory.
Related
I’m working on a project in micropython using an openMV camera and blob detection to determine the orientation of an object. My problem is when the check is executed, I get an error “ArilY is not defined”, because the object isn’t in the camera view yet (moving on conveyer). How can I implement a path in my code to not execute the check and just print that there is no object instead, then begin the loop again and check for the object? I have tried to implement a break with if else but can't seem to get the code right.
'''
import sensor, image, time, math
from pyb import UART
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000) # Wait for settings take effect.
#sensor.set_auto_gain(False) # must be turned off for color tracking
#sensor.set_auto_whitebal(False) # must be turned off for color tracking
threshold_seed = (7,24,-8,4,-3,9)
threshold_aril = (33,76,-14,6,17,69)
threshold_raphe = (36,45,28,43,17,34)
thresholds = [threshold_seed,threshold_aril,threshold_raphe]
clock = time.clock() # Create a clock object to track the FPS.
uart = UART(3, 9600)
arilY = None
seedY = None
def func_pass():
result = "Pass"
print(result)
print("%d\n"%aril.cx(), end='')
uart.write(result)
uart.write("%d\n"%aril.cx())
#these two functions print info to serial monitor and send
def func_fail():
result = "Fail"
print(result)
print("%d\n"%aril.cx(), end='')
uart.write(result)
uart.write("%d\n"%aril.cx())
def func_orientation(seedY, arilY):
if (seedY and arilY):
check = 0
check = (seedY - arilY)
if
func_pass()
else:
func_fail()
while(True): #draw 3 blobs for each fruit
clock.tick()
img = sensor.snapshot()
for seed in img.find_blobs([threshold_seed], pixels_threshold=200, area_threshold=200, merge=True):
img.draw_rectangle(seed[0:4])
img.draw_cross(seed.cx(), seed.cy())
img.draw_string(seed.x()+2,seed.y()+2,"seed")
seedY = seed.cy()
for aril in img.find_blobs([threshold_aril],pixels_threshold=300,area_threshold=300, merge=True):
img.draw_rectangle(aril[0:4])
img.draw_cross(aril.cx(),aril.cy())
img.draw_string(aril.x()+2,aril.y()+2,"aril")
arilY = aril.cy()
for raphe in img.find_blobs([threshold_raphe],pixels_threshold=300,area_threshold=300, merge=True):
img.draw_rectangle(raphe[0:4])
img.draw_cross(raphe.cx(),raphe.cy())
img.draw_string(raphe.x()+2,raphe.y()+2,"raphe")
rapheY = raphe.cy()
func_orientation(seedY, arilY);
Something you could do is preemptively define arilY and SeedY as None before the while loop, then enclose the check in a if(arilY and seedY):
if you want to avoid using None, you could have an additional boolean that you set to true when arilY is detected, then enclose the check in a test for this boolean
But the bigger question here, is why your allocations are in the inner loop? You always redefine seedY and arilY for each iteration of the loop, which mean it will always be equal to seed.y of the last seed in the list, meaning all allocations prior to the last one were useless.
If you move the allocations outside the loop, there shouldn't be a problem.
EDIT: The error does not lie with multiprocessing but rather with another library. I would close the question but #Lukasz Tracewski's point of using joblib may help someone else.
I've got an issue with a task I'm trying to parallelise on windows.
It seems to work for a while then suddenly halts on a particular instance (I can tell as I get the code to print which iteration it's on). I've noticed in the task manager that the CPU usage, which such be about 50%, is minimal for the Python processes. When I then do a keyboard interrupt on the cmd prompt, this suddenly shoots forward a number of instances and the activity goes back up for a short while, only to go back down again.
I've included the bits of my code which I think are relevant. I know it can work (I don't think it's stuck on a problem) and there seems to be a degree of randomness as to when it does freeze.
I'm using the COBYLA solver with max_iter set. I'm not sure if it is relevant, but when I tried to use BFGS, I got a freezing problem.
def get_optimal_angles(G,p,coeff,objective_function,initial_gamma_range,initial_beta_range,seed):
'''
This performs the classical-quantum interchange, improving the values of beta and gamma by reducing the value of
< beta, gamma | C | beta, gamma >. Returns the best angles found and the objective value this refers to. Bounds on the minimiser are set as the starting points
'''
initial_starting_points = random_intial_values((np.array(initial_gamma_range)),(np.array(initial_beta_range)),p,seed)
optimiser_function = minimize(objective_function, initial_starting_points, method='COBYLA', options={'maxiter':1500})
best_angles = optimiser_function.x
objective_value = optimiser_function.fun
return best_angles,objective_value
def find_best_angles_for_graph_6(graph_seed):
print("6:On graph",graph_seed)
#graph = gp.unweighted_erdos_graph(10,0.4,graph_seed)
graph = gp.unweighted_erdos_graph(6,0.4,graph_seed)
graph_coefficients = quantum_operator_z_coefficients(graph,'Yo')
exact_energy =get_exact_energy(graph)
angle_starting_seed = np.arange(1,number_of_angle_points,1)
objective_function= get_black_box_objective_sv(graph,p,graph_coefficients)
list_of_results = []
for angle_seed in angle_starting_seed:
print("On Angle Seed",angle_seed)
best_angles_for_seed, best_objective_value_for_seed = get_optimal_angles(graph,p,graph_coefficients,objective_function,[0,np.pi],[0,np.pi],angle_seed)
success_prob = calculate_energy_success_prob(graph,p,exact_energy, graph_coefficients,best_angles_for_seed,angle_seed)
angle_seed_data_list = [angle_seed,best_objective_value_for_seed,success_prob,best_angles_for_seed]
list_of_results.append(angle_seed_data_list)
list_of_best = get_best_results(list_of_results)
full_results = [graph_seed,exact_energy,list_of_best,list_of_results]
return full_results
import multiprocessing as mp
def main():
physical_cores = 5
pool = mp.Pool(physical_cores)
list_of_results_every_graph_6 = []
list_of_all_graph_results_6 = pool.map(find_best_angles_for_graph_6,range(1,number_of_graphs+1))
print(list_of_all_graph_results_6)
file_name_6 = 'unweighted_erdos_graph_N_6_p_8.pkl'
pickle_6 = open((file_name_6),'wb')
pickle.dump(list_of_all_graph_results_6, pickle_6)
pickle_6.close()
list_of_results_every_graph_10 = []
list_of_all_graph_results_10 = pool.map(find_best_angles_for_graph_10,range(1,number_of_graphs+1))
print(list_of_all_graph_results_10)
file_name_10 = 'unweighted_erdos_graph_N_9_p_8.pkl'
pickle_10 = open((file_name_10),'wb')
pickle.dump(list_of_all_graph_results_10, pickle_10)
pickle_10.close()
if __name__ == "__main__":
main()
EDIT: Here is the full code as a Jupyter notebook. https://www.dropbox.com/sh/6xb7setjsn1c1o3/AAANfH7mEmhuuf9cxh5QWsRQa?dl=0
I am trying to use this Anki add-on written in python.
However, on Anki 2.1 startup, an error is returned.
May anyone have a quick look at the short code in the link and spot the error? Maybe it is incompatible with a recent update of python?
# Anki 2.0 addon
# Author EJS
# https://eshapard.github.io/
#
# Sets the learning steps sequence of each deck options group.
from anki.hooks import addHook
from aqt import mw
#from aqt.utils import showInfo
#import time
ignoreList = ['Default', 'OnHold', 'Parent Category'] #Deck options groups to ignore
# run this on profile load
def updateLearningSteps():
#find all deck option groups
dconf = mw.col.decks.dconf
#cycle through them one by one
for k in dconf:
if dconf[k]["name"] not in ignoreList:
#showInfo(dconf[k]["name"])
ease = dconf[k]["new"]["initialFactor"]/1000.0
#create learning steps
tempList = [15]
for i in range(10):
l = int(1440*ease**i)
if l < 28800:
tempList.append(l)
else:
gradInts = [(l/1440),(l/1440)]
break
#showInfo(str(tempList))
#showInfo(str(gradInts))
mw.col.decks.dconf[k]["new"]["delays"] = tempList
mw.col.decks.dconf[k]["new"]["ints"] = gradInts
mw.col.decks.save(mw.col.decks.dconf[k])
mw.res
et()
# add hook to 'profileLoaded'
addHook("profileLoaded", updateLearningSteps)
This is the error: https://user-images.githubusercontent.com/52420923/66923073-ba2df980-f017-11e9-8b66-c8799db29850.png
The code you've posted doesn't match the error. The error has def updateLearningSteps() without a colon at the end of the line. That is indeed a syntax error.
I have the following code:
import cv2
import numpy as np
image = cv2.imread('pic1.png', cv2.IMREAD_GRAYSCALE)
height = 0
count = 0
it = np.nditer(image)
for(x) in it:
count += 1
if count == 80:
count = 0
height += 1
if x > 400:
print("Height is: " + height)
break
When I try to run the code I get the following error message:
TypeError: Iterator operand or requested dtype holds references, but the REFS_OK flag was not enabled
Why do I get this error? When I tried looking it up it seems like people just work around it instead of fixing it.
Check that the returned image variable isn't None.
Perhaps the image is not in the path your script is run from.
OpenCV doesn't raise an exception when it can't read/load the image, but, rather, returns None, in which case weird exceptions you will meet, when you try to operate on that None... like the exception posted.
(Sorry for speaking like Yoda... :-) )
I have written a function that does some group averaging on a data set. When I call this function it runs, returns the data which is then plotted. During this call I get one warning:
warnings.warn("Warning: converting a masked element to nan.")
which appears not to affect the function call. However, after the function returns, something is happening that locks up the console in which python is running (python 2.7, spyder 2.3.5.2, windows 7). There is no error trace at the time of lock-up, but when I recover from this lock up I get the error trace given below. This "index error" seems to be occurring in the python core and I don't see how to trace it back to my code. Can anyone suggest how to determine the source of this error.
A bit more information on the process under which this error occurs:
When I first start Spyder I run my main calling module in the python console. The module completes correctly and the console returns to the command prompt. If I then click on the variable explorer the IDE locks up and further clicks cause the IDE to grey-out (other actions may have the same effect). I then try to close the IDE which prompts with the options to recover - which I do. The IDE recovers and that is when I get the "Index error" trace in the python console. I can then kill the console from the IDE (yellow triangle) and restart the console. If I then run the calling module again, it runs correctly (i.e. all outputs) but does not return to the command prompt in the restarted python console. Any clicks on the IDE then cause it to grey-out and I need to close the IDE to continue.
>>> Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\monitor.py", line 569, in run
self.update_remote_view()
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\monitor.py", line 450, in update_remote_view
remote_view = make_remote_view(ns, settings, more_excluded_names)
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\monitor.py", line 79, in make_remote_view
minmax=settings['minmax'])
File "C:\Python27\lib\site-packages\spyderlib\widgets\dicteditorutils.py", line 202, in value_to_display
value = repr(value)
File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 3667, in __repr__
data=str(self), mask=str(self._mask),
File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 3647, in __str__
res.view(ndarray)[m] = f
IndexError: too many indices for array
The code that is called when this error occurs is:
def GroupSpectra(spectral, frequency, u, zmd, grp, covar=[1], prenorm=False):
# expand non spectral inputs so they are samwe shape as spectral
frequency = expand(frequency,spectral)
u = expand(u,spectral)
zmd = expand(zmd,spectral)
grp = expand(grp,spectral)
covar = expand(covar,spectral)
scnt = expand(np.array([1]),spectral)
# calc normalized freq
nfreq = frequency*zmd/u
# create frequency bins (freq by grps)
grps = np.unique(grp)
igrps = grps.size
binfreq = np.power(10,np.arange(-5,2,0.1))
iflen = binfreq.size
binfreq = np.tile(binfreq,(igrps,1))
binSpecSum = np.zeros(binfreq.shape)
binSpecSS = np.zeros(binfreq.shape)
binCount = np.ones(binfreq.shape)
binCoVar = np.zeros(binfreq.shape)
SpecAvg = np.zeros(binfreq.shape)
CovAvg = np.zeros(binfreq.shape)
SpecStd = np.zeros(binfreq.shape)
# pre normalize powers ??
if prenorm == True:
spectral = spectral/covar
# put powers in bins
ig = 0
for ig in np.arange(igrps):
idg = grp == grps[ig]
for ix in np.arange(0,iflen-1):
flow = binfreq[0,ix]
fhigh = binfreq[0,ix+1]
idf = ((nfreq >= flow) & (nfreq < fhigh))
idfg= idg & idf
binCount[ig,ix] = np.nansum(scnt[idfg])
binSpecSum[ig,ix] = np.nansum(spectral[idfg])
binSpecSS[ig,ix] = np.nansum(np.power(spectral[idfg],2.0))
binCoVar[ig,ix] = np.nansum(covar[idfg])
# avg spectra
idb = binCount > 0.5
SpecAvg[idb] = np.divide(binSpecSum[idb],binCount[idb])
FreqAvg = binfreq
SpecStd[idb] = np.sqrt(np.divide(binSpecSS[idb],binCount[idb]) - np.square(SpecAvg[idb]) )
CovAvg[idb] = np.divide(binCoVar[idb],binCount[idb] )
# pre normalize powers ??
if prenorm == False:
ida = CovAvg != 0.0
idb = np.isfinite(CovAvg)
idx = ida & idb
SpecAvg[idx] = np.divide(SpecAvg[idx],CovAvg[idx])
# SpecStd = SpecStd/CovAvg
print(FreqAvg.shape)
print(SpecAvg.shape)
print(SpecStd.shape)
return (FreqAvg,SpecAvg, SpecStd)
it took a while to track this down, but changing the following code resulted in the Spyder IDE not locking up on completion of the calling functions. The code was part of a function being used to resize some arrays.
The original two lines of code:
newsmall = np.tile(small, (colb,1))
newsmall = np.transpose(newsmall)
this code as changed to:
newsmall = np.resize(small,(rowb,1))
newsmall = np.tile(newsmall, (1,colb))
The array small had the dimensions (rowb,) before these two lines