Skip to content

BudgetItem's with kwargs

It looks like nb.BudgetItem's can be defined with keyword arguments, but it seems to me that a frequency vector either needs to be given or already exist. See here. So I'm wondering how to define nb.BudgetItem's with keyword arguments within a larger nb.Budget. The fact that freq doesn't yet exist seems to be a problem.

For a simple example, say you want to make a noise budget that's just a bunch of power laws. If the nb.BudgetItem initialization didn't require freq to either be given or already exist, then I think the following would work

class PowerLaw(nb.Noise):
    def calc(self):
        return self.coeff * self.freq**self.power


class TestIFO(nb.Budget):
    noises = [
        PowerLaw(power=-1, coeff=2),
        PowerLaw(power=-2, coeff=3)
    ]

But doing so results in

AttributeError: Frequency array not provided or defined.

Keywords can be defined after a BudgetItem has been defined, see here, so as a test the following works

class PowerLaw2(nb.Noise):
    def calc(self):
        self.update(power=-2, coeff=3)
        return self.coeff * self.freq**self.power


class TestIFO(nb.Budget):
    noises = [
        PowerLaw2
    ]

However, the following

class TestIFO(nb.Budget):
    noises = [
        PowerLaw
    ]

    def __init__(self):
        self.noises[0].update(power=-2, coeff=3)

results in

TypeError: __init__() takes 1 positional argument but 2 were given

Just as further tests to figure out how to do this, both of the following

class PowerLaw3(nb.Noise):
    def __init__(self):
        self.update(power=-2, coeff=3)

    def calc(self):
        return self.coeff * self.freq**self.power


class PowerLaw4(nb.Noise):
    def __init__(self):
        setattr(self, power, -2)
        setattr(self, coeff, 3)

    def calc(self):
        return self.coeff * self.freq**self.power

result in

TypeError: __init__() got an unexpected keyword argument 'freq'

So how would you make a noise budget that's a bunch of power laws without defining a new nb.Noise for each one by hand?