Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Weight scale #87

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d470838
Add functions to get closest value and convert a single value
rowleya May 1, 2020
2af9ec9
Round to nearest when using integer types
rowleya May 20, 2020
9ad8640
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Jul 28, 2020
f225c08
Add function to calculate closest value above
andrewgait Jul 30, 2020
83d789d
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Jul 30, 2020
64dc685
Python 2 is silly
andrewgait Jul 30, 2020
31a7d2f
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Aug 20, 2020
751db09
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Oct 15, 2020
886cdb8
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Jan 15, 2021
357f358
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Feb 5, 2021
ec886e8
Merge branch 'master' into weight_scale
andrewgait Feb 26, 2021
34b0258
Merge branch 'master' into weight_scale
dkfellows Mar 26, 2021
0ee006f
Merge branch 'master' into weight_scale
andrewgait Apr 29, 2021
2b63ee6
Merge branch 'master' into weight_scale
andrewgait Jun 10, 2021
d4b6c71
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Jul 9, 2021
b348efc
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Jul 9, 2021
df53dd7
Merge branch 'master' into weight_scale
andrewgait Nov 11, 2021
e6ad077
Merge branch 'master' into weight_scale
andrewgait Feb 25, 2022
8b1fbc0
Merge branch 'master' into weight_scale
andrewgait May 19, 2022
6c2dc3d
Unittest new functions for closest representable values
andrewgait May 26, 2022
abf63a6
Merge branch 'master' into weight_scale
andrewgait Jun 16, 2022
fc238e2
Merge branch 'master' into weight_scale
andrewgait Jul 11, 2022
38e6088
Merge branch 'master' into weight_scale
andrewgait Aug 3, 2022
9c5dec2
Merge remote-tracking branch 'origin/master' into weight_scale
andrewgait Nov 18, 2022
da6e8b7
Merge branch 'master' into weight_scale
andrewgait Jan 11, 2023
f609bc3
Merge branch 'master' into weight_scale
andrewgait Jan 11, 2023
1ddd7e1
Merge branch 'master' into weight_scale
andrewgait Feb 20, 2023
46fcc07
Merge branch 'master' into weight_scale
andrewgait Mar 1, 2023
c3240b0
Merge branch 'master' into weight_scale
andrewgait Apr 11, 2023
59fb1c4
Merge branch 'master' into weight_scale
andrewgait Apr 21, 2023
fa929f5
Merge branch 'master' into weight_scale
andrewgait May 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions data_specification/enums/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
import numpy as np


def _round_to_int(value):
""" Rounds a number to the closest integer

:param float value: The value to round
:rtype: int
"""
if isinstance(value, int):
return value
return int(round(value))


class DataType(Enum):
"""
Supported data types.
Expand Down Expand Up @@ -49,7 +60,7 @@ class DataType(Enum):
decimal.Decimal("1"),
"B",
False,
int,
_round_to_int,
np.uint8,
"8-bit unsigned integer")
#: 16-bit unsigned integer
Expand All @@ -60,7 +71,7 @@ class DataType(Enum):
decimal.Decimal("1"),
"H",
False,
int,
_round_to_int,
np.uint16,
"16-bit unsigned integer")
#: 32-bit unsigned integer
Expand All @@ -71,7 +82,7 @@ class DataType(Enum):
decimal.Decimal("1"),
"I",
False,
int,
_round_to_int,
np.uint32,
"32-bit unsigned integer")
#: 64-bit unsigned integer
Expand All @@ -82,7 +93,7 @@ class DataType(Enum):
decimal.Decimal("1"),
"Q",
False,
int,
_round_to_int,
np.uint64,
"64-bit unsigned integer")
#: 8-bit signed integer
Expand All @@ -93,7 +104,7 @@ class DataType(Enum):
decimal.Decimal("1"),
"b",
False,
int,
_round_to_int,
np.int8,
"8-bit signed integer")
#: 16-bit signed integer
Expand All @@ -104,7 +115,7 @@ class DataType(Enum):
decimal.Decimal("1"),
"h",
False,
int,
_round_to_int,
np.int16,
"16-bit signed integer")
#: 32-bit signed integer
Expand All @@ -115,7 +126,7 @@ class DataType(Enum):
decimal.Decimal("1"),
"i",
False,
int,
_round_to_int,
np.int32,
"32-bit signed integer")
#: 64-bit signed integer
Expand All @@ -126,7 +137,7 @@ class DataType(Enum):
decimal.Decimal("1"),
"q",
False,
int,
_round_to_int,
np.int64,
"64-bit signed integer")
#: 8.8 unsigned fixed point number
Expand Down Expand Up @@ -386,6 +397,29 @@ def numpy_typename(self):
"""
return self._numpy_typename

def closest_representable_value(self, value):
""" Returns the closest value to the given value that can be
represented by this type

:param value:
:type value: float or in
:rtype: float
"""
return self.decode_from_int(self.encode_as_int(value))

def closest_representable_value_above(self, value):
""" Returns the closest value above the given value that can be
represented by this type

:param value:
:type value: float or in
:rtype: float
"""
closest_value = self.decode_from_int(self.encode_as_int(value))
if closest_value >= value:
return closest_value
return self.decode_from_int(self.encode_as_int(value)+1)

def encode_as_int(self, value):
"""
Returns the value as an integer, according to this type.
Expand Down Expand Up @@ -462,6 +496,14 @@ def decode_numpy_array(self, array):
"""
return array / float(self._scale)

def decode_from_int(self, value):
""" Decode a single value represented as an int according to this type.

:param int array:
:rtype: float or int
"""
return value / float(self._scale)

def decode_array(self, values):
"""
Decodes a byte array into iterable of this type.
Expand Down
5 changes: 5 additions & 0 deletions unittests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ def test_data_type_enum(self):
self.assertEqual(
DataType.S1615.max, decimal.Decimal("65535.999969482421875"))

self.assertEqual(
DataType.S1615.closest_representable_value(1.00001), 1.0)
self.assertEqual(
DataType.S1615.closest_representable_value_above(0.99997), 1.0)

self.assertEqual(DataType.S3231.value, 13)
self.assertEqual(DataType.S3231.size, 8)
self.assertEqual(DataType.S3231.min, decimal.Decimal("-4294967296"))
Expand Down