def test__interp_between_vectors(N): index = numpy.arange(0, 4) vector1 = -1 * index**2 - 1 vector2 = 2 * index**2 + 2 expected = { 1: numpy.array([ [ -1.0, -2.0, -5.0, -10.0], [ 0.5, 1.0, 2.5, 5.0], [ 2.0, 4.0, 10.0, 20.0], ]), 3: numpy.array([ [ -1.00, -2.00, -5.00, -10.00], [ -0.25, -0.50, -1.25, -2.50], [ 0.50, 1.00, 2.50, 5.00], [ 1.25, 2.50, 6.25, 12.50], [ 2.00, 4.00, 10.00, 20.00], ]) } if N: result = core._interp_between_vectors(vector1, vector2, n_nodes=N) nptest.assert_array_equal(result, expected[N]) else: with raises(ValueError): core._interp_between_vectors(vector1, vector2, n_nodes=0)
def test_elev_or_mask(x, y, offset, expected): if expected is None: with raises(ValueError): validate.elev_or_mask(x, y, failNone=True) else: result = validate.elev_or_mask(x, y, offset=offset) nptest.assert_array_equal(result, expected)
def test_split_rows(C, index, axis, first, second): expected = { 'top': numpy.array([ [ 0.0, 1.0, 2.0, 3.0, 4.0], [ 5.0, 6.0, 7.0, 8.0, 9.0], [10.0, 11.0, 12.0, 13.0, 14.0], ]), 'bottom': numpy.array([ [15., 16., 17., 18., 19.], [20., 21., 22., 23., 24.], ]), 'left': numpy.array([ [ 0., 1.], [ 5., 6.], [10., 11.], [15., 16.], [20., 21.], ]), 'right': numpy.array([ [ 2., 3., 4.], [ 7., 8., 9.], [12., 13., 14.], [17., 18., 19.], [22., 23., 24.], ]), } if first and second: a, b = core.split(C, index, axis) nptest.assert_array_equal(a, expected[first]) nptest.assert_array_equal(b, expected[second]) else: with raises(ValueError): left, right = core.split(C, index, axis=axis)
def test_ModelGrid_to_coord_pairs(g1, usemask, which, error): if error: with raises(error): g1.to_coord_pairs(usemask=usemask, which=which) else: expected = { ('nodes', False): numpy.array([ [1.0, 0.0], [1.5, 0.0], [2.0, 0.0], [1.0, 0.5], [1.5, 0.5], [2.0, 0.5], [1.0, 1.0], [1.5, 1.0], [2.0, 1.0], [1.0, 1.5], [1.5, 1.5], [2.0, 1.5], [1.0, 2.0], [1.5, 2.0], [2.0, 2.0], [1.0, 2.5], [1.5, 2.5], [2.0, 2.5], [1.0, 3.0], [1.5, 3.0], [2.0, 3.0], [1.0, 3.5], [1.5, 3.5], [2.0, 3.5], [1.0, 4.0], [1.5, 4.0], [2.0, 4.0] ]), ('cells', False): numpy.array([ [1.25, 0.25], [1.75, 0.25], [1.25, 0.75], [1.75, 0.75], [1.25, 1.25], [1.75, 1.25], [1.25, 1.75], [1.75, 1.75], [1.25, 2.25], [1.75, 2.25], [1.25, 2.75], [1.75, 2.75], [1.25, 3.25], [1.75, 3.25], [1.25, 3.75], [1.75, 3.75] ]), ('cells', True): numpy.array([ [nan, nan], [nan, nan], [nan, nan], [nan, nan], [1.25, 1.25], [1.75, 1.25], [1.25, 1.75], [1.75, 1.75], [1.25, 2.25], [1.75, 2.25], [1.25, 2.75], [1.75, 2.75], [1.25, 3.25], [1.75, 3.25], [1.25, 3.75], [1.75, 3.75] ]) } result = g1.to_coord_pairs(usemask=usemask, which=which) nptest.assert_array_equal(result, expected[which, usemask])
def test__plot_boundaries(simple_boundary, simple_islands): with raises(NotImplementedError): fig1 = _viz_bokeh._plot_boundaries(model_x='x', model_y='y', model=simple_boundary) fig2 = _viz_bokeh._plot_boundaries(model_x=simple_boundary['x'], model_y=simple_boundary['y'], model=None) fig3 = _viz_bokeh._plot_boundaries(island_x='x', island_y='y', island_name='island', islands=simple_islands) fig4 = _viz_bokeh._plot_boundaries( island_x=simple_islands['x'], island_y=simple_islands['y'], island_name=simple_islands['island'], islands=None) fig5 = _viz_bokeh._plot_boundaries(model_x='x', model_y='y', model=simple_boundary, island_x='x', island_y='y', island_name='island', islands=simple_islands) fig6 = _viz_bokeh._plot_boundaries( model_x=simple_boundary['x'], model_y=simple_boundary['y'], model=None, island_x=simple_islands['x'], island_y=simple_islands['y'], island_name=simple_islands['island'], islands=None)
def test_ModelGrid__get_x_y_nodes_and_mask(g1, which, usemask, error): if error: with raises(error): g1._get_x_y(which, usemask=usemask) else: x, y = g1._get_x_y(which, usemask=usemask) nptest.assert_array_equal(x, getattr(g1, 'x' + which[0])) nptest.assert_array_equal(y, getattr(g1, 'y' + which[0]))
def test_requires(obj, error): with raises(error): @utils.requires(obj, 'obj') def foo(): return 4 assert foo() == 4
def test_xy_array_only_one_mask(): mask1 = numpy.array([ [0, 1, 1], [0, 0, 1], [0, 0, 0], ]) _y, _x = numpy.mgrid[:3, :3] y = numpy.ma.MaskedArray(data=_y, mask=mask1) with raises(ValueError): validate.xy_array(_x, y)
def test_ModelGrid_to_dataframe(g1, usemask, which, error): def name_cols(df): df.columns.names = ['coord', 'ii'] df.index.names = ['jj'] return df if error: with raises(ValueError): g1.to_dataframe(usemask=usemask, which=which) else: expected = { (False, 'nodes'): pandas.DataFrame({ ('easting', 0): { 0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0, 5: 1.0, 6: 1.0, 7: 1.0, 8: 1.0 }, ('easting', 1): { 0: 1.5, 1: 1.5, 2: 1.5, 3: 1.5, 4: 1.5, 5: 1.5, 6: 1.5, 7: 1.5, 8: 1.5 }, ('easting', 2): { 0: 2.0, 1: 2.0, 2: 2.0, 3: 2.0, 4: 2.0, 5: 2.0, 6: 2.0, 7: 2.0, 8: 2.0 }, ('northing', 0): { 0: 0.0, 1: 0.5, 2: 1.0, 3: 1.5, 4: 2.0, 5: 2.5, 6: 3.0, 7: 3.5, 8: 4.0 }, ('northing', 1): { 0: 0.0, 1: 0.5, 2: 1.0, 3: 1.5, 4: 2.0, 5: 2.5, 6: 3.0, 7: 3.5, 8: 4.0 }, ('northing', 2): { 0: 0.0, 1: 0.5, 2: 1.0, 3: 1.5, 4: 2.0, 5: 2.5, 6: 3.0, 7: 3.5, 8: 4.0} }).pipe(name_cols), (True, 'cells'): pandas.DataFrame({ ('easting', 0): { 0: nan, 1: nan, 2: 1.25, 3: 1.25, 4: 1.25, 5: 1.25, 6: 1.25, 7: 1.25 }, ('easting', 1): { 0: nan, 1: nan, 2: 1.75, 3: 1.75, 4: 1.75, 5: 1.75, 6: 1.75, 7: 1.75 }, ('northing', 0): { 0: nan, 1: nan, 2: 1.25, 3: 1.75, 4: 2.25, 5: 2.75, 6: 3.25, 7: 3.75 }, ('northing', 1): { 0: nan, 1: nan, 2: 1.25, 3: 1.75, 4: 2.25, 5: 2.75, 6: 3.25, 7: 3.75 } }).pipe(name_cols), } result = g1.to_dataframe(usemask=usemask, which=which) pdtest.assert_frame_equal(result, expected[(usemask, which)], check_names=False) pdtest.assert_index_equal(result.columns, expected[(usemask, which)].columns)
def test__plot_domain(simple_boundary): with raises(NotImplementedError): fig1 = _viz_bokeh._plot_domain(x='x', y='y', data=simple_boundary) fig2 = _viz_bokeh._plot_domain(x=simple_boundary['x'], y=simple_boundary['y'], data=None) fig3 = _viz_bokeh._plot_domain(x='x', y='y', beta='beta', data=simple_boundary) fig4 = _viz_bokeh._plot_domain(x=simple_boundary['x'], y=simple_boundary['y'], beta=simple_boundary['beta'], data=None)
def test_xy_array_diff_masks(): _y, _x = numpy.mgrid[:3, :3] mask1 = numpy.array([ [0, 1, 1], [0, 0, 1], [0, 0, 0], ]) mask2 = numpy.array([ [0, 1, 1], [0, 0, 1], [1, 0, 0], ]) y = numpy.ma.MaskedArray(data=_y, mask=mask1) x = numpy.ma.MaskedArray(data=_x, mask=mask2) with raises(ValueError): validate.xy_array(x, y)
def test_read_grid(): pntfile = resource_filename('pygridtools.tests.baseline_files', 'array_point.shp') cellfile = resource_filename('pygridtools.tests.baseline_files', 'array_grid.shp') result_df = iotools.read_grid(pntfile, othercols=['elev', 'river']) known_df = pandas.DataFrame( data={ 'easting': [1., 2., 3.] * 4, 'northing': sorted([4., 5., 6., 7.] * 3) }, index=pandas.MultiIndex.from_product( [[2, 3, 4, 5], [2, 3, 4]], names=['jj', 'ii'])).assign(elev=0.0).assign( river='test').reset_index().set_index(['ii', 'jj']).sort_index() pdtest.assert_frame_equal(result_df, known_df) with raises(NotImplementedError): result_df = iotools.read_grid(cellfile)
def test_xy_array_diff_shapes(): with raises(ValueError): validate.xy_array(numpy.zeros((3, 3)), numpy.zeros((4, 4)))
def test_call_bad(axis, x, y): fp = base_focus_point(axis) with raises(ValueError): fp(x, y)
def test_spec_bad_axis(): with raises(ValueError): pygridgen.grid._FocusPoint(0.1, 'xjunk', 2, 0.25)
def test_ModelGrid_mask_nodes_errors(mg, polyverts, kwargs, error): with raises(error): mg.mask_nodes(inside=polyverts, **kwargs)
def test_polygon(polycoords, error): with raises(error): poly = validate.polygon(polycoords) nptest.assert_array_equal( numpy.array([(2, 2), (5, 2), (5, 5), (2, 5)]), poly)
def test_mpl_ax_invalid(): with raises(ValueError): validate.mpl_ax('junk')
def test_equivalent_masks(): from numpy import nan X = numpy.array([ 1, 2, 3, nan, nan, 7, 1, 2, 3, nan, nan, 7, 1, 2, 3, nan, nan, nan, 1, 2, 3, nan, nan, nan, 1, 2, 3, nan, nan, 7, ]) Y1 = X.copy() Y2 = numpy.array([ 1, 2, 3, nan, nan, 7, 1, 2, 3, nan, nan, nan, 1, 2, 3, nan, nan, nan, 1, 2, 3, nan, nan, nan, 1, 2, 3, nan, nan, 7, ]) with raises(ValueError): validate.equivalent_masks(X, Y2) x, y = validate.equivalent_masks(X, Y1) nptest.assert_array_equal(X, x.data) nptest.assert_array_equal(Y1, y.data)
def test_masks_no_polys(mg): with raises(ValueError): mg.mask_nodes() with raises(ValueError): mg.mask_centroids()
def test__plot_cells(simple_nodes): with raises(NotImplementedError): fig1 = _viz_bokeh._plot_cells(*simple_nodes)
def test_ModelGrid_bad_shapes(simple_cells): xc, yc = simple_cells with raises(ValueError): mg = core.ModelGrid(xc, yc[2:, 2:])