Skip to content

Commit

Permalink
Fix python3 tests
Browse files Browse the repository at this point in the history
- use uncompressed data in mathematics* to workaround an error in python3.2
- do not test dynprog on python 3 (it requires pickled numpy data...)
- histogram kernel creates huge values when using order > 1 that are
lossy represented as ascii creating test failures. Use order = 1 for
tests
- fix tester for python3 and its debug mode
- fix typos in parameter/datatype and print out proper info messages
when results differ
  • Loading branch information
Soeren Sonnenburg committed Feb 2, 2014
1 parent c7df3b8 commit d354e86
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 52 deletions.
Expand Up @@ -5,34 +5,32 @@
traindat = lm.load_dna('../data/fm_train_dna.dat')
testdat = lm.load_dna('../data/fm_test_dna.dat')
label_traindat = lm.load_labels('../data/label_train_dna.dat')
parameter_list=[[traindat,testdat,label_traindat,3,0,False],[traindat,testdat,label_traindat,3,0,False]]
parameter_list=[[traindat,testdat,label_traindat,1,1e1, 1e0],[traindat,testdat,label_traindat,1,1e4,1e4]]

def kernel_histogram_word_string_modular (fm_train_dna=traindat,fm_test_dna=testdat,label_train_dna=label_traindat,order=3,gap=0,reverse=False):
def kernel_histogram_word_string_modular (fm_train_dna=traindat,fm_test_dna=testdat,label_train_dna=label_traindat,order=3,ppseudo_count=1,npseudo_count=1):

from modshogun import StringCharFeatures, StringWordFeatures, DNA, BinaryLabels
from modshogun import HistogramWordStringKernel, AvgDiagKernelNormalizer
from modshogun import PluginEstimate#, MSG_DEBUG

reverse = reverse
charfeat=StringCharFeatures(DNA)
#charfeat.io.set_loglevel(MSG_DEBUG)
charfeat.set_features(fm_train_dna)
feats_train=StringWordFeatures(charfeat.get_alphabet())
feats_train.obtain_from_char(charfeat, order-1, order, gap, reverse)
feats_train.obtain_from_char(charfeat, order-1, order, 0, False)

charfeat=StringCharFeatures(DNA)
charfeat.set_features(fm_test_dna)
feats_test=StringWordFeatures(charfeat.get_alphabet())
feats_test.obtain_from_char(charfeat, order-1, order, gap, reverse)
feats_test.obtain_from_char(charfeat, order-1, order, 0, False)

pie=PluginEstimate()
pie=PluginEstimate(ppseudo_count,npseudo_count)
labels=BinaryLabels(label_train_dna)
pie.set_labels(labels)
pie.set_features(feats_train)
pie.train()

kernel=HistogramWordStringKernel(feats_train, feats_train, pie)
kernel.set_normalizer(AvgDiagKernelNormalizer())
km_train=kernel.get_kernel_matrix()
kernel.init(feats_train, feats_test)
pie.set_features(feats_test)
Expand Down
10 changes: 5 additions & 5 deletions examples/undocumented/python_modular/mathematics_linsolver_cg.py
@@ -1,10 +1,10 @@
#!/usr/bin/env python

from numpy import *
import numpy
from scipy.io import mmread

# Loading an example sparse matrix of dimension 479x479, real, unsymmetric
mtx=mmread('../../../data/logdet/west0479.mtx.gz')
mtx=mmread('../../../data/logdet/west0479.mtx')

parameter_list=[[mtx,6000,10]]

Expand All @@ -18,8 +18,8 @@ def mathematics_linsolver_cg (matrix=mtx,max_iter=1000,seed=10):
A=matrix.transpose()*matrix+eye(rows, cols)

# Create a random vector (b) of the system Ax=b
random.seed(seed)
b=array(random.randn(rows))
numpy.random.seed(seed)
b=numpy.array(numpy.random.randn(rows))

# create linear system with linear operator and vector
from scipy.sparse import csc_matrix
Expand All @@ -38,7 +38,7 @@ def mathematics_linsolver_cg (matrix=mtx,max_iter=1000,seed=10):
# verifying the solution via direct solving
from scipy.sparse.linalg import spsolve, eigsh
y=spsolve(A,b)
print(sqrt(sum(map(lambda z: z*z,x-y))))
print(numpy.linalg.norm(x-y))

return x

Expand Down
2 changes: 1 addition & 1 deletion examples/undocumented/python_modular/mathematics_logdet.py
Expand Up @@ -4,7 +4,7 @@
from scipy.io import mmread

# Loading an example sparse matrix of dimension 479x479, real, unsymmetric
mtx=mmread('../../../data/logdet/west0479.mtx.gz')
mtx=mmread('../../../data/logdet/west0479.mtx')

parameter_list=[[mtx,100,60,1]]

Expand Down
Expand Up @@ -65,6 +65,11 @@ def loads(str):
return unpickler.load()

def structure_dynprog_modular (fname):
import sys

#pickle is not compatible between python2 -> 3
if sys.version_info[0]>2:
return

data_dict = loads(gzip.GzipFile(fname).read())
#data_dict = loadmat('../data/DynProg_example_py.dat.mat', appendmat=False, struct_as_record=False)
Expand Down
36 changes: 18 additions & 18 deletions src/shogun/base/Parameter.cpp
Expand Up @@ -3197,7 +3197,7 @@ bool TParameter::equals(TParameter* other, float64_t accuracy)
SG_SDEBUG("length_y: %d\n", *m_datatype.m_length_y)
for (index_t i=0; i<*m_datatype.m_length_y; ++i)
{
SG_SDEBUG("comparing element %d which is %d byes from start\n",
SG_SDEBUG("comparing element %d which is %d bytes from start\n",
i, x);

void* pointer_a=&((*(char**)m_parameter)[x]);
Expand Down Expand Up @@ -3283,7 +3283,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if ((data1 && !data2) || (!data1 && data2))
{
SG_SDEBUG("leaving TParameter::compare_ptype(): data1 is at %p while "
SG_SINFO("leaving TParameter::compare_ptype(): data1 is at %p while "
"data2 is at %p\n", data1, data2);
return false;
}
Expand All @@ -3304,7 +3304,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_BOOL: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_BOOL: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3317,7 +3317,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_CHAR: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_CHAR: "
"data1=%c, data2=%c\n", casted1, casted2);
return false;
}
Expand All @@ -3330,7 +3330,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_INT8: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_INT8: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3343,7 +3343,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_UINT8: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_UINT8: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3356,7 +3356,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_INT16: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_INT16: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3369,7 +3369,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_UINT16: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_UINT16: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3382,7 +3382,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_INT32: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_INT32: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3395,7 +3395,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_UINT32: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_UINT32: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3408,7 +3408,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_INT64: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_INT64: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3421,7 +3421,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_UINT64: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_UINT64: "
"data1=%d, data2=%d\n", casted1, casted2);
return false;
}
Expand All @@ -3434,7 +3434,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_FLOAT32: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_FLOAT32: "
"data1=%f, data2=%f\n", casted1, casted2);
return false;
}
Expand All @@ -3447,7 +3447,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,

if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_FLOAT64: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_FLOAT64: "
"data1=%f, data2=%f\n", casted1, casted2);
return false;
}
Expand All @@ -3459,7 +3459,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,
floatmax_t casted2=*((floatmax_t*)data2);
if (CMath::abs(casted1-casted2)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_FLOATMAX: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_FLOATMAX: "
"data1=%f, data2=%f\n", casted1, casted2);
return false;
}
Expand All @@ -3474,7 +3474,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,
if (CMath::abs(casted1_real-casted2_real)>accuracy ||
CMath::abs(casted1_imag-casted2_imag)>accuracy)
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_COMPLEX128: "
SG_SINFO("leaving TParameter::compare_ptype(): PT_COMPLEX128: "
"data1=%f+i%f, data2=%f+i%f\n",
casted1_real, casted1_imag,
casted2_real, casted2_imag);
Expand All @@ -3499,7 +3499,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,
{
if (!(casted1->equals(casted2, accuracy)))
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_SGOBJECT "
SG_SINFO("leaving TParameter::compare_ptype(): PT_SGOBJECT "
"equals returned false\n");
return false;
}
Expand All @@ -3508,7 +3508,7 @@ bool TParameter::compare_ptype(EPrimitiveType ptype, void* data1, void* data2,
{
if (!(casted2->equals(casted1, accuracy)))
{
SG_SDEBUG("leaving TParameter::compare_ptype(): PT_SGOBJECT "
SG_SINFO("leaving TParameter::compare_ptype(): PT_SGOBJECT "
"equals returned false\n");
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/lib/DataType.cpp
Expand Up @@ -67,26 +67,26 @@ bool TSGDataType::equals_without_length(TSGDataType other)
{
if (m_ctype!=other.m_ctype)
{
SG_SDEBUG("leaving TSGDataType::equals_wihtout_length(): container types are "
SG_SDEBUG("leaving TSGDataType::equals_without_length(): container types are "
"different\n");
return false;
}

if (m_stype!=other.m_stype)
{
SG_SDEBUG("leaving TSGDataType::equals_wihtout_length(): struct types are "
SG_SDEBUG("leaving TSGDataType::equals_without_length(): struct types are "
"different\n");
return false;
}

if (m_ptype!=other.m_ptype)
{
SG_SDEBUG("leaving TSGDataType::equals_wihtout_length(): primitive types are "
SG_SDEBUG("leaving TSGDataType::equals_without_length(): primitive types are "
"different\n");
return false;
}

SG_SDEBUG("leaving TSGDataType::equals_wihtout_length(): data types "
SG_SDEBUG("leaving TSGDataType::equals_without_length(): data types "
"without lengths are equal\n");
return true;
}
Expand Down
38 changes: 22 additions & 16 deletions tests/integration/python_modular/tester.py
Expand Up @@ -10,6 +10,9 @@

from generator import setup_tests, get_fname, blacklist, get_test_mod, run_test

def is_python2():
return sys.version_info[0]==2

def typecheck(a, b):
if str(type(a)).find('shogun')>=0 and \
str(type(a)).find('Labels')>=0 and \
Expand Down Expand Up @@ -42,6 +45,8 @@ def compare(a, b, tolerance, sgtolerance):

# print debug output in case of failure
if not result:
pickle.dump(a, open('/tmp/a','wb'))
pickle.dump(b, open('/tmp/b','wb'))
print("Equals failed with debug output")
old_loglevel=a.io.get_loglevel()
a.io.set_loglevel(modshogun.MSG_INFO)
Expand Down Expand Up @@ -93,7 +98,7 @@ def compare_dbg_helper(a, b, tolerance, sgtolerance):
print("Length mismatch (len(a)=%d vs len(b)=%d)" % (len(a), len(b)))
return False
for obj1, obj2 in zip(a,b):
if not compare_dbg(obj1, obj2, tolerance):
if not compare_dbg(obj1, obj2, tolerance, sgtolerance):
return False
return True

Expand Down Expand Up @@ -212,21 +217,22 @@ def tester(tests, cmp_method, opts):
for f in failed:
print("\t" + f[0])

print("Detailed failures:")
for f in failed:
print("\t" + f[0])
got=get_split_string(f[1])
expected=get_split_string(f[2])
#print "=== EXPECTED =========="
#import pdb
#pdb.set_trace()
#print '\n'.join(expected)
#print "=== GOT ==============="
#print '\n'.join(got)
print("====DIFF================")
print('\n'.join(difflib.unified_diff(expected, got, fromfile='expected', tofile='got')))
print("====EOT================")
print("\n\n\n")
if is_python2():
print("Detailed failures:")
for f in failed:
print("\t" + f[0])
got=get_split_string(f[1])
expected=get_split_string(f[2])
#print "=== EXPECTED =========="
#import pdb
#pdb.set_trace()
#print '\n'.join(expected)
#print "=== GOT ==============="
#print '\n'.join(got)
print("====DIFF================")
print('\n'.join(difflib.unified_diff(expected, got, fromfile='expected', tofile='got')))
print("====EOT================")
print("\n\n\n")

sys.exit(1)
sys.exit(0)

0 comments on commit d354e86

Please sign in to comment.