-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_data_distributions.py
145 lines (113 loc) · 3.53 KB
/
plot_data_distributions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Plot distributions of data"""
# import os
import sys
from pathlib import Path
import argparse
import random
import numpy as np
import seaborn as sns
import zarr
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import dask.array as da
import dask
from utils.config import load_config
from utils.logging import setup_logging
import torch
from torch.utils.data import DataLoader
from datasets import LagrangianFMIComposite
TITLES = {
"train": "a) Training data set",
"valid": "b) Validation data set",
"test": "c) Test data set",
}
def main(configpath, splits):
confpath = Path("config") / configpath
dsconf = load_config(confpath / "lagrangian_datasets.yaml")
plt.style.use("distributions.mplstyle")
torch.manual_seed(0)
random.seed(0)
np.random.seed(0)
rng = (0, 100)
n_bins = 100
batch_size = 15
hists = {}
for split in splits:
dset = LagrangianFMIComposite(split=split, **dsconf.fmi)
dloader = DataLoader(
dset,
batch_size=batch_size,
num_workers=batch_size,
shuffle=True,
pin_memory=False,
)
hist = None
bins = None
count = 0
n = 0
for batch in dloader:
inp, _, _ = batch
h_, bins = np.histogram(
inp[:, -1, :, :].numpy().ravel(), bins=n_bins, range=rng
)
count += sum(h_)
if hist is None:
hist = h_
else:
hist += h_
n += 1
hists[split] = {
"hist": hist,
"count": count,
}
# Plot histogram
_, bins = np.histogram([], bins=n_bins, range=rng)
nrows = len(splits)
ncols = 1
fig, axes = plt.subplots(
figsize=(3.5, nrows * 2.1),
nrows=nrows,
ncols=ncols,
squeeze=True,
sharex="row",
sharey="row",
)
width = bins[-1] - bins[-2]
for i, split in enumerate(splits):
axes[i].bar(
bins[:-1],
hists[split]["hist"],
width=width,
align="edge",
color="k",
edgecolor="k",
zorder=10,
)
axes[i].set_title(TITLES[split])
for ax in axes.flat:
ax.xaxis.set_major_locator(ticker.MultipleLocator(20))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(5))
ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True))
ax.xaxis.set_minor_formatter(ticker.NullFormatter())
ax.yaxis.set_major_locator(ticker.LogLocator(base=10.0))
ax.set_ylim(bottom=0)
ax.set_xlim(rng)
ax.grid(which="major", lw=0.5, color="tab:gray", ls="-", zorder=0)
ax.grid(which="minor", lw=0.5, color="tab:gray", ls="-", alpha=0.1, zorder=0)
ax.set_yscale("log")
ax.set_ylabel("Count")
ax.set_xlabel("Rain rate [mm h$^{-1}$]")
outpath = Path(args.outpath)
outpath.parents[0].mkdir(parents=True, exist_ok=True)
fig.savefig(outpath, dpi=600, bbox_inches="tight")
if __name__ == "__main__":
argparser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
argparser.add_argument("config", type=str, help="Configuration folder")
argparser.add_argument(
"splits", type=str, nargs="+", help="Dataset splits that are processed"
)
argparser.add_argument("outpath", type=str, help="Output file path")
args = argparser.parse_args()
main(args.config, args.splits)