Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to get a range of sub-arrays similar to numpy's x[a:b] #218

Open
jneen opened this issue Jun 18, 2024 · 4 comments
Open

Way to get a range of sub-arrays similar to numpy's x[a:b] #218

jneen opened this issue Jun 18, 2024 · 4 comments

Comments

@jneen
Copy link

jneen commented Jun 18, 2024

Hey there! Thanks for this software.

I'm attempting to do some audio processing, which involves a lot of splitting a big 2d array into smaller chunks. My wave is of shape [big_number, 2], for 2-channel stereo audio, and I'm attempting to slice this to get a time range. I would expect, for example, wave[0...1024] to result in an array of shape [1024, 2], but it seems to treat the whole thing as flat and just return an array of shape [1024].

I had a search through the docs and examples and couldn't find anything like this - is there a way to take this kind of slice or do i have to math out the size and reshape afterwards?

@jneen
Copy link
Author

jneen commented Jun 18, 2024

Concrete example:

[10] pry(Spectrum)> @samples
=> Numo::SFloat#shape=[6513231,2]
[[0, 3.05185e-05], 
 [3.05185e-05, 6.1037e-05], 
 [6.1037e-05, 6.1037e-05], 
 [3.05185e-05, 0], 
 [0, -3.05185e-05], 
 [0, -3.05185e-05], 
 [0, -3.05185e-05], 
 [0, 0], 
 [3.05185e-05, 3.05185e-05], 
 [6.1037e-05, 3.05185e-05], 
 [9.15555e-05, 3.05185e-05], 
 [6.1037e-05, 3.05185e-05], 
 [-6.1037e-05, -6.1037e-05], 
 [-0.000152593, -9.15555e-05], 
 [-0.000152593, -9.15555e-05], 
 [-0.000122074, -6.1037e-05], 
 [0, 3.05185e-05], 
 [6.1037e-05, 3.05185e-05], 
 [0, -0.000152593], 
 [-3.05185e-05, -0.000244148], 
 ...
[11] pry(Spectrum)> @samples[0...1024]
=> Numo::SFloat(view)#shape=[1024]
[0, 3.05185e-05, 3.05185e-05, 6.1037e-05, 6.1037e-05, 6.1037e-05, ...]

@jneen
Copy link
Author

jneen commented Jun 18, 2024

I think the thing I would want (and how normal ruby arrays work) is more similar to this:

[4] pry(Spectrum)> @samples[0...2048].reshape(1024, 2)
=> Numo::SFloat#shape=[1024,2]
[[0, 3.05185e-05], 
 [3.05185e-05, 6.1037e-05], 
 [6.1037e-05, 6.1037e-05], 
 [3.05185e-05, 0], 
 [0, -3.05185e-05], 
 [0, -3.05185e-05], 
 [0, -3.05185e-05], 
 [0, 0], 
 [3.05185e-05, 3.05185e-05], 
 [6.1037e-05, 3.05185e-05], 
 [9.15555e-05, 3.05185e-05], 
 [6.1037e-05, 3.05185e-05], 
 [-6.1037e-05, -6.1037e-05], 
 [-0.000152593, -9.15555e-05], 
 [-0.000152593, -9.15555e-05], 
 [-0.000122074, -6.1037e-05], 
 [0, 3.05185e-05], 
 [6.1037e-05, 3.05185e-05], 
 [0, -0.000152593], 
 [-3.05185e-05, -0.000244148], 
 ...

But this feels like it leaks the abstraction of shapes a little bit and forces me to think of it as a totally flattened array.

@kojix2
Copy link
Contributor

kojix2 commented Jun 19, 2024

Hi @jneen
I'm not very familiar with NumPy, so I don't know if my answer is what you expect, but I think you can use true.

import numpy as np

x = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12]])

x[1:3]
# array([[ 5,  6,  7,  8],
#       [ 9, 10, 11, 12]])
require 'numo/narray'

x = Numo::Int32[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]

x[1...3, true]
# x[1..2, true]

# Numo::Int32(view)#shape=[2,4]
# [[5, 6, 7, 8], 
#  [9, 10, 11, 12]]

I'm glad you're interested in NArray.

@jneen
Copy link
Author

jneen commented Jun 19, 2024

Oh! That's... certainly an interesting way to do it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants