예제 #1
0
def test_PredictionEnsemble_plot(
    hind_ds_initialized_1d,
    hist_ds_uninitialized_1d,
    reconstruction_ds_1d,
    observations_ds_1d,
    variable,
    show_members,
    x,
):
    """Test PredictionEnsemble.plot()."""
    he = HindcastEnsemble(hind_ds_initialized_1d)
    kws = {"show_members": show_members, "variable": variable, "x": x}
    he.plot(**kws)
    he = he.add_uninitialized(hist_ds_uninitialized_1d)
    he.plot(**kws)
    he = he.add_observations(reconstruction_ds_1d)
    he.plot(**kws)
    he = he.add_observations(observations_ds_1d)
    he.plot(**kws)

    if x == "time":
        pm = PerfectModelEnsemble(hind_ds_initialized_1d)
        pm.plot(**kws)
        pm = pm.add_control(hist_ds_uninitialized_1d.isel(member=0, drop=True))
        pm.plot(**kws)
def test_calendar_matching_observations(hind_ds_initialized_1d,
                                        reconstruction_ds_1d):
    """Tests that error is thrown if calendars mismatch when adding observations."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    reconstruction_ds_1d["time"] = xr.cftime_range(
        start="1950",
        periods=reconstruction_ds_1d.time.size,
        freq="MS",
        calendar="all_leap",
    )
    with pytest.raises(ValueError, match="does not match"):
        hindcast.add_observations(reconstruction_ds_1d)
예제 #3
0
def test_verify(hind_ds_initialized_1d, reconstruction_ds_1d,
                observations_ds_1d):
    """Test to see if verify can be run from the HindcastEnsemble"""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_observations(reconstruction_ds_1d,
                                         'reconstruction')
    hindcast = hindcast.add_observations(observations_ds_1d, 'observations')
    # Don't need to check for NaNs, etc. since that's handled in the prediction
    # module testing.
    hindcast.verify()  # compute over all observations
    hindcast.verify('reconstruction')  # compute over single observation
    # test all keywords
    hindcast.verify(metric='rmse', comparison='m2o')
예제 #4
0
def make_climpred_hindcast_object(hindcast, observations):
    """
    This function takes a hindcasting dataset of streamflow as well as associated
    observations and creates a hindcasting object that can be used by the
    climpred toolbox for hindcast verification.

    Parameters
    ----------
    hindcast : xarray.Dataset
      The hindcasting streamflow data for a given period
    observations : xarray.Dataset
      The streamflow observations that are used to verify the hindcasts

    Returns
    -------
    hindcast_obj : climpred.HindcastEnsemble object
      The hindcast ensemble formatted to be used in climpred.

    """

    # Todo: Add verification that the variable names are the same
    # Todo: Add verification of sizes, catch and return message.

    # Make the hindcastEnsemble object for the hindcast data
    hindcast_obj = HindcastEnsemble(hindcast)
    # Add the observations to that hindcastEnsemble object for verification.
    hindcast_obj = hindcast_obj.add_observations(observations)

    return hindcast_obj
예제 #5
0
def test_verify_single(hind_ds_initialized_1d, reconstruction_ds_1d):
    """Test to see if verify automatically works with a single observational
    product."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_observations(reconstruction_ds_1d,
                                         'reconstruction')
    hindcast.verify()
예제 #6
0
def test_inplace(hind_ds_initialized_1d, reconstruction_ds_1d,
                 hist_ds_uninitialized_1d):
    """Tests that inplace operations do not work."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    # Adding observations.
    hindcast.add_observations(reconstruction_ds_1d, 'FOSI')
    with_obs = hindcast.add_observations(reconstruction_ds_1d, 'FOSI')
    assert hindcast != with_obs
    # Adding an uninitialized ensemble.
    hindcast.add_uninitialized(hist_ds_uninitialized_1d)
    with_uninit = hindcast.add_uninitialized(hist_ds_uninitialized_1d)
    assert hindcast != with_uninit
    # Applying arbitrary func.
    hindcast.sum('init')
    summed = hindcast.sum('init')
    assert hindcast != summed
예제 #7
0
def test_isel_xarray_func(hind_ds_initialized_1d, reconstruction_ds_1d):
    """Test whether applying isel to the objects works."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_observations(reconstruction_ds_1d, 'FOSI')
    hindcast = hindcast.isel(lead=0, init=slice(0, 3)).isel(time=slice(5, 10))
    assert hindcast.get_initialized().init.size == 3
    assert hindcast.get_initialized().lead.size == 1
    assert hindcast.get_observations('FOSI').time.size == 5
예제 #8
0
def test_hindcastEnsemble_operator_different_datasets(hind_ds_initialized_1d,
                                                      observations_ds_1d):
    """Test that HindcastEnsemble math operator (+-*/) on HindcastEnsemble."""
    he = HindcastEnsemble(hind_ds_initialized_1d)
    he = he.add_observations(observations_ds_1d)
    he2 = HindcastEnsemble(hind_ds_initialized_1d)
    assert not (he2 - he).equals(he2)
    assert not (he - he2).equals(he)
예제 #9
0
def hindcast_recon_3d(hind_ds_initialized_3d, reconstruction_ds_3d):
    """HindcastEnsemble initialized with `initialized`, `reconstruction`(`recon`)."""
    hindcast = HindcastEnsemble(hind_ds_initialized_3d)
    hindcast = hindcast.add_observations(reconstruction_ds_3d)
    hindcast = hindcast - hindcast.sel(
        time=slice("1964", "2014")).mean("time").sel(
            init=slice("1964", "2014")).mean("init")
    return hindcast
예제 #10
0
def test_verify(hind_ds_initialized_1d, reconstruction_ds_1d):
    """Test to see if verify automatically works."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_observations(reconstruction_ds_1d)
    hindcast.verify(metric="acc",
                    comparison="e2o",
                    dim="init",
                    alignment="same_verif")
예제 #11
0
def test_get_observations(hind_ds_initialized_1d, reconstruction_ds_1d):
    """Tests whether get_observations method works."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_observations(reconstruction_ds_1d, 'FOSI')
    # Without name keyword.
    obs = hindcast.get_observations()
    assert obs == hindcast._datasets['observations']['FOSI']
    # With name keyword.
    obs = hindcast.get_observations('FOSI')
    assert obs == hindcast._datasets['observations']['FOSI']
예제 #12
0
def test_HindcastEnsemble_plot(
    hind_ds_initialized_1d,
    hist_ds_uninitialized_1d,
    reconstruction_ds_1d,
    observations_ds_1d,
    variable,
    show_members,
    cmap,
):
    """Test PredictionEnsemble.plot()."""
    he = HindcastEnsemble(hind_ds_initialized_1d)
    kws = {"cmap": cmap, "show_members": show_members, "variable": variable}
    he.plot(**kws)
    he = he.add_uninitialized(hist_ds_uninitialized_1d)
    he.plot(**kws)
    he = he.add_observations(reconstruction_ds_1d)
    he.plot(**kws)
    he = he.add_observations(observations_ds_1d)
    he.plot(**kws)
예제 #13
0
def hindcast_recon_1d_mm(hindcast_recon_1d_ym, reconstruction_ds_1d_mm):
    """HindcastEnsemble with initialized and reconstruction (observations) as a monthly
    observational and initialized time series (no grid)."""
    hind = hindcast_recon_1d_ym.get_initialized().sel(
        init=slice("1964", "1970"))
    del hind.coords["valid_time"]
    hind["lead"].attrs["units"] = "months"
    hindcast = HindcastEnsemble(hind)
    hindcast = hindcast.add_observations(reconstruction_ds_1d_mm)
    return hindcast
예제 #14
0
파일: conftest.py 프로젝트: kthyng/climpred
def hindcast_hist_obs_1d(hind_ds_initialized_1d, hist_ds_uninitialized_1d,
                         observations_ds_1d):
    """HindcastEnsemble initialized with `initialized`, `uninitialzed` and `obs`."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_uninitialized(hist_ds_uninitialized_1d)
    hindcast = hindcast.add_observations(observations_ds_1d)
    hindcast = hindcast - hindcast.sel(
        time=slice("1964", "2014")).mean("time").sel(
            init=slice("1964", "2014")).mean("init")
    return hindcast
예제 #15
0
def hindcast_recon_3d(hind_ds_initialized_3d, reconstruction_ds_3d):
    """HindcastEnsemble initialized with `initialized`, `reconstruction`(`recon`)."""
    # fix to align coords
    for c in ["TLAT", "TLONG", "TAREA"]:
        reconstruction_ds_3d[c] = hind_ds_initialized_3d[c]
    hindcast = HindcastEnsemble(hind_ds_initialized_3d)
    hindcast = hindcast.add_observations(reconstruction_ds_3d)
    hindcast = hindcast - hindcast.sel(time=slice("1964", "2014")).mean("time").sel(
        init=slice("1964", "2014")
    ).mean("init")
    return hindcast
예제 #16
0
def test_smooth_coarsen(reconstruction_ds_3d, hind_ds_initialized_3d):
    """Test whether coarsening reduces dim.size."""
    hindcast = HindcastEnsemble(hind_ds_initialized_3d)
    hindcast = hindcast.add_observations(reconstruction_ds_3d,
                                         'reconstruction')
    hindcast = hindcast.add_uninitialized(reconstruction_ds_3d)
    initialized_before = hindcast._datasets['initialized']
    dim = 'nlon'
    hindcast = hindcast.smooth(smooth_kws={dim: 2})
    actual_initialized = hindcast._datasets['initialized']
    assert initialized_before[dim].size // 2 == actual_initialized[dim].size
예제 #17
0
def test_smooth_goddard(reconstruction_ds_3d, hind_ds_initialized_3d):
    """Test whether goddard smoothing function reduces ntime."""
    hindcast = HindcastEnsemble(
        hind_ds_initialized_3d.isel(nlat=slice(1, None)))
    hindcast = hindcast.add_observations(
        reconstruction_ds_3d.isel(nlat=slice(1, None)), 'reconstruction')
    hindcast = hindcast.add_uninitialized(
        reconstruction_ds_3d.isel(nlat=slice(1, None)))
    initialized_before = hindcast._datasets['initialized']
    hindcast = hindcast.smooth(smooth_kws='goddard2013')
    actual_initialized = hindcast._datasets['initialized']
    dim = 'lead'
    assert actual_initialized[dim].size < initialized_before[dim].size
    for dim in ['nlon', 'nlat']:
        assert actual_initialized[dim[1:]].size < initialized_before[dim].size
예제 #18
0
def test_hindcastEnsemble_plus_hindcastEnsemble(hind_ds_initialized_1d,
                                                hist_ds_uninitialized_1d,
                                                observations_ds_1d, operator):
    """Test that HindcastEnsemble math operator (+-*/) HindcastEnsemble works
    correctly."""
    he = HindcastEnsemble(hind_ds_initialized_1d)
    he = he.add_uninitialized(hist_ds_uninitialized_1d)
    he = he.add_observations(observations_ds_1d)
    other = he.mean("init")
    operator = eval(operator)
    he2 = operator(he, other)
    for dataset in he._datasets:
        if he._datasets[dataset]:
            print("check", dataset)
            assert_equal(
                he2._datasets[dataset],
                operator(he._datasets[dataset], other._datasets[dataset]),
            )
            # check same dims and data_vars as before
            check_dataset_dims_and_data_vars(he, he2, dataset)
예제 #19
0
def hindcast_obs_1d_for_alignment(hind_ds_initialized_1d_cftime,
                                  reconstruction_ds_1d_cftime):
    """HindcastEnsemble initialized with `initialized`, `uninitialzed` and `obs`."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d_cftime)
    hindcast = hindcast.add_observations(reconstruction_ds_1d_cftime)
    return hindcast
def test_get_observations(hind_ds_initialized_1d, reconstruction_ds_1d):
    """Tests whether get_observations method works."""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_observations(reconstruction_ds_1d)
    obs = hindcast.get_observations()
    assert obs == hindcast._datasets["observations"]
예제 #21
0
def test_add_observations_da_1d(hind_ds_initialized_1d, observations_da_1d):
    """Test to see if observations can be added to the HindcastEnsemble as a da"""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_observations(observations_da_1d, 'observations')
    assert hindcast.get_observations()
예제 #22
0
def test_add_observations(hind_ds_initialized_1d, reconstruction_ds_1d):
    """Test to see if observations can be added to the HindcastEnsemble"""
    hindcast = HindcastEnsemble(hind_ds_initialized_1d)
    hindcast = hindcast.add_observations(reconstruction_ds_1d,
                                         'reconstruction')
    assert hindcast.get_observations()