-
Notifications
You must be signed in to change notification settings - Fork 6
Update stack in order to be able to concatenate along an existing axis #90
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
Comments
@alixdamman Hi, I would like to work on this. |
Wouldn't that duplicate stack functionality? The current stack() can only create a new axis, but I would like to generalize it to work also along an existing axis. Or do you think it's better to use another function for that? |
Numpy provides 2 functions: stack and concatenate. stack create a new axis and concatenate do what it is suppose to do (along an existing axis). The question is, do we want to stay close as possible to Numpy? |
I know, and honestly, I am unsure what to do. Yes, I want to be as close as possible to numpy, but only when having named dimensions does not change the game. For example, I do not intend to ever implement vstack and hstack. Whether this is such a case or not is very debatable. IIRC both xarray and Pandas have a single function for both cases, so I would lean towards that but I can be convinced the other way. |
In case I am hit by a bus, see https://github.com/gdementen/larray/tree/concat for a WIP implementation. Still need to see whether or not we can merge stack & concat. |
FWIW, torch uses two functions: https://pytorch.org/docs/stable/generated/torch.stack.html and https://pytorch.org/docs/stable/generated/torch.cat.html#torch.cat But the fact that we deprecated .extend in favour of .append which now works whether the axis exists or not makes me lean to using one "combined" function... Possibly even name it "combine" 😉. I think I have proof-of-concept code (or maybe only syntax experiments?) somewhere in one of my branches, which uses dicts for new axes and lists for existing axes. Assuming: >>> arr1 = ndtest(3)
>>> arr2 = ndtest('a=a3,a4') + 3
>>> arr3 = ndtest(3) + 5
>>> arr4 = ndtest('a=a3,a4') + 8 We would have: >>> stack({'b1': [arr1, arr2],
... 'b2': [arr3, arr4]}, ['b', 'a'])
a\b b1 b2
a0 0 5
a1 1 6
a2 2 7
a3 3 8
a4 4 9
>>> # or
>>> stack([{'b1': arr1, 'b2': arr3},
... {'b1': arr2, 'b2': arr4}], ['a', 'b'])
a\b b1 b2
a0 0 5
a1 1 6
a2 2 7
a3 3 8
a4 4 9 Compare this with the existing: >>> stack({'b1': arr1.append('a', arr2),
... 'b2': arr3.append('a', arr4)}, 'b')
a\b b1 b2
a0 0 5
a1 1 6
a2 2 7
a3 3 8
a4 4 9 Or an hypothetical cat function (below is a inefficient 1D version): >>> def cat(*arrays, axes=None):
... res = arrays[0]
... for a in arrays[1:]:
... res = res.append(axes, a)
... return res
...
>>> stack({'b1': cat(arr1, arr2, axes='a'),
... 'b2': cat(arr3, arr4, axes='a')}, 'b') Or possibly: >>> def cat(arrays, axes=None):
... res = arrays[0]
... for a in arrays[1:]:
... res = res.append(axes, a)
... return res
>>> stack({'b1': cat((arr1, arr2), 'a'),
... 'b2': cat((arr3, arr4), 'a')}, 'b') |
Similar to numpy.concatenate
The text was updated successfully, but these errors were encountered: