Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pyOpenMS-Viz is a Python library that provides a simple interface for extending
- Versatile column selection for easy adaptation to different data formats
- Consistent API across different plotting backends for easy switching between static and interactive plots
- Suitable for use in scripts, Jupyter notebooks, and web applications
- Now supports both pandas and polars DataFrames!
- Interactive plots with zoom, pan, and hover capabilities
- Customizable plot styling and annotations

## Suported Plots
| **Plot Type** | **Required Dimensions** | **pyopenms_viz Name** | **Matplotlib** | **Bokeh** | **Plotly** |
Expand Down Expand Up @@ -57,3 +60,33 @@ Documentation can be found [here](https://pyopenms-viz.readthedocs.io/en/latest/
- Pfeuffer, J., Bielow, C., Wein, S. et al. OpenMS 3 enables reproducible analysis of large-scale mass spectrometry data. Nat Methods 21, 365–367 (2024). [https://doi.org/10.1038/s41592-024-02197-7](https://doi.org/10.1038/s41592-024-02197-7)

- Röst HL, Schmitt U, Aebersold R, Malmström L. pyOpenMS: a Python-based interface to the OpenMS mass-spectrometry algorithm library. Proteomics. 2014 Jan;14(1):74-7. [https://doi.org/10.1002/pmic.201300246](https://doi.org/10.1002/pmic.201300246). PMID: [24420968](https://pubmed.ncbi.nlm.nih.gov/24420968/).

## Quick Start

```python
import pandas as pd
import polars as pl
from pyopenms_viz import plot

# Using pandas DataFrame
df = pd.DataFrame({
'mz': [100, 200, 300],
'intensity': [1000, 2000, 3000]
})
plot(df, x='mz', y='intensity', kind='spectrum')

# Using polars DataFrame
df_pl = pl.DataFrame({
'mz': [100, 200, 300],
'intensity': [1000, 2000, 3000]
})
plot(df_pl, x='mz', y='intensity', kind='spectrum')
Comment on lines +72 to +83
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the user have to explicitly call the plot method? The nice aspect of pyopenms_viz is that it was designed to be compatible with pandas plot method, so that you can perform object/method chaining df.plot(...). This example doesn't really show that. Can the same be done for the polars df example?

```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the LICENSE file for details.
30 changes: 28 additions & 2 deletions pyopenms_viz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
init
"""

from pandas.plotting._core import PlotAccessor
from __future__ import annotations

from typing import Any, Union
import pandas as pd
import polars as pl
from pandas.core.frame import DataFrame
from typing import Any
from pandas.plotting._core import PlotAccessor
from pandas.core.dtypes.generic import ABCDataFrame
import importlib
import types
Expand Down Expand Up @@ -197,4 +201,26 @@ def _get_plot_backend(backend: str | None = None):
return module


def plot(data: Union[pd.DataFrame, pl.DataFrame], *args, **kwargs):
"""
Make plots of MassSpec data using pandas or polars DataFrames.

Parameters
----------
data : pandas.DataFrame or polars.DataFrame
The data to be plotted.
*args : tuple
Variable length argument list.
**kwargs : dict
Arbitrary keyword arguments.

Returns
-------
figure
The plot figure object.
"""
plot_obj = PlotAccessor(data)
return plot_obj(*args, **kwargs)


__all__ = ["PlotAccessor"]
9 changes: 5 additions & 4 deletions pyopenms_viz/_bokeh/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from abc import ABC

from typing import Tuple, Iterator
from typing import Tuple, Iterator, Any, Dict, List, Optional, Union
from dataclasses import dataclass

from bokeh.plotting import figure
Expand All @@ -21,6 +21,7 @@

from pandas.core.frame import DataFrame
from numpy import nan
import numpy as np

# pyopenms_viz imports
from .._core import (
Expand All @@ -35,6 +36,7 @@
SpectrumPlot,
APPEND_PLOT_DOC,
)
from .._dataframe import DataFrameWrapper
from .._misc import ColorGenerator, MarkerShapeGenerator, is_latex_formatted
from ..constants import PEAK_BOUNDARY_ICON, FEATURE_BOUNDARY_ICON

Expand Down Expand Up @@ -294,7 +296,7 @@ def plot(self):
Plot a line plot
"""
if self.by is None:
source = ColumnDataSource(self.data)
source = ColumnDataSource(self.data.to_pandas())
line = self.fig.line(
x=self.x,
y=self.y,
Expand All @@ -303,10 +305,9 @@ def plot(self):
line_width=self.line_width,
)
else:

legend_items = []
for group, df in self.data.groupby(self.by, sort=False):
source = ColumnDataSource(df)
source = ColumnDataSource(df.to_pandas())
line = self.fig.line(
x=self.x,
y=self.y,
Expand Down
Loading