Mercurial > pylearn
comparison test_dataset.py @ 100:574f4db76022
restructuring and added test
author | Frederic Bastien <bastienf@iro.umontreal.ca> |
---|---|
date | Tue, 06 May 2008 14:01:22 -0400 |
parents | 352910e0dbf5 |
children | 4537ac630348 |
comparison
equal
deleted
inserted
replaced
99:a8da709eb6a9 | 100:574f4db76022 |
---|---|
119 assert len(minibatch)==2 | 119 assert len(minibatch)==2 |
120 assert len(minibatch[0])==3 | 120 assert len(minibatch[0])==3 |
121 assert len(minibatch[1])==3 | 121 assert len(minibatch[1])==3 |
122 assert (minibatch[0][:,0:3:2]==minibatch[1]).all() | 122 assert (minibatch[0][:,0:3:2]==minibatch[1]).all() |
123 i+=1 | 123 i+=1 |
124 #assert i==#??? What shoud be the value? | 124 #assert i==#??? What shoud be the value? #option for the rest. |
125 print i | 125 print i |
126 del minibatch,i | 126 del minibatch,i |
127 i=0 | 127 i=0 |
128 for minibatch in ds.minibatches(['x','y'], minibatch_size=3): | 128 for minibatch in ds.minibatches(['x','y'], minibatch_size=3): |
129 assert len(minibatch)==2 | 129 assert len(minibatch)==2 |
177 i+=1 | 177 i+=1 |
178 assert i==6 | 178 assert i==6 |
179 del x,y,i,id | 179 del x,y,i,id |
180 | 180 |
181 i=0 | 181 i=0 |
182 for x,y in ds.minibatches(['x','y'],n_batches=10,minibatch_size=3,offset=4): | 182 m=ds.minibatches(['x','y'],n_batches=10,minibatch_size=3,offset=4) # bugged??? |
183 for x,y in m: | |
183 assert len(x)==3 | 184 assert len(x)==3 |
184 assert len(y)==3 | 185 assert len(y)==3 |
185 for id in range(3): | 186 for id in range(3): |
186 assert (numpy.append(x[id],y[id])==a[i+4]).all() | 187 assert (numpy.append(x[id],y[id])==a[(i+4)%a.shape[0]]).all() |
187 i+=1 | 188 i+=1 |
188 assert i==6 | 189 assert i==m.n_batches*m.minibatch_size |
189 del x,y,i,id | 190 del x,y,i,id |
190 | 191 |
191 | 192 |
192 def test_ds_iterator(array,iterator1,iterator2,iterator3): | 193 def test_ds_iterator(array,iterator1,iterator2,iterator3): |
193 i=0 | 194 i=0 |
209 assert y==array[i][3] | 210 assert y==array[i][3] |
210 assert (z==array[i][0:3:2]).all() | 211 assert (z==array[i][0:3:2]).all() |
211 assert (numpy.append(x,y)==array[i]).all() | 212 assert (numpy.append(x,y)==array[i]).all() |
212 i+=1 | 213 i+=1 |
213 assert i==len(ds) | 214 assert i==len(ds) |
215 | |
216 def test_getitem(array,ds): | |
217 | |
218 def test_ds(orig,ds,index): | |
219 i=0 | |
220 assert len(ds)==len(index) | |
221 for x,z,y in ds('x','z','y'): | |
222 assert (orig[index[i]]['x']==array[index[i]][:3]).all() | |
223 assert (orig[index[i]]['x']==x).all() | |
224 assert orig[index[i]]['y']==array[index[i]][3] | |
225 assert orig[index[i]]['y']==y | |
226 assert (orig[index[i]]['z']==array[index[i]][0:3:2]).all() | |
227 assert (orig[index[i]]['z']==z).all() | |
228 i+=1 | |
229 del i | |
230 ds[0] | |
231 if len(ds)>2: | |
232 ds[:1] | |
233 ds[1:1] | |
234 ds[1:1:1] | |
235 if len(ds)>5: | |
236 ds[[1,2,3]] | |
237 for x in ds: | |
238 pass | |
239 | |
240 assert have_raised("ds['h']") # h is not defined... | |
241 assert have_raised("ds["+str(len(ds))+"]") # h is not defined... | |
242 assert have_raised("ds["+str(len(ds))+"]") # h is not defined... | |
243 assert have_raised("ds[['h']]") # h is not defined... | |
244 | |
245 #ds[:n] returns a dataset with the n first examples. | |
246 ds2=ds[:3] | |
247 assert isinstance(ds2,DataSet) | |
248 test_ds(ds,ds2,index=[0,1,2]) | |
249 | |
250 #ds[i1:i2:s]# returns a ds with the examples i1,i1+s,...i2-s. | |
251 ds2=ds[1:7:2] | |
252 assert isinstance(ds2,DataSet) | |
253 test_ds(ds,ds2,[1,3,5]) | |
254 | |
255 #ds[i] | |
256 ds2=ds[5] | |
257 assert isinstance(ds2,Example) | |
258 | |
259 #ds[[i1,i2,...in]]# returns a ds with examples i1,i2,...in. | |
260 ds2=ds[[4,7,2,8]] | |
261 assert isinstance(ds2,DataSet) | |
262 test_ds(ds,ds2,[4,7,2,8]) | |
263 #ds[i1,i2,...]# should we accept????no syntax of numpy.array | |
264 | |
265 #ds[fieldname]# an iterable over the values of the field fieldname across | |
266 #the ds (the iterable is obtained by default by calling valuesVStack | |
267 #over the values for individual examples). | |
268 | |
269 #ds.<property># returns the value of a property associated with | |
270 #the name <property>. The following properties should be supported: | |
271 # - 'description': a textual description or name for the ds | |
272 # - 'fieldtypes': a list of types (one per field) | |
273 #* ds1 | ds2 | ds3 == ds.hstack([ds1,ds2,ds3])#???? | |
274 #* ds1 & ds2 & ds3 == ds.vstack([ds1,ds2,ds3])#???? | |
275 | |
214 | 276 |
215 print "test_ArrayDataSet" | 277 print "test_ArrayDataSet" |
216 a = numpy.random.rand(10,4) | 278 a = numpy.random.rand(10,4) |
217 ds = ArrayDataSet(a,{'x':slice(3),'y':3,'z':[0,2]})###???tuple not tested | 279 ds = ArrayDataSet(a,{'x':slice(3),'y':3,'z':[0,2]})###???tuple not tested |
218 ds = ArrayDataSet(a,LookupList(['x','y','z'],[slice(3),3,[0,2]]))###???tuple not tested | 280 ds = ArrayDataSet(a,LookupList(['x','y','z'],[slice(3),3,[0,2]]))###???tuple not tested |
219 assert len(ds)==10 | 281 assert len(ds)==10 |
220 #assert ds==a? should this work? | 282 #assert ds==a? should this work? |
221 | 283 |
222 test_iterate_over_examples(a, ds) | 284 test_iterate_over_examples(a, ds) |
223 | 285 test_getitem(a, ds) |
224 | 286 |
225 # - for val1,val2,val3 in dataset(field1, field2,field3): | 287 # - for val1,val2,val3 in dataset(field1, field2,field3): |
226 test_ds_iterator(a,ds('x','y'),ds('y','z'),ds('x','y','z')) | 288 test_ds_iterator(a,ds('x','y'),ds('y','z'),ds('x','y','z')) |
227 | 289 |
228 assert have_raised("ds['h']") # h is not defined... | |
229 assert have_raised("ds[['h']]") # h is not defined... | |
230 | 290 |
231 assert len(ds.fields())==3 | 291 assert len(ds.fields())==3 |
232 for field in ds.fields(): | 292 for field in ds.fields(): |
233 for field_value in field: # iterate over the values associated to that field for all the ds examples | 293 for field_value in field: # iterate over the values associated to that field for all the ds examples |
234 pass | 294 pass |
239 for field_examples in ds.fields(): | 299 for field_examples in ds.fields(): |
240 for example_value in field_examples: | 300 for example_value in field_examples: |
241 pass | 301 pass |
242 | 302 |
243 assert ds == ds.fields().examples() | 303 assert ds == ds.fields().examples() |
244 | 304 # for ((x,y),a_v) in (ds('x','y'),a): #???don't work # haven't found a variant that work.# will not work |
245 def test_ds(orig,ds,index): | |
246 i=0 | |
247 assert len(ds)==len(index) | |
248 for x,z,y in ds('x','z','y'): | |
249 assert (orig[index[i]]['x']==a[index[i]][:3]).all() | |
250 assert (orig[index[i]]['x']==x).all() | |
251 assert orig[index[i]]['y']==a[index[i]][3] | |
252 assert orig[index[i]]['y']==y | |
253 assert (orig[index[i]]['z']==a[index[i]][0:3:2]).all() | |
254 assert (orig[index[i]]['z']==z).all() | |
255 i+=1 | |
256 del i | |
257 ds[0] | |
258 if len(ds)>2: | |
259 ds[:1] | |
260 ds[1:1] | |
261 ds[1:1:1] | |
262 if len(ds)>5: | |
263 ds[[1,2,3]] | |
264 for x in ds: | |
265 pass | |
266 | |
267 #ds[:n] returns a dataset with the n first examples. | |
268 ds2=ds[:3] | |
269 test_ds(ds,ds2,index=[0,1,2]) | |
270 | |
271 #ds[i1:i2:s]# returns a ds with the examples i1,i1+s,...i2-s. | |
272 ds2=ds[1:7:2] | |
273 test_ds(ds,ds2,[1,3,5]) | |
274 | |
275 #ds[[i1,i2,...in]]# returns a ds with examples i1,i2,...in. | |
276 ds2=ds[[4,7,2,8]] | |
277 test_ds(ds,ds2,[4,7,2,8]) | |
278 #ds[i1,i2,...]# should we accept???? | |
279 | |
280 #ds[fieldname]# an iterable over the values of the field fieldname across | |
281 #the ds (the iterable is obtained by default by calling valuesVStack | |
282 #over the values for individual examples). | |
283 | |
284 #ds.<property># returns the value of a property associated with | |
285 #the name <property>. The following properties should be supported: | |
286 # - 'description': a textual description or name for the ds | |
287 # - 'fieldtypes': a list of types (one per field) | |
288 #* ds1 | ds2 | ds3 == ds.hstack([ds1,ds2,ds3]) | |
289 #* ds1 & ds2 & ds3 == ds.vstack([ds1,ds2,ds3]) | |
290 | |
291 # for (x,y) in (ds('x','y'),a): #???don't work # haven't found a variant that work. | |
292 # assert numpy.append(x,y)==z | 305 # assert numpy.append(x,y)==z |
293 | 306 |
294 def test_LookupList(): | 307 def test_LookupList(): |
295 #test only the example in the doc??? | 308 #test only the example in the doc??? |
296 print "test_LookupList" | 309 print "test_LookupList" |