Skip to content

Move image.py out of analysis so importing lensboy doesn't pull in matplotlib#33

Merged
Robertleoj merged 1 commit into
Robertleoj:mainfrom
rossng:avoid-matplotlib-import-on-core
Jun 3, 2026
Merged

Move image.py out of analysis so importing lensboy doesn't pull in matplotlib#33
Robertleoj merged 1 commit into
Robertleoj:mainfrom
rossng:avoid-matplotlib-import-on-core

Conversation

@rossng
Copy link
Copy Markdown
Contributor

@rossng rossng commented Jun 3, 2026

Problem

import lensboy transitively imported matplotlib and scipy. The core path re-exports extract_frames_from_charuco, and charuco.py imported to_gray from lensboy.analysis.image. Importing that submodule forces lensboy/analysis/__init__.py to run, which eagerly imports plots.py → matplotlib. So every core import paid the full analysis-stack cost, even though to_gray/to_color are generic uint8 image helpers with no dependency on analysis.

Fix

Move image.py from lensboy/analysis/ to top-level lensboy/, where it belongs (used by both core charuco.py and analysis/plots.py), and update the two import sites. Core code no longer touches the optional analysis subpackage at load time.

Impact

Cold import lensboy, measured over 12 fresh interpreters (arm64):

median matplotlib modules scipy modules
before 382 ms 94 175
after 73 ms 0 0

~5× faster import; no matplotlib/scipy loaded unless lensboy.analysis is explicitly used.

🤖 Generated with Claude Code

charuco.py (re-exported on the core path) imported to_gray from
lensboy.analysis.image. Importing that submodule runs
lensboy/analysis/__init__.py, which eagerly imports plots.py -> matplotlib,
so every `import lensboy` transitively pulled in matplotlib and scipy.

to_gray/to_color are generic uint8 image helpers with no dependency on
analysis, so move image.py to the top-level lensboy package and update the
two import sites. Core code no longer touches the optional analysis
subpackage at load time.

Cold `import lensboy` (median of 12 fresh interpreters, arm64):
before 382 ms (94 matplotlib + 175 scipy modules) -> after 73 ms (0/0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Robertleoj Robertleoj merged commit 64f0766 into Robertleoj:main Jun 3, 2026
4 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.

2 participants