0

I have an array which contains point clouds (about 100 ladar points). I need to create a set of numpy arrays as quickly as possible.

sweep = np.empty(
    shape=(len(sweep.points),),
    dtype=[
        ('point', np.float64, 3),
        ('intensity', np.float32),
        ## ..... more fields ....
    ]
)

for index, point in enumerate(sweep.points):
        sweep[index]['point'] = (point.x, point.y, point.z)
        sweep[index]['intensity'] = point.intensity
        ## ....more fields...

Writing an explicit loop is very inefficient and slow. Is there a better way to go about this?

1
  • I'm inferring that sweep is a list of points objects, where each point has attributes that you want to place fields of the array, right? Wait this won't work. After defining the empty array, sweep is now an array, not the list of objects! Commented Aug 16, 2018 at 16:45

1 Answer 1

2

It's slightly faster to use a list comprehension to format the data and pass it directly to a numpy array:

np.array([((point.x, point.y, point.z), point.intensity)
          for point in points],
         dtype=[('point', np.float64, 3),
                ('intensity', np.float32)])
Sign up to request clarification or add additional context in comments.

2 Comments

np.fromiter might be 5% faster still or so if that would matter. All it takes to change the code is to change np.array to np.fromiter, change the list comprehension to a generator expression, and add the argument count=len(points).
Actually, Is there a parallel way to doing this? (Obviously without me explictly writing parallel code)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.