Skip to content

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

Open
alixdamman opened this issue Feb 6, 2017 · 6 comments

Comments

@alixdamman
Copy link
Collaborator

Similar to numpy.concatenate

@alixdamman alixdamman self-assigned this Feb 6, 2017
@janga1997
Copy link

@alixdamman Hi, I would like to work on this.

@gdementen
Copy link
Contributor

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?

@alixdamman
Copy link
Collaborator Author

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?

@gdementen
Copy link
Contributor

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.

@alixdamman alixdamman removed their assignment Feb 8, 2017
@alixdamman alixdamman changed the title Add concatenate function Update stack in order to be able to concatenate along an axisting axis Feb 10, 2017
@gdementen gdementen changed the title Update stack in order to be able to concatenate along an axisting axis Update stack in order to be able to concatenate along an existing axis Mar 10, 2017
@gdementen
Copy link
Contributor

gdementen commented Oct 2, 2017

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.

@alixdamman alixdamman added this to the 0.32 milestone Jul 20, 2018
@alixdamman alixdamman removed this from the 0.32 milestone Aug 1, 2019
@gdementen
Copy link
Contributor

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')

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

No branches or pull requests

3 participants