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

Commit

Permalink
Merge branch 'master' into feature-persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski authored Oct 24, 2018
2 parents c016710 + dd30cc6 commit 141ff9d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
20 changes: 20 additions & 0 deletions awkward/array/jagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ def fromuniques(cls, uniques, content):
out._parents = parents
return out

@classmethod
def fromindex(cls, index, content, validate=True):
index = awkward.util.toarray(index, awkward.util.INDEXTYPE, awkward.util.numpy.ndarray)
if isinstance(index, JaggedArray):
index = index.content
if not issubclass(index.dtype.type, awkward.util.numpy.integer):
raise TypeError("index must have integer dtype")
if len(index.shape) != 1 or len(index) != len(content):
raise ValueError("index array must be one-dimensional with the same length as content")

if validate:
if not ((index[1:] - index[:-1])[(index != 0)[1:]] == 1).all():
raise ValueError("every index that is not zero must be one greater than the previous")

starts = awkward.util.numpy.nonzero(index == 0)[0]
offsets = awkward.util.numpy.empty(len(starts) + 1, dtype=awkward.util.INDEXTYPE)
offsets[:-1] = starts
offsets[-1] = len(index)
return cls.fromoffsets(offsets, content)

@classmethod
def fromjagged(cls, jagged):
jagged = jagged._tojagged(copy=False)
Expand Down
5 changes: 3 additions & 2 deletions awkward/array/union.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ def __init__(self, tags, index, contents):
def fromtags(cls, tags, contents):
out = cls.__new__(cls)
out.tags = tags
out.index = awkward.util.numpy.empty(out._tags.shape, dtype=awkward.util.INDEXTYPE)
out.contents = contents

if len(out._tags.reshape(-1)) > 0 and out._tags.reshape(-1).max() >= len(out._contents):
raise ValueError("maximum tag is {0} but there are only {1} contents arrays".format(out._tags.reshape(-1).max(), len(out._contents)))

index = awkward.util.numpy.full(out._tags.shape, -1, dtype=awkward.util.INDEXTYPE)
for tag, content in enumerate(out._contents):
mask = (out._tags == tag)
out._index[mask] = awkward.util.numpy.arange(awkward.util.numpy.count_nonzero(mask))
index[mask] = awkward.util.numpy.arange(awkward.util.numpy.count_nonzero(mask))

out.index = index
return out

def copy(self, tags=None, index=None, contents=None):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_jagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def test_jagged_type(self):
a = JaggedArray([0, 3, 3, 5], [3, 3, 5, 10], [[0.0], [1.1], [2.2], [3.3], [4.4], [5.5], [6.6], [7.7], [8.8], [9.9]])
assert a.type == ArrayType(4, numpy.inf, 1, float)

def test_jagged_fromindex(self):
a = JaggedArray.fromindex([0, 1, 0, 0, 0, 1, 2, 0, 1, 0], [0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
self.assertEqual(a.tolist(), [[0.0, 1.1], [2.2], [3.3], [4.4, 5.5, 6.6], [7.7, 8.8], [9.9]])
self.assertEqual(a.starts.tolist(), [0, 2, 3, 4, 7, 9])
self.assertEqual(a.stops.tolist(), [2, 3, 4, 7, 9, 10])

def test_jagged_str(self):
pass

Expand Down

0 comments on commit 141ff9d

Please sign in to comment.