def test_accessor_nd_to_rgb(): ds = generate_test_dataset(dims={'y': 50, 'x': 50}) def rgb(d): return [d.C11, d.C22, d.C11 / d.C22] assert_equal(visualize.to_rgb(rgb(ds)), ds.nd.to_rgb(rgb))
def test_get_common_resolution(mode, fn): bounds = [(-10.0, 50.0, 0.0, 60.0), (-12.0, 40.0, -2.0, 52.0), (-13.0, 50.0, -3.0, 60.0), (-9.0, 51.0, 1.0, 61.0)] datasets = [generate_test_dataset(extent=ext) for ext in bounds] res = np.array([get_resolution(ds) for ds in datasets]) common_res = tuple(fn(res, axis=0)) assert_equal(get_common_resolution(datasets, mode=mode), common_res)
def test_write_read_netcdf(tmpdir): ds = generate_test_dataset() ds = assemble_complex(ds) path = str(tmpdir.join('test_dataset.nc')) to_netcdf(ds, path) ds_read = open_dataset(path) xr_assert_equal(ds, ds_read)
def test_reproject_no_hidden_effects(): src_crs = epsg4326 dst_crs = sinusoidal ds = generate_test_dataset(crs=src_crs) ds_copy = ds.copy(deep=True) projected = _reproject(ds_copy, dst_crs=dst_crs) xr_assert_identical(ds, ds_copy)
def test_alignment(tmpdir, extent, from_files): datapath = tmpdir.mkdir('data') path = tmpdir.mkdir('aligned') bounds = [ (-10.0, 50.0, 0.0, 60.0), (-12.0, 40.0, -2.0, 52.0), (-13.0, 50.0, -3.0, 60.0), (-9.0, 51.0, 1.0, 61.0) ] datasets = [generate_test_dataset(extent=ext) for ext in bounds] if extent is None: common_bounds = warp.get_common_bounds(datasets) else: common_bounds = extent files = [str(datapath.join('data_%d.nc' % i)) for i in range(len(datasets))] if from_files: for ds, f in zip(datasets, files): to_netcdf(ds, f) datasets = files warp.Alignment(extent=extent).apply(datasets, path=str(path)) aligned = [open_dataset(str(f)) for f in path.listdir()] for ds in aligned: assert_equal(warp.get_bounds(ds), common_bounds) assert_equal( warp.get_transform(ds), warp.get_transform(aligned[0]) ) xr_assert_equal(ds['x'], aligned[0]['x']) xr_assert_equal(ds['y'], aligned[0]['y'])
def test_plot_map(tmpdir): extent = [-5, -2, 2, 3] buffer = 2 ds = generate_test_dataset(extent=extent) ax = visualize.plot_map(ds, buffer=buffer, background=None, scalebar=True) # Check that extent contains full boundary with buffer buffered = shapely.affinity.scale( warp.get_geometry(ds), xfact=(buffer + 1.0), yfact=(buffer + 1.0)) xmin, ymin, xmax, ymax = buffered.bounds # For some reason, the order of x and y is reversed # in the axis extent... ax_xmin, ax_xmax, ax_ymin, ax_ymax = ax.get_extent(crs=ccrs.PlateCarree()) xtol = 0.1 * (xmax - xmin) ytol = 0.1 * (ymax - ymin) numtol = 1e-3 assert ax_xmin <= xmin + numtol assert ax_xmin > xmin - xtol assert ax_xmax >= xmax - numtol assert ax_xmax < xmax + xtol assert ax_ymin <= ymin + numtol assert ax_ymin > ymin - ytol assert ax_ymax >= ymax - numtol assert ax_ymax < ymax + ytol
def test_expand_collapse_coords(): ds = generate_test_dataset() for c in ['x', 'y']: expanded = warp._expand_var_to_xy(ds.coords[c], ds.coords) xr_assert_equal( ds.coords[c], warp._collapse_coords(expanded) )
def test_reprojection_nan_values(): src_crs = epsg4326 dst_crs = sinusoidal ds = generate_test_dataset(crs=src_crs) bounds = get_bounds(ds) proj = Reprojection(crs=dst_crs) warped = proj.apply(ds) xgrid, ygrid = np.meshgrid(warped.x, warped.y) lon, lat = rasterio.warp.transform(dst_crs, src_crs, xgrid.flatten(), ygrid.flatten()) lon = np.array(lon).reshape(xgrid.shape) lat = np.array(lat).reshape(ygrid.shape) inside_bounds = np.logical_and( np.logical_and(lon >= bounds.left, lon <= bounds.right), np.logical_and(lat >= bounds.bottom, lat <= bounds.top)) for v in warped.data_vars: if not set(warped[v].dims).issuperset({'y', 'x'}): continue dim_order = tuple(set(warped[v].dims) - {'y', 'x'}) + ('y', 'x') values = warped[v].transpose(*dim_order).values # Check that pixels strictly inside the original bounds are not NaN assert np.isnan(values[..., inside_bounds]).sum() == 0 # Pixel outside of the original bounds should be mostly NaN, # although some pixels near the edges may have values. outside_values = values[..., ~inside_bounds] assert np.isnan(outside_values).sum() / outside_values.size > 0.5
def test_get_resolution(name, kwargs): ds = generate_test_dataset(**kwargs) res = get_resolution(ds) bounds = get_bounds(ds) resx = abs(bounds.right - bounds.left) / (ncols(ds) - 1) resy = abs(bounds.bottom - bounds.top) / (nrows(ds) - 1) assert_almost_equal(res, (resx, resy))
def test_generate_covariance_matrix(): ds = generate_test_dataset(var=['VV__im', 'VV__re', 'VH__im', 'VH__re']) assemble_complex(ds, inplace=True) cov = generate_covariance_matrix(ds) xr_assert_allclose(np.abs(ds.VV)**2, cov.C11) xr_assert_allclose(np.abs(ds.VH)**2, cov.C22) xr_assert_allclose(ds.VV * np.conj(ds.VH), cov.C12)
def test_get_transform_from_variable(crs): ds = generate_test_dataset(crs=crs) snap_ds = create_snap_ds(crs=crs) assert_equal( warp._get_transform_from_metadata(ds), warp._get_transform_from_metadata(snap_ds) )
def test_collapse_coords(): ds = generate_test_dataset() for c in ds.coords: expanded = xr.concat([ds.coords[c]] * 10, dim='new') xr_assert_equal( ds.coords[c], warp._collapse_coords(expanded) )
def test_write_video(tmpdir, fname): path = str(tmpdir.join(fname)) ntime = 10 ds = generate_test_dataset(ntime=ntime) write_video(ds, path) assert os.path.isfile(path) video = imageio.mimread(path) assert len(video) == ntime
def test_apply_with_vars(): ds = generate_test_dataset() ref = ds.to_array(dim='var').mean('var') result = utils.apply(ds, lambda a: a.mean(axis=1), signature='(time,var)->(time)') xr_assert_allclose(result.transpose(*ref.nd.dims), ref.transpose(*ref.nd.dims))
def test_apply(dims): ds = generate_test_dataset() ref = ds.mean(dims) result = utils.apply(ds, np.mean, signature='({})->()'.format(",".join(dims))) xr_assert_allclose(result.transpose(*ref.nd.dims), ref.transpose(*ref.nd.dims))
def test_is_complex(): ds = assemble_complex(generate_test_dataset()) # Check Dataset assert utils.is_complex(ds) assert not utils.is_complex(disassemble_complex(ds)) # Check DataArray assert utils.is_complex(ds.C12) assert not utils.is_complex(ds.C11)
def test_assemble_complex_dataset(): # Create real dataset with real and imag part ds = generate_test_dataset(var=['a__im', 'a__re', 'b', 'c']) # Check that assembling into complex works ds_complex = assemble_complex(ds) assert_equal(set(ds_complex.data_vars), {'a', 'b', 'c'}) xr_assert_equal(ds_complex['a'].real, ds['a__re']) xr_assert_equal(ds_complex['a'].imag, ds['a__im'])
def test_plot_map_with_background(tmpdir): extent = [-10.0, 50.0, 0.0, 56.0] ds = generate_test_dataset(extent=extent) plt.figure(figsize=(8, 8)) ax = visualize.plot_map(ds, buffer=1, background='_default', imscale=1) # Need to trigger rendering to get image plt.savefig(str(tmpdir / 'figure.pdf')) assert len(ax.get_images()) == 1
def test_apply_with_vars_keep_vars(): ds = generate_test_dataset() ref = ds.mean('time') result = utils.apply(ds, lambda a: a.mean(axis=0), signature='(time,var)->(var)') xr_assert_allclose(result.transpose(*ref.nd.dims), ref.transpose(*ref.nd.dims))
def test_rasterize_no_side_effects(): ds = generate_test_dataset() df = generate_test_geodataframe() df_copy = df.copy() _ = vector.rasterize(df, ds) # Check that the original GeoDataFrame doesn't change as part of the # rasterization assert_geodataframe_equal(df, df_copy)
def test_write_video(tmpdir, fname): path = str(tmpdir.join(fname)) ntime = 10 ds = generate_test_dataset(dims={'y': 20, 'x': 20, 'time': ntime}) visualize.write_video(ds, path) assert os.path.isfile(path) video = imageio.mimread(path) assert len(video) == ntime
def test_get_transform(name, kwargs): ds = generate_test_dataset(**kwargs) bounds = get_bounds(ds) resx = (bounds.right - bounds.left) / (ds.dims['x'] - 1) resy = (bounds.bottom - bounds.top) / (ds.dims['y'] - 1) xoff = bounds.left yoff = bounds.top transform = Affine(resx, 0, xoff, 0, resy, yoff) assert_equal(get_transform(ds), transform)
def test_rasterize_columns(columns, date_field): ds = generate_test_dataset() df = generate_test_geodataframe() raster = vector.rasterize(df, ds, columns=columns, date_field=date_field) if date_field is None: expected_vars = set(columns) - {'geometry'} else: expected_vars = set(columns) - {'geometry', 'date'} assert_equal(set(raster.data_vars), expected_vars)
def test_parallel_invalid_dim(): dims = dict(x=50, y=50, time=50) ds = generate_test_dataset(dims=dims) def _fn(ds): return (ds - ds.mean('time')) / ds.std('time') with assert_raises_regex(ValueError, "The dataset has no dimension 'invalid'"): _ = utils.parallel(_fn, chunks=4, dim='invalid')(ds)
def test_accessor_nd_to_video(tmpdir): ds = generate_test_dataset() path_1 = str(tmpdir.join('video1.avi')) path_2 = str(tmpdir.join('video2.avi')) visualize.write_video(ds, path_1) ds.nd.to_video(path_2) assert_equal_files(path_1, path_2)
def test_reprojection_failure(): ds = generate_test_dataset() transform = get_transform(ds) extent = get_extent(ds) with assert_raises_regex( ValueError, ".* must also specify the `width` and `height`.*"): proj = Reprojection(crs=epsg4326, transform=transform) with assert_raises_regex(ValueError, "Need to provide either `width` and `height` .*"): proj = Reprojection(crs=epsg4326, extent=extent)
def test_rasterize_date_field(): ds = generate_test_dataset() df = generate_test_geodataframe() raster = vector.rasterize(df, ds, date_field='date') assert len(np.unique(df['date'])) == raster.dims['time'] assert_equal( np.unique(df['date']).astype('datetime64[s]'), raster.time.values.astype('datetime64[s]'))
def test_change(): ds1 = testing.generate_test_dataset(dims={ 'y': 5, 'x': 5, 'time': 10 }, mean=[1, 0, 0, 1], sigma=0.1).isel(time=slice(None, 5)) ds2 = testing.generate_test_dataset(dims={ 'y': 5, 'x': 5, 'time': 10 }, mean=[10, 0, 0, 10], sigma=0.1).isel(time=slice(5, None)) ds = xr.concat([ds1, ds2], dim='time') changes = OmnibusTest(n=9, alpha=0.9).apply(ds) assert changes.isel(time=5).all() assert (changes.sum(dim='time') == 1).all()
def create_snap_ds(*args, **kwargs): ds = generate_test_dataset(*args, **kwargs) crs = get_crs(ds) t = get_transform(ds) i2m_string = ','.join(map(str, [t.a, t.d, t.b, t.e, t.c, t.f])) del ds.attrs['crs'] del ds.attrs['transform'] ds['crs'] = ((), 1) attrs = {'crs': crs.wkt, 'i2m': i2m_string} ds['crs'].attrs = attrs return ds
def test_resample_to_width_or_height(name, kwargs, resample_kwargs): ds = generate_test_dataset(**kwargs) resampled = Resample(**resample_kwargs).apply(ds) if 'width' in resample_kwargs: assert_equal(resample_kwargs['width'], ncols(resampled)) elif 'height' in resample_kwargs: assert_equal(resample_kwargs['height'], nrows(resampled)) # Make sure aspect ratio is preserved assert_equal(int(ncols(resampled) / nrows(resampled)), int(ncols(ds) / nrows(ds)))