Skip to content

Commit

Permalink
Merge branch 'release/v1.1.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
wjm41 committed Mar 14, 2023
2 parents 5376f6f + 990222c commit 25b3f23
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ A readable walkthrough of how to use the package together with some useful examp

```sh
pip install molplotly
conda install rdkit
```

## Usage
Expand Down
46 changes: 31 additions & 15 deletions molplotly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re

import pandas as pd
import numpy as np
from dash import Input, Output, dcc, html, no_update
from jupyter_dash import JupyterDash
from pandas.core.groupby import DataFrameGroupBy
Expand Down Expand Up @@ -54,14 +55,14 @@ def find_grouping(
) -> tuple[DataFrameGroupBy, dict]:

if fig.data[0].hovertemplate is not None:
col_names = re.findall(r"(.*?)=(?!%).*?<.*?>", fig.data[0].hovertemplate)
col_names = re.findall(r"(.*?)=.*?<.*?>", fig.data[0].hovertemplate)
col_names = [re.sub(r"(.*)>", "", col_name) for col_name in col_names]
if set(col_names) != set(cols):
if set(cols).issubset(set(col_names)) is False:
raise ValueError(
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}.",
)

df_grouped = df_data.groupby(col_names)
df_grouped = df_data.groupby(cols)

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

curve_dict = {}
for index, data in enumerate(fig.data):
curve_name = re.findall(r".*?=(?!%)(.*?)<.*?>", data.hovertemplate)
curve_name = re.findall(r".*?=(.*?)<.*?>", data.hovertemplate)
curve_name = ", ".join(str(x) for x in curve_name)
if "%{x}" in curve_name:
unique_x_values = np.unique(data.x)
if len(unique_x_values) == 1:
curve_name = curve_name.replace("%{x}", str(unique_x_values[0]))
else:
curve_name = curve_name.replace(", %{x}", "")
if "%{y}" in curve_name:
unique_y_values = np.unique(data.y)
if len(unique_y_values) == 1:
curve_name = curve_name.replace("%{y}", str(unique_y_values[0]))
else:
curve_name = curve_name.replace(", %{y}", "")
curve_dict[index] = str_groups[curve_name]

return df_grouped, curve_dict
Expand Down Expand Up @@ -215,15 +228,15 @@ def add_molecules(

if mol_col is not None and len(mol_col) > 1:
menu = dcc.Dropdown(
options=[{"label": x, "value": x} for x in mol_col],
options=[{"label": x, "smiles_value": x} for x in mol_col],
value=mol_col[0],
multi=True,
id="smiles-menu",
placeholder="Select a mol column to display",
)
elif smiles_col is not None and len(smiles_col) > 1:
menu = dcc.Dropdown(
options=[{"label": x, "value": x} for x in smiles_col],
options=[{"label": x, "smiles_value": x} for x in smiles_col],
value=smiles_col[0],
multi=True,
id="smiles-menu",
Expand Down Expand Up @@ -251,21 +264,24 @@ def add_molecules(
Output("graph-tooltip", "bbox"),
Output("graph-tooltip", "children"),
],
inputs=[Input("graph-basic-2", "hoverData"), Input("smiles-menu", "value")],
inputs=[
Input("graph-basic-2", "hoverData"),
Input("smiles-menu", "smiles_value"),
],
)
def display_hover(hoverData, value):
def display_hover(hoverData, smiles_value):
if hoverData is None:
return False, no_update, no_update

if value is None:
if smiles_value is None:
if mol_col is not None:
value = mol_col
smiles_value = mol_col
elif smiles_col is not None:
value = smiles_col
if isinstance(value, str):
chosen_smiles = [value]
smiles_value = smiles_col
if isinstance(smiles_value, str):
chosen_smiles = [smiles_value]
else:
chosen_smiles = value
chosen_smiles = smiles_value

pt = hoverData["points"][0]
bbox = pt["bbox"]
Expand Down Expand Up @@ -330,7 +346,7 @@ def display_hover(hoverData, value):
)

if title_col is not None:
title = df_row[title_col].astype(str)
title = str(df_row[title_col])
if title_col in caption_transform:
title = caption_transform[title_col](title)

Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="molplotly",
version="1.1.5",
version="1.1.6",
description="plotly add-on to render molecule images on mouseover",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
Expand All @@ -15,6 +15,7 @@
"werkzeug>=2.0.0",
"jupyter-dash>=0.4.2",
"plotly>=5.0.0",
"rdkit-pypi>=2021.9.4",
"pandas",
"ipykernel",
"nbformat",
Expand All @@ -29,3 +30,5 @@
"Programming Language :: Python :: 3",
],
)

# TODO - change link in blog
5 changes: 3 additions & 2 deletions setup_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
setup(
name="molplotly",
packages=["molplotly"],
version="1.1.5",
version="1.1.6",
license="Apache License, Version 2.0",
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.",
long_description=long_description,
long_description_content_type="text/markdown",
author="William McCorkindale",
author_email="[email protected]",
url="https://github.com/wjm41/molplotly",
download_url="https://github.com/wjm41/molplotly/archive/refs/tags/v1.1.4.tar.gz",
download_url="https://github.com/wjm41/molplotly/archive/refs/tags/v1.1.6.tar.gz",
install_requires=[
"dash>=2.0.0",
"werkzeug>=2.0.0",
"jupyter-dash>=0.4.2",
"plotly>=5.0.0",
"rdkit-pypi>=2021.9.4",
"pandas",
"ipykernel",
"nbformat",
Expand Down

0 comments on commit 25b3f23

Please sign in to comment.