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 #7 from scikit-hep/finish-implementation
Browse files Browse the repository at this point in the history
Finish implementation
  • Loading branch information
jpivarski authored Oct 12, 2018
2 parents cbde2d2 + 212a2b7 commit a07178d
Show file tree
Hide file tree
Showing 22 changed files with 2,273 additions and 1,514 deletions.
7 changes: 3 additions & 4 deletions awkward/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from awkward.array.chunked import ChunkedArray, AppendableArray
from awkward.array.indexed import IndexedArray, ByteIndexedArray, IndexedMaskedArray
from awkward.array.indexed import IndexedArray, ByteIndexedArray, SparseArray
from awkward.array.jagged import JaggedArray, ByteJaggedArray
from awkward.array.masked import MaskedArray, BitMaskedArray
from awkward.array.masked import MaskedArray, BitMaskedArray, IndexedMaskedArray
from awkward.array.objects import Methods, ObjectArray
from awkward.array.sparse import SparseArray
from awkward.array.table import Table
from awkward.array.union import UnionArray
from awkward.array.virtual import VirtualArray
Expand All @@ -43,4 +42,4 @@
# convenient access to the version number
from awkward.version import __version__

__all__ = ["ChunkedArray", "AppendableArray", "IndexedArray", "ByteIndexedArray", "IndexedMaskedArray", "JaggedArray", "ByteJaggedArray", "MaskedArray", "BitMaskedArray", "Methods", "ObjectArray", "SparseArray", "Table", "UnionArray", "VirtualArray", "fromiter", "__version__"]
__all__ = ["ChunkedArray", "AppendableArray", "IndexedArray", "ByteIndexedArray", "SparseArray", "JaggedArray", "ByteJaggedArray", "MaskedArray", "BitMaskedArray", "IndexedMaskedArray", "Methods", "ObjectArray", "Table", "UnionArray", "VirtualArray", "fromiter", "__version__"]
90 changes: 87 additions & 3 deletions awkward/array/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import types

import awkward.util

class AwkwardArray(awkward.util.NDArrayOperatorsMixin):
def __array__(self, *args, **kwargs):
raise Exception("{0} {1}".format(args, kwargs))
# hitting this function is usually undesirable; uncomment to search for performance bugs
# raise Exception("{0} {1}".format(args, kwargs))
return awkward.util.numpy.array(self, *args, **kwargs)

def __iter__(self):
for i in range(len(self)):
Expand Down Expand Up @@ -65,8 +69,8 @@ def __bool__(self):
__nonzero__ = __bool__

@property
def jshape(self):
return self.type.jshape
def size(self):
return len(self)

def tolist(self):
import awkward.array.table
Expand All @@ -80,6 +84,9 @@ def tolist(self):
out.append(self._try_tolist(x))
return out

def _valid(self):
pass

def valid(self):
try:
self._valid()
Expand All @@ -88,6 +95,40 @@ def valid(self):
else:
return True

def _argfields(self, function):
if not isinstance(function, types.FunctionType):
raise TypeError("function (or lambda) required")

if (isinstance(function, types.FunctionType) and function.__code__.co_argcount == 1) or isinstance(self._content, awkward.util.numpy.ndarray):
return None, None

required = function.__code__.co_varnames[:function.__code__.co_argcount]
has_varargs = (function.__code__.co_flags & 0x04) != 0
has_kwargs = (function.__code__.co_flags & 0x08) != 0

args = []
kwargs = {}

order = self.columns

for i, n in enumerate(required):
if n in self._content:
args.append(n)
elif str(i) in self._content:
args.append(str(i))
else:
args.append(order[i])

if has_varargs:
while str(i) in self._content:
args.append(str(i))
i += 1

if has_kwargs:
kwargs = [n for n in self._content if n not in required]

return args, kwargs

def apply(self, function):
args, kwargs = self._argfields(function)
if args is None and kwargs is None:
Expand Down Expand Up @@ -123,3 +164,46 @@ def minby(self, function):
args = tuple(self[n] for n in args)
kwargs = dict((n, self[n]) for n in kwargs)
return self[function(*args, **kwargs).argmin()]

class AwkwardArrayWithContent(AwkwardArray):
def __setitem__(self, where, what):
if isinstance(where, awkward.util.string):
self._content[where] = what

elif awkward.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))

def __delitem__(self, where):
if isinstance(where, awkward.util.string):
del self._content[where]
elif awkward.util.isstringslice(where):
for x in where:
del self._content[x]
else:
raise TypeError("invalid index for removing column from Table: {0}".format(where))

@property
def base(self):
if isinstance(self._content, awkward.util.numpy.ndarray):
raise TypeError("array has no Table, and hence no base")
return self._content.base

@property
def columns(self):
if isinstance(self._content, awkward.util.numpy.ndarray):
return []
else:
return self._content.columns

@property
def allcolumns(self):
if isinstance(self._content, awkward.util.numpy.ndarray):
return []
else:
return self._content.allcolumns
Loading

0 comments on commit a07178d

Please sign in to comment.