Skip to content

Commit b678606

Browse files
authored
Use numba for vectorofvectors to_aoesa (#131)
* Use numba for vectorofvectors to_aoesa
1 parent ee8756e commit b678606

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/lgdo/types/vectorofvectors.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import awkward_pandas as akpd
1414
import numpy as np
1515
import pandas as pd
16+
from numba import jit
1617
from numpy.typing import ArrayLike, DTypeLike, NDArray
1718

1819
from .. import utils
@@ -560,18 +561,16 @@ def to_aoesa(
560561
compatible one.
561562
"""
562563
if self.ndim == 2:
563-
ak_arr = self.view_as("ak")
564-
565564
if max_len is None:
566-
max_len = int(ak.max(ak.count(ak_arr, axis=-1)))
567-
568-
nda = ak.fill_none(
569-
ak.pad_none(ak_arr, max_len, clip=True), fill_val
570-
).to_numpy(allow_missing=False)
571-
565+
lens = np.copy(self.cumulative_length)
566+
lens[1:] = lens[1:] - lens[:-1]
567+
max_len = int(np.max(lens))
568+
nda = np.full((len(self), max_len), fill_val)
572569
if preserve_dtype:
573570
nda = nda.astype(self.flattened_data.dtype, copy=False)
574571

572+
_to_aoesa(self.flattened_data.nda, self.cumulative_length.nda, nda)
573+
575574
return aoesa.ArrayOfEqualSizedArrays(nda=nda, attrs=self.getattrs())
576575

577576
raise NotImplementedError
@@ -664,3 +663,11 @@ def view_as(
664663

665664
msg = f"{library} is not a supported third-party format."
666665
raise ValueError(msg)
666+
667+
668+
@jit
669+
def _to_aoesa(flattened_array, cumulative_length, nda):
670+
prev_cl = 0
671+
for i, cl in enumerate(cumulative_length):
672+
nda[i, : (cl - prev_cl)] = flattened_array[prev_cl:cl]
673+
prev_cl = cl

0 commit comments

Comments
 (0)