def display_hover_image(index): if index is None: return '' smiles = details.iloc[index]['SMILES'] image = smiles_to_image(smiles, png=True, b64=True, crop=True, size=500) src = 'data:image/png;base64, %s' % image return src
def get_image_src(cid): if cid is None: return '' smiles = details.loc[cid, 'SMILES'] image = smiles_to_image(smiles, png=True, b64=True, crop=True, size=250) src = 'data:image/png;base64, %s' % image return src
def hover_fn(trace, points, state): #print(points) try: ind = points.point_inds[0] if trace.name == 'Possible Molecules': use = big_umap else: use = known_umap smiles = use.index[ind] image_widget.value = smiles_to_image(smiles, size=200) text_widget.value = smiles except IndexError: pass except Exception as e: print(e)
def plot(big_umap, known_umap, skip=10): big_umap = big_umap.iloc[::skip] known_umap = known_umap.iloc[::skip] # The GDB scatter plot skip = 10 big_scatter = go.Scatter( x=big_umap.loc[:, 0], y=big_umap.loc[:, 1], name='Possible Molecules', mode="markers", hoverinfo="text", opacity=0.5, marker={ "size": 5, "line": { "width": 0.5, "color": "white" }, "color": big_umap.loc[:, 'p'], "colorscale": 'magma', "colorbar": { 'thickness': 20, 'title': 'p(Odorous)' }, }, ) # The known odorants scatter plot known_scatter = go.Scattergl( x=known_umap.loc[:, 0], y=known_umap.loc[:, 1], name='Known Odorous', mode="markers", hoverinfo="text", opacity=1, marker={ "size": 5, "line": { "width": 0.5, "color": "white" }, "color": "blue", }, ) # The axes, etc. layout = go.Layout(xaxis={ "type": "linear", "title": "", "showline": False, "showticklabels": False }, yaxis={ "type": "linear", "title": "", "showline": False, "showticklabels": False }, margin={ "l": 40, "b": 40, "t": 10, "r": 10 }, legend={ "x": 0, "y": 1, 'font': { 'size': 15, 'color': 'white' } }, hovermode="closest", paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(10,10,10,1)", width=1000, height=1000, xaxis_showgrid=False, yaxis_showgrid=False, xaxis_zeroline=False, yaxis_zeroline=False) fig = go.FigureWidget(data=[big_scatter, known_scatter], layout=layout) fig.layout.hovermode = 'closest' # The 2D drawing of the molecule first_smiles = "CCCCO" image_widget = Image(value=smiles_to_image(first_smiles), layout=Layout(height="200px", width="200px", left='50px', top='-250px')) text_widget = Text(value=first_smiles, placeholder=first_smiles, description='SMILES:', disabled=False, layout=Layout(width='400px', left='25px', top='-285px')) def hover_fn(trace, points, state): #print(points) try: ind = points.point_inds[0] if trace.name == 'Possible Molecules': use = big_umap else: use = known_umap smiles = use.index[ind] image_widget.value = smiles_to_image(smiles, size=200) text_widget.value = smiles except IndexError: pass except Exception as e: print(e) def click_fn(trace, points, state): global hover_on if hover_on: fig.data[0].on_hover(None) hover_on = 0 else: fig.data[0].on_hover(hover_fn) hover_on = 1 fig.data[0].on_hover(hover_fn) fig.data[0].on_click(click_fn) canvas = GridBox([fig, image_widget, text_widget], layout={'gridgap': '0'}) return canvas
def plotly_embedding(embedding, features=None, show_features=None, colors=None, colorscale='rainbow'): """ params: embedding: A dataframe wrapped around e.g. a fitted TSNE object, with an index of CIDs features: A dataframe of features, e.g. names, SMILES strings, or physicochemical features, with an index of CIDs """ if features is None: features = pyrfume.load_data("odorants/all-cids-properties.csv", usecols=range(5)) # Only retain those rows corresponding to odorants in the embedding features = features.loc[embedding.index] show_features = show_features or list(features) def format_features(col): return "%s: %s" % (index_name, col.values.split('<br>')) try: index_name = features.index.name or 'Index' names = ( features.loc[:, show_features] .reset_index() .astype("str") .apply(format_features, axis=1) ) except Exception: names = features.index assert embedding.shape[0] == features.shape[0] # The scatter plot scatter = go.Scatter( x=embedding.iloc[:, 0], y=embedding.iloc[:, 1], text=names, mode="markers", hoverinfo="text", opacity=0.5, marker={ "size": 5, "line": {"width": 0.5, "color": "white"}, "color": colors if colors is not None else "black", "colorscale": colorscale, }, ) # The axes, etc. layout = go.Layout( xaxis={"type": "linear", "title": "", "showline": False, "showticklabels": False}, yaxis={"type": "linear", "title": "", "showline": False, "showticklabels": False}, margin={"l": 40, "b": 40, "t": 10, "r": 10}, legend={"x": 0, "y": 1}, hovermode="closest", paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", width=500, height=500, ) fig = go.FigureWidget(data=[scatter], layout=layout) fig.layout.hovermode = 'closest' # The 2D drawing of the molecule image_widget = Image( value=smiles_to_image("CCCCO"), layout=Layout(height="300px", width="300px") ) def hover_fn(trace, points, state): ind = points.point_inds[0] smiles = features["SMILES"].iloc[ind] image_widget.value = smiles_to_image(smiles) scatter = fig.data[0] scatter.on_hover(hover_fn) canvas = VBox([fig, image_widget]) return canvas
def hover_fn(trace, points, state): ind = points.point_inds[0] smiles = features["SMILES"].iloc[ind] image_widget.value = smiles_to_image(smiles)
def get_image_src(smiles): if not smiles: return '' image = smiles_to_image(smiles, png=True, b64=True, crop=True, size=250) src = 'data:image/png;base64, %s' % image return src