コード例 #1
0
 def test_cube_arg(self):
     """Check that a input lazy cube will be realised before return."""
     cube = Cube(da.zeros((1, 1), chunks=(1, 1)), long_name="dummy")
     self.assertTrue(cube.has_lazy_data())
     result = inputcube_nolazy(cube)
     self.coerce_patch.assert_called_with(improver.utilities.load.load_cube,
                                          cube,
                                          no_lazy_load=True)
     self.assertFalse(cube.has_lazy_data())
     self.assertEqual(result, "return")
コード例 #2
0
def test_all():
    """Check that cubes returned using the 'name_constraint' are not lazy"""
    constraint = name_constraint(["dummy1"])
    dummy1_cube = Cube(da.zeros((1, 1), chunks=(1, 1)), long_name="dummy2")
    dummy2_cube = Cube(da.zeros((1, 1), chunks=(1, 1)), long_name="dummy1")
    assert dummy1_cube.has_lazy_data()
    assert dummy2_cube.has_lazy_data()

    res = CubeList([dummy1_cube, dummy2_cube
                    ]).extract_cube(Constraint(cube_func=constraint))
    assert res.name() == "dummy1"
    assert not res.has_lazy_data()
コード例 #3
0
def test_laziness():
    """Test that regridding is lazy when source data is lazy."""
    n_lons = 12
    n_lats = 10
    h = 4
    i = 9
    lon_bounds = (-180, 180)
    lat_bounds = (-90, 90)

    mesh = _gridlike_mesh(n_lons, n_lats)

    # Add a chunked dimension both before and after the mesh dimension.
    # The leading length 1 dimension matches the example in issue #135.
    src_data = np.arange(i * n_lats * n_lons * h).reshape([1, i, -1, h])
    src_data = da.from_array(src_data, chunks=[1, 3, 15, 2])
    src = Cube(src_data)
    mesh_coord_x, mesh_coord_y = mesh.to_MeshCoords("face")
    src.add_aux_coord(mesh_coord_x, 2)
    src.add_aux_coord(mesh_coord_y, 2)
    tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)

    rg = MeshToGridESMFRegridder(src, tgt)

    assert src.has_lazy_data()
    result = rg(src)
    assert result.has_lazy_data()
    out_chunks = result.lazy_data().chunks
    expected_chunks = ((1, ), (3, 3, 3), (10, ), (12, ), (2, 2))
    assert out_chunks == expected_chunks
    assert np.allclose(result.data.reshape([1, i, -1, h]), src_data)
def test_laziness():
    """Test that regridding is lazy when source data is lazy."""
    n_lons = 12
    n_lats = 10
    h = 4
    lon_bounds = (-180, 180)
    lat_bounds = (-90, 90)

    mesh = _gridlike_mesh(n_lons, n_lats)

    src_data = np.arange(n_lats * n_lons * h).reshape([n_lats, n_lons, h])
    src_data = da.from_array(src_data, chunks=[3, 5, 2])
    src = Cube(src_data)
    grid = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)
    src.add_dim_coord(grid.coord("latitude"), 0)
    src.add_dim_coord(grid.coord("longitude"), 1)

    mesh_coord_x, mesh_coord_y = mesh.to_MeshCoords("face")
    tgt_data = np.zeros([n_lats * n_lons])
    tgt = Cube(tgt_data)
    tgt.add_aux_coord(mesh_coord_x, 0)
    tgt.add_aux_coord(mesh_coord_y, 0)

    rg = GridToMeshESMFRegridder(src, tgt)

    assert src.has_lazy_data()
    result = rg(src)
    assert result.has_lazy_data()
    out_chunks = result.lazy_data().chunks
    expected_chunks = ((120, ), (2, 2))
    assert out_chunks == expected_chunks
    assert np.allclose(result.data, src_data.reshape([-1, h]))
コード例 #5
0
 def test_multi(self):
     real_data = np.arange(3.0)
     cube_base = Cube(as_lazy_data(real_data))
     cube_inner = cube_base + 1
     result_a = cube_base + 1
     result_b = cube_inner + 1
     co_realise_cubes(result_a, result_b)
     # Check that target cubes were realised.
     self.assertFalse(result_a.has_lazy_data())
     self.assertFalse(result_b.has_lazy_data())
     # Check that other cubes referenced remain lazy.
     self.assertTrue(cube_base.has_lazy_data())
     self.assertTrue(cube_inner.has_lazy_data())
コード例 #6
0
 def test_multi(self):
     real_data = np.arange(3.)
     cube_base = Cube(as_lazy_data(real_data))
     cube_inner = cube_base + 1
     result_a = cube_base + 1
     result_b = cube_inner + 1
     co_realise_cubes(result_a, result_b)
     # Check that target cubes were realised.
     self.assertFalse(result_a.has_lazy_data())
     self.assertFalse(result_b.has_lazy_data())
     # Check that other cubes referenced remain lazy.
     self.assertTrue(cube_base.has_lazy_data())
     self.assertTrue(cube_inner.has_lazy_data())
コード例 #7
0
def _math_op_common(
    cube,
    operation_function,
    new_unit,
    new_dtype=None,
    in_place=False,
    skeleton_cube=False,
):
    from iris.cube import Cube

    _assert_is_cube(cube)

    if in_place and not skeleton_cube:
        if cube.has_lazy_data():
            cube.data = operation_function(cube.lazy_data())
        else:
            try:
                operation_function(cube.data, out=cube.data)
            except TypeError:
                # Non-ufunc function
                operation_function(cube.data)
        new_cube = cube
    else:
        data = operation_function(cube.core_data())
        if skeleton_cube:
            # Simply wrap the resultant data in a cube, as no
            # cube metadata is required by the caller.
            new_cube = Cube(data)
        else:
            new_cube = cube.copy(data)

    # If the result of the operation is scalar and masked, we need to fix-up the dtype.
    if (
        new_dtype is not None
        and not new_cube.has_lazy_data()
        and new_cube.data.shape == ()
        and ma.is_masked(new_cube.data)
    ):
        new_cube.data = ma.masked_array(0, 1, dtype=new_dtype)

    _sanitise_metadata(new_cube, new_unit)

    return new_cube
def test_laziness():
    """Test that regridding is lazy when source data is lazy."""
    n_lons = 12
    n_lats = 10
    h = 4
    lon_bounds = (-180, 180)
    lat_bounds = (-90, 90)

    grid = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)
    src_data = np.arange(n_lats * n_lons * h).reshape([n_lats, n_lons, h])
    src_data = da.from_array(src_data, chunks=[3, 5, 1])
    src = Cube(src_data)
    src.add_dim_coord(grid.coord("latitude"), 0)
    src.add_dim_coord(grid.coord("longitude"), 1)
    tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)

    assert src.has_lazy_data()
    result = regrid_rectilinear_to_rectilinear(src, tgt)
    assert result.has_lazy_data()
    assert np.allclose(result.data, src_data)
コード例 #9
0
 def test_basic(self):
     real_data = np.arange(3.0)
     cube = Cube(as_lazy_data(real_data))
     co_realise_cubes(cube)
     self.assertFalse(cube.has_lazy_data())
     self.assertArrayAllClose(cube.core_data(), real_data)
コード例 #10
0
 def test_basic(self):
     real_data = np.arange(3.)
     cube = Cube(as_lazy_data(real_data))
     co_realise_cubes(cube)
     self.assertFalse(cube.has_lazy_data())
     self.assertArrayAllClose(cube.core_data(), real_data)