Skip to content

Improve memory efficiency of get_chunk_with_margin  #2859

Open

Description

@h-mayorquin

This part of the code is really bad. It is consistently breaking a job of mine because it multiplies memory usage by five. It should be made efficient:

if need_copy:
if dtype is None:
dtype = traces_chunk.dtype
left_margin = margin
if end_frame < (length + margin):
right_margin = margin
else:
right_margin = end_frame + margin - length
if add_zeros:
traces_chunk2 = np.zeros((full_size, traces_chunk.shape[1]), dtype=dtype)
i0 = left_pad
i1 = left_pad + traces_chunk.shape[0]
traces_chunk2[i0:i1, :] = traces_chunk
if window_on_margin:
# apply inplace taper on border
taper = (1 - np.cos(np.arange(margin) / margin * np.pi)) / 2
taper = taper[:, np.newaxis]
traces_chunk2[:margin] *= taper
traces_chunk2[-margin:] *= taper[::-1]
traces_chunk = traces_chunk2
elif add_reflect_padding:
# in this case, we don't want to taper
traces_chunk = np.pad(
traces_chunk.astype(dtype, copy=False),
[(left_pad, right_pad), (0, 0)],
mode="reflect",
)
else:
# we need a copy to change the dtype
traces_chunk = np.asarray(traces_chunk, dtype=dtype)

Judicious use of the out property can improve this.

Activity

  1. added
    core
    performance
    on May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions