Skip to content

Commit

Permalink
re-usable component to show quantity with significant decimal digits
Browse files Browse the repository at this point in the history
* used in line analysis table
* can pass the maximum number of decimal digits (which becomes the default if no uncertainty is passed)
* if uncertainty is passed, both the value and uncertainty are rounded to the second significant digit in the uncertainties
* might not be entirely perfect - javascript treatment of numbers can be a bit funny... but the full underlying floats are untouched via the API
  • Loading branch information
kecnry committed Apr 12, 2022
1 parent 317e03d commit d67c673
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Other Changes and Additions

- Data dropdowns in plugins are now filtered to only applicable entries. [#1221]

- Reported quantities are rounded/truncated to avoid showing unnecessary precision. [#1244]

2.4 (2022-03-29)
================

Expand Down
4 changes: 4 additions & 0 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
os.path.join(os.path.dirname(__file__),
'components/plugin_section_header.vue'))

ipyvue.register_component_from_file(None, 'j-number-uncertainty',
os.path.join(os.path.dirname(__file__),
'components/number_uncertainty.vue'))

ipyvue.register_component_from_file(None, 'plugin-dataset-select',
os.path.join(os.path.dirname(__file__),
'components/plugin_dataset_select.vue'))
Expand Down
42 changes: 42 additions & 0 deletions jdaviz/components/number_uncertainty.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<template>
<span>
{{ valueTrunc }} {{ uncertainty ? "&#177; " + uncertTrunc : null}} {{ unit }}
</span>
</template>

<script>
module.exports = {
data: function () {
const maxDecs = this.maxDecs || 5;
var nDecs = maxDecs;
var uncertTrunc = null;
if (this.uncertainty !== '') {
// then uncertainty was provided, so let's round both the uncertainty and value
// to show up to the second significant digit in the uncertainty.
nDecs = 2 - Math.log10(parseFloat(this.uncertainty));
if (nDecs > maxDecs) {
nDecs = maxDecs
} else if (nDecs < 0) {
// NOTE: could support rounding of integers to less significant digits, but for now
// we'll at least show all digits before the decimal.
nDecs = 0
}
uncertTrunc = +parseFloat(this.uncertainty).toFixed(nDecs)
}
const valueTrunc = +parseFloat(this.value).toFixed(nDecs);
return {
nDecs: nDecs,
valueTrunc: valueTrunc,
uncertTrunc: uncertTrunc
}
},
props: ['value', 'uncertainty', 'unit', 'maxDecs'],
methods: {
}
};
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@
</a>
</j-tooltip>
</v-col>
<v-col cols=6>{{ item.result }} {{ item.uncertainty ? "&#177; " + item.uncertainty : null}} {{ item.unit }}</v-col>
<v-col cols=6>
<j-number-uncertainty
:value="item.result"
:uncertainty="item.uncertainty"
:unit="item.unit"
:maxDecs="5"
></j-number-uncertainty>
</v-col>
</v-row>
</div>
<div v-if="results_computing"
Expand Down

0 comments on commit d67c673

Please # to comment.