Esempio n. 1
0
def test_spatial_analogs(method):
    if method == "skezely_rizzo":
        pytest.skip("Method not implemented.")
    diss = open_dataset("SpatialAnalogs/dissimilarity")
    data = open_dataset("SpatialAnalogs/indicators")

    target = data.sel(lat=46.1875, lon=-72.1875, time=slice("1970", "1990"))
    candidates = data.sel(time=slice("1970", "1990"))

    out = xca.spatial_analogs(target, candidates, method=method)

    np.testing.assert_allclose(diss[method], out, rtol=1e-3, atol=1e-3)
Esempio n. 2
0
def test_spatial_analogs(method):
    if method in [
            "nearest_neighbor", "kldiv"
    ] and parse_version(__scipy_version__) < parse_version("1.6.0"):
        pytest.skip("Method not supported in scipy<1.6.0")

    diss = open_dataset("SpatialAnalogs/dissimilarity")
    data = open_dataset("SpatialAnalogs/indicators")

    target = data.sel(lat=46.1875, lon=-72.1875, time=slice("1970", "1990"))
    candidates = data.sel(time=slice("1970", "1990"))

    out = xca.spatial_analogs(target, candidates, method=method)
    np.testing.assert_allclose(diss[method], out, rtol=1e-3, atol=1e-3)
Esempio n. 3
0
def test_spatial_analogs_multi_index():
    # Test multi-indexes
    diss = open_dataset("SpatialAnalogs/dissimilarity")
    data = open_dataset("SpatialAnalogs/indicators")

    target = data.sel(lat=46.1875, lon=-72.1875, time=slice("1970", "1990"))
    candidates = data.sel(time=slice("1970", "1990"))

    target_stacked = target.stack(sample=["time"])
    candidates_stacked = candidates.stack(sample=["time"])

    method = "seuclidean"
    out = xca.spatial_analogs(target_stacked,
                              candidates_stacked,
                              dist_dim="sample",
                              method=method)
    np.testing.assert_allclose(diss[method], out, rtol=1e-3, atol=1e-3)

    # Check that it works as well when time dimensions don't have the same length.
    candidates = data.sel(time=slice("1970", "1991"))
    xca.spatial_analogs(target_stacked,
                        candidates_stacked,
                        dist_dim="sample",
                        method=method)
Esempio n. 4
0
def test_spatial_analogs_multidim():
    diss = open_dataset("SpatialAnalogs/dissimilarity")
    data = open_dataset("SpatialAnalogs/indicators")

    targets = data.sel(lat=slice(46, 47),
                       lon=slice(-73, -72),
                       time=slice("1970", "1990"))
    targets = targets.stack(locations=["lat", "lon"])
    candidates = data.sel(time=slice("1970", "1990"))

    out = xca.spatial_analogs(targets, candidates, method="seuclidean")
    assert out.dims == ("locations", "lat", "lon")

    np.testing.assert_array_almost_equal(
        diss.seuclidean, out.sel(locations=(46.1875, -72.1875)), 5)
    assert out.attrs["indices"] == "meantemp,totalpr"