Skip to content

Make pandas, scipy, and matplotlib optional dependencies via attempt_import#646

Merged
DLWoodruff merged 4 commits intomainfrom
copilot/clean-up-dependencies
Apr 7, 2026
Merged

Make pandas, scipy, and matplotlib optional dependencies via attempt_import#646
DLWoodruff merged 4 commits intomainfrom
copilot/clean-up-dependencies

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 3, 2026

Several packages used by mpi-sppy (pandas, scipy, matplotlib) were either undeclared or treated as hard requirements, preventing lightweight installs for users who don't need those features.

Changes

Package metadata (pyproject.toml, setup.py)

  • Removed pandas and scipy from required dependencies
  • Added optional extras: [pandas], [scipy], [plot] (matplotlib)

Deferred imports — replaced direct imports with Pyomo's lazy-loading mechanism:

  • pandas: 4 files (rho_utils, wtracker, phtracker, primal_dual_converger)
  • scipy: 5 files (prox_approx, seqsampling, zhat4xhat, mmw_ci, kkt/interface)
  • matplotlib.pyplot: 2 files (phtracker, primal_dual_converger)
# Before
import pandas as pd
import scipy.stats
from scipy.optimize import least_squares
import matplotlib.pyplot as plt

# After
from pyomo.common.dependencies import attempt_import, pandas as pd, scipy
plt, plt_available = attempt_import('matplotlib.pyplot')
# Usage: scipy.stats.t.ppf(...), scipy.optimize.least_squares(...)

The deferred import pattern (already used in the repo for networkx, dill, yaml) allows modules to load without these packages installed; errors are raised only when the optional functionality is actually invoked.

Copilot AI linked an issue Apr 3, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Clean up dependencies to be optional with import guards Make pandas, scipy, and matplotlib optional dependencies via attempt_import Apr 3, 2026
Copilot AI requested a review from bknueven April 3, 2026 17:48
@bknueven
Copy link
Copy Markdown
Collaborator

bknueven commented Apr 3, 2026

@DLWoodruff I'm a bit ambivalent about having scipy as a soft dependency. We don't use it a lot, so it some ways it's not a huge issue. What do you think?

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 3, 2026

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 70.17%. Comparing base (e496071) to head (cdc5927).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
mpisppy/confidence_intervals/zhat4xhat.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #646      +/-   ##
==========================================
+ Coverage   70.15%   70.17%   +0.02%     
==========================================
  Files         153      153              
  Lines       18905    18905              
==========================================
+ Hits        13262    13267       +5     
+ Misses       5643     5638       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DLWoodruff
Copy link
Copy Markdown
Collaborator

@DLWoodruff I'm a bit ambivalent about having scipy as a soft dependency. We don't use it a lot, so it some ways it's not a huge issue. What do you think?

As we do more with the stochastic problem itself (e.g., confidence intervals), we will use it a little more; however, it is hard to argue that a soft dependency really hurts anyone. It makes some sense to have as many dependencies as possible be soft. So I say we merge this.

straight_tests.py exercises mmw which requires scipy.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@DLWoodruff DLWoodruff marked this pull request as ready for review April 7, 2026 16:23
@DLWoodruff DLWoodruff merged commit 35c4f52 into main Apr 7, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clean up dependencies

3 participants