How to use the awkward.util.numpy function in awkward

To help you get started, we’ve selected a few awkward examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scikit-hep / awkward-array / awkward / derived / strings.py View on Github external
def fromnumpy(cls, array):
        if array.dtype.kind == "S":
            encoding = None
        elif array.dtype.kind == "U":
            encoding = "utf-32le"
        else:
            raise TypeError("not a string array")

        starts = awkward.util.numpy.arange(                   0,  len(array)      * array.dtype.itemsize, array.dtype.itemsize)
        stops  = awkward.util.numpy.arange(array.dtype.itemsize, (len(array) + 1) * array.dtype.itemsize, array.dtype.itemsize)
        content = array.view(awkward.util.CHARTYPE)

        shorter = awkward.util.numpy.ones(len(array), dtype=awkward.util.BOOLTYPE)
        if array.dtype.kind == "S":
            for checkat in range(array.dtype.itemsize - 1, -1, -1):
                shorter &= (content[checkat::array.dtype.itemsize] == 0)
                stops[shorter] -= 1
                if not shorter.any():
                    break

        elif array.dtype.kind == "U":
            content2 = content.view(awkward.util.numpy.uint32)
            itemsize2 = array.dtype.itemsize >> 2                 # itemsize // 4
            for checkat in range(itemsize2 - 1, -1, -1):
                shorter &= (content2[checkat::itemsize2] == 0)    # all four bytes are zero
github scikit-hep / awkward-array / tests / test_methods.py View on Github external
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
                if method != "__call__":
                    raise NotImplemented

                inputs = list(inputs)
                for i in range(len(inputs)):
                    if isinstance(inputs[i], awkward.util.numpy.ndarray) and inputs[i].dtype == awkward.util.numpy.dtype(object) and len(inputs[i]) > 0:
                        idarray = awkward.util.numpy.frombuffer(inputs[i], dtype=awkward.util.numpy.uintp)
                        if (idarray == idarray[0]).all():
                            inputs[i] = inputs[i][0]

                if ufunc is awkward.util.numpy.add or ufunc is awkward.util.numpy.subtract:
                    if not all(isinstance(x, (TypeArrayMethods, TypeMethods)) for x in inputs):
                        raise TypeError("(arrays of) Type can only be added to/subtracted from other (arrays of) Type")
                    out = self.empty_like()
                    out["x"] = getattr(ufunc, method)(*[x.x for x in inputs], **kwargs)
                    return out

                else:
                    return super(TypeArrayMethods, self).__array_ufunc__(ufunc, method, *inputs, **kwargs)
github scikit-hep / awkward-array / tests / test_methods.py View on Github external
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
                if method != "__call__":
                    raise NotImplemented

                inputs = list(inputs)
                for i in range(len(inputs)):
                    if isinstance(inputs[i], awkward.util.numpy.ndarray) and inputs[i].dtype == awkward.util.numpy.dtype(object) and len(inputs[i]) > 0:
                        idarray = awkward.util.numpy.frombuffer(inputs[i], dtype=awkward.util.numpy.uintp)
                        if (idarray == idarray[0]).all():
                            inputs[i] = inputs[i][0]

                if ufunc is awkward.util.numpy.add or ufunc is awkward.util.numpy.subtract:
                    if not all(isinstance(x, (TypeArrayMethods, TypeMethods)) for x in inputs):
                        raise TypeError("(arrays of) Type can only be added to/subtracted from other (arrays of) Type")
                    out = self.empty_like()
                    out["x"] = getattr(ufunc, method)(*[x.x for x in inputs], **kwargs)
                    return out

                else:
                    return super(TypeArrayMethods, self).__array_ufunc__(ufunc, method, *inputs, **kwargs)
github scikit-hep / awkward-array / awkward / derived / strings.py View on Github external
def fromiter(cls, iterable, encoding="utf-8"):
        if encoding is None:
            encoded = iterable
        else:
            encoder = codecs.getencoder(encoding)
            encoded = [encoder(x)[0] for x in iterable]
        counts = [len(x) for x in encoded]
        content = awkward.util.numpy.empty(sum(counts), dtype=awkward.util.CHARTYPE)
        i = 0
        for x in encoded:
            content[i : i + len(x)] = awkward.util.numpy.frombuffer(x, dtype=awkward.util.CHARTYPE)
            i += len(x)
        return cls.fromcounts(counts, content, encoding)
github scikit-hep / uproot / uproot / interp / jagged.py View on Github external
def _destructive_divide(array, divisor):
    if divisor == 1:
        pass
    elif divisor == 2:
        awkward.util.numpy.right_shift(array, 1, out=array)
    elif divisor == 4:
        awkward.util.numpy.right_shift(array, 2, out=array)
    elif divisor == 8:
        awkward.util.numpy.right_shift(array, 3, out=array)
    else:
        awkward.util.numpy.floor_divide(array, divisor, out=array)
    return array
github scikit-hep / uproot / uproot / interp / jagged.py View on Github external
if self.skipbytes == 0:
                offsets = _destructive_divide(byteoffsets, self.content.fromdtype.itemsize)
                starts  = offsets[local_entrystart     : local_entrystop    ]
                stops   = offsets[local_entrystart + 1 : local_entrystop + 1]
                content = self.content.fromroot(data, None, starts[0], stops[-1])
                return JaggedArray(starts, stops, content)

            else:
                bytestarts = byteoffsets[local_entrystart     : local_entrystop    ] + self.skipbytes
                bytestops  = byteoffsets[local_entrystart + 1 : local_entrystop + 1]

                mask = awkward.util.numpy.zeros(len(data), dtype=awkward.util.numpy.int8)
                mask[bytestarts[bytestarts < len(data)]] = 1
                awkward.util.numpy.add.at(mask, bytestops[bytestops < len(data)], -1)
                awkward.util.numpy.cumsum(mask, out=mask)
                data = data[mask.view(awkward.util.numpy.bool_)]

                content = self.content.fromroot(data, None, 0, bytestops[-1])

                itemsize = 1
                sub = self.content
                while hasattr(sub, "content"):
                    sub = sub.content
                if isinstance(sub, uproot.interp.numerical.asdtype):
                    itemsize = sub.fromdtype.itemsize

                counts = bytestops - bytestarts
                shift = math.log(itemsize, 2)
                if shift == round(shift):
                    awkward.util.numpy.right_shift(counts, int(shift), out=counts)
                else:
                    awkward.util.numpy.floor_divide(counts, itemsize, out=counts)
github scikit-hep / uproot / uproot / interp / jagged.py View on Github external
sub = self.content
                while hasattr(sub, "content"):
                    sub = sub.content
                if isinstance(sub, uproot.interp.numerical.asdtype):
                    itemsize = sub.fromdtype.itemsize

                counts = bytestops - bytestarts
                shift = math.log(itemsize, 2)
                if shift == round(shift):
                    awkward.util.numpy.right_shift(counts, int(shift), out=counts)
                else:
                    awkward.util.numpy.floor_divide(counts, itemsize, out=counts)

                offsets = awkward.util.numpy.empty(len(counts) + 1, awkward.util.INDEXTYPE)
                offsets[0] = 0
                awkward.util.numpy.cumsum(counts, out=offsets[1:])

                return JaggedArray(offsets[:-1], offsets[1:], content)
github scikit-hep / awkward-array / awkward / array / sparse.py View on Github external
def fromCOO(cls, length, coordinates, content, default=0):
        content = awkward.util.toarray(content, awkward.util.DEFAULTTYPE)
        if isinstance(content, awkward.util.numpy.ndarray):
            content = awkward.util.numpy.insert(content, 0, default)
        else:
            # FIXME: maybe something with an IndexedArray of ChunkedArrays?
            raise NotImplementedError(type(content))
        return cls(length, coordinates, content)
github scikit-hep / uproot / uproot / interp / jagged.py View on Github external
itemsize = 1
                sub = self.content
                while hasattr(sub, "content"):
                    sub = sub.content
                if isinstance(sub, uproot.interp.numerical.asdtype):
                    itemsize = sub.fromdtype.itemsize

                counts = bytestops - bytestarts
                shift = math.log(itemsize, 2)
                if shift == round(shift):
                    awkward.util.numpy.right_shift(counts, int(shift), out=counts)
                else:
                    awkward.util.numpy.floor_divide(counts, itemsize, out=counts)

                offsets = awkward.util.numpy.empty(len(counts) + 1, awkward.util.INDEXTYPE)
                offsets[0] = 0
                awkward.util.numpy.cumsum(counts, out=offsets[1:])

                return JaggedArray(offsets[:-1], offsets[1:], content)