# "Deactivate" cells not close to template points
# FIXME: This gets more and more dangerous in higher dimensions
# FIXME: Move to function
tree = BallTree(grid)
get_idx = set()
for pt in pts[idx:]:
    get_idx.add(tree.query(pt, k=1, return_distance=False)[0][0])
selected = grid[numpy.array(list(get_idx))]

#### BEGIN REFINEMENT OF RESULTS #########

if opts.refine and opts.result_file is not None:
    # FIXME: temporary -- we shouldn't need to know the spacing of the initial
    # region
    selected = numpy.array([c._center for c in amrlib.deserialize_grid_cells(opts.refine)])
    # refinement already occurred once in the setup stage
    spacing /= 2

    grid_tree = BallTree(selected)
    grid_idx = []
    # Reorder the grid points to match their weight indices
    for res in res_pts:
        dist, idx = grid_tree.query(res, k=1)
        # Stupid floating point inexactitude...
        # assert all([numpy.isclose(a, b) for a, b in zip(res, selected[idx[0][0]])])
        grid_idx.append(idx[0][0])
    selected = get_cr_from_grid(selected[grid_idx], results, cr_thr=0.9)
    print "Selected %d cells from 90% confidence region" % len(selected)
####
# "Deactivate" cells not close to template points
# FIXME: This gets more and more dangerous in higher dimensions
# FIXME: Move to function
tree = BallTree(grid)
get_idx = set()
for pt in pts[idx:]:
    get_idx.add(tree.query(pt, k=1, return_distance=False)[0][0])
selected = grid[numpy.array(list(get_idx))]

#### BEGIN REFINEMENT OF RESULTS #########

if opts.refine and opts.result_file is not None:
    # FIXME: temporary -- we shouldn't need to know the spacing of the initial
    # region
    prev_cells, spacing = amrlib.deserialize_grid_cells(opts.refine)
    selected = numpy.array([c._center for c in prev_cells])
    # refinement already occurred once in the setup stage
    #spacing /= 2
    selected = amrlib.apply_transform(selected, intr_prms, opts.distance_coordinates)

    grid_tree = BallTree(selected)
    grid_idx = []
    # Reorder the grid points to match their weight indices
    for res in res_pts:
        dist, idx = grid_tree.query(res, k=1)
        # Stupid floating point inexactitude...
        #assert all([numpy.isclose(a, b) for a, b in zip(res, selected[idx[0][0]])])
        grid_idx.append(idx[0][0])
    selected = get_cr_from_grid(selected[grid_idx], results, cr_thr=0.9)
    print "Selected %d cells from %.2f%% confidence region" % (len(selected), 0.9)
# "Deactivate" cells not close to template points
# FIXME: This gets more and more dangerous in higher dimensions
# FIXME: Move to function
tree = BallTree(grid)
get_idx = set()
for pt in pts[idx:]:
    get_idx.add(tree.query(pt, k=1, return_distance=False)[0][0])
selected = grid[numpy.array(list(get_idx))]

#### BEGIN REFINEMENT OF RESULTS #########

if opts.refine and opts.result_file is not None:
    # FIXME: temporary -- we shouldn't need to know the spacing of the initial
    # region
    prev_cells, spacing = amrlib.deserialize_grid_cells(opts.refine)
    selected = numpy.array([c._center for c in prev_cells])
    # refinement already occurred once in the setup stage
    #spacing /= 2
    selected = amrlib.apply_transform(selected, intr_prms,
                                      opts.distance_coordinates)

    grid_tree = BallTree(selected)
    grid_idx = []
    # Reorder the grid points to match their weight indices
    for res in res_pts:
        dist, idx = grid_tree.query(res, k=1)
        # Stupid floating point inexactitude...
        #assert all([numpy.isclose(a, b) for a, b in zip(res, selected[idx[0][0]])])
        grid_idx.append(idx[0][0])
    selected = get_cr_from_grid(selected[grid_idx], results, cr_thr=0.9)