class DynamicModel(properties.HasProperties):
            a = properties.Integer('')
            b = properties.Renamed('a')

            @properties.Integer('')
            def c(self):
                return self.a
Exemple #2
0
        class MyHasProps(properties.HasProperties):
            my_int = properties.Integer('My integer')

            not_my_int = properties.Renamed('my_int', warn=False, doc='')
Exemple #3
0
        class MyHasProps(properties.HasProperties):
            my_int = properties.Integer('My integer')

            not_my_int = properties.Renamed('my_int')
Exemple #4
0
 class BadRenamed(properties.HasProperties):
     new_prop = properties.Renamed('no_good')
Exemple #5
0
class Mesh2DGrid(BaseMesh):
    """Contains spatial information of a 2D grid."""
    h1 = properties.Array(doc='Grid cell widths, U-direction',
                          shape=('*', ),
                          dtype=(float, int))
    h2 = properties.Array(doc='Grid cell widths, V-direction',
                          shape=('*', ),
                          dtype=(float, int))
    x0 = properties.Renamed('O')
    O = properties.Vector3(doc='Origin vector', default=[0., 0., 0.])
    U = properties.Vector3(doc='Orientation of h1 axis', default='X')
    V = properties.Vector3(doc='Orientation of h2 axis', default='Y')
    Z = properties.Array(
        doc='Node topography',
        shape=('*', ),
        dtype=float,
        required=False,
        serializer=array_serializer,
        deserializer=array_download(('*', ), (float, )),
    )
    opts = properties.Instance(
        doc='Mesh2D Options',
        instance_class=_Mesh2DOptions,
        default=_Mesh2DOptions,
    )

    @property
    def nN(self):
        """ get number of nodes """
        return (len(self.h1) + 1) * (len(self.h2) + 1)

    @property
    def nC(self):
        """ get number of cells """
        return len(self.h1) * len(self.h2)

    def _nbytes(self, arr=None):
        filenames = ('h1', 'h2', 'O', 'U', 'V', 'Z')
        if arr is None:
            return sum(self._nbytes(fn) for fn in filenames)
        if isinstance(arr, string_types) and arr in filenames:
            if getattr(self, arr, None) is None:
                return 0
            arr = getattr(self, arr)
        if isinstance(arr, ndarray):
            return arr.astype('f4').nbytes
        raise ValueError('Mesh2DGrid cannot calculate the number of '
                         'bytes of {}'.format(arr))

    @properties.observer('Z')
    def _reject_large_files(self, change):
        self._validate_file_size(change['name'], change['value'])

    @properties.validator
    def _validate_Z(self):
        """Check if mesh content is built correctly"""
        if self.Z is None or len(self.Z) == 0:
            return True
        if len(self.Z) != self.nN:
            raise ValueError(
                'Length of Z, {zlen}, must equal number of nodes, '
                '{nnode}'.format(zlen=len(self.Z), nnode=self.nN))
        self._validate_file_size('Z', self.Z)
        return True

    def _get_dirty_data(self, force=False):
        datadict = super(Mesh2DGrid, self)._get_dirty_data(force)
        dirty = self._dirty_props
        if ('h1' in dirty or 'h2' in dirty) or force:
            datadict['tensors'] = dumps(
                dict(
                    h1=self.h1.tolist(),
                    h2=self.h2.tolist(),
                ))
        if ('O' in dirty or 'U' in dirty or 'V' in dirty) or force:
            datadict['OUV'] = dumps(
                dict(
                    O=self.O.tolist(),
                    U=self.U.as_length(self.h1.sum()).tolist(),
                    V=self.V.as_length(self.h2.sum()).tolist(),
                ))
        return datadict

    def _get_dirty_files(self, force=False):
        files = super(Mesh2DGrid, self)._get_dirty_files(force)
        dirty = self._dirty_props
        if self.Z is not None and len(self.Z) > 0 and ('Z' in dirty or force):
            files['Z'] = self._props['Z'].serialize(self.Z)
        return files

    @classmethod
    def _build_from_json(cls, json, **kwargs):
        mesh = Mesh2DGrid(title=kwargs['title'],
                          description=kwargs['description'],
                          h1=json['tensors']['h1'],
                          h2=json['tensors']['h2'],
                          O=json['OUV']['O'],
                          U=json['OUV']['U'],
                          V=json['OUV']['V'],
                          opts=json['meta'])
        try:
            mesh.Z = cls._props['Z'].deserialize(json['Z'], )
        except:
            mesh.Z = []

        return mesh

    @classmethod
    def _build_from_omf(cls, omf_geom, omf_project):
        mesh = Mesh2DGrid(h1=omf_geom.tensor_u,
                          h2=omf_geom.tensor_v,
                          O=omf_geom.origin + omf_project.origin,
                          U=omf_geom.axis_u,
                          V=omf_geom.axis_v)
        if omf_geom.offset_w is not None:
            mesh.Z = omf_geom.offset_w.array
        return mesh

    def _to_omf(self):
        import omf
        geometry = omf.SurfaceGridGeometry(
            tensor_u=self.h1,
            tensor_v=self.h2,
            axis_u=self.U,
            axis_v=self.V,
            origin=self.O,
            offset_w=omf.ScalarArray(self.Z, )
            if self.Z is not None else properties.undefined,
        )
        return geometry
Exemple #6
0
class Mesh3DGrid(BaseMesh):
    """Contains spatial information of a 3D grid volume."""

    h1 = properties.Array(doc='Tensor cell widths, x-direction',
                          shape=('*', ),
                          dtype=(float, int))
    h2 = properties.Array(doc='Tensor cell widths, y-direction',
                          shape=('*', ),
                          dtype=(float, int))
    h3 = properties.Array(doc='Tensor cell widths, z-direction',
                          shape=('*', ),
                          dtype=(float, int))
    x0 = properties.Renamed('O')
    O = properties.Vector3(doc='Origin vector', default=[0., 0., 0.])
    U = properties.Vector3(doc='Orientation of h1 axis', default='X')
    V = properties.Vector3(doc='Orientation of h2 axis', default='Y')
    W = properties.Vector3(doc='Orientation of h3 axis', default='Z')
    opts = properties.Instance(
        doc='Mesh3D Options',
        instance_class=_Mesh3DOptions,
        default=_Mesh3DOptions,
    )

    @property
    def nN(self):
        """ get number of nodes """
        return (len(self.h1) + 1) * (len(self.h2) + 1) * (len(self.h3) + 1)

    @property
    def nC(self):
        """ get number of cells """
        return len(self.h1) * len(self.h2) * len(self.h3)

    def _nbytes(self, arr=None):
        filenames = ('h1', 'h2', 'h3', 'O', 'U', 'V', 'W')
        if arr is None:
            return sum(self._nbytes(fn) for fn in filenames)
        if isinstance(arr, string_types) and arr in filenames:
            if getattr(self, arr, None) is None:
                return 0
            arr = getattr(self, arr)
        if isinstance(arr, ndarray):
            return arr.astype('f4').nbytes
        raise ValueError('Mesh3DGrid cannot calculate the number of '
                         'bytes of {}'.format(arr))

    def _get_dirty_data(self, force=False):
        datadict = super(Mesh3DGrid, self)._get_dirty_data(force)
        dirty = self._dirty_props
        if force or ('h1' in dirty or 'h2' in dirty or 'h3' in dirty):
            datadict['tensors'] = dumps(
                dict(h1=self.h1.tolist(),
                     h2=self.h2.tolist(),
                     h3=self.h3.tolist()))
        if force or any(
            [item in dirty
             for item in ['O', 'U', 'V', 'W', 'h1', 'h2', 'h3']]):
            datadict['OUVZ'] = dumps(
                dict(O=self.O.tolist(),
                     U=self.U.as_length(self.h1.sum()).tolist(),
                     V=self.V.as_length(self.h2.sum()).tolist(),
                     Z=self.W.as_length(self.h3.sum()).tolist()))
        return datadict

    @classmethod
    def _build_from_json(cls, json, **kwargs):
        mesh = Mesh3DGrid(title=kwargs['title'],
                          description=kwargs['description'],
                          h1=json['tensors']['h1'],
                          h2=json['tensors']['h2'],
                          h3=json['tensors']['h3'],
                          O=json['OUVZ']['O'],
                          U=json['OUVZ']['U'],
                          V=json['OUVZ']['V'],
                          W=json['OUVZ']['Z'],
                          opts=json['meta'])
        return mesh

    @classmethod
    def _build_from_omf(cls, omf_geom, omf_project):
        mesh = Mesh3DGrid(h1=omf_geom.tensor_u,
                          h2=omf_geom.tensor_v,
                          h3=omf_geom.tensor_w,
                          O=omf_geom.origin + omf_project.origin,
                          U=omf_geom.axis_u,
                          V=omf_geom.axis_v,
                          W=omf_geom.axis_w)
        return mesh

    def _to_omf(self):
        import omf
        geometry = omf.VolumeGridGeometry(
            tensor_u=self.h1,
            tensor_v=self.h2,
            tensor_w=self.h3,
            axis_u=self.U,
            axis_v=self.V,
            axis_w=self.W,
            origin=self.O,
        )
        return geometry