示例#1
0
def test_verify_no_need_for_control(PM_da_initialized_1d, PM_da_control_1d):
    """Tests that no error is thrown when no control present
    when calling verify(reference=['uninitialized'])."""
    v = "tos"
    comparison = "m2e"
    pm = PerfectModelEnsemble(PM_da_initialized_1d).load()
    # verify needs to control
    skill = pm.verify(metric="mse", comparison=comparison, dim="init")
    assert not skill[v].isnull().any()
    # control not needed for normalized metrics as normalized
    # with verif which is the verification member in PM and
    # not the control simulation.
    assert (not pm.verify(metric="nmse", comparison=comparison,
                          dim="init")[v].isnull().any())

    with pytest.raises(DatasetError) as e:
        pm.verify(metric="mse",
                  comparison=comparison,
                  dim="init",
                  reference=["persistence"])
    assert "at least one control dataset" in str(e.value)

    # unlikely case that control gets deleted after generating uninitialized
    pm = pm.add_control(PM_da_control_1d).generate_uninitialized()
    pm._datasets["control"] = {}
    assert (not pm._compute_uninitialized(
        metric="mse", comparison=comparison, dim="init")[v].isnull().any())

    assert (not pm.verify(metric="mse",
                          comparison=comparison,
                          dim="init",
                          reference=["uninitialized"])[v].isnull().any())
示例#2
0
def test_inplace(PM_ds_initialized_1d, PM_ds_control_1d):
    """Tests that inplace operations do not work."""
    pm = PerfectModelEnsemble(PM_ds_initialized_1d)
    # Adding a control.
    pm.add_control(PM_ds_control_1d)
    with_ctrl = pm.add_control(PM_ds_control_1d)
    assert pm != with_ctrl
    # Adding an uninitialized ensemble.
    pm = pm.add_control(PM_ds_control_1d)
    pm.generate_uninitialized()
    with_uninit = pm.generate_uninitialized()
    assert pm != with_uninit
    # Applying arbitrary func.
    pm.sum("init")
    summed = pm.sum("init")
    assert pm != summed
示例#3
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)
示例#4
0
def test_HindcastEnsemble_as_PerfectModelEnsemble(hindcast_recon_1d_mm):
    """Test that initialized dataset for HindcastEnsemble can also be used for
        PerfectModelEnsemble."""
    v = "SST"
    alignment = "maximize"
    hindcast = hindcast_recon_1d_mm
    assert (not hindcast.verify(
        metric="acc", comparison="e2o", dim="init",
        alignment=alignment)[v].isnull().any())

    # try PerfectModelEnsemble predictability
    init = hindcast.get_initialized()
    pm = PerfectModelEnsemble(init)

    assert (not pm.verify(metric="acc",
                          comparison="m2e",
                          dim=["member", "init"])[v].isnull().any())

    pm = pm.add_control(
        init.isel(member=0, lead=0, drop=True).rename({
            "init": "time"
        }).resample(time="1MS").interpolate("linear"))

    pm = pm.generate_uninitialized()
    assert (not pm.verify(
        metric="acc",
        comparison="m2e",
        dim=["member", "init"],
        reference=["uninitialized"],
    )[v].isnull().any())

    pm.bootstrap(iterations=2,
                 metric="acc",
                 comparison="m2e",
                 dim=["member", "init"])
示例#5
0
def perfectModelEnsemble_initialized_control_1d_dm_cftime(
        PM_ds_initialized_1d_dm_cftime, PM_ds_control_1d_dm_cftime):
    """PerfectModelEnsemble with MPI Perfect-model-framework initialized and control
    timeseries daily mean with cftime coords."""
    pm = PerfectModelEnsemble(PM_ds_initialized_1d_dm_cftime)
    pm = pm.add_control(PM_ds_control_1d_dm_cftime)
    return pm
示例#6
0
def test_calendar_matching_control(PM_ds_initialized_1d, PM_ds_control_1d):
    """Tests that error is thrown if calendars mismatch when adding observations."""
    pm = PerfectModelEnsemble(PM_ds_initialized_1d)
    PM_ds_control_1d["time"] = xr.cftime_range(
        start="1950", periods=PM_ds_control_1d.time.size, freq="MS", calendar="all_leap"
    )
    with pytest.raises(ValueError) as excinfo:
        pm = pm.add_control(PM_ds_control_1d)
    assert "does not match" in str(excinfo.value)
示例#7
0
def test_PerfectModelEnsemble_plot(PM_ds_initialized_1d, PM_ds_control_1d,
                                   variable, show_members, cmap):
    """Test PredictionEnsemble.plot()."""
    pm = PerfectModelEnsemble(PM_ds_initialized_1d)
    kws = {"cmap": cmap, "show_members": show_members, "variable": variable}
    pm.plot(**kws)
    pm = pm.add_control(PM_ds_control_1d)
    pm.plot(**kws)
    pm = pm.generate_uninitialized()
    pm.plot(**kws)
示例#8
0
def test_PerfectModelEnsemble_plus_defined(PM_ds_initialized_1d,
                                           PM_ds_control_1d, other, operator):
    """Test that PerfectModelEnsemble math operator (+-*/) other works correctly for
    allowed other types."""
    he = PerfectModelEnsemble(PM_ds_initialized_1d)
    he = he.add_control(PM_ds_control_1d)
    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))
            # check same dims and data_vars as before
            check_dataset_dims_and_data_vars(he, he2, dataset)
示例#9
0
def perfectModelEnsemble_initialized_control(PM_ds_initialized_1d,
                                             PM_ds_control_1d):
    """PerfectModelEnsemble initialized with `initialized` and `control` xr.Dataset."""
    pm = PerfectModelEnsemble(PM_ds_initialized_1d)
    pm = pm.add_control(PM_ds_control_1d)
    return pm
def test_bootstrap(pm_ds_ds1d, pm_ds_control1d):
    """Test that perfect model ensemble object can be bootstrapped"""
    pm = PerfectModelEnsemble(pm_ds_ds1d)
    pm.add_control(pm_ds_control1d)
    pm.bootstrap(bootstrap=2)
def test_compute_persistence(pm_ds_ds1d, pm_ds_control1d):
    """Test that compute persistence can be run for perfect model ensemble"""
    pm = PerfectModelEnsemble(pm_ds_ds1d)
    pm.add_control(pm_ds_control1d)
    pm.compute_persistence()
def test_compute_uninitialized(pm_ds_ds1d, pm_ds_control1d):
    """Test that compute uninitialized can be run for perfect model ensemble"""
    pm = PerfectModelEnsemble(pm_ds_ds1d)
    pm.add_control(pm_ds_control1d)
    pm.generate_uninitialized()
    pm.compute_uninitialized()
def test_compute_metric(pm_ds_ds1d, pm_ds_control1d):
    """Test that metric can be computed for perfect model ensemble"""
    pm = PerfectModelEnsemble(pm_ds_ds1d)
    pm.add_control(pm_ds_control1d)
    pm.compute_metric()
def test_generate_uninit(pm_ds_ds1d, pm_ds_control1d):
    """Test to see if uninitialized ensemble can be bootstrapped"""
    pm = PerfectModelEnsemble(pm_ds_ds1d)
    pm.add_control(pm_ds_control1d)
    pm.generate_uninitialized()
def test_add_control(pm_ds_ds1d, pm_ds_control1d):
    """Test to see if control can be added to PerfectModelEnsemble"""
    pm = PerfectModelEnsemble(pm_ds_ds1d)
    pm.add_control(pm_ds_control1d)