# cache¶

Memoization and caching utilities.

pyphi.cache.memory_full()

Check if the memory is too full for further caching.

pyphi.cache.cache(cache={}, maxmem=50, typed=False)

Memory-limited cache decorator.

maxmem is a float between 0 and 100, inclusive, specifying the maximum percentage of physical memory that the cache can use.

If typed is True, arguments of different types will be cached separately. For example, f(3.0) and f(3) will be treated as distinct calls with distinct results.

Arguments to the cached function must be hashable.

View the cache statistics named tuple (hits, misses, currsize) with f.cache_info(). Clear the cache and statistics with f.cache_clear(). Access the underlying function with f.__wrapped__.

class pyphi.cache.DictCache

A generic dictionary-based cache.

Intended to be used as an object-level cache of method results.

clear()
size()

Number of items in cache

info()

Return info about cache hits, misses, and size

get(key)

Get a value out of the cache.

Returns None if the key is not in the cache. Updates cache statistics.

set(key, value)

Set a value in the cache

key(*args, _prefix=None, **kwargs)

Get the cache key for the given function args.

Kwargs:

prefix: A constant to prefix to the key.

pyphi.cache.redis_init(db)
pyphi.cache.redis_available()

Check if the Redis server is connected.

class pyphi.cache.RedisCache
clear()

Flush the cache.

static size()

Size of the Redis cache.

Note

This is the size of the entire Redis database.

info()

Return cache information.

Note

This is not the cache info for the entire Redis key space.

get(key)

Get a value from the cache.

Returns None if the key is not in the cache.

set(key, value)

Set a value in the cache.

key()

Delegate to subclasses.

pyphi.cache.validate_parent_cache(parent_cache)
class pyphi.cache.RedisMICECache(subsystem, parent_cache=None)

A Redis-backed cache for find_mice().

See MICECache for more info.

get(key)

Get a value from the cache.

If the MaximallyIrreducibleCauseOrEffect cannot be found in this cache, try and find it in the parent cache.

set(key, value)

Only need to set if the subsystem is uncut.

Caches are only inherited from uncut subsystems.

key(direction, mechanism, purviews=False, _prefix=None)

Cache key. This is the call signature of find_mice().

class pyphi.cache.DictMICECache(subsystem, parent_cache=None)

A subsystem-local cache for MaximallyIrreducibleCauseOrEffect objects.

See MICECache for more info.

set(key, mice)

Set a value in the cache.

Only cache if:
• The subsystem is uncut (caches are only inherited from uncut subsystems so there is no reason to cache on cut subsystems.)

• $$\varphi$$ > 0. Ideally we would cache all mice, but the size of the cache grows way too large, making parallel computations incredibly inefficient because the caches have to be passed between process. This will be changed once global caches are implemented.

• Memory is not too full.

key(direction, mechanism, purviews=False, _prefix=None)

Cache key. This is the call signature of find_mice().

pyphi.cache.MICECache(subsystem, parent_cache=None)

Construct a MaximallyIrreducibleCauseOrEffect cache.

Uses either a Redis-backed cache or a local dict cache on the object.

Parameters

subsystem (Subsystem) – The subsystem that this is a cache for.

Kwargs:
parent_cache (MICECache): The cache generated by the uncut

version of subsystem. Any cached MaximallyIrreducibleCauseOrEffect which are unaffected by the cut are reused in this cache. If None, the cache is initialized empty.

class pyphi.cache.PurviewCache

A network-level cache for possible purviews.

set(key, value)

Only set if purview caching is enabled

pyphi.cache.method(cache_name, key_prefix=None)

Caching decorator for object-level method caches.

Cache key generation is delegated to the cache.

Parameters
• cache_name (str) – The name of the (already-instantiated) cache on the decorated object which should be used to store results of this method.

• *key_prefix – A constant to use as part of the cache key in addition to the method arguments.