(trainer, model, matplotlib=False, show=False,
N_bins = 9, savepath=None)
| 3 | import os |
| 4 | |
| 5 | def plot_longitudinal_accuracy(trainer, model, matplotlib=False, show=False, |
| 6 | N_bins = 9, savepath=None): |
| 7 | |
| 8 | if not matplotlib and not show and savepath is None: |
| 9 | print("warning plot_longitudinal_accuracy: no output will be saved, " |
| 10 | "as show and savepath are both False and None") |
| 11 | |
| 12 | outputs = trainer.predict(model, dataloaders=trainer.datamodule.test_dataloader()) |
| 13 | logits, lonlats, labels = list(zip(*outputs)) |
| 14 | |
| 15 | logits, lonlats, labels = torch.vstack(logits), torch.vstack(lonlats), torch.hstack(labels) |
| 16 | |
| 17 | #slogits, lonlats, labels = logits[:1000], lonlats[:1000], labels[:1000] |
| 18 | lats = lonlats[:,1] |
| 19 | |
| 20 | (logits.argmax(1) == labels).float().mean() |
| 21 | correct = (logits.argmax(1) == labels).float() |
| 22 | |
| 23 | bin_edges = np.linspace(-90,90, N_bins+1) |
| 24 | bin_width = np.diff(bin_edges)[0] |
| 25 | hist_correct, _ = np.histogram(lats, bins=bin_edges, weights=correct) |
| 26 | hist_total, _ = np.histogram(lats, bins=bin_edges) |
| 27 | |
| 28 | hist_accuracy = hist_correct / hist_total |
| 29 | |
| 30 | if savepath is not None: |
| 31 | os.makedirs(savepath, exist_ok=True) |
| 32 | np.savez(os.path.join(savepath, "histogram.npz"), |
| 33 | bin_width=bin_width, |
| 34 | bin_edges=bin_edges, |
| 35 | hist_correct=hist_correct, |
| 36 | hist_total=hist_total, |
| 37 | hist_accuracy=hist_accuracy) |
| 38 | |
| 39 | |
| 40 | |
| 41 | if matplotlib: |
| 42 | import matplotlib.pyplot as plt |
| 43 | fig, ax = plt.subplots() |
| 44 | ax.barh(bin_edges[:-1] + bin_width/2, hist_accuracy, height=np.diff(bin_edges) * 0.9, align='center') |
| 45 | ax.set_xlabel("accuracy") |
| 46 | ax.set_ylabel("latitude") |
| 47 | ax.set_yticks(bin_edges[:-1] + bin_width/2) |
| 48 | |
| 49 | if show: |
| 50 | plt.show() |
| 51 | |
| 52 | if savepath is not None: |
| 53 | fig.savefig(os.path.join(savepath, "barplot.pdf"), bbox_inches="tight", pad_inches=0, transparent=True) |
no test coverage detected