Parameter Distributions

By default a realization obtained by an instance of Model will have degenerate distributions of all blob parameters. For any interesting application this is not enough. In order to provide the highest possible flexibility for setting blob parameters this is done by a separate class, which should implement BlobFactory. An implementation is provided by DefaultBlobFactory which allows to easily set independent distributions of any blob parameters to a wide range of distribution families.

DefaultBlobFactory

We can use the DefaultBlobFactory class in order to change the distribution functions of the following blob parameters:

“amplitude”

blob amplitude

“wp” / “ws”

widths in the principal and secondary blob direction

“vx” / “vy”

velocities in the x- and y-direction

“spp” / “sps”

pulse shape parameters in the principal and secondary direction

By default, the amplitude is exponentially distributed with mean 1, the perpendicular velocity vy is zero, and all other parameters are constant (widths and vx 1, shape parameters 0.5). Individual parameters are reconfigured with the set_sampler method, which takes the parameter name and either one of the built-in distributions or a custom sampling callable. The following distribution functions are implemented:

“exp”

exponential distribution with mean 1

“gamma”

gamma distribution with free_parameter as shape parameter and mean 1

“normal”

normal distribution with zero mean and free_parameter as scale parameter

“uniform”

uniform distribution with mean 1 and free_parameter as width

“ray”

Rayleight distribution with mean 1

“deg”

degenerate distribution at free_parameter

“zeros”

array of zeros

As you can see, some of these distributions require an additional free_parameter to specify the distribution. This is passed as the third argument of set_sampler. set_sampler returns the factory itself, so calls can be chained.

Let’s take a look at an example. Let’s assume we want to sample the blob amplitudes from a normal distribution with 5 as the scale parameter. We choose the default distributions for all other blob parameters. We would set up the Model as follows:

    from blobmodel import DefaultBlobFactory, DistributionEnum, Geometry, Model

    my_blob_factory = DefaultBlobFactory(t_drain=100).set_sampler(
        "amplitude", DistributionEnum.normal, free_parameter=5
    )

    bm = Model(
        geometry=Geometry(Nx=10, Ny=10, Lx=10, Ly=10, dt=0.1, T=20),
        blob_factory=my_blob_factory,
        num_blobs=100,
    )

    ds = bm.make_realization()

If none of the built-in distributions fits, pass a sampling callable to set_sampler instead of a DistributionEnum:

    from blobmodel import DefaultBlobFactory, Geometry, Model

    # Draw the widths from a log-normal distribution, which is not built in.
    # The callable receives the factory's random number generator (draw from
    # it, not from the global np.random state, to stay seedable) and the
    # number of blobs, and returns one value per blob.
    bf = DefaultBlobFactory(seed=42).set_sampler(
        "wp", lambda rng, num_blobs: rng.lognormal(sigma=0.5, size=num_blobs)
    )

    bm = Model(
        geometry=Geometry(Nx=10, Ny=10, Lx=10, Ly=10, dt=0.1, T=20),
        blob_factory=bf,
        num_blobs=100,
    )
    ds = bm.make_realization()

Pre-built blobs: Model.from_blobs

If you already know exactly which blobs you want to realize, you don’t need parameter distributions at all: build the Blob objects yourself and hand the list to Model.from_blobs. Each Blob carries its own parameters, so the sampling arguments of Model (num_blobs and blob_shape) are not needed:

    from blobmodel import Blob, BlobShapeImpl, Geometry, Model
    import numpy as np

    blobs = [
        Blob(
            blob_id=i,
            blob_shape=BlobShapeImpl(),
            amplitude=1,
            width_p=1,
            width_s=1,
            v_x=1,
            v_y=0,
            pos_x0=0,
            pos_y0=pos_y0,
            t_init=t_init,
            t_drain=np.inf,  # no draining
        )
        for i, (pos_y0, t_init) in enumerate([(2, 0), (5, 2), (8, 4)])
    ]

    bm = Model.from_blobs(
        blobs, geometry=Geometry(Nx=10, Ny=10, Lx=10, Ly=10, dt=0.1, T=10)
    )
    ds = bm.make_realization()

Model.from_blobs is a shortcut for wrapping the list in a BlobListFactory, which you can also use directly with Model(blob_factory=...). Note that t_drain=np.inf is the documented way of specifying non-draining blobs.

CallableBlobFactory

If you want custom randomness per blob without writing a whole BlobFactory subclass, use CallableBlobFactory: it builds each blob by calling a getter function with the factory’s random number generator. Drawing the random numbers from that generator (instead of the global np.random state) makes the realization reproducible via CallableBlobFactory(seed=...) or Model(seed=...):

    from blobmodel import Blob, BlobShapeImpl, CallableBlobFactory, Geometry, Model
    import numpy as np

    def blob_getter(rng: np.random.Generator) -> Blob:
        # Draw random numbers from the generator you are given (not from the
        # global np.random state) so that the realization is reproducible
        # through CallableBlobFactory(seed=...) or Model(seed=...).
        return Blob(
            blob_id=0,
            blob_shape=BlobShapeImpl(),
            amplitude=rng.exponential(),
            width_p=1,
            width_s=1,
            v_x=1,
            v_y=0,
            pos_x0=0,
            pos_y0=rng.uniform(0, 10),
            t_init=rng.uniform(0, 10),
            t_drain=np.inf,
        )

    bf = CallableBlobFactory(blob_getter, seed=42)

    bm = Model(
        geometry=Geometry(Nx=10, Ny=10, Lx=10, Ly=10, dt=0.1, T=10),
        num_blobs=10,  # blob_getter is called num_blobs times
        blob_factory=bf,
    )
    ds = bm.make_realization()

CustomBlobFactory

But what if you want to use a distribution function that is not implemented? Or maybe you even want to change the initial position and arrival time of each blob? In this case you can use the CustomBlobFactory class to define all blob parameters individually. An example could look like this:

    from blobmodel import BlobFactory, AbstractBlobShape, Blob, Geometry, Model
    import numpy as np

    class CustomBlobFactory(BlobFactory):
        def __init__(self, t_drain: float = np.inf, seed=None) -> None:
            # Blob draining is owned by the factory: each sampled Blob gets
            # this t_drain. Draw random numbers from self.rng: a seed passed
            # to Model replaces it (via BlobFactory.set_rng), making
            # realizations reproducible.
            self.t_drain = t_drain
            self.rng = np.random.default_rng(seed)

        def sample_blobs(
            self,
            Ly: float,
            T: float,
            num_blobs: int,
            blob_shape: AbstractBlobShape,
        ) -> list[Blob]:

            # set custom parameter distributions
            amp = np.linspace(0.01, 1, num=num_blobs)
            width = np.linspace(0.01, 1, num=num_blobs)
            vx = np.linspace(0.01, 1, num=num_blobs)
            vy = np.linspace(0.01, 1, num=num_blobs)

            posx = np.zeros(num_blobs)
            posy = self.rng.uniform(low=0.0, high=Ly, size=num_blobs)
            t_init = self.rng.uniform(low=0, high=T, size=num_blobs)

            # sort blobs by _t_init
            t_init = np.sort(t_init)

            return [
                Blob(
                    blob_id=i,
                    blob_shape=blob_shape,
                    amplitude=amp[i],
                    width_p=width[i],
                    width_s=width[i],
                    v_x=vx[i],
                    v_y=vy[i],
                    pos_x0=posx[i],
                    pos_y0=posy[i],
                    t_init=t_init[i],
                    t_drain=self.t_drain,
                )
                for i in range(num_blobs)
            ]

        def is_one_dimensional(self) -> bool:
            return False

    bf = CustomBlobFactory(t_drain=2)
    tmp = Model(
        geometry=Geometry(Nx=10, Ny=10, Lx=2, Ly=2, dt=0.1, T=10, periodic_y=True),
        num_blobs=10,
        blob_factory=bf,
    )

    ds = tmp.make_realization()

By assigning an array like to the variables amp, width, vx, vy, posx, posy and t_init we can exactly define every single blob parameter of every single blob.

Note

When using CustomBlobFactory it is your responsibility to make sure all blob variables have the correct dimensions. Also, if you wish to normalize the parameters you have to do this manually.

Stationarity and burn-in

DefaultBlobFactory samples blob arrival times uniformly in [0, T), the same window the output is computed on. Since no blobs have arrived before t = 0, the first part of the realization is a transient in which the mean density is still building up towards its stationary value. A common workaround is to discard an initial time slice of the output, but this wastes computed data.

Instead, sample the arrival times on [-burn_in, T) while keeping the output grid on [0, T): blobs born at negative times have already propagated into the domain when the output starts, so the realization is stationary from t = 0. Blob t_init values may be negative and blobs that never reach the domain within the output window are handled (and, with the default speed_up, skipped cheaply):

    from blobmodel import Blob, CallableBlobFactory, Geometry, Model
    import numpy as np

    T = 10
    burn_in = 5  # roughly a few blob transit/drain times

    def blob_getter(rng: np.random.Generator) -> Blob:
        # Arrivals sampled on [-burn_in, T): blobs born before t = 0 have
        # already propagated into the domain when the output starts.
        return Blob(
            amplitude=rng.exponential(),
            pos_y0=rng.uniform(0, 10),
            t_init=rng.uniform(-burn_in, T),
        )

    bm = Model(
        geometry=Geometry(Nx=10, Ny=10, Lx=10, Ly=10, dt=0.1, T=T),
        # scale num_blobs with T + burn_in to keep the arrival rate fixed
        num_blobs=int(30 * (T + burn_in) / T),
        blob_factory=CallableBlobFactory(blob_getter, seed=42),
    )
    ds = bm.make_realization()

Note that num_blobs should be scaled by (T + burn_in) / T to keep the arrival rate — and thus the stationary mean density — unchanged.