def cli(cfg): log.info('Entering application') cfg.workdir = str(Path.cwd()) fromdir = OmegaConf.select(cfg, 'fromdir', throw_on_missing=True) state = None if fromdir: fromdir = Path(to_absolute_path(fromdir)) cfg_orig, state = from_workdir(fromdir, cfg.state) cfg_orig = OmegaConf.masked_copy(cfg_orig, cfg_orig.keys() - cfg.keys()) cfg = OmegaConf.merge(cfg_orig, cfg) try: if cfg.seed is not None: log.info(f'Setting random seed: {cfg.seed}') torch.manual_seed(cfg.seed) device = cfg.device if cfg.anomaly: torch.autograd.set_detect_anomaly(True) log.warn('Setting anomaly detection on') cfg = instantiate_wf(cfg, fromdir, state) log.info(f'Moving to {device}...') cfg['task']['wf'].to(device) log.info(f'Moved to {device}') call(cfg['task'], _convert_='all') except hydra.errors.InstantiationException as e: raise e.__cause__ from None except KeyboardInterrupt: log.warning('Interrupted!')
def main(config: DictConfig): env = call(config.environment) callbacks = list(instantiate(config.callbacks)["callbacks"]) model = call(config.model, env) model.learn(total_timesteps=config.run["max_timesteps"], callback=callbacks) if config.run["save"]: model.save(config.model["save_path"])
def get_activity_metadata(cfg: DictConfig, id: int) -> Dict[str, Any]: """query metadata from strava api for a given Args: cfg (DictConfig): [description] id (int): activity id Returns: Dict[str, Any]: activity data """ # get metadata with MySession(cfg) as session: meta = session.get(cfg.api.BASE_URL+f'/activities/{id}') meta_dict = meta.json() # save to dict res = dict() for col in cfg.api.TO_GET: res[col] = meta_dict.get(col) res['gear'] = meta_dict['gear'].get( 'name') if 'gear' in meta_dict else None res['id'] = id # convert date strings to python datetime if res['start_date_local'] is not None: res['start_date_local'] = pd.to_datetime(res['start_date_local'])\ .to_pydatetime() # write to db if configured if cfg.TO_DB: to_db = pd.DataFrame(res, index=[0]) to_db['last_update'] = save_datetime_now() with sqlite3.connect(call(cfg.DB)) as con: to_db.to_sql('ACTIVITIES_META', con=con, if_exists='append', index=False, index_label='id') # sleep to net get kicked form api time.sleep(cfg.api.SLEEP) return res
def main(cfg: DictConfig) -> None: wp_maker = instantiate(cfg['wp_maker']) wp_maker._taus = call(cfg['create_df']) wp_maker.run() wp_maker.print_wp() if wp_maker.is_converged(): if os.path.exists(path_to_mlflow := to_absolute_path( cfg['create_df']['path_to_mlflow'])):
def get_token_status(cfg: DictConfig) -> TokenStatus: with sqlite3.connect(call(cfg.DB)) as con: if table_exists(con, 'AUTH_INFO'): auth_info = get_latest(cfg, ['expires_at']) expire_date = pd.to_datetime(auth_info['expires_at']) if expire_date > datetime.now(): return TokenStatus.ACTIVE return TokenStatus.EXPIRED return TokenStatus.NO_TOKEN
def main(cfg: DictConfig): session = Session( repo=(Path.home() / "projects/rlbook/experiments/outputs/bandit").as_posix(), experiment=cfg.bandit["_target_"].split(".")[-1], ) testbed = instantiate(cfg.testbed) bandit = instantiate(cfg.bandit, Q_init=call(cfg.Q_init, testbed)) local_logger.info(f"Running bandit: {cfg.run}") local_logger.debug(f"Testbed expected values: {testbed.expected_values}") local_logger.debug(f"bandit config: {cfg['bandit']}") local_logger.debug(f"run config: {cfg['run']}") session.set_params(OmegaConf.to_container(cfg.run), "experiment") bandit.run(testbed, **OmegaConf.to_container(cfg.run)) df_ar = bandit.output_df() df_ar = optimal_action(df_ar) local_logger.debug(f"\n{df_ar[['run', 'step', 'action', 'optimal_action', 'reward']].head(15)}") bandit_type = cfg.bandit._target_.split(".")[-1] Q_init = cfg.Q_init._target_.split(".")[-1] task_name = f"{bandit_type} - " + ", ".join( [ f"{k}: {OmegaConf.select(cfg, v).split('.')[-1]}" if isinstance(OmegaConf.select(cfg, v), str) else f"{k}: {OmegaConf.select(cfg, v)}" for k, v in cfg.task_labels.items() ] ) local_logger.debug(f"{task_name}") hp_testbed = OmegaConf.to_container(cfg.testbed) hp = OmegaConf.to_container(cfg.bandit) hp["Q_init"] = cfg.Q_init._target_ hp["p_drift"] = hp_testbed["p_drift"] session.set_params(hp, "hyperparameters") # for i in range(min(3, cfg.run.n_runs)): # fig = steps_violin_plotter(df_ar, testbed, run=i) # writer.add_figure(f"run{i}", fig, global_step=cfg.run.steps) final_avg_reward = write_scalars(df_ar, session, "reward", "average_reward", hp) final_optimal_action = write_scalars( df_ar, session, "optimal_action_percent", "optimal_action_percent", hp ) final_metrics = { "average_reward": final_avg_reward, "optimal_action_percent": final_optimal_action, } session.set_params(final_metrics, "final_metrics") local_logger.debug(f"final_metrics: {final_metrics}")
def last_update(cfg: DictConfig, table: str, column: str = 'last_update') -> datetime: with sqlite3.connect(call(cfg.DB)) as con: last_update = con.execute( f""" SELECT MAX({column}) FROM {table} """, ).fetchone()[0] # convert string to datetime return datetime.fromisoformat(last_update)
def get_avail_ids(cfg: DictConfig, table: str) -> Set[int]: # util function to get all available ids in a table query = f""" SELECT id FROM {table} """ with sqlite3.connect(call(cfg.DB)) as con: if table_exists(con, table): ids = pd.read_sql(query, con) ids = set(ids.iloc[:, 0]) return ids return set()
def update_tokens(cfg: DictConfig, token: Dict[str, Any]) -> pd.DataFrame: token_dict = dict() token_dict['token_type'] = token['token_type'] token_dict['access_token'] = token['access_token'] token_dict['refresh_token'] = token['refresh_token'] token_dict['expires_at'] = datetime.fromtimestamp(token['expires_at']) token_dict['last_update'] = save_datetime_now() token_dict['user_id'] = cfg.api.CLIENT_ID token_df = pd.DataFrame(token_dict, index=[0]) with sqlite3.connect(call(cfg.DB)) as con: token_df.to_sql('AUTH_INFO', con=con, if_exists='append', index=False, index_label='last_update') return token_df
def get_latest(cfg: DictConfig, columns: List[str]) -> Dict[str, Any]: columns = ", ".join(columns) query = f""" SELECT {columns} FROM AUTH_INFO WHERE user_id = {cfg.api.CLIENT_ID} ORDER BY last_update desc LIMIT 1; """ with sqlite3.connect(call(cfg.DB)) as con: auth = pd.read_sql(query, con) auth = auth.iloc[0, :].to_dict() return auth
def get_activity_rawdata(cfg: DictConfig, id: int) -> pd.DataFrame: """query rawdata from strava api for a given id Args: cfg (DictConfig): configuration id (int): activity id Returns: pd.DataFrame: activity rawdata """ # get streams from activity with MySession(cfg) as session: stream = session.get( cfg.api.BASE_URL+f'/activities/{id}/streams', params={'keys': ','.join(cfg.api.STREAMS)}) activity = dict() found = list() # iterate over values for values in stream.json(): activity[values['type']] = values['data'] found.append(values['type']) # make df from dict res_df = pd.DataFrame(activity) # extract lat and long if available try: res_df['lat'] = res_df['latlng'].apply(lambda x: x[0]) res_df['long'] = res_df['latlng'].apply(lambda x: x[-1]) except KeyError: res_df['lat'], res_df['long'] = np.nan, np.nan # check if columns missing - if so set as nan missings = set(cfg.api.STREAMS) - set(found) if missings: for missing in missings: res_df[missing] = np.nan # save id res_df['id'] = id # delte old latlng if 'latlng' in res_df: del res_df['latlng'] # write to DB if configured if cfg.TO_DB: res_df['last_update'] = save_datetime_now() with sqlite3.connect(call(cfg.DB)) as con: res_df.to_sql('ACTIVITIES_RAW', con=con, if_exists='append', index=False, index_label='id') time.sleep(cfg.api.SLEEP) return res_df