Skip to content

Commit 41432b6

Browse files
alexarjeclaude
andcommitted
Lazy-load heavy deps to halve import time (~1.5s → ~0.7s)
Move scipy.signal (medfilt2d / signal) and scipy.stats.entropy out of module-top imports in _filter, _impacts, _ssm and _flow, and defer IPython.display in _show, to function-local imports. These were pulled in eagerly during `import musicalgestures` (scipy.signal ~1s, IPython ~0.2s) but are only needed when the relevant methods run. librosa is already lazily loaded upstream. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent aaa97a0 commit 41432b6

5 files changed

Lines changed: 6 additions & 9 deletions

File tree

musicalgestures/_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from scipy.signal import medfilt2d
21
import cv2
32
import numpy as np
43
import matplotlib
@@ -19,6 +18,7 @@ def filter_frame(motion_frame, filtertype, threshold, kernel_size):
1918
np.array(uint8): The filtered frame.
2019
"""
2120

21+
from scipy.signal import medfilt2d # lazy import: keeps scipy.signal out of startup
2222
if filtertype.lower() == 'regular':
2323
motion_frame = (motion_frame > threshold*255)*motion_frame
2424
motion_frame = medfilt2d(motion_frame, kernel_size)

musicalgestures/_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import math
55
import weakref
66
import matplotlib.pyplot as plt
7-
from scipy.stats import entropy
87

98
import musicalgestures
109
from musicalgestures._utils import MgFigure, extract_wav, embed_audio_in_video, MgProgressbar, convert_to_avi, generate_outfilename, ffmpeg_cmd, get_cuda_device_count, cuda_unavailable_reason
@@ -231,6 +230,7 @@ def dense(
231230

232231
time = np.linspace(0, len(xvel)/fps, len(xvel))
233232
acceleration = self.get_acceleration(xvel, fps)
233+
from scipy.stats import entropy # lazy import: keeps scipy.stats out of startup
234234
acceleration_entropy = entropy(acceleration)
235235

236236
ax.plot(time, xvel, label=f'Average acceleration: {round(np.mean(acceleration),3)} m/s\nEntropy of acceleration: {round(acceleration_entropy,3)}')

musicalgestures/_impacts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import numpy as np
44
from numba import jit
5-
from scipy.signal import medfilt2d
65
import matplotlib.pyplot as plt
76
import musicalgestures
87
from musicalgestures._directograms import directogram
@@ -11,6 +10,7 @@
1110

1211
def impact_envelope(directogram, kernel_size=5):
1312

13+
from scipy.signal import medfilt2d # lazy import: keeps scipy.signal out of startup
1414
# Apply a median filter to the directogram using a local window-size given by kernel_size
1515
filtered_directogram = medfilt2d(directogram, kernel_size)
1616
flux = np.zeros(directogram.shape)

musicalgestures/_show.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# import cv2
22
# import numpy as np
33
import os
4-
from IPython.display import Image, display, HTML
5-
# try:
6-
from IPython.display import Video
7-
# except:
8-
# from IPython.core.display import Video
94
from base64 import b64encode
105
import musicalgestures
116

@@ -24,6 +19,8 @@ def mg_show(self, filename=None, key=None, mode='windowed', window_width=640, wi
2419
window_title (str, optional): The title of the window. If None, the title of the window will be the file name. Defaults to None.
2520
ipython_kwargs (dict, optional): Additional arguments for IPython.display.Image or IPython.display.Video. Defaults to None.
2621
"""
22+
# Lazy import: keeps IPython out of `import musicalgestures` startup.
23+
from IPython.display import Image, display, HTML, Video
2724

2825
def show(file, width=640, height=480, mode='windowed', title='Untitled', parent=None, **ipython_kwargs):
2926
"""

musicalgestures/_ssm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import cv2
33
import numpy as np
4-
from scipy import signal
54
import matplotlib as mpl
65
import matplotlib.pyplot as plt
76
import matplotlib.gridspec as gridspec
@@ -38,6 +37,7 @@ def inside(x, pos):
3837

3938
formatter = FuncFormatter(inside)
4039

40+
from scipy import signal # lazy import: keeps scipy.signal out of startup
4141
filt_kernel = np.expand_dims(signal.get_window(w_type, filt_len), axis=0)
4242
X_smooth = signal.convolve(X, filt_kernel, mode='same') / filt_len
4343
X_smooth = X_smooth[:, ::down_sampling]

0 commit comments

Comments
 (0)