larray-project / larray

N-dimensional labelled arrays in Python
https://larray.readthedocs.io/
GNU General Public License v3.0
8 stars 6 forks source link

passing a single Session to stack leads to unexpected result #1057

Closed gdementen closed 1 year ago

gdementen commented 1 year ago
>>> mul2 = ndtest(3) * 2
>>> mul2
a  a0  a1  a2
    0   2   4
>>> mul10 = ndtest(3) * 10
>>> mul10
a  a0  a1  a2
    0  10  20
>>> sess = Session(mul2=mul2, mul10=mul10)
>>> stack(sess, 'array=mul10,mul2')
a\array  mul10  mul2
     a0      0     0
     a1      2    10
     a2      4    20

The problem is that sess is just seen as an iterable in this case, so is taken as a list, discarding its labels.

I knew it was not implemented (see discussion in #180) but I did not realize that the fact that it seemed to work can lead to silent data garbage when users use this and do not realize the labels do not correspond to the correct data anymore.

I think that fixing this is simply a matter of adding the following lines to stack:

    elif isinstance(elements, Session):
        items = elements.items()

We might want to have a default axis name in that case:

        if axes is None:
            axes = 'array'

but I am unsure about this, and if we go down that path, we should have "sensible" default labels in other places too.