Skip to content

Commit

Permalink
Changes associated with the first revision (JGR) of the manuscript.
Browse files Browse the repository at this point in the history
  • Loading branch information
figlesua committed Aug 11, 2023
1 parent dc33fb9 commit 2b8c942
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 142 deletions.
19 changes: 16 additions & 3 deletions NN_Creation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,25 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import sys\n",
"from utils.setup import SetupNeuralNetworks\n",
"\n",
"argv = sys.argv[1:]\n",
"# argv = [\"-c\", \"./nn_config/rasp_etal_2018_optimized_threshold/quantile-0.59/cfg_NN_Creation_rasp_parcorr_pdf-0.59_2Ds.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_testing.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation_rasp.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation_climate_invariant.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation_rasp_tphystnd_1-5.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation_climate_invariant_tphystnd_26-30.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/220224_threshold_optimization/single/cfg_NN_Creation_rasp_tphystnd_28.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/220224_threshold_optimization/pdf/cfg_NN_Creation_rasp_parcorr_pdf-0.59_phq_24.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/220628_rasp_etal_2018_thrs-opt-dict/mse6_act-linear/cfg_NN_Creation_rasp_parcorr_thrs-opt-dict_2Ds.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/230720_hertel_etal_2020_thrs-opt-dict/single/cfg_NN_Creation_hertel_etal_2020_2Ds.yml\"]\n",
"\n",
"setup = SetupNeuralNetworks(argv)\n"
]
Expand All @@ -55,7 +66,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from neural_networks.models import generate_models\n",
Expand Down
18 changes: 15 additions & 3 deletions NN_Creation_optimized_threshold.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import sys\n",
"from utils.setup import SetupNeuralNetworks\n",
"\n",
"argv = sys.argv[1:]\n",
"# argv = [\"-c\", \"./nn_config/rasp_etal_2018_thrs-opt-dict/mse6/cfg_NN_Creation_rasp_parcorr_thrs-opt-dict_2Ds.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_testing.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation_rasp.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation_climate_invariant.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation_rasp_tphystnd_1-5.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/cfg_NN_Creation_climate_invariant_tphystnd_26-30.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/220224_threshold_optimization/single/cfg_NN_Creation_rasp_tphystnd_28.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/220224_threshold_optimization/pdf/cfg_NN_Creation_rasp_parcorr_pdf-0.59_phq_24.yml\"]\n",
"# argv = [\"-c\", \"./nn_config/230720_hertel_etal_2020_thrs-opt-dict/mse6/cfg_NN_Creation_hertel_parcorr_thrs-opt-dict_2Ds.yml\"]\n",
"\n",
"setup = SetupNeuralNetworks(argv)\n"
]
Expand All @@ -55,7 +65,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from neural_networks.models import generate_models\n",
Expand Down
79 changes: 52 additions & 27 deletions neural_networks/model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy.ma as ma
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
from ipykernel.kernelapp import IPKernelApp
def in_notebook():
return IPKernelApp.initialized()
Expand Down Expand Up @@ -95,7 +96,7 @@ def get_truth_pred(self, itime, var, nTime=False):
elif itime == 'mean' or itime == 'range':
if not nTime:
nTime = len(self.valid_gen)
print(f"Time samples: {nTime}")
# print(f"Time samples: {nTime}")
truth = np.zeros([nTime,self.ngeo,1])
pred = np.zeros([nTime,self.ngeo,1])
for iTime in range(nTime):
Expand Down Expand Up @@ -250,6 +251,7 @@ def plot_double_yz(
save=False,
diff=False,
stats=False,
values=False,
**kwargs
):
varname = var.var.value
Expand Down Expand Up @@ -295,18 +297,21 @@ def plot_double_yz(
hor_pvar = hor_psqmean - hor_pmean ** 2
hor_r2 = 1 - (hor_mse / hor_tvar)
mean_stats[iLev,:] = locals()['hor_'+stats]

return self.plot_slices(
truth,
pred,
itime,
varname=varname,
stype='yz',
save=save,
diff=diff,
stats=[False,(stats,mean_stats)][stats is not False],
**kwargs
)

if values:
return truth, pred, [False,(stats,mean_stats)][stats is not False]
else:
return self.plot_slices(
truth,
pred,
itime,
varname=varname,
stype='yz',
save=save,
diff=diff,
stats=[False,(stats,mean_stats)][stats is not False],
**kwargs
)


def plot_slices(
Expand All @@ -326,7 +331,11 @@ def plot_slices(
n_slices = [3,2][diff is False]
n_slices = [n_slices+1,n_slices][stats is False]
fig, axes = plt.subplots(1, n_slices, figsize=(12, 5))

# fig, axes = plt.subplots(1, n_slices, figsize=(10, 8))

label_size = 18 #'xx-large'
mpl.rcParams['font.size'] = 14

extend='both'
if 'vmin' in kwargs.keys() and 'vmax' in kwargs.keys():
vmin, vmax = kwargs['vmin'], kwargs['vmax']
Expand Down Expand Up @@ -387,26 +396,35 @@ def plot_slices(
lat_labels = [str(int(l)) for i, l in enumerate(self.latitudes) if i %9 == 0]
axes[iSlice].set_yticks(lat_ticks)
axes[iSlice].set_yticklabels(lat_labels)
axes[iSlice].set_ylabel('Latitude')
axes[iSlice].set_ylabel('Latitude', fontsize=label_size)
lon_ticks = [int(l) for l in range(len(self.longitudes)) if l %9 ==0]
lon_labels = [str(int(l)) for i, l in enumerate(self.longitudes) if i %9 == 0]
axes[iSlice].set_xticks(lon_ticks)
axes[iSlice].set_xticklabels(lon_labels)
axes[iSlice].set_xlabel('Longitude')
axes[iSlice].set_xlabel('Longitude', fontsize=label_size)
elif stype == 'yz':
P_ticks = [int(press) for press in range(len(P)) if press %5 ==0]
P_label = [str(int(press)) for i, press in enumerate(P) if i %5 == 0]
axes[iSlice].set_yticks(P_ticks)
axes[iSlice].set_yticklabels(P_label)
axes[iSlice].set_ylabel('hPa')
lat_ticks = [int(l) for l in range(len(self.latitudes)) if l %9 ==0]
lat_labels = [str(int(l)) for i, l in enumerate(self.latitudes) if i %9 == 0]
axes[iSlice].set_ylabel('Pressure (hPa)', fontsize=label_size)

degree1 = u'\xb0'
# lat_ticks = [int(l) for l in range(len(self.latitudes)) if l %9 ==0]
# # lat_labels = [str(int(l)) for i, l in enumerate(self.latitudes) if i %9 == 0]
# lat_labels = [str(abs(int(l)))+degree1+['S','N'][l>0] \
# for i, l in enumerate(self.latitudes) if i %9 == 0]

lat_ticks = [0, 21, 31.5, 42, 63]
lat_labels = ['90'+degree1+'S','30'+degree1+'S','EQ.',
'30'+degree1+'N','90'+degree1+'N']

axes[iSlice].set_xticks(lat_ticks)
axes[iSlice].set_xticklabels(lat_labels)
axes[iSlice].set_xlabel('Latitude')
axes[iSlice].set_xlabel('Latitudes', fontsize=label_size)

fig.suptitle(title)
plt.tight_layout()
fig.tight_layout()
if save:
Path(save).mkdir(parents=True, exist_ok=True)
fig.savefig(f"{save}/{varname}_map_time-{itime}.png")
Expand Down Expand Up @@ -654,7 +672,7 @@ def calc_mean_stats(self, psum, tsum, psqsum, tsqsum, sse, nTime):


# Statistics computation
def compute_stats(self, itime, var, nTime=False):
def compute_stats(self, itime, var, nTime=False, zm=False):
"""Compute statistics in for [lat, lon, var, lev]"""

t, p = self.get_truth_pred(itime, var, nTime=nTime)
Expand All @@ -669,11 +687,18 @@ def compute_stats(self, itime, var, nTime=False):

# Compute horizontal stats: single value per var
self.stats = {}
self.stats['hor_tsqmean'] = np.average(np.mean(true_sqmean,axis=1),weights=self.lat_weights)
self.stats['hor_tmean'] = np.average(np.mean(true_mean,axis=1),weights=self.lat_weights)
self.stats['hor_mse'] = np.average(np.mean(mse,axis=1),weights=self.lat_weights)
self.stats['hor_tvar'] = self.stats['hor_tsqmean'] - self.stats['hor_tmean'] ** 2
self.stats['hor_r2'] = 1 - (self.stats['hor_mse'] / self.stats['hor_tvar'])
if not zm:
self.stats['hor_tsqmean'] = np.average(np.mean(true_sqmean,axis=1),weights=self.lat_weights)
self.stats['hor_tmean'] = np.average(np.mean(true_mean,axis=1),weights=self.lat_weights)
self.stats['hor_mse'] = np.average(np.mean(mse,axis=1),weights=self.lat_weights)
self.stats['hor_tvar'] = self.stats['hor_tsqmean'] - self.stats['hor_tmean'] ** 2
self.stats['hor_r2'] = 1 - (self.stats['hor_mse'] / self.stats['hor_tvar'])
else:
self.stats['hor_tsqmean'] = np.mean(true_sqmean,axis=1)
self.stats['hor_tmean'] = np.mean(true_mean,axis=1)
self.stats['hor_mse'] = np.mean(mse,axis=1)
self.stats['hor_tvar'] = self.stats['hor_tsqmean'] - self.stats['hor_tmean'] ** 2
self.stats['hor_r2'] = 1 - (self.stats['hor_mse'] / self.stats['hor_tvar'])


def mean_stats(self):
Expand Down
4 changes: 2 additions & 2 deletions neural_networks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def generate_all_single_nn(setup):
output_list.append(var_name)
elif spcam_var.dimensions == 2:
var_name = spcam_var.name
output_list.append(var_name)
output_list.append(var_name)

for output in output_list:
model_description = ModelDescription(
Expand Down Expand Up @@ -396,4 +396,4 @@ def get_parents_sherpa(setup):
var_names = np.array(aggregated_results[output][pc_alpha]['var_names'])
parent_idxs = aggregated_results[output][pc_alpha]['parents'][threshold]
parents = var_names[parent_idxs]
return parents, pc_alpha, threshold
return parents, pc_alpha, threshold
Loading

0 comments on commit 2b8c942

Please sign in to comment.