def create_app(context: str = 'prod') -> DashAppInstance: """ create_app initializes the app instance Args: context (str, optional): One of either 'prod', 'dev', 'testing. Defaults to 'prod' where dash.Dash.run_server(debug=False). Change to 'dev' or 'test' to set debug to true. Returns: Env: Config variables based on context argument received DashAppInstance: Dash instance with appropriate configuration settings """ Env = from_object(context) LANGUAGE = Env.LANG body = Root( LANGUAGE, Parameters.create(os.environ, []), ) App = Dash( __name__, external_stylesheets=body.external_stylesheets, external_scripts=body.external_scripts, ) App.title = Env.CHIME_TITLE App.layout = body.html wrap_callbacks(App) return Env, App
def update_parameters(i, *input_values) -> List[dict]: """Reads html form outputs and converts them to a parameter instance Returns Parameters """ inputs_dict = SidebarCallbacks.get_formated_values(i, input_values) dt = inputs_dict["doubling_time"] if inputs_dict[ "doubling_time"] else None dfh = inputs_dict["date_first_hospitalized"] if not dt else None pars = Parameters( current_hospitalized=inputs_dict["current_hospitalized"], date_first_hospitalized=dfh, doubling_time=dt, hospitalized=Disposition.create( days=inputs_dict["hospitalized_los"], rate=inputs_dict["hospitalized_rate"] / 100, ), icu=Disposition( days=inputs_dict["icu_los"], rate=inputs_dict["icu_rate"] / 100, ), infectious_days=inputs_dict["infectious_days"], market_share=inputs_dict["market_share"] / 100, n_days=inputs_dict["n_days"], population=inputs_dict["population"], recovered=0, #FIXME input or pass through from defaults is required! relative_contact_rate=inputs_dict["relative_contact_rate"] / 100, ventilated=Disposition.create( days=inputs_dict["ventilated_los"], rate=inputs_dict["ventilated_rate"] / 100, )) return [{ "inputs_dict": inputs_dict, "parameters": parameters_serializer(pars) }]
def create_params_from_file(file: str): """ Create CHIME Parameters object from input config file. :param file: :return: Parameters object """ args = ['--parameters', file] p = Parameters.create(os.environ, args) return p
def halving_param(): return Parameters( current_date=datetime(year=2020, month=3, day=28), current_hospitalized=100, doubling_time=6.0, hospitalized=Disposition.create(rate=0.05, days=7), icu=Disposition.create(rate=0.02, days=9), infectious_days=14, market_share=0.05, mitigation_date=datetime(year=2020, month=3, day=28), n_days=60, population=500000, recovered=0, relative_contact_rate=0.7, ventilated=Disposition.create(rate=0.01, days=10), )
def fixture_penn_chime_setup() -> Tuple[Parameters, Sir]: """Initializes penn_chime parameters and SIR model """ p = Parameters( current_hospitalized=69, date_first_hospitalized=date(2020, 3, 7), doubling_time=None, hospitalized=Disposition.create(days=7, rate=0.025), icu=Disposition.create(days=9, rate=0.0075), infectious_days=14, market_share=0.15, n_days=100, population=3600000, recovered=0, relative_contact_rate=0.3, ventilated=Disposition.create(days=10, rate=0.005), ) return p, Sir(p)
def parameters(self): ''' return penn_chime Parameters object populated with instance values ''' doubling_time = self.doubling_time if self.date_first_hospitalized: doubling_time = None mitigation_date = self.mitigation_date relative_contact_rate = max(self.relative_contact_rate, EPSILON) if relative_contact_rate == EPSILON: mitigation_date = None return Parameters( current_date=timezone.localdate(), population=self.population, current_hospitalized=self.current_hospitalized, date_first_hospitalized=self.date_first_hospitalized, doubling_time=doubling_time, hospitalized=Disposition( days=self.hospitalized_days, rate=self.hospitalized_rate, ), icu=Disposition( days=self.icu_days, rate=self.icu_rate, ), infectious_days=self.infectious_days, market_share=self.market_share, n_days=self.n_days, mitigation_date=mitigation_date, relative_contact_rate=relative_contact_rate, ventilated=Disposition( days=self.ventilated_days, rate=self.ventilated_rate, ), recovered=0, # not implemented )
def parameters_deserializer(p_json: str): values = loads(p_json) dates = { key: parse_date(values[key]).date() if values[key] else None for key in ( "current_date", "date_first_hospitalized", "mitigation_date", ) } return Parameters( current_date=dates["current_date"], current_hospitalized=values["current_hospitalized"], hospitalized=Disposition.create( days=values["hospitalized"][0], rate=values["hospitalized"][1], ), icu=Disposition.create( days=values["icu"][0], rate=values["icu"][1], ), infectious_days=values["infectious_days"], date_first_hospitalized=dates["date_first_hospitalized"], doubling_time=values["doubling_time"], market_share=values["market_share"], max_y_axis=values["max_y_axis"], mitigation_date=dates["mitigation_date"], n_days=values["n_days"], population=values["population"], recovered=values["recovered"], region=values["region"], relative_contact_rate=values["relative_contact_rate"], ventilated=Disposition.create( days=values["ventilated"][0], rate=values["ventilated"][1], ), )
def defaults(): return Parameters( region=Regions( delaware=564696, chester=519293, montgomery=826075, bucks=628341, philly=1581000, ), current_date=datetime(year=2020, month=3, day=28), current_hospitalized=14, date_first_hospitalized=datetime(year=2020, month=3, day=7), doubling_time=4.0, hospitalized=Disposition.create(rate=0.025, days=7), icu=Disposition.create(rate=0.0075, days=9), infectious_days=14, n_days=60, market_share=0.15, mitigation_date=datetime(year=2020, month=3, day=28), recovered=0, relative_contact_rate=0.3, ventilated=Disposition.create(rate=0.005, days=10), )
def sim_chimes(experiment: str, p: Parameters): """ Run many chime simulations - demo. Need to decide on argument passing :param experiment: :param params: :return: """ base_input_params_dict = vars(p) which_param_set = '' # Check which of date_first_hospitalized and doubling_time is set which_param_set = '' if p.date_first_hospitalized is not None and p.doubling_time is None: which_param_set = 'date_first_hospitalized' elif p.date_first_hospitalized is None and p.doubling_time is not None: which_param_set = 'doubling_time' else: print( "Gonna be trouble. Either date_first_hospitalized or doubling_time should be set." ) # Create a range of social distances relative_contact_rates = np.arange(0.05, 0.85, 0.05) # Create range of mitigation dates dates = pd.date_range('2020-03-21', '2020-03-27').to_pydatetime() mitigation_dates = [d.date() for d in dates] # Create a range of recovery times infectious_days = np.arange(8.00, 13.0, 1.00) num_scenarios = len(relative_contact_rates) # We can store outputs any way we want. For this demo, just going to # use a master list. # This will be a list of dicts of the # result dataframes (+ 1 dict containing the scenario inputs) results_list = [] scenarios_list = [(md, rc, id) for md in mitigation_dates for rc in relative_contact_rates for id in infectious_days] for (mit_date, rcr_pct, inf_days) in scenarios_list: sim_scenario = '{}_md{:%m%d}rc{:.0f}id{}'.format( experiment, mit_date, 100 * rcr_pct, int(inf_days)) # Update the parameters for this scenario p.mitigation_date = mit_date p.relative_contact_rate = rcr_pct p.infectious_days = inf_days if which_param_set == 'date_first_hospitalized': p.doubling_time = None else: p.date_first_hospitalized = None input_params_dict = OrderedDict(vars(p)) # Run the model m = Model(p) # Gather results results = gather_sim_results(m, sim_scenario, input_params_dict) # Append results to results list results_list.append(results.copy()) return results_list