Skip to content

Commit e33d972

Browse files
author
Vincent Berenz
committed
plotting in terminal score as function of focus
1 parent a5b05d2 commit e33d972

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

nightfocus/camera.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
from __future__ import annotations
44

55
import os
6+
from typing import List, Tuple, Sequence, Optional, Union
7+
from io import StringIO
8+
import numpy as np
9+
import plotext as plt
10+
from rich.console import Console
11+
from rich.table import Table
12+
from loguru import logger
613
from abc import ABC, abstractmethod
714
from io import StringIO
815
from pathlib import Path
@@ -80,10 +87,10 @@ def take_picture(self, focus: Focus) -> Image:
8087

8188
def _log_optimization_history(history: Sequence[Tuple[float, float]]) -> None:
8289
"""
83-
Log the optimization history as a formatted table using Rich.
90+
Log the optimization history as a formatted table and plot using Rich and Plotext.
8491
8592
Args:
86-
history: List of (focus, score) tuples to be displayed in the table
93+
history: List of (focus, score) tuples to be displayed in the table and plot
8794
"""
8895
if not history:
8996
logger.warning("No history data to display")
@@ -115,6 +122,39 @@ def _log_optimization_history(history: Sequence[Tuple[float, float]]) -> None:
115122
# Log the table using loguru
116123
logger.info("\n" + table_output.getvalue())
117124

125+
126+
# Create a terminal plot
127+
try:
128+
# Extract focus and score values
129+
focus_vals = [h[0] for h in history]
130+
scores = [h[1] for h in history]
131+
132+
# Sort by focus for better visualization
133+
sorted_pairs = sorted(zip(focus_vals, scores), key=lambda x: x[0])
134+
focus_vals, scores = zip(*sorted_pairs)
135+
136+
# Create the plot
137+
plt.clear_figure()
138+
plt.plot(focus_vals, scores, marker="dot", label="Score")
139+
plt.title("Focus Optimization History")
140+
plt.xlabel("Focus Value")
141+
plt.ylabel("Score")
142+
plt.grid(True)
143+
144+
# Add markers for all points
145+
plt.scatter(focus_vals, scores, marker="dot")
146+
147+
# Find and highlight the best focus
148+
best_idx = np.argmax(scores)
149+
plt.scatter([focus_vals[best_idx]], [scores[best_idx]], marker="*", color="red")
150+
151+
# Show the plot
152+
logger.info("\nFocus Optimization Plot:")
153+
plt.show()
154+
155+
except Exception as e:
156+
logger.warning(f"Failed to create terminal plot: {str(e)}")
157+
118158

119159
def optimize_focus(
120160
camera: "Camera",

poetry.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ opencv-python = "^4.11.0.86"
2222
pillow = "^11.2.1"
2323
tifffile = "2024.8.30"
2424
multipledispatch = "^1.0.0"
25+
plotext = "^5.2.8"
2526

2627
[tool.poetry.scripts]
2728
nightfocus = "nightfocus.main:cli"

0 commit comments

Comments
 (0)