Skip to content

Commit b59c3c3

Browse files
committed
🐛 missing function and start deconvoluting utils, add imports to pkg
1 parent f1b9239 commit b59c3c3

File tree

5 files changed

+55
-32
lines changed

5 files changed

+55
-32
lines changed

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ dependencies = [
3131
"matplotlib",
3232
"kaleido",
3333
"pyvis",
34-
# "wordcloud",
34+
"wordcloud",
3535
"cyjupyter",
3636
"nltk",
37-
# "webweb"
38-
# "acore",
37+
"webweb",
38+
"acore",
3939
"dash-cytoscape",
4040
]
4141

src/vuecore/linkers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from io import StringIO
2+
3+
import requests
4+
5+
6+
def get_clustergrammer_link(net, filename=None):
7+
clustergrammer_url = "http://amp.pharm.mssm.edu/clustergrammer/matrix_upload/"
8+
if filename is None:
9+
file_string = net.write_matrix_to_tsv()
10+
file_obj = StringIO(file_string)
11+
if "filename" not in net.dat or net.dat["filename"] is None:
12+
fake_filename = "Network.txt"
13+
else:
14+
fake_filename = net.dat["filename"]
15+
r = requests.post(clustergrammer_url, files={"file": (fake_filename, file_obj)})
16+
else:
17+
file_obj = open(filename, "r")
18+
r = requests.post(clustergrammer_url, files={"file": file_obj})
19+
link = r.text
20+
return link

src/vuecore/translate.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import base64
2+
import io
3+
4+
from dash import html
5+
6+
7+
def hex2rgb(color):
8+
_hex = color.lstrip("#")
9+
rgb = tuple(int(_hex[i : i + 2], 16) for i in (0, 2, 4))
10+
rgba = rgb + (0.6,)
11+
return rgba
12+
13+
14+
def mpl_to_html_image(plot, width=800):
15+
buf = io.BytesIO()
16+
plot.savefig(buf, format="png")
17+
data = base64.b64encode(buf.getbuffer()).decode("utf8")
18+
figure = html.Img(src="data:image/png;base64,{}".format(data), width=f"{width}")
19+
20+
return figure

src/vuecore/utils.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import base64
2-
import io
31
import random
42
from collections import defaultdict
53
from urllib import error
64

75
import bs4 as bs
8-
import dash_html_components as html
96
import networkx as nx
107
import pandas as pd
118
import requests
@@ -24,15 +21,6 @@ def check_columns(df, cols):
2421
return True
2522

2623

27-
def mpl_to_html_image(plot, width=800):
28-
buf = io.BytesIO()
29-
plot.savefig(buf, format="png")
30-
data = base64.b64encode(buf.getbuffer()).decode("utf8")
31-
figure = html.Img(src="data:image/png;base64,{}".format(data), width="800")
32-
33-
return figure
34-
35-
3624
def generate_html(network):
3725
"""
3826
This method gets the data structures supporting the nodes, edges,
@@ -282,13 +270,6 @@ def __extract_style(el):
282270
return getattr(html, name.title())(contents, style=style)
283271

284272

285-
def hex2rgb(color):
286-
hex = color.lstrip("#")
287-
rgb = tuple(int(hex[i : i + 2], 16) for i in (0, 2, 4))
288-
rgba = rgb + (0.6,)
289-
return rgba
290-
291-
292273
def get_rgb_colors(n):
293274
colors = []
294275
r = int(random.random() * 256)

src/vuecore/viz.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
from webweb import Web
3232
from wordcloud import STOPWORDS, WordCloud
3333

34-
from . import dendrogram, utils, wgcna
34+
from . import utils
35+
from .linkers import get_clustergrammer_link
36+
from .translate import hex2rgb, mpl_to_html_image
3537

3638
# matplotlib.use("Agg")
3739

@@ -2128,7 +2130,7 @@ def get_sankey_plot(
21282130
label=nodes,
21292131
color=[
21302132
(
2131-
"rgba" + str(utils.hex2rgb(node_colors[c]))
2133+
"rgba" + str(hex2rgb(node_colors[c]))
21322134
if node_colors[c].startswith("#")
21332135
else node_colors[c]
21342136
)
@@ -2140,7 +2142,7 @@ def get_sankey_plot(
21402142
target=[list(nodes).index(i) for i in data[args["target"]].tolist()],
21412143
value=data[args["weight"]].tolist(),
21422144
color=[
2143-
"rgba" + str(utils.hex2rgb(c)) if c.startswith("#") else c
2145+
"rgba" + str(hex2rgb(c)) if c.startswith("#") else c
21442146
for c in data[args["source_colors"]].tolist()
21452147
],
21462148
label=hover_data,
@@ -2413,7 +2415,7 @@ def get_clustergrammer_plot(data, identifier, args):
24132415
)
24142416
clustergrammer_net.load_df(data)
24152417

2416-
link = utils.get_clustergrammer_link(clustergrammer_net, filename=None)
2418+
link = get_clustergrammer_link(clustergrammer_net, filename=None)
24172419

24182420
iframe = html.Iframe(src=link, width=1000, height=900)
24192421

@@ -2547,7 +2549,7 @@ def get_WGCNAPlots(data, identifier):
25472549
height=800,
25482550
)
25492551
)
2550-
dendro_tree = wgcnaAnalysis.get_dendrogram(
2552+
dendro_tree = wgcna_analysis.get_dendrogram(
25512553
METDiss, METDiss.index, distfun=None, linkagefun="ward", div_clusters=False
25522554
)
25532555
if dendro_tree is not None:
@@ -2611,12 +2613,12 @@ def get_WGCNAPlots(data, identifier):
26112613
)
26122614
)
26132615
):
2614-
df = wgcnaAnalysis.get_percentiles_heatmap(
2616+
df = wgcna_analysis.get_percentiles_heatmap(
26152617
METcor, dendro_tree, bydendro=True, bycols=False
26162618
).T
26172619
else:
2618-
df = wgcnaAnalysis.df_sort_by_dendrogram(
2619-
wgcnaAnalysis.df_sort_by_dendrogram(METcor, dendro_tree).T,
2620+
df = wgcna_analysis.df_sort_by_dendrogram(
2621+
wgcna_analysis.df_sort_by_dendrogram(METcor, dendro_tree).T,
26202622
dendro_tree,
26212623
)
26222624

@@ -3271,7 +3273,7 @@ def get_km_plot(data, identifier, args):
32713273
plot.set_xlabel(xlabel)
32723274
plot.set_ylabel(ylabel)
32733275
if environment == "app":
3274-
figure = utils.mpl_to_html_image(plot.figure, width=800)
3276+
figure = mpl_to_html_image(plot.figure, width=800)
32753277
result = html.Div(id=identifier, children=[figure])
32763278
else:
32773279
result = plot.figure
@@ -3311,7 +3313,7 @@ def get_cumulative_hazard_plot(data, identifier, args):
33113313
plot.set_xlabel(xlabel)
33123314
plot.set_ylabel(ylabel)
33133315
if environment == "app":
3314-
figure = utils.mpl_to_html_image(plot.figure, width=800)
3316+
figure = mpl_to_html_image(plot.figure, width=800)
33153317
result = html.Div(id=identifier, children=[figure])
33163318
else:
33173319
result = plot.figure

0 commit comments

Comments
 (0)