Example #1
0
def scenario_4b():
    print(
        "The second set of simulations starts in 2021 and ends in 2023, while the MDA starts in 2021 and ends in 2022."
    )
    BetFilePath = 'files/InputBet_scen1.csv'
    MDAFilePath = 'files/InputMDA_scen4b.csv'
    PrevFilePath = 'files/OutputPrev_scen4b.csv'

    Trachoma_Simulation(BetFilePath=BetFilePath,
                        MDAFilePath=MDAFilePath,
                        PrevFilePath=PrevFilePath,
                        SaveOutput=False,
                        OutSimFilePath=None,
                        InSimFilePath='files/OutputVals_scen4a.p')
Example #2
0
def scenario_4a():
    print(
        "The first set of simulations starts in 2018 and ends in 2020, while the MDA starts in 2019 and ends in 2020."
    )
    BetFilePath = 'files/InputBet_scen1.csv'
    MDAFilePath = 'files/InputMDA_scen4a.csv'
    PrevFilePath = 'files/OutputPrev_scen4a.csv'

    Trachoma_Simulation(BetFilePath=BetFilePath,
                        MDAFilePath=MDAFilePath,
                        PrevFilePath=PrevFilePath,
                        SaveOutput=True,
                        OutSimFilePath='files/OutputVals_scen4a.p',
                        InSimFilePath=None)
Example #3
0
def scenario_6c():
    print(
        "For comparison purposes, the following code generates a third set of simulations starting in 2018 and ending in 2023, with MDA from 2021 to 2023."
    )
    BetFilePath = 'files/InputBet_scen1.csv'
    MDAFilePath = 'files/InputMDA_scen6c.csv'
    PrevFilePath = 'files/OutputPrev_scen6c.csv'

    Trachoma_Simulation(BetFilePath=BetFilePath,
                        MDAFilePath=MDAFilePath,
                        PrevFilePath=PrevFilePath,
                        SaveOutput=False,
                        OutSimFilePath=None,
                        InSimFilePath=None)
Example #4
0
def scenario_6b():
    print(
        "The second set of simulations starts in 2021 and ends in 2023, with MDA also from 2021 to 2023."
    )
    BetFilePath = 'files/InputBet_scen1.csv'
    MDAFilePath = 'files/InputMDA_scen6b.csv'
    PrevFilePath = 'files/OutputPrev_scen6b.csv'

    Trachoma_Simulation(BetFilePath=BetFilePath,
                        MDAFilePath=MDAFilePath,
                        PrevFilePath=PrevFilePath,
                        SaveOutput=False,
                        OutSimFilePath=None,
                        InSimFilePath='files/OutputVals_scen6a.p')
Example #5
0
def scenario_3():
    print(
        "The simulations start in 2019 and end in 2020, while the there is no MDA."
    )
    BetFilePath = 'files/InputBet_scen3.csv'
    MDAFilePath = 'files/InputMDA_scen3.csv'
    PrevFilePath = 'files/OutputPrev_scen3.csv'

    Trachoma_Simulation(BetFilePath=BetFilePath,
                        MDAFilePath=MDAFilePath,
                        PrevFilePath=PrevFilePath,
                        SaveOutput=False,
                        OutSimFilePath=None,
                        InSimFilePath=None)
    print("results in files/OutputPrev_scen3.csv")
Example #6
0
def run_trachoma(request_hash, params):

    print_function = app.logger.info if app.logger is not None else print

    disease = 'trachoma'
    file_abbrev = file_name_disease_abbreviations[disease]

    try:

        # fill in params from UI input
        iu = params['iu']
        country = iu[0:3]
        iu_id = iu[3:]
        numReps = 200 if params['runs'] > 200 else params['runs']
        MDA_Cov = params['coverage']
        mda_list = params['mdaRounds']

        print_function(
            f"running Trachoma simulation for {iu} {country} {iu_id} {numReps} {mda_list}"
        )

        # source directory info
        source_data_path_root = f"diseases/{disease}/source-data"
        source_data_gcs_path_root = f"/{bucket_name}/{source_data_path_root}"

        source_directory_path = f"{source_data_path_root}/{country}/{iu}"
        source_directory_gs_path = f"/{bucket_name}/{source_directory_path}"

        # output directory info
        output_data_path_root = f"diseases/{disease}/output-data"
        output_data_gcs_path_root = f"/{bucket_name}/{output_data_path_root}"

        output_directory_path = f"{output_data_path_root}/{country}/{iu}/{request_hash}"
        output_directory_gs_path = f"/{bucket_name}/{output_directory_path}"

        # generate Input MDA file from input
        mda_file_name = f"InputMDA-{request_hash}.csv"
        mda_file_gcs_path = f"{gs_prefix}{output_directory_gs_path}/{mda_file_name}"

        # Input MDA data
        df = pandas.DataFrame.from_records([{
            'start_sim_year': 2020,
            'end_sim_year': 2030,
            'first_mda': '',
            'last_mda': '',
            'mda_vector': json.dumps(mda_list)
        }])

        # write Input MDA to file
        df.to_csv(mda_file_gcs_path, index=None)

        # set up GCS & file paths for simulation
        bet_file_gcs_path = f"{gs_prefix}{source_directory_gs_path}/InputBet_{country}{iu_id}.csv"
        infect_file_gcs_path = f"{gs_prefix}{output_directory_gs_path}/InfectFile-{request_hash}.csv"
        in_sim_file_path = f"{source_directory_path}/OutputVals_{country}{iu_id}.p"

        output_prevalence_file_name = f"OutputPrev-{request_hash}.csv"
        output_prevalence_blob_path = f"{output_directory_gs_path}/{output_prevalence_file_name}"
        output_prevalence_gcs_path = f"{gs_prefix}{output_directory_gs_path}/{output_prevalence_file_name}"
        output_prevalence_https_path = f"{https_prefix}{output_directory_gs_path}/{output_prevalence_file_name}"

        # make a json file path to summarise it into
        summary_json_file_name = f"OutputPrev-{request_hash}-summary.json"
        summary_json_gcs_path = f"{output_directory_path}/{summary_json_file_name}"
        summary_json_https_path = f"{https_prefix}{output_directory_gs_path}/{summary_json_file_name}"

        # put together historical prevalence file paths
        historical_prevalence_filename = f"OutputPrev_{country}{iu_id}.csv"
        historical_prevalence_gcs_path = f"{gs_prefix}{source_directory_gs_path}/{historical_prevalence_filename}"
        historical_prevalence_https_path = f"{https_prefix}{source_directory_gs_path}/{historical_prevalence_filename}"

        # check for existing historical prevalence summary
        historical_prevalence_summary_filename = f"OutputPrev_{country}{iu_id}-summary.json"
        historical_prevalence_summary_blob_path = f"{source_directory_gs_path}/{historical_prevalence_summary_filename}"
        historical_prevalence_summary_gcs_path = f"{source_directory_path}/{historical_prevalence_summary_filename}"
        historical_prevalence_summary_https_path = f"{https_prefix}{source_directory_gs_path}/{historical_prevalence_summary_filename}"

        # stick it all in a dict to save to storage and send to client on success
        Result = {
            'status': True,
            'isNewSimulation': False,
            'historicalDataUrl': historical_prevalence_https_path,
            'historicalSummaryUrl': historical_prevalence_summary_https_path,
            'futureDataUrl': output_prevalence_https_path,
            'futureSummaryUrl': summary_json_https_path,
        }

        if not gcs.blob_exists(output_prevalence_blob_path):

            # we're about to kick off a new simulation
            Result['isNewSimulation'] = True

            # run the simulation
            Trachoma_Simulation(
                BetFilePath=bet_file_gcs_path,
                MDAFilePath=mda_file_gcs_path,
                PrevFilePath=output_prevalence_gcs_path,
                InfectFilePath=infect_file_gcs_path,
                InSimFilePath=in_sim_file_path,
                SaveOutput=False,
                OutSimFilePath=None,
                MDA_Cov=MDA_Cov,
                numReps=numReps,
                useCloudStorage=True,
                logger=app.logger,
                download_blob_to_file=gcs.download_blob_to_file,
            )

            generate_summary(output_prevalence_gcs_path, summary_json_gcs_path)

            # generate & write one out if it doesn't exist
            if not gcs.blob_exists(historical_prevalence_summary_blob_path):
                generate_summary(historical_prevalence_gcs_path,
                                 historical_prevalence_summary_gcs_path)

        # snag the output for sending to browser now
        output_result_json = json.dumps(Result)

        # save result to file for JS to hit next time
        ResultJsonFilePath = f"{output_directory_path}/{file_abbrev}-{iu}-{request_hash}-info.json"
        Result[
            'isNewSimulation'] = False  # because reading from static file means it's not new
        gcs.write_string_to_file(json.dumps(Result), ResultJsonFilePath)

        return Response(output_result_json,
                        mimetype='application/json; charset=UTF-8')

    except Exception as e:

        return json.dumps({'status': False, 'msg': str(e)})
Example #7
0
                    prev_csv_file_name = f"{file_name_root}-prev.csv"
                    PrevFilePath = f"{output_dir}/{prev_csv_file_name}"
                    PrevCloudPath = f"{cloud_dir}/{prev_csv_file_name}"

                    infect_csv_file_name = f"{file_name_root}-infect.csv"
                    InfectFilePath = f"{output_dir}/{infect_csv_file_name}"
                    InfectCloudPath = f"{cloud_dir}/{infect_csv_file_name}"

                    print( f"=== Running:\n\tGroup: {group}\n\tMDA_Cov {MDA_Cov}\n\tMDA type {mda_type}\n\tinput {MDAFilePath}\n\toutput {PrevFilePath}\n" )

                    # run the simulation
                    Trachoma_Simulation(
                        BetFilePath=BetFilePath,
                        MDAFilePath=MDAFilePath,
                        PrevFilePath=PrevFilePath,
                        InfectFilePath=InfectFilePath,
                        SaveOutput=False,
                        OutSimFilePath=None,
                        InSimFilePath=InSimFilePath,
                        MDA_Cov=MDA_Cov
                    )

                    # upload prevalence file
                    prev_blob = bucket.blob( PrevCloudPath )
                    prev_blob.upload_from_filename( PrevFilePath )

                    # upload infection file
                    infect_blob = bucket.blob( InfectCloudPath )
                    infect_blob.upload_from_filename( InfectFilePath )

                    # remove the input file
                    os.remove( MDAFilePath )