view test_dataset.py @ 58:17729d7104fa

added function test_ArrayDataSet
author Frederic Bastien <bastienf@iro.umontreal.ca>
date Thu, 01 May 2008 16:17:10 -0400
parents 70147d00615a
children 9165d86855ab
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
    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"]
    print "x|y"
    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 in ds['x','y']: # don't work
#        assert numpy.append(x['x'],x['y']).all()==a[i].all()
#        i+=1
#    for (x,y) in (ds('x','y'),a): #don't work # haven't found a variant that work.
#        assert numpy.append(x,y)==z
    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']
    except :
        have_thrown = True
    assert have_thrown == True
    assert ds == ds.fields().examples()
    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(field1,field2):
        pass
    for field_examples in ds.fields():
        for example_value in field_examples:
            pass
test_ArrayDataSet()