question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TrackedArray vs numpy array

See original GitHub issue

Hi,

I did some processing for a Trimesh object and then got the new vertices viamyverts = mesh.vertices. I then did some subsequent computations on myverts which went fine. However at some point I happened to notice that myverts is actually of type TrackedArray. Converting it to a simple numpy array I got a speedup by a factor of 4.

This is not really a big issue, but I was wondering if there should be a method to return the vertices as a simple numpy array, or maybe at least the docs should mention that using the property will yield a TrackedArray.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mikedhcommented, Nov 1, 2019

Oh interesting. The TrackedArray is the thing that enables caching, which provides huge speedups in applications accessing properties multiple times so it’s kind of necessary for vertices and faces. If this turns into an ongoing issue, I wonder if we could make it so copies of a TrackedArray revert to ndarray type.

That being said, in some quick and dirty profiling I couldn’t see any speed difference:

In [1]: import numpy as np                                                                                                                 

In [2]: import trimesh                                                                                                                     

In [3]: m = trimesh.creation.icosphere()                                                                                                   

In [5]: v = m.vertices                                                                                                                     

In [6]: n = m.vertices.view(np.ndarray)                                                                                                    

In [7]: n                                                                                                                                  
Out[7]: 
array([[-0.52573111,  0.85065081,  0.        ],
       [ 0.52573111,  0.85065081,  0.        ],
       [-0.52573111, -0.85065081,  0.        ],
       ...,
       [ 0.91298249, -0.39960705, -0.08232358],
       [ 0.91298249,  0.39960705,  0.08232358],
       [ 0.91298249,  0.39960705, -0.08232358]])

In [8]: v                                                                                                                                  
Out[8]: 
TrackedArray([[-0.52573111,  0.85065081,  0.        ],
              [ 0.52573111,  0.85065081,  0.        ],
              [-0.52573111, -0.85065081,  0.        ],
              ...,
              [ 0.91298249, -0.39960705, -0.08232358],
              [ 0.91298249,  0.39960705,  0.08232358],
              [ 0.91298249,  0.39960705, -0.08232358]])

In [9]: %timeit np.linalg.norm(v, axis=1)                                                                                                  
31.6 µs ± 978 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [10]: %timeit np.linalg.norm(n, axis=1)                                                                                                 
30.7 µs ± 1.08 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [11]: %timeit np.dot(n, n.T)                                                                                                            
1.81 ms ± 43.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [12]: %timeit np.dot(v, v.T)                                                                                                            
1.84 ms ± 32.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

1reaction
jjnurminencommented, Nov 7, 2019

Ok. It’s not really a problem for me - just wanted to make sure there wasn’t a bug or something. I think my use case is kind of uncommon anyway, since I’m looping through a subset of vertices and modifying it in iterative fashion.

Thanks for the package BTW - it already saved me a lot of time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

trimesh.caching — trimesh 3.17.1 documentation
Properly subclass a numpy ndarray to track changes. Avoids some pitfalls of subclassing by forcing contiguous arrays and does a view into a...
Read more >
Tracking signs in numpy array - python - Stack Overflow
Use sign function: np.sign(x) != np.sign(y). If you want the coordinates, you can wrap that around np.where or np.nonzero
Read more >
numpy.asarray — NumPy v1.24 Manual
Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples...
Read more >
trimesh/caching.py - ynic-debian
Properly subclass a numpy ndarray to track changes. ... arrays, and does a view into a TrackedArray. 37. 38. Parameters. 39. ------------.
Read more >
batch must contain tensors, numpy arrays, numbers, dicts or lists
I just use the native example of PyTorch to load Cifar10 and Cifar100 datasets, but it returns this. default_collate: batch must contain tensors ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found