Skip to content

Commit 25b3f23

Browse files
author
wjm41
committed
Merge branch 'release/v1.1.6'
2 parents 5376f6f + 990222c commit 25b3f23

4 files changed

Lines changed: 38 additions & 19 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ A readable walkthrough of how to use the package together with some useful examp
1717

1818
```sh
1919
pip install molplotly
20-
conda install rdkit
2120
```
2221

2322
## Usage

molplotly/main.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99

1010
import pandas as pd
11+
import numpy as np
1112
from dash import Input, Output, dcc, html, no_update
1213
from jupyter_dash import JupyterDash
1314
from pandas.core.groupby import DataFrameGroupBy
@@ -54,14 +55,14 @@ def find_grouping(
5455
) -> tuple[DataFrameGroupBy, dict]:
5556

5657
if fig.data[0].hovertemplate is not None:
57-
col_names = re.findall(r"(.*?)=(?!%).*?<.*?>", fig.data[0].hovertemplate)
58+
col_names = re.findall(r"(.*?)=.*?<.*?>", fig.data[0].hovertemplate)
5859
col_names = [re.sub(r"(.*)>", "", col_name) for col_name in col_names]
59-
if set(col_names) != set(cols):
60+
if set(cols).issubset(set(col_names)) is False:
6061
raise ValueError(
6162
f"marker_col/color_col/facet_col is misspecified because the specified dataframe grouping names {cols} don't match the names in the plotly figure {col_names}.",
6263
)
6364

64-
df_grouped = df_data.groupby(col_names)
65+
df_grouped = df_data.groupby(cols)
6566

6667
str_groups = {}
6768
for name, group in df_grouped:
@@ -72,8 +73,20 @@ def find_grouping(
7273

7374
curve_dict = {}
7475
for index, data in enumerate(fig.data):
75-
curve_name = re.findall(r".*?=(?!%)(.*?)<.*?>", data.hovertemplate)
76+
curve_name = re.findall(r".*?=(.*?)<.*?>", data.hovertemplate)
7677
curve_name = ", ".join(str(x) for x in curve_name)
78+
if "%{x}" in curve_name:
79+
unique_x_values = np.unique(data.x)
80+
if len(unique_x_values) == 1:
81+
curve_name = curve_name.replace("%{x}", str(unique_x_values[0]))
82+
else:
83+
curve_name = curve_name.replace(", %{x}", "")
84+
if "%{y}" in curve_name:
85+
unique_y_values = np.unique(data.y)
86+
if len(unique_y_values) == 1:
87+
curve_name = curve_name.replace("%{y}", str(unique_y_values[0]))
88+
else:
89+
curve_name = curve_name.replace(", %{y}", "")
7790
curve_dict[index] = str_groups[curve_name]
7891

7992
return df_grouped, curve_dict
@@ -215,15 +228,15 @@ def add_molecules(
215228

216229
if mol_col is not None and len(mol_col) > 1:
217230
menu = dcc.Dropdown(
218-
options=[{"label": x, "value": x} for x in mol_col],
231+
options=[{"label": x, "smiles_value": x} for x in mol_col],
219232
value=mol_col[0],
220233
multi=True,
221234
id="smiles-menu",
222235
placeholder="Select a mol column to display",
223236
)
224237
elif smiles_col is not None and len(smiles_col) > 1:
225238
menu = dcc.Dropdown(
226-
options=[{"label": x, "value": x} for x in smiles_col],
239+
options=[{"label": x, "smiles_value": x} for x in smiles_col],
227240
value=smiles_col[0],
228241
multi=True,
229242
id="smiles-menu",
@@ -251,21 +264,24 @@ def add_molecules(
251264
Output("graph-tooltip", "bbox"),
252265
Output("graph-tooltip", "children"),
253266
],
254-
inputs=[Input("graph-basic-2", "hoverData"), Input("smiles-menu", "value")],
267+
inputs=[
268+
Input("graph-basic-2", "hoverData"),
269+
Input("smiles-menu", "smiles_value"),
270+
],
255271
)
256-
def display_hover(hoverData, value):
272+
def display_hover(hoverData, smiles_value):
257273
if hoverData is None:
258274
return False, no_update, no_update
259275

260-
if value is None:
276+
if smiles_value is None:
261277
if mol_col is not None:
262-
value = mol_col
278+
smiles_value = mol_col
263279
elif smiles_col is not None:
264-
value = smiles_col
265-
if isinstance(value, str):
266-
chosen_smiles = [value]
280+
smiles_value = smiles_col
281+
if isinstance(smiles_value, str):
282+
chosen_smiles = [smiles_value]
267283
else:
268-
chosen_smiles = value
284+
chosen_smiles = smiles_value
269285

270286
pt = hoverData["points"][0]
271287
bbox = pt["bbox"]
@@ -330,7 +346,7 @@ def display_hover(hoverData, value):
330346
)
331347

332348
if title_col is not None:
333-
title = df_row[title_col].astype(str)
349+
title = str(df_row[title_col])
334350
if title_col in caption_transform:
335351
title = caption_transform[title_col](title)
336352

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="molplotly",
5-
version="1.1.5",
5+
version="1.1.6",
66
description="plotly add-on to render molecule images on mouseover",
77
long_description=open("README.md").read(),
88
long_description_content_type="text/markdown",
@@ -15,6 +15,7 @@
1515
"werkzeug>=2.0.0",
1616
"jupyter-dash>=0.4.2",
1717
"plotly>=5.0.0",
18+
"rdkit-pypi>=2021.9.4",
1819
"pandas",
1920
"ipykernel",
2021
"nbformat",
@@ -29,3 +30,5 @@
2930
"Programming Language :: Python :: 3",
3031
],
3132
)
33+
34+
# TODO - change link in blog

setup_pip.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010
setup(
1111
name="molplotly",
1212
packages=["molplotly"],
13-
version="1.1.5",
13+
version="1.1.6",
1414
license="Apache License, Version 2.0",
1515
description="molplotly is an add-on to plotly built on RDKit which allows 2D images of molecules to be shown in scatterplots when hovering over the datapoints.",
1616
long_description=long_description,
1717
long_description_content_type="text/markdown",
1818
author="William McCorkindale",
1919
author_email="wjm41@cam.ac.uk",
2020
url="https://github.com/wjm41/molplotly",
21-
download_url="https://github.com/wjm41/molplotly/archive/refs/tags/v1.1.4.tar.gz",
21+
download_url="https://github.com/wjm41/molplotly/archive/refs/tags/v1.1.6.tar.gz",
2222
install_requires=[
2323
"dash>=2.0.0",
2424
"werkzeug>=2.0.0",
2525
"jupyter-dash>=0.4.2",
2626
"plotly>=5.0.0",
27+
"rdkit-pypi>=2021.9.4",
2728
"pandas",
2829
"ipykernel",
2930
"nbformat",

0 commit comments

Comments
 (0)