Skip to content

Commit

Permalink
Add weakref to dependency cache (#390)
Browse files Browse the repository at this point in the history
* add weakref to dependency cache

* bugfix
  • Loading branch information
jonathf committed Jun 10, 2022
1 parent d687105 commit 926b1ff
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions chaospy/distributions/baseclass/utils.py
@@ -1,8 +1,5 @@
"""Distribution utility functions."""
import sys
import os
import logging
from contextlib import wraps
from weakref import WeakValueDictionary
from itertools import permutations

import numpy
Expand Down Expand Up @@ -175,7 +172,7 @@ def declare_dependencies(
return dependencies, parameters, rotation


DISTRIBUTION_IDENTIFIERS = {}
DISTRIBUTION_IDENTIFIERS = WeakValueDictionary()


def init_dependencies(
Expand Down Expand Up @@ -208,7 +205,7 @@ def init_dependencies(
>>> core = chaospy.Normal()
>>> core._dependencies
[{0}]
>>> DISTRIBUTION_IDENTIFIERS
>>> dict(DISTRIBUTION_IDENTIFIERS)
{0: normal()}
>>> distribution = chaospy.Iid(core, 2)
>>> distribution._dependencies
Expand All @@ -226,7 +223,7 @@ def init_dependencies(
rotation = numpy.asarray(rotation)
assert rotation.dtype == int and rotation.ndim == 1
length = len(rotation)
next_new_id = len(DISTRIBUTION_IDENTIFIERS)
next_new_id = max(list(DISTRIBUTION_IDENTIFIERS.keys()) + [-1]) + 1
new_identifiers = numpy.arange(next_new_id, next_new_id + length, dtype=int)
for idx in new_identifiers:
DISTRIBUTION_IDENTIFIERS[idx] = distribution
Expand Down Expand Up @@ -270,8 +267,6 @@ def format_repr_kwargs(**parameters):
[]
"""
out = []

defaults_only = True
for name, (param, default) in list(parameters.items()):
defaults_only &= (isinstance(param, (int, float)) and param == default) or (
Expand Down

0 comments on commit 926b1ff

Please sign in to comment.