Esempio n. 1
0
 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)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
 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)
Esempio n. 5
0
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
Esempio n. 6
0
 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)
Esempio n. 7
0
 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)
Esempio n. 8
0
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)
Esempio n. 9
0
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)
Esempio n. 10
0
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, )
Esempio n. 11
0
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)
Esempio n. 12
0
 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)