Skip to content

Commit

Permalink
docs: handle to plt import, check plt inside modules
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sasatani committed Dec 7, 2024
1 parent f0b920b commit 1385223
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 43 deletions.
58 changes: 17 additions & 41 deletions miniscope_io/plots/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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.")
Expand Down Expand Up @@ -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()
14 changes: 12 additions & 2 deletions miniscope_io/process/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +16,11 @@

logger = init_logger("video")

try:
import matplotlib.pyplot as plt
except ImportError:
plt = None


class FrameProcessor:
"""
Expand Down Expand Up @@ -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
Expand All @@ -207,7 +218,6 @@ def denoise(
freq_filtered_frames = []

index = 0
fig = plt.figure()

processor = FrameProcessor(
height=reader.height,
Expand Down

0 comments on commit 1385223

Please sign in to comment.