Skip to content

Commit

Permalink
Merge branch 'main' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski authored Jan 12, 2024
2 parents 2b78eec + eb004ce commit 9d49207
Show file tree
Hide file tree
Showing 36 changed files with 420 additions and 357 deletions.
5 changes: 2 additions & 3 deletions src/awkward/_connect/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,8 @@ def handle_arrow(obj, generate_bitmasks=False, pass_empty_field=False):
if len(batches) == 0:
# create an empty array following the input schema
return form_handle_arrow(
obj.schema,
pass_empty_field=pass_empty_field,
).length_zero_array(highlevel=False)
obj.schema, pass_empty_field=pass_empty_field
).length_zero_array()
elif len(batches) == 1:
return handle_arrow(batches[0], generate_bitmasks, pass_empty_field)
else:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/_nplikes/typetracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ def _attach_report(
def typetracer_with_report(
form: ak.forms.Form, getkey: Callable[[Form, str], str]
) -> tuple[ak.contents.Content, TypeTracerReport]:
layout = form.length_zero_array(highlevel=False).to_typetracer(forget_length=True)
layout = form.length_zero_array().to_typetracer(forget_length=True)
report = TypeTracerReport()
_attach_report(layout, form, report, getkey)
return layout, report
16 changes: 16 additions & 0 deletions src/awkward/_reducers.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ class ArgMin(KernelReducer):
preferred_dtype: Final = np.int64
needs_position: Final = True

@classmethod
def _dtype_for_kernel(cls, dtype: DTypeLike) -> DTypeLike:
dtype = np.dtype(dtype)
if dtype == np.bool_:
return np.dtype(np.int8)
else:
return super()._dtype_for_kernel(dtype)

def apply(
self,
array: ak.contents.NumpyArray,
Expand Down Expand Up @@ -183,6 +191,14 @@ class ArgMax(KernelReducer):
preferred_dtype: Final = np.int64
needs_position: Final = True

@classmethod
def _dtype_for_kernel(cls, dtype: DTypeLike) -> DTypeLike:
dtype = np.dtype(dtype)
if dtype == np.bool_:
return np.dtype(np.int8)
else:
return super()._dtype_for_kernel(dtype)

def apply(
self,
array: ak.contents.NumpyArray,
Expand Down
18 changes: 11 additions & 7 deletions src/awkward/contents/bitmaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,13 +771,15 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
def to_packed(self, recursive: bool = True) -> Self:
if self._content.is_record:
next = self.to_IndexedOptionArray64()

content = next._content.to_packed()
if content.length > self._length:
content = content[: self._length]
content = (
next._content[: self._length].to_packed(True)
if recursive
else next._content[: self._length]
)

return ak.contents.IndexedOptionArray(
next._index, content, parameters=next._parameters
Expand All @@ -790,9 +792,11 @@ def to_packed(self) -> Self:
else:
mask = self._mask[:excess_length]

content = self._content.to_packed()
if content.length > self._length:
content = content[: self._length]
content = (
self._content[: self._length].to_packed(True)
if recursive
else self._content[: self._length]
)

return BitMaskedArray(
mask,
Expand Down
19 changes: 12 additions & 7 deletions src/awkward/contents/bytemaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,21 +1133,26 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
def to_packed(self, recursive: bool = True) -> Self:
if self._content.is_record:
next = self.to_IndexedOptionArray64()
content = next._content.to_packed()
if content.length > self._mask.length:
content = content[: self._mask.length]

content = (
next._content[: self._mask.length].to_packed(True)
if recursive
else next._content[: self._mask.length]
)

return ak.contents.IndexedOptionArray(
next._index, content, parameters=next._parameters
)

else:
content = self._content.to_packed()
if content.length > self._mask.length:
content = content[: self._mask.length]
content = (
self._content[: self._mask.length].to_packed(True)
if recursive
else self._content[: self._mask.length]
)

return ByteMaskedArray(
self._mask, content, self._valid_when, parameters=self._parameters
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/contents/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ def to_json(
},
)

def to_packed(self) -> Content:
def to_packed(self, recursive: bool = True) -> Content:
raise NotImplementedError

def to_list(self, behavior: dict | None = None) -> list:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/contents/emptyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
def to_packed(self, recursive: bool = True) -> Self:
return self

def _to_list(self, behavior, json_conversions):
Expand Down
10 changes: 5 additions & 5 deletions src/awkward/contents/indexedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,13 +1148,13 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
def to_packed(self, recursive: bool = True) -> Self:
if self.parameter("__array__") == "categorical":
return IndexedArray(
self._index, self._content.to_packed(), parameters=self._parameters
)
content = self._content.to_packed(True) if recursive else self._content
return IndexedArray(self._index, content, parameters=self._parameters)
else:
return self.project().to_packed()
projected = self.project()
return projected.to_packed(True) if recursive else projected

def _to_list(self, behavior, json_conversions):
if not self._backend.nplike.known_data:
Expand Down
28 changes: 14 additions & 14 deletions src/awkward/contents/indexedoptionarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ def to_ByteMaskedArray(self, valid_when):
carry = ak.index.Index(carry)

if self._content.length is not unknown_length and self._content.length == 0:
content = self._content.form.length_one_array(
backend=self._backend, highlevel=False
)._carry(carry, False)
content = self._content.form.length_one_array(backend=self._backend)._carry(
carry, False
)
else:
content = self._content._carry(carry, False)

Expand Down Expand Up @@ -1717,28 +1717,28 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
index_nplike = self._backend.index_nplike
is_none = self._index.data < 0
num_none = index_nplike.index_as_shape_item(index_nplike.count_nonzero(is_none))
if self.parameter("__array__") == "categorical" or (
self._backend.index_nplike.known_data
and self._content.length <= (self._index.length - num_none)
):
def to_packed(self, recursive: bool = True) -> Self:
if self.parameter("__array__") == "categorical":
content = self._content.to_packed(True) if recursive else self._content
return ak.contents.IndexedOptionArray(
self._index, self._content.to_packed(), parameters=self._parameters
self._index, content, parameters=self._parameters
)

else:
index_nplike = self._backend.index_nplike
original_index = self._index.data
is_none = original_index < 0
num_none = index_nplike.count_nonzero(is_none)
new_index = index_nplike.empty(self._index.length, dtype=self._index.dtype)
new_index[is_none] = -1
new_index[~is_none] = index_nplike.arange(
index_nplike.shape_item_as_index(new_index.size - num_none),
dtype=self._index.dtype,
)
projected = self.project()
return ak.contents.IndexedOptionArray(
ak.index.Index(new_index, nplike=index_nplike),
self.project().to_packed(),
ak.index.Index(new_index, nplike=self._backend.index_nplike),
projected.to_packed(True) if recursive else projected,
parameters=self._parameters,
)

Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/listarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,8 +1602,8 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
return self.to_ListOffsetArray64(True).to_packed()
def to_packed(self, recursive: bool = True) -> Self:
return self.to_ListOffsetArray64(True).to_packed(recursive)

def _to_list(self, behavior, json_conversions):
if not self._backend.nplike.known_data:
Expand Down
10 changes: 7 additions & 3 deletions src/awkward/contents/listoffsetarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,10 +2214,14 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
def to_packed(self, recursive: bool = True) -> Self:
next = self.to_ListOffsetArray64(True)
next_content = next._content[: next._offsets[-1]].to_packed()
return ListOffsetArray(next._offsets, next_content, parameters=next._parameters)
next_content = next._content[: next._offsets[-1]]
return ListOffsetArray(
next._offsets,
next_content.to_packed(True) if recursive else next_content,
parameters=next._parameters,
)

def _to_list(self, behavior, json_conversions):
if not self._backend.nplike.known_data:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/contents/numpyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
def to_packed(self, recursive: bool = True) -> Self:
return self.to_contiguous().to_RegularArray()

def _to_list(self, behavior, json_conversions):
Expand Down
6 changes: 2 additions & 4 deletions src/awkward/contents/recordarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,12 +1242,10 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
def to_packed(self, recursive: bool = True) -> Self:
return RecordArray(
[
x.to_packed()
if x.length == self._length
else x[: self._length].to_packed()
x[: self._length].to_packed(True) if recursive else x[: self._length]
for x in self._contents
],
self._fields,
Expand Down
14 changes: 6 additions & 8 deletions src/awkward/contents/regulararray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,18 +1458,16 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
def to_packed(self, recursive: bool = True) -> Self:
index_nplike = self._backend.index_nplike
length = self._length * self._size
if self._content.length == length:
content = self._content.to_packed()
else:
content = self._content[
: index_nplike.shape_item_as_index(length)
].to_packed()
content = self._content[: index_nplike.shape_item_as_index(length)]

return RegularArray(
content, self._size, self._length, parameters=self._parameters
content.to_packed(True) if recursive else content,
self._size,
self._length,
parameters=self._parameters,
)

def _to_list(self, behavior, json_conversions):
Expand Down
15 changes: 10 additions & 5 deletions src/awkward/contents/unionarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,15 +1592,18 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
tags = self._tags.raw(self._backend.nplike)
original_index = index = self._index.raw(self._backend.nplike)[: tags.shape[0]]
def to_packed(self, recursive: bool = True) -> Self:
index_nplike = self._backend.index_nplike
tags = self._tags.data
original_index = index = self._index.data[: tags.shape[0]]

contents = list(self._contents)

for tag in range(len(self._contents)):
is_tag = tags == tag
num_tag = self._backend.index_nplike.count_nonzero(is_tag)
num_tag = index_nplike.index_as_shape_item(
index_nplike.count_nonzero(is_tag)
)

if len(contents[tag]) > num_tag:
if original_index is index:
Expand All @@ -1610,7 +1613,9 @@ def to_packed(self) -> Self:
)
contents[tag] = self.project(tag)

contents[tag] = contents[tag].to_packed()
contents[tag] = (
contents[tag].to_packed(True) if recursive else contents[tag]
)

return UnionArray(
ak.index.Index8(tags, nplike=self._backend.index_nplike),
Expand Down
7 changes: 5 additions & 2 deletions src/awkward/contents/unmaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,11 @@ def continuation():
else:
raise AssertionError(result)

def to_packed(self) -> Self:
return UnmaskedArray(self._content.to_packed(), parameters=self._parameters)
def to_packed(self, recursive: bool = True) -> Self:
return UnmaskedArray(
self._content.to_packed(True) if recursive else self._content,
parameters=self._parameters,
)

def _to_list(self, behavior, json_conversions):
if not self._backend.nplike.known_data:
Expand Down
32 changes: 2 additions & 30 deletions src/awkward/forms/emptyform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
from __future__ import annotations

from collections.abc import Callable
from inspect import signature

import awkward as ak
from awkward._errors import deprecate
from awkward._meta.emptymeta import EmptyMeta
from awkward._nplikes.numpy_like import NumpyMetadata
from awkward._nplikes.shape import ShapeItem
Expand Down Expand Up @@ -59,34 +57,8 @@ def _to_dict_part(self, verbose, toplevel):
def type(self):
return ak.types.UnknownType()

def to_NumpyForm(self, *args, **kwargs):
def legacy_impl(dtype):
deprecate(
f"the `dtype` parameter in {type(self).__name__}.to_NumpyForm is deprecated, "
f"in favour of a new `primitive` argument. Pass `primitive` by keyword to opt-in to the new behavior.",
version="2.4.0",
)
return ak.forms.numpyform.from_dtype(dtype)

def new_impl(*, primitive):
return ak.forms.numpyform.NumpyForm(primitive)

dispatch_table = [
new_impl,
legacy_impl,
]
for func in dispatch_table:
sig = signature(func)
try:
bound_arguments = sig.bind(*args, **kwargs)
except TypeError:
continue
else:
return func(*bound_arguments.args, **bound_arguments.kwargs)
raise AssertionError(
f"{type(self).__name__}.to_NumpyForm accepts either the new `primitive` argument as a keyword-only "
f"argument, or the legacy `dtype` argument as positional or keyword"
)
def to_NumpyForm(self, primitive):
return ak.forms.numpyform.NumpyForm(primitive)

def _columns(self, path, output, list_indicator):
output.append(".".join(path))
Expand Down
Loading

0 comments on commit 9d49207

Please sign in to comment.