#     "longitude_fuzz": -123.882,
        #     "calc_slope": 59.5,
        #     "stand_age": 15.0
        #   }
        # }

        data = json.loads(txt)

        # default is defined in plots.py
        # default weight dict
        weight_dict = None

        ps, num_candidates = get_nearest_neighbors(
            data['plot_attrs'],
            data['tree_list'],
            data['variant'],
            weight_dict,
            k=20,
            verbose=True)

        print ".... Total Candidates: ", num_candidates
        print
        print "----- Top candidate"
        pseries = ps[0]
        #print pseries
        cond_id = pseries.name
        print "ID::", cond_id
        print "Certainty::", pseries['_certainty']
        print "NN Distance::", pseries['_kdtree_distance']
        print "NONSPEC_TPA::", pseries['NONSPEC_TPA']
        print "NONSPEC_BA::", pseries['NONSPEC_BA']
Example #2
0
def impute_nearest_neighbor(stand_results, savetime):
    # import here to avoid circular dependencies
    from trees.models import Stand, IdbSummary
    from trees.plots import get_nearest_neighbors

    # you can pass the output of impute_rasters OR a stand id

    try:
        stand_id = stand_results['stand_id']
    except TypeError:
        stand_id = int(stand_results)

    try:
        stand_qs = Stand.objects.filter(id=stand_id)
        stand = stand_qs[0]
    except (Stand.DoesNotExist, IndexError):
        exc = Exception("Cant run nearest neighbor; Stand %s does not exist." %
                        stand_id)
        raise impute_nearest_neighbor.retry(exc=exc)

    if stand.is_locked:
        if stand.cond_id != stand.locked_cond_id:
            # should never happen; fix it here
            stand_qs.filter(nn_savetime__lt=savetime).update(
                cond_id=stand.locked_cond_id, nn_savetime=savetime)
            stand.invalidate_cache()

        return {'stand_id': stand_id, 'cond_id': stand.cond_id}

    # Do we have the required attributes yet?
    if not (stand.strata and stand.elevation and stand.aspect and stand.slope
            and stand.geometry_final):
        # if not, retry it
        exc = Exception(
            "Cant run nearest neighbor; missing required attributes.")
        raise impute_nearest_neighbor.retry(exc=exc)

    # get variant code
    variant = stand.collection.variant.code

    stand_list = stand.strata.stand_list
    geom = stand.geometry_final.transform(4326, clone=True)
    site_cond = {
        'latitude_fuzz': geom.centroid[1],
        'longitude_fuzz': geom.centroid[0],
    }
    if stand.aspect:
        site_cond['calc_aspect'] = stand.aspect
    if stand.elevation:
        site_cond['elev_ft'] = stand.elevation
    if stand.slope:
        site_cond['calc_slope'] = stand.slope
    if stand.strata.search_age:
        site_cond['stand_age'] = float(stand.strata.search_age)

    weight_dict = None  # take the default defined in plots.py
    ps, num_candidates = get_nearest_neighbors(site_cond,
                                               stand_list['classes'],
                                               variant=variant,
                                               weight_dict=weight_dict,
                                               k=2)

    # Take the top match
    cond_id = int(ps[0].name)

    # TODO confirm that cond_id exists in the idbsummary and fvsaggregate tables?

    # update the database
    # stuff might have changed, we don't want a wholesale update of all fields like save()
    # use the timestamp to make sure we don't clobber a more recent request
    stand_qs.filter(nn_savetime__lt=savetime).update(cond_id=cond_id,
                                                     nn_savetime=savetime)

    stand.invalidate_cache()

    return {'stand_id': stand_id, 'cond_id': cond_id}
    stand_list = [tuple(tl.treelist) for tl in tls]

    idb = IdbSummary.objects.get(cond_id=cond)
    site_cond = {
        'calc_aspect': idb.calc_aspect,
        'elev_ft': idb.elev_ft,
        'latitude_fuzz': idb.latitude_fuzz,
        'longitude_fuzz': idb.longitude_fuzz,
        'calc_slope': idb.calc_slope,
        'stand_age': idb.stand_age
    }

    weight_dict = None

    try:
        ps, num_candidates = get_nearest_neighbors(
            site_cond, stand_list, variant, weight_dict, k=4)
    except:
        print "Error: ", site_cond
        continue

    if num_candidates < 2:
        print "DID NOT MATCH ANYTHING BUT ITSELF"
        continue

    for pseries in ps[1:4]:  # top 3
        # ['original_condition', 'variant', 'matched_condition', 'certainty']
        certainty = pseries['_certainty']
        if certainty < 0.75:  # ... that are at least 75% certain
            continue
        data = [cond, variant, pseries.name, certainty]
        fh.write(','.join([str(x) for x in data]))
Example #4
0
        #     "latitude_fuzz": 42.394,
        #     "longitude_fuzz": -123.882,
        #     "calc_slope": 59.5,
        #     "stand_age": 15.0
        #   }
        # }

        data = json.loads(txt)

        # default is defined in plots.py
        # default weight dict
        weight_dict = None

        ps, num_candidates = get_nearest_neighbors(data['plot_attrs'],
                                                   data['tree_list'],
                                                   data['variant'],
                                                   weight_dict,
                                                   k=20,
                                                   verbose=True)

        print ".... Total Candidates: ", num_candidates
        print
        print "----- Top candidate"
        pseries = ps[0]
        #print pseries
        cond_id = pseries.name
        print "ID::", cond_id
        print "Certainty::", pseries['_certainty']
        print "NN Distance::", pseries['_kdtree_distance']
        print "NONSPEC_TPA::", pseries['NONSPEC_TPA']
        print "NONSPEC_BA::", pseries['NONSPEC_BA']
        print "PLOT_BA::", pseries['PLOT_BA']
Example #5
0
    idb = IdbSummary.objects.get(cond_id=cond)
    site_cond = {
        'calc_aspect': idb.calc_aspect,
        'elev_ft': idb.elev_ft,
        'latitude_fuzz': idb.latitude_fuzz,
        'longitude_fuzz': idb.longitude_fuzz,
        'calc_slope': idb.calc_slope,
        'stand_age': idb.stand_age
    }

    weight_dict = None

    try:
        ps, num_candidates = get_nearest_neighbors(site_cond,
                                                   stand_list,
                                                   variant,
                                                   weight_dict,
                                                   k=4)
    except:
        print "Error: ", site_cond
        continue

    if num_candidates < 2:
        print "DID NOT MATCH ANYTHING BUT ITSELF"
        continue

    for pseries in ps[1:4]:  # top 3
        # ['original_condition', 'variant', 'matched_condition', 'certainty']
        certainty = pseries['_certainty']
        if certainty < 0.75:  # ... that are at least 75% certain
            continue
Example #6
0
def impute_nearest_neighbor(stand_results, savetime):
    # import here to avoid circular dependencies
    from trees.models import Stand, IdbSummary
    from trees.plots import get_nearest_neighbors

    # you can pass the output of impute_rasters OR a stand id

    try:
        stand_id = stand_results['stand_id']
    except TypeError:
        stand_id = int(stand_results)

    try:
        stand_qs = Stand.objects.filter(id=stand_id)
        stand = stand_qs[0]
    except (Stand.DoesNotExist, IndexError):
        exc = Exception("Cant run nearest neighbor; Stand %s does not exist." % stand_id)
        raise impute_nearest_neighbor.retry(exc=exc)

    # Do we have the required attributes yet?
    if not (stand.strata and stand.elevation and stand.aspect and stand.slope and stand.geometry_final):
        # if not, retry it
        exc = Exception("Cant run nearest neighbor; missing required attributes.")
        raise impute_nearest_neighbor.retry(exc=exc)

    # get variant code
    variant = stand.collection.variant.code

    stand_list = stand.strata.stand_list
    geom = stand.geometry_final.transform(4326, clone=True)
    site_cond = {
        'latitude_fuzz': geom.centroid[1],
        'longitude_fuzz': geom.centroid[0],
    }
    if stand.aspect:
        site_cond['calc_aspect'] = stand.aspect
    if stand.elevation:
        site_cond['elev_ft'] = stand.elevation
    if stand.slope:
        site_cond['calc_slope'] = stand.slope
    if stand.strata.search_age:
        site_cond['stand_age'] = float(stand.strata.search_age)

    weight_dict = None  # take the default defined in plots.py
    ps, num_candidates = get_nearest_neighbors(
        site_cond, stand_list['classes'], variant=variant,
        weight_dict=weight_dict, k=2
    )

    # Take the top match
    cond_id = int(ps[0].name)

    # just confirm that it exists
    # TODO confirm that cond_id exists in the fvsaggregate table
    IdbSummary.objects.get(cond_id=cond_id)

    # update the database
    # stuff might have changed, we dont want a wholesale update of all fields like save()
    # use the timestamp to make sure we don't clobber a more recent request
    stand_qs.filter(nn_savetime__lt=savetime).update(cond_id=cond_id, nn_savetime=savetime)

    stand.invalidate_cache()

    return {'stand_id': stand_id, 'cond_id': cond_id}