def main(configuration, gisPath, studyId, useCache):

    # Parse the country prefix
    prefix = cl.get_prefix(configuration)
    if prefix is None:
        sys.stderr.write(
            "Invalid country code associated with configuration file: {}\n".
            format(configuration))
        sys.exit(1)

    # Load the configuration
    cfg = cl.load_configuration(configuration)

    # Attempt to load the data from the database, if that fails, fall back to local file
    try:
        if not useCache:
            cl.query_betas(cfg["connection_string"],
                           studyId,
                           filename=BETAVALUES)
        else:
            print("Using cached calibration values for map...")

    except Exception as error:
        if not os.path.exists(BETAVALUES):
            sys.stderr.write(
                "An unrecoverable error occurred: {}\n".format(error))
            sys.exit(1)
        else:
            print(
                "Unable to refresh calibration data from database, using previous copy..."
            )

    # Proceed with creating beta map
    create_beta_map(cfg, gisPath, prefix)
def process(configuration, gisPath, prefix):
    # Load the configuration
    cfg = load_configuration(configuration)

    # Load and bin the population
    filename = "{}/{}_population.asc".format(gisPath, prefix)
    [ascHeader, population] = load(filename, "population")
    data = list(i for j in population for i in j)
    data = list(i for i in data if i != ascHeader['nodata'])
    populationBreaks = bin_data(data)

    # Get the access to treatments rate and bin if need be
    [treatments, needsBinning] = get_treatments_list(cfg, gisPath)
    if treatments == -1:
        print("Unable to load determine the treatments in the configuration.")
        exit(1)
    if needsBinning:
        data = list(i for j in treatments for i in j)
        data = list(i for i in data if i != ascHeader['nodata'])
        treatments = bin_data(data)

    # Load the PfPR data
    filename = "{}/{}_pfpr2to10.asc".format(gisPath, prefix)
    [_, pfpr] = load(filename, "PfPR")

    # Load the climate and treatment rasters
    climate = get_climate_zones(cfg, gisPath)
    treatment = get_treatments_raster(cfg, gisPath)

    # Prepare our results
    pfprRanges = {}
    zoneTreatments = {}

    # Process the data
    for row in range(0, ascHeader['nrows']):
        for col in range(0, ascHeader['ncols']):

            # Press on if there is nothing to do
            zone = climate[row][col]
            if zone == ascHeader['nodata']:
                continue

            # Note the bins
            popBin = int(get_bin(population[row][col], populationBreaks))
            treatBin = get_bin(treatment[row][col], treatments)

            # Add to the dictionary as needed
            if zone not in pfprRanges:
                pfprRanges[zone] = {}
            if popBin not in pfprRanges[zone]:
                pfprRanges[zone][popBin] = []
            if zone not in zoneTreatments:
                zoneTreatments[zone] = []

            # Add to our data sets
            pfprRanges[zone][popBin].append(pfpr[row][col])
            if treatBin not in zoneTreatments[zone]:
                zoneTreatments[zone].append(treatBin)

    return [pfprRanges, zoneTreatments, populationBreaks]
Example #3
0
def main(configuration, studyId):
    try:
        # Load the configuration, query for the list of replicates
        cfg = load_configuration(configuration)
        replicates = select(cfg["connection_string"], SELECT_REPLICATES, {'studyId':studyId})

        # Display list, prompt user
        if len(replicates) == 0:
            print("No studies returned!")
            exit(0)

        print("Studies returned: ")
        for replicate in replicates:
            print("{}\t{}\t{}\t{}".format(replicate[0], replicate[1], replicate[2], replicate[3]))

        # Prompt for replicate id
        replicateId = int(input("Replicate to retrive: "))

        # Load the data set, exit if nothing is returned
        rows = select(cfg["connection_string"], SELECT_DATASET, {'replicateId':replicateId})
        if len(rows) == 0:
            print("No data returned!")
            exit(0)
    
        # Save the replicate to disk
        filename = "{}-verification-data.csv".format(replicateId)
        print("Saving data set to: {}".format(filename))
        with open(filename, "w") as csvfile:
            for row in rows:
                line = ','.join(str(row[ndx]) for ndx in range(0, len(row)))
                csvfile.write("{}\n".format(line))

    except DatabaseError:
        sys.stderr.write("An unrecoverable database error occurred, exiting.\n")
        sys.exit(1)
Example #4
0
def main(configuration, gisPath, tolerance, step, username):
    global parameters

    # Determine the country prefix
    prefix = cl.get_prefix(configuration)
    if prefix is None:
        sys.stderr.write("Invalid country code associated with configuration file: {}\n".format(configuration))
        sys.exit(1)

    # Load the configuration, and potentially raster data
    cfg = cl.load_configuration(configuration)
    climate = cl.get_climate_zones(cfg, gisPath)
    treatment = cl.get_treatments_raster(cfg, gisPath)

    # Load the relevent raster data
    filename = os.path.join(gisPath, POPULATION_FILE.format(prefix))    
    [ascheader, population] = load_asc(filename)
    lookup = cl.load_betas(CALIBRATION)

    # Read the epsilons file in
    [_, beta] = load_asc(BETAVALUES.format(prefix))
    [_, epsilon] = load_asc(EPSILONVALUES.format(prefix))

    print ("Evaluating epsilons for {} rows, {} columns".format(ascheader['nrows'], ascheader['ncols']))

    # Scan each of the epsilons
    for row in range(0, ascheader['nrows']):
        for col in range(0, ascheader['ncols']):
            # Pass on nodata
            value = epsilon[row][col]
            if value == ascheader['nodata']: continue

            # Pass when value is less than maximum
            if value < tolerance: continue

            # Update the running list
            addBeta(lookup, step, climate[row][col], beta[row][col], population[row][col], treatment[row][col])

        # Note the progress
        progressBar(row + 1, ascheader['nrows'])

    # Check to see if we are done
    if len(parameters) == 0:
        print("Nothing to reduce!")
    else:
        writeBetas(lookup, prefix, username)
Example #5
0
def main(args):
    try:
        cfg = load_configuration(args.configuration)

        # Add the study if one was supplied
        if args.add is not None:
            studies = add_study(cfg["connection_string"], args.add)
            print(f"Study Id: {studies}")

        # Delete study
        if args.remove is not None:
            # delete
            remove_study(cfg["connection_string"], args.remove)

        # Rename study
        if args.update:
            id = args.update[0]
            sname = str(args.update[1])
            rename_studyname(cfg["connection_string"], id, sname)

        # Display the formated list of studies in the database
        if args.list:
            rows = get_studies(cfg["connection_string"])

            # Was nothing returned?
            if len(rows) == 0:
                print("No records returned")
                return

            # Display our resuts
            layout = "{!s:32} {!s:10}"
            print(layout.format("Study Name", "Study Id"))
            print("-" * 42)
            for row in rows:
                print(layout.format(*(row[1], row[0])))

    except database.DatabaseError:
        # Defer to database library for error messages
        sys.exit(1)
Example #6
0
def main(configuration, studyId, burnIn, subset):
    try:
        # Load the configuration
        cfg = load_configuration(configuration)

        # Get the studies
        print("Querying for studies...")
        studies = get_studies(cfg["connection_string"], studyId)
        if len(studies) == 0:
            print("No studies to process!")
            return

        # Let the user know what is going on
        if len(studies) == 1:
            print("Processing study...")
        else:
            print("Processing {} studies...".format(len(studies)))

        counter = 1
        for study in studies:
            # Process the replicates for this study
            replicates = get_replicates(cfg["connection_string"], study[0],
                                        study[1])
            process_frequencies(cfg["connection_string"], replicates, subset)
            process_summaries(cfg["connection_string"], replicates, burnIn)

            # Update the status for the user
            if len(studies) > 1:
                print("{} of {} studies complete".format(
                    counter, len(studies)))
                counter += 1

    except database.DatabaseError:
        sys.stderr.write(
            "An unrecoverable database error occurred, exiting.\n")
        sys.exit(1)