Пример #1
0
 def __init__(self, element_type, ndim):
     self._element_type = element_type
     self._ndim = NatLiteral(ndim) if isinstance(ndim, int) else ndim
     super(tndarray, self).__init__()
Пример #2
0
class tndarray(HailType):
    """Hail type for n-dimensional arrays.

    .. include:: _templates/experimental.rst

    In Python, these are represented as NumPy :obj:`ndarray`.

    Notes
    -----

    NDArrays contain elements of only one type, which is parameterized by
    `element_type`.

    Parameters
    ----------
    element_type : :class:`.HailType`
        Element type of array.
    ndim : int32
        Number of dimensions.

    See Also
    --------
    :class:`.NDArrayExpression`, :func:`.ndarray`
    """

    @typecheck_method(element_type=hail_type, ndim=oneof(NatBase, int))
    def __init__(self, element_type, ndim):
        self._element_type = element_type
        self._ndim = NatLiteral(ndim) if isinstance(ndim, int) else ndim
        super(tndarray, self).__init__()

    @property
    def element_type(self):
        """NDArray element type.

        Returns
        -------
        :class:`.HailType`
            Element type.
        """
        return self._element_type

    @property
    def ndim(self):
        """NDArray number of dimensions.

        Returns
        -------
        :obj:`int`
            Number of dimensions.
        """
        assert isinstance(self._ndim, NatLiteral), "tndarray must be realized with a concrete number of dimensions"
        return self._ndim.n

    def _traverse(self, obj, f):
        if f(self, obj):
            for elt in obj:
                self.element_type._traverse(elt, f)

    def _typecheck_one_level(self, annotation):
        raise NotImplementedError

    def __str__(self):
        return "ndarray<{}, {}>".format(self.element_type, self.ndim)

    def _eq(self, other):
        return isinstance(other, tndarray) and self.element_type == other.element_type

    def _pretty(self, l, indent, increment):
        l.append('ndarray<')
        self._element_type._pretty(l, indent, increment)
        l.append(', ')
        l.append(str(self.ndim))
        l.append('>')

    def _parsable_string(self):
        return f'NDArray[{self._element_type._parsable_string()},{self.ndim}]'

    def _convert_from_json(self, x):
        raise NotImplementedError

    def _convert_to_json(self, x):
        raise NotImplementedError

    def clear(self):
        self._element_type.clear()
        self._ndim.clear()

    def unify(self, t):
        return isinstance(t, tndarray) and \
               self._element_type.unify(t._element_type) and \
               self._ndim.unify(t._ndim)

    def subst(self):
        return tndarray(self._element_type.subst(), self._ndim.subst())
Пример #3
0
class tndarray(HailType):
    """Hail type for n-dimensional arrays.

    .. include:: _templates/experimental.rst

    In Python, these are represented as NumPy :obj:`ndarray`.

    Notes
    -----

    NDArrays contain elements of only one type, which is parameterized by
    `element_type`.

    Parameters
    ----------
    element_type : :class:`.HailType`
        Element type of array.
    ndim : int32
        Number of dimensions.

    See Also
    --------
    :class:`.NDArrayExpression`, :func:`.ndarray`
    """

    @typecheck_method(element_type=hail_type, ndim=oneof(NatBase, int))
    def __init__(self, element_type, ndim):
        self._element_type = element_type
        self._ndim = NatLiteral(ndim) if isinstance(ndim, int) else ndim
        super(tndarray, self).__init__()

    @property
    def element_type(self):
        """NDArray element type.

        Returns
        -------
        :class:`.HailType`
            Element type.
        """
        return self._element_type

    @property
    def ndim(self):
        """NDArray number of dimensions.

        Returns
        -------
        :obj:`int`
            Number of dimensions.
        """
        assert isinstance(self._ndim, NatLiteral), "tndarray must be realized with a concrete number of dimensions"
        return self._ndim.n

    def _traverse(self, obj, f):
        if f(self, obj):
            for elt in np.nditer(obj):
                self.element_type._traverse(elt.item(), f)

    def _typecheck_one_level(self, annotation):
        if annotation is not None and not isinstance(annotation, np.ndarray):
            raise TypeError("type 'ndarray' expected Python 'numpy.ndarray', but found type '%s'" % type(annotation))

    def __str__(self):
        return "ndarray<{}, {}>".format(self.element_type, self.ndim)

    def _eq(self, other):
        return isinstance(other, tndarray) and self.element_type == other.element_type

    def _pretty(self, l, indent, increment):
        l.append('ndarray<')
        self._element_type._pretty(l, indent, increment)
        l.append(', ')
        l.append(str(self.ndim))
        l.append('>')

    def _parsable_string(self):
        return f'NDArray[{self._element_type._parsable_string()},{self.ndim}]'

    def _convert_from_json(self, x):
        np_type = self.element_type.to_numpy()
        return np.ndarray(shape=x['shape'], buffer=np.array(x['data'], dtype=np_type), strides=x['strides'], dtype=np_type)

    def _convert_to_json(self, x):
        data = x.reshape(x.size).tolist()

        strides = []
        axis_one_step_byte_size = x.itemsize
        for dimension_size in reversed(x.shape):
            strides.insert(0, axis_one_step_byte_size)
            axis_one_step_byte_size *= (dimension_size if dimension_size > 0 else 1)

        json_dict = {
            "shape": x.shape,
            "strides": strides,
            "flags": 0,
            "data": data,
            "offset": 0
        }
        return json_dict

    def clear(self):
        self._element_type.clear()
        self._ndim.clear()

    def unify(self, t):
        return isinstance(t, tndarray) and \
               self._element_type.unify(t._element_type) and \
               self._ndim.unify(t._ndim)

    def subst(self):
        return tndarray(self._element_type.subst(), self._ndim.subst())

    def _get_context(self):
        return self.element_type.get_context()
Пример #4
0
 def __init__(self, element_type, ndim):
     self._element_type = element_type
     self._ndim = NatLiteral(ndim) if isinstance(ndim, int) else ndim
     super(tndarray, self).__init__()
Пример #5
0
class tndarray(HailType):
    """Hail type for n-dimensional arrays.

    .. include:: _templates/experimental.rst

    In Python, these are represented as NumPy :obj:`ndarray`.

    Notes
    -----

    NDArrays contain elements of only one type, which is parameterized by
    `element_type`.

    Parameters
    ----------
    element_type : :class:`.HailType`
        Element type of array.
    ndim : int32
        Number of dimensions.

    See Also
    --------
    :class:`.NDArrayExpression`, :func:`.ndarray`
    """
    @typecheck_method(element_type=hail_type, ndim=oneof(NatBase, int))
    def __init__(self, element_type, ndim):
        self._element_type = element_type
        self._ndim = NatLiteral(ndim) if isinstance(ndim, int) else ndim
        super(tndarray, self).__init__()

    @property
    def element_type(self):
        """NDArray element type.

        Returns
        -------
        :class:`.HailType`
            Element type.
        """
        return self._element_type

    @property
    def ndim(self):
        """NDArray number of dimensions.

        Returns
        -------
        :obj:`int`
            Number of dimensions.
        """
        assert isinstance(
            self._ndim, NatLiteral
        ), "tndarray must be realized with a concrete number of dimensions"
        return self._ndim.n

    def _traverse(self, obj, f):
        if f(self, obj):
            for elt in obj:
                self.element_type._traverse(elt, f)

    def _typecheck_one_level(self, annotation):
        raise NotImplementedError

    def __str__(self):
        return "ndarray<{}, {}>".format(self.element_type, self.ndim)

    def _eq(self, other):
        return isinstance(other,
                          tndarray) and self.element_type == other.element_type

    def _pretty(self, l, indent, increment):
        l.append('ndarray<')
        self._element_type._pretty(l, indent, increment)
        l.append(', ')
        l.append(str(self.ndim))
        l.append('>')

    def _parsable_string(self):
        return f'NDArray[{self._element_type._parsable_string()},{self.ndim}]'

    def _convert_from_json(self, x):
        raise NotImplementedError

    def _convert_to_json(self, x):
        raise NotImplementedError

    def clear(self):
        self._element_type.clear()
        self._ndim.clear()

    def unify(self, t):
        return isinstance(t, tndarray) and \
               self._element_type.unify(t._element_type) and \
               self._ndim.unify(t._ndim)

    def subst(self):
        return tndarray(self._element_type.subst(), self._ndim.subst())