Skip to content

Remove several functions and objects from PyMC root namespace#6973

Open
ricardoV94 wants to merge 32 commits intopymc-devs:v6from
ricardoV94:cleanup_namespace
Open

Remove several functions and objects from PyMC root namespace#6973
ricardoV94 wants to merge 32 commits intopymc-devs:v6from
ricardoV94:cleanup_namespace

Conversation

@ricardoV94
Copy link
Copy Markdown
Member

@ricardoV94 ricardoV94 commented Oct 26, 2023

  • All arviz plots must be retrieved from pymc.plots.foo
  • All arviz stats must be retrieved from pymc.stats.foo
  • Removed many rare or non-user facing objects
  • Remove rare or non-user facing VI objects (I am not familiar enough to know which ones should stay and which ones should not)

Closes #6761

Also

  • Move all custom Exceptions to pymc.exceptions.py

The following script prints 174 after and 272 before this PR:

import pymc
from types import ModuleType
len([a for a in dir(pymc) if not isinstance(getattr(pymc, a), ModuleType) and not a.startswith("__")])

The following entries were removed:

CallableTensor
DictToArrayBijection
ELPDData
GeneratorAdapter
ImputationWarning
IncorrectArgumentsError
SamplingError
ShapeError
ShapeWarning
TraceDirectoryError
_calculate_ics
apply_test_function
autocorr
autocorrplot
autocov
bfmi
bool_types
compare
compareplot
complex_types
compute_log_likelihood
cont_inputs
continuous_types
convert_observed_data
densityplot
discrete_types
drop_warning_stat
energyplot
ess
expand_packed_triangular
float_types
forestplot
generator
guess_scaling
hdi
int_types
invlogit
invprobit
isgenerator
join_nonshared_inputs
kde
kdeplot
logaddexp
logit
logsumexp
loo
loo_pit
make_shared_replacements
make_ufunc
mcse
pairplot
plot_autocorr
plot_bf
plot_bpv
plot_compare
plot_density
plot_dist
plot_dist_comparison
plot_dot
plot_ecdf
plot_elpd
plot_energy
plot_ess
plot_forest
plot_hdi
plot_kde
plot_khat
plot_lm
plot_loo_pit
plot_mcse
plot_pair
plot_parallel
plot_posterior
plot_ppc
plot_rank
plot_separation
plot_trace
plot_ts
plot_violin
predictions_to_inference_data
probit
psislw
r2_samples
r2_score
reloo
rhat
smartfloatX
str_for_dist
str_for_model
str_for_potential_or_deterministic
summary
to_inference_data
trace_cov
traceplot
typefilter
waic
weight_predictions
wrap_xarray_ufunc

📚 Documentation preview 📚: https://pymc--6973.org.readthedocs.build/en/6973/

@ricardoV94 ricardoV94 added maintenance major Include in major changes release notes section request discussion labels Oct 26, 2023
@maresb
Copy link
Copy Markdown
Contributor

maresb commented Oct 26, 2023

Depending on how semver-y people feel, this might be best targeted for PyMC v6.

@ricardoV94
Copy link
Copy Markdown
Member Author

Depending on how semver-y people feel, this might be best targeted for PyMC v6.

I think that will be a strong argument not to go on with these changes

@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 26, 2023

Codecov Report

❌ Patch coverage is 82.05128% with 14 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (v6@5fa428c). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pymc/_deprecations.py 59.09% 9 Missing ⚠️
pymc/variational/opvi.py 66.66% 3 Missing ⚠️
pymc/step_methods/metropolis.py 87.50% 1 Missing ⚠️
pymc/variational/operators.py 75.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##             v6    #6973   +/-   ##
=====================================
  Coverage      ?   91.89%           
=====================================
  Files         ?      125           
  Lines         ?    20258           
  Branches      ?        0           
=====================================
  Hits          ?    18616           
  Misses        ?     1642           
  Partials      ?        0           
Files with missing lines Coverage Δ
pymc/__init__.py 100.00% <100.00%> (ø)
pymc/distributions/multivariate.py 94.24% <100.00%> (ø)
pymc/exceptions.py 100.00% <ø> (ø)
pymc/func_utils.py 88.63% <100.00%> (ø)
pymc/math.py 74.17% <100.00%> (ø)
pymc/sampling/__init__.py 100.00% <100.00%> (ø)
pymc/sampling/jax.py 93.47% <ø> (ø)
pymc/sampling/mcmc.py 92.03% <100.00%> (ø)
pymc/step_methods/__init__.py 100.00% <100.00%> (ø)
pymc/tuning/__init__.py 100.00% <100.00%> (ø)
... and 8 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ColCarroll
Copy link
Copy Markdown
Member

I'll put in a vote against this -- there are enough pymc scripts and libraries out there that don't follow library updates very closely, but would like to continue to benefit from better sampling routines by having liberal version specifications.

Probably the right way would be to use some deprecations library that issued a warning for ~a year, and provided a copy/paste upgrade path. I'm not sure the benefits (tidier namespace? are there others?) outweigh the work and downsides.

Comment thread pymc/plots/__init__.py Outdated
Comment on lines -34 to -56
def alias_deprecation(func, alias: str):
original = func.__name__

@functools.wraps(func)
def wrapped(*args, **kwargs):
raise FutureWarning(
f"The function `{alias}` from PyMC was an alias for `{original}` from ArviZ. "
"It was removed in PyMC 4.0. "
f"Switch to `pymc.{original}` or `arviz.{original}`."
)

return wrapped


# Aliases of ArviZ functions
autocorrplot = alias_deprecation(az.plot_autocorr, alias="autocorrplot")
forestplot = alias_deprecation(az.plot_forest, alias="forestplot")
kdeplot = alias_deprecation(az.plot_kde, alias="kdeplot")
energyplot = alias_deprecation(az.plot_energy, alias="energyplot")
densityplot = alias_deprecation(az.plot_density, alias="densityplot")
pairplot = alias_deprecation(az.plot_pair, alias="pairplot")
traceplot = alias_deprecation(az.plot_trace, alias="traceplot")
compareplot = alias_deprecation(az.plot_compare, alias="compareplot")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like this has already been issuing deprecation warnings. Was this working and warning against using everything slated to be removed since v4?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was just for utilities whose names have changed. Those could be safely removed by now yes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, then I agree with @ColCarroll that we should definitely deprecate all the other stuff well in advance of removing it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also why remove the deprecation warning? In case you want to clear up the namespace then you could refactor it into something like https://peps.python.org/pep-0562/

Copy link
Copy Markdown
Member Author

@ricardoV94 ricardoV94 Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this deprecation warning because I removed the objects that were being deprecated as well

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These one specifically were deprecated since v4, seems safe no?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't there still lots of people who use PyMC3 because of the name recognition and all the SEO? Seems pretty low-effort to provide explicit instructions for them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds silly. This will not be the thing that people switching from v3 to v5 will find challenging

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, I agree it's silly in the case of these ArviZ warnings, but you could do something similar to provide a transition period for the rest of the stuff. Proof-of-concept: ricardoV94#4

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I am going to try and do that!

@twiecki
Copy link
Copy Markdown
Member

twiecki commented Oct 26, 2023

I'm in favor. Cruft accumulates in any code base and it requires consistent effort to stem the tide, which this attempts. I don't think many people are using these functions and those can easily adjust their imports. In a version or two we'll all have forgotten about it, just like with aesara and pytensor.

@ricardoV94 ricardoV94 added this to the v6 milestone Mar 23, 2026
@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@jessegrabowski jessegrabowski changed the base branch from main to v6 April 23, 2026 04:18
@github-actions github-actions Bot added docs hackathon Suitable for hackathon labels Apr 23, 2026
@jessegrabowski
Copy link
Copy Markdown
Member

re-hydrated this PR. Commits are small and focused so if we want to argue about them we can, and it's easy to drop one. Number of items in the main namespace drops from 305 -> 143.

@jessegrabowski jessegrabowski marked this pull request as ready for review April 23, 2026 04:21
@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Apr 23, 2026

Comment thread docs/source/api.rst Outdated
Comment thread docs/source/api.rst Outdated
@ricardoV94
Copy link
Copy Markdown
Member Author

let's add a test to avoid accidental root creep like we did in pymc-devs/pytensor#1863

Comment thread docs/source/api/misc.rst
Comment thread docs/source/api/math.rst Outdated
Comment thread pymc/tuning/starting.py
@jessegrabowski
Copy link
Copy Markdown
Member

let's add a test to avoid accidental root creep like we did in pymc-devs/pytensor#1863

Added, lmk what you think.

Copy link
Copy Markdown
Member Author

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add a deprecation getter to help with the transition?

Comment thread docs/source/api/misc.rst
@jessegrabowski
Copy link
Copy Markdown
Member

Do we want to add a deprecation getter to help with the transition?

I guess if we're being responsible we should. Can we give a firm cut-over date in the warning so it doesn't linger around for 2 years?

@ricardoV94
Copy link
Copy Markdown
Member Author

Can we give a firm cut-over date in the warning so it doesn't linger around for 2 years?

Sure

@jessegrabowski
Copy link
Copy Markdown
Member

I added the getter in _deprecation.py. The warning says we'll delete the forwarding in the first release of 2027, which gives everyone plenty of time.

Comment thread pymc/sampling/mcmc.py Outdated
@ricardoV94
Copy link
Copy Markdown
Member Author

Looks great @jessegrabowski just some standing minor comments from me and @aloctavodia that you haven't incorporated (and something I spotted now)

Copy link
Copy Markdown
Member

@aloctavodia aloctavodia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a couple more comments not directly related to the aim of this PR. So happy if they are solved in this PR or a separate one.

Comment thread docs/source/api/stats.rst Outdated
Comment thread docs/source/api/stats.rst Outdated
Comment thread docs/source/api.rst Outdated
Comment thread docs/source/api.rst Outdated
@ricardoV94
Copy link
Copy Markdown
Member Author

@aloctavodia tried to address your comments above

@ricardoV94 ricardoV94 requested a review from OriolAbril April 28, 2026 09:10
@aloctavodia
Copy link
Copy Markdown
Member

Thanks, LGTM!

@jessegrabowski
Copy link
Copy Markdown
Member

is anything left to be done here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs hackathon Suitable for hackathon maintenance major Include in major changes release notes section request discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove some math utilities from the PyMC root scope?

6 participants