Skip to content

Commit dea1ed7

Browse files
refactor highlight function and curve modification
1 parent f50da75 commit dea1ed7

File tree

1 file changed

+72
-71
lines changed

1 file changed

+72
-71
lines changed

ephys/classes/plot/plot_trace.py

Lines changed: 72 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,6 @@ class TracePlotPyQt(TracePlot):
265265
def __init__(self, trace: Trace, backend: str = "pyqt", **kwargs) -> None:
266266
super().__init__(trace=trace, backend=backend, **kwargs)
267267
self.win = pg.GraphicsLayoutWidget(show=self.params.show, title="Trace Plot")
268-
self.highlight: dict[str, int | None | QColor] = {
269-
"sweep_index": None,
270-
"color": QColor(),
271-
}
272-
self.highlight_trace = []
273-
self.highlight_trace_dict = {"channel": None, "sweep": None}
274268
self.flat_sweep_index_start = np.array([])
275269
self.flat_sweep_index_end = np.array([])
276270

@@ -509,9 +503,6 @@ def update_theme(self, theme: str, **kwargs) -> None:
509503
)
510504
)
511505

512-
# reset highlights
513-
self.highlight["sweep_index"] = None
514-
self.highlight["color"] = QColor()
515506
window_items = self.handle_windows()
516507
if self.trace.window is not None:
517508
# Update existing regions with new theme colors
@@ -534,79 +525,89 @@ def update_theme(self, theme: str, **kwargs) -> None:
534525
color=self.params.axis_color,
535526
text=plot_item.getAxis("bottom").labelText,
536527
)
537-
item_list = plot_item.listDataItems()
538-
for i, curve in enumerate(item_list):
539-
curve.setPen(
540-
color=utils.color_picker_qcolor(
541-
length=len(item_list),
542-
index=i,
543-
alpha=self.params.alpha,
544-
color=self.params.color,
545-
)
546-
)
528+
self._set_curve_pen(plot_item.listDataItems())
547529

548530
def sweep_highlight(
549531
self, sweep_index: int | None = None, color="red", alpha=1, width=2
550532
) -> None:
533+
"""
534+
Highlights a specific sweep in the plot by adjusting the pen properties of the curves.
535+
This method dims all existing curves to a specified alpha value (default 0.1) and highlights
536+
the selected sweep by changing its color, alpha, and width. If a highlight curve for the
537+
specified sweep index already exists, it updates its data and pen. Otherwise, it creates a new
538+
highlight curve for the given sweep index. If no sweep is highlighted, it restores the original
539+
alpha values and removes any highlight curves.
540+
Args:
541+
sweep_index (int | None): The index of the sweep to highlight. If None, removes highlight.
542+
color (str, optional): The color to use for highlighting. Defaults to "red".
543+
alpha (float, optional): The alpha (transparency) value for the highlight. Defaults to 1.
544+
width (int, optional): The width of the highlight pen. Defaults to 2.
545+
Returns:
546+
None
547+
"""
548+
549+
pen_opts = pg.mkPen(color=color, alpha=alpha, width=width)
550+
plot_items = [
551+
plot_item
552+
for plot_item in self.win.items()
553+
if isinstance(plot_item, pg.PlotItem)
554+
]
555+
551556
highlight_exists = False
552557
if self.params.alpha != 0.1:
553-
for plot_item in self.win.items():
554-
if isinstance(plot_item, pg.PlotItem):
555-
item_list = plot_item.listDataItems()
556-
for i, curve in enumerate(item_list):
557-
if curve.opts["pen"].color().alphaF() != 0.1:
558-
curve.setPen(
559-
color=utils.color_picker_qcolor(
560-
length=len(item_list),
561-
index=i,
562-
alpha=0.1,
563-
color=self.params.color,
564-
)
558+
for plot_item in plot_items:
559+
self._set_curve_pen(plot_item.listDataItems(), alpha=0.1)
560+
561+
for plot_item in plot_items:
562+
for sweep in plot_item.listDataItems():
563+
if isinstance(sweep, HighlightCurve):
564+
sweep.update_data(
565+
sweep_index=sweep_index,
566+
)
567+
sweep.setPen(
568+
pen=pen_opts,
569+
)
570+
highlight_exists = sweep.sweep_index is not None
571+
572+
if not highlight_exists and isinstance(sweep_index, int):
573+
for plot_item in plot_items:
574+
for sweep in plot_item.listDataItems():
575+
if isinstance(sweep, TraceCurve):
576+
new_highlight: HighlightCurve | None = (
577+
sweep.return_sweep_highlight(
578+
sweep_index=sweep_index,
579+
pen=pen_opts,
565580
)
566-
for item in self.win.items():
567-
if isinstance(item, pg.PlotItem):
568-
item_list = item.items
569-
for sweep in item_list:
570-
if isinstance(sweep, HighlightCurve):
571-
sweep.update_data(
572-
sweep_index=sweep_index,
573581
)
574-
sweep.setPen(
575-
pg.mkPen(color=color, alpha=alpha, width=width),
576-
)
577-
if sweep.sweep_index is None:
578-
highlight_exists = False
579-
else:
582+
if new_highlight is not None:
583+
plot_item.addItem(new_highlight)
580584
highlight_exists = True
581-
if not highlight_exists and isinstance(sweep_index, int):
582-
for item in self.win.items():
583-
if isinstance(item, pg.PlotItem):
584-
item_list = item.items
585-
for sweep in item_list:
586-
if isinstance(sweep, TraceCurve):
587-
new_highlight: HighlightCurve | None = (
588-
sweep.return_sweep_highlight(
589-
sweep_index=sweep_index,
590-
pen=pg.mkPen(color, alpha=alpha, width=width),
591-
)
592-
)
593-
if new_highlight is not None:
594-
item.addItem(new_highlight)
595-
highlight_exists = True
585+
596586
if not highlight_exists:
597-
for plot_item in self.win.items():
598-
if isinstance(plot_item, pg.PlotItem):
599-
item_list = plot_item.listDataItems()
600-
for i, curve in enumerate(item_list):
601-
curve.setPen(
602-
color=utils.color_picker_qcolor(
603-
length=len(item_list),
604-
index=i,
605-
alpha=self.params.alpha,
606-
color=self.params.color,
607-
)
608-
)
587+
for plot_item in plot_items:
588+
item_list = plot_item.listDataItems()
589+
self._set_curve_pen(item_list, alpha=self.params.alpha)
590+
for sweep in item_list:
591+
if isinstance(sweep, HighlightCurve):
592+
plot_item.removeItem(sweep)
593+
594+
def remove_highlight(self) -> None:
595+
self.sweep_highlight(sweep_index=None)
609596

610597
def show(self) -> None:
611598
"""Show the plot window."""
612599
self.win.show()
600+
601+
def _set_curve_pen(self, curves, alpha=None) -> None:
602+
if alpha is None:
603+
alpha = self.params.alpha
604+
for i, curve in enumerate(curves):
605+
if curve.opts["pen"].color().alphaF() != alpha:
606+
curve.setPen(
607+
color=utils.color_picker_qcolor(
608+
length=len(curves),
609+
index=i,
610+
alpha=alpha,
611+
color=self.params.color,
612+
)
613+
)

0 commit comments

Comments
 (0)