def clone(self, debug = False, trace = False): """ Copy the simulation just enough to be able to run the copy without modifying the original simulation """ new = empty_clone(self) new_dict = new.__dict__ for key, value in self.__dict__.items(): if key not in ('debug', 'trace', 'tracer'): new_dict[key] = value new.persons = self.persons.clone(new) setattr(new, new.persons.key, new.persons) new.entities = {new.persons.key: new.persons} for entity_class in self.tax_benefit_system.group_entities: entity = self.entities[entity_class.key].clone(new) new.entities[entity.key] = entity setattr(new, entity_class.key, entity) # create shortcut simulation.household (for instance) if debug: new_dict['debug'] = True if trace: new_dict['trace'] = True if debug or trace: if self.debug or self.trace: new_dict['tracer'] = self.tracer.clone() else: new_dict['tracer'] = Tracer() return new
def check_tracing_params(accessor, param_key): tracer = Tracer() tracer.record_calculation_start('A', '2015-01') tracingParams = TracingParameterNodeAtInstant(parameters('2015-01-01'), tracer) param = accessor(tracingParams) assert_near(tracer.trace['A<2015-01>']['parameters'][param_key], param)
def test_variable_stats(): tracer = Tracer() tracer.record_calculation_start("A", 2017) tracer.record_calculation_start("B", 2017) tracer.record_calculation_start("B", 2017) tracer.record_calculation_start("B", 2016) assert_equals(tracer.usage_stats['B']['nb_requests'], 3) assert_equals(tracer.usage_stats['A']['nb_requests'], 1) assert_equals(tracer.usage_stats['C']['nb_requests'], 0)
def test_log_format(): tracer = Tracer() tracer.record_calculation_start("A", 2017) tracer.record_calculation_start("B", 2017) tracer.record_calculation_end("B", 2017, 1) tracer.record_calculation_end("A", 2017, 2) lines = tracer.computation_log() assert_equals(lines[0], ' A<2017> >> 2') assert_equals(lines[1], ' B<2017> >> 1')
def __init__( self, tax_benefit_system, simulation_json=None, debug=False, period=None, trace=False, opt_out_cache=False, memory_config=None, ): """ Create an empty simulation To fill the simulation with input data, you can use the :any:`SimulationBuilder` or proceed manually. """ self.tax_benefit_system = tax_benefit_system assert tax_benefit_system is not None if period: assert isinstance(period, periods.Period) self.period = period # To keep track of the values (formulas and periods) being calculated to detect circular definitions. # See use in formulas.py. # The data structure of requested_periods_by_variable_name is: {variable_name: [period1, period2]} self.requested_periods_by_variable_name = {} self.max_nb_cycles = None self.debug = debug self.trace = trace or self.debug if self.trace: self.tracer = Tracer() else: self.tracer = None self.opt_out_cache = opt_out_cache self.memory_config = memory_config self._data_storage_dir = None self.instantiate_entities() if simulation_json is not None: warnings.warn( ' '.join([ "The 'simulation_json' argument of the Simulation is deprecated since version 25.0, and will be removed in the future.", "The proper way to init a simulation from a JSON-like dict is to use SimulationBuilder.build_from_entities. See <https://openfisca.org/doc/openfisca-python-api/simulation_builder.html#openfisca_core.simulation_builder.SimulationBuilder.build_from_dict>" ]), Warning) from openfisca_core.simulation_builder import SimulationBuilder SimulationBuilder().build_from_entities(tax_benefit_system, simulation_json, simulation=self)
def __init__( self, tax_benefit_system, simulation_json = None, debug = False, period = None, trace = False, opt_out_cache = False, memory_config = None, ): """ If a ``simulation_json`` is given, initialises a simulation from a JSON dictionary. Note: This way of initialising a simulation, still under experimentation, aims at replacing the initialisation from `scenario.make_json_or_python_to_attributes`. If no ``simulation_json`` is given, initialises an empty simulation. """ self.tax_benefit_system = tax_benefit_system assert tax_benefit_system is not None if period: assert isinstance(period, periods.Period) self.period = period # To keep track of the values (formulas and periods) being calculated to detect circular definitions. # See use in formulas.py. # The data structure of requested_periods_by_variable_name is: {variable_name: [period1, period2]} self.requested_periods_by_variable_name = {} self.max_nb_cycles = None self.debug = debug self.trace = trace or self.debug if self.trace: self.tracer = Tracer() else: self.tracer = None self.opt_out_cache = opt_out_cache self.memory_config = memory_config self._data_storage_dir = None self.instantiate_entities(simulation_json)
def test_consistency(): tracer = Tracer() tracer.record_calculation_start("rsa", 2017) tracer.record_calculation_end("unkwonn", 2017, 100)
# -*- coding: utf-8 -*- from nose.tools import raises from openfisca_core.tracers import Tracer tracer = Tracer() @raises(ValueError) def test_consistency(): tracer.record_calculation_start("rsa", "2016-01") tracer.record_calculation_end("unkwonn", "2016-01", 100)