Commit 97627513 authored by Leo Pound Singer's avatar Leo Pound Singer

Revert "Drop memoized() decorator in favor of standard library functools.lru_cache"

This reverts commit 8e12c6d8651deec03bab2eb21ab6377bf7b51588.
The package is not available in Debian jessie.
Original: e52a3c326e62d9b17fe6a9e8aef3c7369998b197
parent 35c47f4e
......@@ -12,7 +12,6 @@ Build-Depends: debhelper (>= 9),
liboctave-dev,
pkg-config,
python-all-dev,
python-backports.functools-lru-cache,
python-h5py,
python-healpy (>= 1.9.1),
python-matplotlib (>= 1.2.0),
......@@ -101,7 +100,6 @@ Depends:
${misc:Depends},
${python:Depends},
${shlibs:Depends},
python-backports.functools-lru-cache,
python-h5py,
python-matplotlib (>= 1.2.0),
python-pkg-resources,
......
......@@ -15,7 +15,6 @@ BuildRoot: %{_tmppath}/%{name}-%{version}-root
BuildRequires: chealpix-devel >= 3.30.0
BuildRequires: gsl-devel
BuildRequires: hdf5-devel
BuildRequires: python-backports-functools_lru_cache
BuildRequires: healpy >= 1.9.1
BuildRequires: python-matplotlib >= 1.2.0
BuildRequires: python-shapely
......@@ -91,7 +90,6 @@ library.
Summary: Python Bindings for LALInference
Group: LAL
Requires: %{name} = %{version}
Requires: python-backports-functools_lru_cache
Requires: healpy >= 1.9.1
Requires: python-shapely
Requires: numpy >= 1.7
......
......@@ -86,6 +86,7 @@ import lal.series
from glue.ligolw import utils as ligolw_utils
# BAYESTAR imports.
from lalinference.bayestar.decorator import memoized
from lalinference.io import fits
from lalinference.bayestar import ligolw as ligolw_bayestar
from lalinference.bayestar import filter
......@@ -93,11 +94,6 @@ from lalinference.bayestar import timing
from lalinference.bayestar.sky_map import ligolw_sky_map, rasterize
# Other imports.
try:
from functools import lru_cache
except ImportError:
# FIXME: remove when we drop support for Python < 3.2
from backports.functools_lru_cache import lru_cache
import os
import sys
import numpy as np
......@@ -188,7 +184,7 @@ if opts.psd_files: # read pycbc psds here
else:
reference_psd_filenames_by_process_id = ligolw_bayestar.psd_filenames_by_process_id_for_xmldoc(xmldoc)
@lru_cache(maxsize=None)
@memoized
def reference_psds_for_filename(filename):
xmldoc = ligolw_utils.load_filename(
filename, contenthandler=lal.series.PSDContentHandler)
......
......@@ -19,7 +19,7 @@
Collection of Python decorators.
"""
__author__ = "Leo Singer <leo.singer@ligo.org>"
__all__ = ('with_numpy_random_seed', 'as_dict')
__all__ = ('memoized', 'with_numpy_random_seed')
from functools import wraps
......@@ -27,6 +27,39 @@ from collections import Hashable
from astropy.utils.misc import NumpyRNGContext
def memoized(func):
"""Memoize a function or class by caching its return values for any given
arguments."""
cache = {}
# FIXME: In Python 3.4, use inspect.getcallargs to bind function arguments.
# This will allow us to handle default arguments better.
# (Though inspect.getcallargs was added in Python 2.7, it won't work with
# built-in functions or functions from C extensions until Python 3.4.
# See https://bugs.python.org/issue17481.)
@wraps(func)
def memo(*args, **kwargs):
# Create a key out of the arguments.
key = (args, frozenset(kwargs.items()))
if isinstance(args, Hashable): # The key is immutable.
try:
# Look up the return value for these arguments.
ret = cache[key]
except KeyError:
# Not found; invoke function and store return value.
ret = cache[key] = func(*args, **kwargs)
else: # The key is mutable. We can't cache it.
ret = func(*args, **kwargs)
# Done!
return ret
# Return wrapped function.
return memo
def with_numpy_random_seed(func, seed=0):
"""Decorate a function so that it is called with a pre-defined random seed.
The random seed is restored when the function returns."""
......
......@@ -28,16 +28,14 @@ import logging
import numpy as np
import math
from scipy import optimize
try:
from functools import lru_cache
except ImportError:
# FIXME: remove when we drop support for Python < 3.2
from backports.functools_lru_cache import lru_cache
# LAL imports
import lal
import lalsimulation
# My own imports
from .decorator import memoized
log = logging.getLogger('BAYESTAR')
......@@ -48,10 +46,10 @@ unitInverseSqrtHertz = lal.Unit('s^1/2')
# Memoize FFT plans
CreateForwardCOMPLEX16FFTPlan = lru_cache(maxsize=None)(lal.CreateForwardCOMPLEX16FFTPlan)
CreateForwardREAL8FFTPlan = lru_cache(maxsize=None)(lal.CreateForwardREAL8FFTPlan)
CreateReverseCOMPLEX16FFTPlan = lru_cache(maxsize=None)(lal.CreateReverseCOMPLEX16FFTPlan)
CreateReverseREAL8FFTPlan = lru_cache(maxsize=None)(lal.CreateReverseREAL8FFTPlan)
CreateForwardCOMPLEX16FFTPlan = memoized(lal.CreateForwardCOMPLEX16FFTPlan)
CreateForwardREAL8FFTPlan = memoized(lal.CreateForwardREAL8FFTPlan)
CreateReverseCOMPLEX16FFTPlan = memoized(lal.CreateReverseCOMPLEX16FFTPlan)
CreateReverseREAL8FFTPlan = memoized(lal.CreateReverseREAL8FFTPlan)
def ceil_pow_2(n):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment