diff --git a/README.md b/README.md index 895c428..c278ea0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/molplotly/main.py b/molplotly/main.py index 0eb63ec..e552e9a 100644 --- a/molplotly/main.py +++ b/molplotly/main.py @@ -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 @@ -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: @@ -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 @@ -215,7 +228,7 @@ 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", @@ -223,7 +236,7 @@ def add_molecules( ) 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", @@ -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"] @@ -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) diff --git a/setup.py b/setup.py index 283979e..97b6696 100644 --- a/setup.py +++ b/setup.py @@ -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", @@ -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", @@ -29,3 +30,5 @@ "Programming Language :: Python :: 3", ], ) + +# TODO - change link in blog \ No newline at end of file diff --git a/setup_pip.py b/setup_pip.py index 780d821..b30163a 100644 --- a/setup_pip.py +++ b/setup_pip.py @@ -10,7 +10,7 @@ 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, @@ -18,12 +18,13 @@ author="William McCorkindale", author_email="wjm41@cam.ac.uk", 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",