Skip to content

Commit

Permalink
update comparing topics and fixed minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nargesr committed Oct 10, 2023
1 parent dcf1ca5 commit 75365d3
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 614,490 deletions.
50 changes: 34 additions & 16 deletions Topyfic/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import random
import pickle
from scipy.cluster.hierarchy import ward, dendrogram, leaves_list
from statsmodels.stats.multitest import multipletests, fdrcorrection
from statsmodels.stats.multitest import fdrcorrection

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
Expand Down Expand Up @@ -431,6 +431,7 @@ def convertDatTraits(data):

def TopicTraitRelationshipHeatmap(self,
metaData,
annotation=False,
save=True,
show=True,
file_format="pdf",
Expand All @@ -440,6 +441,8 @@ def TopicTraitRelationshipHeatmap(self,
:param metaData: traits you would like to see the relationship with topics (must be column name of cell_participation.obs)
:type metaData: list
:param annotation: indicate if you want to add correlation and p_values as a text in each square (default:False)
:type annotation: bool
:param save: indicate if you want to save the plot or not (default: True)
:type save: bool
:param show: indicate if you want to show the plot or not (default: True)
Expand Down Expand Up @@ -469,29 +472,42 @@ def TopicTraitRelationshipHeatmap(self,
topicsTraitCor.loc[i, j] = tmp[0]
topicsTraitPvalue.loc[i, j] = tmp[1]

topicsTraitCor.fillna(0.0, inplace=True)
topicsTraitPvalue.fillna(1.0, inplace=True)

for i in range(topicsTraitPvalue.shape[0]):
rejected, tmp = fdrcorrection(topicsTraitPvalue.iloc[i, :])
if not rejected.all():
topicsTraitPvalue.iloc[i, :] = tmp

fig, ax = plt.subplots(figsize=(topicsTraitPvalue.shape[0] * 1.5,
topicsTraitPvalue.shape[1] * 1.5), facecolor='white')

xlabels = self.cell_participation.to_df().columns
ylabels = datTraits.columns

# Loop over data dimensions and create text annotations.
tmp_cor = topicsTraitCor.T.round(decimals=3)
tmp_pvalue = topicsTraitPvalue.T.round(decimals=3)
labels = (np.asarray(["{0}\n({1})".format(cor, pvalue)
for cor, pvalue in zip(tmp_cor.values.flatten(),
tmp_pvalue.values.flatten())])) \
.reshape(topicsTraitCor.T.shape)

sns.set(font_scale=1.5)
res = sns.heatmap(topicsTraitCor.T, annot=labels, fmt="", cmap='RdBu_r',
vmin=-1, vmax=1, ax=ax, annot_kws={'size': 20, "weight": "bold"},
xticklabels=xlabels, yticklabels=ylabels)
if annotation:
fig, ax = plt.subplots(figsize=(topicsTraitPvalue.shape[0] * 1.5,
topicsTraitPvalue.shape[1] * 1.5), facecolor='white')

# Loop over data dimensions and create text annotations.
tmp_cor = topicsTraitCor.T.round(decimals=3)
tmp_pvalue = topicsTraitPvalue.T.round(decimals=3)
labels = (np.asarray(["{0}\n({1})".format(cor, pvalue)
for cor, pvalue in zip(tmp_cor.values.flatten(),
tmp_pvalue.values.flatten())])) \
.reshape(topicsTraitCor.T.shape)

sns.set(font_scale=1.5)
res = sns.heatmap(topicsTraitCor.T, annot=labels, fmt="", cmap='RdBu_r',
vmin=-1, vmax=1, ax=ax, annot_kws={'size': 20, "weight": "bold"},
xticklabels=xlabels, yticklabels=ylabels)

else:
fig, ax = plt.subplots(figsize=(topicsTraitPvalue.shape[0],
topicsTraitPvalue.shape[1]), facecolor='white')

sns.set(font_scale=1.5)
res = sns.heatmap(topicsTraitCor.T, cmap='RdBu_r',
vmin=-1, vmax=1, ax=ax, annot_kws={'size': 20, "weight": "bold"},
xticklabels=xlabels, yticklabels=ylabels)
res.set_xticklabels(res.get_xmajorticklabels(), fontsize=20, fontweight="bold", rotation=90)
res.set_yticklabels(res.get_ymajorticklabels(), fontsize=20, fontweight="bold")
plt.yticks(rotation=0)
Expand Down Expand Up @@ -615,6 +631,7 @@ def average_cell_participation(self,
plt.bar(df['index'], df[0], color=color, width=0.5)

plt.xlabel(self.top_model.name)
plt.xticks(rotation=45)
plt.title(f"Distribution of Cell-Topic participation of {self.top_model.name}")
plt.ylabel("Average cell participation")

Expand Down Expand Up @@ -764,6 +781,7 @@ def cell_participation_distribution(self,
else:
sns.boxplot(data=df, color=color, ax=ax)

plt.xticks(rotation=45)
plt.ylabel("cell participation")
plt.title(f"Distribution of Cell-Topic participation of {self.top_model.name}")

Expand Down
Loading

0 comments on commit 75365d3

Please sign in to comment.