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 #159 from scikit-hep/pad-axis-parameter
Browse files Browse the repository at this point in the history
add an axis parameter to the pad method
  • Loading branch information
jpivarski authored Jul 14, 2019
2 parents c5180b0 + 4c5ed76 commit 513f28a
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions awkward/array/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@ def _util_flatten(cls, array, axis):
return array.reshape(array.shape[:axis] + (-1,) + array.shape[axis + 2:])

@classmethod
def _util_pad(cls, array, length, maskedwhen, clip):
def _util_pad(cls, array, length, maskedwhen, clip, axis):
if isinstance(array, AwkwardArray):
return array.pad(length, maskedwhen=maskedwhen, clip=clip)
return array.pad(length, maskedwhen=maskedwhen, clip=clip, axis=axis)

elif len(array.shape) == 1:
raise ValueError("pad cannot be applied to scalars")
Expand Down
4 changes: 2 additions & 2 deletions awkward/array/chunked.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,8 @@ def flatten(self, axis=0):
out.knowchunksizes()
return out

def pad(self, length, maskedwhen=True, clip=False):
return self.copy(chunks=[self._util_pad(x, length, maskedwhen, clip) for x in self._chunks], chunksizes=self.chunksizes)
def pad(self, length, maskedwhen=True, clip=False, axis=0):
return self.copy(chunks=[self._util_pad(x, length, maskedwhen, clip, axis) for x in self._chunks], chunksizes=self.chunksizes)

def regular(self):
self._valid()
Expand Down
8 changes: 4 additions & 4 deletions awkward/array/indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ def flatten(self, axis=0):
self._valid()
return self._util_flatten(self._content[self._index], axis)

def pad(self, length, maskedwhen=True, clip=False):
return self._util_pad(self._content[self._index], length, maskedwhen, clip)
def pad(self, length, maskedwhen=True, clip=False, axis=0):
return self._util_pad(self._content[self._index], length, maskedwhen, clip, axis)

def regular(self):
self._valid()
Expand Down Expand Up @@ -725,8 +725,8 @@ def flatten(self, axis=0):
out[mask] = content[self._inverse[mask]]
return out

def pad(self, length, maskedwhen=True, clip=False):
return self._util_pad(self._content.dense, length, maskedwhen, clip)
def pad(self, length, maskedwhen=True, clip=False, axis=0):
return self._util_pad(self._content.dense, length, maskedwhen, clip, axis)

def regular(self):
self._valid()
Expand Down
8 changes: 7 additions & 1 deletion awkward/array/jagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,13 @@ def ready(x):
table = first.Table.named("tuple", columns1, *columns2)
return first.JaggedArray(first._starts, first._stops, table)

def pad(self, length, maskedwhen=True, clip=False):
def pad(self, length, maskedwhen=True, clip=False, axis=0):
if not self._util_isinteger(axis) or axis < 0:
raise TypeError("axis must be a non-negative integer (can't count from the end)")

if axis > 0:
return type(self).fromcounts(self.counts, self._util_pad(self._content, length, maskedwhen, clip, axis - 1))

flatstarts = self._starts.reshape(-1)

if clip:
Expand Down
4 changes: 2 additions & 2 deletions awkward/array/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ def flattentuple(self):
def flatten(self, axis=0):
return self._util_flatten(self._content[self.boolmask(maskedwhen=False)], axis)

def pad(self, length, maskedwhen=True, clip=False):
return self.copy(content=self._util_pad(self._content, length, maskedwhen, clip))
def pad(self, length, maskedwhen=True, clip=False, axis=0):
return self.copy(content=self._util_pad(self._content, length, maskedwhen, clip, axis))

def regular(self):
self._valid()
Expand Down
12 changes: 9 additions & 3 deletions awkward/array/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ def flattentuple(self):
def flatten(self, axis=0):
return self.copy(content=self._util_flatten(self._content, axis))

def pad(self, length, maskedwhen=True, clip=False):
return self.copy(content=self._util_pad(self._content, length, maskedwhen, clip))
def pad(self, length, maskedwhen=True, clip=False, axis=0):
return self.copy(content=self._util_pad(self._content, length, maskedwhen, clip, axis))

def regular(self):
return self.numpy.array(self)
Expand Down Expand Up @@ -652,7 +652,13 @@ def flatten(self, axis=0):
else:
return self.fromjagged(self.JaggedArray.fromcounts([len(content)], content))

def pad(self, length, maskedwhen=None, clip=False):
def pad(self, length, maskedwhen=None, clip=False, axis=0):
if not self._util_isinteger(axis) or axis < 0:
raise TypeError("axis must be a non-negative integer (can't count from the end)")

if axis > 0:
raise ValueError("axis too deep for StringArray")

if maskedwhen is None:
maskedwhen = ord(b" ")
elif not isinstance(maskedwhen, bytes) or not len(maskedwhen) == 1:
Expand Down
4 changes: 2 additions & 2 deletions awkward/array/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,10 +767,10 @@ def argcross(self, other, nested=False):
def flatten(self, axis=0):
raise ValueError("cannot flatten through a Table")

def pad(self, length, maskedwhen=True, clip=False):
def pad(self, length, maskedwhen=True, clip=False, axis=0):
out = self.copy(contents={})
for n, x in self._contents.items():
out[n] = self._util_pad(x, length, maskedwhen, clip)
out[n] = self._util_pad(x, length, maskedwhen, clip, axis)
return out

def regular(self):
Expand Down
4 changes: 2 additions & 2 deletions awkward/array/union.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ def flattentuple(self):
def flatten(self, axis=0):
raise NotImplementedError("flatten not yet implemented for UnionArray")

def pad(self, length, maskedwhen=True, clip=False):
def pad(self, length, maskedwhen=True, clip=False, axis=0):
self._valid()
return self.copy(contents=[self._util_pad(x, length, maskedwhen, clip) for x in self._contents])
return self.copy(contents=[self._util_pad(x, length, maskedwhen, clip, axis) for x in self._contents])

def regular(self):
self._valid()
Expand Down
4 changes: 2 additions & 2 deletions awkward/array/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ def flattentuple(self):
def flatten(self, axis=0):
return self._util_flatten(self.array, axis)

def pad(self, length, maskedwhen=True, clip=False):
return self._util_pad(self.array, length, maskedwhen, clip)
def pad(self, length, maskedwhen=True, clip=False, axis=0):
return self._util_pad(self.array, length, maskedwhen, clip, axis)

def regular(self):
return self._util_regular(self.array)
Expand Down
2 changes: 1 addition & 1 deletion awkward/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import re

__version__ = "0.12.1"
__version__ = "0.12.2"
version = __version__
version_info = tuple(re.split(r"[-\.]", __version__))

Expand Down

0 comments on commit 513f28a

Please sign in to comment.