Skip to content

Crash on full infinites values at warmup #60

@SBouchard01

Description

@SBouchard01

If the first sampling of logl values is full of np.inf during the warmup, the code will crash at line 461 as finite_idx will be empty.

The error can be reproduced easily with the following snippet (or by setting a log_likelihood constant at -np.inf):

import numpy as np
logl = np.asarray([np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf])
x = np.arange(len(logl)) # Technically logl is sampled from x but this example is just here to test logl

inf_logl_mask = np.isinf(logl)
if np.any(inf_logl_mask):
    all_idx = np.arange(len(x))
    infinite_idx = all_idx[inf_logl_mask]
    finite_idx = all_idx[~inf_logl_mask]
    idx = np.random.choice(finite_idx, size=len(infinite_idx), replace=True)

Somehow, calling Sampler.sample_prior(Sampler.n_prior) before run() seems to mitigate the issue a bit (I think this might be because the state of numpy.random changes when the sampling is called?)

The error raised

File "lib/python3.12/site-packages/pocomc/sampler.py", line 461, in run
    idx = np.random.choice(finite_idx, size=len(infinite_idx), replace=True)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "numpy/random/mtrand.pyx", line 970, in numpy.random.mtrand.RandomState.choice
ValueError: 'a' cannot be empty unless no samples are taken

Suggestions of solutions

Either handle the error to clarify the reason of the crash, or re-create a sampling until some values are non-infinite (with a fixed number of tries - then raise an error)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions