Repository navigation
Adding depth-wise table stacking - #8939
Conversation
|
@himanshupathak21061998 - thanks, looks like a good start. Indeed cstack is quite similar to vstack, and going back #6660 the crux was about Looking at this I got an idea to make this implementation super simple. Roughly speaking, cstack is just exactly vstack, but with the final output columns reshaped correctly. So I think it might work to literally call One hint, which is a brand new feature of table. You can avoid making a new output table object with the following: This temporarily corrupts The reshape and transpose is simple for input 1-d columns, but getting it straight for input N-d columns is a little trick and that is a challenge left for you! 😄 |
|
Thanks for helping @taldcroft and sorry for the delay. Changes updated now I will start writing tests for this. |
|
@himanshupathak21061998 - if you rebase, the 32bit errors would go away. |
|
@himanshupathak21061998 - I think the whole thing can be done far more simply by fully-using the similarity of vstack and cstack. I thought you might get this independently, but here is a solution that seems to work: BTW, no need to follow the legacy pattern of having an internal |
|
Example: |
Sorry, but confusion was because of this case I thought it be like this My mistake I thought that shape with more than two elements tuple is not allowed. Next time I will try better thanks @taldcroft |
27efe0e to
ec7243e
Compare
|
|
||
| with pytest.raises(TableMergeError): | ||
| table.cstack([t2, t3]) | ||
|
|
There was a problem hiding this comment.
Is it okay that we cannot merge column having a different shape for cstack?
There was a problem hiding this comment.
Yes. This sort of stacking depends on the input column shapes being the same. The same happens for numpy vstack:
In [16]: a = np.array([[1, 2]])
In [17]: b = np.array([[3, 4]])
In [18]: np.vstack([a, b])
Out[18]:
array([[1, 2],
[3, 4]])
In [19]: b = np.array([[[3, 33], [4, 44]]])
In [20]: np.vstack([a, b])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~/git/astropy/cstack.py in <module>()
----> 1 np.vstack([a, b])
~/miniconda3/envs/astropy/lib/python3.6/site-packages/numpy/core/shape_base.py in vstack(tup)
232
233 """
--> 234 return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
235
236 def hstack(tup):
ValueError: all the input arrays must have same number of dimensions
| 1 .. 5 3 .. 7 | ||
| 2 .. 6 4 .. 8 | ||
| """ | ||
| n_rows = set(len(table) for table in tables) |
There was a problem hiding this comment.
If we enter cstack([t1, t1[1]]) it returns true should I change this behaviour. I thought it is better to ask before doing?
|
@taldcroft thanks for helping. I have performed all changes as required. |
taldcroft
left a comment
There was a problem hiding this comment.
Looking good, just a few things to change.
| """ | ||
| n_rows = set(len(table) for table in tables) | ||
| if len(n_rows) != 1: | ||
| raise ValueError('Tables length are not matching. ' |
There was a problem hiding this comment.
Use 'table lengths must all match for cstack.'.
| self._setup(operation_table_type) | ||
| t2 = self.t1.copy() | ||
| out = table.cstack([self.t1, t2['a']]) | ||
| assert out.pformat() == [' a [2] b [2] ', |
There was a problem hiding this comment.
Since the repr for a multi-dimensional column does not show all the data, you need to compare with a full version of each column. A test helper function like this might help:
def compare_cstack(tables, out):
for ii, tbl in enumerate(tables):
for name, out_col in out.columns.items():
if name in tbl.colnames:
assert np.all(tbl[name] == out[name][:, ii])
else:
assert np.all(out[name].mask[:, ii])
With this what is important is that every input data value is different. So in the setup just use the simplest values and keep incrementing, e.g.:
self.t1 = t_cls.read([' a b',
' 0. a',
' 1. b'], format='ascii')
self.t2 = t_cls.read([' a b c',
' 2. c 4',
' 3. d 5'], format='ascii')
self.t3 = t_cls({'a': [[7., 8.], [9., 10.]],
'b': [['e', 'f'], ['g', 'h']]},
names=('a', 'b'))
# and so on.
| 'b': [['foo', 'bar'], ['pez', 'sez']]}, | ||
| names=('a', 'b')) | ||
| self.t4 = t_cls(self.t1, copy=True, masked=t_cls is Table) | ||
| self.t5 = t_cls({'a': [[4., 2.], [1., 6.]], |
There was a problem hiding this comment.
One of the inputs should have a masked column with a masked element.
| t1 = self.t1 | ||
| t2 = self.t2 | ||
| t4 = self.t4 | ||
| t12 = table.cstack([t1, t2], join_type='outer') |
There was a problem hiding this comment.
The t12 and t124 tests are basically the same and not improving coverage. So just keep the t124 test.
| t2 = self.t2 | ||
| t4 = self.t4 | ||
|
|
||
| t12 = table.cstack([t1, t2], join_type='inner') |
There was a problem hiding this comment.
Same comment as before, delete the t12 test.
|
|
||
| with pytest.raises(TableMergeError): | ||
| table.cstack([t2, t3]) | ||
|
|
There was a problem hiding this comment.
Yes. This sort of stacking depends on the input column shapes being the same. The same happens for numpy vstack:
In [16]: a = np.array([[1, 2]])
In [17]: b = np.array([[3, 4]])
In [18]: np.vstack([a, b])
Out[18]:
array([[1, 2],
[3, 4]])
In [19]: b = np.array([[[3, 33], [4, 44]]])
In [20]: np.vstack([a, b])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~/git/astropy/cstack.py in <module>()
----> 1 np.vstack([a, b])
~/miniconda3/envs/astropy/lib/python3.6/site-packages/numpy/core/shape_base.py in vstack(tup)
232
233 """
--> 234 return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
235
236 def hstack(tup):
ValueError: all the input arrays must have same number of dimensions
|
@taldcroft Do I need to include more tests for it. |
63fb8ac to
db14e6b
Compare
|
@himanshupathak21061998 - I did final review and added a few commits to try to finish this up. Team effort! |
|
@taldcroft - I suppose a rebase would solve at least some of the CI issues. |
|
@bsipocz - I rebased on latest master but it is still failing to build in Travis. Any clue? |
|
I see the same issue on master, but don't yet see the cause for it. It's definitely unrelated to this PR. |
|
@taldcroft - Travis is all green now. |
Trying to fixing:#6660
Not ready for a final review but contains an implementation of cstack method for depth wise stacking.