Halyn Betchkal
Halyn Betchkal

Reputation: 242

Pythonic way to join proximal segments in a 2D numpy array?

I've got a 2D numpy array that represents fragmented time segments as begin/end times. I've devised a way to join the segments when they are proximal (less than some minimum gap length):

def join_proximal_segments(original_segments, min_gap):

  begins = np.array([original_segments.T[0, 0]])
  ends = np.array([])

  for i in np.arange(len(original_segments)-1):

    if((original_segments.T[0, i+1] - original_segments.T[1, i]) > min_gap):

      begins = np.append(begins, original_segments.T[1, i])
      ends = np.append(ends, original_segments.T[0, i+1])

  ends = np.append(ends, original_segments.T[1, -1])

  joined_segments = np.array([begins, ends]).T

  return joined_segments

It seems quite inelegant. Is there a more direct way to approach this problem using numpy?

Edit: an example array

detections = np.array([[2, 60],[62, 78],[97, 105],[255, 340],[343, 347]])

Upvotes: 1

Views: 60

Answers (1)

tenhjo
tenhjo

Reputation: 4537

I am not sure I understand your problem fully but I hope this example helps you enough to figure out the vectorisation of your problem:

import numpy as np

min_gap = 10
original_segments = np.array([[  2,  60],
                              [ 62,  78],
                              [ 97, 105],
                              [255, 340],
                              [343, 347]])


def join_proximal_segments(original_segments, min_gap):
    diff = original_segments[1:, 0] - original_segments[:-1, 1]
    diff_bool = diff > min_gap

    joined_segments = np.empty((sum(diff_bool)+1, 2),
                                 dtype=original_segments.dtype)
    # beginnings
    joined_segments[0, 0] = original_segments[0, 0]
    joined_segments[1:, 0] = original_segments[:-1, 1][diff_bool]
    # ends
    joined_segments[:-1, 1] = original_segments[1:, 0][diff_bool]
    joined_segments[-1, 1] = original_segments[-1, 1]

    return joined_segments


print(join_proximal_segments(original_segments, min_gap))
# [[  2  97]
#  [ 78 255]
#  [105 347]]

Upvotes: 1

Related Questions