importtypingimporterrrimportnumpyasnpimportscipy.stats.distributionsas_distributionsfrom..importconfigfrom..exceptionsimportDistributionCastErrorfrom.importtypesiftyping.TYPE_CHECKING:# pragma: nocoverfrom..coreimportScaffold# Scan the scipy distributions module for all distribution names. Ignore `_gen` which are# the factory functions for the distribution classes. `rvs` is a duck type check._available_distributions=[dford,vin_distributions.__dict__.items()ifhasattr(v,"rvs")andnotd.endswith("_gen")]_available_distributions.append("constant")
[docs]@config.nodeclassDistribution:scaffold:"Scaffold"distribution:str=config.attr(type=types.in_(_available_distributions),required=True)""" Name of the scipy.stats distribution function. """parameters:dict[str,typing.Any]=config.catch_all(type=types.any_())""" Parameters to pass to the distribution. """def__init__(self,**kwargs):ifself.distribution=="constant":self._distr=_ConstantDistribution(self.parameters["constant"])returntry:self._distr=getattr(_distributions,self.distribution)(**self.parameters)exceptExceptionase:errr.wrap(DistributionCastError,e,prepend=f"Can't cast to '{self.distribution}': ",)
[docs]defdraw(self,n,rng:"np.random.Generator | None"=None):""" Draw n random samples from the distribution. Drawn through the distribution's own inverse CDF (:meth:`~scipy.stats.rv_continuous.ppf`) rather than :meth:`~scipy.stats.rv_continuous.rvs`, on uniform draws taken from `rng` itself rather than handed to scipy as a ``random_state``. `rvs` would have put scipy in charge of validating `rng`, and scipy accepts only a real :class:`numpy.random.Generator` or :class:`~numpy.random.RandomState`, checked by ``isinstance`` rather than by which methods it has -- which a generator kind with nothing of numpy's behind it (one backed by ``bsb_native``'s Rust, say) is not, and cannot be made to satisfy short of wrapping a real numpy bit generator around it just to pass the check. Drawing the uniforms ourselves needs only ``rng.random(n)``, which is what every generator kind already has to answer -- unlike the rest of the framework, which also reaches for ``.integers()`` and ``.choice()``, so `rng` is typed here more strictly than it is actually required to be. This is not free: inverse-CDF sampling is scipy's generic fallback, not the specialised, faster and more precise sampler some distributions ship their own version of (the normal distribution among them), so a draw here costs more and, in the extreme tails, resolves less precisely than `rvs` would have given the same generator. A discrete distribution's draw is also `ppf`'s own return type, `float64`, not the `int` `rvs` gives -- cast it where an integer is needed, as the built-in call sites already do. :param n: Number of samples to draw. :param rng: Generator to draw from, keyed the way a caller drawing as part of a reconstruction already has one, e.g. from :meth:`RngConsumer.get_rng <bsb.rng.RngConsumer.get_rng>`. Left unset, falls back to an unseeded draw. """quantiles=rng.random(n)ifrngisnotNoneelsenp.random.random(n)returnself._distr.ppf(quantiles)
[docs]defdefinition_interval(self,epsilon=0):""" Returns the `epsilon` and 1 - `epsilon` values of the distribution Percent point function. :param float epsilon: ratio of the interval to ignore """ifepsilon<0orepsilon>1:raiseValueError("Epsilon must be between 0 and 1")returnself._distr.ppf(epsilon),self._distr.ppf(1-epsilon)
[docs]defcdf(self,value):""" Returns the result of the cumulative distribution function for `value` :param float value: value to evaluate """returnself._distr.cdf(value)
[docs]defsf(self,value):""" Returns the result of the Survival function for `value` :param float value: value to evaluate """returnself._distr.sf(value)
def__getattr__(self,attr):if"_distr"notinself.__dict__:raiseAttributeError("No underlying _distr found for distribution node.")returngetattr(self._distr,attr)
class_ConstantDistribution:def__init__(self,const):self.const=constdefrvs(self,size):returnnp.full(size,self.const,dtype=type(self.const))defppf(self,q):# A constant's inverse CDF is itself at every quantile, so `draw` -- and# `definition_interval`, which was already calling this unconditionally --# both work for a `constant` distribution without special-casing it.returnnp.full(np.shape(q),self.const,dtype=type(self.const))