def roi_df(args, model_name, roi): if args.fixed_t: args.roi = roi # Temporary csv = Path(args.data_path) / ("covidtimeseries_%s.csv" % args.roi) csv = csv.resolve() assert csv.exists(), "No such csv file: %s" % csv stan_data, t0 = ncs.get_stan_data(csv, args) global_start = datetime.strptime('01/22/20', '%m/%d/%y') frame_start = datetime.strptime(t0, '%m/%d/%y') day_offset = (frame_start - global_start).days else: day_offset = 0 model_path = ncs.get_model_path(args.models_path, model_name) extension = ['csv', 'pkl'][args.fit_format] rois = ncs.list_rois(args.fits_path, model_name, extension) if args.rois: rois = list(set(rois).intersection(args.rois)) fit_path = ncs.get_fit_path(args.fits_path, model_name, roi) if args.fit_format == 1: fit = ncs.load_fit(fit_path, model_path) stats = ncs.get_waic_and_loo(fit) samples = fit.to_dataframe() elif args.fit_format == 0: samples = ncs.extract_samples(args.fits_path, args.models_path, model_name, roi, args.fit_format) stats = ncs.get_waic(samples) df = ncs.make_table(roi, samples, args.params, stats, quantiles=args.quantiles, day_offset=day_offset) return model_name, roi, df
def roi_df(args, model_name, roi): model_path = ncs.get_model_path(args.models_path, model_name) extension = ['csv', 'pkl'][args.fit_format] rois = ncs.list_rois(args.fits_path, model_name, extension) if args.rois: rois = list(set(rois).intersection(args.rois)) fit_path = ncs.get_fit_path(args.fits_path, model_name, roi) if args.fit_format == 1: fit = ncs.load_fit(fit_path, model_path) stats = ncs.get_waic_and_loo(fit) samples = fit.to_dataframe() elif args.fit_format == 0: samples = ncs.extract_samples(args.fits_path, args.models_path, model_name, roi, args.fit_format) stats = ncs.get_waic(samples) df = ncs.make_table(roi, samples, args.params, stats, quantiles=args.quantiles) return model_name, roi, df
weights_out = tables_path / ('weights_for_averaging.csv') df_weights.to_csv(weights_out) roi_model_combos = ncs.get_fits_path_weights(df_weights) # load fits and extract samples per roi we have weights for for roi,models in roi_model_combos.items(): dfs = [] for model_name in models: model_path = ncs.get_model_path(args.models_path, model_name) extension = ['csv', 'pkl'][args.fit_format] fit_path = ncs.get_fit_path(args.fits_path, model_name, roi) df_roi = df_weights.loc[roi] model_name_weight = model_name + '_weight' weight = df_roi[model_name_weight] if args.fit_format == 1: fit = ncs.load_fit(fit_path, model_path) # stats = ncs.get_waic_and_loo(fit) samples = fit.to_dataframe() samples_weighted_df = samples.sample(frac=weight, replace=True) dfs.append(samples_weighted_df) elif args.fit_format == 0: samples = ncs.extract_samples(args.fits_path, args.models_path, model_name, roi, args.fit_format) stats = ncs.get_waic(samples) samples = fit.to_dataframe() samples_weighted_df = samples.sample(frac=weight, replace=True) dfs.append(samples_weighted_df) df_model_averaged = pd.concat(dfs) df_model_averaged.reset_index(inplace=True, drop=True) df_model_averaged.to_csv(Path(args.fits_path) / f'DiscreteAverage_{roi}.csv')