diff --git a/miniscope_io/plots/video.py b/miniscope_io/plots/video.py index 541f35b6..042c4296 100644 --- a/miniscope_io/plots/video.py +++ b/miniscope_io/plots/video.py @@ -4,14 +4,20 @@ from typing import List -import matplotlib.pyplot as plt -import numpy as np -from matplotlib import animation -from matplotlib.backend_bases import KeyEvent -from matplotlib.widgets import Button, Slider - from miniscope_io.models.frames import NamedFrame +try: + import matplotlib.pyplot as plt + from matplotlib import animation + from matplotlib.backend_bases import KeyEvent + from matplotlib.widgets import Button, Slider +except ImportError: + plt = None + animation = None + Button = None + Slider = None + KeyEvent = None + class VideoPlotter: """ @@ -31,6 +37,11 @@ def show_video_with_controls(videos: List[NamedFrame], fps: int = 20) -> None: fps : int, optional Frames per second for the video, by default 20 """ + if plt is None: + raise ModuleNotFoundError( + "matplotlib is not a required dependency of miniscope-io, to use it, " + "install it manually or install miniscope-io with `pip install miniscope-io[plot]`" + ) if any(frame.frame_type == "video_list_frame" for frame in videos): raise NotImplementedError("Only single videos or frames are supported for now.") @@ -105,38 +116,3 @@ def animate(i: int) -> None: ) plt.show() - - @staticmethod - def show_video_side_by_side( - fig: plt.Figure, frames: list[np.ndarray], titles: str = None - ) -> None: - """ - Plot a list of frames side by side using matplotlib. - - Parameters - ---------- - fig : plt.Figure - Figure to plot on - frames : list[np.ndarray] - List of frames to plot - titles : str, optional - List of titles for each frame, by default None - - Raises - ------ - ValueError - If the number of frames and titles do not match - """ - num_frames = len(frames) - plt.clf() # Clear current figure - - for i, frame in enumerate(frames): - plt.subplot(1, num_frames, i + 1) - plt.imshow(frame, cmap="gray") - if titles: - plt.title(titles[i]) - - plt.axis("off") # Turn off axis labels - - plt.tight_layout() - fig.canvas.draw() diff --git a/miniscope_io/process/video.py b/miniscope_io/process/video.py index c12e39a0..759d43e8 100644 --- a/miniscope_io/process/video.py +++ b/miniscope_io/process/video.py @@ -6,7 +6,6 @@ from typing import Tuple import cv2 -import matplotlib.pyplot as plt import numpy as np from miniscope_io import init_logger @@ -17,6 +16,11 @@ logger = init_logger("video") +try: + import matplotlib.pyplot as plt +except ImportError: + plt = None + class FrameProcessor: """ @@ -190,6 +194,13 @@ def denoise( Process a video file and display the results. Might be useful to define some using environment variables. """ + if plt is None: + raise ModuleNotFoundError( + "matplotlib is not a required dependency of miniscope-io, to use it, " + "install it manually or install miniscope-io with `pip install miniscope-io[plot]`" + ) + fig = plt.figure() + reader = VideoReader(video_path) pathstem = Path(video_path).stem output_dir = Path.cwd() / config.output_dir @@ -207,7 +218,6 @@ def denoise( freq_filtered_frames = [] index = 0 - fig = plt.figure() processor = FrameProcessor( height=reader.height,