forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroundinghelper.py
43 lines (35 loc) · 1.3 KB
/
roundinghelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
from mantid.kernel import Direction, StringListValidator
import numpy
'''
This file contains functions which deal with rounding in algorithms
such as BinWidthAtX and MedianBinWidth.
'''
# Name of the rounding mode property
PROP_NAME_ROUNDING_MODE = 'Rounding'
# Available rounding modes.
ROUNDING_NONE = 'None'
ROUNDING_TEN_TO_INT = '10^n'
def declare_rounding_property(o):
'''
Declares the properties needed for rounding.
'''
rounding = StringListValidator()
rounding.addAllowedValue(ROUNDING_NONE)
rounding.addAllowedValue(ROUNDING_TEN_TO_INT)
o.declareProperty(name=PROP_NAME_ROUNDING_MODE,
defaultValue=ROUNDING_NONE, validator=rounding,
direction=Direction.Input, doc='Bin width rounding')
def round(x, mode):
'''
Rounds x depending on the rounding mode selected.
'''
if mode == ROUNDING_TEN_TO_INT:
rounded = 10.0**numpy.floor(numpy.log10(numpy.abs(x)))
return numpy.copysign(rounded, x)
return x