-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
161 lines (125 loc) · 6.94 KB
/
dashboard.py
File metadata and controls
161 lines (125 loc) · 6.94 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# app is here: https://share.streamlit.io/ouranosinc/info-crue-cmip6/main/dashboard.py
import streamlit as st
import holoviews as hv
from pathlib import Path
import pandas as pd
import numpy as np
import xarray as xr
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import glob
import hvplot.xarray
from matplotlib import colors
useCat=True
st.set_page_config(layout="wide")
st.title('Diagnostiques de ESPO-G6')
tab1, tab2 = st.tabs(["properties and measures", "correlogram"])
with tab1:
if useCat:
from xscen.config import CONFIG, load_config
from xscen.catalog import ProjectCatalog
load_config('configuration/paths_ESPO-G_j.yml', 'configuration/config_ESPO-G.yml', verbose=(__name__ == '__main__'), reset=True)
pcat = ProjectCatalog(CONFIG['paths']['project_catalog'])
col1, col2, col3 = st.columns([3, 1, 1])
# choose id
option_id = st.selectbox('id',sorted(pcat.search(type=['simulations','simulation']).df.id.unique()))
# choose region
option_region = st.selectbox('region', pcat.search(type=['simulations','simulation']).df.domain.unique())
#load all properties from ref, sim, scen
ref = pcat.search( processing_level=['diag-ref-prop*', 'diag_ref_prop'], domain=option_region).to_dask()
sim = pcat.search(id= option_id, processing_level='diag-sim-prop*', domain=option_region).to_dask()
scen = pcat.search(id= option_id, processing_level='diag-scen-prop*', domain=option_region).to_dask()
#get meas
meas_sim = pcat.search(id=option_id, processing_level='diag-sim-meas*', domain=option_region).to_dask()
meas_scen = pcat.search(id=option_id, processing_level='diag-scen-meas*', domain=option_region).to_dask()
# load hmap
if 'NAM' not in option_region:
hm = pcat.search(domain=option_region,id=scen.attrs['cat:id'],processing_level='diag-heatmap*').to_dask()
imp = pcat.search(domain=option_region,id=scen.attrs['cat:id'],processing_level='diag-improved*').to_dask()
# choose properties
option_var = st.selectbox('Properties',scen.data_vars)
prop_sim = sim[option_var]
prop_ref = ref[option_var]
prop_scen = scen[option_var]
meas_scen_prop = meas_scen[option_var]
meas_sim_prop = meas_sim[option_var]
#colormap
maxi_prop = max(prop_ref.max().values, prop_scen.max().values, prop_sim.max().values)
mini_prop = min(prop_ref.min().values, prop_scen.min().values, prop_sim.min().values)
maxi_meas = max(abs(meas_scen_prop).max().values, abs(meas_sim_prop).max().values)
print(prop_sim)
cmap='viridis_r' if 'standard_name' in prop_sim.attrs and prop_sim.attrs['standard_name']== 'precipitation_flux' else 'plasma'
cmap_meas ='BrBG' if 'standard_name' in prop_sim.attrs and prop_sim.attrs['standard_name']== 'precipitation_flux' else 'coolwarm'
long_name=prop_sim.attrs['long_name']
#
col1, col2, col3 = st.columns([6,3,4])
w, h = 300, 300
wb, hb = 400, 300
col1.write(hv.render(prop_ref.hvplot(title=f'REF\n{long_name}',width=600, height=616, cmap=cmap, clim=(mini_prop,maxi_prop))))
col2.write(hv.render(prop_sim.hvplot(width=w, height=h, title=f'SIM', cmap=cmap, clim=(mini_prop,maxi_prop)).opts(colorbar=False)))
col3.write(hv.render(meas_sim_prop.hvplot(width=wb, height=hb, title=f'SIM meas', cmap=cmap_meas, clim=(-maxi_meas,maxi_meas))))
col2.write(hv.render(prop_scen.hvplot(width=w, height=h, title=f'SCEN', cmap=cmap, clim=(mini_prop,maxi_prop)).opts(colorbar=False)))
col3.write(hv.render(meas_scen_prop.hvplot(width=wb, height=hb, title=f'SCEN meas', cmap=cmap_meas, clim=(-maxi_meas,maxi_meas))))
if 'NAM' not in option_region :
#plot the heat map
fig_hmap, ax = plt.subplots(figsize=(7,3))
cmap=plt.cm.RdYlGn_r
norm = colors.BoundaryNorm(np.linspace(0,1,4), cmap.N)
im = ax.imshow(hm.heatmap.values, cmap=cmap, norm=norm)
ax.set_xticks(ticks = np.arange(len(hm.properties.values)), labels=hm.properties.values, rotation=45,ha='right')
ax.set_yticks(ticks = np.arange(len(hm.datasets.values)), labels=[x.split('.')[2].split('-')[1] for x in hm.datasets.values])
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size='15%', pad=0.4)
fig_hmap.add_axes(cax)
cbar = fig_hmap.colorbar(im, cax=cax, ticks=[0, 1], orientation='horizontal')
cbar.ax.set_xticklabels(['best', 'worst'])
plt.title('Normalised mean measure of properties')
fig_hmap.tight_layout()
#plot improved
percent_better= imp.improved_grid_points.values
percent_better=np.reshape(np.array(percent_better), (1, len(percent_better)))
fig_per, ax = plt.subplots(figsize=(7, 3))
cmap=plt.cm.RdYlGn
norm = colors.BoundaryNorm(np.linspace(0,1,100), cmap.N)
im = ax.imshow(percent_better, cmap=cmap, norm=norm)
ax.set_xticks(ticks=np.arange(len(imp.properties.values)), labels= imp.properties.values, rotation=45,ha='right')
ax.set_yticks(ticks=np.arange(1), labels=[''])
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size='15%', pad=0.4)
fig_per.add_axes(cax)
cbar = fig_per.colorbar(im, cax=cax, ticks=np.arange(0,1.1,0.1), orientation='horizontal')
plt.title('Fraction of grid cells of scen that improved or stayed the same compared to sim')
fig_per.tight_layout()
col1, col2 = st.columns([1,1])
col1.write(fig_hmap)
col2.write(fig_per)
with tab2:
if useCat:
from xscen.config import CONFIG, load_config
from xscen.catalog import ProjectCatalog
load_config('paths_ESPO-G_j.yml', 'config_ESPO-G.yml', verbose=(__name__ == '__main__'), reset=True)
pcat = ProjectCatalog(CONFIG['paths']['project_catalog'])
# choose id
option_id_corr = st.selectbox('id ',sorted(pcat.search(type='simulation', processing_level=f'correlogram*').df.id.unique()))
data ={}
domains=['Haudenosaunee', 'Ute', 'Dene']
steps=['sim','scen']
for dom in domains :
data[dom]={}
data[dom]['ref'] = pcat.search(
processing_level=f'correlogram-ref',
domain=dom).to_dask(xarray_open_kwargs={'decode_timedelta': False})
for step in steps:
data[dom][step] = pcat.search(
id= option_id_corr,
domain=dom,
processing_level=f'correlogram-{step}').to_dask(
xarray_open_kwargs={'decode_timedelta':False})
for var in ['pr', 'tasmax', 'tasmin']:
cols = st.columns([2,2,2])
for i,dom in enumerate(domains):
ds = xr.Dataset()
for step in ['ref'] + steps:
#st.write((data[dom][step]))
ds[step] = data[dom][step][f'correlogram_{var}']
cols[i].write(hv.render(ds.hvplot(title=f"{var} - {dom}", width=450, height=250)))