Skip to content

Instantly share code, notes, and snippets.

@timothyrenner
Created December 11, 2019 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothyrenner/56a15fc5eb089c2a9ab0ee05c502bb51 to your computer and use it in GitHub Desktop.
Save timothyrenner/56a15fc5eb089c2a9ab0ee05c502bb51 to your computer and use it in GitHub Desktop.
Graph Fused Lasso + H3
from h3 import h3
from pygfl.easy import solve_gfl
def build_neighbor_edges(hexids):
# Hash the hexid to the position so we can easily look
# up where the original hexid position is in the array.
hexid_to_position = {h:ii for ii,h in enumerate(hexids)}
edges = []
for h in hexids:
for n in h3.k_ring(h,1):
if n != h and n in hexid_to_position:
edges.append((hexid_to_position[h], hexid_to_position[n]))
return np.array(edges)
def gfl_solve_h3(hexids, values):
# Edges has the positions of "from" and "to" hexids in the hexids array.
edges = build_neighbor_edges(hexids)
# gfl_solution is the fitted values, same dimension as values array.
gfl_solution = solve_gfl(values, edges)
return gfl_solution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment