Skip to content

Commit d357303

Browse files
Make health pill text round ints.
Also fixed the check for whether valid values exist (in deciding whether to show numbers on top of health pills). Previously, the indices were off. Change: 149511724
1 parent 9781be4 commit d357303

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

tensorflow/tensorboard/components/tf_graph_common/lib/scene/scene.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,15 @@ export function positionEllipse(ellipse, cx: number, cy: number,
507507

508508
/**
509509
* @param {number} stat A stat for a health pill (such as mean or variance).
510+
* @param {boolean} shouldRoundOnesDigit Whether to round this number to the
511+
* ones digit. Useful for say int, uint, and bool output types.
510512
* @return {string} A human-friendly string representation of that stat.
511513
*/
512-
function _humanizeHealthPillStat(stat) {
514+
export function humanizeHealthPillStat(stat, shouldRoundOnesDigit) {
515+
if (shouldRoundOnesDigit) {
516+
return stat.toFixed(0);
517+
}
518+
513519
if (Math.abs(stat) >= 1) {
514520
return stat.toFixed(1);
515521
}
@@ -521,7 +527,7 @@ function _humanizeHealthPillStat(stat) {
521527
*/
522528
function _addHealthPill(
523529
nodeGroupElement: SVGElement, healthPill: HealthPill,
524-
nodeInfo: render.RenderGroupNodeInfo) {
530+
nodeInfo: render.RenderNodeInfo) {
525531
// Check if text already exists at location.
526532
d3.select(nodeGroupElement.parentNode).selectAll('.health-pill').remove();
527533

@@ -605,13 +611,33 @@ function _addHealthPill(
605611
healthPillY += nodeInfo.labelOffset;
606612
}
607613

608-
if (lastHealthPillOverview[4] || lastHealthPillOverview[5] ||
609-
lastHealthPillOverview[6]) {
610-
// At least 1 "non-Inf and non-NaN" value exists. Show stats on tensor
611-
// values.
614+
if (lastHealthPillOverview[2] || lastHealthPillOverview[3] ||
615+
lastHealthPillOverview[4]) {
616+
// At least 1 "non-Inf and non-NaN" value exists (a -, 0, or + value). Show
617+
// stats on tensor values.
618+
619+
// Determine if we should display the output range as integers.
620+
let shouldRoundOnesDigit = false;
621+
let node = nodeInfo.node as OpNode;
622+
let attributes = node.attr;
623+
if (attributes && attributes.length) {
624+
// Find the attribute for output type if there is one.
625+
for (let i = 0; i < attributes.length; i++) {
626+
if (attributes[i].key === 'T') {
627+
// Note whether the output type is an integer.
628+
let outputType = attributes[i].value['type'];
629+
shouldRoundOnesDigit =
630+
outputType && /^DT_(BOOL|INT|UINT)/.test(outputType);
631+
break;
632+
}
633+
}
634+
}
635+
612636
let statsSvg = document.createElementNS(svgNamespace, 'text');
613-
const minString = _humanizeHealthPillStat(lastHealthPillData[8]);
614-
const maxString = _humanizeHealthPillStat(lastHealthPillData[9]);
637+
const minString =
638+
humanizeHealthPillStat(lastHealthPillData[8], shouldRoundOnesDigit);
639+
const maxString =
640+
humanizeHealthPillStat(lastHealthPillData[9], shouldRoundOnesDigit);
615641
statsSvg.textContent = minString + ' ~ ' + maxString;
616642
statsSvg.classList.add('health-pill-stats');
617643
statsSvg.setAttribute('x', String(healthPillWidth / 2));
@@ -641,7 +667,7 @@ export function addHealthPills(
641667

642668
let svgRootSelection = d3.select(svgRoot);
643669
svgRootSelection.selectAll('g.nodeshape')
644-
.each(function(nodeInfo: render.RenderGroupNodeInfo) {
670+
.each(function(nodeInfo: render.RenderNodeInfo) {
645671
// Only show health pill data for this node if it is available.
646672
let healthPills = nodeNamesToHealthPills[nodeInfo.node.name];
647673
let healthPill = healthPills ? healthPills[healthPillStepIndex] : null;

tensorflow/tensorboard/components/tf_graph_common/test/graph-test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,22 @@ suite('graph', () => {
8282
});
8383
});
8484

85+
test('health pill numbers round correctly', () => {
86+
// Integers are rounded to the ones place.
87+
assert.equal(tf.graph.scene.humanizeHealthPillStat(42.0, true), '42');
88+
89+
// Numbers with magnitude >= 1 are rounded to the tenths place.
90+
assert.equal(tf.graph.scene.humanizeHealthPillStat(1, false), '1.0');
91+
assert.equal(tf.graph.scene.humanizeHealthPillStat(42.42, false), '42.4');
92+
assert.equal(tf.graph.scene.humanizeHealthPillStat(-42.42, false), '-42.4');
93+
94+
// Numbers with magnitude < 1 are written in scientific notation rounded to
95+
// the tenths place.
96+
assert.equal(tf.graph.scene.humanizeHealthPillStat(0, false), '0.0e+0');
97+
assert.equal(tf.graph.scene.humanizeHealthPillStat(0.42, false), '4.2e-1');
98+
assert.equal(
99+
tf.graph.scene.humanizeHealthPillStat(-0.042, false), '-4.2e-2');
100+
});
101+
85102
// TODO(bp): write tests.
86103
});

0 commit comments

Comments
 (0)