How to use the awkward.util 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
                stops[shorter] -= 4
                if not shorter.any():
                    break
github scikit-hep / awkward-array / tests / test_methods.py View on Github external
def _number_op(self, operator, scalar, reverse=False):
                if not isinstance(scalar, (numbers.Number, awkward.util.numpy.number)):
                    raise TypeError("cannot {0} a Type with a {1}".format(operator.__name__, type(scalar).__name__))
                if reverse:
                    return Type(operator(scalar, self.x))
                else:
                    return Type(operator(self.x, scalar))
github scikit-hep / awkward-array / awkward / generate.py View on Github external
def typeof(obj):
    if obj is None:
        return None

    elif isinstance(obj, (bool, numpy.bool_, numpy.bool)):
        return BoolFillable
    elif isinstance(obj, (numbers.Number, awkward.numpy.number)):
        return NumberFillable
    elif isinstance(obj, bytes):
        return BytesFillable
    elif isinstance(obj, awkward.util.string):
        return StringFillable

    elif isinstance(obj, dict):
        if any(not isinstance(x, str) for x in obj):
            raise TypeError("only dicts with str-typed keys may be converted")
        if len(obj) == 0:
            return None
        else:
            return set(obj)

    elif isinstance(obj, tuple) and hasattr(obj, "_fields") and obj._fields is type(obj)._fields:
        return obj._fields, type(obj)

    elif isinstance(obj, Iterable):
        return JaggedFillable
github scikit-hep / uproot-methods / uproot_methods / classes / TVector2.py View on Github external
    @awkward.util.wrapjaggedmethod(JaggedArrayMethods)
    def from_cartesian(cls, x, y):
        return cls(x, y)
github scikit-hep / awkward-array / awkward / derived / strings.py View on Github external
def __getitem__(self, where):
        if awkward.util.isstringslice(where):
            raise IndexError("cannot index StringArray with string or sequence of strings")

        if isinstance(where, tuple) and len(where) == 0:
            return self
        if not isinstance(where, tuple):
            where = (where,)
        head, tail = where[0], where[1:]

        if isinstance(head, awkward.util.integer):
            return super(StringArray, self).__getitem__(where)

        elif tail == ():
            out = self._content[where]
            return self.__class__(out.starts, out.stops, out.content, self.encoding)

        else:
            out = self._content[where]
            return self.__class__(out.starts, out.stops, out.content, self.encoding)
github scikit-hep / awkward-array / awkward / array / sparse.py View on Github external
original_head = head
            if head < 0:
                head += self._length
            if not 0 <= head < self._length:
                raise IndexError("index {0} is out of bounds for size {1}".format(original_head, self._length))
            i = awkward.util.numpy.searchsorted(self._coordinates, head, side="left")
            if self._coordinates[i] == head:
                return self._content[(i + 1,) + tail]
            else:
                return self._content[(0,) + tail]

        if isinstance(head, slice):
            start, stop, step = head.indices(self._length)
            head = awkward.util.numpy.arange(start, stop, step)

        head = awkward.util.numpy.array(head, copy=False)
        if len(head.shape) == 1 and issubclass(head.dtype.type, (awkward.util.numpy.bool, awkward.util.numpy.bool_)):
            if self._length != len(head):
                raise IndexError("boolean index did not match indexed array along dimension 0; dimension is {0} but corresponding boolean dimension is {1}".format(self._length, len(head)))

            head = awkward.util.numpy.arange(self._length)[head]

        if len(head.shape) == 1 and issubclass(head.dtype.type, awkward.util.numpy.integer):
            mask = (head < 0)
            if mask.any():
                head[mask] += self._length
            if (head < 0).any() or (head >= self._length).any():
                raise IndexError("index is out of bounds")

            index = awkward.util.numpy.searchsorted(self._coordinates, head, side="left")
            index[index >= len(self._coordinates)] = len(self._coordinates) - 1
            missed = (self._coordinates[index] != head)
github scikit-hep / awkward-array / awkward / array / base.py View on Github external
def __setitem__(self, where, what):
        if isinstance(where, awkward.util.string):
            self._content[where] = what

        elif self._util_isstringslice(where):
            if len(where) != len(what):
                raise ValueError("number of keys ({0}) does not match number of provided arrays ({1})".format(len(where), len(what)))
            for x, y in zip(where, what):
                self._content[x] = y

        else:
            raise TypeError("invalid index for assigning column to Table: {0}".format(where))
github scikit-hep / awkward-array / awkward / array / virtual.py View on Github external
#   (1) no cache and _array is None: make a new one
        #   (2) no cache and _array is an array: return _array
        #   (3) have a cache and _array is None: make a new one (filling cache)
        #   (4) have a cache and _array is a key and cache[key] was evicted: make a new one (filling cache)
        #   (5) have a cache and _array is a key and cache[key] exists: return cache[key]
        #
        # Abnormal states (user manually changed cache after materialization):
        #   (6) no cache and _array is a key (user removed _cache): make a new one
        #   (7) have a cache and _array is an array (user added _cache): fill cache and return _array

        if self._array is None:
            # states (1) and (3)
            return self.materialize()

        elif self._cache is None:
            if isinstance(self._array, (VirtualArray.TransientKey, awkward.util.string)):
                # abnormal state (6)
                return self.materialize()
            else:
                # state (2)
                return self._array

        else:
            if isinstance(self._array, (VirtualArray.TransientKey, awkward.util.string)):
                try:
                    # state (5)
                    return self._cache[self._array]
                except:
                    # state (4), taking any error in __getitem__ as evidence that it was evicted
                    return self.materialize()
            else:
                # abnormal state (7)
github scikit-hep / awkward-array / awkward / array / chunked.py View on Github external
def __delitem__(self, where):
        if isinstance(where, awkward.util.string):
            for chunk in self._chunks:
                del chunk[where]
        elif self._util_isstringslice(where):
            for chunk in self._chunks:
                for x in where:
                    del chunk[x]
        else:
            raise TypeError("invalid index for removing column from Table: {0}".format(where))
github scikit-hep / awkward-array / awkward / derived / strings.py View on Github external
left, right = inputs[0], inputs[1]

            if isinstance(left, (str, bytes)):
                left = StringArray.fromstr(len(right), left)
            elif isinstance(left, awkward.util.numpy.ndarray) and (left.dtype.kind == "U" or left.dtype.kind == "S"):
                left = StringArray.fromnumpy(left)
            elif isinstance(left, awkward.util.numpy.ndarray) and left.dtype == awkward.util.numpy.dtype(object):
                left = StringArray.fromiter(left)
            elif not isinstance(left, StringMethods):
                return awkward.util.numpy.zeros(len(right), dtype=awkward.util.BOOLTYPE)

            if isinstance(right, (str, bytes)):
                right = StringArray.fromstr(len(left), right)
            elif isinstance(right, awkward.util.numpy.ndarray) and (right.dtype.kind == "U" or right.dtype.kind == "S"):
                right = StringArray.fromnumpy(right)
            elif isinstance(right, awkward.util.numpy.ndarray) and right.dtype == awkward.util.numpy.dtype(object):
                right = StringArray.fromiter(right)
            elif not isinstance(right, StringMethods):
                return awkward.util.numpy.zeros(len(left), dtype=awkward.util.BOOLTYPE)

            left = awkward.array.jagged.JaggedArray(left.starts, left.stops, left.content)
            right = awkward.array.jagged.JaggedArray(right.starts, right.stops, right.content)

            maybeequal = (left.counts == right.counts)

            leftmask = left[maybeequal]
            rightmask = right[maybeequal]

            reallyequal = (leftmask == rightmask).count_nonzero() == leftmask.counts

            out = awkward.util.numpy.zeros(len(left), dtype=awkward.util.BOOLTYPE)
            out[maybeequal] = reallyequal