@@ -108,6 +108,134 @@ def test_from_numpy_sparse(self):
108
108
t = Table .from_numpy (domain , sp .bsr_matrix (x ))
109
109
self .assertTrue (sp .isspmatrix_csr (t .X ))
110
110
111
+ @staticmethod
112
+ def _new_table (attrs , classes , metas , s ):
113
+ def nz (x ): # pylint: disable=invalid-name
114
+ return x if x .size else np .empty ((5 , 0 ))
115
+
116
+ domain = Domain (attrs , classes , metas )
117
+ X = np .arange (s , s + len (attrs ) * 5 ).reshape (5 , - 1 )
118
+ Y = np .arange (100 + s , 100 + s + len (classes ) * 5 )
119
+ if len (classes ) > 1 :
120
+ Y = Y .reshape (5 , - 1 )
121
+ M = np .arange (200 + s , 200 + s + len (metas ) * 5 ).reshape (5 , - 1 )
122
+ return Table .from_numpy (domain , nz (X ), nz (Y ), nz (M ))
123
+
124
+ def test_concatenate_horizontal (self ):
125
+ a , b , c , d , e , f , g = map (ContinuousVariable , "abcdefg" )
126
+
127
+ # Common case; one class, no empty's
128
+ tab1 = self ._new_table ((a , b ), (c , ), (d , ), 0 )
129
+ tab2 = self ._new_table ((e , ), (), (f , g ), 1000 )
130
+ joined = Table .concatenate ((tab1 , tab2 ), axis = 1 )
131
+ domain = joined .domain
132
+ self .assertEqual (domain .attributes , (a , b , e ))
133
+ self .assertEqual (domain .class_vars , (c , ))
134
+ self .assertEqual (domain .metas , (d , f , g ))
135
+ np .testing .assert_equal (joined .X , np .hstack ((tab1 .X , tab2 .X )))
136
+ np .testing .assert_equal (joined .Y , tab1 .Y )
137
+ np .testing .assert_equal (joined .metas , np .hstack ((tab1 .metas , tab2 .metas )))
138
+
139
+ # One part of one table is empty
140
+ tab1 = self ._new_table ((a , b ), (), (), 0 )
141
+ tab2 = self ._new_table ((), (), (c , ), 1000 )
142
+ joined = Table .concatenate ((tab1 , tab2 ), axis = 1 )
143
+ domain = joined .domain
144
+ self .assertEqual (domain .attributes , (a , b ))
145
+ self .assertEqual (domain .class_vars , ())
146
+ self .assertEqual (domain .metas , (c , ))
147
+ np .testing .assert_equal (joined .X , np .hstack ((tab1 .X , tab2 .X )))
148
+ np .testing .assert_equal (joined .metas , np .hstack ((tab1 .metas , tab2 .metas )))
149
+
150
+ # Multiple classes, two empty parts are merged
151
+ tab1 = self ._new_table ((a , b ), (c , ), (), 0 )
152
+ tab2 = self ._new_table ((), (d , ), (), 1000 )
153
+ joined = Table .concatenate ((tab1 , tab2 ), axis = 1 )
154
+ domain = joined .domain
155
+ self .assertEqual (domain .attributes , (a , b ))
156
+ self .assertEqual (domain .class_vars , (c , d ))
157
+ self .assertEqual (domain .metas , ())
158
+ np .testing .assert_equal (joined .X , np .hstack ((tab1 .X , tab2 .X )))
159
+ np .testing .assert_equal (joined .Y , np .vstack ((tab1 .Y , tab2 .Y )).T )
160
+
161
+ # Merging of attributes and selection of weights
162
+ tab1 = self ._new_table ((a , b ), (c , ), (), 0 )
163
+ tab1 .attributes = dict (a = 5 , b = 7 )
164
+ tab2 = self ._new_table ((d , ), (e , ), (), 1000 )
165
+ tab2 .W = np .arange (5 )
166
+ tab3 = self ._new_table ((f , g ), (), (), 2000 )
167
+ tab3 .attributes = dict (a = 1 , c = 4 )
168
+ tab3 .W = np .arange (5 , 10 )
169
+ joined = Table .concatenate ((tab1 , tab2 , tab3 ), axis = 1 )
170
+ domain = joined .domain
171
+ self .assertEqual (domain .attributes , (a , b , d , f , g ))
172
+ self .assertEqual (domain .class_vars , (c , e ))
173
+ self .assertEqual (domain .metas , ())
174
+ np .testing .assert_equal (joined .X , np .hstack ((tab1 .X , tab2 .X , tab3 .X )))
175
+ np .testing .assert_equal (joined .Y , np .vstack ((tab1 .Y , tab2 .Y )).T )
176
+ self .assertEqual (joined .attributes , dict (a = 5 , b = 7 , c = 4 ))
177
+ np .testing .assert_equal (joined .ids , tab1 .ids )
178
+ np .testing .assert_equal (joined .W , tab2 .W )
179
+
180
+ # Raise an exception when no tables are given
181
+ self .assertRaises (ValueError , Table .concatenate , (), axis = 1 )
182
+
183
+ def test_concatenate_invalid_axis (self ):
184
+ self .assertRaises (ValueError , Table .concatenate , (), axis = 2 )
185
+
186
+ def test_with_column (self ):
187
+ a , b , c , d , e , f , g = map (ContinuousVariable , "abcdefg" )
188
+ col = np .arange (9 , 14 )
189
+ colr = col .reshape (5 , - 1 )
190
+ tab = self ._new_table ((a , b , c ), (d , ), (e , f ), 0 )
191
+
192
+ # Add to attributes
193
+ tabw = tab .add_column (g , np .arange (9 , 14 ))
194
+ self .assertEqual (tabw .domain .attributes , (a , b , c , g ))
195
+ np .testing .assert_equal (tabw .X , np .hstack ((tab .X , colr )))
196
+ np .testing .assert_equal (tabw .Y , tab .Y )
197
+ np .testing .assert_equal (tabw .metas , tab .metas )
198
+
199
+ # Add to metas
200
+ tabw = tab .add_column (g , np .arange (9 , 14 ), to_metas = True )
201
+ self .assertEqual (tabw .domain .metas , (e , f , g ))
202
+ np .testing .assert_equal (tabw .X , tab .X )
203
+ np .testing .assert_equal (tabw .Y , tab .Y )
204
+ np .testing .assert_equal (tabw .metas , np .hstack ((tab .metas , colr )))
205
+
206
+ # Add to empty attributes
207
+ tab = self ._new_table ((), (d , ), (e , f ), 0 )
208
+ tabw = tab .add_column (g , np .arange (9 , 14 ))
209
+ self .assertEqual (tabw .domain .attributes , (g , ))
210
+ np .testing .assert_equal (tabw .X , colr )
211
+ np .testing .assert_equal (tabw .Y , tab .Y )
212
+ np .testing .assert_equal (tabw .metas , tab .metas )
213
+
214
+ # Add to empty metas
215
+ tab = self ._new_table ((a , b , c ), (d , ), (), 0 )
216
+ tabw = tab .add_column (g , np .arange (9 , 14 ), to_metas = True )
217
+ self .assertEqual (tabw .domain .metas , (g , ))
218
+ np .testing .assert_equal (tabw .X , tab .X )
219
+ np .testing .assert_equal (tabw .Y , tab .Y )
220
+ np .testing .assert_equal (tabw .metas , colr )
221
+
222
+ # Pass values as a list
223
+ tab = self ._new_table ((a , ), (d , ), (e , f ), 0 )
224
+ tabw = tab .add_column (g , [4 , 2 , - 1 , 2 , 5 ])
225
+ self .assertEqual (tabw .domain .attributes , (a , g ))
226
+ np .testing .assert_equal (
227
+ tabw .X , np .array ([[0 , 1 , 2 , 3 , 4 ], [4 , 2 , - 1 , 2 , 5 ]]).T )
228
+
229
+ # Add non-primitives as metas; join `float` and `object` to `object`
230
+ tab = self ._new_table ((a , ), (d , ), (e , f ), 0 )
231
+ t = StringVariable ("t" )
232
+ tabw = tab .add_column (t , list ("abcde" ))
233
+ self .assertEqual (tabw .domain .attributes , (a , ))
234
+ self .assertEqual (tabw .domain .metas , (e , f , t ))
235
+ np .testing .assert_equal (
236
+ tabw .metas ,
237
+ np .hstack ((tab .metas , np .array (list ("abcde" )).reshape (5 , - 1 ))))
238
+
111
239
112
240
class TestTableFilters (unittest .TestCase ):
113
241
def setUp (self ):
0 commit comments