Skip to content

Commit

Permalink
MAINT: use functools for memoization
Browse files Browse the repository at this point in the history
- replace `@cached_var` with `@functools.lru_cache`
  and `@cached_const` with `@functools.cached_property`
- refactor calls of any `cached_property`, now not callable anymore,
  substitute with `self.set_link_attribute(property)` where necessary
- remove `cache_helper` and `cache` attribute in `core.network`
- adapt `clear_cache()` methods accordingly,
  introduce `clear_cache_startswith()`
- remove `Network.clear_link_attribute()`
- adapt `Network.{del|set}_link_attribute()` accordingly,
  drop clearing cache of `path_lengths()` in `set_link_attribute()`
- related issue: #148

- locally disable pylint false positive `not-callable` on
  `multiprocess.Pool()`
  • Loading branch information
fkuehlein committed Nov 28, 2023
1 parent 157567b commit fe23f23
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 231 deletions.
16 changes: 10 additions & 6 deletions src/pyunicorn/climate/climate_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# Import essential packages
#

# Import decorator for memoization
from functools import cached_property

# Import NumPy for the array object and fast numerics
import numpy as np

Expand All @@ -28,7 +31,6 @@

# Import GeoNetwork and GeoGrid classes
from ..core import GeoNetwork, GeoGrid
from ..core.network import cached_const


#
Expand Down Expand Up @@ -626,7 +628,7 @@ def set_link_density(self, link_density):
threshold = self.threshold_from_link_density(link_density)
self.set_threshold(threshold)

@cached_const('base', 'correlation_distance')
@cached_property
def correlation_distance(self):
"""
Return correlation weighted distances between nodes.
Expand Down Expand Up @@ -658,14 +660,14 @@ def correlation_distance(self):
"""
return self.similarity_measure() * self.grid.angular_distance()

@cached_const('base', 'inv_correlation_distance')
@cached_property
def inv_correlation_distance(self):
"""
Return correlation weighted distances between nodes.
:rtype: 2D matrix [index, index]
"""
m = self.correlation_distance()
m = self.correlation_distance
np.fill_diagonal(m, np.inf)
self.set_link_attribute('inv_correlation_distance', 1 / m)
return 1 / m
Expand Down Expand Up @@ -694,7 +696,8 @@ def correlation_distance_weighted_closeness(self):
:rtype: 1D Numpy array [index]
:return: the correlation distance weighted closeness sequence.
"""
self.inv_correlation_distance()
self.set_link_attribute(
'inv_correlation_distance', self.inv_correlation_distance)
return self.closeness('inv_correlation_distance')

def local_correlation_distance_weighted_vulnerability(self):
Expand All @@ -717,5 +720,6 @@ def local_correlation_distance_weighted_vulnerability(self):
:return: the local correlation distance weighted vulnerability
sequence.
"""
self.inv_correlation_distance()
self.set_link_attribute(
'inv_correlation_distance', self.inv_correlation_distance)
return self.local_vulnerability('inv_correlation_distance')
12 changes: 7 additions & 5 deletions src/pyunicorn/climate/tsonis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
# Import essential packages
#

# Import decorator for memoization
from functools import cached_property

# Import NumPy for the array object and fast numerics
import numpy as np

from .climate_network import ClimateNetwork
from .climate_data import ClimateData
from ..core.network import cached_const


#
Expand Down Expand Up @@ -197,7 +199,7 @@ def calculate_similarity_measure(self, anomaly):
"""
return self._calculate_correlation(anomaly)

@cached_const('base', 'correlation')
@cached_property
def correlation(self):
"""
Return the correlation matrix at zero lag.
Expand Down Expand Up @@ -289,7 +291,7 @@ def correlation_weighted_average_path_length(self):
:return float: the correlation weighted average path length.
"""
self.correlation()
self.set_link_attribute('correlation', self.correlation)
return self.average_path_length('correlation')

def correlation_weighted_closeness(self):
Expand All @@ -305,7 +307,7 @@ def correlation_weighted_closeness(self):
:rtype: 1D Numpy array [index]
:return: the correlation weighted closeness sequence.
"""
self.correlation()
self.set_link_attribute('correlation', self.correlation)
return self.closeness('correlation')

def local_correlation_weighted_vulnerability(self):
Expand All @@ -321,5 +323,5 @@ def local_correlation_weighted_vulnerability(self):
:rtype: 1D Numpy array [index]
:return: the correlation weighted vulnerability sequence.
"""
self.correlation()
self.set_link_attribute('correlation', self.correlation)
return self.local_vulnerability('correlation')
2 changes: 1 addition & 1 deletion src/pyunicorn/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# Import classes
#

from .network import Network, NetworkError, nz_coords, cached_const
from .network import Network, NetworkError, nz_coords
from .spatial_network import SpatialNetwork
from .geo_network import GeoNetwork
from .grid import Grid
Expand Down
2 changes: 1 addition & 1 deletion src/pyunicorn/core/interacting_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ def internal_global_clustering(self, node_list):
:return float: the internal global clustering coefficient for a
subnetwork.
"""
clustering = self.local_clustering()
clustering = self.local_clustering
internal_clustering = clustering[node_list].mean()
return internal_clustering

Expand Down
Loading

0 comments on commit fe23f23

Please sign in to comment.