Skip to content

Commit 37ce0ce

Browse files
committed
fix bug for smax and smin in dotHeatmap
1 parent 35a8fdd commit 37ce0ce

4 files changed

Lines changed: 823 additions & 40 deletions

File tree

PyComplexHeatmap/dotHeatmap.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def dotHeatmap2d(
3131
colors=None,
3232
cmap=None,
3333
max_s=None,
34+
min_s=None,
3435
spines=False,
3536
c_na='black',
3637
**kwargs
@@ -98,6 +99,8 @@ def dotHeatmap2d(
9899
r = min(w * 72 / len(col_labels), h * 72 / len(row_labels))
99100
# r is the minimal of width and height for each scatter point, unit is point.
100101
max_s = r**2
102+
if min_s is None:
103+
min_s=max_s * 0.1
101104
# s
102105
s = kwargs.pop("s", None)
103106
# print(s is None,vmin,vmax)
@@ -168,7 +171,7 @@ def dotHeatmap2d(
168171
ax.scatter(
169172
x=df1.X.values,
170173
y=df1.Y.values,
171-
s=df1.S * max_s,
174+
s=df1.S * (max_s - min_s) + min_s,
172175
c=df1.C.values,
173176
**kwargs
174177
) # vmax=vmax,vmin=vmin,
@@ -189,7 +192,7 @@ def dotHeatmap2d(
189192
ax.scatter(
190193
x=df2.X.values,
191194
y=df2.Y.values,
192-
s=df2.S * max_s,
195+
s=df2.S * (max_s - min_s) + min_s,
193196
c=df2.C.values,
194197
**kwargs
195198
)
@@ -204,7 +207,7 @@ def dotHeatmap2d(
204207
ax.scatter(
205208
x=df1.X.values,
206209
y=df1.Y.values,
207-
s=df1.S * max_s,
210+
s=df1.S * (max_s - min_s) + min_s,
208211
c=df1.C.values,
209212
**kwargs
210213
)
@@ -325,7 +328,7 @@ def __init__(
325328
spines=False,
326329
grid='minor',
327330
max_s=None,
328-
min_s=0.01
331+
min_s=None,
329332
**kwargs
330333
):
331334
# if not hue is None:
@@ -393,8 +396,9 @@ def format_data(self, data, mask=None, z_score=None, standard_scale=None):
393396
raise ValueError("s must be a str, int or float!")
394397

395398
if not self.smin is None: #s is a dataframe, perform standard normalization.
399+
# scale to [0,1], then s * (max_size - min_size) + min_size
396400
delta=self.smax-self.smin
397-
self.kwargs["s"]=self.kwargs["s"].applymap(lambda x:max((x-self.smin)/delta,self.min_s))
401+
self.kwargs["s"]=self.kwargs["s"].applymap(lambda x:(x-self.smin)/delta)
398402

399403
# c
400404
if not self.c is None:
@@ -458,12 +462,6 @@ def plot_matrix(self, row_order, col_order):
458462
nrows = len(row_order)
459463
ncols = len(col_order)
460464

461-
ratio=self.kwargs.pop('ratio',None)
462-
if not ratio is None:
463-
print("Warning: ratio is deprecated, please use max_s instead")
464-
if self.max_s is None:
465-
self.max_s = ratio
466-
467465
if self.max_s is None:
468466
# The unit of size for the s parameter is squared points. This means
469467
# that the area of the marker is specified in points squared.
@@ -474,16 +472,21 @@ def plot_matrix(self, row_order, col_order):
474472
self.ax_heatmap.get_window_extent().width / self.ax_heatmap.figure.dpi,
475473
self.ax_heatmap.get_window_extent().height / self.ax_heatmap.figure.dpi,
476474
) # unit is inch
477-
r = min(w * 72 / self.data2d.shape[1], h * 72 / self.data2d.shape[0])
475+
r = min(w * 72 / self.data2d.shape[1], h * 72 / self.data2d.shape[0]) # r = d / 2
478476
# r is the minimal of width and height for each scatter point, unit is point.
479-
max_s = r ** 2
477+
self.max_s = r ** 2
480478
if self.verbose >= 1:
481-
print(f"Inferred max_s (max size of scatter point) is: {max_s}")
479+
print(f"Inferred max_s (max size of scatter point) is: {self.max_s}")
482480
else:
483-
max_s = self.max_s
481+
# max_s = self.max_s
484482
if self.verbose >= 1:
485-
print(f"Using user provided max_s: {max_s}")
486-
self.kwargs['max_s'] = max_s
483+
print(f"Using user provided max_s: {self.max_s}")
484+
if self.min_s is None:
485+
self.min_s=self.max_s * 0.1
486+
# else:
487+
# min_s=self.min_s
488+
self.kwargs['max_s'] = self.max_s
489+
self.kwargs['min_s'] = self.min_s
487490
self.col_split_gap_pixel = self.col_split_gap * mm2inch * self.ax.figure.dpi
488491
self.wspace = (
489492
(self.col_split_gap_pixel * ncols)
@@ -617,13 +620,21 @@ def collect_legends(self):
617620
# s=self.kwargs.get('s',None)
618621
# colors=self.kwargs.get('colors',None)
619622
markers1 = {}
620-
max_s = self.kwargs['max_s']
623+
max_s = self.max_s
624+
min_s = self.min_s
625+
smax=self.smax
626+
smin=self.smin if not self.smin is None else 0
627+
# print(max_s,min_s,smax,smin)
621628
ms = {}
622-
for f in [1, 0.8, 0.6, 0.4, 0.2]:
623-
k = str(round(f * self.smax, 2))
629+
# for f in [1, 0.8, 0.6, 0.4, 0.2]:
630+
# k = str(round(f * self.smax, 2))
631+
# markers1[k] = self.dot_legend_marker
632+
# ms[k] = f * np.sqrt(max_s) * self.alpha
633+
for ratio in np.linspace(1,smin / smax,5):
634+
k = str(round(ratio * smax, 2))
624635
markers1[k] = self.dot_legend_marker
625-
ms[k] = f * np.sqrt(max_s) * self.alpha
626-
# ms[k] = np.sqrt(f * max_s * self.alpha)
636+
ms[k] = ratio * np.sqrt(max_s) * self.alpha
637+
print(ms)
627638
title = self.s if not self.s is None else self.value
628639
self.legend_dict[f"{title} (dot)"]=tuple(
629640
[

notebooks/dotHeatmap.ipynb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 2,
5+
"execution_count": 1,
66
"id": "57e9d138-6953-4426-83c3-f4682fca20aa",
77
"metadata": {},
88
"outputs": [],
@@ -26,7 +26,9 @@
2626
{
2727
"cell_type": "markdown",
2828
"id": "d4b1aff5-d4c4-43b8-8578-84472db22de3",
29-
"metadata": {},
29+
"metadata": {
30+
"jp-MarkdownHeadingCollapsed": true
31+
},
3032
"source": [
3133
"## Load an example brain networks dataset from seaborn"
3234
]
@@ -409,7 +411,9 @@
409411
{
410412
"cell_type": "markdown",
411413
"id": "599e9968-58ea-412e-8337-802d5ac7cff9",
412-
"metadata": {},
414+
"metadata": {
415+
"jp-MarkdownHeadingCollapsed": true
416+
},
413417
"source": [
414418
"# Dot Heatmap"
415419
]
@@ -1514,14 +1518,6 @@
15141518
" show_rownames=True,verbose=1,legend_gap=7,spines=True,dot_legend_marker='D')\n",
15151519
"plt.show()"
15161520
]
1517-
},
1518-
{
1519-
"cell_type": "code",
1520-
"execution_count": null,
1521-
"id": "dc8df1e2-f84c-495c-ac41-5af9a84a80bd",
1522-
"metadata": {},
1523-
"outputs": [],
1524-
"source": []
15251521
}
15261522
],
15271523
"metadata": {

notebooks/gene_enrichment_analysis.ipynb

Lines changed: 9 additions & 7 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)