def test_compute_full_statistic_yr_cube(self): data = [self.cube1_yr, self.cube2_yr] stats = multi_model_statistics(data, 'full', ['mean']) expected_full_mean = np.ma.ones((4, 3, 2, 2)) expected_full_mean.mask = np.zeros((4, 3, 2, 2)) expected_full_mean.mask[2:4] = True self.assert_array_equal(stats['mean'].data, expected_full_mean)
def test_edge_case_time_not_in_middle_of_months(span): """Test case when time coords are not on 15th for monthly data. Expected behaviour: `multi_model_statistics` will set all dates to the 15th. """ time_points = range(1, 4) dates1 = [datetime(1850, i, 12, 0, 0, 0) for i in time_points] dates2 = [datetime(1850, i, 25, 0, 0, 0) for i in time_points] cubes = ( generate_cube_from_dates(dates1), generate_cube_from_dates(dates2), ) statistic = 'min' statistics = (statistic, ) result = multi_model_statistics(cubes, span, statistics) result_cube = result[statistic] time_coord = result_cube.coord('time') desired = np.array((14., 45., 73.)) np.testing.assert_array_equal(time_coord.points, desired) # input cubes are updated in-place for cube in cubes: np.testing.assert_array_equal(cube.coord('time').points, desired)
def test_edge_case_different_time_offsets(span): cubes = ( generate_cube_from_dates('monthly', '360_day', offset='days since 1888-01-01'), generate_cube_from_dates('monthly', '360_day', offset='days since 1899-01-01'), ) statistic = 'min' statistics = (statistic, ) result = multi_model_statistics(cubes, span, statistics) result_cube = result[statistic] time_coord = result_cube.coord('time') assert time_coord.units.calendar == 'gregorian' assert time_coord.units.origin == 'days since 1850-01-01' desired = np.array((14., 45., 73.)) np.testing.assert_array_equal(time_coord.points, desired) # input cubes are updated in-place for cube in cubes: np.testing.assert_array_equal(cube.coord('time').points, desired)
def test_compute_overlap_statistic_yr_cube(self): data = [self.cube4, self.cube4] stats = multi_model_statistics(products=data, statistics=['mean'], span='overlap') expected_ovlap_mean = np.ma.ones((2, 3, 2, 2)) self.assert_array_equal(stats['mean'].data, expected_ovlap_mean)
def multimodel_test(cubes, span, statistic): """Run multimodel test with some simple checks.""" statistics = [statistic] result = multi_model_statistics(cubes, span=span, statistics=statistics) assert isinstance(result, dict) assert statistic in result return result
def test_compute_full_statistic_mon_cube(self): data = [self.cube1, self.cube2] stats = multi_model_statistics(products=data, statistics=['mean'], span='full') expected_full_mean = np.ma.ones((5, 3, 2, 2)) expected_full_mean.mask = np.ones((5, 3, 2, 2)) expected_full_mean.mask[1] = False self.assert_array_equal(stats['mean'].data, expected_full_mean)
def test_compute_full_statistic_yr_cube(self): data = [self.cube4, self.cube5] stats = multi_model_statistics(products=data, statistics=['mean'], span='full') expected_full_mean = np.ma.ones((4, 3, 2, 2)) expected_full_mean.mask = np.zeros((4, 3, 2, 2)) expected_full_mean.mask[2:4] = True self.assert_array_equal(stats['mean'].data, expected_full_mean)
def test_edge_case_sub_daily_data_fail(span): """Test case when cubes with sub-daily time coords are passed.""" cube = generate_cube_from_dates('hourly') cubes = (cube, cube) statistic = 'min' statistics = (statistic, ) with pytest.raises(ValueError): _ = multi_model_statistics(cubes, span, statistics)
def test_edge_case_time_no_overlap_fail(): """Test case when time coords do not overlap using span='overlap'. Expected behaviour: `multi_model_statistics` should fail if time points are not overlapping. """ cubes = generate_cubes_with_non_overlapping_timecoords() statistic = 'min' statistics = (statistic, ) with pytest.raises(ValueError): _ = multi_model_statistics(cubes, 'overlap', statistics)
def test_edge_case_time_no_overlap_success(): """Test case when time coords do not overlap using span='full'. Expected behaviour: `multi_model_statistics` should use all available time points. """ cubes = generate_cubes_with_non_overlapping_timecoords() statistic = 'min' statistics = (statistic, ) result = multi_model_statistics(cubes, 'full', statistics) result_cube = result[statistic] assert result_cube.coord('time').shape == (6, )
def test_multimodel_statistics(frequency, span, statistics, expected): """High level test for multicube statistics function. - Should work for multiple data frequencies - Should be able to deal with multiple statistics - Should work for both span arguments - Should deal correctly with different mask options - Return type should be a dict with all requested statistics as keys """ cubes = get_cubes_for_validation_test(frequency) if isinstance(statistics, str): statistics = (statistics, ) expected = (expected, ) result = multi_model_statistics(cubes, span, statistics) assert isinstance(result, dict) assert set(result.keys()) == set(statistics) for i, statistic in enumerate(statistics): result_cube = result[statistic] expected_data = np.ma.array(expected[i], mask=False) assert_array_allclose(result_cube.data, expected_data)
def test_compute_overlap_statistic_yr_cube(self): data = [self.cube1_yr, self.cube1_yr] stats = multi_model_statistics(data, 'overlap', ['mean']) expected_ovlap_mean = np.ma.ones((2, 3, 2, 2)) self.assert_array_equal(stats['mean'].data, expected_ovlap_mean)