Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #32 from guitargeek/concat
Browse files Browse the repository at this point in the history
Fixed concat and concatenate
  • Loading branch information
jpivarski authored Nov 19, 2018
2 parents 99a71c0 + 28f62d0 commit cc67b4d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
27 changes: 21 additions & 6 deletions awkward/array/jagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,15 +1103,22 @@ def max(self):
else:
return self._minmax_general(False, False)

@classmethod
def concat(cls, first, *rest): # all elements of first followed by all elements of second
arrays = (first,) + rest
@awkward.util.bothmethod
def concatenate(isclassmethod, cls_or_self, arrays):
if isclassmethod:
cls = cls_or_self
if not all(isinstance(x, JaggedArray) for x in arrays):
raise TypeError("cannot concatenate non-JaggedArrays with JaggedArray.concatenate")
else:
self = cls_or_self
cls = self.__class__
if not isinstance(self, JaggedArray) or not all(isinstance(x, JaggedArray) for x in arrays):
raise TypeError("cannot concatenate non-JaggedArrays with JaggedArray.concatenate")
arrays = (self,) + tuple(arrays)

for x in arrays:
x._valid()

if not all(isinstance(x, JaggedArray) for x in arrays):
raise TypeError("cannot concat JaggedArrays with non-JaggedArrays")

starts = awkward.util.numpy.concatenate([x._starts for x in arrays])
stops = awkward.util.numpy.concatenate([x._stops for x in arrays])
content = awkward.util.concatenate([x._content for x in arrays])
Expand Down Expand Up @@ -1215,6 +1222,14 @@ def fromuniques(cls, uniques, content, subdtype):
tmp = ByteJaggedArray.__bases__[0].fromuniques(uniques, awkward.util.numpy.array([]))
return cls(tmp._starts, tmp._stops, content, subdtype=subdtype)

@classmethod
def fromregular(cls, content, size=1):
quotient = -(-len(content) // size)
offsets = awkward.util.numpy.arange(0, quotient * size + 1, size, dtype=awkward.util.INDEXTYPE)
if len(offsets) > 0:
offsets[-1] = len(content)
return cls.fromoffsets(offsets, content)

def copy(self, starts=None, stops=None, content=None, subdtype=None):
out = super(ByteJaggedArray, self).copy(starts=starts, stops=stops, content=content)
if subdtype is None:
Expand Down
13 changes: 11 additions & 2 deletions awkward/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def is_intstring(x):
return isinstance(x, string) and is_intstring._pattern.match(x) is not None
is_intstring._pattern = re.compile("^_(0|[1-9]+[0-9]*)$")

class bothmethod(object):
def __init__(self, fcn):
self.fcn = fcn
def __get__(self, ins, typ):
if ins is None:
return lambda *args, **kwargs: self.fcn(True, typ, *args, **kwargs)
else:
return lambda *args, **kwargs: self.fcn(False, ins, *args, **kwargs)

################################################################ array helpers

import distutils.version
Expand All @@ -84,7 +93,7 @@ def is_intstring(x):
MASKTYPE = numpy.dtype(numpy.bool_)
BITMASKTYPE = numpy.dtype(numpy.uint8)
BOOLTYPE = numpy.dtype(numpy.bool_)

def toarray(value, defaultdtype, passthrough=None):
import awkward.array.base
if passthrough is None:
Expand Down Expand Up @@ -132,7 +141,7 @@ def concatenate(arrays):
if all(isinstance(x, numpy.ndarray) for x in arrays):
return numpy.concatenate(arrays)
else:
return arrays[0].concat(*arrays[1:])
return arrays[0].concatenate(arrays[1:])

def isstringslice(where):
import awkward.array.base
Expand Down
13 changes: 13 additions & 0 deletions tests/test_jagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ def test_jagged_max(self):
a = JaggedArray([0, 3, 3, 5], [3, 3, 5, 10], [[0.0, 0.0], [1.1, 1.1], [2.2, 2.2], [3.3, 3.3], [4.4, 4.4], [5.5, 5.5], [6.6, 6.6], [7.7, 7.7], [8.8, 8.8], [9.9, 9.9]])
assert a.max().tolist() == [[2.2, 2.2], [-numpy.inf, -numpy.inf], [4.4, 4.4], [9.9, 9.9]]

def test_jagged_concatenate(self):
lst = [[1, 2, 3], [], [4, 5], [6, 7], [8], [], [9], [10, 11], [12]]
a_orig = JaggedArray.fromiter(lst)
a1 = JaggedArray.fromiter(lst[:3])
a2 = JaggedArray.fromiter(lst[3:6])
a3 = JaggedArray.fromiter(lst[6:])

a_instance_concat = a1.concatenate([a2, a3])
assert a_instance_concat.tolist() == a_orig.tolist()

a_class_concat = JaggedArray.concatenate([a1, a2, a3])
assert a_class_concat.tolist() == a_orig.tolist()

def test_jagged_get(self):
a = JaggedArray.fromoffsets([0, 3, 3, 8, 10, 10], [0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
assert [a[i].tolist() for i in range(len(a))] == [[0.0, 1.1, 2.2], [], [3.3, 4.4, 5.5, 6.6, 7.7], [8.8, 9.9], []]
Expand Down

0 comments on commit cc67b4d

Please sign in to comment.