Skip to content

Commit

Permalink
Fix KL 2D
Browse files Browse the repository at this point in the history
  • Loading branch information
borcuttjahns committed Sep 20, 2023
1 parent fd8bfe6 commit a8bdeca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 5 additions & 4 deletions bicytok/figures/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def KL_divergence_2D(dataset, signal_receptor, target_cells, ax):
target_receptor_counts[:, 1] *= conversion_factor
off_target_receptor_counts[:, 1] *= conversion_factor

KL_div = calculate_kl_divergence_2D(target_receptor_counts[:, 1], off_target_receptor_counts[:, 1])
KL_div = calculate_kl_divergence_2D(target_receptor_counts[:, 0:2], off_target_receptor_counts[:, 0:2])
if np.mean(target_receptor_counts[:, 1]) > np.mean(off_target_receptor_counts[:, 1]):
results.append((KL_div, receptor_name, signal_receptor))
else:
Expand Down Expand Up @@ -738,11 +738,12 @@ def EMD_2D_pair(dataset, target_cells, signal_receptor, special_receptor):
return optimal_transport

def calculate_kl_divergence_2D(targCellMark, offTargCellMark):
kdeTarg = KernelDensity(kernel='gaussian').fit(targCellMark.reshape(-1, 1))
kdeOffTarg = KernelDensity(kernel='gaussian').fit(offTargCellMark.reshape(-1, 1))
kdeTarg = KernelDensity(kernel='gaussian').fit(targCellMark.reshape(-1, 2))
kdeOffTarg = KernelDensity(kernel='gaussian').fit(offTargCellMark.reshape(-1, 2))
minVal = np.minimum(targCellMark.min(), offTargCellMark.min()) - 10
maxVal = np.maximum(targCellMark.max(), offTargCellMark.max()) + 10
outcomes = np.arange(minVal, maxVal + 1).reshape(-1, 1)
X, Y = np.mgrid[minVal:maxVal:((maxVal-minVal) / 100), minVal:maxVal:((maxVal-minVal) / 100)]
outcomes = np.concatenate((X.reshape(-1, 1), Y.reshape(-1, 1)), axis=1)
distTarg = np.exp(kdeTarg.score_samples(outcomes))
distOffTarg = np.exp(kdeOffTarg.score_samples(outcomes))
KL_div = stats.entropy(distOffTarg.flatten() + 1e-200, distTarg.flatten() + 1e-200, base=2)
Expand Down
6 changes: 4 additions & 2 deletions bicytok/figures/figure4.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ def makeFigure():
ax, f = getSetup((40, 40), (1,1))
target_cells = 'Treg'
######################################################
'''
resultsEMD = []
for receptor in receptors:
val = EMD_2D(new_df, receptor, target_cells, ax = None)
print(val)
resultsEMD.append(val)
flattened_results = [result_tuple for inner_list in resultsEMD for result_tuple in inner_list]
# Create a DataFrame from the flattened_results
Expand All @@ -52,7 +54,7 @@ def makeFigure():
'''

resultsKL = []
for receptor in receptors:
for receptor in receptors[0:5]:
val = KL_divergence_2D(new_df, receptor, target_cells, ax = None)
resultsKL.append(val)
flattened_resultsKL = [result_tuple for inner_list in resultsKL for result_tuple in inner_list]
Expand All @@ -67,5 +69,5 @@ def makeFigure():
ax[0].set_xlabel('Receptor')
ax[0].set_ylabel('Receptor')
ax[0].set_title('KL Heatmap')
'''

return f

0 comments on commit a8bdeca

Please sign in to comment.