MCPcopy Create free account
hub / github.com/RolnickLab/climart / profile_errors

Function profile_errors

climart/utils/plotting.py:206–259  ·  view source on GitHub ↗
(Y_true, Y_preds, plot_profiles=200, var_name=None, data_dir: str = None,
                   error_type='mean', plot_type='scatter', set_seed=False, title="")

Source from the content-addressed store, hash-verified

204
205
206def profile_errors(Y_true, Y_preds, plot_profiles=200, var_name=None, data_dir: str = None,
207 error_type='mean', plot_type='scatter', set_seed=False, title=""):
208 coords_data = get_coordinates(data_dir)
209 lat = list(coords_data.get_index('lat'))
210 lon = list(coords_data.get_index('lon'))
211
212 total_profiles, n_levels = Y_true.shape
213
214 if set_seed: # To get the same profiles everytime
215 np.random.seed(7)
216
217 errors = np.abs(Y_true - Y_preds)
218 # print(errors.shape, Y_true.shape, total_profiles / 8192)
219
220 if plot_type.lower() == 'scatter':
221 latitude = []
222 longitude = []
223
224 for i in lat:
225 for j in lon:
226 latitude.append(i)
227 longitude.append(j)
228
229 lat_var = np.array(latitude)
230 lon_var = np.array(longitude)
231
232 n_times = int(total_profiles / 8192)
233 indices = np.arange(0, total_profiles)
234 indices_train = np.random.choice(total_profiles, total_profiles - plot_profiles, replace=False)
235 indices_rest = np.setxor1d(indices_train, indices, assume_unique=True)
236
237 lat_var = np.mean(np.vstack([np.expand_dims(lat_var, 1)] * n_times), axis=1)
238 lon_var = np.mean(np.vstack([np.expand_dims(lon_var, 1)] * n_times), axis=1)
239 lon_plot = lon_var[indices_rest]
240 lat_plot = lat_var[indices_rest]
241 errors_lev = errors[indices_rest]
242 errors_lev = einops.rearrange(np.array(errors_lev), 'p l -> l p') # p = profile dim
243 print(errors.shape, Y_true.shape) # (81920, 50) (81920, 50)
244 else:
245 errors_lev = errors.reshape(n_levels, 8192, -1) # level x spatial_dim x snapshot_dim
246 errors_lev = np.mean(errors_lev, axis=2) # mean over all snapshots
247 errors_lev = errors_lev.reshape((n_levels, len(lat), len(lon))) # reshape back to spatial grid
248 lon_plot, lat_plot = np.meshgrid(lon, lat)
249
250 if error_type.lower() == 'toa':
251 err = errors_lev[0]
252 elif error_type.lower() == 'surface':
253 err = errors_lev[-1]
254 elif error_type.lower() == 'mean':
255 err = np.mean(errors_lev, axis=0)
256
257 pp = profile_plot(lon_plot, lat_plot, err, var_name, plot_type=plot_type, title=title)
258
259 return pp
260
261
262def profile_plot(lon_plot, lat_plot, errors, var_name=None, plot_type='scatter', dpi=70, title=""):

Callers

nothing calls this directly

Calls 2

get_coordinatesFunction · 0.90
profile_plotFunction · 0.85

Tested by

no test coverage detected