def test_anomalies_preserve_metadata(period, reference, standardize=False): cube = make_map_data(number_years=2) cube.var_name = "si" cube.units = "m" metadata = copy.deepcopy(cube.metadata) result = anomalies(cube, period, reference, standardize=standardize) assert result.metadata == metadata for coord_cube, coord_res in zip(cube.coords(), result.coords()): if coord_cube.has_bounds() and coord_res.has_bounds(): assert_array_equal(coord_cube.bounds, coord_res.bounds) assert coord_cube == coord_res
def test_anomalies(period, reference, standardize=False): cube = make_map_data(number_years=2) result = anomalies(cube, period, reference, standardize=standardize) if reference is None: if period == 'full': anom = np.arange(-359.5, 360) elif period == 'day': anom = np.concatenate((np.ones(360) * -180, np.ones(360) * 180)) elif period == 'month': anom1 = np.concatenate( [np.arange(-194.5, -165) for x in range(12)]) anom2 = np.concatenate([np.arange(165.5, 195) for x in range(12)]) anom = np.concatenate((anom1, anom2)) elif period == 'season': anom = np.concatenate(( np.arange(-314.5, -255), np.arange(-224.5, -135), np.arange(-224.5, -135), np.arange(-224.5, -135), np.arange(15.5, 105), np.arange(135.5, 225), np.arange(135.5, 225), np.arange(135.5, 225), np.arange(375.5, 405), )) else: if period == 'full': anom = np.arange(-179.5, 540) elif period == 'day': anom = np.concatenate((np.zeros(360), np.ones(360) * 360)) elif period == 'month': anom1 = np.concatenate([np.arange(-14.5, 15) for x in range(12)]) anom2 = np.concatenate([np.arange(345.5, 375) for x in range(12)]) anom = np.concatenate((anom1, anom2)) elif period == 'season': anom = np.concatenate(( np.arange(-374.5, -315), np.arange(-44.5, 45), np.arange(-44.5, 45), np.arange(-44.5, 45), np.arange(-44.5, 45), np.arange(315.5, 405), np.arange(315.5, 405), np.arange(315.5, 405), np.arange(315.5, 345), )) zeros = np.zeros_like(anom) print(anom) print(result.data[0, 1, ...]) assert_array_equal(result.data, np.array([[zeros, anom], [anom, zeros]])) assert_array_equal(result.coord('time').points, cube.coord('time').points)
def test_standardized_anomalies(period, standardize=True): cube = make_map_data(number_years=2) result = anomalies(cube, period, standardize=standardize) if period == 'full': expected_anomalies = (cube.data - np.mean(cube.data, axis=2, keepdims=True)) if standardize: # NB: default behaviour for np.std is ddof=0, whereas # default behaviour for iris.analysis.STD_DEV is ddof=1 expected_stdanomalies = expected_anomalies / np.std( expected_anomalies, axis=2, keepdims=True, ddof=1) expected = np.ma.masked_invalid(expected_stdanomalies) assert_array_equal(result.data, expected) else: expected = np.ma.masked_invalid(expected_anomalies) assert_array_equal(result.data, expected)
def test_anomalies(period): cube = make_map_data(number_years=2) result = anomalies(cube, period) if period == 'full': anom = np.arange(-359.5, 360, 1) zeros = np.zeros_like(anom) assert_array_equal(result.data, np.array([[zeros, anom], [anom, zeros]])) elif period == 'day': anom = np.concatenate((np.ones(360) * -180, np.ones(360) * 180)) zeros = np.zeros_like(anom) assert_array_equal(result.data, np.array([[zeros, anom], [anom, zeros]])) elif period == 'month': anom1 = np.concatenate([np.arange(-194.5, -165) for x in range(12)]) anom2 = np.concatenate([np.arange(165.5, 195) for x in range(12)]) anom = np.concatenate((anom1, anom2)) zeros = np.zeros_like(anom) print(result.data[0, 1]) assert_array_equal(result.data, np.array([[zeros, anom], [anom, zeros]])) elif period == 'season': anom = np.concatenate(( np.arange(-314.5, -255), np.arange(-224.5, -135), np.arange(-224.5, -135), np.arange(-224.5, -135), np.arange(15.5, 105), np.arange(135.5, 225), np.arange(135.5, 225), np.arange(135.5, 225), np.arange(375.5, 405), )) zeros = np.zeros_like(anom) print(result.data[0, 1]) assert_array_equal(result.data, np.array([[zeros, anom], [anom, zeros]])) assert_array_equal(result.coord('time').points, cube.coord('time').points)