VarCacheSingles
- class PlasmaCalcs.quantities.caching.VarCacheSingles(*, MBmax=None, nmax=None, remove_strategy='used')
Bases:
VarCachecache of vars, where each cached quantity represents a single point (in dimension space).
“single point” refers to having at most one value for each of the dimensions,but not maindims. E.g., one snap, fluid, component. Any number of x,y,z.When appending a CachedQuantity which is not a single point (in dimension space),separate it into multiple quantities, and append each of those instead.VarCache documentation:———————–cache of vars. {var: {id: CachedQuantity}.id is quant.id for CachedQuantity quant;it helps when determining which cached quantity to pop, if cache size is limited.Use self.append_cq(var, CachedQuantity) to add a CachedQuantity to this cache.Use self.append(var, value, behavior) to create a CachedQuantity and add it to this cache.Tracks self.order_entered (updated only when entering a new var),and self.order_used (updated when entering OR loading a var).Each of these is an OrderedDict with keys (var, qid) tuples; values None.The ‘latest’ entry is at the end.- MBmax: None or number
- maximum size in MB of this cache. None –> no maximum.if provided along with nmax, the more restrictive one will be used.
- nmax: None or number
- maximum number of entries in this cache. None –> no maximum.if provided along with MBmax, the more restrictive one will be used.
- remove_strategy: ‘used’ or ‘entered’
- whether to remove the oldest entries based on order_used or order_entered.Only applies when cache size exceeds max (determined by MBmax and nmax).
Methods
__contains__(var)return whether at least one cached value for var exists in cache.
__getitem__(key)return self.cache[key].
__iter__()iterate over self.cache.
__len__()return len of cache.
append(var, value, behavior[, ukey])create CachedQuantity(value, behavior, ukey=ukey, **kw), and append it to this cache.
append_cq(var, cached_quantity)add cached_quantity to this cache.
clear()empties the cache of all cached values.
get(var, behavior)return cached value from CachedQuantity with matching behavior, if possible,
inverse_lookup(var, behavior, *[, lenient, ...])returns list of quantities from self.cache[var] for which behavior.compatible(quantity.behavior)
items()return items of self.cache.
iterate over all quantities in this cache.
keys()return keys of self.cache.
lookup(var, behavior)returns (relevance, quant) for cached quantity for var, with quantity.relevant(behavior).
mark_used(var, quant)mark (var, quant) as having just been used.
pop([itemkey, strategy, _clear_empty_var])remove and return an entry from this cache.
remove and return the oldest entry from this cache, based on self.order_entered.
pop_used()remove and return the oldest entry from this cache, based on self.order_used.
remove(var, behavior, *[, missing_ok])remove all quantities for which behavior.compatible(quantity.behavior, lenient=True), for var.
timing()return dict of timing stats.
values()return values of self.cache.
_get_oldest_itemkey([strategy])return (var, cached_quantity.id) for the oldest item in this cache.
_remove_entries_if_too_large([...])remove entries from this cache until size WILL be below max AFTER adding new_cached_quantity.
remove entries from this cache until size is below MBmax.
remove entries from this cache until size is below nmax.
Attributes
alias to size_MB
total number of times a value was loaded from this cache.
total number of times loaded quantities currently in the cache.
number of entries in this cache.
total size of this cache, in MB.
total time saved by using this cache, in seconds.
total time saved by quantities which are currently in the cache.
- property MB
alias to size_MB
- __contains__(var)
return whether at least one cached value for var exists in cache.
- __getitem__(key)
return self.cache[key].
- __iter__()
iterate over self.cache.
- __len__()
return len of cache. This is the number of vars each with at least 1 cached value.
- _get_oldest_itemkey(strategy=None)
return (var, cached_quantity.id) for the oldest item in this cache.
- strategy: None, ‘used’, or ‘entered’
- determines meaning of ‘oldest’. If None, use self.remove_strategy.
- _remove_entries_if_too_large(new_cached_quantity=None)
remove entries from this cache until size WILL be below max AFTER adding new_cached_quantity.
This method doesn’t add new_cached_quantity, but pretends it will be added.if new_cached_quantity is None, clean up this cache without adding anything.returns (total number of entries removed due to nmax, number removed due to MBmax).Note - always attempts removals due to nmax first.
- _remove_entries_if_too_large_MBmax(new_cached_quantity=None)
remove entries from this cache until size is below MBmax.
if self.MBmax is not provided, do nothing.if new_cached_quantity is provided, treat self.size_MB as if it is new_cached_quantity.MB bigger.returns total number of entries removed.
- _remove_entries_if_too_large_nmax(new_cached_quantity=None)
remove entries from this cache until size is below nmax.
if self.nmax is not provided, do nothing.if new_cached_quantity is provided, treat self.size as if it is 1 bigger.returns total number of entries removed.
- append(var, value, behavior, ukey=None, **kw)
create CachedQuantity(value, behavior, ukey=ukey, **kw), and append it to this cache.
var: str value: any behavior: dict or Behavior
(if dict, internally convert to Behavior.)- ukey: None or str
- if provided, pop ‘units’ from behavior, and ukey is the string to usefor getting the conversion factor for value to another units system.See help(self) for details.
- append_cq(var, cached_quantity)
add cached_quantity to this cache.
If cached_quantity is not a single point (in dimension space), first separate, then append each.if self.nmax or self.MBmax is provided, remove entries until size is below max.if cached_quantity is compatible with any existing cached quantity for var,delete the pre-existing one(s) before adding the new one.
- clear()
empties the cache of all cached values.
- get(var, behavior)
return cached value from CachedQuantity with matching behavior, if possible,
else raise CacheNotApplicableError.behavior is matching if CachedQuantity.matches_behavior(behavior),i.e. if CachedQuantity.behavior.compatible(behavior, lenient=True).
- inverse_lookup(var, behavior, *, lenient=True, subdims=True, **kw_compatible)
returns list of quantities from self.cache[var] for which behavior.compatible(quantity.behavior)
- items()
return items of self.cache.
- iter_quantities()
iterate over all quantities in this cache.
- keys()
return keys of self.cache.
- lookup(var, behavior)
returns (relevance, quant) for cached quantity for var, with quantity.relevant(behavior).
returns (False, None) if no quantity.relevant(behavior).else, returns (True, quant) if exact match, or (subdims, quant) if subdim match.subdim match occurs when dim from cached quantity is a value within dim from behaviore.g. fluid=0 in cached quantity, fluid=[0,1,2] in behavior.
- mark_used(var, quant)
mark (var, quant) as having just been used.
This affects bookkeeping, as well as pop order for strategy=’used’, if cache gets too large.
- property n_used
total number of times a value was loaded from this cache.
- property n_used_here
total number of times loaded quantities currently in the cache.
- pop(itemkey=None, *, strategy=None, _clear_empty_var=True)
remove and return an entry from this cache.
When called with no arguments, e.g. self.pop(), removes the oldest entry.- itemkey: None or (var, cached_quantity.id) tuple
- if provided, pop THIS entry from the cache. (self.cache[var][cached_quantity.id])else, pop oldest entry.
- strategy: None, ‘used’, or ‘entered’
- determines meaning of ‘oldest’. If None, use self.remove_strategy.
- _clear_empty_var: bool
- whether to delete self.cache[var] if it becomes empty due to popping this item.
- pop_entered()
remove and return the oldest entry from this cache, based on self.order_entered.
- pop_used()
remove and return the oldest entry from this cache, based on self.order_used.
- remove(var, behavior, *, missing_ok=False, **kw)
remove all quantities for which behavior.compatible(quantity.behavior, lenient=True), for var.
i.e., quantities where behavior[k]==quantity[k] for all k in [*behavior.keys(), *behavior.dims.keys()].- missing_ok: bool
- if not missing_ok, raise CacheNotApplicableError if there are no relevant quantities.
additional kwargs are passed to behavior.compatible(…, **kw). E.g. could use lenient=False instead.returns list of all removed quantities.
- property size
number of entries in this cache.
- property size_MB
total size of this cache, in MB.
- property time_saved
total time saved by using this cache, in seconds.
- property time_saved_here
total time saved by quantities which are currently in the cache.
- timing()
return dict of timing stats.
- values()
return values of self.cache.