Ejemplo n.º 1
0
    def test_set_spatial_mask(self):
        dmap = DimensionMap()
        dims = Dimension('x', 3), Dimension('y', 7)
        mask_var = create_spatial_mask_variable('a_mask', None, dims)
        self.assertFalse(np.any(mask_var.get_mask()))
        dmap.set_spatial_mask(mask_var)
        self.assertEqual(dmap.get_spatial_mask(), mask_var.name)

        with self.assertRaises(DimensionMapError):
            dmap.set_variable(DMK.SPATIAL_MASK, mask_var)

        # Test custom variables may be used.
        dmap = DimensionMap()
        dims = Dimension('x', 3), Dimension('y', 7)
        mask_var = create_spatial_mask_variable('a_mask', None, dims)
        attrs = {'please keep me': 'no overwriting'}
        dmap.set_spatial_mask(mask_var, attrs=attrs)
        attrs = dmap.get_attrs(DMK.SPATIAL_MASK)
        self.assertIn('please keep me', attrs)

        # Test default attributes are not added.
        dmap = DimensionMap()
        dmap.set_spatial_mask('foo', default_attrs={'blue': 'world'})
        prop = dmap.get_property(DMK.SPATIAL_MASK)
        self.assertEqual(prop['attrs'], {'blue': 'world'})
Ejemplo n.º 2
0
    def test_set_spatial_mask(self):
        dmap = DimensionMap()
        dims = Dimension('x', 3), Dimension('y', 7)
        mask_var = create_spatial_mask_variable('a_mask', None, dims)
        self.assertFalse(np.any(mask_var.get_mask()))
        dmap.set_spatial_mask(mask_var)
        self.assertEqual(dmap.get_spatial_mask(), mask_var.name)

        with self.assertRaises(DimensionMapError):
            dmap.set_variable(DMK.SPATIAL_MASK, mask_var)

        # Test custom variables may be used.
        dmap = DimensionMap()
        dims = Dimension('x', 3), Dimension('y', 7)
        mask_var = create_spatial_mask_variable('a_mask', None, dims)
        attrs = {'please keep me': 'no overwriting'}
        dmap.set_spatial_mask(mask_var, attrs=attrs)
        attrs = dmap.get_attrs(DMK.SPATIAL_MASK)
        self.assertIn('please keep me', attrs)

        # Test default attributes are not added.
        dmap = DimensionMap()
        dmap.set_spatial_mask('foo', default_attrs={'blue': 'world'})
        prop = dmap.get_property(DMK.SPATIAL_MASK)
        self.assertEqual(prop['attrs'], {'blue': 'world'})
Ejemplo n.º 3
0
    def test_grid(self):
        # Test mask variable information is propagated through property.
        grid = self.get_gridxy(with_xy_bounds=True)
        self.assertTrue(grid.is_vectorized)
        self.assertTrue(grid.has_bounds)
        np.random.seed(1)
        value = np.random.rand(*grid.shape)
        select = value > 0.4
        mask_var = create_spatial_mask_variable('nonstandard', select, grid.dimensions)
        grid.set_mask(mask_var)
        field = Field(grid=grid)
        self.assertTrue(field.grid.has_bounds)
        self.assertEqual(field.dimension_map.get_spatial_mask(), mask_var.name)
        self.assertNumpyAll(field.grid.get_mask(), mask_var.get_mask())

        # Test dimension map bounds are updated appropriately.
        dim = Dimension('count', 2)
        x = Variable(name='x', value=[1., 2.], dimensions=dim)
        y = Variable(name='y', value=[1., 2.], dimensions=dim)
        xb = Variable(name='xb', value=[[0., 1.5], [1.5, 2.5]], dimensions=[dim, 'bounds'])
        yb = Variable(name='yb', value=[[0., 1.5], [1.5, 2.5]], dimensions=[dim, 'bounds'])
        variables = [x, y, xb, yb]
        dmap = DimensionMap()
        dmap.set_variable(DMK.X, x, bounds=xb)
        dmap.set_variable(DMK.Y, y, bounds=yb)
        f = Field(dimension_map=dmap, variables=variables)
        self.assertTrue(f.grid.has_bounds)
Ejemplo n.º 4
0
Archivo: base.py Proyecto: huard/ocgis
    def get_or_create_spatial_mask(*args, **kwargs):
        """
        Get or create the spatial mask variable and return its mask value as a boolean array.

        :param tuple args: See table

        ===== ======================================================= ===================================
        Index Type                                                    Description
        ===== ======================================================= ===================================
        0     :class:`ocgis.spatial.base.AbstractXYZSpatialContainer` Target XYZ spatial container
        1:    <varying>                                               See :meth:`ocgis.Variable.get_mask`
        ===== ======================================================= ===================================

        :param dict kwargs: See keyword arguments to :meth:`~ocgis.Variable.get_mask`. If ``create`` and ``check_value``
         are ``True`` and no mask variable exists, then coordinate variable masks are combined using a logical OR
         operation.
        :rtype: :class:`numpy.ndarray`
        """
        from ocgis.spatial.base import create_spatial_mask_variable

        args = list(args)
        sobj = args[0]  # Spatial object containing coordinate variables
        args = args[1:]

        create = kwargs.get(KeywordArgument.CREATE, False)
        check_value = kwargs.get(KeywordArgument.CHECK_VALUE, False)

        # Check for existing mask variable
        mask_variable = sobj.mask_variable

        ret = None
        if mask_variable is None:
            if create:
                if check_value:
                    # Combine coordinate variable masks using a logical OR operation
                    for ii, cvar in enumerate(sobj.coordinate_variables):
                        currmask = cvar.get_mask(create=True, check_value=True)
                        if ii == 0:
                            mask_value = currmask
                        else:
                            mask_value = np.logical_or(currmask, mask_value)
                else:
                    # Let the mask creation function handling the creation of mask values
                    mask_value = None

                # Create the spatial mask variable and set it
                mask_variable = create_spatial_mask_variable(
                    VariableName.SPATIAL_MASK, mask_value, sobj.dimensions)
                sobj.set_mask(mask_variable)

        if mask_variable is not None:
            # Validate the mask variable checking attributes, data types, etc.
            sobj.driver.validate_spatial_mask(mask_variable)
            # Get the actual boolean array from the mask variable
            ret = mask_variable.get_mask(*args, **kwargs)

        return ret
Ejemplo n.º 5
0
Archivo: base.py Proyecto: NCPP/ocgis
    def get_or_create_spatial_mask(*args, **kwargs):
        """
        Get or create the spatial mask variable and return its mask value as a boolean array.

        :param tuple args: See table

        ===== ======================================================= ===================================
        Index Type                                                    Description
        ===== ======================================================= ===================================
        0     :class:`ocgis.spatial.base.AbstractXYZSpatialContainer` Target XYZ spatial container
        1:    <varying>                                               See :meth:`ocgis.Variable.get_mask`
        ===== ======================================================= ===================================

        :param dict kwargs: See keyword arguments to :meth:`~ocgis.Variable.get_mask`. If ``create`` and ``check_value``
         are ``True`` and no mask variable exists, then coordinate variable masks are combined using a logical OR
         operation.
        :rtype: :class:`numpy.ndarray`
        """
        from ocgis.spatial.base import create_spatial_mask_variable

        args = list(args)
        sobj = args[0]  # Spatial object containing coordinate variables
        args = args[1:]

        create = kwargs.get(KeywordArgument.CREATE, False)
        check_value = kwargs.get(KeywordArgument.CHECK_VALUE, False)

        # Check for existing mask variable
        mask_variable = sobj.mask_variable

        ret = None
        if mask_variable is None:
            if create:
                if check_value:
                    # Combine coordinate variable masks using a logical OR operation
                    for ii, cvar in enumerate(sobj.coordinate_variables):
                        currmask = cvar.get_mask(create=True, check_value=True)
                        if ii == 0:
                            mask_value = currmask
                        else:
                            mask_value = np.logical_or(currmask, mask_value)
                else:
                    # Let the mask creation function handling the creation of mask values
                    mask_value = None

                # Create the spatial mask variable and set it
                mask_variable = create_spatial_mask_variable(VariableName.SPATIAL_MASK, mask_value, sobj.dimensions)
                sobj.set_mask(mask_variable)

        if mask_variable is not None:
            # Validate the mask variable checking attributes, data types, etc.
            sobj.driver.validate_spatial_mask(mask_variable)
            # Get the actual boolean array from the mask variable
            ret = mask_variable.get_mask(*args, **kwargs)

        return ret
Ejemplo n.º 6
0
Archivo: base.py Proyecto: huard/ocgis
    def set_spatial_mask(sobj, value, cascade=False):
        """
        Set the spatial mask on an XYZ spatial container.

        :param sobj: Target XYZ spatial container
        :type sobj: :class:`ocgis.spatial.base.AbstractXYZSpatialContainer`
        :param value: The spatial mask value. This may be a variable or a boolean array. If it is a boolean array, a
         spatial mask variable will be created and this array set as its mask.
        :type value: :class:`ocgis.Variable` | :class:`numpy.ndarray`
        :param bool cascade: If ``True``, cascade the mask across shared dimensions on the grid.
        """
        from ocgis.spatial.grid import grid_set_mask_cascade
        from ocgis.spatial.base import create_spatial_mask_variable
        from ocgis.variable.base import Variable

        if value is None:
            # Remove the mask variable from the parent object and update the dimension map.
            if sobj.has_mask:
                sobj.parent.remove_variable(sobj.mask_variable)
                sobj.dimension_map.set_spatial_mask(None)
        else:
            if isinstance(value, Variable):
                # Set the mask variable from the incoming value.
                sobj.parent.add_variable(value, force=True)
                mask_variable = value
            else:
                # Convert the incoming boolean array into a mask variable.
                mask_variable = sobj.mask_variable
                if mask_variable is None:
                    dimensions = sobj.dimensions
                    mask_variable = create_spatial_mask_variable(
                        VariableName.SPATIAL_MASK, value, dimensions)
                    sobj.parent.add_variable(mask_variable)
                else:
                    mask_variable.set_mask(value)
            sobj.dimension_map.set_spatial_mask(mask_variable)

            if cascade:
                grid_set_mask_cascade(sobj)
Ejemplo n.º 7
0
Archivo: base.py Proyecto: NCPP/ocgis
    def set_spatial_mask(sobj, value, cascade=False):
        """
        Set the spatial mask on an XYZ spatial container.

        :param sobj: Target XYZ spatial container
        :type sobj: :class:`ocgis.spatial.base.AbstractXYZSpatialContainer`
        :param value: The spatial mask value. This may be a variable or a boolean array. If it is a boolean array, a
         spatial mask variable will be created and this array set as its mask.
        :type value: :class:`ocgis.Variable` | :class:`numpy.ndarray`
        :param bool cascade: If ``True``, cascade the mask across shared dimensions on the grid.
        """
        from ocgis.spatial.grid import grid_set_mask_cascade
        from ocgis.spatial.base import create_spatial_mask_variable
        from ocgis.variable.base import Variable

        if value is None:
            # Remove the mask variable from the parent object and update the dimension map.
            if sobj.has_mask:
                sobj.parent.remove_variable(sobj.mask_variable)
                sobj.dimension_map.set_spatial_mask(None)
        else:
            if isinstance(value, Variable):
                # Set the mask variable from the incoming value.
                sobj.parent.add_variable(value, force=True)
                mask_variable = value
            else:
                # Convert the incoming boolean array into a mask variable.
                mask_variable = sobj.mask_variable
                if mask_variable is None:
                    dimensions = sobj.dimensions
                    mask_variable = create_spatial_mask_variable(VariableName.SPATIAL_MASK, value, dimensions)
                    sobj.parent.add_variable(mask_variable)
                else:
                    mask_variable.set_mask(value)
            sobj.dimension_map.set_spatial_mask(mask_variable)

            if cascade:
                grid_set_mask_cascade(sobj)
Ejemplo n.º 8
0
 def fixture_mask(self):
     return create_spatial_mask_variable('mask', [False, False, False, False, True, False],
                                         dimensions=self.fixture_element_dimension)
Ejemplo n.º 9
0
 def fixture_mask(self):
     return create_spatial_mask_variable(
         'mask', [False, False, False, False, True, False],
         dimensions=self.fixture_element_dimension)