convenience function to plot a regular grid of lon lat coordinates
(spatialencoder,
bds=[-180,-90,180,90],
#degrees_per_pixel = 1,
title=None, show=True, savepath=None, plot_points=None, class_idx=None,
save_globe=True)
| 7 | import geopandas as gpd |
| 8 | |
| 9 | def plot_predictions(spatialencoder, |
| 10 | bds=[-180,-90,180,90], |
| 11 | #degrees_per_pixel = 1, |
| 12 | title=None, show=True, savepath=None, plot_points=None, class_idx=None, |
| 13 | save_globe=True): |
| 14 | """ |
| 15 | convenience function to plot a regular grid of lon lat coordinates |
| 16 | """ |
| 17 | device = spatialencoder.device |
| 18 | |
| 19 | ideal_degrees_per_pixel_lat = 180 / (bds[3] - bds[1]) |
| 20 | ideal_degrees_per_pixel_lon = 360 / (bds[2] - bds[0]) |
| 21 | |
| 22 | degrees_per_pixel = max(ideal_degrees_per_pixel_lat,ideal_degrees_per_pixel_lat) |
| 23 | num_pix_lon = int((bds[2] - bds[0]) * degrees_per_pixel) |
| 24 | num_pix_lat = int((bds[3] - bds[1]) * degrees_per_pixel) |
| 25 | lon = torch.tensor(np.linspace(bds[0], bds[2], num_pix_lon ), device=device) |
| 26 | lat = torch.tensor(np.linspace(bds[1], bds[3], num_pix_lat), device=device) |
| 27 | lons, lats = torch.meshgrid(lon, lat) |
| 28 | |
| 29 | # ij indexing to xy indexing |
| 30 | lons, lats = lons.T, lats.T |
| 31 | |
| 32 | lonlats = torch.stack([lons, lats], dim=-1).view(-1, 2) |
| 33 | |
| 34 | spatialencoder.eval() |
| 35 | if spatialencoder.regression: |
| 36 | Y = spatialencoder(lonlats) |
| 37 | else: |
| 38 | Y = torch.sigmoid(spatialencoder(lonlats)) |
| 39 | |
| 40 | if class_idx is not None: |
| 41 | Y = Y[:, class_idx].unsqueeze(-1) |
| 42 | |
| 43 | Y = Y.view(num_pix_lat, num_pix_lon, Y.size(-1)) |
| 44 | |
| 45 | # if not binary show predictions instead of probabilities |
| 46 | if not Y.size(-1) == 1: |
| 47 | y = Y.argmax(-1) |
| 48 | else: |
| 49 | y = Y |
| 50 | |
| 51 | if savepath is not None and save_globe: |
| 52 | fig = draw_globe(y, lonlats, plot_points, title) |
| 53 | os.makedirs(os.path.dirname(savepath), exist_ok=True) |
| 54 | plt.tight_layout() |
| 55 | |
| 56 | file, ext = os.path.splitext(savepath) |
| 57 | fig.savefig(file + "_globe" + ext, transparent=True, bbox_inches="tight", pad_inches=0) |
| 58 | |
| 59 | fig = draw_map(y, plot_points, title, bds=bds) |
| 60 | |
| 61 | if show or savepath is None: |
| 62 | plt.show() |
| 63 | |
| 64 | if savepath is not None: |
| 65 | os.makedirs(os.path.dirname(savepath), exist_ok=True) |
| 66 | plt.tight_layout() |
no test coverage detected