0

There have been similar questions, but none of them had answer I was looking for. I am getting: AttributeError: 'ParticleFilter' object has no attribute 'initialCoordinates' (function create_gaussian_particles) even though I have definted in in init constructor and have created an instance of it, I can't sure what is the problem, I tried to look up myself, but nothing, here is the code:

import numpy as np

class ParticleFilter(object):

    def __init__(self, N, initialConditions, imageSize):

        self.create_particles(N, initialConditions, imageSize)
        self.create_weights(N)
        self.imageSize = imageSize
        self.initialCoordinates = np.array([initialConditions[0, 0], initialConditions[0, 1]])
        self.initialStdDev = np.array([initialConditions[1, 0], initialConditions[1, 1]])

    def create_particles(self, N, initialConditions, imageSize):
        if initialConditions is None:
            self.create_uniform_particles(N, imageSize)
        else:
            self.create_gaussian_particles(N, initialConditions)

    def create_gaussian_particles(self, N, initialConditions):
        self.particles = np.empty((N, 2))
        self.particles[:, 0] = self.initialCoordinates[0] + (np.random.randn(N) * self.initialStdDev[0])
        self.particles[:, 1] = self.initialCoordinates[1] + (np.random.randn(N) * self.initialStdDev[1])

    def create_uniform_particles(self, N, imageSize):
        pass

    def create_weights(self, N):
        pass

N = 1000
initialPosition = np.array([10, 10])
initialStdDev = np.array([5, 5])
initialConditions = np.array([initialPosition, initialStdDev])
imageSize = np.array([480, 360])
pf = ParticleFilter(N, initialConditions, imageSize)

File "c:/Projects/pf2.py", line 65, in <module>
    pf = ParticleFilter(N, initialConditions, imageSize)

File "c://Projects/pf2.py", line 8, in __init__
    self.create_particles(N, initialConditions, imageSize)

File "c://Projects/pf2.py", line 18, in create_particles
    self.create_gaussian_particles(N, initialConditions)


 File "c://Projects/pf2.py", line 22, in create_gaussian_particles
    self.particles[:, 0] = self.initialCoordinates[0] + (np.random.randn(N) * self.initialStdDev[0])

AttributeError: 'ParticleFilter' object has no attribute 'initialCoordinates'
Jokubas11
  • 79
  • 7
  • Can you include the full traceback whick should tell us the sequence of events. init calls create_particles which calls create_gaussian_particles which uses initialCoordinates. – tdelaney Apr 20 '20 at 22:02
  • File "c:/Projects/pf2.py", line 65, in pf = ParticleFilter(N, initialConditions, imageSize) File "c://Projects/pf2.py", line 8, in __init__ self.create_particles(N, initialConditions, imageSize) File "c://Projects/pf2.py", line 18, in create_particles self.create_gaussian_particles(N, initialConditions) File "c://Projects/pf2.py", line 22, in create_gaussian_particles self.particles[:, 0] = self.initialCoordinates[0] + (np.random.randn(N) * self.initialStdDev[0]) AttributeError: 'ParticleFilter' object has no attribute 'initialCoordinates' – Jokubas11 Apr 20 '20 at 22:05
  • 1
    Is it just a case of moving the final two lines of init to the front? – tdelaney Apr 20 '20 at 22:09

1 Answers1

1

A couple of things here - first, __ init __ is not a constructor. That may seem pedantic, but it can be important not to think of it like one. Secondly, if you look at the stack trace that is produced it will tell you the exact sequence of events that occurred to cause the error. Stack traces are super helpful, not just a bunch of noise the interpreter makes when something goes wrong. They should be the first thing you look when an exception occurs. In this case, you can see that the line that causes the error:

self.particles[:, 0] = self.initialCoordinates[0] + (np.random.randn(N) * self.initialStdDev[0])

Which is in the method create_gaussian_particles(self, N, initialConditions). Looking at the stack frame before that, we can see that create_gaussian_particles(...) is called from __init__(...) before initialCoordinates is defined. The code is, in a very roundabout way, attempting to access the property before it is defined.

BS Labs
  • 116
  • 4