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 load(component_list): components = [] for component in component_list: if isinstance(component, str): if '(' in component: i = component.index('(') args = literal_eval(component[i:]) if not isinstance(args, tuple): args = (args,) component = component[:i] call = True else: call = False module_path, _, component_name = component.rpartition('.') component = getattr(import_module(module_path), component_name) # Establish the initial configuration if hasattr(component, 'configuration_defaults'): config.read_dict(component.configuration_defaults, layer='component_configs', source=module_path) if call: component = component(*args) elif isinstance(component, type): component = component() if isinstance(component, Iterable): components.extend(component) else: components.append(component) return components
def configure(input_draw_number=None, model_draw_number=None, verbose=False, simulation_config=None): if simulation_config: if isinstance(simulation_config, dict): config.read_dict(simulation_config) else: config.read(simulation_config) if input_draw_number is not None: config.run_configuration.set_with_metadata('draw_number', input_draw_number, layer='override', source='command_line_argument') if model_draw_number is not None: config.run_configuration.set_with_metadata('model_draw_number', model_draw_number, layer='override', source='command_line_argument')
def prepare_component_configuration(component_config, path=None): if 'configuration' in component_config: config.read_dict(component_config['configuration'], layer='model_override', source=path) def process_level(level, prefix): component_list = [] for c in level: if isinstance(c, dict): for k, v in c.items(): component_list.extend(process_level(v, prefix + [k])) else: component_list.append('.'.join(prefix + [c])) return component_list return process_level(component_config['components'], [])