-
Notifications
You must be signed in to change notification settings - Fork 372
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
Support for Multidimensional Slicing (Numpy Issue) #541
Comments
After a helpful StackOverflow question pointed out the obvious, multidimensional Numpy arrays can be sliced without the
Yea... that turned out to be as simple as it should be. This can be applied in Hy directly using get:
If support for this is to be added to the standard library, the Should another function be added to support this? |
I'm not sure if this is the best place to put this, but as the linked issues seem more general about replacing splice I'll leave it here Would it be worth introducing a reader macro for creating complex slices? I've had a look around and there doesnt seem to be an easy way of doing this in hy at the moment. After a bit of playing around I came up with
which takes e.g. |
It seems worth a reader macro to me. I think the |
On second thought, perhaps So (cut arr (, 0 #:(1 10 2) Ellipsis 3))
arr[0,1:10:2,...,3] Maybe the tuple part should be implied: (cut arr 0 #:(1 10 2) Ellipsis 3) But then the more common one-slice cut would be: (cut arr #:(1 10 2)) arr[1:10:2]
# or equivalently?
arr[slice(1,10,2)] A better option might be a Maybe we could set up an internal reader macro to use (cuts arr 0 :[1 10 2] Ellipsis 3) Also (cuts arr 0 :[1 10 2] :[] 3) |
Related question to this. How should one going about assign a value to an array slice? |
@jakirkham I believe you can do: (setv (cut mylist a b) [1 2 3]) |
Thanks @kirbyfan64 and that does work with a I was also wondering how this might be expanded to more dimensions or would this simply be a drop in replacement of |
Related question, given this
I thought maybe if I made it was a singleton tuple this would help (given
On the other hand, using
|
I believe your code is actually compiled to: a[slice(1, 3):] which doesn't make much sense. Try: (cut a 1 3) Tip: whenever something seems odd, use |
That tip really ought to be in the docs somewhere. |
I'll look into adding a note about this in the docs, but I'm not quite sure where it would go right now. |
Maybe in |
One way to do |
I have a macro |
I came up with (deftag $ [expr]
`(slice ~@(list-comp (if (= x '.) None
(= x '...) 'Ellipsis
x)
[x expr]))) It makes slice objects much more concise, e.g.,
(defmacro $ [&rest args]
`(slice ~@(list-comp (if (= x '.) None
(= x '...) 'Ellipsis
x)
[x args]))) Then Perhaps Perhaps |
I stumbled upon this https://qiita.com/riktor/items/cd914612673fe7828a8d our slicing is inadequate so someone fixed it with a macro. (warning, Japanese). But that's one possible syntax we could have. |
I think since LISP is supposed to center on LISt Processing, I think it makes sense that the Hy community should be somewhat concerned about having excellent support for Python's #1 vector and matrix processing library (NumPy, and the superset of that, Pandas). I'm happy to see this discussion is still ongoing and hasn't died quite yet. I want to stress that when coming up with a good syntax for slicing to keep the machine learning and data science oriented Pythonists happy, that we don't forget about the third parameter one can throw into a slice which can define the step (and also the direction), and also the ability to reference multiple specific indices at once (and even mix both ranged and specific indices in the same statement) and also use a mask, because those are a powerful and oft-used shorthand for a lot of Pythonists and data scientists. I didn't see this third "step" parameter referenced at all at the Japanese article, but I'm very glad to see it in an example further up in this discussion (here) and also in the example given in the original post. I also think brevity should be emphasized because this is something that would be as common for many people who might want to use Hy + Pandas as using conjunctions and articles in English. For that reason, I think the idea listed above of slice literal is really attractive. I'm a bit new to LISP (but I'm loving it!) so I don't know how possible it would be to do this in a performant way... but I would like to see a concise syntax that can handle something as complex as the below from this example of the SciPy Lectures
What seems like a decent starting point to me for dreaming up a decent syntax might be something like the following:
Of course, I haven't been super active in the Hy community, yet, so I wouldn't blame anyone for completely disregarding my thoughts on this, but I also really like Hy and thus felt the desire to provide my perspective as someone in the big data analytics world working closely with data scientists every day. |
Just want to share my implementation (based on the Japanese article),
It should work for the cases mentioned above. For example,
|
That solution worked perfectly for me, @guanyilun. Thanks! I just wrote a little extra code to provide a Tag Macro:
|
Numpy has support for multidimensional arrays like so:
It's a beautiful way of handling matrices but it brings up the issue that it takes in a
tuple
as an argument for slicing, making it difficult to use one of Numpy's greatest features in Hy.I don't think this is necessarily a bug, since it's an issue with an external library's extension of Lisp syntax, but a macro to fix this issue would be amazing.
And here I once thought Python's slicing notation was beautiful and convenient.
The text was updated successfully, but these errors were encountered: