view test_dataset.py @ 82:158653a9bc7c

Automated merge with ssh://p-omega1@lgcm.iro.umontreal.ca/tlearn
author Frederic Bastien <bastienf@iro.umontreal.ca>
date Mon, 05 May 2008 11:02:03 -0400
parents b4159cbdc06b 4b0859606d05
children aa9e786ee849
line wrap: on
line source

#!/bin/env python
from dataset import *
from math import *
import numpy

def test1():
    global a,ds
    a = numpy.random.rand(10,4)
    print a
    ds = ArrayDataSet(a,{'x':slice(3),'y':3,'z':[0,2]})
    print "len(ds)=",len(ds)
    assert(len(ds)==10)
    print "example 0 = ",ds[0]
#    assert
    print "x=",ds["x"]
    print "x|y"
    for x,y in ds("x","y"):
        print x,y
    minibatch_iterator = ds.minibatches(fieldnames=['z','y'],n_batches=1,minibatch_size=3,offset=4)
    minibatch = minibatch_iterator.__iter__().next()
    print "minibatch=",minibatch
    for var in minibatch:
        print "var=",var
    print "take a slice and look at field y",ds[1:6:2]["y"]

def test_ArrayDataSet():
    #don't test stream
    #tested only with float value
    #test with y too
    #test missing value

    a = numpy.random.rand(10,4)
    print a
    ds = ArrayDataSet(a,{'x':slice(3),'y':3,'z':[0,2]})
    assert len(ds)==10
    #assert ds==a? should this work?
    for i in range(len(ds)):
        assert ds[i]['x'].all()==a[i][:2].all()
        assert ds[i]['y']==a[i][3]
        assert ds[i]['z'].all()==a[i][0:3:2].all()
    print "x=",ds["x"]
    i=0
    for x in ds('x','y'):
        assert numpy.append(x['x'],x['y']).all()==a[i].all()
        i+=1

    i=0
    for x,y in ds('x','y'):
        assert numpy.append(x,y).all()==a[i].all()
        i+=1
    for minibatch in ds.minibatches(['x','z'], minibatch_size=3):
        assert minibatch[0][:,0:3:2].all()==minibatch[1].all()
    for x,z in ds.minibatches(['x','z'], minibatch_size=3):
        assert x[:,0:3:2].all()==z.all()

#    for minibatch in ds.minibatches(['z','y'], minibatch_size=3):
#        print minibatch
#    minibatch_iterator = ds.minibatches(fieldnames=['z','y'],n_batches=1,minibatch_size=3,offset=4)
#    minibatch = minibatch_iterator.__iter__().next()
#    print "minibatch=",minibatch
#    for var in minibatch:
#        print "var=",var
#    print "take a slice and look at field y",ds[1:6:2]["y"]
    have_thrown = False
    try:
        ds['h'] # h is not defined...
    except :
        have_thrown = True
    assert have_thrown == True

    have_thrown = False
    try:
        ds[['h']] # h is not defined...
    except :
        have_thrown = True
    assert have_thrown == True

    assert len(ds.fields())==3
    for field in ds.fields():
        for field_value in field: # iterate over the values associated to that field for all the ds examples
            pass
    for field in ds('x','z').fields():
        pass
    for field in ds.fields('x','y'):
        pass
    for field_examples in ds.fields():
        for example_value in field_examples:
            pass

    assert ds == ds.fields().examples()


    #ds[:n] returns a dataset with the n first examples.
    assert len(ds[:3])==3
    i=0
    for x,z in ds[:3]('x','z'):
        assert ds[i]['z'].all()==a[i][0:3:2].all()
        i+=1

    #ds[i1:i2:s]# returns a ds with the examples i1,i1+s,...i2-s.
        ds[1:7:2][1] #fail???
    assert len(ds[1:7:2])==3 # should be number example 1,3 and 5
    i=0
    for x,z in ds[1:7:2]('x','z'):
        assert ds[i]['z'].all()==a[i][0:3:2].all()
        i+=1
    ds2=ds[1:7:2]
    for i in range(len(ds2)):
        print ds2[i]
    #ds[[i1,i2,...in]]# returns a ds with examples i1,i2,...in.
    i=0
    for x in ds[[1,2]]:
        assert numpy.append(x['x'],x['y']).all()==a[i].all()
        i+=1
    #ds[i1,i2,...]# should we accept????
    #ds[fieldname]# an iterable over the values of the field fieldname across
      #the ds (the iterable is obtained by default by calling valuesVStack
      #over the values for individual examples).

    #ds.<property># returns the value of a property associated with
      #the name <property>. The following properties should be supported:
      #    - 'description': a textual description or name for the ds
      #    - 'fieldtypes': a list of types (one per field)
    #* ds1 | ds2 | ds3 == ds.hstack([ds1,ds2,ds3])
    #* ds1 & ds2 & ds3 == ds.vstack([ds1,ds2,ds3])

#    for (x,y) in (ds('x','y'),a): #don't work # haven't found a variant that work.
#        assert numpy.append(x,y)==z

def test_LookupList():
    #test only the example in the doc???
       example = LookupList(['x','y','z'],[1,2,3])
       example['x'] = [1, 2, 3] # set or change a field
       x, y, z = example
       x = example[0]
       x = example["x"]
       assert example.keys()==['x','y','z']
       assert example.values()==[[1,2,3],2,3]
       assert example.items()==[('x',[1,2,3]),('y',2),('z',3)]
       example.append_keyval('u',0) # adds item with name 'u' and value 0
       assert len(example)==4 # number of items = 4 here
       example2 = LookupList(['v','w'], ['a','b'])
       example3 = LookupList(['x','y','z','u','v','w'], [[1, 2, 3],2,3,0,'a','b'])
       print example3
       print example+example2
       print example+example2
       assert example+example2==example3
       have_throw=False
       try:
           example+example
       except:
           have_throw=True
       assert have_throw

def ApplyFunctionDataSet():
    raise NotImplementedError()
def CacheDataSet():
    raise NotImplementedError()
def FieldsSubsetDataSet():
    raise NotImplementedError()
def DataSetFields():
    raise NotImplementedError()
def MinibatchDataSet():
    raise NotImplementedError()
def HStackedDataSet():
    raise NotImplementedError()
def VStackedDataSet():
    raise NotImplementedError()
def ArrayFieldsDataSet():
    raise NotImplementedError()

test1()
test_LookupList()
test_ArrayDataSet()