Skip to content

Commit e3374db

Browse files
authored
Double-check existing docstrings + start preparing release (#935)
* Changes * Update more docstrings and update PyMC to 5.26 * Final pass at docstrings * Update index and readme
1 parent ae00df6 commit e3374db

30 files changed

+228
-213
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ If you use Bambi and want to cite it please use
153153

154154
Bambi is a community project and welcomes contributions. Additional information can be found in the [Contributing](https://github.com/bambinos/bambi/blob/main/CONTRIBUTING.md) Readme.
155155

156-
For a list of contributors see the [GitHub contributor](https://github.com/bambinos/bambi/graphs/contributors) page
156+
For a list of contributors see the [GitHub contributor](https://github.com/bambinos/bambi/graphs/contributors) page.
157157

158158
## Donations
159159

@@ -163,6 +163,8 @@ If you want to support Bambi financially, you can [make a donation](https://numf
163163

164164
Bambi wishes to maintain a positive community. Additional details can be found in the [Code of Conduct](https://github.com/bambinos/bambi/blob/main/CODE_OF_CONDUCT.md)
165165

166+
If you have experienced or witnessed any behaviour that violates our Code of Conduct, we encourage you to report it through [this contact form](https://bambinos.github.io/bambi/contact.html).
167+
166168
## License
167169

168170
[MIT License](https://github.com/bambinos/bambi/blob/main/LICENSE)

bambi/__init__.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
from pymc import math
66

7-
from .backend import PyMCModel
8-
from .config import config
9-
from .data import clear_data_home, load_data
10-
from .families import Family, Likelihood, Link
11-
from .formula import Formula
12-
from .models import Model
13-
from .priors import Prior
14-
from . import interpret
7+
from bambi.backend import PyMCModel
8+
from bambi.config import config
9+
from bambi.data import clear_data_home, load_data
10+
from bambi.families import Family, Likelihood, Link
11+
from bambi.formula import Formula
12+
from bambi.models import Model
13+
from bambi.priors import Prior
1514

1615
__version__ = version("bambi")
1716

bambi/data/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Code for loading datasets."""
22

3-
from .datasets import clear_data_home, load_data
3+
from bambi.data.datasets import clear_data_home, load_data
44

55
__all__ = ["clear_data_home", "load_data"]

bambi/data/datasets.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
}
190190

191191

192-
def get_data_home(data_home=None):
192+
def get_data_home(data_home: str | None = None):
193193
"""Return the path of the Bambi data dir.
194194
195195
This folder is used to avoid downloading the data several times.
@@ -201,7 +201,7 @@ def get_data_home(data_home=None):
201201
202202
Parameters
203203
----------
204-
data_home: str
204+
data_home : str or None, optional
205205
The path to Bambi data dir.
206206
"""
207207
if data_home is None:
@@ -218,8 +218,8 @@ def clear_data_home(data_home: str | None = None):
218218
Parameters
219219
----------
220220
data_home: str or None, optional
221-
The path to Bambi data dir. By default a folder named `"bambi_data"` in the user home
222-
folder.
221+
The path to Bambi data dir.
222+
By default a folder named `"bambi_data"` in the user home folder.
223223
"""
224224
data_home = get_data_home(data_home)
225225
shutil.rmtree(data_home)
@@ -250,14 +250,14 @@ def load_data(dataset: str | None = None, data_home: str | None = None):
250250
251251
Parameters
252252
----------
253-
dataset: str
253+
dataset : str or None, optional
254254
Name of dataset to load.
255-
data_home: str, optional
256-
Where to save remote datasets
255+
data_home : str or None, optional
256+
Where to save remote datasets.
257257
258258
Returns
259259
-------
260-
pandas.DataFrame
260+
pd.DataFrame or str
261261
"""
262262
home_dir = get_data_home(data_home=data_home)
263263

@@ -274,19 +274,29 @@ def load_data(dataset: str | None = None, data_home: str | None = None):
274274
f"({datafile.checksum}), file may be corrupted. Run `bambi.clear_data_home()` "
275275
"and try again, or please open an issue."
276276
)
277+
277278
return pd.read_csv(file_path)
278-
else:
279-
if dataset is None:
280-
return _list_datasets(home_dir)
281-
else:
282-
raise ValueError(
283-
f"Dataset {dataset} not found! "
284-
f"The following are available:\n{_list_datasets(home_dir)}"
285-
)
279+
280+
if dataset is None:
281+
return _list_datasets(home_dir)
282+
283+
raise ValueError(
284+
f"Dataset {dataset} not found! " f"The following are available:\n{_list_datasets(home_dir)}"
285+
)
286286

287287

288288
def _list_datasets(home_dir):
289-
"""Get a string representation of all available datasets with descriptions."""
289+
"""Get a string representation of all available datasets with descriptions.
290+
291+
Parameters
292+
----------
293+
home_dir : str
294+
Name of the home directoy.
295+
296+
Returns
297+
-------
298+
str
299+
"""
290300
lines = []
291301
for filename, resource in itertools.chain(DATASETS.items()):
292302
file_path = os.path.join(home_dir, filename)

bambi/defaults/hsgp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""This module contains the default priors for the parameters of different covariance kernels
2-
that can be used in HSGP terms."""
1+
"""Default priors for parameters of covariance kernels in HSGP terms."""
32

43
# fmt: off
54
HSGP_COV_PARAMS_DEFAULT_PRIORS = {

bambi/defaults/utils.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def generate_prior(dist, **kwargs):
1313
1414
Parameters
1515
----------
16-
dist: str, int, float
16+
dist : str, int, float
1717
If a string, it is the name of the prior distribution with default values taken from
1818
`SETTINGS_DISTRIBUTIONS`. If a number, it is a factor used to scale the standard deviation
1919
of the priors generated automatically by Bambi.
@@ -28,14 +28,16 @@ def generate_prior(dist, **kwargs):
2828
Prior
2929
The Prior instance.
3030
"""
31+
32+
if not isinstance(dist, str | int | float):
33+
raise ValueError("'dist' must be the name of a distribution or a numeric value.")
34+
3135
if isinstance(dist, str):
3236
prior = Prior(dist, **SETTINGS_DISTRIBUTIONS[dist])
3337
if kwargs:
3438
prior.update(**{k: generate_prior(v) for k, v in kwargs.items()})
35-
elif isinstance(dist, (int, float)):
36-
prior = dist
3739
else:
38-
raise ValueError("'dist' must be the name of a distribution or a numeric value.")
40+
prior = dist
3941
return prior
4042

4143

@@ -52,7 +54,7 @@ def generate_prior_hsgp(cov_name: str):
5254
5355
Returns
5456
-------
55-
dict[str, Prior]
57+
dict of str to Prior
5658
The priors for the parameters of the covariance function
5759
"""
5860
config = HSGP_COV_PARAMS_DEFAULT_PRIORS[cov_name]
@@ -84,7 +86,7 @@ def get_default_prior(term_type, **kwargs):
8486
Raises
8587
------
8688
ValueError
87-
If `term_type` is not within the values listed above.
89+
If `term_type` is not one of the values listed above.
8890
8991
Returns
9092
-------
@@ -111,13 +113,13 @@ def generate_likelihood(name, params, parent):
111113
112114
Parameters
113115
----------
114-
name: str
116+
name : str
115117
The name of the likelihood function.
116-
args: dict
117-
Indicates the auxiliary parameters and the values for their default priors. The keys are the
118-
names of the parameters and the values are passed to `generate_prior()` to obtain the
118+
params : dict
119+
Indicates the auxiliary parameters and the values for their default priors.
120+
Keys are names of the parameters and values are passed to `generate_prior()` to obtain the
119121
actual instance of `bambi.Prior`.
120-
parent: str
122+
parent : str
121123
The name of the parent parameter. In other words, the name of the mean parameter in the
122124
likelihood function.
123125
@@ -136,13 +138,13 @@ def generate_family(name, likelihood, link, family, default_priors=None):
136138
----------
137139
name : str
138140
The name of the family.
139-
likelihood: bambi.Likelihood
141+
likelihood : bambi.Likelihood
140142
A representation of the likelihood function that corresponds to the family being created.
141143
link : bambi.Link
142144
A representation of the link function that corresponds to the family being created.
143145
family : subclass of bambi.Family
144146
A subclass of bambi.Family that generates the instance of the desired family.
145-
default_priors : dict
147+
default_priors : dict or None, optional
146148
Default priors for non-parent parameters.
147149
148150
Returns

bambi/families/family.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Dict, Union
2-
31
import numpy as np
42
import pymc as pm
53
import xarray as xr
@@ -17,7 +15,7 @@ class Family:
1715
The name of the family. It can be any string.
1816
likelihood : Likelihood
1917
A `bambi.families.Likelihood` instance specifying the model likelihood function.
20-
link : str or dict[str, str or Link]
18+
link : str or dict of str to (str or Link)
2119
The link function that's used for every parameter in the likelihood function.
2220
Keys are the names of the parameters and values are the link functions.
2321
These can be a `str` with a name or a `bambi.families.Link` instance.
@@ -60,7 +58,7 @@ class Family:
6058
"tan_2",
6159
]
6260

63-
def __init__(self, name, likelihood, link: Union[str, Dict[str, Union[str, Link]]]):
61+
def __init__(self, name, likelihood, link: str | dict[str, str | Link]):
6462
self.name = name
6563
self.likelihood = likelihood
6664
self.link = link
@@ -143,7 +141,7 @@ def posterior_predictive(self, model, posterior, random_seed, **kwargs):
143141
the parameters that allow to derive them.
144142
random_seed : int, RandomState or Generator, optional
145143
Seed for the random number generator.
146-
kwargs :
144+
kwargs : dict
147145
Parameters that are used to get draws but do not appear in the posterior object or
148146
other configuration parameters.
149147
For instance, the 'n' in binomial models and multinomial models.
@@ -184,7 +182,10 @@ def log_likelihood(self, model, posterior, data, **kwargs):
184182
The xarray dataset that contains the draws for all the parameters in the posterior.
185183
It must contain the parameters that are needed in the distribution of the response, or
186184
the parameters that allow to derive them.
187-
kwargs :
185+
data : pd.DataFrame or None
186+
A data frame with values for the predictors and the response on which the model's
187+
log-likelihood function is evaluated. If omitted, the original dataset is used.
188+
kwargs : dict
188189
Parameters that are used to get draws but do not appear in the posterior object or
189190
other configuration parameters.
190191
For instance, the 'n' in binomial models and multinomial models.

bambi/families/likelihood.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class Likelihood:
3535
----------
3636
name : str
3737
Name of the likelihood function. Must be a valid PyMC distribution name.
38-
params : Sequence[str]
38+
params : sequence of str or None, optional
3939
The name of the parameters the likelihood function accepts.
40-
parent : str
40+
parent : str or None, optional
4141
Optional specification of the name of the mean parameter in the likelihood.
4242
This is the parameter whose transformation is modeled by the linear predictor.
43-
dist : pymc.Distribution or callable
43+
dist : pymc.Distribution or callable or None, optional
4444
Optional custom PyMC distribution that will be used to compute the likelihood.
4545
4646
Notes

bambi/families/link.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ class Link:
131131
The name of the link function. If it is a known name, it's not necessary to pass any
132132
other arguments because functions are already defined internally. If not known, all of
133133
`link`, `linkinv` and `linkinv_backend` must be specified.
134-
link : function
134+
link : function or None, optional
135135
A function that maps the response to the linear predictor. Known as the :math:`g` function
136136
in GLM jargon. Does not need to be specified when `name` is a known name.
137-
linkinv : function
137+
linkinv : function or None, optional
138138
A function that maps the linear predictor to the response. Known as the :math:`g^{-1}`
139139
function in GLM jargon. Does not need to be specified when `name` is a known name.
140-
linkinv_backend : function
140+
linkinv_backend : function or None, optional
141141
Same than `linkinv` but must be something that works with PyMC backend (i.e. it must
142142
work with PyTensor tensors). Does not need to be specified when `name` is a known
143143
name.

bambi/formula.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Formula:
1010
1111
Allows to describe a model with multiple formulas. The first formula describes the response
1212
variable and its predictors. The following formulas describe predictors for other parameters
13-
of the likelihood function, allowing distributional models.
13+
of the response distribution, allowing distributional models.
1414
1515
Parameters
1616
----------
@@ -30,12 +30,12 @@ def check_additionals(self, additionals: Sequence[str]):
3030
3131
Parameters
3232
----------
33-
additionals : Sequence[str]
33+
additionals : sequence of str
3434
Model formulas that describe model parameters rather than a response variable.
3535
3636
Returns
3737
-------
38-
additionals : Sequence[str]
38+
additionals : sequence of str
3939
If all formulas match the required format, it returns them.
4040
"""
4141
for additional in additionals:
@@ -63,9 +63,9 @@ def check_additional(self, additional: str):
6363
if response is None:
6464
raise ValueError("Additional formulas must contain a response name.")
6565

66-
# The response is a name, not a function call for example
66+
# The response is a name, not a function call, for example
6767
if not isinstance(response.term.components[0], fm.terms.variable.Variable):
68-
raise ValueError("The response must be a name.")
68+
raise ValueError("The response must be a name")
6969

7070
self.additionals_lhs.append(response.term.name)
7171

@@ -74,7 +74,7 @@ def get_all_formulas(self):
7474
7575
Returns
7676
-------
77-
list
77+
list of str
7878
All the formulas in the instance.
7979
"""
8080
return [self.main] + list(self.additionals)
@@ -91,19 +91,19 @@ def __repr__(self):
9191

9292

9393
def formula_has_intercept(formula: str) -> bool:
94-
"""Determines if a model formula results in a model with intercept."""
94+
"""Determines if a model formula describes a model with an intercept."""
9595
description = fm.model_description(formula)
9696
return any(isinstance(term, fm.terms.Intercept) for term in description.terms)
9797

9898

9999
def check_ordinal_formula(formula: Formula) -> Formula:
100-
"""Check if a supplied formula can be used with an ordinal model
100+
"""Check if a supplied formula can be used with an ordinal model.
101101
102102
Ordinal models have the following constrains (for the moment):
103-
* A single formula must be passed. This is because Bambi does not support
104-
modeling the thresholds as a function of predictors.
105-
* The intercept is omitted. This is to avoid non-identifiability issues
106-
between the intercept and the thresholds.
103+
* A single formula must be passed. This is because Bambi does not support modeling the
104+
thresholds as a function of predictors.
105+
* The intercept is omitted. This is to avoid non-identifiability issues between the intercept
106+
and the thresholds.
107107
"""
108108
if len(formula.additionals) > 0:
109109
raise ValueError("Ordinal families don't accept multiple formulas")

0 commit comments

Comments
 (0)