def run(iter): (Simulation().set().rand_seed(1928).pragma_autocompact( True).pragma_live_info(True).pragma_live_info_ts(False).fn_group_setup( grp_setup).done().add().rule( FluProgressRule()).rule(FluLocationRule()).probe( probe_flu_at(school_l, 'low-income') ). # the simulation output we care about and want monitored probe(probe_flu_at(school_m, 'med-income')). # ^ done().db(fpath_db).gen_groups( tbl='students', attr_db=[], rel_db=[GroupDBRelSpec(name='school', col='school_id')], attr_fix={}, rel_fix={ 'home': site_home }, rel_at='school').done().run(iter))
def pop_db_gen(): # Add foreign keys to the 'people' table: # BEGIN TRANSACTION; # CREATE TABLE people_tmp (sp_id INTEGER, sp_hh_id INTEGER, age INTEGER, sex TEXT, race INTEGER, relate INTEGER, income INTEGER, school_id INTEGER, work_id INTEGER); # INSERT INTO people_tmp SELECT sp_id, sp_hh_id, age, sex, race, relate, income, school_id, work_id FROM people; # DROP TABLE people; # CREATE TABLE people (sp_id INTEGER, sp_hh_id INTEGER, age INTEGER, sex TEXT, race INTEGER, relate INTEGER, income INTEGER, school_id INTEGER, work_id INTEGER, CONSTRAINT fk__people__households FOREIGN KEY (sp_hh_id) REFERENCES households (sp_id), CONSTRAINT fk__people__schools FOREIGN KEY (school_id) REFERENCES schools (sp_id), CONSTRAINT fk__people__workplaces FOREIGN KEY (work_id) REFERENCES workplaces (sp_id)); # INSERT INTO people SELECT sp_id, sp_hh_id, age, sex, race, relate, income, school_id, work_id FROM people_tmp; # DROP TABLE people_tmp; # COMMIT; if not 'sim-flu-ac' in session: return jsonify({ 'res': False, 'err': 'The simulation has not been initialized' }) json = request.get_json() fpath = db_get_fpath(json['db']) if not fpath: return jsonify({ 'res': False, 'err': 'Incorrect database specified' }) attr_db = json['attr_db'] rel_db = json['rel_db'] site_home = Site('home') stdout = io.StringIO() with redirect_stdout(stdout): sim = session['sim-flu-ac'] sim.gen_groups_from_db( fpath_db = fpath, tbl = json['tbl'], attr_db = attr_db, rel_db = [GroupDBRelSpec(name=rel['name'], col=rel['col']) for rel in rel_db], attr_fix = {}, rel_fix = { 'home': site_home }, rel_at = 'school', is_verbose = False ) session['stdout'].append(stdout.getvalue()) return jsonify({ 'res': True, 'pop': sim.get_state()['pop'], 'stdout': session['stdout'] })
]) if te.is_db_empty and do_sim and (do_school or do_school_work): for t in te.traj.values(): t.get_sim().gen_groups_from_db(locale_db, schema=None, tbl='people', rel_at='home', limit=0, attr_fix={}, rel_fix={'home': site_home}, attr_db=['age_group'], rel_db=[ GroupDBRelSpec(name='school', col='school_id', fk_schema=None, fk_tbl='schools', fk_col='sp_id', sites=None) ]) if te.is_db_empty and do_sim and (do_work or do_school_work): for t in te.traj.values(): t.get_sim().gen_groups_from_db( locale_db, schema=None, tbl='people', rel_at='home', limit=0, attr_fix={}, rel_fix={'home': site_home}, attr_db=['age_group'],
probe_grp_size_flu_school_m02 = probe_grp_size_flu_school( 'school-m02', school_m02, pp) probe_grp_size_flu_school_m03 = probe_grp_size_flu_school( 'school-m03', school_m03, pp) # ---------------------------------------------------------------------------------------------------------------------- # (3) Simulation: (Simulation().set().rand_seed(rand_seed).pragma_autocompact(True). pragma_live_info(pragma_live_info).pragma_live_info_ts(pragma_live_info_ts). pragma_rule_analysis_for_db_gen(True).done().add().rule( SimpleFluProgressRule()). # rule(ProgressFluSimpleAlleghenyRule()). # rule(FluLocationAlleghenyRule()). # probe(probe_grp_size_few_schools). probe(probe_grp_size_flu_school_l01).probe(probe_grp_size_flu_school_l02). probe(probe_grp_size_flu_school_l03).probe( probe_grp_size_flu_school_m01).probe(probe_grp_size_flu_school_m02).probe( probe_grp_size_flu_school_m03).done().gen_groups_from_db( fpath_db_in, tbl='students', attr_fix={ 'flu': 's' }, rel_fix={ 'home': site_home }, attr_db=[], rel_db=[GroupDBRelSpec('school', 'school_id', sites['school'])], rel_at='school').run(3).summary(True, 0, 0, 0, 0, (1, 0)))
GroupSplitSpec(p=0.9, attr_set={'flu': 's'}), GroupSplitSpec(p=0.1, attr_set={'flu': 'i'}) ] (Simulation().set().rand_seed(1928).pragma_autocompact(True).pragma_live_info( True).pragma_live_info_ts(False).fn_group_setup( grp_setup).done().add().rule(FluProgressRule()). rule(FluLocationRule()).probe(probe_flu_at( school_l, 'low-income')). # the simulation output we care about and want monitored probe(probe_flu_at(school_m, 'med-income')). # ^ done().db(fpath_db).gen_groups( tbl='students', attr_db=[], rel_db=[GroupDBRelSpec(name='school', col='school_id')], attr_fix={}, rel_fix={ 'home': site_home }, rel_at='school').done().run(2)) # ---------------------------------------------------------------------------------------------------------------------- # Key points # Static rule analysis - Automatically form groups based on rules # Dynamic rule analysis - Alert the modeler they might have missed something # # After 100 iterations # Low income school - 24% of infected kids # Medium income school - 14% of infected kids
# probe(probe_flu_at(school_m, 'm.p')). commit(). # gen_sites_from_db( # fpath_db = fpath_db, # name = 'school', # tbl = 'schools', # name_col = 'sp_id' # ). gen_groups_from_db( fpath_db = fpath_db, tbl = 'students', attr_fix = {}, rel_fix = { 'home': site_home }, attr_db = [], # rel_db = [GroupDBRelSpec(name='school', col='school_id', entities=sites['school'])], rel_db = [GroupDBRelSpec(tbl='schools', col_from='school_id', col_to='sp_id', name='school')], rel_at = 'school' # TODO: move to group_setup() ) ) # print(s.pop.sites[450149323]) # print(s.pop.sites[450149323]) # sys.exit(77) (s. add(). probe(probe_flu_at(s.pop.sites[450149323], 'l.p')). probe(probe_flu_at(s.pop.sites[450149323], 'm.p')). commit(). setup_groups(setup_group).
(Simulation(). set(). rand_seed(rand_seed). pragma_autocompact(True). pragma_live_info(pragma_live_info). pragma_live_info_ts(pragma_live_info_ts). pragma_rule_analysis_for_db_gen(True). done(). add(). rule(SEIRModel()). rule(ResetSchoolDayRule(TimePoint(7))). rule(GoToAndBackTimeAtRule(t_at_attr='t@school')). # rule(AttrRule()). probe(probe_grp_size_schools). done(). gen_groups_from_db( fpath_db_in, tbl = 'people', attr_fix = {}, rel_fix = { 'home': site_home }, attr_db = [], rel_db = [ GroupDBRelSpec('school', 'school_id', sites['school']) ], rel_at = 'home' ). run(1). summary() )
os.remove(fpath_groups) if os.path.isfile(fpath_groups): print('Loading groups... ', end='') with gzip.GzipFile(fpath_groups, 'rb') as f: gc.disable() groups = pickle.load(f) gc.enable() print('done.') else: print('Generating groups... ', end='') groups = Group.gen_from_db(fpath_db, tbl='people', attr=[], rel=[ GroupDBRelSpec('home', 'sp_hh_id', sites['home']), GroupDBRelSpec('school', 'school_id', sites['school']) ], rel_at='home') print('done.') print('Saving groups... ', end='') with gzip.GzipFile(fpath_groups, 'wb') as f: pickle.dump(groups, f) print('done.') inf('groups', groups, do_calc_size_groups) # groups: 164,459 (175.2M) # file 5.1M