Ejemplo n.º 1
0
    if inargs.save_preds:
        if verbose > 0: print('Save predictions')
        preds = model.predict(test_features, batch_size=4096)
        results_df = create_results_df(
            test_set.date_strs,
            test_set.station_ids,
            preds[:, 0],
            preds[:, 1],
            train_time,
            model.count_params(),
        )
        results_df.to_csv(inargs.results_dir + inargs.exp_name + '.csv')


if __name__ == '__main__':
    p = ArgParser()

    # Config file
    p.add('-c', '--config', is_config_file=True, help='Config file path.')

    # Directories and experiment name
    p.add_argument(
        '--data_dir',
        type=str,
        default='/project/meteo/w2w/C7/ppnn_data/',
        help='Directory containing input data. '
        'Default: /project/meteo/w2w/C7/ppnn_data/',
    )
    p.add_argument(
        '--results_dir',
        type=str,
Ejemplo n.º 2
0
    # TODO combine into one data frame
    return Result(stages=s, output=Namespace(first_level_results=first_level_results,
                                             overall_determinants=overall_determinants,
                                             resampled_determinants=resampled_determinants.drop(
                                                 ['options', 'build_model', 'files'],
                                                 axis=1)))

def _mk_tamarack_parser(p):
    #p.add_argument("--csv-file", dest="csv_file", type=str, required=True,
    #               help="CSV file containing at least 'group' and 'file' columns")
    p.add_argument("--common-time-point", dest="common_time_pt", type=float, required=True,
                   help="Time point to resample everything to (must be one of the group IDs).")
    return p


_tamarack_parser = BaseParser(_mk_tamarack_parser(ArgParser(add_help=False)), group_name='tamarack')
tamarack_parser = AnnotatedParser(parser=_tamarack_parser, namespace="tamarack")

def main(args):
    # TODO rewrite using `mk_application`
    p = CompoundParser(
          [execution_parser,
           application_parser,
           registration_parser,
           tamarack_parser,
           AnnotatedParser(parser=mk_mbm_parser(with_common_space=False,
                                                with_maget=True),
                           namespace="mbm")])

    options = parse(p, args[1:])
    stages = tamarack_pipeline(options).stages
Ejemplo n.º 3
0
 def __init__(self):
     self.__arg_parser = ArgParser()
     self.__logger = logging.getLogger(self.__class__.__name__)
Ejemplo n.º 4
0
def load_args(my_config=None):
    p = ArgParser()
    p.add_argument('-c',
                   '--my-config',
                   is_config_file=True,
                   help='config file path',
                   default=my_config)
    p.add_argument('--datadir', type=str, required=True, help='Path to data')
    p.add_argument('--exp_id',
                   type=str,
                   required=True,
                   help='Experiment identifier')
    p.add_argument('--model_save_dir',
                   type=str,
                   required=True,
                   help='Path to save model')
    p.add_argument('--pred_save_dir',
                   type=str,
                   required=True,
                   help='Path to save predictions')
    p.add_argument('--var_dict',
                   required=True,
                   help='Variables: as an ugly dictionary...')
    p.add_argument('--output_vars',
                   nargs='+',
                   help='Output variables. Format {var}_{level}',
                   default=None)
    p.add_argument('--filters',
                   type=int,
                   nargs='+',
                   required=True,
                   help='Filters for each layer')
    p.add_argument('--kernels',
                   type=int,
                   nargs='+',
                   default=None,
                   help='Kernel size for each layer')
    p.add_argument('--lead_time',
                   type=int,
                   required=True,
                   help='Forecast lead time')
    # p.add_argument('--iterative', type=bool, default=False, help='Is iterative forecast')
    # p.add_argument('--iterative_max_lead_time', type=int, default=5*24, help='Max lead time for iterative forecasts')
    p.add_argument('--lr', type=float, default=1e-4, help='Learning rate')
    p.add_argument('--activation',
                   type=str,
                   default='relu',
                   help='Activation function')
    p.add_argument('--batch_size', type=int, default=64, help='batch_size')
    p.add_argument('--epochs', type=int, default=1000, help='epochs')
    p.add_argument('--optimizer', type=str, default='adam', help='Optimizer')
    p.add_argument('--early_stopping_patience',
                   type=int,
                   default=None,
                   help='Early stopping patience')
    p.add_argument('--min_es_delta',
                   type=float,
                   default=0,
                   help='Minimum improvement for early stopping')
    p.add_argument('--restore_best_weights',
                   type=int,
                   default=1,
                   help='ES parameter')
    p.add_argument('--reduce_lr_patience',
                   type=int,
                   default=None,
                   help='Reduce LR patience')
    p.add_argument('--reduce_lr_factor',
                   type=float,
                   default=0.2,
                   help='Reduce LR factor')
    p.add_argument('--min_lr_times',
                   type=int,
                   default=1,
                   help='Reduce LR patience')
    p.add_argument('--lr_step', type=int, default=None, help='LR decay step')
    p.add_argument('--lr_divide',
                   type=int,
                   default=None,
                   help='LR decay division factor')
    p.add_argument('--loss', type=str, default='mse', help='Loss function')
    p.add_argument('--train_years',
                   type=str,
                   nargs='+',
                   default=('1979', '2015'),
                   help='Start/stop years for training')
    p.add_argument('--valid_years',
                   type=str,
                   nargs='+',
                   default=('2016', '2016'),
                   help='Start/stop years for validation')
    p.add_argument('--test_years',
                   type=str,
                   nargs='+',
                   default=('2017', '2018'),
                   help='Start/stop years for testing')
    p.add_argument('--data_subsample',
                   type=int,
                   default=1,
                   help='Subsampling for training data')
    p.add_argument('--norm_subsample',
                   type=int,
                   default=1,
                   help='Subsampling for mean/std')
    p.add_argument('--gpu', type=int, default=0, help='Which GPU', nargs='+')
    p.add_argument('--network_type', type=str, default='resnet', help='Type')
    p.add_argument('--bn_position',
                   type=str,
                   default=None,
                   help='pre, mid or post')
    p.add_argument('--nt_in',
                   type=int,
                   default=1,
                   help='Number of input time steps')
    p.add_argument('--dt_in',
                   type=int,
                   default=1,
                   help='Time step of intput time steps (after subsampling)')
    p.add_argument('--use_bias',
                   type=int,
                   default=1,
                   help='Use bias in resnet convs')
    p.add_argument('--l2', type=float, default=0, help='Weight decay')
    p.add_argument('--momentum', type=float, default=0.9, help='Momentum')
    p.add_argument('--dropout', type=float, default=0, help='Dropout')
    p.add_argument('--skip',
                   type=int,
                   default=1,
                   help='Add skip convs in resnet builder')
    # p.add_argument('--u_skip', type=int, default=1, help='Add skip convs in unet')
    # p.add_argument('--unet_layers', type=int, default=5, help='Number of unet layers')
    p.add_argument('--unres',
                   type=int,
                   default=2,
                   nargs='+',
                   help='Resblocks per unet partition')
    p.add_argument('--cmip', type=int, default=0, help='Is CMIP')
    p.add_argument('--cmip_dir',
                   type=str,
                   default=None,
                   nargs='+',
                   help='Dirs for CMIP data')
    p.add_argument('--pretrained_model',
                   type=str,
                   default=None,
                   help='Path to pretrained model')
    p.add_argument('--last_pretrained_layer',
                   type=str,
                   default=None,
                   help='Name of last pretrained layer')
    p.add_argument('--last_trainable_layer',
                   type=str,
                   default=None,
                   help='Name of last trainable layer')
    p.add_argument('--ext_mean',
                   type=str,
                   default=None,
                   help='External normalization mean')
    p.add_argument('--ext_std',
                   type=str,
                   default=None,
                   help='External normalization std')
    p.add_argument('--cont_time',
                   type=int,
                   default=0,
                   help='Continuous time 0/1')
    p.add_argument('--multi_dt',
                   type=int,
                   default=1,
                   help='Differentiate through multiple time steps')
    p.add_argument('--parametric', type=int, default=0, help='Is parametric')
    p.add_argument('--one_cycle', type=int, default=0, help='Use OneCycle LR')
    # p.add_argument('--las_kernel', type=int, default=None, help='')
    # p.add_argument('--las_gauss_std', type=int, default=None, help='')
    p.add_argument('--long_skip',
                   type=int,
                   default=0,
                   help='Use long skip connections 0/1')

    p.add_argument('--train_tfr_files',
                   type=str,
                   default=None,
                   help='TFR regex')
    p.add_argument('--valid_tfr_files',
                   type=str,
                   default=None,
                   help='TFR regex')
    p.add_argument('--test_tfr_files',
                   type=str,
                   default=None,
                   help='TFR regex')
    p.add_argument('--tfr_num_parallel_calls', type=int, default=1, help='')
    p.add_argument('--tfr_buffer_size', type=int, default=100, help='')
    p.add_argument('--tfr_prefetch', type=int, default=None, help='')

    p.add_argument('--min_lead_time', type=int, default=None, help='for cont.')
    p.add_argument('--y_roll', type=int, default=None, help='In hours.')
    p.add_argument('--X_roll', type=int, default=None, help='In hours.')
    p.add_argument('--discard_first',
                   type=int,
                   default=None,
                   help='Discard first x time steps in train generator')

    p.add_argument('--relu_idxs',
                   type=int,
                   default=None,
                   nargs='+',
                   help='for tp')
    p.add_argument('--tp_log', type=float, default=None, help='for tp')

    p.add_argument('--tfr_out',
                   type=int,
                   default=0,
                   help='output all times up to lead time')
    p.add_argument('--tfr_out_idxs',
                   type=int,
                   default=None,
                   nargs='+',
                   help='out idxs')

    p.add_argument('--predict_difference', type=int, default=0, help='')
    p.add_argument('--is_categorical', type=int, default=0, help='')
    p.add_argument('--bin_min', type=float, default=None, help='')
    p.add_argument('--bin_max', type=float, default=None, help='')
    p.add_argument('--num_bins', type=int, default=None, help='')
    p.add_argument('--quantile_bins', type=int, default=0, help='')

    args = p.parse_args() if my_config is None else p.parse_args(args=[])
    args.var_dict = ast.literal_eval(args.var_dict)
    return vars(args)
Ejemplo n.º 5
0
                and not self.overwrite):
            print(
                "Found manfiest files, skipping ingest, use --overwrite to overwrite them."
            )
            return

        for setn, manifest in self.manifests.items():
            pairs = self.train_or_val_pairs(setn)
            records = [(os.path.relpath(fname, self.out_dir), int(tgt))
                       for fname, tgt in pairs]
            records.insert(0, ('@FILE', 'STRING'))
            np.savetxt(manifest, records, fmt='%s\t%s')


if __name__ == "__main__":
    parser = ArgParser()
    parser.add_argument('--input_dir',
                        required=True,
                        help='Directory to find input tars',
                        default=None)
    parser.add_argument('--out_dir',
                        required=True,
                        help='Directory to write ingested files',
                        default=None)
    parser.add_argument(
        '--target_size',
        type=int,
        default=256,
        help=
        'Size in pixels to scale shortest side DOWN to (0 means no scaling)')
    parser.add_argument('--overwrite',
Ejemplo n.º 6
0
                                      "Thickness calculation options.")
    group.add_argument("--run-thickness",
                       action='store_true',
                       dest="run_thickness",
                       help="Run thickness computation.")
    group.add_argument("--no-run-thickness",
                       action='store_false',
                       dest="run_thickness",
                       help="Don't run thickness computation.")
    parser.set_defaults(run_thickness=True)
    group.add_argument(
        "--label-mapping",
        type=str,
        dest="label_mapping",
        help="path to CSV file mapping; see minclaplace/wiki/LaplaceGrid")
    group.add_argument(
        "--atlas-fwhm",
        dest="atlas_fwhm",
        type=float,  # default ?!
        help="Blurring kernel (mm) for atlas")
    group.add_argument(
        "--thickness-fwhm",
        dest="thickness_fwhm",
        type=float,  # default??
        help="Blurring kernel (mm) for cortical surfaces")
    return parser


thickness_parser = AnnotatedParser(parser=BaseParser(
    _mk_thickness_parser(ArgParser(add_help=False)), "thickness"),
                                   namespace="thickness")
Ejemplo n.º 7
0
def argument_parser():
    parser = ArgParser()
    parser.add_argument('--parties-table',
                        default='Parties',
                        env_var='PARTIES_TABLE')
    parser.add_argument('--drivers-table',
                        default='Drivers',
                        env_var='DRIVERS_TABLE')
    parser.add_argument('--passengers-table',
                        default='Passengers',
                        env_var='PASSENGERS_TABLE')
    parser.add_argument(
        '--envelope-bucket',
        env_var='ENVELOPE_BUCKET',
        default='http://s3.amazonaws.com/flyingj-wedding/envelopes/')
    parser.add_argument(
        '--template-bucket',
        env_var='TEMPLATE_BUCKET',
        default='flyingj-wedding/templates',
        help='S3 bucket name and prefix where templates are kept')
    parser.add_argument('--invitation-template',
                        env_var='INVITATION_TEMPLATE',
                        default='invitation_template.html',
                        help='S3 key of the invitation template')
    parser.add_argument('--rideshare-template',
                        env_var='RIDESHARE_TEMPLATE',
                        default='rideshare_template.html',
                        help='S3 key of the rideshare-form template')
    parser.add_argument('--rsvp-template',
                        env_var='RSVP_TEMPLATE',
                        default='rsvp_template.html',
                        help='S3 key of the rsvp template')
    parser.add_argument('--thank-you-template',
                        env_var='THANK_YOU_TEMPLATE',
                        default='thank_you_template.html',
                        help='S3 key of the thank-you-for-your-RSVP template')
    parser.add_argument('--error-url',
                        env_var='ERROR_URL',
                        default='https://www.flyingjs4.life/500.html')
    parser.add_argument(
        '--rideshare-url',
        env_var='RIDESHARE_URL',
        default=
        '/rideshare?guest={{guestId}}&party={{partyId}}&local={{local}}&rideshare={{rideshare}}',
        help='URL template for the ride sharing form.')
    parser.add_argument(
        '--decline-url',
        env_var='DECLINE_URL',
        default='/decline.html',
        help='URL of the page to show when a whole party is not attending.')
    parser.add_argument('--thank-you-url',
                        env_var='THANK_YOU_URL',
                        default='/thanks?firstName={{firstName}}',
                        help='URL template for the thank-you page.')
    parser.add_argument('--homepage-url',
                        env_var='HOMEPAGE_URL',
                        default='https://www.flyingjs4.life/index.html',
                        help='URL of the Flying Js wedding website homepage!')
    parser.add_argument(
        '--verbosity',
        env_var='VERBOSITY',
        default=logging.getLevelName(logging.WARNING),
        help=
        f'Logging verbosity. One of: {",".join(logging._nameToLevel.keys())}',
        type=lambda level: logging._nameToLevel[level.upper()])
    return parser
Ejemplo n.º 8
0
def test_sentic_pipeline(
    pipeline_storage,
    sentic_zip_url,
    full_sentic_zip_client,
    full_sentic_zip_owl_filename,
):
    argparse = ArgParser()
    SenticPipeline.add_arguments(argparse)

    args = argparse.parse_args(
        [
            "--sentic_zip_url",
            sentic_zip_url,
            "--owl_filename",
            full_sentic_zip_owl_filename,
        ]
    )
    pipeline_kwds = vars(args).copy()
    sentic_pipeline = SenticPipeline(
        http_client=full_sentic_zip_client, **pipeline_kwds
    )
    pipeline_wrapper = PipelineWrapper(sentic_pipeline, pipeline_storage)

    extract_kwds = pipeline_wrapper.extract()
    graph_generator = pipeline_wrapper.transform(**extract_kwds)

    nodes_by_id = {}
    primitive_ids = set()
    sentic_ids = set()
    edges_by_subject = {}
    for node_or_edge in graph_generator:
        if isinstance(node_or_edge, KgNode):
            node = node_or_edge
            nodes_by_id[node.id] = node
            type = node.id.split(":", 2)[1]
            if type == sentic_types.PRIMITIVE:
                primitive_ids.add(node.id)
            elif type == sentic_types.SENTIC:
                sentic_ids.add(node.id)
        elif isinstance(node_or_edge, KgEdge):
            edge = node_or_edge
            subject_edges = edges_by_subject.setdefault(edge.subject, [])
            subject_edges.append(node_or_edge)

    # assert that all concept nodes are related to sentics, primitives and other concepts
    for id, node in nodes_by_id.items():
        type = node.id.split(":", 2)[1]
        if type != sentic_types.CONCEPT:
            continue
        assert id in edges_by_subject
        concept_edges, primitive_edges, sentic_edges = [], [], []
        for edge in edges_by_subject[id]:
            if edge.object in primitive_ids:
                primitive_edges.append(edge)
            elif edge.object in sentic_ids:
                sentic_edges.append(edge)
            else:
                concept_edges.append(edge)
        assert len(concept_edges) > 0, node
        assert len(primitive_edges) > 0, node
        assert len(sentic_edges) > 0, node
Ejemplo n.º 9
0
def main():
    p = ArgParser()
    p.add("-c", "--my-config", is_config_file=True, help="config file path")
    p.add(
        "-o",
        "--out",
        help="output directory, '-' for stdin",
        type=DirectoryType(),
        required=True,
    )
    p.add(
        "-a",
        "--as_of",
        default=0,
        help="number of days in the past to project from",
        type=int,
    )
    p.add("-y", "--y_max", help="max y-scale for the census graph", type=int)
    p.add(
        "-d",
        "--n_days",
        help="make a census/admits plot out to n_days",
        type=int,
        action="append",
    )
    p.add("-P", "--prefix", help="prefix for filenames")
    p.add(
        "-pp",
        "--plot_pairs",
        action="store_true",
        help="Plot posterior samples in a pair-plot grid",
    )
    p.add(
        "-pc",
        "--plot_capacity",
        action="store_true",
        help="plot capacity as a horizontal line",
    )

    options = p.parse_args()

    prefix = ""
    if options.prefix is not None:
        prefix = f"{options.prefix}_"

    n_days = [30, 90, 180]
    if options.n_days:
        n_days = options.n_days

    dir = options.out
    print(f"Output directory: {dir}")
    paramdir = path.join(dir, "parameters")
    outdir = path.join(dir, "output")
    figdir = path.join(dir, "figures")

    census_ts, params, args = read_inputs(paramdir)
    first_day = census_ts["date"].values[0]

    # TODO: This needs to be configurable based on the time period specificed
    as_of_days_ago = args["as_of"]
    nobs = census_ts.shape[0] - as_of_days_ago

    # define capacity
    vent_capacity, hosp_capacity = None, None
    if options.plot_capacity:
        vent_capacity = float(params.base.loc[params.param == "vent_capacity"])
        hosp_capacity = float(params.base.loc[params.param == "hosp_capacity"])

    # Chains
    df = pd.read_json(path.join(f"{outdir}", "chains.json.bz2"),
                      orient="records",
                      lines=True)
    print(f"READ chains file: {df.shape[0]} total iterations")
    # remove burn-in
    # TODO: Make 1000 configurable
    df = df.loc[(df.iter > 1000)]

    qlist = []
    for day in range(census_ts.shape[0]):
        ldist = logistic(df.logistic_L, df.logistic_k,
                         df.logistic_x0 - df.offset.astype(int), day)
        qlist.append(np.quantile(ldist, [0.05, 0.5, 0.95]))

    # logistic SD plot
    qmat = np.vstack(qlist)
    fig = plt.figure()

    plt.plot(list(range(census_ts.shape[0])), 1 - qmat[:, 1])
    plt.fill_between(
        x=list(range(census_ts.shape[0])),
        y1=1 - qmat[:, 0],
        y2=1 - qmat[:, 2],
        alpha=0.3,
        lw=2,
        edgecolor="k",
    )
    plt.ylabel(f"Relative (effective) social contact")
    plt.xlabel(f"Days since {first_day}")
    plt.ylim(0, 1)
    fig.savefig(path.join(f"{figdir}", f"{prefix}effective_soc_dist.pdf"))

    for howfar in n_days:
        plt_predictive(
            df,
            first_day,
            census_ts,
            figdir,
            as_of_days_ago,
            howfar=howfar,
            prefix=prefix,
            y_max=options.y_max,
            hosp_capacity=hosp_capacity,
            vent_capacity=vent_capacity,
        )

    mk_projection_tables(df, first_day, outdir)

    toplot = df[[
        "beta",
        "hosp_prop",
        "ICU_prop",
        "vent_prop",
        "hosp_LOS",
        "ICU_LOS",
        "vent_LOS",
        "incubation_days",
        "recovery_days",
        "logistic_k",
        "logistic_x0",
        "logistic_L",
        "nu",
    ]]

    pspace = np.linspace(0.001, 0.999, 1000)

    fig, ax = plt.subplots(figsize=(8, 40), ncols=1, nrows=len(toplot.columns))
    for i in range(len(toplot.columns)):
        cname = toplot.columns[i]
        if params.loc[params.param == cname,
                      "distribution"].iloc[0] == "gamma":
            x = sps.gamma.ppf(
                pspace,
                params.loc[params.param == cname, "p1"],
                0,
                params.loc[params.param == cname, "p2"],
            )
            y = sps.gamma.pdf(
                x,
                params.loc[params.param == cname, "p1"],
                0,
                params.loc[params.param == cname, "p2"],
            )
        elif params.loc[params.param == cname,
                        "distribution"].iloc[0] == "beta":
            x = sps.beta.ppf(
                pspace,
                params.loc[params.param == cname, "p1"],
                params.loc[params.param == cname, "p2"],
            )
            y = sps.beta.pdf(
                x,
                params.loc[params.param == cname, "p1"],
                params.loc[params.param == cname, "p2"],
            )
        ax[i].plot(x, y, label="prior")
        ax[i].hist(toplot[cname], density=True, label="posterior", bins=30)
        ax[i].set_xlabel(params.loc[params.param == cname,
                                    "description"].iloc[0])
        ax[i].legend()
    plt.tight_layout()
    fig.savefig(path.join(f"{figdir}", f"{prefix}marginal_posteriors_v2.pdf"))

    if options.plot_pairs:
        #  Make a pair plot for diagnosing posterior dependence
        plt_pairplot_posteriors(toplot, figdir, prefix=prefix)
Ejemplo n.º 10
0
def _mk_varian_recon_parser():
    p = ArgParser(add_help=False)
    p.add_argument("--fid-input-dir",
                   dest="fid_input_directory",
                   type=str,
                   default='.',
                   help="Directory where fid files are located")
    p.add_argument(
        "--petable",
        dest="petable",
        type=str,
        default=None,
        help=
        "petable name and location if not specified by procpar file and current directory"
    )
    p.add_argument(
        "--mouse_list",
        dest="mouse_list",
        type=str,
        default=None,
        help=
        "reconstruct list of mice instead of all mice (zero-based indexing, comma-delimited, no spaces)"
    )
    p.add_argument("--fermi_ellipse",
                   dest="fermi_ellipse",
                   action="store_true",
                   default=False,
                   help="apply fermi ellipse filter")
    p.add_argument(
        "--grappa_coil_groupings",
        dest="grappa_coil_groupings",
        type=str,
        default=None,
        help="groups of cross-talking coils (example: 0,2,5;1,3,4,6 )")
    p.add_argument("--procpar_file_name",
                   dest="procpar_file_name",
                   type=str,
                   default=None,
                   help="procpar file name (if not simply 'procpar')")
    p.add_argument("--output-file-name",
                   dest="output_file_name",
                   type=str,
                   default='',
                   help="Name to give output files")
    p.add_argument(
        "--num-reps",
        dest="num_reps",
        type=int,
        default=None,
        help=
        "Tell code how many reps you expect varian_recon to spit out for each file"
    )
    p.add_argument("--num-fids",
                   dest="num_fids",
                   type=int,
                   default=None,
                   help="Tell code how many fid files you are providing")
    p.add_argument(
        "--final-dc-crop-output-dir",
        dest="final_dc_crop_output_dir",
        type=str,
        default='.',
        help=
        "Directory where you want the final dc and cropped images to be placed."
    )
    return p
Ejemplo n.º 11
0
def main():
    p = ArgParser()
    p.add("-c", "--my-config", is_config_file=True, help="config file path")
    p.add(
        "-o",
        "--out",
        help="output directory, '-' for stdin",
        type=DirectoryType(),
        required=True,
    )
    p.add(
        "-a",
        "--as_of",
        default=0,
        help="number of days in the past to project from",
        type=int,
    )
    p.add("-y", "--y_max", help="max y-scale for the census graph", type=int)
    p.add(
        "-d",
        "--n_days",
        help="make a census/admits plot out to n_days",
        type=int,
        action="append",
    )
    p.add("-P", "--prefix", help="prefix for filenames")
    p.add(
        "-pp",
        "--plot_pairs",
        action="store_true",
        help="Plot posterior samples in a pair-plot grid",
    )
    p.add(
        "-pc",
        "--plot_capacity",
        action="store_true",
        help="plot capacity as a horizontal line",
    )
    p.add("-b",
          "--burn_in",
          type=int,
          help="how much of the burn-in to discard",
          default=2000)

    options = p.parse_args()
    burn_in = options.burn_in
    prefix = ""
    if options.prefix is not None:
        prefix = f"{options.prefix}_"

    n_days = [30, 90, 180]
    if options.n_days:
        n_days = options.n_days

    dir = options.out
    print(f"Output directory: {dir}")
    paramdir = path.join(dir, "parameters")
    outdir = path.join(dir, "output")
    figdir = path.join(dir, "figures")

    census_ts, params, args = read_inputs(paramdir)
    first_day = census_ts[census_ts.columns[0]].values[
        0]  #weird chack for non-ascii characters in the input file

    # TODO: This needs to be configurable based on the time period specificed
    as_of_days_ago = args["as_of"]
    nobs = census_ts.shape[0] - as_of_days_ago

    # define capacity
    vent_capacity, hosp_capacity = None, None
    if options.plot_capacity:
        vent_capacity = float(params.base.loc[params.param == "vent_capacity"])
        hosp_capacity = float(params.base.loc[params.param == "hosp_capacity"])


# df = pd.read_json("/Users/crandrew/projects/chime_sims/output/2020_05_08_20_38_34/output/chains.json.bz2", lines = True)
# census_ts = pd.read_csv('/Users/crandrew/projects/chime_sims/output/2020_05_08_20_38_34/parameters/census_ts.csv')
# params = pd.read_csv('/Users/crandrew/projects/chime_sims/output/2020_05_08_20_38_34/parameters/params.csv')

# Chains
    df = pd.read_json(path.join(f"{outdir}", "chains.json.bz2"),
                      orient="records",
                      lines=True)
    print(f"READ chains file: {df.shape[0]} total iterations")
    # remove burn-in

    iters_remaining = df.iter.max() - burn_in
    assert iters_remaining > 100, f"Breaking here: you are casting aside {burn_in} iterations as burn-in, but there are only {df.iter.max()} iteratons per chain"
    if iters_remaining < 1000:
        warnings.warn(
            f"You're only using {iters_remaining} iterations per chain.  This may not be fully cromulent."
        )
    df = df.loc[(df.iter > burn_in)]

    # make the social distancing plot
    SD_plot(census_ts, params, df, figdir, prefix)

    ##
    for howfar in n_days:
        plt_predictive(
            df,
            first_day,
            census_ts,
            figdir,
            as_of_days_ago,
            howfar=howfar,
            prefix=prefix,
            y_max=options.y_max,
            hosp_capacity=hosp_capacity,
            vent_capacity=vent_capacity,
        )

    mk_projection_tables(df, first_day, outdir)

    toplot = df[[
        "beta",
        "hosp_prop",
        "ICU_prop",
        "vent_prop",
        "hosp_LOS",
        "ICU_LOS",
        "vent_LOS",
        "incubation_days",
        "recovery_days",
        "logistic_k",
        "logistic_x0",
        "logistic_L",
        "nu",
    ]]

    pspace = np.linspace(0.001, 0.999, 1000)

    fig, ax = plt.subplots(figsize=(8, 40), ncols=1, nrows=len(toplot.columns))
    for i in range(len(toplot.columns)):
        cname = toplot.columns[i]
        if params.loc[params.param == cname,
                      "distribution"].iloc[0] == "gamma":
            x = sps.gamma.ppf(
                pspace,
                params.loc[params.param == cname, "p1"],
                0,
                params.loc[params.param == cname, "p2"],
            )
            y = sps.gamma.pdf(
                x,
                params.loc[params.param == cname, "p1"],
                0,
                params.loc[params.param == cname, "p2"],
            )
        elif params.loc[params.param == cname,
                        "distribution"].iloc[0] == "beta":
            x = sps.beta.ppf(
                pspace,
                params.loc[params.param == cname, "p1"],
                params.loc[params.param == cname, "p2"],
            )
            y = sps.beta.pdf(
                x,
                params.loc[params.param == cname, "p1"],
                params.loc[params.param == cname, "p2"],
            )
        ax[i].plot(x, y, label="prior")
        ax[i].hist(toplot[cname], density=True, label="posterior", bins=30)
        ax[i].set_xlabel(params.loc[params.param == cname,
                                    "description"].iloc[0])
        ax[i].legend()
    plt.tight_layout()
    fig.savefig(path.join(f"{figdir}", f"{prefix}marginal_posteriors_v2.pdf"))

    if options.plot_pairs:
        #  Make a pair plot for diagnosing posterior dependence
        plt_pairplot_posteriors(toplot, figdir, prefix=prefix)
Ejemplo n.º 12
0
def ParseArgs():
    parser = ArgParser(default_config_files=[
                       os.getcwd() + '/src/initial_configurations/default'])

    # Core settings
    core_parse = parser.add_argument_group('Core setting')
    core_parse.add_argument('-s', '--start_date',         dest='start_date',
                            default=START_DATE,   type=str, help='Training start date')
    core_parse.add_argument('-p','--train_period',       dest='train_period',
                            default=TRAIN_PERIOD, type=int, help='Time period of training file is used')
    core_parse.add_argument('-n','--new_run',              dest='new_run',         default=1,
                             type=int, help='If the model checkpoint is erased to run new model')
    core_parse.add_argument('-l','--local_run',            dest='local_run',       default=1,
                             type=int, help='If the parameter JSON file is kept locally insteat to  redis')
    core_parse.add_argument('-nni','--tuning',               dest='tuning',          default=0,
                             type=int, help='Whether or not to peform hyper parameter tuning')

    # Data
    data_parse = parser.add_argument_group('Data setting')
    data_parse.add_argument('--train_data_path',    dest='train_data_path',
                            default=DATA_PATH,  type=str, help='Directory where the training files are located')
    
    data_parse.add_argument('--random_seed',        dest='random_seed',        default=8888,
                            type=int, help='Random seed used for shuffling the list of training files')
    data_parse.add_argument('--num_cores',          dest='num_cores',
                            default=24,         type=int, help='Number of CPU cores')
    data_parse.add_argument('--train_ratio',        dest='train_ratio',        default=0.7,
                            type=float, help='Fraction of data to be used for training')
    data_parse.add_argument('--valid_ratio',        dest='valid_ratio',        default=0.15,       type=float,
                            help='Fraction of data to be used for validation (only matters when there is a third dataset to be created for testing)')
    data_parse.add_argument('--batch_size',         dest='batch_size',
                            default=32,         type=int, help='Number of examples per batch')
    data_parse.add_argument('--prefetch_size',      dest='prefetch_size',      default=1,
                            type=int, help='Number of batches to be prepared in queue')

    # Model
    model_parse = parser.add_argument_group('Model setting')
    model_parse.add_argument('--model',                dest='model',               default='DNN',
                             type=str,   help='Select the model to train e.g. DNN; note that this version only has DNN')
    model_parse.add_argument('--loss',            dest='loss',              default=40,
                             type=int,   help="Setting of loss function '10','11','12','20','21','22','30','31','32','40'")
    model_parse.add_argument('--hidden_units',    dest='hidden_units',      default=[
                             128, 64],      type=int,   nargs='+', help='List containing the number of hidden units to use for each hidden layer')
    model_parse.add_argument('--learning_rate',   dest='learning_rate',
                             default=0.001,         type=float, help='Learning rate of updating gradient')
    model_parse.add_argument('--decay_step',      dest='decay_step',
                             default=100,           type=int,   help='Decay step')
    model_parse.add_argument('--decay_rate',      dest='decay_rate',        default=0.98,
                             type=float, help='Decay rate for exponential decay of learning rate')
    model_parse.add_argument('--Lambda',          dest='Lambda',            default=0.25,
                             type=float, help='Lambda for L2,L1 regularization; alpha for focal loss')
    model_parse.add_argument('--gamma',           dest='gamma',
                             default=2.,            type=float, help='parameter for focal loss')
    model_parse.add_argument('--beta',            dest='beta',
                             default=1.,            type=float, help='Regularization parameter')
    model_parse.add_argument('--drop_rate',       dest='drop_rate',
                             default=0.5,            type=float, help='dropout rate')
    model_parse.add_argument('--embedding_units', dest='embedding_units',   default=[1, 35, 359, 3, 2], type=int, nargs='+',
                             help='List containing the number of embedding units to use for features (in order): [weekday, region, city, adexchange, slotformat]; this replaces the one hot encoding')
    model_parse.add_argument('--embedding_units_ohe', dest='embedding_units_ohe',   default=[
                             45], type=int, nargs='+', help='List containing the number of embedding units to use for OHE features (in order): usertag')

    # Training
    train_parse = parser.add_argument_group('Training hyperparameters')
    train_parse.add_argument('--has_gpu',              dest='has_gpu',
                             default=0,     type=int,   help='1 if GPU is present, else 0')
    train_parse.add_argument('--is_test',              dest='is_test',             default=0,
                             type=int,   help='1 if the trained model will be evaluated, else 0')
    train_parse.add_argument('--num_epochs_min',       dest='num_epochs_min',
                             default=100,   type=float,   help='Minimum number of training epochs')
    train_parse.add_argument('--num_epochs',           dest='num_epochs',
                             default=101,   type=float,   help='Number of total training epochs')
    train_parse.add_argument('--validation_length',    dest='validation_length',   default=100,
                             type=int,   help='In one validation, how many number of batches to use')
    train_parse.add_argument('--test_length',          dest='test_length',
                             default=100,   type=int,   help='In one test, how many number of batches to use')
    train_parse.add_argument('--earlystop_check_frequency', dest='earlystop_check_frequency',
                             default=10,     type=int,   help='earlystop_check_frequency')
    train_parse.add_argument('--earlystop_duration',       dest='earlystop_duration',
                             default=10,     type=int,   help='earlystop_duration')
    train_parse.add_argument('--valid_loss_delta',         dest='valid_loss_delta',
                             default=0.0001, type=float, help='valid_loss_delta')
    train_parse.add_argument('--num_threshold_buffer',     dest='num_threshold_buffer',
                             default=3,      type=int,   help='num_threshold_buffer')
    train_parse.add_argument('--percentile_threshold',     dest='percentile_threshold',
                             default=8,      type=int,   help='percentile_threshold')

    # Directory paths
    dir_parse = parser.add_argument_group('Directory paths')
    dir_parse.add_argument('--save_dir',         dest='save_dir',
                           default='./Outputs/',        type=str, help='Directory to save model directories')
    dir_parse.add_argument('--load_dir',         dest='load_dir',         default='latest',
                           type=str, help='Directory to load old model,default "new" as the latest model')
    dir_parse.add_argument('--store_dir',        dest='store_dir',        default='latest',
                           type=str, help='Directory to store current model, default "latest" to save in timestamp')
    dir_parse.add_argument('--result_dir',       dest='result_dir',       default='result.csv',
                           type=str, help='Directory to store (history) performance result')
    dir_parse.add_argument('--builder_save_dir', dest='builder_save_dir', default='builder_save',
                           type=str, help='Directory to store current model for tfjs predictor')

    _args, _ = parser.parse_known_args()
    return vars(_args)
def main():
    # if __name__ == "__main__":
    # n_chains = 8
    # n_iters = 3000
    # penalty = .25
    # fit_penalty = False
    # sample_obs = False
    # as_of_days_ago = 0
    # census_ts = pd.read_csv(path.join(f"~/projects/chime_sims/data/", f"PAH_ts.csv"), encoding = "latin")
    # # impute vent with the proportion of hosp.  this is a crude hack
    # census_ts.loc[census_ts.vent.isna(), "vent"] = census_ts.hosp.loc[
    #     census_ts.vent.isna()
    # ] * np.mean(census_ts.vent / census_ts.hosp)
    # # import parameters
    # params = pd.read_csv(path.join(f"/Users/crandrew/projects/chime_sims/data/", f"PAH_parameters.csv"), encoding = "latin")
    # flexible_beta = True
    # fit_penalty = True
    # y_max = None
    # figdir = f"/Users/crandrew/projects/chime_sims/output/foo/"
    # outdir = f"/Users/crandrew/projects/chime_sims/output/"
    # burn_in = 2000
    # prefix = ""
    # reopen_day = 100
    # reopen_speed = .1
    # reopen_cap = .5

    # forecast_change_prior_mean = 0
    # forecast_change_prior_sd = -99920
    # forecast_priors = dict(mu = forecast_change_prior_mean,
    #                         sig = forecast_change_prior_sd)
    # ignore_vent = True
    # else:
    p = ArgParser()
    p.add("-c", "--my-config", is_config_file=True, help="config file path")
    p.add("-P", "--prefix", help="prefix for old-style inputs")
    p.add("-p", "--parameters", help="the path to the parameters csv")
    p.add("-t", "--ts", help="the path to the time-series csv")
    p.add("-C",
          "--n_chains",
          help="number of chains to run",
          default=8,
          type=int)
    p.add(
        "-i",
        "--n_iters",
        help="number of iterations to run per chain",
        default=5000,
        type=int,
    )
    p.add(
        "-f",
        "--fit_penalty",
        action="store_true",
        help="fit the penalty based on the last week of data",
    )
    p.add(
        "--penalty",
        help="penalty factor used for shrinkage (0.05 - 1)",
        default=0.05,
        type=float,
    )
    p.add(
        "-s",
        "--sample_obs",
        action="store_true",
        help="adds noise to the values in the time-series",
    )
    p.add("-o", "--out", help="output directory")
    p.add(
        "-a",
        "--as_of",
        default=0,
        help="number of days in the past to project from",
        type=int,
    )
    p.add(
        "-b",
        "--flexible_beta",
        action="store_true",
        help="flexible, vs simple, logistic represetation of beta",
    )
    p.add("-v", "--verbose", action="store_true", help="verbose output")
    p.add("-B",
          "--burn_in",
          type=int,
          help="how much of the burn-in to discard",
          default=2000)
    p.add(
        "-d",
        "--n_days",
        help="make a census/admits plot out to n_days",
        type=int,
        action="append",
    )
    p.add("-y", "--y_max", help="max y-scale for the census graph", type=int)
    p.add(
        "-pp",
        "--plot_pairs",
        action="store_true",
        help="Plot posterior samples in a pair-plot grid",
    )
    p.add("--reopen_day",
          type=int,
          help="day at which to commence evaluating the reopen function",
          default=8675309)
    p.add("--reopen_from_today",
          action="store_true",
          help="reopen_day becomes number of days from last ts entry")
    p.add("--reopen_speed", type=float, help="how fast to reopen", default=0.1)
    p.add("--reopen_cap",
          type=float,
          help="how much reopening to allow",
          default=1.0)
    p.add("--reopen_caps",
          nargs='+',
          help="List of different reopenings to allow")
    p.add(
        "--forecast_change_prior_mean",
        type=float,
        help=
        "prior on how much the census will change over the next week, in percent",
        default=0)
    p.add(
        "--forecast_change_prior_sd",
        type=float,
        help=
        "strength of prior on how much the census will change over the next week, in percent",
        default=-9999.9)
    p.add(
        "--save_chains",
        action="store_true",
        help=
        "store the chains?  It'll make it take longer, as there is a lot of info in them.",
    )
    p.add("--save_reopening_csv",
          action="store_true",
          help="Save the reopening projections into a combined csv")
    p.add(
        "--ignore_vent",
        action="store_true",
        help="don't fit to vent, multiply the likelihood by zero",
    )
    p.add("--one_reopen",
          action="store_true",
          help="Only simulate on reopen day")

    options = p.parse_args()

    prefix = options.prefix
    n_chains = options.n_chains
    n_iters = options.n_iters
    penalty = options.penalty
    fit_penalty = options.fit_penalty
    sample_obs = options.sample_obs
    as_of_days_ago = options.as_of
    flexible_beta = options.flexible_beta
    burn_in = options.burn_in
    y_max = options.y_max
    reopen_day = options.reopen_day
    reopen_speed = options.reopen_speed
    reopen_cap = options.reopen_cap
    reopen_caps = options.reopen_caps
    forecast_priors = dict(mu=options.forecast_change_prior_mean,
                           sig=options.forecast_change_prior_sd)
    save_chains = options.save_chains
    save_reopening_csv = options.save_reopening_csv
    ignore_vent = options.ignore_vent
    one_reopen = options.one_reopen
    reopen_from_today = options.reopen_from_today

    if flexible_beta:
        print("doing flexible beta")
    else:
        print('doing logistic')

    dir = get_dir_name(options)
    print(dir)

    census_ts, params = get_inputs(options)

    if reopen_from_today:
        reopen_day = census_ts.shape[0] + reopen_day

    if census_ts is None or params is None:
        print("You must specify either --prefix or --parameters and --ts")
        print(p.format_help())
        exit(1)

    if not fit_penalty:
        assert penalty >= 0.05 and penalty < 1

    outdir = path.join(dir, "output")
    makedirs(outdir)
    figdir = path.join(dir, "figures")
    makedirs(figdir)
    paramdir = path.join(dir, "parameters")
    makedirs(paramdir)

    write_inputs(options, paramdir, census_ts, params)
    ## start here when debug
    nobs = census_ts.shape[0] - as_of_days_ago

    # expand out the spline terms and append them to params
    # also add the number of observations, as i'll need this for evaluating the knots
    # finally, estimate the scaling factors for the design matrix
    if flexible_beta == True:
        beta_spline_power = int(params.loc[params.param == "beta_spline_power",
                                           'base'])
        beta_splines = pd.DataFrame([{
            "param":
            f"beta_spline_coef_{i}",
            'base':
            0,
            "distribution":
            "norm",
            "p1":
            0,
            "p2":
            float(params.p2.loc[params.param == 'beta_spline_prior']**
                  beta_spline_power),
            'description':
            'spile term for beta'
        } for i in range(
            int(params.base.loc[params.param == "beta_spline_dimension"]))])
        nobsd = pd.DataFrame(dict(param='nobs',
                                  base=nobs,
                                  distribution="constant",
                                  p1=np.nan,
                                  p2=np.nan,
                                  description='number of observations(days)'),
                             index=[0])
        # create the design matrix in order to compute the scaling factors
        # this is critical to make the prior on design matrix flexibility invariant to the scaling of the features
        beta_k = int(params.loc[params.param == "beta_spline_dimension",
                                'base'])
        knots = np.linspace(0, nobs - nobs / beta_k / 2, beta_k)
        X = np.stack([
            power_spline(day, knots, beta_spline_power, xtrim=nobs)
            for day in range(nobs)
        ])
        Xmu = np.mean(X, axis=0)
        Xsig = np.std(X, axis=0)
        Xscale = pd.DataFrame(
            dict(param=['Xmu', 'Xsig'],
                 base=[Xmu, Xsig],
                 distribution="constant",
                 p1=[np.nan, np.nan],
                 p2=[np.nan, np.nan],
                 description=['', '']))
        params = pd.concat([params, beta_splines, nobsd, Xscale])
        # set the ununsed ones to constant
        params.loc[params.param.isin([
            'logistic_k', 'logistic_L', 'logistic_x0', 'beta_spline_power',
            'beta_spline_prior', 'beta_spline_dimension'
        ]), 'distribution'] = "constant"

    # rolling window variance
    rwstd = []
    for i in range(nobs):
        y = census_ts.hosp[:i][-7:]
        rwstd.append(np.std(y))
    census_ts["hosp_rwstd"] = np.nan
    census_ts.loc[range(nobs), "hosp_rwstd"] = rwstd

    rwstd = []
    for i in range(nobs):
        y = census_ts.vent[:i][-7:]
        rwstd.append(np.std(y))
    census_ts["vent_rwstd"] = np.nan
    census_ts.loc[range(nobs), "vent_rwstd"] = rwstd

    if sample_obs:
        fig = plt.figure()
        plt.plot(census_ts.vent, color="red")
        plt.fill_between(
            x=list(range(nobs)),
            y1=census_ts.vent + 2 * census_ts.vent_rwstd,
            y2=census_ts.vent - 2 * census_ts.vent_rwstd,
            alpha=0.3,
            lw=2,
            edgecolor="k",
        )
        plt.title("week-long rolling variance")

    if fit_penalty:
        pen_vec = np.linspace(0.05, 0.5, 10)
        tuples_for_starmap = [(n_iters, i, 7, j, params, census_ts, forecast_priors) \
                              for i in range(n_chains) for j in pen_vec]
        pool = mp.Pool(mp.cpu_count())
        shrinkage_chains = pool.starmap(get_test_loss, tuples_for_starmap)
        pool.close()
        # put together the mp results
        chain_dict = {i: [] for i in pen_vec}
        for i in range(len(tuples_for_starmap)):
            chain_dict[tuples_for_starmap[i][3]] += shrinkage_chains[i][
                burn_in:].tolist()  # get the penalty value

        mean_test_loss = [np.mean(np.array(chain_dict[i])) for i in pen_vec]

        fig = plt.figure()
        plt.plot(pen_vec, mean_test_loss)
        plt.fill_between(
            x=pen_vec,
            y1=[
                float(np.quantile(chain_dict[i][1000:], [0.025]))
                for i in pen_vec
            ],
            y2=[
                float(np.quantile(chain_dict[i][1000:], [0.975]))
                for i in pen_vec
            ],
            alpha=0.3,
            lw=2,
            edgecolor="k",
        )
        plt.xlabel("penalty factor")
        plt.ylabel("log10(test MSE)")
        # identify the best penalty
        best_penalty = pen_vec[np.argmin(mean_test_loss)]
    elif penalty < 1:
        best_penalty = penalty
    # fit the actual chains
    df = do_chains(n_iters=n_iters,
                   params=params,
                   obs=census_ts,
                   best_penalty=best_penalty,
                   sample_obs=sample_obs,
                   holdout=as_of_days_ago,
                   n_chains=n_chains,
                   forecast_priors=forecast_priors,
                   parallel=True,
                   ignore_vent=ignore_vent)
    if save_chains:
        df.to_json(path.join(f"{outdir}", "chains.json.bz2"),
                   orient="records",
                   lines=True)

    # process the output
    burn_in_df = df.loc[(df.iter <= burn_in)]
    df = df.loc[(df.iter > burn_in)]

    # do the SD plot
    SD_plot(census_ts, params, df, figdir,
            prefix if prefix is not None else "")

    ## SEIR plot
    SEIR_plot(df=df,
              first_day=census_ts[census_ts.columns[0]].values[0],
              howfar=300,
              figdir=figdir,
              prefix=prefix if prefix is not None else "",
              as_of_days_ago=as_of_days_ago,
              census_ts=census_ts)

    ## Rt plot
    Rt_plot(df=df,
            first_day=census_ts[census_ts.columns[0]].values[0],
            howfar=300,
            figdir=figdir,
            prefix=prefix if prefix is not None else "",
            params=params,
            census_ts=census_ts)

    # make predictive plot
    n_days = [30, 90, 180]
    if options.n_days:
        n_days = options.n_days

    first_day = census_ts[census_ts.columns[0]].values[0]
    for howfar in n_days:
        plt_predictive(
            df,
            first_day,
            census_ts,
            figdir,
            as_of_days_ago,
            howfar=howfar,
            prefix=prefix if prefix is not None else "",
            y_max=y_max,
            hosp_capacity=None,
            vent_capacity=None,
        )

    # reopening
    if reopen_caps is None:
        reopen_caps = [reopen_cap]
    colors = [
        'blue', 'green', 'orange', 'red', 'yellow', 'cyan', 'purple', 'brown',
        'grey'
    ]
    final_dataframe = pd.DataFrame()
    for cap in reopen_caps:
        cap = float(cap)
        if reopen_day >= 299:
            reopen_day_gap = 25
        else:
            reopen_day_gap = math.ceil((300 - reopen_day) / len(colors))
        reopen_days = np.arange(reopen_day, 299, reopen_day_gap)
        if one_reopen:
            reopen_days = [reopen_day]
        qmats = []
        for day in reopen_days:
            pool = mp.Pool(mp.cpu_count())
            reop = pool.starmap(reopen_wrapper,
                                [(df.iloc[i], day, reopen_speed, cap)
                                 for i in range(df.shape[0])])
            pool.close()
            reop = np.stack(reop)
            reopq = np.quantile(reop, [.05, .25, .5, .75, .95], axis=0)
            qmats.append(reopq)
        dates = pd.date_range(f"{first_day}", periods=301, freq="d")
        fig = plt.figure()
        for i in range(len(reopen_days)):
            plt.plot_date(dates,
                          qmats[i][2, :, 3],
                          "-",
                          label=f"re-open after {reopen_days[i]} days",
                          color=colors[i])
            plt.fill_between(x=dates,
                             y1=qmats[i][1, :, 3],
                             y2=qmats[i][3, :, 3],
                             alpha=.2,
                             color=colors[i])
            if one_reopen:
                if cap == 1:
                    tmp_sufix = f'current'
                else:
                    if cap > 1:
                        tmp_sufix = f'reduction_{math.floor((cap-1)*100)}'
                    else:
                        tmp_sufix = f'increase_{math.ceil((1-cap)*100)}'
            else:
                tmp_sufix = f'{int(cap*100)}%Reduction_{reopen_days[i]}'
            hosp_census = create_csv_export_dataframe(qmats[i], 3,
                                                      "hosp_census", dates,
                                                      301, tmp_sufix)
            vent_census = create_csv_export_dataframe(qmats[i], 5,
                                                      "vent_census", dates,
                                                      301, tmp_sufix)
            hosp_admits = create_csv_export_dataframe(qmats[i], 0,
                                                      "hosp_admits", dates,
                                                      301, tmp_sufix)
            vent_admits = create_csv_export_dataframe(qmats[i], 2,
                                                      "vent_admits", dates,
                                                      301, tmp_sufix)
            tmp_combine_census = pd.merge(hosp_census, vent_census, on="date")
            tmp_combine_admits = pd.merge(hosp_admits, vent_admits, on="date")
            tmp_combine = pd.merge(tmp_combine_census,
                                   tmp_combine_admits,
                                   on="date")
            if final_dataframe.size == 0:
                final_dataframe = tmp_combine
            else:
                final_dataframe = pd.merge(final_dataframe,
                                           tmp_combine,
                                           on="date")
    if save_reopening_csv:
        final_dataframe.to_csv(path.join(f"{outdir}", "hosp_projections.csv"))

    mk_projection_tables(df, first_day, outdir)

    toplot = df[[
        "beta",
        "hosp_prop",
        "ICU_prop",
        "vent_prop",
        "hosp_LOS",
        "ICU_LOS",
        "vent_LOS",
        "incubation_days",
        "recovery_days",
        "logistic_k",
        "logistic_x0",
        "logistic_L",
        "nu",
    ]]

    pspace = np.linspace(0.001, 0.999, 1000)

    fig, ax = plt.subplots(figsize=(8, 40), ncols=1, nrows=len(toplot.columns))
    for i in range(len(toplot.columns)):
        cname = toplot.columns[i]
        if params.loc[params.param == cname,
                      "distribution"].iloc[0] == "gamma":
            x = sps.gamma.ppf(
                pspace,
                params.loc[params.param == cname, "p1"],
                0,
                params.loc[params.param == cname, "p2"],
            )
            y = sps.gamma.pdf(
                x,
                params.loc[params.param == cname, "p1"],
                0,
                params.loc[params.param == cname, "p2"],
            )
        elif params.loc[params.param == cname,
                        "distribution"].iloc[0] == "beta":
            x = sps.beta.ppf(
                pspace,
                params.loc[params.param == cname, "p1"],
                params.loc[params.param == cname, "p2"],
            )
            y = sps.beta.pdf(
                x,
                params.loc[params.param == cname, "p1"],
                params.loc[params.param == cname, "p2"],
            )
        ax[i].plot(x, y, label="prior")
        ax[i].hist(toplot[cname], density=True, label="posterior", bins=30)
        ax[i].set_xlabel(params.loc[params.param == cname,
                                    "description"].iloc[0])
        ax[i].legend()

    if options.plot_pairs:
        #  Make a pair plot for diagnosing posterior dependence
        plt_pairplot_posteriors(toplot, figdir, prefix=prefix)

    if options.verbose:
        print(f"Output directory: {dir}")
    else:
        print(dir)
Ejemplo n.º 14
0
def main():
    """See module docstring at the top of this file"""

    parser = ArgParser(description='Make a plot of the sky to plan and evaluate tracking')
    parser.add_argument(
        '--tle-filename',
        help='filename of two-line element (TLE) target ephemeris'
    )
    parser.add_argument(
        '--time-start',
        help=('UTC start time of 24-hour window in which the first above-horizon pass will be '
            ' plotted. Many natural language date formats are supported. If not specified, the '
            'current time will be used.'
        ),
        type=str)
    parser.add_argument(
        '--axis-west-limit',
        help='western limit for the right ascension axis in degrees from the meridian',
        default=110,
        type=float)
    parser.add_argument(
        '--axis-east-limit',
        help='eastern limit for the right ascension axis in degrees from the meridian',
        default=110,
        type=float)
    custom_model_group = parser.add_argument_group(
        title='Custom Mount Model Options',
        description=('Set all of these to use a custom mount model instead of a stored model. '
            'If no GPS is connected, also set the Observer Location Options.'),
    )
    custom_model_group.add_argument(
        '--mount-pole-az',
        help='azimuth of the mount pole',
        type=float)
    custom_model_group.add_argument(
        '--mount-pole-alt',
        help='altitude of the mount pole',
        type=float)
    gps_client.add_program_arguments(
        parser=parser,
        group_description=(
            'Set all of these to indicate observer location with a custom mount model'
        ),
    )
    args = parser.parse_args()
    custom_model_args = (args.mount_pole_az, args.mount_pole_alt)
    observer_args = (args.lat, args.lon, args.elevation)

    if args.time_start is not None:
        time_start = dateutil.parser.parse(args.time_start)
        time_start = time_start.replace(tzinfo=dateutil.tz.tzutc())
    else:
        time_start = datetime.utcnow()
    time_stop = time_start + timedelta(days=1)

    if all(arg is None for arg in custom_model_args):
        if any(arg is not None for arg in observer_args):
            # pylint: disable=not-callable
            parser.error('Observer location options require the custom mount model args to be set')
        mount_model = track.model.load_stored_model(max_age=None)
    elif any(arg is None for arg in custom_model_args):
        # pylint: disable=not-callable
        parser.error("Must give all args in the custom mount model group or none of them.")
    else:
        # Generate a custom mount model from program arguments
        location = gps_client.make_location_from_args(args)
        mount_model = track.model.load_default_model(
            mount_pole_az=Longitude(args.mount_pole_az*u.deg),
            mount_pole_alt=Longitude(args.mount_pole_alt*u.deg),
            location=location,
        )

    ax = make_sky_plot()
    plot_reachable_zone(
        ax,
        mount_model,
        axis_0_west_limit=args.axis_west_limit,
        axis_0_east_limit=args.axis_east_limit
    )

    if args.tle_filename:
        print('Searching for first pass within the following time range:')
        print(time_start.isoformat() + 'Z')
        print(time_stop.isoformat() + 'Z')

        time_rise, time_set = plot_tle(
            ax,
            args.tle_filename,
            mount_model.location,
            time_start,
            time_stop
        )
        if all((time_rise, time_set)):
            plot_mount_motion(ax, time_rise, time_set)
            print('Found a pass with these start and end times:')
            print(time_rise.isoformat() + 'Z')
            print(time_set.isoformat() + 'Z')
        else:
            print('No pass was found.')

    plt.legend(
        bbox_to_anchor=(0.8, 1.4),
        loc='upper left',
        borderaxespad=0,
    )
    # Shrink current axis by 20% so the legend will fit inside the figure window because matplotlib
    # developers apparently don't use their own code
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height * 0.8])
    plt.show()