Skip to content

Commit 46dd565

Browse files
committed
add option to show heatmap text
1 parent 4d2ac29 commit 46dd565

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

treeprofiler/layouts/staple_layouts.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class LayoutHeatmap(TreeLayout):
264264
def __init__(self, name=None, column=0, width=70, height=None,
265265
padding_x=1, padding_y=0, prop=None, internal_rep=None,
266266
value_color=None, value_range=[], color_range=None, minval=0, maxval=None,
267-
absence_color="#EBEBEB",
267+
absence_color="#EBEBEB", show_text=False,
268268
legend=True):
269269

270270
super().__init__(name)
@@ -284,6 +284,7 @@ def __init__(self, name=None, column=0, width=70, height=None,
284284
self.height = height
285285
self.padding_x = padding_x
286286
self.padding_y = padding_y
287+
self.show_text = show_text
287288

288289
def set_tree_style(self, tree, tree_style):
289290
super().set_tree_style(tree, tree_style)
@@ -305,9 +306,10 @@ def set_tree_style(self, tree, tree_style):
305306
)
306307
def set_node_style(self, node):
307308
heatmap_num = node.props.get(self.prop)
308-
309+
309310
if heatmap_num is not None and heatmap_num != 'NaN':
310311
heatmap_num = float(heatmap_num)
312+
text = "%.2f" % (heatmap_num) if self.show_text else ''
311313
if node.is_leaf:
312314
# heatmap
313315
tooltip = ""
@@ -318,13 +320,14 @@ def set_node_style(self, node):
318320

319321
gradient_color = self.value_color.get(heatmap_num)
320322
if gradient_color:
321-
identF = RectFace(width=self.width, height=self.height,
323+
identF = RectFace(width=self.width, height=self.height, text=text,
322324
color=gradient_color, padding_x=self.padding_x, padding_y=self.padding_y, tooltip=tooltip)
323325
node.add_face(identF, column = self.column, position = 'aligned')
324326

325327
elif node.is_leaf and node.props.get(self.internal_prop) is not None:
326328
heatmap_num = node.props.get(self.internal_prop)
327329
heatmap_num = float(heatmap_num)
330+
text = "%.2f" % (heatmap_num) if self.show_text else ''
328331
# heatmap
329332
tooltip = ""
330333
if node.name:
@@ -335,13 +338,14 @@ def set_node_style(self, node):
335338
gradient_color = self.value_color.get(heatmap_num)
336339

337340
if gradient_color:
338-
identF = RectFace(width=self.width, height=self.height,
341+
identF = RectFace(width=self.width, height=self.height, text=text,
339342
color=gradient_color, padding_x=self.padding_x, padding_y=self.padding_y, tooltip=tooltip)
340343
node.add_face(identF, column = self.column, position = 'aligned', collapsed_only=True)
341344

342345
elif node.props.get(self.internal_prop) is not None:
343346
heatmap_num = node.props.get(self.internal_prop)
344347
heatmap_num = float(heatmap_num)
348+
text = "%.2f" % (heatmap_num) if self.show_text else ''
345349
# heatmap
346350
tooltip = ""
347351
if node.name:
@@ -352,7 +356,7 @@ def set_node_style(self, node):
352356
gradient_color = self.value_color.get(heatmap_num)
353357

354358
if gradient_color:
355-
identF = RectFace(width=self.width, height=self.height,
359+
identF = RectFace(width=self.width, height=self.height, text=text,
356360
color=gradient_color, padding_x=self.padding_x, padding_y=self.padding_y, tooltip=tooltip)
357361
node.add_face(identF, column = self.column, position = 'aligned', collapsed_only=True)
358362

treeprofiler/tree_plot.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ def poplulate_plot_args(plot_args_p):
261261
nargs='+',
262262
required=False,
263263
help="<prop1> <prop2> names of numerical properties which need to be read as heatmap-layout")
264+
group.add_argument('--show-heatmap-text',
265+
default=False,
266+
action='store_true',
267+
help="Show gradient in heatmap-layout")
268+
264269
group.add_argument('--barplot-layout',
265270
nargs='+',
266271
required=False,
@@ -514,7 +519,7 @@ def run(args):
514519
heatmap_layouts, level = get_heatmap_layouts(tree,
515520
args.heatmap_layout, level, column_width=args.column_width,
516521
padding_x=args.padding_x, padding_y=args.padding_y,
517-
internal_rep=internal_num_rep, color_config=color_config, norm_method='min-max')
522+
internal_rep=internal_num_rep, color_config=color_config, norm_method='min-max', show_text=args.show_heatmap_text)
518523
layouts.extend(heatmap_layouts)
519524
for prop in args.heatmap_layout:
520525
visualized_props.append(prop)
@@ -524,7 +529,7 @@ def run(args):
524529
heatmap_mean_layouts, level = get_heatmap_layouts(tree,
525530
args.heatmap_mean_layout, level, column_width=args.column_width,
526531
padding_x=args.padding_x, padding_y=args.padding_y,
527-
internal_rep=internal_num_rep, color_config=color_config, norm_method='mean')
532+
internal_rep=internal_num_rep, color_config=color_config, norm_method='mean', show_text=args.show_heatmap_text)
528533
layouts.extend(heatmap_mean_layouts)
529534
for prop in args.heatmap_mean_layout:
530535
visualized_props.append(prop)
@@ -534,7 +539,7 @@ def run(args):
534539
heatmap_zscore_layouts, level = get_heatmap_layouts(tree,
535540
args.heatmap_zscore_layout, level, column_width=args.column_width,
536541
padding_x=args.padding_x, padding_y=args.padding_y,
537-
internal_rep=internal_num_rep, color_config=color_config, norm_method='zscore')
542+
internal_rep=internal_num_rep, color_config=color_config, norm_method='zscore', show_text=args.show_heatmap_text)
538543
layouts.extend(heatmap_zscore_layouts)
539544
for prop in args.heatmap_zscore_layout:
540545
visualized_props.append(prop)
@@ -1856,7 +1861,7 @@ def process_prop_values(tree, prop):
18561861

18571862
return layouts, level, prop_color_dict
18581863

1859-
def get_heatmap_layouts(tree, props, level, column_width=70, padding_x=1, padding_y=0, internal_rep='avg', color_config=None, norm_method='min-max', global_scaling=True):
1864+
def get_heatmap_layouts(tree, props, level, column_width=70, padding_x=1, padding_y=0, internal_rep='avg', color_config=None, norm_method='min-max', show_text=False, global_scaling=True):
18601865
# Helper functions for normalization
18611866
def min_max_normalize(value, minval, maxval):
18621867
return 0 if maxval - minval == 0 else (value - minval) / (maxval - minval)
@@ -1958,15 +1963,15 @@ def parse_color_config(prop, color_config, minval, maxval):
19581963
index = np.abs(index_values - normalized_value).argmin() + 1
19591964
if value not in value2color: # Ensure color is not overwritten
19601965
value2color[value] = gradientscolor.get(index, "")
1961-
1966+
19621967
# Add layout for the current property
19631968
layout = staple_layouts.LayoutHeatmap(
19641969
name=f'Heatmap_{prop}_{norm_method}', column=level,
19651970
width=column_width, padding_x=padding_x, padding_y=padding_y,
19661971
internal_rep=internal_rep, prop=prop,
19671972
maxval=maxval, minval=minval, value_color=value2color,
19681973
value_range=[minval, maxval], color_range=gradientscolor,
1969-
absence_color=nan_color
1974+
absence_color=nan_color, show_text=show_text,
19701975
)
19711976
layouts.append(layout)
19721977
level += 1

0 commit comments

Comments
 (0)