Beispiel #1
0
 def __init__(self, components):
     self.components = components
     self.values = ValuesManager()
     self.events = EventManager()
     self.population = PopulationManager()
     self.tables = InterpolatedDataManager()
     self.components.extend([self.tables, self.values, self.events, self.population])
     self.current_time = None
Beispiel #2
0
class SimulationContext:
    """context"""
    def __init__(self, components):
        self.components = components
        self.values = ValuesManager()
        self.events = EventManager()
        self.population = PopulationManager()
        self.tables = InterpolatedDataManager()
        self.components.extend(
            [self.tables, self.values, self.events, self.population])
        self.current_time = None

    def setup(self):
        builder = Builder(self)
        components = [self.values, self.events, self.population, self.tables
                      ] + list(self.components)
        done = set()

        i = 0
        while i < len(components):
            component = components[i]
            if component is None:
                raise ValueError(
                    'None in component list. This likely indicates a bug in a factory function'
                )

            if isinstance(component, Iterable):
                # Unpack lists of components so their constituent components get initialized
                components.extend(component)
            if component not in done:
                if hasattr(component, 'configuration_defaults'):
                    # This reapplies configuration from some components but
                    # that shouldn't be a problem.
                    config.read_dict(component.configuration_defaults,
                                     layer='component_configs',
                                     source=component)
                if hasattr(component, 'setup'):
                    sub_components = component.setup(builder)
                    done.add(component)
                    if sub_components:
                        components.extend(sub_components)
            i += 1
        self.values.setup_components(components)
        self.events.setup_components(components)
        self.population.setup_components(components)

        self.events.get_emitter('post_setup')(None)

    def __repr__(self):
        return ("SimulationContext(\ncomponents: {},\nvalues: {},\n".format(
            self.components, self.values) +
                "events: {},\npopulation: {},\ntables: {},\n".format(
                    self.events, self.population, self.tables) +
                "current_time: {})".format(self.current_time))
Beispiel #3
0
def test_set_combiner():
    # This is the normal configuration for collecting lists of meids for calculating cause deleted tables
    manager = ValuesManager()

    value = manager.get_value('test', set_combiner)
    value.source = lambda: set()

    assert value() == set()

    manager.mutator(lambda: 'thing one', 'test')
    assert value() == {'thing one'}

    manager.mutator(lambda: 'thing one', 'test')
    assert value() == {'thing one'}  # duplicates are truly removed

    manager.mutator(lambda: 'thing two', 'test')
    assert value() == {'thing one',
                       'thing two'}  # but unique values are collected
Beispiel #4
0
def test_replace_combiner():
    manager = ValuesManager()

    value = manager.get_value('test', replace_combiner)
    value.source = lambda: 1

    assert value() == 1

    manager.mutator(lambda value: 42, 'test')
    assert value() == 42

    manager.mutator(lambda value: 84, 'test')
    assert value() == 84
Beispiel #5
0
def test_joint_value():
    # This is the normal configuration for PAF and disability weight type values
    manager = ValuesManager()

    index = pd.Index(range(10))

    value = manager.get_value('test', list_combiner,
                              joint_value_post_processor)
    value.source = lambda index: [pd.Series(0, index=index)]

    assert np.all(value(index) == 0)

    manager.mutator(lambda index: pd.Series(0.5, index=index), 'test')
    assert np.all(value(index) == 0.5)

    manager.mutator(lambda index: pd.Series(0.5, index=index), 'test')
    assert np.all(value(index) == 0.75)
Beispiel #6
0
def manager(mocker):
    manager = ValuesManager()
    builder = mocker.MagicMock()
    builder.step_size = lambda: lambda: pd.Timedelta(days=1)
    manager.setup(builder)
    return manager