Different storage backends for the data of a simulation.
Storing and retrieving calculated vectors in memory.
is_eternal (bool) – Whether the storage is eternal.
Create a copy of the storage whose future mutations are independent.
The underlying arrays are shared, as they are treated as immutable by the engine; only the period-to-array mapping is copied.
InMemoryStorage – A new storage with the same data.
Examples
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> period = periods.period("2017")
>>> storage.put(numpy.array([1, 2, 3]), period)
>>> clone = storage.clone()
>>> clone.get(period)
array([1, 2, 3])
>>> clone.delete(period)
>>> storage.get(period)
array([1, 2, 3])
Delete the data for the specified Period from memory.
Note
If period is specified, all data will be deleted.
Examples
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> storage.put(value, period)
>>> storage.get(period)
array([1, 2, 3])
>>> storage.delete(period)
>>> storage.get(period)
>>> storage.put(value, period)
>>> storage.delete()
>>> storage.get(period)
Retrieve the data for the specified Period from memory.
period (None | Period) – The Period for which data should be retrieved.
Examples
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> storage.put(value, period)
>>> storage.get(period)
array([1, 2, 3])
List of storage’s known periods.
KeysView[Period] – A sequence containing the storage’s known periods.
Examples
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> storage.get_known_periods()
dict_keys([])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> storage.put([], period)
>>> storage.get_known_periods()
dict_keys([Period(('year', Instant((2017, 1, 1)), 1))])
Memory usage of the storage.
MemoryUsage – A dictionary representing the storage’s memory usage.
MemoryUsage
Examples
>>> from openfisca_core import data_storage
>>> storage = data_storage.InMemoryStorage()
>>> storage.get_memory_usage()
{'nb_arrays': 0, 'total_nb_bytes': 0, 'cell_size': nan}
Store the specified data in memory for the specified Period.
Examples
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> value = numpy.array([1, "2", "salary"])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> storage.put(value, period)
>>> storage.get(period)
array(['1', '2', 'salary'], ...)
Storing and retrieving calculated vectors on disk.
Decode a file by loading its contents as a numpy array.
file (str) – Path to the file to be decoded.
EnumArray – Representing the data in the file.
ndarray[generic] – Representing the data in the file.
Note
If the file is associated with Enum values, the
array is converted back to an EnumArray object.
Examples
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, indexed_enums, periods
>>> class Housing(indexed_enums.Enum):
... OWNER = "Owner"
... TENANT = "Tenant"
... FREE_LODGER = "Free lodger"
... HOMELESS = "Homeless"
>>> array = numpy.array([1])
>>> value = indexed_enums.EnumArray(array, Housing)
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage._decode_file(storage._files[period])
EnumArray([Housing.TENANT])
Copy the storage into storage_dir, making future mutations independent.
The stored files are copied into storage_dir, so that neither
simulation writes into — or deletes — the files of the other.
storage_dir (str) – Path to store the copied vectors. Must be different
from the current storage directory; created if it does not
exist yet.
OnDiskStorage – A new storage with the same data.
ValueError – If storage_dir is the current storage directory.
Examples
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, 2, 3])
>>> period = periods.period("2017")
>>> with tempfile.TemporaryDirectory() as directory:
... with tempfile.TemporaryDirectory() as clone_directory:
... storage = data_storage.OnDiskStorage(
... directory, preserve_storage_dir=True
... )
... storage.put(value, period)
... clone = storage.clone(clone_directory)
... clone.delete(period)
... storage.get(period)
array([1, 2, 3])
Delete the data for the specified period from disk.
period (None | Period) – The period for which data should be deleted. If not
specified, all data will be deleted.
Examples
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.get(period)
array([1, 2, 3])
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.delete(period)
... storage.get(period)
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.delete()
... storage.get(period)
Retrieve the data for the specified period from disk.
period (None | Period) – The period for which data should be retrieved.
None – If no data is available.
EnumArray – Representing the data for the specified period.
ndarray[generic] – Representing the data for the specified period.
Examples
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.get(period)
array([1, 2, 3])
List of storage’s known periods.
KeysView[Period] – A sequence containing the storage’s known periods.
Examples
>>> import tempfile
>>> from openfisca_core import data_storage, periods
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.get_known_periods()
dict_keys([])
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put([], period)
... storage.get_known_periods()
dict_keys([Period(('year', Instant((2017, 1, 1)), 1))])
Store the specified data on disk for the specified period.
Examples
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, "2", "salary"])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.get(period)
array(['1', '2', 'salary'], ...)
Restore the storage from disk.
Examples
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> directory = tempfile.TemporaryDirectory()
>>> storage1 = data_storage.OnDiskStorage(directory.name)
>>> storage1.put(value, period)
>>> storage1._files
{Period(('year', Instant((2017, 1, 1)), 1)): '...2017.npy'}
>>> storage2 = data_storage.OnDiskStorage(directory.name)
>>> storage2._files
{}
>>> storage2.restore()
>>> storage2._files
{Period((<DateUnit.YEAR: 'year'>, Instant((2017, 1, 1...2017.npy'}
>>> directory.cleanup()