Skip to content

Commit

Permalink
tiny perf improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Dec 12, 2024
1 parent f35500e commit 154ee8f
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions znh5md/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ def concatenate_varying_shape_arrays(
"""

# Determine the maximum shape along all dimensions
maxshape = list(values[0].shape)
for value in values[1:]:
maxshape = [max(a, b) for a, b in zip(maxshape, value.shape)]
shapes = np.array([value.shape for value in values])
maxshape = tuple(np.max(shapes, axis=0))

# Add the batch dimension
maxshape = (len(values), *maxshape)

# Create an array filled with the fillvalue
# Create the array filled with the fillvalue
dataset = np.full(maxshape, fillvalue, dtype=dtype)

# Insert each value into the dataset
# Get the slices for each value and assign them all at once
for i, value in enumerate(values):
# Create slices for each dimension of the current value
slices = tuple(slice(0, dim) for dim in value.shape)
dataset[(i,) + slices] = value
slices = (i,) + tuple(slice(0, dim) for dim in value.shape)
dataset[slices] = value

return dataset

Expand Down

0 comments on commit 154ee8f

Please sign in to comment.