Skip to content

Commit debdc73

Browse files
authored
Merge pull request #98 from singjc/main
fix/refactor: minor code clean up and fix for annotations in peakmaps
2 parents 24b82d9 + f10cebc commit debdc73

6 files changed

Lines changed: 172 additions & 175 deletions

File tree

pyopenms_viz/_bokeh/core.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
11
from __future__ import annotations
22

33
from abc import ABC
4+
from typing import Tuple
45

5-
from typing import Tuple, Iterator
6-
from dataclasses import dataclass
7-
8-
from bokeh.plotting import figure
9-
from bokeh.palettes import Plasma256
10-
from bokeh.transform import linear_cmap
116
from bokeh.models import (
7+
BoxEditTool,
128
ColumnDataSource,
9+
GlyphRenderer,
10+
Label,
1311
Legend,
1412
Range1d,
15-
BoxEditTool,
1613
Span,
1714
VStrip,
18-
GlyphRenderer,
19-
Label,
2015
)
21-
22-
from pandas.core.frame import DataFrame
16+
from bokeh.palettes import Plasma256
17+
from bokeh.plotting import figure
18+
from bokeh.transform import linear_cmap
2319
from numpy import nan
20+
from pandas.core.frame import DataFrame
2421

2522
# pyopenms_viz imports
2623
from .._core import (
27-
BasePlot,
28-
LinePlot,
29-
VLinePlot,
30-
ScatterPlot,
24+
APPEND_PLOT_DOC,
3125
BaseMSPlot,
26+
BasePlot,
3227
ChromatogramPlot,
28+
LinePlot,
3329
MobilogramPlot,
3430
PeakMapPlot,
31+
ScatterPlot,
3532
SpectrumPlot,
36-
APPEND_PLOT_DOC,
33+
VLinePlot,
3734
)
3835
from .._misc import ColorGenerator, MarkerShapeGenerator, is_latex_formatted
39-
from ..constants import PEAK_BOUNDARY_ICON, FEATURE_BOUNDARY_ICON
36+
from ..constants import FEATURE_BOUNDARY_ICON, PEAK_BOUNDARY_ICON
4037

4138

4239
class BOKEHPlot(BasePlot, ABC):
@@ -60,11 +57,11 @@ def fig(self, value):
6057

6158
def _load_extension(self) -> None:
6259
try:
63-
from bokeh.plotting import figure, show
6460
from bokeh.models import ColumnDataSource, Legend
61+
from bokeh.plotting import figure, show
6562
except ImportError:
6663
raise ImportError(
67-
f"bokeh is not installed. Please install using `pip install bokeh` to use this plotting library in pyopenms-viz"
64+
"bokeh is not installed. Please install using `pip install bokeh` to use this plotting library in pyopenms-viz"
6865
)
6966

7067
def _create_figure(self):
@@ -303,7 +300,6 @@ def plot(self):
303300
line_width=self.line_width,
304301
)
305302
else:
306-
307303
legend_items = []
308304
for group, df in self.data.groupby(self.by, sort=False):
309305
source = ColumnDataSource(df)
@@ -439,7 +435,6 @@ def plot(self):
439435

440436

441437
class BOKEH_MSPlot(BaseMSPlot, BOKEHPlot, ABC):
442-
443438
def get_line_renderer(self, **kwargs) -> None:
444439
return BOKEHLinePlot(**kwargs)
445440

@@ -555,9 +550,7 @@ class BOKEHPeakMapPlot(BOKEH_MSPlot, PeakMapPlot):
555550

556551
# NOTE: canvas is only used in matplotlib backend
557552
def create_main_plot(self, canvas=None):
558-
559553
if not self.plot_3d:
560-
561554
scatterPlot = self.get_scatter_renderer(data=self.data, config=self._config)
562555

563556
tooltips, custom_hover_data = self._create_tooltips(

pyopenms_viz/_config.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
from abc import ABC, ABCMeta
2-
from dataclasses import dataclass, field, asdict, fields
3-
from typing import Tuple, Literal, Dict, Any, Union, Iterator
1+
from abc import ABC
42
from copy import deepcopy
5-
from ._misc import ColorGenerator, MarkerShapeGenerator
3+
from dataclasses import asdict, dataclass, field, fields
4+
from typing import Any, Dict, Iterator, Literal, Tuple, Union
5+
66
import pandas as pd
77

8+
from ._misc import ColorGenerator, MarkerShapeGenerator
9+
810

911
@dataclass(kw_only=True)
1012
class BaseConfig(ABC):
11-
1213
@classmethod
1314
def from_dict(cls, config_dict: Dict[str, Any]) -> "BaseConfig":
1415
"""
@@ -87,6 +88,7 @@ class LegendConfig(BaseConfig):
8788
1.2,
8889
0.5,
8990
) # for fine control legend positioning in matplotlib
91+
ncol: int = 1 # number of columns in legend
9092

9193
@staticmethod
9294
def _matplotlibLegendLocationMapper(loc):
@@ -288,7 +290,6 @@ class ScatterConfig(BasePlotConfig):
288290
)
289291

290292
def __post_init__(self):
291-
292293
super().__post_init__()
293294
if not isinstance(self.marker, MarkerShapeGenerator):
294295
self.marker = MarkerShapeGenerator(shapes=self.marker)
@@ -424,6 +425,10 @@ class PeakMapConfig(ScatterConfig):
424425
x_kind (str): Type of plot for the X-axis marginal. Defaults to "chromatogram".
425426
aggregation_method (Literal["mean", "sum", "max"]): Method for aggregating data. Defaults to "mean".
426427
annotation_data (pd.DataFrame | None): Data for annotations. Defaults to None.
428+
annotation_colormap (str): Colormap for annotations. Defaults to "Dark2".
429+
annotation_line_width (float): Width of the annotation lines. Defaults to 3.
430+
annotation_line_type (str): Type of the annotation lines (e.g., "solid", "dashed"). Defaults to "solid".
431+
annotation_legend_config (Dict | LegendConfig): Configuration for the annotation legend. Defaults to a LegendConfig instance with title "Features".
427432
xlabel (str): Label for the X-axis. Defaults to "Retention Time".
428433
ylabel (str): Label for the Y-axis. Defaults to "mass-to-charge".
429434
zlabel (str): Label for the Z-axis. Defaults to "Intensity".
@@ -448,6 +453,18 @@ def marginal_config_factory(kind):
448453

449454
aggregation_method: Literal["mean", "sum", "max"] = "mean"
450455
annotation_data: pd.DataFrame | None = None
456+
annotation_x_lb: str = "leftWidth"
457+
annotation_x_ub: str = "rightWidth"
458+
annotation_y_lb: str = "IM_leftWidth"
459+
annotation_y_ub: str = "IM_rightWidth"
460+
annotation_colors: str = "color"
461+
annotation_names: str = "name"
462+
annotation_colormap: str = "Dark2"
463+
annotation_line_width: float = 3
464+
annotation_line_type: str = "solid"
465+
annotation_legend_config: Dict | LegendConfig = field(
466+
default_factory=ScatterConfig.default_legend_factory
467+
)
451468

452469
### override axes and title labels
453470
xlabel: str = "Retention Time"
@@ -498,6 +515,10 @@ def __post_init__(self):
498515
self.annotation_data = (
499516
None if self.annotation_data is None else self.annotation_data.copy()
500517
)
518+
if not isinstance(self.annotation_legend_config, LegendConfig):
519+
self.annotation_legend_config = LegendConfig.from_dict(
520+
self.annotation_legend_config
521+
)
501522

502523

503524
def bokeh_line_dash_mapper(bokeh_dash, target_library="plotly"):

0 commit comments

Comments
 (0)