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 utils.raises(ValueError): validate.xy_array(_x, y)
def test_xy_array_as_pairs(): _y, _x = numpy.mgrid[:3, :3] pairs = validate.xy_array(_y, _x) known_pairs = numpy.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]) nptest.assert_array_equal(pairs, known_pairs)
def mask_with_polygon(x, y, polyverts, inside=True): """ Mask x-y arrays inside or outside a polygon Parameters ---------- x, y : array-like NxM arrays of x- and y-coordinates. polyverts : sequence of a polygon's vertices A sequence of x-y pairs for each vertex of the polygon. inside : bool (default is True) Toggles masking the inside or outside the polygon Returns ------- mask : bool array The NxM mask that can be applied to ``x`` and ``y``. """ # validate input polyverts = validate.polygon(polyverts) points = validate.xy_array(x, y, as_pairs=True) # compute the mask mask = mpath.Path(polyverts).contains_points(points).reshape(x.shape) if inside: return mask else: return ~mask
def mask_with_polygon(x, y, polyverts, inside=True): """ Mask x-y arrays inside or outside a polygon Parameters ---------- x, y : array-like NxM arrays of x- and y-coordinates. polyverts : sequence of a polygon's vertices A sequence of x-y pairs for each vertex of the polygon. inside : bool (default = True) Toggles returning a mask *inside* or *outside* the polygon. Returns ------- mask : bool array The NxM mask that can be applied to ``x`` and ``y``. """ # validate input polyverts = validate.polygon(polyverts) points = validate.xy_array(x, y, as_pairs=True) # compute the mask mask = mpath.Path(polyverts).contains_points(points).reshape(x.shape) # invert if we're masking things outside the polygon if not inside: mask = ~mask return mask
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 utils.raises(ValueError): validate.xy_array(x, y)
def write_gridout_file(xcoords, ycoords, outfile): xcoords, ycoords = validate.xy_array(xcoords, ycoords, as_pairs=False) ny, nx = xcoords.shape df = pandas.DataFrame({'x': xcoords.flatten(), 'y': ycoords.flatten()}) with Path(outfile).open('w') as f: f.write('## {:d} x {:d}\n'.format(nx, ny)) df.to_csv(f, sep=' ', index=False, header=False, na_rep='NaN', float_format='%.3f') return df
def test_xy_array_diff_shapes(): with utils.raises(ValueError): validate.xy_array(numpy.zeros((3, 3)), numpy.zeros((4, 4)))
def test_xy_array_not_as_pairs(): _x, _y = numpy.mgrid[:9, :9] x, y = validate.xy_array(_x, _y, as_pairs=False) nptest.assert_array_equal(x, _x) nptest.assert_array_equal(y, _y)
def test_only_one_mask(self): y = np.ma.MaskedArray(data=self.y2, mask=self.mask1) validate.xy_array(self.x2, y)
def test_diff_masks(self): y = np.ma.MaskedArray(data=self.y2, mask=self.mask1) x = np.ma.MaskedArray(data=self.x2, mask=self.mask2) validate.xy_array(x, y)
def test_diff_shapes(self): validate.xy_array(self.y1, self.x2)
def test_as_pairs(self): xy = validate.xy_array(self.y2, self.x2) nt.assert_true(np.all(xy == self.known_pairs))
def test_not_as_pairs(self): x, y = validate.xy_array(self.y1, self.x1, as_pairs=False) nt.assert_true(np.all(x == self.y1)) nt.assert_true(np.all(y == self.x1))