Not passing python unit tests - python

Below is my tester file test_a1_partc.py
import unittest
from a1_partc import DisjointSet
class A1_PartCTestCase(unittest.TestCase):
"""These are the test cases for functions and classes of a1_partb"""
def test_Init_Len_NumSets(self):
the_set=DisjointSet()
self.assertEqual(len(the_set),0)
self.assertEqual(the_set.get_num_sets(), 0)
def test_Make_Set(self):
the_set = DisjointSet()
rc1 = the_set.make_set("cat")
self.assertEqual(len(the_set),1)
self.assertEqual(the_set.get_num_sets(), 1)
self.assertEqual(rc1, True)
rc2 = the_set.make_set("dog")
self.assertEqual(len(the_set),2)
self.assertEqual(the_set.get_num_sets(), 2)
self.assertEqual(rc2, True)
rc3 = the_set.make_set("fish")
self.assertEqual(len(the_set),3)
self.assertEqual(the_set.get_num_sets(), 3)
self.assertEqual(rc3, True)
rc1 = the_set.make_set("cat")
self.assertEqual(len(the_set),3)
self.assertEqual(the_set.get_num_sets(), 3)
self.assertEqual(rc1, False)
rc2 = the_set.make_set("dog")
self.assertEqual(len(the_set),3)
self.assertEqual(the_set.get_num_sets(), 3)
self.assertEqual(rc2, False)
rc3 = the_set.make_set("fish")
self.assertEqual(len(the_set),3)
self.assertEqual(the_set.get_num_sets(), 3)
self.assertEqual(rc3, False)
def test_Find_Set(self):
the_set = DisjointSet()
the_set.make_set("cat")
the_set.make_set("dog")
the_set.make_set("fish")
self.assertEqual(the_set.find_set("cat"), "cat")
self.assertEqual(the_set.find_set("dog"), "dog")
self.assertEqual(the_set.find_set("fish"), "fish")
def test_Get_Set_Size(self):
the_set = DisjointSet()
the_set.make_set("cat")
the_set.make_set("dog")
the_set.make_set("fish")
self.assertEqual(the_set.get_set_size("cat"), 1)
self.assertEqual(the_set.get_set_size("dog"), 1)
self.assertEqual(the_set.get_set_size("fish"),1)
self.assertEqual(the_set.get_set_size("hamster"), 0)
def test_Union_Set(self):
the_set = DisjointSet()
the_set.make_set("cat")
the_set.make_set("dog")
the_set.make_set("fish")
rc = the_set.union_set("cat","dog")
self.assertEqual(rc,True)
self.assertEqual(the_set.find_set("cat"), the_set.find_set("dog"))
self.assertNotEqual(the_set.find_set("fish"), the_set.find_set("dog"))
self.assertNotEqual(the_set.find_set("fish"), the_set.find_set("cat"))
self.assertEqual(len(the_set), 3)
self.assertEqual(the_set.get_num_sets(), 2)
self.assertEqual(the_set.get_set_size("cat"), 2)
self.assertEqual(the_set.get_set_size("dog"), 2)
self.assertEqual(the_set.get_set_size("fish"), 1)
the_set.make_set("hamster")
the_set.make_set("turtle")
the_set.make_set("frog")
the_set.make_set("gerbil")
self.assertEqual(len(the_set), 7)
self.assertEqual(the_set.get_num_sets(), 6)
rc = the_set.union_set("turtle","frog")
self.assertEqual(rc,True)
self.assertEqual(len(the_set), 7)
self.assertEqual(the_set.get_num_sets(), 5)
self.assertEqual(the_set.find_set("turtle"), the_set.find_set("frog"))
self.assertNotEqual(the_set.find_set("hamster"), the_set.find_set("frog"))
self.assertNotEqual(the_set.find_set("dog"), the_set.find_set("turtle"))
self.assertNotEqual(the_set.find_set("gerbil"), the_set.find_set("turtle"))
self.assertNotEqual(the_set.find_set("cat"), the_set.find_set("frog"))
self.assertNotEqual(the_set.find_set("fish"), the_set.find_set("frog"))
self.assertEqual(the_set.get_set_size("hamster"), 1)
self.assertEqual(the_set.get_set_size("turtle"), 2)
self.assertEqual(the_set.get_set_size("frog"), 2)
self.assertEqual(the_set.get_set_size("gerbil"), 1)
rc = the_set.union_set("fish","frog")
self.assertEqual(rc,True)
self.assertEqual(len(the_set), 7)
self.assertEqual(the_set.get_num_sets(), 4)
self.assertEqual(the_set.find_set("frog"), the_set.find_set("fish"))
self.assertEqual(the_set.find_set("turtle"), the_set.find_set("fish"))
self.assertEqual(the_set.get_set_size("frog"),3)
self.assertEqual(the_set.get_set_size("fish"),3)
self.assertEqual(the_set.get_set_size("turtle"),3)
rc = the_set.union_set("fish","turtle")
self.assertEqual(rc,False)
self.assertEqual(len(the_set), 7)
self.assertEqual(the_set.get_num_sets(), 4)
self.assertEqual(the_set.find_set("frog"), the_set.find_set("fish"))
self.assertEqual(the_set.find_set("turtle"), the_set.find_set("fish"))
self.assertEqual(the_set.get_set_size("frog"),3)
self.assertEqual(the_set.get_set_size("fish"),3)
self.assertEqual(the_set.get_set_size("turtle"),3)
rc = the_set.union_set("fish","cat")
self.assertEqual(rc,True)
self.assertEqual(len(the_set), 7)
self.assertEqual(the_set.get_num_sets(), 3)
self.assertEqual(the_set.find_set("frog"), the_set.find_set("cat"))
self.assertEqual(the_set.find_set("turtle"), the_set.find_set("dog"))
self.assertEqual(the_set.find_set("fish"), the_set.find_set("dog"))
self.assertEqual(the_set.get_set_size("frog"),5)
self.assertEqual(the_set.get_set_size("fish"),5)
self.assertEqual(the_set.get_set_size("turtle"),5)
self.assertEqual(the_set.get_set_size("cat"),5)
self.assertEqual(the_set.get_set_size("dog"),5)
self.assertEqual(the_set.get_set_size("hamster"),1)
self.assertEqual(the_set.get_set_size("gerbil"),1)
if __name__ == '__main__':
unittest.main()
Below is my a1_partb.py
class SetList:
class Node:
def __init__(self, data=None, my_set=None, next_node=None, prev_node=None):
self.data = data
self.next_node = next_node
self.prev_node = prev_node
self.my_set = my_set
def get_data(self):
return self.data
def get_next(self):
return self.next_node
def get_previous(self):
return self.prev_node
def get_set(self):
return self.my_set
def __init__(self):
self.head = None
self.tail = None
self.length = 0
def get_front(self):
return self.head
def get_back(self):
return self.tail
def make_set(self, data):
if self.head is None:
new_node = self.Node(data, self)
self.head = new_node
self.tail = new_node
self.length = 1
return new_node
else:
return None
def union_set(self, other_set):
if other_set.head is None:
return 0
if self.head is None:
self.head = other_set.head
self.tail = other_set.tail
self.length = other_set.length
current = other_set.head
while current is not None:
current.my_set = self
current = current.next_node
other_set.length = 0
other_set.head = None
other_set.tail = None
return self.length
self.tail.next_node = other_set.head
other_set.head.prev_node = self.tail
self.tail = other_set.tail
self.length += other_set.length
current = other_set.head
while current is not None:
current.my_set = self
current = current.next_node
other_set.length = 0
other_set.head = None
other_set.tail = None
return self.length
def find_data(self, data):
current = self.head
while current is not None:
if current.data == data:
return current
current = current.next_node
return None
def representative_node(self):
if self.head is None:
return None
else:
return self.head
def representative(self):
if self.head is None:
return None
else:
return self.head.data
def __len__(self):
return self.length
Below is a1_partc.py file ( it took me so long to write this code and I think error lies in this file)
from a1_partb import SetList
class DisjointSet:
def __init__(self):
self.set_list = SetList()
def make_set(self, element):
self.set_list.insert_set([element])
def get_set_size(self, element):
return len(self.find_set(element))
def find_set(self, element):
for s in self.set_list:
if element in s:
return s
return None
def union_set(self, element1, element2):
set1 = self.find_set(element1)
set2 = self.find_set(element2)
if set1 is None or set2 is None:
raise ValueError("Both elements must be in sets")
if set1 is not set2:
self.set_list.remove_set(set1)
self.set_list.remove_set(set2)
self.set_list.insert_set(set1 + set2)
def get_num_sets(self):
return len(self.set_list)
def __len__(self):
return len(self.set_list)
And below is the error I am getting (NOTE: I can't modify tester file, I think the error is in a1_partc.py Please help me fix the code, and maybe a1_partb.py is good)
EE.EE
======================================================================
ERROR: test_Find_Set (__main__.A1_PartCTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\Sahil Patel\Documents\Python\test_a1_partc.py", line 55, in test_Find_Set
the_set.make_set("cat")
File "c:\Users\Sahil Patel\Documents\Python\a1_partc.py", line 60, in make_set
self.set_list.insert_set([element])
AttributeError: 'SetList' object has no attribute 'insert_set'
======================================================================
ERROR: test_Get_Set_Size (__main__.A1_PartCTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\Sahil Patel\Documents\Python\test_a1_partc.py", line 64, in test_Get_Set_Size
the_set.make_set("cat")
File "c:\Users\Sahil Patel\Documents\Python\a1_partc.py", line 60, in make_set
self.set_list.insert_set([element])
AttributeError: 'SetList' object has no attribute 'insert_set'
ERROR: test_Union_Set (__main__.A1_PartCTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\Sahil Patel\Documents\Python\test_a1_partc.py", line 77, in test_Union_Set
the_set.make_set("cat")
File "c:\Users\Sahil Patel\Documents\Python\a1_partc.py", line 60, in make_set
self.set_list.insert_set([element])
AttributeError: 'SetList' object has no attribute 'insert_set'
----------------------------------------------------------------------
Ran 5 tests in 0.002s
FAILED (errors=4)
I want to pass all unit test cases

Related

VTK textactor not showing

I'm using VTK to show textactor with info 'ID' added to the actors. When my mouse is over an actor, the MouseEvent will call an 'add_text' function. The function was called and printed the correct 'ID' but there's no textactor adding to the window, is there any problem with my program?
class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("MouseMoveEvent", self.MouseMove)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
self.new = vtkTextActor()
self.old = vtkTextActor()
self.on_actor = False
def add_text(self,renderer,ID):
self.new.SetInput(ID)
txtprop = self.new.GetTextProperty()
txtprop.SetFontSize(36)
self.new.SetDisplayPosition(0,0)
# renderer.RemoveActor(self.old)
self.old = self.new
if self.on_actor:
renderer.AddActor(self.new)
print("add_text called , ID : ",ID)
else:
pass
# renderer.RemoveActor(self.old)
# renderer.RemoveActor(self.new)
def MouseMove(self,obj,event):
Mousepos = self.GetInteractor().GetEventPosition()
picker = vtk.vtkPropPicker()
picker.Pick(Mousepos[0], Mousepos[1], 0, self.GetDefaultRenderer())
self.NewPickedActor = picker.GetActor()
if self.NewPickedActor:
if not self.on_actor:
self.on_actor = True
print("on actor")
info = self.NewPickedActor.GetProperty().GetInformation()
info = str(info)
pattern_1 = re.compile(r"ID.*\d+")
pattern_2 = re.compile(r"\d+")
string = pattern_1.findall(info)[0]
ID = pattern_2.findall(string)[0]
# print("ID : ",ID)
self.add_text(self.GetDefaultRenderer() , ID)
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty())
self.NewPickedActor.GetProperty().SetDiffuse(1.0)
self.NewPickedActor.GetProperty().SetSpecular(0.0)
self.LastPickedActor = self.NewPickedActor
else:
pass
else:
print("not on actor")
self.on_actor = False
Update , the problem is solved, source code :
class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
self.AddObserver("LeftButtonReleaseEvent", self.leftButtonReleaseEvent)
self.AddObserver("MouseMoveEvent", self.MouseMove)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
self.new = vtkTextActor()
self.old = vtkTextActor()
self.on_actor = False
self.picker = vtk.vtkPropPicker()
def add_text(self,renderer,ID):
self.new.SetInput(ID)
txtprop = self.new.GetTextProperty()
txtprop.SetFontSize(72)
self.new.SetDisplayPosition(0,0)
self.old = self.new
if self.on_actor:
renderer.AddActor(self.new)
# print("ID : ",ID)
def MouseMove(self,obj,event):
Mousepos = self.GetInteractor().GetEventPosition()
picker = vtk.vtkPropPicker()
picker.Pick(Mousepos[0], Mousepos[1], 0, self.GetDefaultRenderer())
self.NewPickedActor = picker.GetActor()
if self.NewPickedActor:
self.on_actor = True
# print("on actor")
info = self.NewPickedActor.GetProperty().GetInformation()
info = str(info)
pattern_1 = re.compile(r"ID.*\d+")
pattern_2 = re.compile(r"\d+")
string = pattern_1.findall(info)[0]
ID = pattern_2.findall(string)[0]
# print("ID : ",ID)
self.add_text(self.GetDefaultRenderer() , ID)
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty())
self.NewPickedActor.GetProperty().SetDiffuse(1.0)
self.NewPickedActor.GetProperty().SetSpecular(0.0)
self.LastPickedActor = self.NewPickedActor
self.OnLeftButtonDown()
self.OnLeftButtonUp()
else:
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.on_actor = False
self.GetDefaultRenderer().RemoveActor(self.new)
self.GetDefaultRenderer().RemoveActor(self.old)
self.OnLeftButtonDown()
self.OnLeftButtonUp()
def leftButtonPressEvent(self, obj, event):
self.RemoveObservers("MouseMoveEvent")
self.OnLeftButtonDown()
def leftButtonReleaseEvent(self, obj, event):
self.AddObserver("MouseMoveEvent",self.MouseMove)
self.OnLeftButtonUp()

How to combine functions that don't return any specific values into a class

The following two functions read and save the image in directory:
def resample(image, reference_image, def_value=0.0):
return sitk.Resample(image,
reference_image,
sitk.Transform(),
sitk.sitkLinear,
def_value,
reference_image.GetPixelID())
def ResampleNiiSave(dcmPath, nrdPath):
dcmItk = dicomread(dcmPath, imgitk=True) # contains one file multiple slice
nrdItk = nrrdread(nrdPath, imgitk=True) # contains single nrrd file
data = [dcmItk, None]
if len(nrdItk.GetSize()) == 4:
if nrdItk.GetSize()[3] == 3:
for ig in range(3):
imgnp = sitk.GetArrayFromImage(nrdItk)[ig, ...]
imgItk = sitk.GetImageFromArray(imgnp)
data[1] = imgItk
nrdItkAl = resample(data[1], data[0])
nrd2niiPath = os.path.splitext(nrdPath)[0]+'_{}'.format(ig)+'.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
del imgnp, imgItk # necessary?
elif nrdItk.GetSize()[3] == 1:
imgnp = sitk.GetArrayFromImage(nrdItk)
imgnp = np.squeeze(imgnp)
imgItk = sitk.GetImageFromArray(imgnp)
data[1] = imgItk
nrdItkAl = resample(data[1], data[0])
nrd2niiPath = os.path.splitext(nrdPath)[0] + '.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
del imgnp, imgItk
else:
raise ValueError("image has unknown dimension")
else:
data[1] = nrdItk
nrdItkAl = sitk.Resample(data[1], data[0], sitk.Transform(), sitk.sitkLinear,
np.min(sitk.GetArrayFromImage(data[1])).astype('double'), data[1].GetPixelID())
nrd2niiPath = os.path.splitext(nrdPath)[0] + '.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
dcm2niiPath = dcmPath+'.nii.gz'
if os.path.isfile(dcm2niiPath) is None:
sitk.WriteImage(dcmItk, dcm2niiPath)
The ResampleNiiSave function doesn't necessarily return any value, rather it performs alignment and saves the variables to another format.
How do I create a class to combine both functions as class methods. I tried the following with little luck:
class prepSeg:
def __init__(self, dcmPath, nrdPath):
self.dcmPath = dcmPath
self.nrdPath = nrdPath
def resample(self, image, reference_image, def_value=0.0):
return sitk.Resample(image,
reference_image,
sitk.Transform(),
sitk.sitkLinear,
def_value,
reference_image.GetPixelID())
def ResampleNiiSave(self):
dcmItk = dicomread(self.dcmPath, imgitk=True) # contains one file multiple slice
nrdItk = nrrdread(self.nrdPath, imgitk=True) # contains single nrrd file
data = [dcmItk, None]
if len(nrdItk.GetSize()) == 4:
if nrdItk.GetSize()[3] == 3:
for ig in range(3):
imgnp = sitk.GetArrayFromImage(nrdItk)[ig, ...]
imgItk = sitk.GetImageFromArray(imgnp)
data[1] = imgItk
nrdItkAl = self.resample(data[1], data[0])
nrd2niiPath = os.path.splitext(self.nrdPath)[0]+'_{}'.format(ig)+'.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
del imgnp, imgItk # necessary?
elif nrdItk.GetSize()[3] == 1:
imgnp = sitk.GetArrayFromImage(nrdItk)
imgnp = np.squeeze(imgnp)
imgItk = sitk.GetImageFromArray(imgnp)
data[1] = imgItk
nrdItkAl = self.resample(data[1], data[0])
nrd2niiPath = os.path.splitext(self.nrdPath)[0] + '.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
del imgnp, imgItk
else:
raise ValueError("image has unknown dimension")
else:
data[1] = nrdItk
nrdItkAl = sitk.Resample(data[1], data[0], sitk.Transform(), sitk.sitkLinear,
np.min(sitk.GetArrayFromImage(data[1])).astype('double'), data[1].GetPixelID())
nrd2niiPath = os.path.splitext(self.nrdPath)[0] + '.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
dcm2niiPath = self.dcmPath+'.nii.gz'
if os.path.isfile(dcm2niiPath) is None:
sitk.WriteImage(dcmItk, dcm2niiPath)
Now, what should be the return for the prepSeg class. However I run prepSeg with two file paths inputs, it simply creates the class object but don't perform any of the tasks in the resample and save.
For example:
A = prepSeg(dcmFiles[-2],nrdFilesIdx[-2])
# .ResampleNiiSave()
# prepSeg.ResampleNiiSave
print(A)
A.ResampleNiiSave
returns:
prepSeg object at 0x0000025E6D96E710>
<bound method prepSeg.ResampleNiiSave of <prepSeg object at 0x0000025E6D96E710>>
Ok, you've got yourself in a mess with your IDE.
It is fooling you about what is going on.
Try this code:
class prepSeg():
def __init__(self):
self.a = ''
def foo(self):
print(self)
print(self.a)
print(self.b)
A = prepSeg()
A.foo()
#prepSeg.foo(prepSeg)
This produces the output:
<__main__.prepSeg object at 0x04267B70>
Traceback (most recent call last):
File "D:\andrew\Python\soMissingAttribute.py", line 12, in <module>
A.foo()
File "D:\andrew\Python\soMissingAttribute.py", line 8, in foo
print(self.b)
AttributeError: 'prepSeg' object has no attribute 'b'
Notice object at 0x... and the wording of the AttributeError. Also it is talking about b which is deliberately missing.
If I swap the driver code round:
A = prepSeg()
#A.foo()
prepSeg.foo(prepSeg)
I get a different output:
<class '__main__.prepSeg'>
Traceback (most recent call last):
File "D:\andrew\Python\soMissingAttribute.py", line 13, in <module>
prepSeg.foo(prepSeg)
File "D:\andrew\Python\soMissingAttribute.py", line 7, in foo
print(self.a)
AttributeError: type object 'prepSeg' has no attribute 'a'
Now self prints: <class '__main__.prepSeg'> and the AttributeError is about type object 'prepSeg' having no attribute a.

I had this error while writing a code on motion planning. How to get rif of it?

import time, timeit, random, pygame, sys
from math import *
import numpy as np
XDIM = 1000 #window length
YDIM = 1200 #window breadth
WINSIZE = [XDIM, YDIM]
EPSILON = 7.0 #threshold
NUMNODES = 10000
GOAL_RADIUS = 10
MIN_DISTANCE_TO_ADD = 1.0 #incremental distance
GAME_LEVEL = 1
RANDOM_COUNT = 10000
pygame.init()
fpsClock = pygame.time.Clock()
#screen parameters and variable
screen = pygame.display.set_mode(WINSIZE)
pygame.display.set_caption('Q-learning')
white = 255, 240, 200
black = 20, 20, 40
red = 255, 0, 0
blue = 0, 255, 0
green = 0, 0, 255
cyan = 0,255,255
class Node:
def __init__(self):
self.x = x
self.y = y
self.cost = 0.0
self.parent = None
self.children = set()
class RRT(__Q_table):
def __init__(self, start, goal,
obstacleList, incremental_dist = 15.0,
learning_rate=20, max_iterations = 2000, randArea= None):
self.start = Node(start[0], start[1])
self.end = Node(goal[0], goal[1])
self.Xrand = randArea[0]
self.Yrand = randArea[1]
self.margin = incremental_distance
self.learning_rate = learning_rate
self.obstacleList = obstacleList
self.max_iterations = max_iterations
def planning(self, animation = True):
self.NodeList = {0 : self.start}
i=0
while True:
print(i)
if set(self.start).intersection(obstacleList) == None:
self.NodeList.append(self.start)
print(self.NodeList)
rnd = self.get_random_point()
nearest_index = self.GetNearestListIndex(rnd)
new_node = self.steer(rnd, nearest_index)
if self.Collision_check(new_node, self.obstacleList):
near_indices = self.find_near_nodes(new_node, 5)
new_node = self.choose_parent(new_node, near_indices)
self.nodeList[i+100] = new_node
self.rewire(i+100, new_node, near_indices)
self.nodeList[new_node.parent].children.add(i+100)
if len(self.NodeList) > self.max_iterations:
leaves = [keys for key, node in self.NodeList.items]
if len(leaves) > 1:
index = leaves[random.randint(0, len(leaves)-1)]
self.NodeList[self.NodeList[index].paresnt].children.discard(index)
self.NodeList.pop(index)
else:
leaves = [key for key, node in self.NodeList.items() if len(node.children)==0]
index = leaves[random.randint(0, len(leaves) -1)]
self.NodeList[self.NodeList[index].parent].children.discard(index)
self.NodeList.pop(index)
if animation == True:
self.DrawGraph(rnd)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.obstacleList.append((event.pos[0], event.pos[1], 30, 30))
self.path_validation()
elif event.button == 3:
self.end.x = event.pos[0]
self.end.y = event.pos[1]
self.path_validation()
def path_validation(self):
lastIndex = self.get_the_best_last_index()
if lastIndex and set(lastIndex).intersection(obstacleList) == None:
while self.NodeList[lastIndex].parent is not None:
nodeIndex = lastIndex
lastIndex = self.NodeList[lastIndex].parent
dx = self.NodeList[nodeIndex].x - self.NodeList[lastIndex].x
dy = self.NodeList[nodeIndex].y - self.NodeList[lastIndex].y
d = math.sqrt.atan2(dx**2 + dy**2)
theta = math.atan2(dy, dx)
if not self.check_collision_extend(self.NodeList[lastIndex].x,self.NodeList[lastIndex].y, theta, d):
self.NodeList[lastIndex].children.discard(nodeIndex)
self.eliminate_branch(nodeIndex)
def eliminate_brach(self, nodeIndex):
safenodesList = []
if set(nodeIndex).intersection(obstacleList) == None:
safenodesList.append(nodeIndex)
for not_safe in safenodesList[nodeIndex].children:
self.eliminate_branch(not_safe)
self.NodeList.pop(nodeIndex)
def choose_parent(self,new_node,nearest_index):
if len(nearest_index) == 0:
return new_node
if Current_point == nearest_index and self.CollisionCheck(new_node, obstacleList):
Current_point = new_node
return Current_point
distanceList = []
for i in near_indices:
dx = new_node.x - self.NodeList[i].x
dy = new_node.y - self.NodeList[i].y
d = math.sqrt(dx **2 + dy **2)
theta = math.atan2(dy, dx)
if self.check_collision_extend(self.NodeList[i].x, self.NodeList[i].y, theta, d):
distanceList.append(self.NodeList[i].cost + d)
else:
distanceList.append(float("inf"))
minimum_cost = min(distanceList)
minimum_index = near_indices[distacelist.index(minimum_cost)]
if minimum_cost == float("inf"):
print("minimum_cost is INFINITE")
return Current_point
Current_point.cost = minimum_cost
Current_point.parent = minimum_index
Current_point_with_maximum_Q_value = self.max_Q_nextvalue(Current_point)
return Current_point.cost, Current_point.parent, Current_point_with_maximum_Q_value
def steer(self, rnd, nearest_index):
nearest_node = self.NodeList[nearest_index]
theta = math.atan2(rnd[1] - nearest_node.y, rnd[0] - nearest_node.x)
new_node = Node(nearest_node.x , nearest_node.y)
new_node.x += incremental_distance * math.cos(theta)
new_node.y += incremental_distance * math.sin(theta)
new_node.cost = nearest_node.cost + incremental_distance
new_node.parent = nearest_index
return new_node
def get_random_point(self, Current_point=None):
self.Current_point = Current_point
if random.randint(0, 100) > self.learning_rate:
rnd = [random.uniform(0, Xrand), random.uniform(0, yrand)]
elif random.randint(0, 100) <= RANDOM_COUNT:
Current_point = max_Q_action(Current_point)
rnd = random_action(Current_point)
else:
rnd = [self.end.x, self.end.y]
return rnd
def get_best_last_index(self):
disglist = [(key, self.calc_dist_to_goal(node.x, node.y)) for key, node in self.NodeList.items()]
goal_indices = [key for key, distance in disglist if distance <= self.margin]
if len(goal_indices) == 0:
return None
minimum_cost = min([self.NodeList[key].cost for key in goal_indices])
for i in goal_indices:
if self.NodeList[i].cost == minimum_cost:
return i
return None
def gen_final_course(self, goal_indices):
path = [[self.end.x, self.end.y]]
while self.NodeList[goal_indices].parent is not None:
node = self.NodeList[goal_indices]
path.append([node.x, node.y])
goal_indices = node.parent
path.append([self.start.x, self.start.y])
return path
def calc_dist_to_goal(self, x, y):
return np.linalg.norm([x - self.end.x, y - self.end.y])
def find_near_nodes(self, new_node, value):
r = self.margin * value
distanceList = np.subtract( np.array([ (node.x, node.y) for node in self.NodeList.values() ]), (new_node.x,new_node.y))**2
distanceList = np.sum(distanceList, axis=1)
near_indices = np.where(distanceList <= r ** 2)
near_indices = np.array(list(self.NodeList.keys()))[near_indices]
return nearinds
def rewire(self, newNodeInd, new_node, near):
nnode = len(self.NodeList)
for i in nearinds:
nearNode = self.nodeList[i]
dx = new_node.x - nearNode.x
dy = new_node.y - nearNode.y
d = math.sqrt(dx ** 2 + dy ** 2)
scost = new_node.cost + d
if near_node.cost > scost:
theta = math.atan2(dy, dx)
if self.check_collision_extend(nearNode.x, nearNode.y, theta, d):
self.NodeList[nearNode.parent].children.discard(i)
nearNode.parent = newNodeInd
nearNode.cost = scost
new_node.children.add(i)
def check_collision_extend(self, nix, niy, theta, d):
tmpNode = Node(nix,niy)
for i in range(int(d/5)):
tmpNode.x += 5 * math.cos(theta)
tmpNode.y += 5 * math.sin(theta)
if not self.CollisionCheck(tmpNode, self.obstacleList):
return False
return True
def DrawGraph(self, rnd=None):
screen.fill((255, 255, 255))
for node in self.NodeList.values():
if node.parent is not None:
pygame.draw.line(screen,(0,255,0),[self.NodeList[node.parent].x,self.NodeList[node.parent].y],[node.x,node.y])
for node in self.NodeList.values():
if len(node.children) == 0:
pygame.draw.circle(screen, (255,0,255), [int(node.x),int(node.y)], 2)
for(sx,sy,ex,ey) in self.obstacleList:
pygame.draw.rect(screen,(0,0,0), [(sx,sy),(ex,ey)])
pygame.draw.circle(screen, (255,0,0), [self.start.x, self.start.y], 10)
pygame.draw.circle(screen, (0,0,255), [self.end.x, self.end.y], 10)
lastIndex = self.get_best_last_index()
if lastIndex is not None:
path = self.gen_final_course(lastIndex)
ind = len(path)
while ind > 1:
pygame.draw.line(screen,(255,0,0),path[ind-2],path[ind-1])
ind-=1
pygame.display.update()
def Get_nearest_list_index(self, rnd):
distanceList = np.subtract( np.array([ (node.x, node.y) for node in self.NodeList.values() ]), (rnd[0],rnd[1]))**2
distanceList = np.sum(distanceList, axis=1)
minimum_index = list(self.NodeList.keys())[np.argmin(distancelist)]
return minimum_index
def Collision_check(self, node, obstacleList):
for(sx,sy,ex,ey) in obstacleList:
sx,sy,ex,ey = sx+2,sy+2,ex+2,ey+2
if node.x > sx and node.x < sx+ex:
if node.y > sy and node.y < sy+ey:
return False
return True
def main():
print("start RRT path planning")
obstacleList = [
(400, 380, 400, 20),
(400, 220, 20, 180),
(500, 280, 150, 20),
(0, 500, 100, 20),
(500, 450, 20, 150),
(400, 100, 20, 80),
(100, 100, 100, 20)
]
rrt = RRT(obstacleList = obstacleList, start =[10, 580], goal = [540, 150],
randArea = [XDIM, YDIM])
path = rrt.Planning(animation=show_animation)
if __name__ == '__main__':
main()
The error is -
TypeError Traceback (most recent call last)
<ipython-input-1-6e62c6da9819> in <module>
531
532 if __name__ == '__main__':
--> 533 main()
534
<ipython-input-1-6e62c6da9819> in main()
525
526 rrt = RRT(obstacleList = obstacleList, start =[10, 580], goal = [540, 150],
--> 527 randArea = [XDIM, YDIM])
528
529 path = rrt.planning(animation=show_animation)
<ipython-input-1-6e62c6da9819> in __init__(self, start, goal, obstacleList, incremental_dist, learning_rate, max_iterations, randArea)
249 learning_rate=20, max_iterations = 2000, randArea= None):
250
--> 251 self.start = Node(start[0], start[1])
252 self.end = Node(goal[0], goal[1])
253 self.Xrand = randArea[0]
TypeError: init() takes 1 positional argument but 3 were given
I am trying to write a code for motion planning in python using pygame module. I encountered this error given above. I even tried reducing the parameters into the constructor init() but it did not work out.
Please note that here I am not trying to do inheritance.
Thanks
Replace
path = rrt.Planning(animation=show_animation)
With
path = rrt.planning(animation=show_animation)
Edit: after the addition of the tracelog
Your error is that you provided two arguments to create a Node:
self.start = Node(start[0], start[1])
But its __init__ method expects none to be passed:
class Node:
def __init__(self):

function, not defined by using def

I am very new to Python, however I am in need of using Python for my project, I have been learning on the go. There is a point I don't understand with the piece of code below:
result['data'] = pool.map(image_processor, im_info)
So, I figured out pool.map(somefunc,[list of params]) is akin to parfor of Matlab. What I don't understand is the part relating image_processor. I am assuming it should be a function, however it is not defined as one using def. The code is working perfectly fine and I tried to trace it with pdb and it seems to process the images as intended in batches, for caffe input. This might be a very trivial question, I just am new to Python. So what is the order of execution that image_processor just works and how can I make changes in this function (I will be passing multiple parameters, I just need to know where I can make those changes as the function is not defined using a def) Thx.
#Data layer for video. Change flow_frames and RGB_frames to be the path to the flow and RGB frames.
import sys
sys.path.append('../../python')
import caffe
import io
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import scipy.misc
import time
import pdb
import glob
import pickle as pkl
import random
import h5py
from multiprocessing import Pool
from threading import Thread
import skimage.io
import copy
flow_frames = 'flow_images/'
RGB_frames = 'frames/'
test_frames = 16
train_frames = 16
test_buffer = 3
train_buffer = 24
def processImageCrop(im_info, transformer, flow):
im_path = im_info[0]
im_crop = im_info[1]
im_reshape = im_info[2]
im_flip = im_info[3]
data_in = caffe.io.load_image(im_path)
if (data_in.shape[0] < im_reshape[0]) | (data_in.shape[1] < im_reshape[1]):
data_in = caffe.io.resize_image(data_in, im_reshape)
if im_flip:
data_in = caffe.io.flip_image(data_in, 1, flow)
data_in = data_in[im_crop[0]:im_crop[2], im_crop[1]:im_crop[3], :]
processed_image = transformer.preprocess('data_in',data_in)
return processed_image
class ImageProcessorCrop(object):
def __init__(self, transformer, flow):
self.transformer = transformer
self.flow = flow
def __call__(self, im_info):
return processImageCrop(im_info, self.transformer, self.flow)
class sequenceGeneratorVideo(object):
def __init__(self, buffer_size, clip_length, num_videos, video_dict, video_order):
self.buffer_size = buffer_size
self.clip_length = clip_length
self.N = self.buffer_size*self.clip_length
self.num_videos = num_videos
self.video_dict = video_dict
self.video_order = video_order
self.idx = 0
def __call__(self):
label_r = []
im_paths = []
im_crop = []
im_reshape = []
im_flip = []
if self.idx + self.buffer_size >= self.num_videos:
idx_list = range(self.idx, self.num_videos)
idx_list.extend(range(0, self.buffer_size-(self.num_videos-self.idx)))
else:
idx_list = range(self.idx, self.idx+self.buffer_size)
for i in idx_list:
key = self.video_order[i]
label = self.video_dict[key]['label']
video_reshape = self.video_dict[key]['reshape']
video_crop = self.video_dict[key]['crop']
label_r.extend([label]*self.clip_length)
im_reshape.extend([(video_reshape)]*self.clip_length)
r0 = int(random.random()*(video_reshape[0] - video_crop[0]))
r1 = int(random.random()*(video_reshape[1] - video_crop[1]))
im_crop.extend([(r0, r1, r0+video_crop[0], r1+video_crop[1])]*self.clip_length)
f = random.randint(0,1)
im_flip.extend([f]*self.clip_length)
rand_frame = int(random.random()*(self.video_dict[key]['num_frames']-self.clip_length)+1+1)
frames = []
for i in range(rand_frame,rand_frame+self.clip_length):
frames.append(self.video_dict[key]['frames'] %i)
im_paths.extend(frames)
im_info = zip(im_paths,im_crop, im_reshape, im_flip)
self.idx += self.buffer_size
if self.idx >= self.num_videos:
self.idx = self.idx - self.num_videos
return label_r, im_info
def advance_batch(result, sequence_generator, image_processor, pool):
label_r, im_info = sequence_generator()
#tmp = image_processor(im_info[0])
result['data'] = pool.map(image_processor, im_info)
result['label'] = label_r
cm = np.ones(len(label_r))
cm[0::16] = 0
result['clip_markers'] = cm
class BatchAdvancer():
def __init__(self, result, sequence_generator, image_processor, pool):
self.result = result
self.sequence_generator = sequence_generator
self.image_processor = image_processor
self.pool = pool
def __call__(self):
return advance_batch(self.result, self.sequence_generator, self.image_processor, self.pool)
class videoRead(caffe.Layer):
def initialize(self):
self.train_or_test = 'test'
self.flow = False
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_testVideos.txt'
def setup(self, bottom, top):
random.seed(10)
self.initialize()
f = open(self.video_list, 'r')
f_lines = f.readlines()
f.close()
video_dict = {}
current_line = 0
self.video_order = []
for ix, line in enumerate(f_lines):
video = line.split(' ')[0].split('/')[1]
l = int(line.split(' ')[1])
frames = glob.glob('%s%s/*.jpg' %(self.path_to_images, video))
num_frames = len(frames)
video_dict[video] = {}
video_dict[video]['frames'] = frames[0].split('.')[0] + '.%04d.jpg'
video_dict[video]['reshape'] = (240,320)
video_dict[video]['crop'] = (227, 227)
video_dict[video]['num_frames'] = num_frames
video_dict[video]['label'] = l
self.video_order.append(video)
self.video_dict = video_dict
self.num_videos = len(video_dict.keys())
#set up data transformer
shape = (self.N, self.channels, self.height, self.width)
self.transformer = caffe.io.Transformer({'data_in': shape})
self.transformer.set_raw_scale('data_in', 255)
if self.flow:
image_mean = [128, 128, 128]
self.transformer.set_is_flow('data_in', True)
else:
image_mean = [103.939, 116.779, 128.68]
self.transformer.set_is_flow('data_in', False)
channel_mean = np.zeros((3,227,227))
for channel_index, mean_val in enumerate(image_mean):
channel_mean[channel_index, ...] = mean_val
self.transformer.set_mean('data_in', channel_mean)
self.transformer.set_channel_swap('data_in', (2, 1, 0))
self.transformer.set_transpose('data_in', (2, 0, 1))
self.thread_result = {}
self.thread = None
pool_size = 24
self.image_processor = ImageProcessorCrop(self.transformer, self.flow)
self.sequence_generator = sequenceGeneratorVideo(self.buffer_size, self.frames, self.num_videos, self.video_dict, self.video_order)
self.pool = Pool(processes=pool_size)
self.batch_advancer = BatchAdvancer(self.thread_result, self.sequence_generator, self.image_processor, self.pool)
self.dispatch_worker()
self.top_names = ['data', 'label','clip_markers']
print 'Outputs:', self.top_names
if len(top) != len(self.top_names):
raise Exception('Incorrect number of outputs (expected %d, got %d)' %
(len(self.top_names), len(top)))
self.join_worker()
for top_index, name in enumerate(self.top_names):
if name == 'data':
shape = (self.N, self.channels, self.height, self.width)
elif name == 'label':
shape = (self.N,)
elif name == 'clip_markers':
shape = (self.N,)
top[top_index].reshape(*shape)
def reshape(self, bottom, top):
pass
def forward(self, bottom, top):
if self.thread is not None:
self.join_worker()
#rearrange the data: The LSTM takes inputs as [video0_frame0, video1_frame0,...] but the data is currently arranged as [video0_frame0, video0_frame1, ...]
new_result_data = [None]*len(self.thread_result['data'])
new_result_label = [None]*len(self.thread_result['label'])
new_result_cm = [None]*len(self.thread_result['clip_markers'])
for i in range(self.frames):
for ii in range(self.buffer_size):
old_idx = ii*self.frames + i
new_idx = i*self.buffer_size + ii
new_result_data[new_idx] = self.thread_result['data'][old_idx]
new_result_label[new_idx] = self.thread_result['label'][old_idx]
new_result_cm[new_idx] = self.thread_result['clip_markers'][old_idx]
for top_index, name in zip(range(len(top)), self.top_names):
if name == 'data':
for i in range(self.N):
top[top_index].data[i, ...] = new_result_data[i]
elif name == 'label':
top[top_index].data[...] = new_result_label
elif name == 'clip_markers':
top[top_index].data[...] = new_result_cm
self.dispatch_worker()
def dispatch_worker(self):
assert self.thread is None
self.thread = Thread(target=self.batch_advancer)
self.thread.start()
def join_worker(self):
assert self.thread is not None
self.thread.join()
self.thread = None
def backward(self, top, propagate_down, bottom):
pass
class videoReadTrain_flow(videoRead):
def initialize(self):
self.train_or_test = 'train'
self.flow = True
self.buffer_size = train_buffer #num videos processed per batch
self.frames = train_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = flow_frames
self.video_list = 'ucf101_split1_trainVideos.txt'
class videoReadTest_flow(videoRead):
def initialize(self):
self.train_or_test = 'test'
self.flow = True
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = flow_frames
self.video_list = 'ucf101_split1_testVideos.txt'
class videoReadTrain_RGB(videoRead):
def initialize(self):
self.train_or_test = 'train'
self.flow = False
self.buffer_size = train_buffer #num videos processed per batch
self.frames = train_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_trainVideos.txt'
class videoReadTest_RGB(videoRead):
def initialize(self):
self.train_or_test = 'test'
self.flow = False
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_testVideos.txt'
The code is from https://github.com/LisaAnne/lisa-caffe-public/blob/lstm_video_deploy/examples/LRCN_activity_recognition/sequence_input_layer.py
See this line:
self.image_processor = ImageProcessorCrop(self.transformer, self.flow)
The ImageProcessorCrop class has a __call__ method, which allows these objects to be used as if they're functions. When you call them, it calls that method.

pyqtgraph plot widget is not update

I have two working stream.
1 - receiving data from com port and sends signal.
def __packet_parser(self, *args):
while self.__thred_is_runing:
data = self.__connection.read_all()
if data != b'':
self.change_data.emit(self.__readline())
self.__callback(self.__readline())
2 - draw graph.
def set_value_by_plot_name(self, value, name='default'):
self.__plots[name].setData(np.append(self.__plots[name].getData()[1][1:], value))
def __test(self, value):
tmp = value.decode('windows-1252')
data = tmp.split('\t')
if len(data) == 10:
self.__gr.set_value_by_plot_name(int(data[1]))
def main(self):
self.__window = Window()
self.__gr = Graphics()
w = self.__gr.get_widget()
self.__window.add_widget(w)
connect = COMPortConnection('COM7', 38400)
connect.change_data.connect(self.__test)
connect.open()
self.__window.show()
few seconds everything works fine, and then ceases to be updated.
What problem?
Data is updated, picture is not.
I had a similar problem, i solved it by passing by an intermediate method i'm not sure why:
class GraphWindowController:
# init ...
def start_graph(self):
# ... process
self.view.run_graph(self.graph_nodes)
stream_signals_and_slots = []
my_steam_signal.connect(self.add_value_to_graph)
def add_value_to_graph(self, values):
self.view.update(values)
class GraphWindow(QtGui.QFrame, Ui_GraphWindow):
def __init__(self, parent=None):
"""Initializer."""
super(GraphWindow, self).__init__(parent)
self.setupUi(self)
self.connect_signals()
self.plot_widget = None
self.y_data = []
self.x_data = []
self.curve = []
self.display_range = None
self.velocity = None
self.values_to_display = None
self.count_value_to_display = 0
self.legend = None
self.build_gui()
# Doing stuff ...
def build_gui(self):
self.plot_widget = pg.PlotWidget()
layout = QtGui.QGridLayout()
self.PlotLayout.setLayout(layout)
layout.addWidget(self.plot_widget, 0,1)
def configure_graph(self, display_range, velocity):
self.display_range = display_range
self.velocity = velocity
self.plot_widget.setLabels(left='axis 1')
self.legend = pg.LegendItem((100, 60), offset=(70, 30))
self.legend.setParentItem(self.plot_widget.graphicsItem())
self.plot_widget.showGrid(True, True, 251)
self.plot_widget.showButtons()
self.plot_widget.setRange(rect=None, xRange=[0, self.display_range], yRange=[
1300, -300], padding=0.09, update=True, disableAutoRange=True)
def run_graph(self, values_to_display):
self.values_to_display = values_to_display
self.curve_number = len(self.values_to_display)
for i in range(self.curve_number):
self.y_data.append([])
colors_to_use = self.enumerate_colors(
self.curve_number)
# Blablabla doing stuff ...
def update(self, my_values):
value = my_values
for i in range(self.curve_number):
if len(self.y_data[i]) >= int((self.display_range * 1000) / self.velocity):
del self.y_data[i][0]
else:
my_var = len(self.x_data) * (self.velocity / 1000.0)
self.x_data.append(my_var)
self.y_data[i].append(int(value[i]))
my_data_to_display = list(
zip(self.x_data, self.y_data[i]))
my_array = np.array(my_data_to_display)
self.curve[i].setData(my_array)

Categories

Resources