Created
December 11, 2019 13:26
-
-
Save timothyrenner/56a15fc5eb089c2a9ab0ee05c502bb51 to your computer and use it in GitHub Desktop.
Graph Fused Lasso + H3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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