Skip to content

Commit

Permalink
added streamlit utils for streamlit sake
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanwilliammd committed Apr 10, 2024
1 parent e594998 commit 5923f20
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
78 changes: 78 additions & 0 deletions iderare_pheno/streamlit_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
from datetime import datetime

import pandas as pd
import scipy.cluster
import yaml
from matplotlib import pyplot as plt

iderare_template = os.path.join(os.path.dirname(__file__), "templates", "template_iderare.yml")


# Fill template_iderare.yml with the given phenotype
def generate_yml(hpo_sets):
with open(iderare_template, "r") as f:
y = yaml.safe_load(f)
y["analysis"]["hpo_ids"] = hpo_sets

yaml_str = yaml.dump(y, default_flow_style=False, sort_keys=False)
return yaml_str


# Convert data(s) to dataframe
def list2tsv(
term_id,
name,
sim_score=None,
):
if sim_score is None:
data = {"id": term_id, "name": name}
else:
rank = [i + 1 for i in range(len(name))]
data = {"rank": rank, "id": term_id, "name": name, "score": sim_score}
df = pd.DataFrame(data)
return df.to_csv(index=False, sep="\t")


# Print the dendrogram tree
def linkage_dendrogram(linkage, labels, title="Similarity", threshold=0.3, path_to_save=None):
if len(linkage) == 0:
print("Linkage is empty. The data not possible due to blank linkage information.")
return
plt.figure(figsize=(20, len(linkage)))
scipy.cluster.hierarchy.dendrogram(
linkage,
labels=labels,
show_contracted=True,
leaf_font_size=plt.rcParams["font.size"] * 1.5,
color_threshold=threshold,
orientation="right",
)
plt.title(title, fontsize=plt.rcParams["font.size"] * 2)

plt.axvline(x=threshold, c="r", lw=2, linestyle="--")
plt.text(
threshold,
0,
"Similarity Threshold",
fontsize=plt.rcParams["font.size"] * 1.5,
va="bottom",
ha="center",
color="r",
)
plt.xlim(0, 1.0)
plt.xlabel("Distance", fontsize=plt.rcParams["font.size"] * 2)
plt.ylabel("Disease", fontsize=plt.rcParams["font.size"] * 2)
plt.tight_layout()

if not os.path.exists("output"):
os.makedirs("output")
print("Folder output created.")
else:
print("Folder output already exists.")

if path_to_save is None:
path_to_save = "output/{date_time}_{title}.png".format(
date_time=datetime.now().strftime("%Y%m%d_%H%M%S"), title=title[0:30]
)
plt.savefig(path_to_save)
2 changes: 1 addition & 1 deletion iderare_pheno/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
_MAJOR = "0"
_MINOR = "4"
_MINOR = "5"
# On main and in a nightly release the patch should be one ahead of the last
# released build.
_PATCH = "0"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dynamic = ["version"]
readme = "README.md"
classifiers = [
"Intended Audience :: Science/Research",
"Development Status :: 4 - Beta",
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python :: 3",
"Topic :: Scientific/Engineering :: Medical Science Apps.",
Expand Down

0 comments on commit 5923f20

Please sign in to comment.