Beispiel #1
0
def fit_all_case_data(num_procs=4):
    pool = multi.Pool(num_procs)
    case_counts = parse_tsv()
    results = pool.map(fit_one_case_data, list(case_counts.items()))
    for k, v in results:
        if v is not None:
            FIT_CASE_DATA[k] = v
Beispiel #2
0
def fit_all_case_data(num_procs=4):
    pool = multi.Pool(num_procs)
    print(f"Pooling with {num_procs} processors")
    case_counts = parse_tsv()
    scenario_data = load_population_data()
    age_distributions = load_distribution()
    params = []

    for region in case_counts:
        if region in scenario_data:
            params.append([
                region, case_counts[region],
                scenario_data.get(region, None),
                age_distributions[scenario_data[region]['ages']], False
            ])
    results = pool.map(fit_population, params)

    results_dict = {}
    for k, params in results:
        if params is None:
            results_dict[k] = None
        elif np.isfinite(params['logInitial']):
            results_dict[k] = params
        else:
            results_dict[k] = None

    return results_dict
Beispiel #3
0
def generate(output_json, num_procs=1):
    scenario = {}
    fit_all_case_data(num_procs)
    print("DONE")
    print(FIT_CASE_DATA)
    print(output_json)
    case_counts = parse_tsv()

    with open(SCENARIO_POPS, 'r') as fd:
        rdr = csv.reader(fd, delimiter='\t')
        hdr = next(rdr)
        idx = {
            'name': hdr.index('name'),
            'size': hdr.index('populationServed'),
            'ages': hdr.index('ageDistribution'),
            'beds': hdr.index('hospitalBeds'),
            'icus': hdr.index('ICUBeds'),
            'hemisphere': hdr.index('hemisphere')
        }

        args = ['name', 'ages', 'size', 'beds', 'icus', 'hemisphere']
        for region in rdr:
            region_name = region[idx['name']]
            entry = [region[idx[arg]] for arg in args]
            scenario[region_name] = AllParams(*entry)
            if region_name in case_counts:
                set_mitigation(case_counts[region_name], scenario[region_name])
            else:
                scenario[region_name].containment.reduction = [
                    float(x)
                    for x in scenario[region_name].containment.reduction
                ]

    with open(output_json, "w+") as fd:
        marshalJSON(scenario, fd)
Beispiel #4
0
def fit_all_case_data():
    Params = Fitter()

    case_counts = parse_tsv()
    for region, data in case_counts.items():
        fit = Params.fit(data)
        if fit:
            FIT_CASE_DATA[region] = fit
Beispiel #5
0
def generate(output_json, num_procs=1, recalculate=False):
    scenarios = []
    fit_fname = os.path.join(BASE_PATH, FIT_PARAMETERS)
    if recalculate or (not os.path.isfile(fit_fname)):
        fit_all_case_data(num_procs)
        with open(fit_fname, 'w') as fh:
            json.dump(FIT_CASE_DATA, fh)
    else:
        with open(fit_fname, 'r') as fh:
            tmp = json.load(fh)
            for k, v in tmp.items():
                FIT_CASE_DATA[k] = v

    case_counts = parse_tsv()

    with open(SCENARIO_POPS, 'r') as fd:
        rdr = csv.reader(fd, delimiter='\t')
        hdr = next(rdr)
        idx = {
            'name': hdr.index('name'),
            'size': hdr.index('populationServed'),
            'ages': hdr.index('ageDistribution'),
            'beds': hdr.index('hospitalBeds'),
            'icus': hdr.index('ICUBeds'),
            'hemisphere': hdr.index('hemisphere'),
            'srcPopulation': hdr.index('srcPopulation'),
            'srcHospitalBeds': hdr.index('srcHospitalBeds'),
            'srcICUBeds': hdr.index('srcICUBeds')
        }

        args = [
            'name', 'ages', 'size', 'beds', 'icus', 'hemisphere',
            'srcPopulation', 'srcHospitalBeds', 'srcICUBeds'
        ]
        for region in rdr:
            region_name = region[idx['name']]
            entry = [region[idx[arg]] for arg in args]
            scenario = AllParams(
                *entry, region_name if region_name in case_counts else 'None')
            if region_name in case_counts:
                set_mitigation(case_counts[region_name], scenario,
                               FIT_CASE_DATA.get(region_name, None))
            else:
                scenario.mitigation.mitigation_intervals = []

            scenarios.append(ScenarioData(scenario, region_name))

    with open(output_json, "w+") as fd:
        output = ScenarioArray(scenarios)
        output.marshalJSON(fd)
Beispiel #6
0
def generate(output_json, num_procs=1, recalculate=False):
    scenario = {}
    fit_fname = os.path.join(BASE_PATH, FIT_PARAMETERS)
    if recalculate or (not os.path.isfile(fit_fname)):
        fit_all_case_data(num_procs)
        with open(fit_fname, 'w') as fh:
            json.dump(FIT_CASE_DATA, fh)
    else:
        with open(fit_fname, 'r') as fh:
            tmp = json.load(fh)
            for k, v in tmp.items():
                FIT_CASE_DATA[k] = v

    print("DONE")
    print(FIT_CASE_DATA)
    print(output_json)
    case_counts = parse_tsv()

    with open(SCENARIO_POPS, 'r') as fd:
        rdr = csv.reader(fd, delimiter='\t')
        hdr = next(rdr)
        idx = {
            'name': hdr.index('name'),
            'size': hdr.index('populationServed'),
            'ages': hdr.index('ageDistribution'),
            'beds': hdr.index('hospitalBeds'),
            'icus': hdr.index('ICUBeds'),
            'hemisphere': hdr.index('hemisphere')
        }

        args = ['name', 'ages', 'size', 'beds', 'icus', 'hemisphere']
        for region in rdr:
            region_name = region[idx['name']]
            entry = [region[idx[arg]] for arg in args]
            scenario[region_name] = AllParams(*entry)
            if region_name in case_counts:
                set_mitigation(case_counts[region_name], scenario[region_name])
            else:
                scenario[region_name].containment.reduction = [
                    float(x)
                    for x in scenario[region_name].containment.reduction
                ]

    with open(output_json, "w+") as fd:
        marshalJSON(scenario, fd)
Beispiel #7
0
def getRegions():
    regions = parse_tsv()
    return set(regions.keys())
Beispiel #8
0
def generate(output_json, num_procs=1, recalculate=False):
    import re
    scenarios = []
    case_counts = parse_tsv()
    scenario_data = load_population_data()

    for fname in (FIT_PARAMETERS, 'fit_parameters_1stwave.json'):
        first_wave = '1stwave' in fname
        print("reading file", fname)
        fit_fname = os.path.join(BASE_PATH, fname)
        if (recalculate or
            (not os.path.isfile(fit_fname))) and (not first_wave):
            results = fit_all_case_data(num_procs)
            with open(fit_fname, 'w') as fh:
                json.dump(results, fh)
        else:
            with open(fit_fname, 'r') as fh:
                results = json.load(fh)

        for region in scenario_data:
            if region not in results or results[
                    region] is None or region.startswith('FRA-'):
                continue
            if first_wave:  # skip if a small region or not fit to case data (no 'containment_start')
                results[region]['logInitial'] = np.log(
                    results[region]['initialCases'])
                results[region]['tMax'] = '2020-08-31'
                results[region]['seroprevalence'] = 0.0
                if (re.match('[A-Z][A-Z][A-Z]-', region)
                        and results[region]['initialCases'] < 100) or (
                            'containment_start' not in results[region]):
                    continue
            elif np.isnan(results[region]['logInitial']) or np.isinf(
                    results[region]['logInitial']):
                continue

            scenario = AllParams(**scenario_data[region],
                                 tMin=results[region]['tMin'],
                                 tMax=results[region]['tMax'],
                                 cases_key=region
                                 if region in case_counts else 'None')
            if first_wave:
                scenario.mitigation.mitigation_intervals = [
                    MitigationInterval(
                        name="Intervention 1",
                        tMin=datetime.strptime(
                            results[region]['containment_start'],
                            '%Y-%m-%d').date(),
                        id=uuid4(),
                        tMax=scenario.simulation.simulation_time_range.end +
                        timedelta(1),
                        color=mitigation_colors.get("Intervention 1",
                                                    "#cccccc"),
                        mitigationValue=round(100 *
                                              results[region]['efficacy']))
                ]
            elif region in case_counts:
                set_mitigation(scenario,
                               results[region].get('mitigations', []))
            else:
                scenario.mitigation.mitigation_intervals = []
            if len(scenario.mitigation.mitigation_intervals):
                scenario.mitigation.mitigation_intervals[
                    -1].time_range.end = datetime.strptime(
                        results[region]['tMax'],
                        '%Y-%m-%d').date() + timedelta(1)
            scenario.population.seroprevalence = round(
                100 * results[region]['seroprevalence'], 2)
            scenario.population.initial_number_of_cases = int(
                round(np.exp(results[region]['logInitial'])))

            if first_wave:
                scenario_name = f"[1st wave] {region}"
            else:
                scenario_name = region

            scenarios.append(ScenarioData(scenario, scenario_name))

    with open(output_json, "w+") as fd:
        output = ScenarioArray(scenarios)
        output.marshalJSON(fd)
Beispiel #9
0
            scenarios.append(ScenarioData(scenario, scenario_name))

    with open(output_json, "w+") as fd:
        output = ScenarioArray(scenarios)
        output.marshalJSON(fd)


if __name__ == '__main__':

    generate('test.json', recalculate=False)

    from scripts.test_fitting_procedure import generate_data, check_fit
    from scripts.model import trace_ages, get_IFR
    from matplotlib import pyplot as plt

    case_counts = parse_tsv()
    scenario_data = load_population_data()
    age_distributions = load_distribution()
    # region = 'JPN-Kagawa'
    region = 'United States of America'
    # region = 'Germany'
    region = 'Switzerland'
    region = 'USA-Texas'
    age_dis = age_distributions[scenario_data[region]['ages']]
    region, p, fit_params = fit_population(
        (region, case_counts[region], scenario_data[region], age_dis, True))

    model_data = generate_data(fit_params)
    model_cases = model_data['cases'][7:] - model_data['cases'][:-7]
    model_deaths = model_data['deaths'][7:] - model_data['deaths'][:-7]
    model_time = fit_params.time[7:]