|
| 1 | +import importlib |
| 2 | +import inspect |
| 3 | +import operator |
| 4 | + |
| 5 | +import pymc as pm |
| 6 | + |
| 7 | + |
| 8 | +class InferenceMethods: |
| 9 | + """Obtain a dictionary of available inference methods for Bambi |
| 10 | + models and or the default kwargs of each inference method. |
| 11 | + """ |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + # In order to access inference methods, a bayeux model must be initialized |
| 15 | + self.bayeux_model = bayeux_model() |
| 16 | + self.bayeux_methods = self._get_bayeux_methods(bayeux_model()) |
| 17 | + self.pymc_methods = self._pymc_methods() |
| 18 | + |
| 19 | + def _get_bayeux_methods(self, model): |
| 20 | + # Bambi only supports bayeux MCMC methods |
| 21 | + mcmc_methods = model.methods.get("mcmc") |
| 22 | + return {"mcmc": mcmc_methods} |
| 23 | + |
| 24 | + def _pymc_methods(self): |
| 25 | + return {"mcmc": ["mcmc"], "vi": ["vi"]} |
| 26 | + |
| 27 | + def _remove_parameters(self, fn_signature_dict): |
| 28 | + # Remove 'pm.sample' parameters that are irrelevant for Bambi users |
| 29 | + params_to_remove = [ |
| 30 | + "progressbar", |
| 31 | + "progressbar_theme", |
| 32 | + "var_names", |
| 33 | + "nuts_sampler", |
| 34 | + "return_inferencedata", |
| 35 | + "idata_kwargs", |
| 36 | + "callback", |
| 37 | + "mp_ctx", |
| 38 | + "model", |
| 39 | + ] |
| 40 | + return {k: v for k, v in fn_signature_dict.items() if k not in params_to_remove} |
| 41 | + |
| 42 | + def get_kwargs(self, method): |
| 43 | + """Get the default kwargs for a given inference method. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + method : str |
| 48 | + The name of the inference method. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + dict |
| 53 | + The default kwargs for the inference method. |
| 54 | + """ |
| 55 | + if method in self.bayeux_methods.get("mcmc"): |
| 56 | + bx_method = operator.attrgetter(method)( |
| 57 | + self.bayeux_model.mcmc # pylint: disable=no-member |
| 58 | + ) |
| 59 | + return bx_method.get_kwargs() |
| 60 | + elif method in self.pymc_methods.get("mcmc"): |
| 61 | + return self._remove_parameters(get_default_signature(pm.sample)) |
| 62 | + elif method in self.pymc_methods.get("vi"): |
| 63 | + return get_default_signature(pm.ADVI.fit) |
| 64 | + else: |
| 65 | + raise ValueError( |
| 66 | + f"Inference method '{method}' not found in the list of available" |
| 67 | + " methods. Use `bmb.inference_methods.names` to list the available methods." |
| 68 | + ) |
| 69 | + |
| 70 | + @property |
| 71 | + def names(self): |
| 72 | + return {"pymc": self.pymc_methods, "bayeux": self.bayeux_methods} |
| 73 | + |
| 74 | + |
| 75 | +def bayeux_model(): |
| 76 | + """Dummy bayeux model for obtaining inference methods. |
| 77 | +
|
| 78 | + A dummy model is needed because algorithms are dynamically determined at |
| 79 | + runtime, based on the libraries that are installed. A model can give |
| 80 | + programmatic access to the available algorithms via the `methods` attribute. |
| 81 | +
|
| 82 | + Returns |
| 83 | + ------- |
| 84 | + bayeux.Model |
| 85 | + A dummy model with a simple quadratic likelihood function. |
| 86 | + """ |
| 87 | + if importlib.util.find_spec("bayeux") is None: |
| 88 | + return {"mcmc": []} |
| 89 | + |
| 90 | + import bayeux as bx # pylint: disable=import-outside-toplevel |
| 91 | + |
| 92 | + return bx.Model(lambda x: -(x**2), 0.0) |
| 93 | + |
| 94 | + |
| 95 | +def get_default_signature(fn): |
| 96 | + """Get the default parameter values of a function. |
| 97 | +
|
| 98 | + This function inspects the signature of the provided function and returns |
| 99 | + a dictionary containing the default values of its parameters. |
| 100 | +
|
| 101 | + Parameters |
| 102 | + ---------- |
| 103 | + fn : callable |
| 104 | + The function for which default argument values are to be retrieved. |
| 105 | +
|
| 106 | + Returns |
| 107 | + ------- |
| 108 | + dict |
| 109 | + A dictionary mapping argument names to their default values. |
| 110 | +
|
| 111 | + """ |
| 112 | + defaults = {} |
| 113 | + for key, val in inspect.signature(fn).parameters.items(): |
| 114 | + if val.default is not inspect.Signature.empty: |
| 115 | + defaults[key] = val.default |
| 116 | + return defaults |
| 117 | + |
| 118 | + |
| 119 | +inference_methods = InferenceMethods() |
0 commit comments