Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reference cycles between MockFactory and ModelFactory. #918

Merged
merged 7 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
"""
import sys
import pytest
import numpy as np

Expand All @@ -13,6 +14,25 @@
__all__ = ('test_hearin15', )



@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows")
def test_memory_leak():
model = PrebuiltHodModelFactory('hearin15')
halocat = FakeSim()
import resource

model.populate_mock(halocat)
maxrss = []
for i in range(9):
model.mock.populate()
maxrss.append(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)

import numpy
# memory usage shall not increase significantly per run.
assert (numpy.diff(maxrss) < 1024).all()


def test_hearin15():
"""
"""
Expand Down
11 changes: 11 additions & 0 deletions halotools/empirical_models/factories/mock_factory_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .mock_helpers import three_dim_pos_bundle, infer_mask_from_kwargs

from .. import model_helpers, model_defaults
import weakref

try:
from ... import mock_observables
Expand Down Expand Up @@ -76,6 +77,7 @@ def __init__(self, **kwargs):
except:
pass


# Create a list of halo properties that will be inherited by the mock galaxies
self.additional_haloprops = copy(model_defaults.default_haloprop_list_inherited_by_mock)

Expand All @@ -86,6 +88,15 @@ def __init__(self, **kwargs):

self.galaxy_table = Table()

@property
def model(self):
""" model, a weak reference to the model because mock is always a member of model """
return self._model()

@model.setter
def model(self, value):
self._model = weakref.ref(value)

@abstractmethod
def populate(self, **kwargs):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,17 @@ def update_param_dict_decorator(self, component_model, func_name):
:ref:`param_dict_mechanism`
"""

# do not pass self into the scope;
# assuming param_dict is not replaced during life cycle of self.
# passing `self` causes a cycle reference when
# the function is assigned as attributes of self.
__param_dict__ = self.param_dict
def decorated_func(*args, **kwargs):

# Update the param_dict as necessary
for key in list(self.param_dict.keys()):
for key in list(__param_dict__.keys()):
if key in component_model.param_dict:
component_model.param_dict[key] = self.param_dict[key]
component_model.param_dict[key] = __param_dict__[key]

func = getattr(component_model, func_name)
return func(*args, **kwargs)
Expand Down