Skip to content

Commit

Permalink
fix normalized and deprecate numpy asscalar
Browse files Browse the repository at this point in the history
- fix normalized as per 3adf692
- Deprecated use of numpy asscalar as per linkedin#64 and
  EchoDel@7269a35
  • Loading branch information
earthgecko committed Jan 20, 2023
1 parent dcf2ed8 commit 4fd8e0f
Showing 1 changed file with 53 additions and 9 deletions.
62 changes: 53 additions & 9 deletions src/luminol/modules/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,17 @@ def add_offset(self, offset):

def normalize(self):
"""
Return a new time series with all values normalized to 0 to 1.
Return a new time series with all values normalized between 0 and 1.
:return: `None`
"""
maximum = self.max()
if maximum:
self.values = [value / maximum for value in self.values]
# @modified 20180108 - fix normalized
# self.values = [value / maximum for value in self.values]
minimum = self.min()
if minimum:
self.values = [((value - minimum) / (maximum - minimum)) for value in self.values]

def crop(self, start_timestamp, end_timestamp):
"""
Expand Down Expand Up @@ -316,7 +320,11 @@ def average(self, default=None):
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the average value or `None`.
"""
return numpy.asscalar(numpy.average(self.values)) if self.values else default
# @modified 20230120 - Task #4820: Deprecation of numpy asscalar function in luminol
# As per https://github.com/linkedin/luminol/pull/64
# https://github.com/EchoDel/luminol/commit/7269a35e12d2dc1c7a7db9e8d5a71558cb6c9935
# return numpy.asscalar(numpy.average(self.values)) if self.values else default
return numpy.average(self.values).item() if self.values else default

def median(self, default=None):
"""
Expand All @@ -325,7 +333,11 @@ def median(self, default=None):
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the median value or `None`.
"""
return numpy.asscalar(numpy.median(self.values)) if self.values else default
# @modified 20230120 - Task #4820: Deprecation of numpy asscalar function in luminol
# As per https://github.com/linkedin/luminol/pull/64
# https://github.com/EchoDel/luminol/commit/7269a35e12d2dc1c7a7db9e8d5a71558cb6c9935
# return numpy.asscalar(numpy.median(self.values)) if self.values else default
return numpy.median(self.values).item() if self.values else default

def max(self, default=None):
"""
Expand All @@ -334,7 +346,11 @@ def max(self, default=None):
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the maximum value or `None`.
"""
return numpy.asscalar(numpy.max(self.values)) if self.values else default
# @modified 20230120 - Task #4820: Deprecation of numpy asscalar function in luminol
# As per https://github.com/linkedin/luminol/pull/64
# https://github.com/EchoDel/luminol/commit/7269a35e12d2dc1c7a7db9e8d5a71558cb6c9935
# return numpy.asscalar(numpy.max(self.values)) if self.values else default
return numpy.max(self.values).item() if self.values else default

def min(self, default=None):
"""
Expand All @@ -343,7 +359,11 @@ def min(self, default=None):
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the maximum value or `None`.
"""
return numpy.asscalar(numpy.min(self.values)) if self.values else default
# @modified 20230120 - Task #4820: Deprecation of numpy asscalar function in luminol
# As per https://github.com/linkedin/luminol/pull/64
# https://github.com/EchoDel/luminol/commit/7269a35e12d2dc1c7a7db9e8d5a71558cb6c9935
# return numpy.asscalar(numpy.min(self.values)) if self.values else default
return numpy.min(self.values).item() if self.values else default

def percentile(self, n, default=None):
"""
Expand All @@ -353,7 +373,11 @@ def percentile(self, n, default=None):
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the Nth percentile value or `None`.
"""
return numpy.asscalar(numpy.percentile(self.values, n)) if self.values else default
# @modified 20230120 - Task #4820: Deprecation of numpy asscalar function in luminol
# As per https://github.com/linkedin/luminol/pull/64
# https://github.com/EchoDel/luminol/commit/7269a35e12d2dc1c7a7db9e8d5a71558cb6c9935
# return numpy.asscalar(numpy.percentile(self.values, n)) if self.values else default
return numpy.percentile(self.values, n).item() if self.values else default

def stdev(self, default=None):
"""
Expand All @@ -362,7 +386,11 @@ def stdev(self, default=None):
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the standard deviation value or `None`.
"""
return numpy.asscalar(numpy.std(self.values)) if self.values else default
# @modified 20230120 - Task #4820: Deprecation of numpy asscalar function in luminol
# As per https://github.com/linkedin/luminol/pull/64
# https://github.com/EchoDel/luminol/commit/7269a35e12d2dc1c7a7db9e8d5a71558cb6c9935
# return numpy.asscalar(numpy.std(self.values)) if self.values else default
return numpy.std(self.values).item() if self.values else default

def sum(self, default=None):
"""
Expand All @@ -371,4 +399,20 @@ def sum(self, default=None):
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the sum or `None`.
"""
return numpy.asscalar(numpy.sum(self.values)) if self.values else default
# @modified 20230120 - Task #4820: Deprecation of numpy asscalar function in luminol
# As per https://github.com/linkedin/luminol/pull/64
# https://github.com/EchoDel/luminol/commit/7269a35e12d2dc1c7a7db9e8d5a71558cb6c9935
# return numpy.asscalar(numpy.sum(self.values)) if self.values else default
return numpy.sum(self.values).item() if self.values else default

# @added 20180110 - use mean as per Paul Bourke method
def mean(self, default=None):
"""
Calculate the mean value over the time series.
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the mean value or `None`.
"""
# @modified 20230120 - Task #4820: Deprecation of numpy asscalar function in luminol
# return numpy.asscalar(numpy.mean(self.values)) if self.values else default
return numpy.mean(self.values).item() if self.values else default

0 comments on commit 4fd8e0f

Please sign in to comment.