convenience function to plot a scatter plot of values at lonlats
(spatialencoder,
lonlats,
#degrees_per_pixel = 1,
title=None,
show=True,
savepath=None,
class_idx=None,
plot_kwargs={},
lonlatscrs="4326",
plot_crs="4326",
)
| 67 | fig.savefig(savepath, transparent=True, bbox_inches="tight", pad_inches=0) |
| 68 | |
| 69 | def plot_predictions_at_points(spatialencoder, |
| 70 | lonlats, |
| 71 | #degrees_per_pixel = 1, |
| 72 | title=None, |
| 73 | show=True, |
| 74 | savepath=None, |
| 75 | class_idx=None, |
| 76 | plot_kwargs={}, |
| 77 | lonlatscrs="4326", |
| 78 | plot_crs="4326", |
| 79 | ): |
| 80 | """ |
| 81 | convenience function to plot a scatter plot of values at lonlats |
| 82 | """ |
| 83 | device = spatialencoder.device |
| 84 | |
| 85 | lons, lats = lonlats[0], lonlats[1] |
| 86 | |
| 87 | # ij indexing to xy indexing |
| 88 | lons, lats = lons.T, lats.T |
| 89 | |
| 90 | lonlats = torch.Tensor(lonlats) |
| 91 | |
| 92 | with torch.no_grad(): |
| 93 | if spatialencoder.regression: |
| 94 | Y = spatialencoder(lonlats) |
| 95 | else: |
| 96 | Y = torch.sigmoid(spatialencoder(lonlats)) |
| 97 | |
| 98 | if class_idx is not None: |
| 99 | Y = Y[:, class_idx].unsqueeze(-1) |
| 100 | |
| 101 | # if not binary show predictions instead of probabilities |
| 102 | if not Y.size(-1) == 1: |
| 103 | y = Y.argmax(-1) |
| 104 | else: |
| 105 | y = Y |
| 106 | |
| 107 | # geopandas dataframe of points |
| 108 | df = pd.DataFrame(lonlats, columns=['longitude', 'latitude']) |
| 109 | gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude), crs=lonlatscrs) |
| 110 | # add predictions |
| 111 | gdf['y'] = y |
| 112 | |
| 113 | if plot_crs != lonlatscrs: |
| 114 | plot_gdf = gdf.to_crs(epsg=plot_crs) |
| 115 | else: |
| 116 | plot_gdf = gdf |
| 117 | |
| 118 | fig = scatter_plot_gdf(plot_gdf, plot_key='y', plot_kwargs=plot_kwargs, title=title) |
| 119 | |
| 120 | if show or savepath is None: |
| 121 | plt.show() |
| 122 | |
| 123 | if savepath is not None: |
| 124 | os.makedirs(os.path.dirname(savepath), exist_ok=True) |
| 125 | plt.tight_layout() |
| 126 | fig.savefig(savepath, transparent=True, bbox_inches="tight", pad_inches=0) |
no test coverage detected