Skip to content

Commit

Permalink
#17 unittests and DetectPrecision() method were fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim55667757 committed Jan 2, 2023
1 parent 8d52307 commit 73d1cd2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
10 changes: 7 additions & 3 deletions pricegenerator/PriceGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,19 @@ def DetectPrecision(self, examples):
"""
Auto-detect precision from example values. E.g. 0.123 => 3, 0.12345 => 5 and so on.
This method set `self.precision` variable after detect precision.
See also about statistics.mode: https://docs.python.org/3.9/library/statistics.html#statistics.mode
Changed in Python version >= 3.8: Now mode() method handles multimodal datasets by returning the first mode encountered.
Previously, it raised StatisticsError when more than one mode was found.
:param examples: numpy.ndarray with examples of float values.
"""
uLogger.debug("Detecting precision of data values...")
try:
if self.precision != 0:
self.precision = mode(list(map(lambda x: len(str(x).split('.')[-1]) if len(str(x).split('.')) > 1 else 0, examples)))
self.precision = mode(list(map(lambda x: len(str(x).split('.')[-1]) if len(str(x).split('.')) > 1 else 0, examples))) if self.precision >= 0 else 0

except StatisticsError as e:
uLogger.warning("Unable to unambiguously determine Mode() of the value of precision! Precision is set to 2 (default). StatisticsError: '{}'".format(e))
uLogger.debug("Statistic mode() method return an error! Precision is set to 2 (default). StatisticsError: '{}'".format(e))
self.precision = 2

finally:
Expand Down
34 changes: 21 additions & 13 deletions tests/test_PriceGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,28 @@ def test_PandasDFheaders(self):
assert list(self.model.prices) == headers, "Expected headers: {}".format(headers)

def test_DetectPrecision(self):
"""
See also about statistics.mode: https://docs.python.org/3.9/library/statistics.html#statistics.mode
Changed in Python version >= 3.8: Now mode() method handles multimodal datasets by returning the first mode encountered.
Previously, it raised StatisticsError when more than one mode was found.
"""
testData = [
[np.array([1, 2, 3], dtype=float), 1], # precision can be 1
[np.array([1., 2., 3.], dtype=float), 1], # precision can be 1
[np.array([1.1, 2.2, 3.3], dtype=float), 1], # precision can be 1
[np.array([1.11, 2.2, 3.3], dtype=float), 1], # precision can be 1
[np.array([1.11, 2.22, 3.3], dtype=float), 2], # precision can be 2
[np.array([1.11, 2.22, 3.33], dtype=float), 2], # precision can be 2
[np.array([1.11, 2.22, 3.3, 4.4], dtype=float), 2], # precision can be 2 by default because mode() error
[np.array([1.111, 2.22, 3.333, 4.444], dtype=float), 3], # precision can be 3
[np.array([1.111, 2.222, 3.3, 4.4], dtype=float), 2], # precision can be 2 by default because mode() error
[np.array([1.1111, 2.2222, 3.3333, 4.4], dtype=float), 4], # precision can be 4
[np.array([1.1111, 2., 3., 4.], dtype=float), 1], # precision can be 1
[np.array([1.1111, 2.2222, 3., 4.], dtype=float), 2], # precision can be 2 by default because mode() error
[np.array([1.1111, 2.2222, 3.1, 4.44], dtype=float), 4], # precision can be 4
[np.array([1, 2, 3], dtype=float), 1], # 1
[np.array([1., 2., 3.], dtype=float), 1], # 1
[np.array([1.1, 2.2, 3.3], dtype=float), 1], # 1
[np.array([1.11, 2.2, 3.3], dtype=float), 1], # 1
[np.array([1.11, 2.22, 3.3], dtype=float), 2], # 2
[np.array([1.11, 2.22, 3.33], dtype=float), 2], # 2
[np.array([1.11, 2.22, 3.3, 4.4], dtype=float), 2], # 2
[np.array([1.111, 2.22, 3.333, 4.444], dtype=float), 3], # 3
[np.array([1.111, 2.222, 3.3, 4.4], dtype=float), 3], # 3
[np.array([1.1111, 2.2222, 3.3333, 4.4], dtype=float), 4], # 4
[np.array([1.1111, 2., 3., 4.], dtype=float), 1], # 1
[np.array([1.1111, 2.2222, 3., 4.], dtype=float), 4], # 4
[np.array([1.1111, 2.2222, 3.1, 4.44], dtype=float), 4], # 4
[np.array([1.11, 2.222, 3.333, 4.44], dtype=float), 2], # 2
[np.array([1.111, 2.22, 3.33, 4.444], dtype=float), 3], # 3
[np.array([1.1111, 2.222, 3.33, 4.44], dtype=float), 2], # 2
]
for test in testData:
self.model.DetectPrecision(test[0])
Expand Down

0 comments on commit 73d1cd2

Please sign in to comment.