def kabko(request):
    kabko = request.GET.getlist('kabko')
    if kabko and len(kabko) > 0 and kabko[0]:
        return model(request, kabko[0])

    with database.get_conn() as conn, conn.cursor() as cur:
        kabko_scored = ModelDataRepo.fetch_kabko_scored(cur)

        rata_fit, rata_test = ModelDataRepo.fetch_scores_avg(cur)
        flat_fit, flat_test = ModelDataRepo.fetch_scores_flat(cur)

    rata_fit = _preprocess_scores_flat(rata_fit)
    rata_test = _preprocess_scores_flat(rata_test)
    flat_fit = _preprocess_scores_flat(flat_fit)
    flat_test = _preprocess_scores_flat(flat_test)

    data = {
        "kabko_scored": kabko_scored,
        "score_headers": score_headers_flat,
        "rata_fit": rata_fit,
        "rata_test": rata_test,
        "flat_fit": flat_fit,
        "flat_test": flat_test
    }
    return render(request, "kabko.html", data)
def model(request, kabko):
    with database.get_conn() as conn, conn.cursor() as cur:
        kabko_scored = ModelDataRepo.fetch_kabko_scored(cur)

        if kabko not in {x[0] for x in kabko_scored}:
            raise Http404

        kabko = ModelDataRepo.get_kabko_full(kabko, cur)
        fit_scores, test_scores = ModelDataRepo.fetch_scores(kabko.kabko, cur)

    mod = SeicrdRlcModel(kabko)
    params = kabko.get_params_init(extra_days=config.PREDICT_DAYS)
    model_result = mod.model(**params)

    plotter = ModelPlotter(kabko, model_result)
    datasets = [
        "infectious_all", "critical_cared", "recovered", "dead", "infected"
    ]
    compare = {
        d: mpld3.fig_to_html(plotter.plot(plotter.plot_main_data, [d]))
        for d in datasets
    }

    fit_sets, fit_scores = _preprocess_scores(fit_scores)
    test_sets, test_scores = _preprocess_scores(test_scores)

    fit_scores = fit_scores[1:]
    test_scores = test_scores[1:]
    '''
    import logging
    logger = logging.getLogger("mylogger")
    logger.info("Fit scores: " + str(len(fit_scores)))
    logger.info("Test scores: " + str(len(test_scores)))
    '''
    main = {
        "main":
        mpld3.fig_to_html(plotter.plot(plotter.plot_main)),
        "main_lite":
        mpld3.fig_to_html(plotter.plot(plotter.plot_main_lite)),
        "daily_lite":
        mpld3.fig_to_html(plotter.plot(plotter.plot_daily_lite)),
        "mortality_rate":
        mpld3.fig_to_html(plotter.plot(plotter.plot_mortality_rate)),
        "over":
        mpld3.fig_to_html(plotter.plot(plotter.plot_over)),
        #"healthcare": plotter.plot(plotter.plot_healthcare)
    }

    data = {
        "kabko": kabko,
        "kabko_scored": kabko_scored,
        "main_plots": main,
        "compare_plots": compare,
        "fit_sets": fit_sets,
        "test_sets": test_sets,
        "fit_scores": fit_scores,
        "test_scores": test_scores
    }

    return render(request, "grafik.html", data)
예제 #3
0
def clear_job():
    try_remove_job('timed_job')
    try_remove_job('daily_job')
    try_remove_job('weekly_job_1')
    try_remove_job('weekly_job_2')
    database.init()
    with database.get_conn() as conn, conn.cursor() as cur:
        cur.execute("DELETE FROM " + TABLE_NAME)
        conn.commit()
def update_map_all(any=True,
                   predict_days=PREDICT_DAYS,
                   update_prediction=True,
                   update_real=False,
                   max_process_count=None,
                   max_tasks_per_child=10,
                   pool=None,
                   inner_max_process_count=1,
                   inner_max_tasks_per_child=100):
    latest_tanggal = None
    with database.get_conn() as conn, conn.cursor() as cur:
        #latest_tanggal = MapDataRepo.get_latest_tanggal(cur)
        kabko = MapDataRepo.fetch_kabko_need_mapping(latest_tanggal,
                                                     any=any,
                                                     cur=cur)

    print("%s kabko needs updating" % (str(len(kabko)), ))

    if len(kabko) == 0:
        print("No kabko to update maps")
        return

    print("Caching kabko geometry")
    cache_geometry()

    print("Updating maps of %s kabko" % (str(len(kabko)), ))
    updater = None  #get_updater()

    args = [(*k, latest_tanggal, predict_days, update_prediction, update_real,
             updater, database.singleton, inner_max_process_count,
             inner_max_tasks_per_child) for k in kabko]

    if not pool and max_process_count == 1:
        for arg in args:
            update_map(*arg)
    else:
        #gc.collect()
        #pool = pool or Pool(processes=max_process_count, maxtasksperchild=max_tasks_per_child)
        pool = pool or ThreadPool(
            processes=util.min_none(len(args), max_process_count))
        try:
            output = pool.starmap(update_map, args)
            pool.close()
            pool.join()
            print("Done updating maps of %s kabko" % (len(args), ))
        except ConnectionError as ex:
            raise
        finally:
            pool.terminate()
            del pool
def fit_all(test_splits=[],
            max_process_count=1,
            max_tasks_per_child=None,
            pool=None):
    with database.get_conn() as conn, conn.cursor() as cur:
        latest_tanggal = ModelDataRepo.get_latest_tanggal(cur)
        kabko = ModelDataRepo.fetch_kabko_need_fitting(latest_tanggal, cur)

    print("Fitting %s kabko with %s test_splits and %s process" %
          (str(len(kabko)), str(test_splits), str(max_process_count)))

    if not pool and max_process_count == 1:
        '''
        with db.get_conn() as conn, conn.cursor() as cur:
            kabko_full = {k:ModelDataRepo.get_kabko_full(k, cur=cur) for k in kabko}
        '''
        args = [(k, test_splits, latest_tanggal, None, database.singleton)
                for k in kabko]
        for arg in args:
            fit(*arg)
    else:
        args = [(k, test_splits, latest_tanggal, None, database.singleton)
                for k in kabko]
        gc.collect()
        pool = pool or Pool(processes=max_process_count,
                            maxtasksperchild=max_tasks_per_child)
        #pool = pool or ThreadPool(processes=util.min_none(len(args), max_process_count))
        try:
            output = pool.starmap(fit, args)
            pool.close()
            pool.join()
        except Exception as ex:
            raise
        finally:
            pool.terminate()
            del pool
            gc.collect()
예제 #6
0
def init():
    database.init()
    #config.init_plot()
    with database.get_conn() as conn, conn.cursor() as cur:
        ModelDataRepo.init_weights(cur)