Mercurial > pylearn
comparison test_dataset.py @ 71:5b699b31770a
merge
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Fri, 02 May 2008 18:19:35 -0400 |
parents | dde1fb1b63ba |
children | b4159cbdc06b 4b0859606d05 |
comparison
equal
deleted
inserted
replaced
70:76e5c0f37165 | 71:5b699b31770a |
---|---|
1 #!/bin/env python | |
2 from dataset import * | |
3 from math import * | |
4 import numpy | |
5 | |
6 def test1(): | |
7 global a,ds | |
8 a = numpy.random.rand(10,4) | |
9 print a | |
10 ds = ArrayDataSet(a,{'x':slice(3),'y':3,'z':[0,2]}) | |
11 print "len(ds)=",len(ds) | |
12 assert(len(ds)==10) | |
13 print "example 0 = ",ds[0] | |
14 # assert | |
15 print "x=",ds["x"] | |
16 print "x|y" | |
17 for x,y in ds("x","y"): | |
18 print x,y | |
19 minibatch_iterator = ds.minibatches(fieldnames=['z','y'],n_batches=1,minibatch_size=3,offset=4) | |
20 minibatch = minibatch_iterator.__iter__().next() | |
21 print "minibatch=",minibatch | |
22 for var in minibatch: | |
23 print "var=",var | |
24 print "take a slice and look at field y",ds[1:6:2]["y"] | |
25 | |
26 def test_ArrayDataSet(): | |
27 #don't test stream | |
28 #tested only with float value | |
29 a = numpy.random.rand(10,4) | |
30 print a | |
31 ds = ArrayDataSet(a,{'x':slice(3),'y':3,'z':[0,2]}) | |
32 assert len(ds)==10 | |
33 #assert ds==a? should this work? | |
34 for i in range(len(ds)): | |
35 assert ds[i]['x'].all()==a[i][:2].all() | |
36 assert ds[i]['y']==a[i][3] | |
37 assert ds[i]['z'].all()==a[i][0:3:2].all() | |
38 print "x=",ds["x"] | |
39 print "x|y" | |
40 i=0 | |
41 for x in ds('x','y'): | |
42 assert numpy.append(x['x'],x['y']).all()==a[i].all() | |
43 i+=1 | |
44 # i=0 | |
45 # for x in ds['x','y']: # don't work | |
46 # assert numpy.append(x['x'],x['y']).all()==a[i].all() | |
47 # i+=1 | |
48 # for (x,y) in (ds('x','y'),a): #don't work # haven't found a variant that work. | |
49 # assert numpy.append(x,y)==z | |
50 i=0 | |
51 for x,y in ds('x','y'): | |
52 assert numpy.append(x,y).all()==a[i].all() | |
53 i+=1 | |
54 for minibatch in ds.minibatches(['x','z'], minibatch_size=3): | |
55 assert minibatch[0][:,0:3:2].all()==minibatch[1].all() | |
56 for x,z in ds.minibatches(['x','z'], minibatch_size=3): | |
57 assert x[:,0:3:2].all()==z.all() | |
58 | |
59 # for minibatch in ds.minibatches(['z','y'], minibatch_size=3): | |
60 # print minibatch | |
61 # minibatch_iterator = ds.minibatches(fieldnames=['z','y'],n_batches=1,minibatch_size=3,offset=4) | |
62 # minibatch = minibatch_iterator.__iter__().next() | |
63 # print "minibatch=",minibatch | |
64 # for var in minibatch: | |
65 # print "var=",var | |
66 # print "take a slice and look at field y",ds[1:6:2]["y"] | |
67 have_thrown = False | |
68 try: | |
69 ds['h'] # h is not defined... | |
70 except : | |
71 have_thrown = True | |
72 assert have_thrown == True | |
73 assert len(ds.fields())==3 | |
74 for field in ds.fields(): | |
75 for field_value in field: # iterate over the values associated to that field for all the ds examples | |
76 pass | |
77 for field in ds('x','z').fields(): | |
78 pass | |
79 for field in ds.fields('x','y'): | |
80 pass | |
81 for field_examples in ds.fields(): | |
82 for example_value in field_examples: | |
83 pass | |
84 | |
85 assert ds == ds.fields().examples() | |
86 | |
87 | |
88 #test missing value | |
89 | |
90 assert len(ds[:3])==3 | |
91 i=0 | |
92 for x,z in ds[:3]('x','z'): | |
93 assert ds[i]['z'].all()==a[i][0:3:2].all() | |
94 i+=1 | |
95 #ds[i1:i2:s]# returns a ds with the examples i1,i1+s,...i2-s. | |
96 | |
97 #ds[i]# returns an Example. | |
98 | |
99 #ds[[i1,i2,...in]]# returns a ds with examples i1,i2,...in. | |
100 | |
101 #ds[fieldname]# an iterable over the values of the field fieldname across | |
102 #the ds (the iterable is obtained by default by calling valuesVStack | |
103 #over the values for individual examples). | |
104 | |
105 #ds.<property># returns the value of a property associated with | |
106 #the name <property>. The following properties should be supported: | |
107 # - 'description': a textual description or name for the ds | |
108 # - 'fieldtypes': a list of types (one per field) | |
109 #* ds1 | ds2 | ds3 == ds.hstack([ds1,ds2,ds3]) | |
110 #* ds1 & ds2 & ds3 == ds.vstack([ds1,ds2,ds3]) | |
111 | |
112 | |
113 test_ArrayDataSet() | |
114 |