From scipy-svn at scipy.org Mon Jul 3 18:32:49 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 3 Jul 2006 17:32:49 -0500 (CDT) Subject: [Scipy-svn] r2034 - trunk/Lib/weave/examples Message-ID: <20060703223249.D771ED40898@new.scipy.org> Author: oliphant Date: 2006-07-03 17:32:46 -0500 (Mon, 03 Jul 2006) New Revision: 2034 Modified: trunk/Lib/weave/examples/cast_copy_transpose.py trunk/Lib/weave/examples/ramp2.py Log: Fix usage of Float64 Modified: trunk/Lib/weave/examples/cast_copy_transpose.py =================================================================== --- trunk/Lib/weave/examples/cast_copy_transpose.py 2006-06-30 20:52:56 UTC (rev 2033) +++ trunk/Lib/weave/examples/cast_copy_transpose.py 2006-07-03 22:32:46 UTC (rev 2034) @@ -20,7 +20,7 @@ sys.path.insert(0,'..') import scipy.weave.inline_tools as inline_tools import scipy.weave.c_spec as c_spec -from weave.converters import blitz as cblitz +from scipy.weave.converters import blitz as cblitz def _cast_copy_transpose(type,a_2d): assert(len(shape(a_2d)) == 2) @@ -58,7 +58,7 @@ def _inplace_transpose(a_2d): assert(len(shape(a_2d)) == 2) - numeric_type = c_spec.num_to_c_types[a_2d.typecode()] + numeric_type = c_spec.num_to_c_types[a_2d.dtype.char] code = """ %s temp; for(int i = 0; i < Na_2d[0]; i++) @@ -120,6 +120,7 @@ def _castCopyAndTranspose(type, *arrays): cast_arrays = () + import copy for a in arrays: if a.dtype == numpy.dtype(type): cast_arrays = cast_arrays + (copy.copy(numpy.transpose(a)),) @@ -135,8 +136,8 @@ def compare(m,n): - a = ones((n,n),Float64) - type = Float32 + a = ones((n,n),float64) + type = float32 print 'Cast/Copy/Transposing (%d,%d)array %d times' % (n,n,m) t1 = time.time() for i in range(m): Modified: trunk/Lib/weave/examples/ramp2.py =================================================================== --- trunk/Lib/weave/examples/ramp2.py 2006-06-30 20:52:56 UTC (rev 2033) +++ trunk/Lib/weave/examples/ramp2.py 2006-07-03 22:32:46 UTC (rev 2034) @@ -19,7 +19,7 @@ mod = ext_tools.ext_module('ramp_ext') # type declarations - result = array([0],float) + result = array([0],float64) start,end = 0.,0. code = """ const int size = Nresult[0]; @@ -51,7 +51,7 @@ except: build_ramp_ext() import ramp_ext - arr = array([0]*10000,float) + arr = array([0]*10000,float64) for i in xrange(10000): ramp_ext.Ramp(arr, 0.0, 1.0) t2 = time.time() From scipy-svn at scipy.org Tue Jul 4 12:43:42 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 4 Jul 2006 11:43:42 -0500 (CDT) Subject: [Scipy-svn] r2035 - trunk/Lib/sandbox/svm Message-ID: <20060704164342.DBA74158A42@new.scipy.org> Author: fullung Date: 2006-07-04 11:43:35 -0500 (Tue, 04 Jul 2006) New Revision: 2035 Added: trunk/Lib/sandbox/svm/kernel.py Log: Separated out kernels. Added facility for custom kernels. Added: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-07-03 22:32:46 UTC (rev 2034) +++ trunk/Lib/sandbox/svm/kernel.py 2006-07-04 16:43:35 UTC (rev 2035) @@ -0,0 +1,75 @@ +__all__ = [ + 'LinearKernel', + 'PolynomialKernel', + 'RBFKernel', + 'SigmoidKernel', + 'CustomKernel' + ] + +def svm_node_dot(x, y): + # associate node indexes with array indexes + xidx = dict(zip(x['index'][:-1],range(0,len(x)))) + yidx = dict(zip(y['index'][:-1],range(0,len(y)))) + # indexes in either vector + indexes = N.unique(N.hstack([x['index'],y['index']])) + z = 0. + for j in indexes: + if j in xidx and j in yidx: + # dot if index is present in both vectors + z += x['value'][xidx[j]]*y['value'][yidx[j]] + return z + +class LinearKernel: + def __init__(self, dot=svm_node_dot): + self.dot = dot + + def __call__(self, x, y): + return self.dot(x, y) + +class PolynomialKernel: + def __init__(self, degree, gamma, coef0, dot=svm_node_dot): + self.degree = degree + self.gamma = gamma + self.coef0 = coef0 + self.dot = dot + + def __call__(self, x, y): + base = self.gamma*self.dot(x, y) + self.coef0 + tmp = base + ret = 1. + t = self.degree + while t > 0: + if t % 2 == 1: ret *= tmp + tmp *= tmp + t /= 2 + return ret + +class RBFKernel: + def __init__(self, gamma, dot=svm_node_dot): + self.gamma = gamma + self.dot = dot + + def __call__(self, x, y): + z = self.dot(x) + self.dot(y) - 2*self.dot(x, y) + return N.exp(-self.gamma*z) + +class SigmoidKernel: + def __init__(self, gamma, coef0, dot=svm_node_dot): + self.gamma = gamma + self.coef0 = coef0 + self.dot = dot + + def kernel_sigmoid(x, y, gamma, coef0): + return N.tanh(self.gamma*self.dot(x, y)+self.coef0) + +class CustomKernel: + """ + XXX example CustomKernel(lambda x, y, d: d(x,y)) + """ + + def __init__(self, f, dot=svm_node_dot): + self.f = f + self.dot = dot + + def __call__(self, x, y): + return self.f(x, y, dot) From scipy-svn at scipy.org Wed Jul 5 10:23:41 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 09:23:41 -0500 (CDT) Subject: [Scipy-svn] r2036 - trunk/Lib/sparse Message-ID: <20060705142341.3F6DC15A3BA@new.scipy.org> Author: edschofield Date: 2006-07-05 09:23:38 -0500 (Wed, 05 Jul 2006) New Revision: 2036 Modified: trunk/Lib/sparse/sparse.py Log: Better handling of 0-d arrays in sparse matrix operations Modified: trunk/Lib/sparse/sparse.py =================================================================== --- trunk/Lib/sparse/sparse.py 2006-07-04 16:43:35 UTC (rev 2035) +++ trunk/Lib/sparse/sparse.py 2006-07-05 14:23:38 UTC (rev 2036) @@ -196,14 +196,14 @@ return csc * other def __truediv__(self, other): - if isscalar(other): + if isscalarlike(other): return self * (1./other) else: raise NotImplementedError, "sparse matrix division not yet supported" def __div__(self, other): # Always do true division - if isscalar(other): + if isscalarlike(other): return self * (1./other) else: raise NotImplementedError, "sparse matrix division not yet supported" @@ -634,7 +634,7 @@ def __radd__(self, other): """ Function supporting the operation: self + other. """ - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): raise NotImplementedError, 'adding a scalar to a CSC matrix is ' \ 'not yet supported' elif isspmatrix(other): @@ -659,7 +659,7 @@ raise TypeError, "unsupported type for sparse matrix addition" def __add__(self, other): - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): raise NotImplementedError, 'adding a scalar to a CSC matrix is ' \ 'not yet supported' elif isspmatrix(other): @@ -686,7 +686,7 @@ def __mul__(self, other): """ Scalar, vector, or matrix multiplication """ - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = self.copy() new.data *= other new.dtype = new.data.dtype @@ -696,7 +696,7 @@ return self.dot(other) def __rmul__(self, other): # other * self - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = self.copy() new.data = other * new.data new.dtype = new.data.dtype @@ -719,7 +719,7 @@ """ Element-by-element power (unless other is a scalar, in which case return the matrix power.) """ - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = self.copy() new.data = new.data ** other new.dtype = new.data.dtype @@ -1218,7 +1218,7 @@ def __add__(self, other): # First check if argument is a scalar - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): # Now we would add this scalar to every element. raise NotImplementedError, 'adding a scalar to a CSR matrix ' \ 'is not yet supported' @@ -1244,7 +1244,7 @@ def __mul__(self, other): """ Scalar, vector, or matrix multiplication """ - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = self.copy() new.data = other * new.data # allows type conversion new.dtype = new.data.dtype @@ -1254,7 +1254,7 @@ return self.dot(other) def __rmul__(self, other): # other * self - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = self.copy() new.data = other * new.data # allows type conversion new.dtype = new.data.dtype @@ -1277,7 +1277,7 @@ """ Element-by-element power (unless other is a scalar, in which case return the matrix power.) """ - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = self.copy() new.data = new.data ** other new.dtype = new.data.dtype @@ -1913,7 +1913,7 @@ def __add__(self, other): # First check if argument is a scalar - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = dok_matrix(self.shape, dtype=self.dtype) # Add this scalar to every element. M, N = self.shape @@ -1943,7 +1943,7 @@ def __radd__(self, other): # First check if argument is a scalar - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = dok_matrix(self.shape, dtype=self.dtype) # Add this scalar to every element. M, N = self.shape @@ -1975,7 +1975,7 @@ return new def __mul__(self, other): # self * other - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = dok_matrix(self.shape, dtype=self.dtype) # Multiply this scalar by every element. for (key, val) in self.iteritems(): @@ -1986,7 +1986,7 @@ return self.dot(other) def __rmul__(self, other): # other * self - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = dok_matrix(self.shape, dtype=self.dtype) # Multiply this scalar by every element. for (key, val) in self.iteritems(): @@ -2549,7 +2549,7 @@ else: raise IndexError, "invalid index" - if isscalar(x): + if isscalar(x): # does this work with 0-dim arrays too? if len(row) == 0: row[:] = seq self.data[i] = [x for item in seq] # make copies @@ -2612,7 +2612,7 @@ def __mul__(self, other): # self * other - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): new = self.copy() if other == 0: # Multiply by zero: return the zero matrix @@ -2632,7 +2632,7 @@ def __rmul__(self, other): # other * self - if isscalar(other) or (isdense(other) and rank(other)==0): + if isscalarlike(other): # Multiplication by a scalar is symmetric return self.__mul__(other) else: @@ -2723,6 +2723,10 @@ def isdense(x): return _isinstance(x, ndarray) +def isscalarlike(x): + """Is x either a scalar, an array scalar, or a 0-dim array?""" + return isscalar(x) or (isdense(x) and x.ndim == 0) + def isshape(x): """Is x a valid 2-tuple of dimensions? """ From scipy-svn at scipy.org Wed Jul 5 10:24:27 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 09:24:27 -0500 (CDT) Subject: [Scipy-svn] r2037 - trunk/Lib/maxentropy/examples Message-ID: <20060705142427.CFE9E1589CE@new.scipy.org> Author: edschofield Date: 2006-07-05 09:24:24 -0500 (Wed, 05 Jul 2006) New Revision: 2037 Modified: trunk/Lib/maxentropy/examples/conditionalexample2.py Log: Clean up conditional maximum entropy example; remove Python 2.4-ism Modified: trunk/Lib/maxentropy/examples/conditionalexample2.py =================================================================== --- trunk/Lib/maxentropy/examples/conditionalexample2.py 2006-07-05 14:23:38 UTC (rev 2036) +++ trunk/Lib/maxentropy/examples/conditionalexample2.py 2006-07-05 14:24:24 UTC (rev 2037) @@ -17,9 +17,7 @@ """ __author__ = 'Ed Schofield' -__version__= '2.1' - import math from scipy import maxentropy, sparse import numpy @@ -49,9 +47,9 @@ numsamplepoints = len(samplespace) # Utility data structures: store the indices of each context and label in a -# dict for fast lookups of their indices in their respective lists: -samplespace_index = dict((x, i) for i, x in enumerate(samplespace)) -context_index = dict((c, i) for i, c in enumerate(contexts)) +# dict for fast lookups of their indices into their respective lists: +samplespace_index = dict([(x, i) for i, x in enumerate(samplespace)]) +context_index = dict([(c, i) for i, c in enumerate(contexts)]) # # Dense array version: # F = numpy.array([[f_i(x, c) for c in contexts for x in samplespace] for f_i in f]) @@ -63,7 +61,6 @@ for i, f_i in enumerate(f): for c, context in enumerate(contexts): for x, samplepoint in enumerate(samplespace): - # print "context: %s; \t sample point: %s" % (samplepoint, context) F[i, c * numsamplepoints + x] = f_i(samplepoint, context) @@ -97,7 +94,6 @@ # Fit the model model.fit() -model.fit() # Output the distribution print "\nFitted model parameters are:\n" + str(model.params) @@ -120,39 +116,3 @@ print -# Ignore below here - -# # Also suppose the corpus never contains the English word 'purple', but -# # we attempt to impose a fourth constraint -# # (4) p(? | c) = 0 for c = 'purple' -# # Is this possible under this framework? -# def f3(x, c): -# return x=='en' and c == 'beans' -# f.append(f3) -# -# print "The conditional distributions in some contexts not in the corpus:" -# newcontexts = ['purple', 'may'] -# newF = sparse.lil_matrix((len(f), len(newcontexts) * numsamplepoints)) -# for c, context in enumerate(newcontexts): -# # We need to compute the features of all sample points in these new contexts -# for x, samplepoint in enumerate(samplespace): -# for i, f_i in enumerate(f): -# newF[i, c * numsamplepoints + x] = f_i(samplepoint, context) -# # Computing N is not necessary. -# # newN[0, context_index[c] * numsamplepoints + samplespace_index[x]] += 1 -# -# model.F = newF -# del model.p_tilde, model.p_tilde_context -# model.clearcache() -# p = model.probdist() -# print "c \ x \t", -# for label in samplespace: -# print label + "\t", -# -# for c, context in enumerate(newcontexts): -# print "\n" + context + "\t", -# for x, label in enumerate(samplespace): -# print ("%.3f" % p[c*numsamplepoints+x]) + "\t", -# -# print - From scipy-svn at scipy.org Wed Jul 5 10:53:30 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 09:53:30 -0500 (CDT) Subject: [Scipy-svn] r2038 - trunk/Lib/sandbox/svm Message-ID: <20060705145330.8984015A3BD@new.scipy.org> Author: fullung Date: 2006-07-05 09:53:24 -0500 (Wed, 05 Jul 2006) New Revision: 2038 Modified: trunk/Lib/sandbox/svm/kernel.py Log: Fixed RBF kernel. Modified: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-07-05 14:24:24 UTC (rev 2037) +++ trunk/Lib/sandbox/svm/kernel.py 2006-07-05 14:53:24 UTC (rev 2038) @@ -50,7 +50,7 @@ self.dot = dot def __call__(self, x, y): - z = self.dot(x) + self.dot(y) - 2*self.dot(x, y) + z = self.dot(x, x) + self.dot(y, y) - 2*self.dot(x, y) return N.exp(-self.gamma*z) class SigmoidKernel: From scipy-svn at scipy.org Wed Jul 5 11:07:25 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 10:07:25 -0500 (CDT) Subject: [Scipy-svn] r2039 - trunk/Lib/sandbox/svm Message-ID: <20060705150725.87F2C15A3C1@new.scipy.org> Author: fullung Date: 2006-07-05 10:07:14 -0500 (Wed, 05 Jul 2006) New Revision: 2039 Added: trunk/Lib/sandbox/svm/dataset.py Modified: trunk/Lib/sandbox/svm/kernel.py Log: New design to hide libsvm data structure from the users. Added: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-05 14:53:24 UTC (rev 2038) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-05 15:07:14 UTC (rev 2039) @@ -0,0 +1,98 @@ +__all__ = [ + 'LibSvmRegressionDataSet', + 'LibSvmClassificationDataSet', + 'LibSvmOneClassDataSet' + ] + +import numpy as N + +import libsvm + +def svm_node_dot(x, y): + # associate node indexes with array indexes + xidx = dict(zip(x['index'][:-1],range(0,len(x)))) + yidx = dict(zip(y['index'][:-1],range(0,len(y)))) + # indexes in either vector + indexes = N.unique(N.hstack([x['index'],y['index']])) + z = 0. + for j in indexes: + if j in xidx and j in yidx: + # dot if index is present in both vectors + z += x['value'][xidx[j]]*y['value'][yidx[j]] + return z + +class LibSvmDataSet: + def __init__(self, data, kernel): + self.data = data + self.kernel = kernel + + def precompute(self): + n = 1 + len(data) + 1 + a = N.zeros((n,), dtype=svm_node_dtype) + a[-1] = -1, 0. # end of record marker + grammat = [a.copy() for i in range(len(data))] + for i, a in enumerate(grammat): + a[0] = 0, i + 1 # id + for i in range(len(data)): + for j in range(i, len(data)): + z = self.kernel(data[i], N.transpose(data[j])) + grammat[i][j+1]['value'] = z + grammat[j][i+1]['value'] = z + return LibSvmPrecomputedDataSet(grammat, self.data, self.kernel) + +class LibSvmPrecomputedDataSet: + def __init__(self, grammat, data, kernel): + self.grammat = grammat + self.data = data + self.kernel = kernel + + def extend(self, data): + raise NotImplementedError + +class LibSvmRegressionDataSet(LibSvmDataSet): + def __init__(self, data): + f = lambda x: (x[0], convert_to_svm_node(x[1])) + LibSvmDataSet.__init__(self, map(f, data)) + +class LibSvmClassificationDataSet(LibSvmDataSet): + def __init__(self, data): + labels = N.array(map(lambda x: x[0]), dtype=N.intc) + assert N.alltrue(labels >= 0), \ + 'labels must be non-negative integers' + f = lambda x: (x[0],convert_to_svm_node(x[1])) + LibSvmDataSet.__init__(self, map(f, data)) + +class LibSvmOneClassDataSet(LibSvmDataSet): + def __init__(self, data): + f = map(lambda x: tuple([0,convert_to_svm_node(x)])) + LibSvmDataSet.__init__(self, map(f, data)) + +def convert_to_svm_node(x): + y = N.empty(len(x)+1, dtype=libsvm.svm_node_dtype) + y[-1] = (-1, 0.) + if isinstance(x, dict): + x = x.items() + if isinstance(x, list): + x.sort(cmp=lambda x,y: cmp(x[0],y[0])) + y[:-1] = x + else: + y['index'][:-1] = N.arange(1,len(x)+1) + y['value'][:-1] = x + assert N.alltrue(y[:-1]['index'] >= 1), \ + 'indexes must be positive' + assert len(x) == len(N.unique(y[:-1]['index'])), \ + 'indexes must be unique' + return y + +def svm_node_dot(x, y): + # associate node indexes with array indexes + xidx = dict(zip(x['index'][:-1],range(0,len(x)))) + yidx = dict(zip(y['index'][:-1],range(0,len(y)))) + # indexes in either vector + indexes = N.unique(N.hstack([x['index'],y['index']])) + z = 0. + for j in indexes: + if j in xidx and j in yidx: + # dot if index is present in both vectors + z += x['value'][xidx[j]]*y['value'][yidx[j]] + return z Modified: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-07-05 14:53:24 UTC (rev 2038) +++ trunk/Lib/sandbox/svm/kernel.py 2006-07-05 15:07:14 UTC (rev 2039) @@ -6,28 +6,15 @@ 'CustomKernel' ] -def svm_node_dot(x, y): - # associate node indexes with array indexes - xidx = dict(zip(x['index'][:-1],range(0,len(x)))) - yidx = dict(zip(y['index'][:-1],range(0,len(y)))) - # indexes in either vector - indexes = N.unique(N.hstack([x['index'],y['index']])) - z = 0. - for j in indexes: - if j in xidx and j in yidx: - # dot if index is present in both vectors - z += x['value'][xidx[j]]*y['value'][yidx[j]] - return z - class LinearKernel: - def __init__(self, dot=svm_node_dot): + def __init__(self, dot): self.dot = dot def __call__(self, x, y): return self.dot(x, y) class PolynomialKernel: - def __init__(self, degree, gamma, coef0, dot=svm_node_dot): + def __init__(self, degree, gamma, coef0, dot): self.degree = degree self.gamma = gamma self.coef0 = coef0 @@ -45,7 +32,7 @@ return ret class RBFKernel: - def __init__(self, gamma, dot=svm_node_dot): + def __init__(self, gamma, dot): self.gamma = gamma self.dot = dot @@ -54,7 +41,7 @@ return N.exp(-self.gamma*z) class SigmoidKernel: - def __init__(self, gamma, coef0, dot=svm_node_dot): + def __init__(self, gamma, coef0, dot): self.gamma = gamma self.coef0 = coef0 self.dot = dot @@ -63,11 +50,7 @@ return N.tanh(self.gamma*self.dot(x, y)+self.coef0) class CustomKernel: - """ - XXX example CustomKernel(lambda x, y, d: d(x,y)) - """ - - def __init__(self, f, dot=svm_node_dot): + def __init__(self, f, dot): self.f = f self.dot = dot From scipy-svn at scipy.org Wed Jul 5 11:08:08 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 10:08:08 -0500 (CDT) Subject: [Scipy-svn] r2040 - trunk/Lib/sandbox/svm Message-ID: <20060705150808.4743C15A3C1@new.scipy.org> Author: fullung Date: 2006-07-05 10:08:01 -0500 (Wed, 05 Jul 2006) New Revision: 2040 Modified: trunk/Lib/sandbox/svm/dataset.py Log: minor Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-05 15:07:14 UTC (rev 2039) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-05 15:08:01 UTC (rev 2040) @@ -83,16 +83,3 @@ assert len(x) == len(N.unique(y[:-1]['index'])), \ 'indexes must be unique' return y - -def svm_node_dot(x, y): - # associate node indexes with array indexes - xidx = dict(zip(x['index'][:-1],range(0,len(x)))) - yidx = dict(zip(y['index'][:-1],range(0,len(y)))) - # indexes in either vector - indexes = N.unique(N.hstack([x['index'],y['index']])) - z = 0. - for j in indexes: - if j in xidx and j in yidx: - # dot if index is present in both vectors - z += x['value'][xidx[j]]*y['value'][yidx[j]] - return z From scipy-svn at scipy.org Wed Jul 5 14:27:59 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 13:27:59 -0500 (CDT) Subject: [Scipy-svn] r2041 - in trunk/Lib/special: cephes tests Message-ID: <20060705182759.3DF1FD408DF@new.scipy.org> Author: cookedm Date: 2006-07-05 13:27:34 -0500 (Wed, 05 Jul 2006) New Revision: 2041 Modified: trunk/Lib/special/cephes/airy.c trunk/Lib/special/cephes/hyp2f1.c trunk/Lib/special/cephes/hyperg.c trunk/Lib/special/cephes/i0.c trunk/Lib/special/cephes/i1.c trunk/Lib/special/cephes/iv.c trunk/Lib/special/cephes/j0.c trunk/Lib/special/cephes/j1.c trunk/Lib/special/cephes/jn.c trunk/Lib/special/cephes/jv.c trunk/Lib/special/cephes/k0.c trunk/Lib/special/cephes/k1.c trunk/Lib/special/cephes/kn.c trunk/Lib/special/cephes/mconf.h trunk/Lib/special/cephes/psi.c trunk/Lib/special/cephes/struve.c trunk/Lib/special/cephes/yn.c trunk/Lib/special/tests/test_basic.py Log: upgrade bessel functions (and related) in cephes to cephes 2.8 Modified: trunk/Lib/special/cephes/airy.c =================================================================== --- trunk/Lib/special/cephes/airy.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/airy.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -51,9 +51,8 @@ /* airy.c */ /* -Cephes Math Library Release 2.1: January, 1989 -Copyright 1984, 1987, 1989 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier */ #include "mconf.h" @@ -823,7 +822,15 @@ }; #endif -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double fabs ( double ); +extern double exp ( double ); +extern double sqrt ( double ); +extern double polevl ( double, void *, int ); +extern double p1evl ( double, void *, int ); +extern double sin ( double ); +extern double cos ( double ); +#else double fabs(), exp(), sqrt(); double polevl(), p1evl(), sin(), cos(); #endif Modified: trunk/Lib/special/cephes/hyp2f1.c =================================================================== --- trunk/Lib/special/cephes/hyp2f1.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/hyp2f1.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -31,11 +31,10 @@ * Transformation for x < -0.5 * Psi function expansion if x > 0.5 and c - a - b integer * Conditionally, a recurrence on c to make c-a-b > 0 - * - * x < -1 AMS 15.3.7 transformation applied (Travis Oliphant). - * valid for b,a,c,(b-a) != integer and (c-a),(c-b) != negative integer - * * + * x < -1 AMS 15.3.7 transformation applied (Travis Oliphant) + * valid for b,a,c,(b-a) != integer and (c-a),(c-b) != negative integer + * * x >= 1 is rejected (unless special cases are present) * * The parameters a, b, c are considered to be integer @@ -64,9 +63,8 @@ /* -Cephes Math Library Release 2.2: November, 1992 -Copyright 1984, 1987, 1992 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier */ @@ -94,13 +92,22 @@ #define ETHRESH 1.0e-12 -#ifndef ANSIPROT -double fabs(), pow(), round(), Gamma(), log(), exp(), psi(); -static double hyt2f1(); -static double hys2f1(); -#else +#ifdef ANSIPROT +extern double fabs ( double ); +extern double pow ( double, double ); +extern double round ( double ); +extern double gamma ( double ); +extern double log ( double ); +extern double exp ( double ); +extern double psi ( double ); static double hyt2f1(double, double, double, double, double *); static double hys2f1(double, double, double, double, double *); +double hyp2f1(double, double, double, double); +#else +double fabs(), pow(), round(), gamma(), log(), exp(), psi(); +static double hyt2f1(); +static double hys2f1(); +double hyp2f1(); #endif extern double MAXNUM, MACHEP, NAN; @@ -110,7 +117,6 @@ double d, d1, d2, e; double p, q, r, s, y, ax; double ia, ib, ic, id, err; -double t1; int flag, i, aid; err = 0.0; @@ -120,7 +126,6 @@ ia = round(a); /* nearest integer to a */ ib = round(b); - if( a <= 0 ) { if( fabs(a-ia) < EPS ) /* a is a negative integer */ @@ -133,23 +138,19 @@ flag |= 2; } - -if( fabs(ax-1.0) > EPS ) /* |x| != 1.0 */ - { - if( fabs(b-c) < EPS ) /* b = c */ - { +if (fabs(ax-1.0) > EPS) { /* |x| != 1.0 */ + if( fabs(b-c) < EPS ) { /* b = c */ y = pow( s, -a ); /* s to the -a power */ goto hypdon; - } - - if( fabs(a-c) < EPS ) /* a = c */ - { + } + if( fabs(a-c) < EPS ) { /* a = c */ y = pow( s, -b ); /* s to the -b power */ goto hypdon; - } + } } + if( c <= 0.0 ) { ic = round(c); /* nearest integer to c */ @@ -167,7 +168,22 @@ if( flag ) /* function is a polynomial */ goto hypok; +if (x < -1.0) { + double t1; + r = -x; + p = hyp2f1(a, 1-c+a, 1-b+a, 1.0/x); + q = hyp2f1(b, 1-c+b, 1-b+a, 1.0/x); + p *= pow(r, -a); + q *= pow(r, -b); + t1 = gamma(c); + s = t1*gamma(b-a)/(gamma(b)*gamma(c-a)); + y = t1*gamma(a-b)/(gamma(a)*gamma(c-b)); + return s*p + y*q; +} +if( ax > 1.0 ) /* series diverges */ + goto hypdiv; + p = c - a; ia = round(p); /* nearest integer to c-a */ if( (ia <= 0.0) && (fabs(p-ia) < EPS) ) /* negative int c - a */ @@ -182,7 +198,6 @@ id = round(d); /* nearest integer to d */ q = fabs(d-id); - /* Thanks to Christian Burger * for reporting a bug here. */ if( fabs(ax-1.0) < EPS ) /* |x| == 1.0 */ @@ -198,7 +213,7 @@ } if( d <= 0.0 ) goto hypdiv; - y = Gamma(c)*Gamma(d)/(Gamma(p)*Gamma(r)); + y = gamma(c)*gamma(d)/(gamma(p)*gamma(r)); goto hypdon; } @@ -238,11 +253,6 @@ if( flag & 12 ) goto hypf; /* negative integer c-a or c-b */ - -if( ax > 1.0 ) /* series diverges */ - goto hypdiv; - - hypok: y = hyt2f1( a, b, c, x, &err ); @@ -264,27 +274,15 @@ /* The alarm exit */ hypdiv: - -/* Added by Travis Oliphant */ - -if ((x < -1) & (fabs(b-a-(ib-ia)) < EPS)) { /* Handle negative values of x */ - r = -x; - p = hyp2f1(a, 1-c+a, 1-b+a, 1.0/x); - q = hyp2f1(b, 1-c+b, 1-a+b, 1.0/x); - p *= pow (r, -a); - q *= pow (r, -b); - t1 = Gamma(c); - s = t1*Gamma(b-a)/(Gamma(b)*Gamma(c-a)); - y = t1*Gamma(a-b)/(Gamma(a)*Gamma(c-b)); - return s*p + y*q; -} - mtherr( "hyp2f1", OVERFLOW ); return( MAXNUM ); } + + + /* Apply transformations for |x| near 1 * then call the power series */ @@ -322,9 +320,9 @@ goto done; /* If power series fails, then apply AMS55 #15.3.6 */ q = hys2f1( a, b, 1.0-d, s, &err ); - q *= Gamma(d) /(Gamma(c-a) * Gamma(c-b)); + q *= gamma(d) /(gamma(c-a) * gamma(c-b)); r = pow(s,d) * hys2f1( c-a, c-b, d+1.0, s, &err1 ); - r *= Gamma(-d)/(Gamma(a) * Gamma(b)); + r *= gamma(-d)/(gamma(a) * gamma(b)); y = q + r; q = fabs(q); /* estimate cancellation error */ @@ -333,7 +331,7 @@ r = q; err += err1 + (MACHEP*r)/y; - y *= Gamma(c); + y *= gamma(c); goto done; } else @@ -358,9 +356,9 @@ /* sum for t = 0 */ y = psi(1.0) + psi(1.0+e) - psi(a+d1) - psi(b+d1) - ax; - y /= Gamma(e+1.0); + y /= gamma(e+1.0); - p = (a+d1) * (b+d1) * s / Gamma(e+2.0); /* Poch for t=1 */ + p = (a+d1) * (b+d1) * s / gamma(e+2.0); /* Poch for t=1 */ t = 1.0; do { @@ -377,7 +375,7 @@ if( id == 0.0 ) { - y *= Gamma(c)/(Gamma(a)*Gamma(b)); + y *= gamma(c)/(gamma(a)*gamma(b)); goto psidon; } @@ -397,10 +395,10 @@ y1 += p; } nosum: - p = Gamma(c); - y1 *= Gamma(e) * p / (Gamma(a+d1) * Gamma(b+d1)); + p = gamma(c); + y1 *= gamma(e) * p / (gamma(a+d1) * gamma(b+d1)); - y *= p / (Gamma(a+d2) * Gamma(b+d2)); + y *= p / (gamma(a+d2) * gamma(b+d2)); if( (aid & 1) != 0 ) y = -y; Modified: trunk/Lib/special/cephes/hyperg.c =================================================================== --- trunk/Lib/special/cephes/hyperg.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/hyperg.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -60,21 +60,27 @@ /* -Cephes Math Library Release 2.1: November, 1988 -Copyright 1984, 1987, 1988 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier */ #include "mconf.h" -#ifndef ANSIPROT -double exp(), fabs(); -static double hy1f1p(); -static double hy1f1a(); -#else +#ifdef ANSIPROT +extern double exp ( double ); +extern double log ( double ); +extern double gamma ( double ); +extern double lgam ( double ); +extern double fabs ( double ); +double hyp2f0 ( double, double, double, int, double * ); static double hy1f1p(double, double, double, double *); static double hy1f1a(double, double, double, double *); -double hyp2f0( double,double,double,int,double*); +double hyperg (double, double, double); +#else +double exp(), log(), gamma(), lgam(), fabs(), hyp2f0(); +static double hy1f1p(); +static double hy1f1a(); +double hyperg(); #endif extern double MAXNUM, MACHEP; @@ -125,10 +131,7 @@ double *err; { double n, a0, sum, t, u, temp; -#ifndef ANSIPROT -double fabs(); -#endif -double an, bn, maxt, pcanc; +double an, bn, maxt; /* set up for power series summation */ @@ -139,8 +142,7 @@ n = 1.0; t = 1.0; maxt = 0.0; -pcanc = 1.0; /* estimate 100% error if problem */ -*err = pcanc; +*err = 1.0; while( t > MACHEP ) @@ -160,7 +162,8 @@ temp = fabs(u); if( (temp > 1.0 ) && (maxt > (MAXNUM/temp)) ) { - goto blowup; + *err = 1.0; /* blowup: estimate 100% error */ + return sum; } a0 *= u; @@ -186,12 +189,8 @@ if( sum != 0.0 ) maxt /= fabs(sum); maxt *= MACHEP; /* this way avoids multiply overflow */ -pcanc = fabs( MACHEP * n + maxt ); +*err = fabs( MACHEP * n + maxt ); -blowup: - -*err = pcanc; - return( sum ); } @@ -218,9 +217,6 @@ double *err; { double h1, h2, t, u, temp, acanc, asum, err1, err2; -#ifndef ANSIPROT -double exp(), log(), Gamma(), lgam(), fabs(), hyp2f0(); -#endif if( x == 0 ) { @@ -241,14 +237,14 @@ h1 = hyp2f0( a, a-b+1, -1.0/x, 1, &err1 ); -temp = exp(u) / Gamma(b-a); +temp = exp(u) / gamma(b-a); h1 *= temp; err1 *= temp; h2 = hyp2f0( b-a, 1.0-a, 1.0/x, 2, &err2 ); if( a < 0 ) - temp = exp(t) / Gamma(a); + temp = exp(t) / gamma(a); else temp = exp( t - lgam(a) ); @@ -265,7 +261,7 @@ if( b < 0 ) { - temp = Gamma(b); + temp = gamma(b); asum *= temp; acanc *= fabs(temp); } @@ -291,9 +287,6 @@ int type; /* determines what converging factor to use */ double *err; { -#ifndef ANSIPROT -double fabs(); -#endif double a0, alast, t, tlast, maxt; double n, an, bn, u, sum, temp; Modified: trunk/Lib/special/cephes/i0.c =================================================================== --- trunk/Lib/special/cephes/i0.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/i0.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -70,13 +70,11 @@ /* -Cephes Math Library Release 2.0: April, 1987 -Copyright 1984, 1987 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 2000 by Stephen L. Moshier */ #include "mconf.h" -#include /* Chebyshev coefficients for exp(-x) I0(x) * in the interval [0,8]. @@ -353,7 +351,11 @@ }; #endif -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double chbevl ( double, void *, int ); +extern double exp ( double ); +extern double sqrt ( double ); +#else double chbevl(), exp(), sqrt(); #endif Modified: trunk/Lib/special/cephes/i1.c =================================================================== --- trunk/Lib/special/cephes/i1.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/i1.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -71,9 +71,8 @@ /* -Cephes Math Library Release 2.0: March, 1987 -Copyright 1985, 1987 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1985, 1987, 2000 by Stephen L. Moshier */ #include "mconf.h" @@ -351,7 +350,12 @@ #endif /* i1.c */ -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double chbevl ( double, void *, int ); +extern double exp ( double ); +extern double sqrt ( double ); +extern double fabs ( double ); +#else double chbevl(), exp(), sqrt(), fabs(); #endif Modified: trunk/Lib/special/cephes/iv.c =================================================================== --- trunk/Lib/special/cephes/iv.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/iv.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -22,7 +22,7 @@ * function, according to the formula * * v -x - * Iv(x) = (x/2) e hyperg( v+0.5, 2v+1, 2x ) / Gamma(v+1) + * Iv(x) = (x/2) e hyperg( v+0.5, 2v+1, 2x ) / gamma(v+1) * * If v is a negative integer, then v is replaced by -v. * @@ -47,17 +47,23 @@ /* -Cephes Math Library Release 2.1: November, 1988 -Copyright 1984, 1987, 1988 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier */ #include "mconf.h" -#ifndef ANSIPROT -double hyperg(), exp(), Gamma(), log(), fabs(), floor(); +#ifdef ANSIPROT +extern double hyperg ( double, double, double ); +extern double exp ( double ); +extern double gamma ( double ); +extern double log ( double ); +extern double fabs ( double ); +extern double floor ( double ); +#else +double hyperg(), exp(), gamma(), log(), fabs(), floor(); #endif -extern double MACHEP, MAXNUM, NAN; +extern double MACHEP, MAXNUM; double iv( v, x ) double v, x; @@ -82,7 +88,7 @@ if( t != v ) { mtherr( "iv", DOMAIN ); - return( NAN ); + return( 0.0 ); } if( v != 2.0 * floor(v/2.0) ) sign = -1; @@ -104,7 +110,7 @@ ax = fabs(x); t = v * log( 0.5 * ax ) - x; -t = sign * exp(t) / Gamma( v + 1.0 ); +t = sign * exp(t) / gamma( v + 1.0 ); ax = v + 0.5; return( t * hyperg( ax, 2.0 * ax, 2.0 * x ) ); } Modified: trunk/Lib/special/cephes/j0.c =================================================================== --- trunk/Lib/special/cephes/j0.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/j0.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -84,9 +84,8 @@ */ /* -Cephes Math Library Release 2.1: January, 1989 -Copyright 1984, 1987, 1989 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier */ /* Note: all coefficients satisfy the relative error criterion @@ -459,8 +458,17 @@ }; #endif -#ifndef ANSIPROT -double j0(), polevl(), p1evl(), log(), sin(), cos(), sqrt(); +#ifdef ANSIPROT +extern double polevl ( double, void *, int ); +extern double p1evl ( double, void *, int ); +extern double log ( double ); +extern double sin ( double ); +extern double cos ( double ); +extern double sqrt ( double ); +double j0 ( double ); +#else +double polevl(), p1evl(), log(), sin(), cos(), sqrt(); +double j0(); #endif extern double TWOOPI, SQ2OPI, PIO4; Modified: trunk/Lib/special/cephes/j1.c =================================================================== --- trunk/Lib/special/cephes/j1.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/j1.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -73,9 +73,8 @@ /* -Cephes Math Library Release 2.1: January, 1989 -Copyright 1984, 1987, 1989 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier */ /* @@ -445,8 +444,17 @@ #define Z2 (*(double *)DZ2) #endif -#ifndef ANSIPROT -double j1(), polevl(), p1evl(), log(), sin(), cos(), sqrt(); +#ifdef ANSIPROT +extern double polevl ( double, void *, int ); +extern double p1evl ( double, void *, int ); +extern double log ( double ); +extern double sin ( double ); +extern double cos ( double ); +extern double sqrt ( double ); +double j1 ( double ); +#else +double polevl(), p1evl(), log(), sin(), cos(), sqrt(); +double j1(); #endif extern double TWOOPI, THPIO4, SQ2OPI; @@ -457,7 +465,7 @@ w = x; if( x < 0 ) - return -j1(-x); + return -j1(-x); if( w <= 5.0 ) { Modified: trunk/Lib/special/cephes/jn.c =================================================================== --- trunk/Lib/special/cephes/jn.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/jn.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -42,12 +42,15 @@ */ /* jn.c -Cephes Math Library Release 2.0: April, 1987 -Copyright 1984, 1987 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 2000 by Stephen L. Moshier */ #include "mconf.h" -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double fabs ( double ); +extern double j0 ( double ); +extern double j1 ( double ); +#else double fabs(), j0(), j1(); #endif extern double MACHEP; @@ -86,7 +89,7 @@ double y = x*x; return sign * 0.125 * y * (1 - y / 12.); } else { - return( sign * (2.0 * j1(x) / x - j0(x)) ); + return( sign * (2.0 * j1(x) / x - j0(x)) ); } } Modified: trunk/Lib/special/cephes/jv.c =================================================================== --- trunk/Lib/special/cephes/jv.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/jv.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -42,9 +42,8 @@ /* -Cephes Math Library Release 2.2: July, 1992 -Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier */ @@ -57,21 +56,37 @@ #define MAXGAM 171.624376956302725 #endif -#ifndef ANSIPROT -double fabs(), floor(), frexp(), polevl(), j0(), j1(), sqrt(), cbrt(); -double exp(), log(), sin(), cos(), acos(), pow(), Gamma(), lgam(); -static double recur(), jvs(), hankel(), jnx(), jnt(); -int airy(); -#else +#ifdef ANSIPROT +extern int airy ( double, double *, double *, double *, double * ); +extern double fabs ( double ); +extern double floor ( double ); +extern double frexp ( double, int * ); +extern double polevl ( double, void *, int ); +extern double j0 ( double ); +extern double j1 ( double ); +extern double sqrt ( double ); +extern double cbrt ( double ); +extern double exp ( double ); +extern double log ( double ); +extern double sin ( double ); +extern double cos ( double ); +extern double acos ( double ); +extern double pow ( double, double ); +extern double gamma ( double ); +extern double lgam ( double ); static double recur(double *, double, double *, int); static double jvs(double, double); static double hankel(double, double); static double jnx(double, double); static double jnt(double, double); -extern int airy ( double x, double *ai, double *aip, double *bi, double *bip ); +#else +int airy(); +double fabs(), floor(), frexp(), polevl(), j0(), j1(), sqrt(), cbrt(); +double exp(), log(), sin(), cos(), acos(), pow(), gamma(), lgam(); +static double recur(), jvs(), hankel(), jnx(), jnt(); #endif -extern double MAXNUM, MACHEP, MINLOG, MAXLOG, NAN; +extern double MAXNUM, MACHEP, MINLOG, MAXLOG; #define BIG 1.44115188075855872E+17 double jv( n, x ) @@ -109,7 +124,8 @@ if( (x < 0.0) && (y != an) ) { mtherr( "Jv", DOMAIN ); - return (NAN); + y = 0.0; + goto done; } y = fabs(x); @@ -259,7 +275,8 @@ double *newn; int cancel; { -double pkm2, pkm1, pk, pkp1, qkm2, qkm1; +double pkm2, pkm1, pk, qkm2, qkm1; +/* double pkp1; */ double k, ans, qk, xk, yk, r, t, kf; static double big = BIG; int nflag, ctr; @@ -357,7 +374,7 @@ do { pkm2 = (pkm1 * r - pk * x) / x; - pkp1 = pk; + /* pkp1 = pk; */ pk = pkm1; pkm1 = pkm2; r -= 2.0; @@ -436,9 +453,9 @@ && (n > 0.0) && (n < (MAXGAM-1.0)) ) { - t = pow( 0.5*x, n ) / Gamma( n + 1.0 ); + t = pow( 0.5*x, n ) / gamma( n + 1.0 ); #if DEBUG -printf( "pow(.5*x, %.4e)/Gamma(n+1)=%.5e\n", n, t ); +printf( "pow(.5*x, %.4e)/gamma(n+1)=%.5e\n", n, t ); #endif y *= t; } Modified: trunk/Lib/special/cephes/k0.c =================================================================== --- trunk/Lib/special/cephes/k0.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/k0.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -70,9 +70,8 @@ */ /* -Cephes Math Library Release 2.0: April, 1987 -Copyright 1984, 1987 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 2000 by Stephen L. Moshier */ #include "mconf.h" @@ -274,7 +273,13 @@ #endif /* k0.c */ -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double chbevl ( double, void *, int ); +extern double exp ( double ); +extern double i0 ( double ); +extern double log ( double ); +extern double sqrt ( double ); +#else double chbevl(), exp(), i0(), log(), sqrt(); #endif extern double PI; Modified: trunk/Lib/special/cephes/k1.c =================================================================== --- trunk/Lib/special/cephes/k1.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/k1.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -70,9 +70,8 @@ */ /* -Cephes Math Library Release 2.0: April, 1987 -Copyright 1984, 1987 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 2000 by Stephen L. Moshier */ #include "mconf.h" @@ -277,7 +276,13 @@ }; #endif -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double chbevl ( double, void *, int ); +extern double exp ( double ); +extern double i1 ( double ); +extern double log ( double ); +extern double sqrt ( double ); +#else double chbevl(), exp(), i1(), log(), sqrt(); #endif extern double PI; Modified: trunk/Lib/special/cephes/kn.c =================================================================== --- trunk/Lib/special/cephes/kn.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/kn.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -37,10 +37,8 @@ /* -Cephes Math Library Release 2.1: November, 1988 -Copyright 1984, 1987, 1988 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 - +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier */ @@ -83,7 +81,12 @@ #define EUL 5.772156649015328606065e-1 #define MAXFAC 31 -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double fabs ( double ); +extern double exp ( double ); +extern double log ( double ); +extern double sqrt ( double ); +#else double fabs(), exp(), log(), sqrt(); #endif extern double MACHEP, MAXNUM, MAXLOG, PI; Modified: trunk/Lib/special/cephes/mconf.h =================================================================== --- trunk/Lib/special/cephes/mconf.h 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/mconf.h 2006-07-05 18:27:34 UTC (rev 2041) @@ -199,3 +199,4 @@ /* Variable for error reporting. See mtherr.c. */ extern int merror; +#define gamma Gamma Modified: trunk/Lib/special/cephes/psi.c =================================================================== --- trunk/Lib/special/cephes/psi.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/psi.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -1,6 +1,6 @@ /* psi.c * - * Psi (diGamma) function + * Psi (digamma) function * * * SYNOPSIS: @@ -16,7 +16,7 @@ * psi(x) = -- ln | (x) * dx * - * is the logarithmic derivative of the Gamma function. + * is the logarithmic derivative of the gamma function. * For integer x, * n-1 * - @@ -52,9 +52,8 @@ */ /* -Cephes Math Library Release 2.2: July, 1992 -Copyright 1984, 1987, 1992 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier */ #include "mconf.h" @@ -109,7 +108,12 @@ #define EUL 0.57721566490153286061 -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double floor ( double ); +extern double log ( double ); +extern double tan ( double ); +extern double polevl ( double, void *, int ); +#else double floor(), log(), tan(), polevl(); #endif extern double PI, MAXNUM; Modified: trunk/Lib/special/cephes/struve.c =================================================================== --- trunk/Lib/special/cephes/struve.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/struve.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -31,32 +31,28 @@ /* -Cephes Math Library Release 2.1: January, 1989 -Copyright 1984, 1987, 1989 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.81: June, 2000 +Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier */ - -#import "mconf.h" - -#define ANSIPROT +#include "mconf.h" #define DEBUG 0 -#ifndef ANSIPROT -double Gamma(), pow(), sqrt(), yn(), yv(), jv(), fabs(), floor(); -double sin(), cos(); +#ifdef ANSIPROT +extern double gamma ( double ); +extern double pow ( double, double ); +extern double sqrt ( double ); +extern double yn ( int, double ); +extern double jv ( double, double ); +extern double fabs ( double ); +extern double floor ( double ); +extern double sin ( double ); +extern double cos ( double ); +double yv ( double, double ); +double onef2 (double, double, double, double, double * ); +double threef0 (double, double, double, double, double * ); #else -double onef2( double,double,double,double,double*); -double threef0( double,double,double,double,double*); -extern double fabs(double); -extern double floor(double); -extern double sqrt(double); -extern double sin ( double x ); -extern double cos ( double x ); -extern double pow ( double x, double y ); -extern double Gamma ( double x ); -extern double jv ( double n, double x ); -extern double yn ( int n, double x ); -double struve(double, double); -double yv(double, double); +double gamma(), pow(), sqrt(), yn(), yv(), jv(), fabs(), floor(); +double sin(), cos(); +double onef2(), threef0(); #endif static double stop = 1.37e-17; extern double MACHEP, INFINITY; @@ -225,12 +221,14 @@ double onef2err, threef0err; if (x == 0.0) { - if ((v>-1) || ((floor(v)-v)==0.5)) return 0.0; - if (v<-1) { - if ((int)(floor(0.5-v)-1) % 2) return -INFINITY; - else return INFINITY; - } - return 2.0/PI; + if (v > -1) { + return 0.0; + } else if (v < -1) { + if ((int)(floor(0.5-v)-1) % 2) return -INFINITY; + else return INFINITY; + } else { + return 2.0/PI; + } } f = floor(v); @@ -271,13 +269,13 @@ if( onef2err <= threef0err ) { - g = Gamma( v + 1.5 ); + g = gamma( v + 1.5 ); y = y * h * t / ( 0.5 * f * g ); return(y); } else { - g = Gamma( v + 0.5 ); + g = gamma( v + 0.5 ); ya = ya * h / ( f * g ); ya = ya + yv( v, x ); return(ya); Modified: trunk/Lib/special/cephes/yn.c =================================================================== --- trunk/Lib/special/cephes/yn.c 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/cephes/yn.c 2006-07-05 18:27:34 UTC (rev 2041) @@ -48,13 +48,16 @@ */ /* -Cephes Math Library Release 2.1: December, 1988 -Copyright 1984, 1987 by Stephen L. Moshier -Direct inquiries to 30 Frost Street, Cambridge, MA 02140 +Cephes Math Library Release 2.8: June, 2000 +Copyright 1984, 1987, 2000 by Stephen L. Moshier */ #include "mconf.h" -#ifndef ANSIPROT +#ifdef ANSIPROT +extern double y0 ( double ); +extern double y1 ( double ); +extern double log ( double ); +#else double y0(), y1(), log(); #endif extern double MAXNUM, MAXLOG; Modified: trunk/Lib/special/tests/test_basic.py =================================================================== --- trunk/Lib/special/tests/test_basic.py 2006-07-05 15:08:01 UTC (rev 2040) +++ trunk/Lib/special/tests/test_basic.py 2006-07-05 18:27:34 UTC (rev 2041) @@ -1155,11 +1155,23 @@ pass class test_hyp2f1(ScipyTestCase): - def check_hyp2f1(self): - hyp = hyp2f1(1,1,2,.5) - hrl = -(1/.5)*log(1-.5) - assert_almost_equal(hyp,hrl,8) + # a collection of special cases taken from AMS 55 + values = [[0.5, 1, 1.5, 0.2**2, 0.5/0.2*log((1+0.2)/(1-0.2))], + [0.5, 1, 1.5, -0.2**2, 1./0.2*arctan(0.2)], + [1, 1, 2, 0.2, -1/0.2*log(1-0.2)], + [3, 3.5, 1.5, 0.2**2, + 0.5/0.2/(-5)*((1+0.2)**(-5)-(1-0.2)**(-5))], + [-3, 3, 0.5, sin(0.2)**2, cos(2*3*0.2)], + [3, 4, 8, 1, gamma(8)*gamma(8-4-3)/gamma(8-3)/gamma(8-4)], + [3, 2, 3-2+1, -1, 1./2**3*sqrt(pi)* + gamma(1+3-2)/gamma(1+0.5*3-2)/gamma(0.5+0.5*3)], + [4, 0.5+4, 5./6+4, 1./9, (0.75)**4*sqrt(pi)* + gamma(5./6+2./3*4)/gamma(0.5+4./3)*gamma(5./6+4./3)], + ] + for i, (a, b, c, x, v) in enumerate(values): + cv = hyp2f1(a, b, c, x) + assert_almost_equal(cv, v, 8, err_msg='test #%d' % i) class test_hyp3f0(ScipyTestCase): @@ -1182,11 +1194,19 @@ assert_array_almost_equal(hypu,hprl,12) class test_i0(ScipyTestCase): - def check_i0(self): - oiz = i0(.1) - oizr = iv(0,.1) - assert_almost_equal(oiz,oizr,8) + values = [[0.0, 1.0], + [1e-10, 1.0], + [0.1, 0.9071009258], + [0.5, 0.6450352706], + [1.0, 0.4657596077], + [2.5, 0.2700464416], + [5.0, 0.1835408126], + [20.0, 0.0897803119], + ] + for i, (x, v) in enumerate(values): + cv = i0(x) * exp(-x) + assert_almost_equal(cv, v, 8, err_msg='test #%d' % i) class test_i0e(ScipyTestCase): @@ -1198,11 +1218,18 @@ class test_i1(ScipyTestCase): def check_i1(self): + values = [[0.0, 0.0], + [1e-10, 0.4999999999500000e-10], + [0.1, 0.0452984468], + [0.5, 0.1564208032], + [1.0, 0.2079104154], + [5.0, 0.1639722669], + [20.0, 0.0875062222], + ] + for i, (x, v) in enumerate(values): + cv = i1(x) * exp(-x) + assert_almost_equal(cv, v, 8, err_msg='test #%d' % i) - oi1 = i1(.1) - oi1r = iv(1,.1) - assert_almost_equal(oi1,oi1r,8) - class test_i1e(ScipyTestCase): def check_i1e(self): From scipy-svn at scipy.org Wed Jul 5 16:02:42 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 15:02:42 -0500 (CDT) Subject: [Scipy-svn] r2042 - in trunk/Lib/special: cephes tests Message-ID: <20060705200242.CF4ADD408F0@new.scipy.org> Author: cookedm Date: 2006-07-05 15:02:38 -0500 (Wed, 05 Jul 2006) New Revision: 2042 Modified: trunk/Lib/special/cephes/hyp2f1.c trunk/Lib/special/tests/test_basic.py Log: cephes: fix up hyp2f1 and its test case Modified: trunk/Lib/special/cephes/hyp2f1.c =================================================================== --- trunk/Lib/special/cephes/hyp2f1.c 2006-07-05 18:27:34 UTC (rev 2041) +++ trunk/Lib/special/cephes/hyp2f1.c 2006-07-05 20:02:38 UTC (rev 2042) @@ -117,28 +117,37 @@ double d, d1, d2, e; double p, q, r, s, y, ax; double ia, ib, ic, id, err; -int flag, i, aid; +int i, aid; +int neg_int_a = 0, neg_int_b = 0; +int neg_int_ca_or_cb = 0; err = 0.0; ax = fabs(x); s = 1.0 - x; -flag = 0; ia = round(a); /* nearest integer to a */ ib = round(b); -if( a <= 0 ) - { - if( fabs(a-ia) < EPS ) /* a is a negative integer */ - flag |= 1; - } +if (x == 0.0) { + return 1.0; +} -if( b <= 0 ) - { - if( fabs(b-ib) < EPS ) /* b is a negative integer */ - flag |= 2; - } +d = c - a - b; +if (d <= -1) { + return pow(s, d) * hyp2f1(c-a, c-b, c, x); +} +if (d <= 0 && x == 1) + goto hypdiv; -if (fabs(ax-1.0) > EPS) { /* |x| != 1.0 */ +if (a <= 0 && fabs(a-ia) < EPS ) { /* a is a negative integer */ + neg_int_a = 1; +} + +if (b <= 0 && fabs(b-ib) < EPS ) { /* b is a negative integer */ + neg_int_b = 1; +} + +if (ax < 1.0 || x == -1.0) { + /* 2F1(a,b;b;x) = (1-x)**(-a) */ if( fabs(b-c) < EPS ) { /* b = c */ y = pow( s, -a ); /* s to the -a power */ goto hypdon; @@ -157,24 +166,28 @@ if( fabs(c-ic) < EPS ) /* c is a negative integer */ { /* check if termination before explosion */ - if( (flag & 1) && (ia > ic) ) + if( neg_int_a && (ia > ic) ) goto hypok; - if( (flag & 2) && (ib > ic) ) + if( neg_int_b && (ib > ic) ) goto hypok; goto hypdiv; } } -if( flag ) /* function is a polynomial */ +if (neg_int_a || neg_int_b) /* function is a polynomial */ goto hypok; if (x < -1.0) { double t1; - r = -x; + t1 = fabs(b - a); + if (fabs(t1 - round(t1)) < EPS) { + /* this transformation has a pole for b-a= +-integer */ + goto hypdiv; + } p = hyp2f1(a, 1-c+a, 1-b+a, 1.0/x); - q = hyp2f1(b, 1-c+b, 1-b+a, 1.0/x); - p *= pow(r, -a); - q *= pow(r, -b); + q = hyp2f1(b, 1-c+b, 1-a+b, 1.0/x); + p *= pow(-x, -a); + q *= pow(-x, -b); t1 = gamma(c); s = t1*gamma(b-a)/(gamma(b)*gamma(c-a)); y = t1*gamma(a-b)/(gamma(a)*gamma(c-b)); @@ -187,41 +200,35 @@ p = c - a; ia = round(p); /* nearest integer to c-a */ if( (ia <= 0.0) && (fabs(p-ia) < EPS) ) /* negative int c - a */ - flag |= 4; + neg_int_ca_or_cb = 1; r = c - b; ib = round(r); /* nearest integer to c-b */ if( (ib <= 0.0) && (fabs(r-ib) < EPS) ) /* negative int c - b */ - flag |= 8; + neg_int_ca_or_cb = 1; -d = c - a - b; id = round(d); /* nearest integer to d */ q = fabs(d-id); /* Thanks to Christian Burger * for reporting a bug here. */ -if( fabs(ax-1.0) < EPS ) /* |x| == 1.0 */ - { - if( x > 0.0 ) - { - if( flag & 12 ) /* negative int c-a or c-b */ - { +if( fabs(ax-1.0) < EPS ) { /* |x| == 1.0 */ + if( x > 0.0 ) { + if (neg_int_ca_or_cb) { if( d >= 0.0 ) goto hypf; else goto hypdiv; - } + } if( d <= 0.0 ) goto hypdiv; y = gamma(c)*gamma(d)/(gamma(p)*gamma(r)); goto hypdon; - } - + } if( d <= -1.0 ) goto hypdiv; +} - } - /* Conditionally make d > 0 by recurrence on c * AMS55 #15.2.27 */ @@ -250,7 +257,7 @@ } -if( flag & 12 ) +if (neg_int_ca_or_cb) goto hypf; /* negative integer c-a or c-b */ hypok: Modified: trunk/Lib/special/tests/test_basic.py =================================================================== --- trunk/Lib/special/tests/test_basic.py 2006-07-05 18:27:34 UTC (rev 2041) +++ trunk/Lib/special/tests/test_basic.py 2006-07-05 20:02:38 UTC (rev 2042) @@ -1166,8 +1166,10 @@ [3, 4, 8, 1, gamma(8)*gamma(8-4-3)/gamma(8-3)/gamma(8-4)], [3, 2, 3-2+1, -1, 1./2**3*sqrt(pi)* gamma(1+3-2)/gamma(1+0.5*3-2)/gamma(0.5+0.5*3)], - [4, 0.5+4, 5./6+4, 1./9, (0.75)**4*sqrt(pi)* - gamma(5./6+2./3*4)/gamma(0.5+4./3)*gamma(5./6+4./3)], + [5, 2, 5-2+1, -1, 1./2**5*sqrt(pi)* + gamma(1+5-2)/gamma(1+0.5*5-2)/gamma(0.5+0.5*5)], + [4, 0.5+4, 1.5-2*4, -1./3, (8./9)**(-2*4)*gamma(4./3)* + gamma(1.5-2*4)/gamma(3./2)/gamma(4./3-2*4)], ] for i, (a, b, c, x, v) in enumerate(values): cv = hyp2f1(a, b, c, x) From scipy-svn at scipy.org Wed Jul 5 16:40:14 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 15:40:14 -0500 (CDT) Subject: [Scipy-svn] r2043 - in trunk/Lib/special: cephes tests Message-ID: <20060705204014.9251615A3F5@new.scipy.org> Author: cookedm Date: 2006-07-05 15:40:06 -0500 (Wed, 05 Jul 2006) New Revision: 2043 Modified: trunk/Lib/special/cephes/jv.c trunk/Lib/special/tests/test_basic.py Log: cephes: correct jv for small values of x. Fixes #221 Modified: trunk/Lib/special/cephes/jv.c =================================================================== --- trunk/Lib/special/cephes/jv.c 2006-07-05 20:02:38 UTC (rev 2042) +++ trunk/Lib/special/cephes/jv.c 2006-07-05 20:40:06 UTC (rev 2043) @@ -130,8 +130,9 @@ y = fabs(x); -if( y < MACHEP ) - goto underf; +if( y*y < fabs(n+1)*MACHEP ) { + return pow(0.5*x, n) / gamma(n+1); +} k = 3.6 * sqrt(y); t = 3.6 * sqrt(an); Modified: trunk/Lib/special/tests/test_basic.py =================================================================== --- trunk/Lib/special/tests/test_basic.py 2006-07-05 20:02:38 UTC (rev 2042) +++ trunk/Lib/special/tests/test_basic.py 2006-07-05 20:40:06 UTC (rev 2043) @@ -1098,7 +1098,6 @@ def check_gegenbauer(self): a = 5*rand()-0.5 if any(a==0): a = -0.2 - print "Gegenbauer, a = ", a Ca0 = gegenbauer(0,a) Ca1 = gegenbauer(1,a) Ca2 = gegenbauer(2,a) @@ -1335,8 +1334,15 @@ assert_almost_equal(jv(-3,2), -jv(3,2), 14) def check_jv(self): - jc = jv(0,.1) - assert_almost_equal(jc,0.99750156206604002,8) + values = [[0, 0.1, 0.99750156206604002], + [2./3, 1e-8, 0.3239028506761532e-5], + [2./3, 1e-10, 0.1503423854873779e-6], + [3.1, 1e-10, 0.1711956265409013e-32], + [2./3, 4.0, -0.2325440850267039], + ] + for i, (v, x, y) in enumerate(values): + yc = jv(v, x) + assert_almost_equal(yc, y, 8, err_msg='test #%d' % i) class test_jve(ScipyTestCase): def check_negv(self): From scipy-svn at scipy.org Wed Jul 5 16:51:17 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 5 Jul 2006 15:51:17 -0500 (CDT) Subject: [Scipy-svn] r2044 - trunk/Lib/sandbox/models Message-ID: <20060705205117.5FD66D408E9@new.scipy.org> Author: jonathan.taylor Date: 2006-07-05 15:51:12 -0500 (Wed, 05 Jul 2006) New Revision: 2044 Modified: trunk/Lib/sandbox/models/bsplines.py trunk/Lib/sandbox/models/formula.py Log: tabs -> spaces fix Modified: trunk/Lib/sandbox/models/bsplines.py =================================================================== --- trunk/Lib/sandbox/models/bsplines.py 2006-07-05 20:40:06 UTC (rev 2043) +++ trunk/Lib/sandbox/models/bsplines.py 2006-07-05 20:51:12 UTC (rev 2044) @@ -3,6 +3,8 @@ import scipy.integrate import _bspline +# note to self: check out eig_banded! in linalg.decomp? + def _zerofunc(x): return N.zeros(x.shape, N.float) Modified: trunk/Lib/sandbox/models/formula.py =================================================================== --- trunk/Lib/sandbox/models/formula.py 2006-07-05 20:40:06 UTC (rev 2043) +++ trunk/Lib/sandbox/models/formula.py 2006-07-05 20:51:12 UTC (rev 2044) @@ -76,7 +76,7 @@ self.func(namespace=namespace, **extra) """ - if not hasattr(self, 'func') or not usefn: + if not hasattr(self, 'func') or not usefn: val = namespace[self.termname] if isinstance(val, Formula): val = val(namespace=namespace, **extra) From scipy-svn at scipy.org Thu Jul 6 20:14:19 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 6 Jul 2006 19:14:19 -0500 (CDT) Subject: [Scipy-svn] r2045 - trunk/Lib/sandbox/svm/libsvm-2.82 Message-ID: <20060707001419.20E89D40ABA@new.scipy.org> Author: fullung Date: 2006-07-06 19:14:10 -0500 (Thu, 06 Jul 2006) New Revision: 2045 Added: trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win Removed: trunk/Lib/sandbox/svm/libsvm-2.82/libsvm.def Modified: trunk/Lib/sandbox/svm/libsvm-2.82/svm.h Log: Build a proper libsvm DLL on Windows. Added: trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win =================================================================== --- trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win 2006-07-05 20:51:12 UTC (rev 2044) +++ trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win 2006-07-07 00:14:10 UTC (rev 2045) @@ -0,0 +1,22 @@ +CXX = cl.exe +LINK = link.exe + +CPPFLAGS = -D_WIN32 -D_USRDLL -DLIBSVM_DLL -DLIBSVM_EXPORTS +CXXFLAGSALL = -nologo -EHsc -GS -W3 -Wp64 $(CPPFLAGS) +CXXFLAGSDBG = -MDd -Od -Z7 -RTCcsu +CXXFLAGSOPT = -MD -O2 +CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSDBG) +#CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSDBG) + +LINKFLAGS = /nologo /DLL + +#all: libsvm_.dll + +libsvm_.dll: svm.obj + $(LINK) $(LINKFLAGS) svm.obj /OUT:libsvm_.dll + +svm.obj: svm.cpp svm.h + $(CXX) $(CXXFLAGS) -c svm.cpp + +clean: + -erase /Q *.obj *.dll *.exp *.lib Deleted: trunk/Lib/sandbox/svm/libsvm-2.82/libsvm.def =================================================================== --- trunk/Lib/sandbox/svm/libsvm-2.82/libsvm.def 2006-07-05 20:51:12 UTC (rev 2044) +++ trunk/Lib/sandbox/svm/libsvm-2.82/libsvm.def 2006-07-07 00:14:10 UTC (rev 2045) @@ -1,17 +0,0 @@ -LIBRARY libsvm_.dll -EXPORTS -svm_train -svm_cross_validation -svm_save_model -svm_load_model -svm_get_svm_type -svm_get_nr_class -svm_get_labels -svm_get_svr_probability -svm_predict_values -svm_predict -svm_predict_probability -svm_destroy_model -svm_destroy_param -svm_check_parameter -svm_check_probability_model Modified: trunk/Lib/sandbox/svm/libsvm-2.82/svm.h =================================================================== --- trunk/Lib/sandbox/svm/libsvm-2.82/svm.h 2006-07-05 20:51:12 UTC (rev 2044) +++ trunk/Lib/sandbox/svm/libsvm-2.82/svm.h 2006-07-07 00:14:10 UTC (rev 2045) @@ -1,6 +1,16 @@ #ifndef _LIBSVM_H #define _LIBSVM_H +#ifdef LIBSVM_DLL +#ifdef LIBSVM_EXPORTS +#define LIBSVM_API __declspec(dllexport) +#else +#define LIBSVM_API __declspec(dllimport) +#endif /* _LIBSVM_EXPORTS */ +#else +#define LIBSVM_API +#endif /* _WIN32 */ + #ifdef __cplusplus extern "C" { #endif @@ -42,26 +52,26 @@ int probability; /* do probability estimates */ }; -struct svm_model *svm_train(const struct svm_problem *prob, const struct svm_parameter *param); -void svm_cross_validation(const struct svm_problem *prob, const struct svm_parameter *param, int nr_fold, double *target); +LIBSVM_API struct svm_model *svm_train(const struct svm_problem *prob, const struct svm_parameter *param); +LIBSVM_API void svm_cross_validation(const struct svm_problem *prob, const struct svm_parameter *param, int nr_fold, double *target); -int svm_save_model(const char *model_file_name, const struct svm_model *model); -struct svm_model *svm_load_model(const char *model_file_name); +LIBSVM_API int svm_save_model(const char *model_file_name, const struct svm_model *model); +LIBSVM_API struct svm_model *svm_load_model(const char *model_file_name); -int svm_get_svm_type(const struct svm_model *model); -int svm_get_nr_class(const struct svm_model *model); -void svm_get_labels(const struct svm_model *model, int *label); -double svm_get_svr_probability(const struct svm_model *model); +LIBSVM_API int svm_get_svm_type(const struct svm_model *model); +LIBSVM_API int svm_get_nr_class(const struct svm_model *model); +LIBSVM_API void svm_get_labels(const struct svm_model *model, int *label); +LIBSVM_API double svm_get_svr_probability(const struct svm_model *model); -void svm_predict_values(const struct svm_model *model, const struct svm_node *x, double* dec_values); -double svm_predict(const struct svm_model *model, const struct svm_node *x); -double svm_predict_probability(const struct svm_model *model, const struct svm_node *x, double* prob_estimates); +LIBSVM_API void svm_predict_values(const struct svm_model *model, const struct svm_node *x, double* dec_values); +LIBSVM_API double svm_predict(const struct svm_model *model, const struct svm_node *x); +LIBSVM_API double svm_predict_probability(const struct svm_model *model, const struct svm_node *x, double* prob_estimates); -void svm_destroy_model(struct svm_model *model); -void svm_destroy_param(struct svm_parameter *param); +LIBSVM_API void svm_destroy_model(struct svm_model *model); +LIBSVM_API void svm_destroy_param(struct svm_parameter *param); -const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param); -int svm_check_probability_model(const struct svm_model *model); +LIBSVM_API const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param); +LIBSVM_API int svm_check_probability_model(const struct svm_model *model); #ifdef __cplusplus } From scipy-svn at scipy.org Thu Jul 6 21:01:35 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 6 Jul 2006 20:01:35 -0500 (CDT) Subject: [Scipy-svn] r2046 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060707010135.C5368D40C8E@new.scipy.org> Author: fullung Date: 2006-07-06 20:01:27 -0500 (Thu, 06 Jul 2006) New Revision: 2046 Removed: trunk/Lib/sandbox/svm/tests/test_utils.py trunk/Lib/sandbox/svm/utils.py Log: Code integrated into NumPy itself. Deleted: trunk/Lib/sandbox/svm/tests/test_utils.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_utils.py 2006-07-07 00:14:10 UTC (rev 2045) +++ trunk/Lib/sandbox/svm/tests/test_utils.py 2006-07-07 01:01:27 UTC (rev 2046) @@ -1,17 +0,0 @@ -from numpy.testing import * - -# XXX remove this -import os, sys -sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))) - -import svm.utils as utils -import numpy as N - -class test_util(NumpyTestCase): - def check_addressof_array(self): - a = N.array([1.]) - addr = utils.addressof_array(a) - self.assert_(addr != 0) - -if __name__ == '__main__': - NumpyTest().run() Deleted: trunk/Lib/sandbox/svm/utils.py =================================================================== --- trunk/Lib/sandbox/svm/utils.py 2006-07-07 00:14:10 UTC (rev 2045) +++ trunk/Lib/sandbox/svm/utils.py 2006-07-07 01:01:27 UTC (rev 2046) @@ -1,31 +0,0 @@ -__all__ = [ - 'load_ctypes_library', - 'addressof_array', - 'array_as_ctype' - ] - -import numpy as N -import ctypes - -def load_ctypes_library(libname, loader_path): - import sys, os - if sys.platform == 'win32': - libname = '%s.dll' % libname - else: - libname = '%s.so' % libname - loader_path = os.path.abspath(loader_path) - if not os.path.isdir(loader_path): - libdir = os.path.dirname(loader_path) - else: - libdir = loader_path - libpath = os.path.join(libdir, libname) - if sys.platform == 'win32': - return ctypes.cdll.load(libpath) - else: - return ctypes.cdll.LoadLibrary(libpath) - -def addressof_array(a): - return N.cast[N.intp](int(a.__array_data__[0], 16)) - -def array_as_ctype(a, ctype): - return ctypes.cast(addressof_array(a), ctypes.POINTER(ctype)) From scipy-svn at scipy.org Thu Jul 6 21:02:20 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 6 Jul 2006 20:02:20 -0500 (CDT) Subject: [Scipy-svn] r2047 - trunk/Lib/sandbox/svm Message-ID: <20060707010220.397ACD40C8E@new.scipy.org> Author: fullung Date: 2006-07-06 20:02:09 -0500 (Thu, 06 Jul 2006) New Revision: 2047 Removed: trunk/Lib/sandbox/svm/data.py Modified: trunk/Lib/sandbox/svm/dataset.py Log: Datasets. Deleted: trunk/Lib/sandbox/svm/data.py =================================================================== --- trunk/Lib/sandbox/svm/data.py 2006-07-07 01:01:27 UTC (rev 2046) +++ trunk/Lib/sandbox/svm/data.py 2006-07-07 01:02:09 UTC (rev 2047) @@ -1,50 +0,0 @@ -__all__ = [ - 'LinearData', - 'LinearOneClassData', - ] - -import libsvm - -import numpy as N - -class LinearData: - def __init__(self): - self.kernel_type = libsvm.LINEAR - - def convert_train_data(self, data): - svm_data = [] - for label, x in data: - y = N.empty((len(x)+1,), dtype=libsvm.svm_node_dtype) - y['index'][:-1] = N.arange(1, len(x)+1) - y['value'][:-1] = x - y[-1] = (-1, 0.) - svm_data.append((label, y)) - return svm_data - - def convert_test_data(self, x): - y = N.empty((len(x)+1,), dtype=libsvm.svm_node_dtype) - y['index'][:-1] = N.arange(1, len(x)+1) - y['value'][:-1] = x - y[-1] = (-1, 0.) - return y - -class LinearOneClassData: - def __init__(self): - self.kernel_type = libsvm.LINEAR - - def convert_train_data(self, data): - svm_data = [] - for x in data: - y = N.empty((len(x)+1,), dtype=libsvm.svm_node_dtype) - y['index'][:-1] = 0 - y['value'][:-1] = x - y[-1] = (-1, 0.) - svm_data.append((0, y)) - return svm_data - - def convert_test_data(self, x): - y = N.empty((len(x)+1,), dtype=libsvm.svm_node_dtype) - y['index'][:-1] = 0 - y['value'][:-1] = x - y[-1] = (-1, 0.) - return y Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-07 01:01:27 UTC (rev 2046) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-07 01:02:09 UTC (rev 2047) @@ -1,72 +1,50 @@ __all__ = [ 'LibSvmRegressionDataSet', 'LibSvmClassificationDataSet', - 'LibSvmOneClassDataSet' + 'LibSvmOneClassDataSet', + 'LibSvmTestDataSet' ] import numpy as N import libsvm -def svm_node_dot(x, y): - # associate node indexes with array indexes - xidx = dict(zip(x['index'][:-1],range(0,len(x)))) - yidx = dict(zip(y['index'][:-1],range(0,len(y)))) - # indexes in either vector - indexes = N.unique(N.hstack([x['index'],y['index']])) - z = 0. - for j in indexes: - if j in xidx and j in yidx: - # dot if index is present in both vectors - z += x['value'][xidx[j]]*y['value'][yidx[j]] - return z - class LibSvmDataSet: - def __init__(self, data, kernel): + def __init__(self, data): self.data = data - self.kernel = kernel - def precompute(self): - n = 1 + len(data) + 1 - a = N.zeros((n,), dtype=svm_node_dtype) - a[-1] = -1, 0. # end of record marker - grammat = [a.copy() for i in range(len(data))] - for i, a in enumerate(grammat): - a[0] = 0, i + 1 # id - for i in range(len(data)): - for j in range(i, len(data)): - z = self.kernel(data[i], N.transpose(data[j])) - grammat[i][j+1]['value'] = z - grammat[j][i+1]['value'] = z - return LibSvmPrecomputedDataSet(grammat, self.data, self.kernel) + def getgamma(self): + maxlen = 0 + for y, x in self.data: + maxlen = N.maximum(maxlen, x['index'].max()) + return 1.0 / maxlen + gamma = property(getgamma, 'Gamma parameter for RBF kernel') -class LibSvmPrecomputedDataSet: - def __init__(self, grammat, data, kernel): - self.grammat = grammat - self.data = data - self.kernel = kernel - - def extend(self, data): - raise NotImplementedError - class LibSvmRegressionDataSet(LibSvmDataSet): - def __init__(self, data): - f = lambda x: (x[0], convert_to_svm_node(x[1])) - LibSvmDataSet.__init__(self, map(f, data)) + def __init__(self, origdata): + data = map(lambda x: (x[0], convert_to_svm_node(x[1])), origdata) + LibSvmDataSet.__init__(self, data) class LibSvmClassificationDataSet(LibSvmDataSet): - def __init__(self, data): - labels = N.array(map(lambda x: x[0]), dtype=N.intc) + def __init__(self, origdata): + labels = N.array(map(lambda x: x[0], origdata), dtype=N.intc) assert N.alltrue(labels >= 0), \ 'labels must be non-negative integers' - f = lambda x: (x[0],convert_to_svm_node(x[1])) - LibSvmDataSet.__init__(self, map(f, data)) + labels.sort() + self.labels = labels + data = map(lambda x: (x[0],convert_to_svm_node(x[1])), origdata) + LibSvmDataSet.__init__(self, data) + class LibSvmOneClassDataSet(LibSvmDataSet): - def __init__(self, data): - f = map(lambda x: tuple([0,convert_to_svm_node(x)])) - LibSvmDataSet.__init__(self, map(f, data)) + def __init__(self, origdata): + data = map(lambda x: tuple([0,convert_to_svm_node(x)]), origdata) + LibSvmDataSet.__init__(self, data) +class LibSvmTestDataSet: + def __init__(self, origdata): + self.data = map(lambda x: convert_to_svm_node(x), origdata) + def convert_to_svm_node(x): y = N.empty(len(x)+1, dtype=libsvm.svm_node_dtype) y[-1] = (-1, 0.) From scipy-svn at scipy.org Thu Jul 6 21:03:03 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 6 Jul 2006 20:03:03 -0500 (CDT) Subject: [Scipy-svn] r2048 - trunk/Lib/sandbox/svm/tests Message-ID: <20060707010303.CAB92D40C8E@new.scipy.org> Author: fullung Date: 2006-07-06 20:02:56 -0500 (Thu, 06 Jul 2006) New Revision: 2048 Added: trunk/Lib/sandbox/svm/tests/test_dataset.py Log: Basic tests for datasets. Added: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-07 01:02:09 UTC (rev 2047) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-07 01:02:56 UTC (rev 2048) @@ -0,0 +1,59 @@ +from numpy.testing import * +import numpy as N + +from svm.dataset import convert_to_svm_node +from svm.dataset import * +from svm.libsvm import svm_node_dtype + +class test_dataset(NumpyTestCase): + def check_convert_dict(self): + x = N.array([(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node({}), x) + + x = N.array([(1,2.),(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node({1:2.}), x) + + x = N.array([(1,2.),(3,4.),(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node({1:2.,3:4.}), x) + + # check for positive indexes + self.assertRaises(AssertionError, convert_to_svm_node, {0:0.}) + + def check_convert_list(self): + x = N.array([(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node([]), x) + + x = N.array([(1,2.),(3,4.),(-1,0.)], dtype=svm_node_dtype) + # check that indexes are sorted + assert_array_equal(convert_to_svm_node([(3,4.),(1,2.)]), x) + + # check for unique indexes + self.assertRaises(AssertionError, + convert_to_svm_node, [(1,0.),(1,0.)]) + + def check_convert_array(self): + x = N.array([(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node(N.empty(0)), x) + + x = N.array([(1,1.),(2,2.),(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node(N.arange(1,3)), x) + + def check_regression(self): + data = [(1.0, N.arange(5))] + dataset = LibSvmRegressionDataSet(data) + self.assertAlmostEqual(dataset.gamma, 0.2) + + def check_classification(self): + data = [(1, N.arange(4)), (2, N.arange(10))] + dataset = LibSvmClassificationDataSet(data) + self.assertAlmostEqual(dataset.gamma, 0.1) + self.assert_(1 in dataset.labels) + self.assert_(2 in dataset.labels) + + def check_oneclass(self): + data = [N.arange(2)] + dataset = LibSvmOneClassDataSet(data) + self.assertAlmostEqual(dataset.gamma, 0.5) + +if __name__ == '__main__': + NumpyTest().run() From scipy-svn at scipy.org Thu Jul 6 21:03:50 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 6 Jul 2006 20:03:50 -0500 (CDT) Subject: [Scipy-svn] r2049 - trunk/Lib/sandbox/svm Message-ID: <20060707010350.F3EA1D40C8E@new.scipy.org> Author: fullung Date: 2006-07-06 20:03:44 -0500 (Thu, 06 Jul 2006) New Revision: 2049 Modified: trunk/Lib/sandbox/svm/libsvm.py Log: Minor changes due to code moving to Numpy. Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-07-07 01:02:56 UTC (rev 2048) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-07-07 01:03:44 UTC (rev 2049) @@ -27,17 +27,15 @@ ] import numpy as N +from ctypes import c_int, c_double, POINTER, Structure, c_char_p +_libsvm = N.ctypes_load_library('libsvm_', __file__) + svm_node_dtype = \ N.dtype({'names' : ['index', 'value'], 'formats' : [N.intc, N.float64]}, align=1) -import utils -_libsvm = utils.load_ctypes_library('libsvm_', __file__) - -from ctypes import c_int, c_double, POINTER, Structure, c_char_p - # svm types C_SVC = 0 NU_SVC = 1 @@ -76,7 +74,8 @@ ('C', c_double), ('nr_weight', c_int), ('weight_label', POINTER(c_int)), - # parameter weight is weight*C, of C-SVC and nu-SVC + # parameter weight of C-SVC and nu-SVC + # actual weight of class is this weight * C ('weight', POINTER(c_double)), # parameter nu of nu-SVC, one-class SVM and nu-SVR ('nu', c_double), @@ -99,8 +98,10 @@ class svm_model(Structure): _fields_ = [ + # parameters used to train the model ('param', svm_parameter), ('nr_class', c_int), + # el ('l', c_int), # support vectors (length el) ('SV', POINTER(POINTER(svm_node))), @@ -110,9 +111,8 @@ ('rho', POINTER(c_double)), # length nr_class*(nr_class-1)/2 for classification # length 1 for regression - # length 0 for one-class ('probA', POINTER(c_double)), - # ??? for classification (length nr_class*(nr_class-1)/2) + # length nr_class*(nr_class-1)/2 for classification ('probB', POINTER(c_double)), # support vectors labels for classifcation (length nr_class) ('labels', POINTER(c_int)), From scipy-svn at scipy.org Thu Jul 6 22:00:42 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 6 Jul 2006 21:00:42 -0500 (CDT) Subject: [Scipy-svn] r2050 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060707020042.29FDAD40CA0@new.scipy.org> Author: fullung Date: 2006-07-06 21:00:33 -0500 (Thu, 06 Jul 2006) New Revision: 2050 Added: trunk/Lib/sandbox/svm/tests/test_kernel.py Modified: trunk/Lib/sandbox/svm/kernel.py Log: Fix kernels. Modified: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-07-07 01:03:44 UTC (rev 2049) +++ trunk/Lib/sandbox/svm/kernel.py 2006-07-07 02:00:33 UTC (rev 2050) @@ -6,24 +6,22 @@ 'CustomKernel' ] +import numpy as N + class LinearKernel: - def __init__(self, dot): - self.dot = dot + def __call__(self, x, y, dot): + return dot(x, y) - def __call__(self, x, y): - return self.dot(x, y) - class PolynomialKernel: - def __init__(self, degree, gamma, coef0, dot): + def __init__(self, degree, gamma, coef0): self.degree = degree self.gamma = gamma self.coef0 = coef0 - self.dot = dot - def __call__(self, x, y): - base = self.gamma*self.dot(x, y) + self.coef0 + def __call__(self, x, y, dot): + base = self.gamma*dot(x, y) + self.coef0 tmp = base - ret = 1. + ret = 1.0 t = self.degree while t > 0: if t % 2 == 1: ret *= tmp @@ -32,27 +30,24 @@ return ret class RBFKernel: - def __init__(self, gamma, dot): + def __init__(self, gamma): self.gamma = gamma - self.dot = dot - def __call__(self, x, y): - z = self.dot(x, x) + self.dot(y, y) - 2*self.dot(x, y) + def __call__(self, x, y, dot): + z = dot(x, x) + dot(y, y) - 2*dot(x, y) return N.exp(-self.gamma*z) class SigmoidKernel: - def __init__(self, gamma, coef0, dot): + def __init__(self, gamma, coef0): self.gamma = gamma self.coef0 = coef0 - self.dot = dot - def kernel_sigmoid(x, y, gamma, coef0): - return N.tanh(self.gamma*self.dot(x, y)+self.coef0) + def __call__(self, x, y, dot): + return N.tanh(self.gamma*dot(x, y)+self.coef0) class CustomKernel: - def __init__(self, f, dot): + def __init__(self, f): self.f = f - self.dot = dot - def __call__(self, x, y): + def __call__(self, x, y, dot): return self.f(x, y, dot) Added: trunk/Lib/sandbox/svm/tests/test_kernel.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_kernel.py 2006-07-07 01:03:44 UTC (rev 2049) +++ trunk/Lib/sandbox/svm/tests/test_kernel.py 2006-07-07 02:00:33 UTC (rev 2050) @@ -0,0 +1,41 @@ +from numpy.testing import * +import numpy as N + +from svm.kernel import * + +class test_kernel(NumpyTestCase): + def check_linear_kernel(self): + kernel = LinearKernel() + dot = N.dot + x = N.array([2.]) + self.assertAlmostEqual(kernel(x, x, dot), 4.) + + def check_polynomial_kernel(self): + kernel = PolynomialKernel(degree=6, gamma=1.0, coef0=1.0) + dot = N.dot + x = N.array([2.]) + self.assertAlmostEqual(kernel(x, x, dot), 15625.) + + def check_sigmoid_kernel(self): + kernel = SigmoidKernel(gamma=0.2, coef0=0.3) + dot = N.dot + x = N.array([2.]) + self.assertAlmostEqual(kernel(x, x, dot), 0.80049902) + + def check_rbf_kernel(self): + kernel = RBFKernel(gamma=1.0) + dot = N.dot + x, y = N.array([2.]), N.array([3.]) + self.assertAlmostEqual(kernel(x, y, dot), N.exp(-1.)) + + def check_custom_kernel(self): + def f(x, y, dot): + return 4 * dot(x, y) + kernel = CustomKernel(f) + def dot(x, y): + return 2 * N.dot(x, y) + x = N.array([2.]) + self.assertAlmostEqual(kernel(x, x, dot), 32.0) + +if __name__ == '__main__': + NumpyTest().run() From scipy-svn at scipy.org Thu Jul 6 22:33:37 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 6 Jul 2006 21:33:37 -0500 (CDT) Subject: [Scipy-svn] r2051 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060707023337.8F4C015A448@new.scipy.org> Author: fullung Date: 2006-07-06 21:33:26 -0500 (Thu, 06 Jul 2006) New Revision: 2051 Modified: trunk/Lib/sandbox/svm/__init__.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Training of regression model works. Modified: trunk/Lib/sandbox/svm/__init__.py =================================================================== --- trunk/Lib/sandbox/svm/__init__.py 2006-07-07 02:00:33 UTC (rev 2050) +++ trunk/Lib/sandbox/svm/__init__.py 2006-07-07 02:33:26 UTC (rev 2051) @@ -18,4 +18,5 @@ from classification import * from regression import * from oneclass import * -from data import * +from dataset import * +from kernel import * Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-07 02:00:33 UTC (rev 2050) +++ trunk/Lib/sandbox/svm/model.py 2006-07-07 02:33:26 UTC (rev 2051) @@ -1,81 +1,72 @@ __all__ = [ - 'Model' + 'LibSvmModel' ] +from ctypes import * + +from kernel import * import libsvm -import utils -import numpy as N -from ctypes import * +class LibSvmModel: + def __init__(self, svm_type, kernel, + tolerance=0.001, shrinking=True, cache_size=40): + """ + Parameters: -class Model: - def __init__(self, dtype, shrinking=True, cache_size=40, tol=0.001): - self.dtype = dtype + - `svm_type`: XXX + - `kernel`: XXX + - `tolerance`: tolerance of termination criterion + - `shrinking`: whether to use the shrinking heuristics + - `cache_size` kernel evaluation cache size (MB) + """ + self.svm_type = svm_type + self.kernel = kernel + self.tolerance = tolerance self.shrinking = shrinking self.cache_size = cache_size - self.tol = tol - def fit(self, data): - svm_data = self.dtype.convert_train_data(data) - # libsvm requires data to be sorted by label - svm_data.sort(cmp=lambda x, y: cmp(x[0], y[0])) - param = self.setup_svm_parameter(svm_data) + param = libsvm.svm_parameter() - # XXX find better way to keep x and y references - problem, x, y = self.setup_svm_problem(svm_data) + if isinstance(kernel, LinearKernel): + param.kernel_type = libsvm.LINEAR + elif isinstance(kernel, PolynomialKernel): + param.kernel_type = libsvm.POLY + param.degree = kernel.degree + param.gamma = kernel.gamma + param.coef0 = kernel.coef0 + elif isinstance(kernel, RBFKernel): + param.kernel_type = libsvm.RBF + param.gamma = kernel.gamma + elif isinstance(kernel, SigmoidKernel): + param.kernel_type = libsvm.SIGMOID + param.gamma = kernel.gamma + param.coef0 = kernel.coef0 + else: + raise ValueError, 'unknown kernel type' - self.check_problem_param(problem, param) - model = libsvm.svm_train(problem, param) - self.results = self.Results(self.dtype, model) + param.svm_type = svm_type + param.eps = tolerance + param.shrinking = shrinking + param.cache_size = cache_size - # XXX find better way to keep svm_data reference - self.results.svm_data = svm_data + self.param = param - return self.results - - def predict(self, x): - return self.results.predict(svm_data) - - def setup_svm_parameter(self, svm_data): - param = libsvm.svm_parameter() - param.svm_type = getattr(self, 'svm_type') - param.kernel_type = getattr(self.dtype, 'kernel_type') - param.degree = getattr(self.dtype, 'degree', 0) - if hasattr(self.dtype, 'gamma') and self.dtype.gamma is None: - maxlen = 0 - for x in svm_data: - maxlen = max(maxlen, x[1]['index'][:-1].max()) - param.gamma = 1.0/maxlen - else: - param.gamma = getattr(self.dtype, 'gamma', 0.0) - param.coef0 = getattr(self.dtype, 'coef0', 0) - param.cache_size = getattr(self, 'cache_size') - param.eps = getattr(self, 'tol') - param.C = getattr(self, 'cost', 0.0) - # XXX nr_weight, weight_label, weight - param.nr_weight = 0 - # XXX setting these to None zeros svm_type - ###param.weight_label = None - ###param.weight = None - param.nu = getattr(self, 'nu', 0.0) - param.p = getattr(self, 'epsilon', 0.0) - param.shrinking = getattr(self, 'shrinking') - param.probability = 0 - return param - - def setup_svm_problem(self, svm_data): + def fit(self, dataset): + # XXX don't poke around in dataset's internals problem = libsvm.svm_problem() - problem.l = len(svm_data) + problem.l = len(dataset.data) y = (c_double*problem.l)() x = (POINTER(libsvm.svm_node)*problem.l)() - for i, (label, node) in enumerate(svm_data): - y[i] = label - x[i] = utils.array_as_ctype(node, libsvm.svm_node) + for i, (yi, xi) in enumerate(dataset.data): + y[i] = yi + x[i] = cast(xi.ctypes.data, POINTER(libsvm.svm_node)) problem.x = cast(addressof(x), POINTER(POINTER(libsvm.svm_node))) problem.y = cast(addressof(y), POINTER(c_double)) - return problem, x, y - def check_problem_param(self, problem, param): + self._check_problem_param(problem, self.param) + model = libsvm.svm_train(problem, self.param) + + def _check_problem_param(self, problem, param): error_msg = libsvm.svm_check_parameter(problem, param) if error_msg: raise ValueError, error_msg Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-07 02:00:33 UTC (rev 2050) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-07 02:33:26 UTC (rev 2051) @@ -1,51 +1,40 @@ -from model import Model -from results import Results +__all__ = [ + 'LibSvmEpsilonRegressionModel', + 'LibSvmNuRegressionModel' + ] + +from model import LibSvmModel import libsvm -import utils +""" class RegressionResults(Results): - def __init__(self, dtype, model): - Results.__init__(self, dtype, model) + def __init__(self, model): + Results.__init__(self, model) model = model.contents self.rho = model.rho[0] self.sv_coef = model.sv_coef[0][:model.l] - def predict(self, x): - x = self.dtype.convert_test_data(x) - xptr = utils.array_as_ctype(x, libsvm.svm_node) - return libsvm.svm_predict(self.model, xptr) + def predict(self, dataset): + #x = self.dtype.convert_test_data(x) + #xptr = utils.array_as_ctype(x, libsvm.svm_node) + #return libsvm.svm_predict(self.model, xptr) + raise NotImplementedError +""" -class EpsilonSVRModel(Model): - """ - A model for epsilon-SV regression. - - See also: - - - Smola, Scholkopf: A Tutorial on Support Vector Regression - - Gunn: Support Vector Machines for Classification and Regression - - Muller, Vapnik: Using Support Vector Machines for Time Series - Prediction - """ - - Results = RegressionResults - - def __init__(self, dtype, cost=1.0, epsilon=0.1, **kwargs): - Model.__init__(self, dtype, **kwargs) - self.svm_type = libsvm.EPSILON_SVR - self.cost = cost +class LibSvmEpsilonRegressionModel(LibSvmModel): + def __init__(self, kernel, epsilon=0.1, cost=1.0, **kwargs): + LibSvmModel.__init__(self, libsvm.EPSILON_SVR, kernel, **kwargs) self.epsilon = epsilon - -class NuSVRModel(Model): - """ - A model for nu-SV regression. - - See also: Scholkopf, et al.: New Support Vector Algorithms - """ - - Results = RegressionResults - - def __init__(self, dtype, cost=1.0, nu=0.5, **kwargs): - Model.__init__(self, dtype, **kwargs) - self.svm_type = libsvm.NU_SVR self.cost = cost + self.param.p = epsilon + self.param.C = cost + self.param.probability = 1 + +class LibSvmNuRegressionModel(LibSvmModel): + def __init__(self, kernel, nu=0.5, cost=1.0, **kwargs): + LibSvmModel.__init__(self, libsvm.NU_SVR, kernel, **kwargs) self.nu = nu + self.cost = cost + self.param.nu = nu + self.param.C = cost + self.param.probability = 1 Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-07 02:00:33 UTC (rev 2050) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-07 02:33:26 UTC (rev 2051) @@ -1,22 +1,31 @@ from numpy.testing import * +import numpy as N -# XXX remove this -import os, sys -sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))) +from svm.regression import * +from svm.dataset import LibSvmRegressionDataSet +from svm.kernel import LinearKernel -import svm -import numpy as N +class test_regression(NumpyTestCase): + def check_basics(self): + Model = LibSvmEpsilonRegressionModel + Kernel = LinearKernel() + Model(Kernel) + Model(Kernel, epsilon=0.1) + Model(Kernel, cost=1.0) + model = Model(Kernel, shrinking=False) + self.assert_(not model.shrinking) -class test_regression(NumpyTestCase): - def check_epsilon_svr(self): + def check_epsilon(self): y = [10., 20., 30., 40.] - x = [[0, 0], [0, 1], [1, 0], [1, 1]] - data = zip(y, x) - dtype = svm.LinearData() - model = svm.EpsilonSVRModel(dtype, cost=10.0, epsilon=0.1) - results = model.fit(data) - for label, sample in data: - print results.predict(sample) + x = [N.array([0, 0]), + N.array([0, 1]), + N.array([1, 0]), + N.array([1, 1])] + dataset = LibSvmRegressionDataSet(zip(y, x)) + Model = LibSvmEpsilonRegressionModel + model = Model(LinearKernel()) + model.fit(dataset) + if __name__ == '__main__': NumpyTest().run() From scipy-svn at scipy.org Fri Jul 7 17:43:54 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 7 Jul 2006 16:43:54 -0500 (CDT) Subject: [Scipy-svn] r2052 - trunk/Lib/ndimage Message-ID: <20060707214354.6AECE383DC5@new.scipy.org> Author: stefan Date: 2006-07-07 16:43:47 -0500 (Fri, 07 Jul 2006) New Revision: 2052 Modified: trunk/Lib/ndimage/__init__.py Log: Enable ndimage on 64-bit systems. Modified: trunk/Lib/ndimage/__init__.py =================================================================== --- trunk/Lib/ndimage/__init__.py 2006-07-07 02:33:26 UTC (rev 2051) +++ trunk/Lib/ndimage/__init__.py 2006-07-07 21:43:47 UTC (rev 2052) @@ -29,8 +29,6 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import numpy -if numpy.int_ == numpy.int64: - raise ImportError, "ndimage does not yet support 64-bit systems" from filters import * from fourier import * from interpolation import * From scipy-svn at scipy.org Fri Jul 7 18:16:18 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 7 Jul 2006 17:16:18 -0500 (CDT) Subject: [Scipy-svn] r2053 - in trunk/Lib/sandbox/svm: . libsvm-2.82 tests Message-ID: <20060707221618.133B6D40FB4@new.scipy.org> Author: fullung Date: 2006-07-07 17:15:59 -0500 (Fri, 07 Jul 2006) New Revision: 2053 Removed: trunk/Lib/sandbox/svm/results.py Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_libsvm.py trunk/Lib/sandbox/svm/tests/test_oneclass.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Predict stuff. Added some docs. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -1,17 +1,26 @@ -from model import Model -from results import Results -import libsvm -import utils +__all__ = [ + 'LibSvmCClassificationModel', + 'LibSvmNuClassificationModel', + ] import numpy as N -from ctypes import c_double +from ctypes import cast, POINTER, c_int, c_double -# XXX classification models need weights +from model import LibSvmModel +import libsvm -class ClassificationResults(Results): - def __init__(self, dtype, model): - Results.__init__(self, dtype, model) +class LibSvmClassificationResults: + def __init__(self, model, dataset): + self.model = model + # keep a reference to the dataset here because the support + # vectors point to some or all of the vectors in the dataset + self.dataset = dataset + # XXX this is probably suboptimal when training many models + # that each only have a few support vectors. look at some + # options when we do memory optimization. + model = model.contents + self.nr_class = model.nr_class self.labels = model.labels[:self.nr_class] self.rho = model.rho[:self.nr_class*(self.nr_class-1)/2] self.nSV = model.nSV[:self.nr_class] @@ -20,62 +29,137 @@ sv_coef[i,:] = c[:model.l] self.sv_coef = sv_coef - def predict(self, x): - x = self.dtype.convert_test_data(x) - xptr = utils.array_as_ctype(x, libsvm.svm_node) - return int(libsvm.svm_predict(self.model, xptr)) + def __del__(self): + libsvm.svm_destroy_model(self.model) - def predict_values(self, x): - x = self.dtype.convert_test_data(x) - n = self.nr_class*(self.nr_class-1)/2 - v = N.empty((n,), dtype=N.float64) - xptr = utils.array_as_ctype(x, libsvm.svm_node) - vptr = utils.array_as_ctype(v, c_double) - libsvm.svm_predict_values(self.model, xptr, vptr) - count = 0 - d = {} - for i in range(len(self.labels)): - for j in range(i+1, len(self.labels)): - d[self.labels[i], self.labels[j]] = v[count] - d[self.labels[j], self.labels[i]] = -v[count] - count += 1 - return d + def predict(self, dataset): + """ + This function does classification on a test vector x and + returns the label of the predicted class. + """ + def p(x): + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + return libsvm.svm_predict(self.model, xptr) + return map(p, dataset.data) -class ClassificationModel(Model): - Results = ClassificationResults + def predict_values(self, dataset): + """ + This function does classification on a test vector x and + returns decision values. - def __init__(self, dtype, **kwargs): - Model.__init__(self, dtype, **kwargs) + For training data with nr_class classes, this function returns + nr_class*(nr_class-1)/2 decision values in a dictionary. The + keys of the dictionary are 2-tuples, one for each combination + of two class labels. + """ + def p(x): + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + n = self.nr_class*(self.nr_class-1)/2 + v = N.empty((n,), dtype=N.float64) + vptr = cast(v.ctypes.data, POINTER(c_double)) + libsvm.svm_predict_values(self.model, xptr, vptr) + count = 0 + d = {} + for i in range(len(self.labels)): + for j in range(i+1, len(self.labels)): + d[self.labels[i], self.labels[j]] = v[count] + d[self.labels[j], self.labels[i]] = -v[count] + count += 1 + return d + return map(p, dataset.data) - def predict_values(self, x): - return self.results.predict_values(svm_data) + def predict_probability(self, x): + """ + This function does classification on a test vector x for a + model with probability information. -class CSVCModel(ClassificationModel): + This function returns a 2-tuple. The first item is the label + of the class with the highest probability. The second item is + a list of all the class probabilities. + """ + raise NotImplementedError + +class LibSvmClassificationModel(LibSvmModel): """ + A model for support vector classification. + + Classification models can predict a class label, decision values + over all classes or a posterior class probability. + + See also: + + - Platt. Probabilistic Outputs for Support Vector Machines and + Comparisons to Regularized Likelihood Methods. + - Lin. A Note on Platt's Probabilistic Outputs for Support Vector + Machines. + """ + + Results = LibSvmClassificationResults + + def __init__(self, kernel, weights, **kwargs): + LibSvmModel.__init__(self, kernel, **kwargs) + if weights is not None: + # XXX check whether labels need to be sorted + self.weight_labels = N.empty((len(weights),), dtype=N.intp) + self.weights = N.empty((len(weights),), dtype=N.float64) + for i, (label, weight) in enumerate(weights): + self.weight_labels[i] = label + self.weights[i] = weight + + self.param.nr_weight = len(weights) + self.param.weight_label = \ + cast(self.weight_labels.ctypes.data, POINTER(c_int)) + self.param.weight = \ + cast(self.weights.ctypes.data, POINTER(c_double)) + +class LibSvmCClassificationModel(LibSvmClassificationModel): + """ A model for C-SV classification. See also: - - Hsu, et al.: A Practical Guide to Support Vector Classification - - Gunn: Support Vector Machines for Classification and Regression + - Hsu, et al. A Practical Guide to Support Vector Classification. + - Gunn. Support Vector Machines for Classification and Regression. + - Burges. A Tutorial on Support Vector Machines for Pattern + Recognition. """ - def __init__(self, dtype, cost=1.0, **kwargs): - ClassificationModel.__init__(self, dtype, **kwargs) - self.svm_type = libsvm.C_SVC + def __init__(self, kernel, cost=1.0, weights=None, **kwargs): + """ + Parameters: + + - `cost`: XXX + - `weights`: XXX + """ + LibSvmClassificationModel.__init__(self, kernel, weights, **kwargs) self.cost = cost + self.param.svm_type = libsvm.C_SVC + self.param.C = cost + # always train probability model parameters + # XXX review this decision when we do performance optimization + self.param.probability = True -class NuSVCModel(ClassificationModel): +class LibSvmNuClassificationModel(LibSvmClassificationModel): """ A model for nu-SV classification. See also: - - Chen, et al.: A Tutorial on nu-Support Vector Machines - - Scholkopf, et al.: New Support Vector Algorithms + - Chen, et al. A Tutorial on nu-Support Vector Machines. + - Scholkopf, et al. New Support Vector Algorithms. """ - def __init__(self, dtype, nu=0.5, **kwargs): - ClassificationModel.__init__(self, dtype, **kwargs) - self.svm_type = libsvm.NU_SVC + def __init__(self, kernel, nu=0.5, weights=None, **kwargs): + """ + Parameters: + + - `nu`: XXX + - `weights`: XXX + """ + LibSvmClassificationModel.__init__(self, kernel, weights, **kwargs) self.nu = nu + self.param.svm_type = libsvm.NU_SVC + self.param.nu = nu + # always train probability model parameters + # XXX review this decision when we do performance optimization + self.param.probability = True Modified: trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win =================================================================== --- trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win 2006-07-07 22:15:59 UTC (rev 2053) @@ -6,7 +6,7 @@ CXXFLAGSDBG = -MDd -Od -Z7 -RTCcsu CXXFLAGSOPT = -MD -O2 CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSDBG) -#CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSDBG) +#CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSOPT) LINKFLAGS = /nologo /DLL Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/model.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -8,18 +8,15 @@ import libsvm class LibSvmModel: - def __init__(self, svm_type, kernel, - tolerance=0.001, shrinking=True, cache_size=40): + def __init__(self, kernel, tolerance=0.001, shrinking=True, cache_size=40): """ Parameters: - - `svm_type`: XXX - `kernel`: XXX - `tolerance`: tolerance of termination criterion - `shrinking`: whether to use the shrinking heuristics - `cache_size` kernel evaluation cache size (MB) """ - self.svm_type = svm_type self.kernel = kernel self.tolerance = tolerance self.shrinking = shrinking @@ -44,15 +41,23 @@ else: raise ValueError, 'unknown kernel type' - param.svm_type = svm_type param.eps = tolerance param.shrinking = shrinking param.cache_size = cache_size + # set defaults for optional parameters + param.nr_weight = 0 + param.weight = None + param.weight_label = None + param.probability = False self.param = param def fit(self, dataset): # XXX don't poke around in dataset's internals + + # no reference to the svm_problem is kept because a svm_model + # only requires some parameters and the support vectors chosen + # from the dataset problem = libsvm.svm_problem() problem.l = len(dataset.data) y = (c_double*problem.l)() @@ -62,10 +67,16 @@ x[i] = cast(xi.ctypes.data, POINTER(libsvm.svm_node)) problem.x = cast(addressof(x), POINTER(POINTER(libsvm.svm_node))) problem.y = cast(addressof(y), POINTER(c_double)) + self._check_problem_param(problem, self.param) - self._check_problem_param(problem, self.param) model = libsvm.svm_train(problem, self.param) + # XXX because libsvm only does a shallow copy of the + # svm_parameter into the model, we have to make sure that a + # reference to weight labels and weights are kept somewhere + + return self.Results(model, dataset) + def _check_problem_param(self, problem, param): error_msg = libsvm.svm_check_parameter(problem, param) if error_msg: Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -1,41 +1,49 @@ -from model import Model -from results import Results +__all__ = [ + 'LibSvmOneClassModel' + ] + +from ctypes import cast, POINTER + +from model import LibSvmModel import libsvm -import utils -from ctypes import c_double, byref - -class OneClassResults(Results): - def __init__(self, dtype, model): - Results.__init__(self, dtype, model) +class LibSvmOneClassResults: + def __init__(self, model, dataset): + self.model = model + self.dataset = dataset model = model.contents self.rho = model.rho[0] self.sv_coef = model.sv_coef[0][:model.l] - def predict_values(self, x): - x = self.dtype.convert_test_data(x) - v = c_double(0.0) - xptr = utils.array_as_ctype(x, libsvm.svm_node) - libsvm.svm_predict_values(self.model, xptr, byref(v)) - return v.value + def __del__(self): + libsvm.svm_destroy_model(self.model) + + def predict(self, dataset): + def p(x): + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + # XXX maybe we want to cast return value to int + return libsvm.svm_predict(self.model, xptr) + return map(p, dataset.data) - def predict(self, x): - x = self.dtype.convert_test_data(x) - xptr = utils.array_as_ctype(x, libsvm.svm_node) - v = libsvm.svm_predict(self.model, xptr) - return int(v) + # XXX predict_values might also be useful -class OneClassModel(Model): +class LibSvmOneClassModel(LibSvmModel): """ A model for distribution estimation (one-class SVM). - See also: Scholkopf, et al.: Estimating the Support of a - High-Dimensional Distribution + See also: Scholkopf, et al. Estimating the Support of a + High-Dimensional Distribution. """ - Results = OneClassResults + Results = LibSvmOneClassResults - def __init__(self, dtype, nu=0.5, **kwargs): - Model.__init__(self, dtype, **kwargs) - self.svm_type = libsvm.ONE_CLASS + def __init__(self, kernel, nu=0.5, **kwargs): + """ + Parameters: + + - `nu`: XXX + """ + LibSvmModel.__init__(self, kernel, **kwargs) self.nu = nu + self.param.svm_type = libsvm.ONE_CLASS + self.param.nu = nu Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -3,38 +3,85 @@ 'LibSvmNuRegressionModel' ] +from ctypes import cast, POINTER + from model import LibSvmModel import libsvm -""" -class RegressionResults(Results): - def __init__(self, model): - Results.__init__(self, model) +# XXX document why get_svr_probability could be useful + +class LibSvmRegressionResults: + def __init__(self, model, dataset): + self.model = model + self.dataset = dataset model = model.contents self.rho = model.rho[0] self.sv_coef = model.sv_coef[0][:model.l] + self.sigma = model.probA[0] + def __del__(self): + libsvm.svm_destroy_model(self.model) + def predict(self, dataset): - #x = self.dtype.convert_test_data(x) - #xptr = utils.array_as_ctype(x, libsvm.svm_node) - #return libsvm.svm_predict(self.model, xptr) - raise NotImplementedError -""" + """ + This function does regression on a test vector x and returns + the function value of x calculated using the model. + """ + def p(x): + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + return libsvm.svm_predict(self.model, xptr) + return map(p, dataset.data) + def get_svr_probability(self): + """ + This function returns a value sigma > 0, which is a parameter + in the probability model for the test data. + + For test data, we consider the probability model: + + target value = predicted value + z + + where z is the Laplace distribution: e^(-|x|/sigma)/(2*sigma). + """ + return self.sigma + class LibSvmEpsilonRegressionModel(LibSvmModel): + """ + A model for epsilon-SV regression. + + See also: + + - Smola, Schoelkopf. A Tutorial on Support Vector Regression. + - Gunn. Support Vector Machines for Classification and Regression. + - Mueller, Vapnik. Using Support Vector Machines for Time Series + Prediction. + """ + + Results = LibSvmRegressionResults + def __init__(self, kernel, epsilon=0.1, cost=1.0, **kwargs): - LibSvmModel.__init__(self, libsvm.EPSILON_SVR, kernel, **kwargs) + LibSvmModel.__init__(self, kernel, **kwargs) self.epsilon = epsilon self.cost = cost + self.param.svm_type = libsvm.EPSILON_SVR self.param.p = epsilon self.param.C = cost - self.param.probability = 1 + self.param.probability = True class LibSvmNuRegressionModel(LibSvmModel): + """ + A model for nu-SV regression. + + See also: Schoelkopf, et al. New Support Vector Algorithms. + """ + + Results = LibSvmRegressionResults + def __init__(self, kernel, nu=0.5, cost=1.0, **kwargs): - LibSvmModel.__init__(self, libsvm.NU_SVR, kernel, **kwargs) + LibSvmModel.__init__(self, kernel, **kwargs) self.nu = nu self.cost = cost + self.param.svm_type = libsvm.NU_SVR self.param.nu = nu self.param.C = cost - self.param.probability = 1 + self.param.probability = True Deleted: trunk/Lib/sandbox/svm/results.py =================================================================== --- trunk/Lib/sandbox/svm/results.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/results.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -1,22 +0,0 @@ -__all__ = [ - 'ClassificationResults', - 'RegressionResults', - 'OneClassResults' - ] - -import libsvm -import utils - -import numpy as N -from ctypes import * - -class Results: - def __init__(self, dtype, model): - self.dtype = dtype - self.model = model - model = model.contents - self.svm_type = model.param.svm_type - self.nr_class = model.nr_class - - def __del__(self): - libsvm.svm_destroy_model(self.model) Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -1,34 +1,40 @@ from numpy.testing import * +import numpy as N -# XXX remove this -import os, sys -sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))) +from svm.classification import * +from svm.dataset import LibSvmClassificationDataSet +from svm.dataset import LibSvmTestDataSet +from svm.kernel import * -import svm -import numpy as N - class test_classification(NumpyTestCase): - def check_csvc(self): - labels = [0, 1, 1, 2] - samples = [[0, 0], [0, 1], [1, 0], [1, 1]] - data = zip(labels, samples) - dtype = svm.LinearData() - model = svm.CSVCModel(dtype, cost=10.0) - results = model.fit(data) - for label, sample in data: - assert_equal(results.predict(sample), label) - v = results.predict_values(sample) + def check_basics(self): + Model = LibSvmCClassificationModel + Kernel = LinearKernel() + Model(Kernel) + Model(Kernel, cost=1.0) + weights = [(2, 10.0), (1, 20.0), (0, 30.0)] + Model(Kernel, weights=weights) + Model(Kernel, 1.0, weights) + model = Model(Kernel, cost=1.0, weights=weights) - def check_nusvc(self): + def check_c_train(self): labels = [0, 1, 1, 2] - samples = [[0, 0], [0, 1], [1, 0], [1, 1]] - data = zip(labels, samples) - dtype = svm.LinearData() - model = svm.NuSVCModel(dtype, nu=0.5) - results = model.fit(data) - for label, sample in data: - assert_equal(results.predict(sample), label) - v = results.predict_values(sample) + x = [N.array([0, 0]), + N.array([0, 1]), + N.array([1, 0]), + N.array([1, 1])] + traindata = LibSvmClassificationDataSet(zip(labels, x)) + Model = LibSvmCClassificationModel + model = Model(RBFKernel(traindata.gamma)) + results = model.fit(traindata) + + testdata = LibSvmTestDataSet(x) + results.predict(testdata) + results.predict_values(testdata) + + def check_nu_train(self): + pass + if __name__ == '__main__': NumpyTest().run() Modified: trunk/Lib/sandbox/svm/tests/test_libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_libsvm.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/tests/test_libsvm.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -1,13 +1,9 @@ from numpy.testing import * +import numpy as N -# XXX remove this -import os, sys -sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))) - import svm.libsvm as libsvm -import numpy as N -class test_structs(NumpyTestCase): +class test_libsvm(NumpyTestCase): def check_svm_node(self): node = libsvm.svm_node() node = N.empty((), dtype=libsvm.svm_node_dtype) Modified: trunk/Lib/sandbox/svm/tests/test_oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -1,25 +1,31 @@ from numpy.testing import * +import numpy as N -# XXX remove this -import os, sys -sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))) +from svm.oneclass import * +from svm.dataset import LibSvmOneClassDataSet +from svm.dataset import LibSvmTestDataSet +from svm.kernel import * -import svm -import numpy as N - class test_oneclass(NumpyTestCase): - def check_oneclass(self): - data = [[0, 0], [0, 1], [1, 0], [1, 1]] - dtype = svm.LinearOneClassData() - model = svm.OneClassModel(dtype, nu=0.5) - results = model.fit(data) - for sample in data: - print results.predict_values(sample) + def check_basics(self): + Model = LibSvmOneClassModel + Kernel = LinearKernel() + Model(Kernel) + Model(Kernel, nu=1.0) - print results.predict_values([0.2, 0.2]) - print results.predict_values([2., 2.]) - print results.predict([0.2, 0.2]) - print results.predict([2., 2.]) + def check_train(self): + x = [N.array([0, 0]), + N.array([0, 1]), + N.array([1, 0]), + N.array([1, 1])] + dataset = LibSvmOneClassDataSet(x) + + Model = LibSvmOneClassModel + model = Model(LinearKernel()) + results = model.fit(dataset) + testdata = LibSvmTestDataSet(x) + results.predict(testdata) + if __name__ == '__main__': NumpyTest().run() Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-07 21:43:47 UTC (rev 2052) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-07 22:15:59 UTC (rev 2053) @@ -3,7 +3,8 @@ from svm.regression import * from svm.dataset import LibSvmRegressionDataSet -from svm.kernel import LinearKernel +from svm.dataset import LibSvmTestDataSet +from svm.kernel import * class test_regression(NumpyTestCase): def check_basics(self): @@ -15,7 +16,7 @@ model = Model(Kernel, shrinking=False) self.assert_(not model.shrinking) - def check_epsilon(self): + def check_epsilon_train(self): y = [10., 20., 30., 40.] x = [N.array([0, 0]), N.array([0, 1]), @@ -25,7 +26,14 @@ Model = LibSvmEpsilonRegressionModel model = Model(LinearKernel()) - model.fit(dataset) + results = model.fit(dataset) + testdata = LibSvmTestDataSet(x) + results.predict(testdata) + results.get_svr_probability() + + def check_nu_train(self): + pass + if __name__ == '__main__': NumpyTest().run() From scipy-svn at scipy.org Fri Jul 7 21:51:18 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 7 Jul 2006 20:51:18 -0500 (CDT) Subject: [Scipy-svn] r2054 - in trunk/Lib: . cluster/src io ndimage ndimage/src optimize sandbox/delaunay sandbox/numexpr sandbox/xplt/pygist signal special Message-ID: <20060708015118.41ED1780693@new.scipy.org> Author: oliphant Date: 2006-07-07 20:51:06 -0500 (Fri, 07 Jul 2006) New Revision: 2054 Modified: trunk/Lib/cluster/src/swig_num.i trunk/Lib/cluster/src/vq_wrap.cpp trunk/Lib/io/numpyiomodule.c trunk/Lib/ndimage/setup.py trunk/Lib/ndimage/src/nd_image.h trunk/Lib/optimize/minpack.h trunk/Lib/sandbox/delaunay/_delaunay.cpp trunk/Lib/sandbox/numexpr/interpreter.c trunk/Lib/sandbox/xplt/pygist/gistCmodule.c trunk/Lib/setup.py trunk/Lib/signal/C_bspline_util.c trunk/Lib/signal/D_bspline_util.c trunk/Lib/signal/S_bspline_util.c trunk/Lib/signal/Z_bspline_util.c trunk/Lib/signal/medianfilter.c trunk/Lib/signal/sigtools.h trunk/Lib/signal/splinemodule.c trunk/Lib/special/ufunc_extras.h Log: Get compilation of SciPy except for ndimage. Modified: trunk/Lib/cluster/src/swig_num.i =================================================================== --- trunk/Lib/cluster/src/swig_num.i 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/cluster/src/swig_num.i 2006-07-08 01:51:06 UTC (rev 2054) @@ -279,7 +279,7 @@ //PyErr_SetString(PyExc_ValueError, "error converting internal data to array"); return NULL; } - res->flags |= OWN_DATA; // we want the array to deallocate mem when it is finished. + res->flags |= NPY_OWNDATA; // we want the array to deallocate mem when it is finished. // stick result in the output tuple (target). // Need to think about generality of this one... return (PyObject *) res; Modified: trunk/Lib/cluster/src/vq_wrap.cpp =================================================================== --- trunk/Lib/cluster/src/vq_wrap.cpp 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/cluster/src/vq_wrap.cpp 2006-07-08 01:51:06 UTC (rev 2054) @@ -560,7 +560,7 @@ } -#include "numpy/arrayobject.h" +#include "numpy/noprefix.h" // hmmm. how do we prevent typedefs from conflicting // with users definition on complex numbers? @@ -731,7 +731,7 @@ //PyErr_SetString(PyExc_ValueError, "error converting internal data to array"); return NULL; } - res->flags |= OWN_DATA; // we want the array to deallocate mem when it is finished. + res->flags |= NPY_OWNDATA; // we want the array to deallocate mem when it is finished. // stick result in the output tuple (target). // Need to think about generality of this one... return (PyObject *) res; Modified: trunk/Lib/io/numpyiomodule.c =================================================================== --- trunk/Lib/io/numpyiomodule.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/io/numpyiomodule.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -41,7 +41,7 @@ #define OBJECTTYPE(arr) ((arr)->descr->type_num) #define BASEOBJ(arr) ((PyArrayObject *)((arr)->base)) #define RANK(arr) ((arr)->nd) -#define ISCONTIGUOUS(m) ((m)->flags & CONTIGUOUS) +#define ISCONTIGUOUS(m) ((m)->flags & NPY_CONTIGUOUS) #define PYSETERROR(message) \ { PyErr_SetString(ErrorObject, message); goto fail; } @@ -635,7 +635,7 @@ } size = PySequence_Size(res); - for (k=0; k < MIN(size,2); k++) { + for (k=0; k < NPY_MIN(size,2); k++) { elobj = PySequence_GetItem(res, k); if (elobj == NULL) goto fail; elN = PyString_Size(elobj); @@ -695,7 +695,7 @@ switch(ctype) { case 'c': return PyArray_CHAR; case 'b': return PyArray_UBYTE; - case '1': return PyArray_SBYTE; + case '1': return PyArray_BYTE; case 's': return PyArray_SHORT; case 'i': return PyArray_INT; #ifdef PyArray_UNSIGNED_TYPES Modified: trunk/Lib/ndimage/setup.py =================================================================== --- trunk/Lib/ndimage/setup.py 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/ndimage/setup.py 2006-07-08 01:51:06 UTC (rev 2054) @@ -8,7 +8,7 @@ config.add_extension("_nd_image", sources=["src/nd_image.c","src/ni_filters.c", "src/ni_fourier.c","src/ni_interpolation.c", - "src/ni_measure.c","src/numcompat.c", + "src/ni_measure.c", "src/ni_morphology.c","src/ni_support.c"], include_dirs=['src'], ) Modified: trunk/Lib/ndimage/src/nd_image.h =================================================================== --- trunk/Lib/ndimage/src/nd_image.h 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/ndimage/src/nd_image.h 2006-07-08 01:51:06 UTC (rev 2054) @@ -36,7 +36,7 @@ #define NO_IMPORT_ARRAY #endif #include "Python.h" -#include "numcompat.h" +#include "numpy/libnumarray.h" #define NI_MAXDIM MAXDIM Modified: trunk/Lib/optimize/minpack.h =================================================================== --- trunk/Lib/optimize/minpack.h 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/optimize/minpack.h 2006-07-08 01:51:06 UTC (rev 2054) @@ -33,7 +33,7 @@ #define PYERR(errobj,message) {PyErr_SetString(errobj,message); goto fail;} #define PYERR2(errobj,message) {PyErr_Print(); PyErr_SetString(errobj, message); goto fail;} -#define ISCONTIGUOUS(m) ((m)->flags & CONTIGUOUS) +#define ISCONTIGUOUS(m) ((m)->flags & NPY_CONTIGUOUS) #define STORE_VARS() PyObject *store_multipack_globals[4]; int store_multipack_globals3; Modified: trunk/Lib/sandbox/delaunay/_delaunay.cpp =================================================================== --- trunk/Lib/sandbox/delaunay/_delaunay.cpp 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/sandbox/delaunay/_delaunay.cpp 2006-07-08 01:51:06 UTC (rev 2054) @@ -5,7 +5,7 @@ #include "VoronoiDiagramGenerator.h" #include "delaunay_utils.h" #include "natneighbors.h" -#include "numpy/arrayobject.h" +#include "numpy/noprefix.h" using namespace std; Modified: trunk/Lib/sandbox/numexpr/interpreter.c =================================================================== --- trunk/Lib/sandbox/numexpr/interpreter.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/sandbox/numexpr/interpreter.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -1,6 +1,6 @@ #include "Python.h" #include "structmember.h" -#include "numpy/arrayobject.h" +#include "numpy/noprefix.h" #include "math.h" #include "complex_functions.inc" Modified: trunk/Lib/sandbox/xplt/pygist/gistCmodule.c =================================================================== --- trunk/Lib/sandbox/xplt/pygist/gistCmodule.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/sandbox/xplt/pygist/gistCmodule.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -187,6 +187,7 @@ #include "pyfpe.h" #include "numpy/arrayobject.h" +#define OWN_DATA NPY_OWNDATA #include "hlevel.h" #include "pstdlib.h" Modified: trunk/Lib/setup.py =================================================================== --- trunk/Lib/setup.py 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/setup.py 2006-07-08 01:51:06 UTC (rev 2054) @@ -19,7 +19,7 @@ config.add_subpackage('sparse') config.add_subpackage('special') config.add_subpackage('stats') - config.add_subpackage('ndimage') + #config.add_subpackage('ndimage') config.add_subpackage('weave') config.make_svn_version_py() # installs __svn_version__.py config.make_config_py() Modified: trunk/Lib/signal/C_bspline_util.c =================================================================== --- trunk/Lib/signal/C_bspline_util.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/signal/C_bspline_util.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -26,7 +26,7 @@ void C_IIR_order2_cascade (__complex__ float,__complex__ float,__complex__ float,__complex__ float,__complex__ float*,__complex__ float*,int,int,int); int C_IIR_forback1(__complex__ float,__complex__ float,__complex__ float*,__complex__ float*,int,int,int,float); void C_FIR_mirror_symmetric(__complex__ float*,__complex__ float*,int,__complex__ float*,int,int,int); -int C_separable_2Dconvolve_mirror(__complex__ float*,__complex__ float*,int,int,__complex__ float*,__complex__ float*,int,int,intp*,intp*); +int C_separable_2Dconvolve_mirror(__complex__ float*,__complex__ float*,int,int,__complex__ float*,__complex__ float*,int,int,npy_intp*,npy_intp*); void C_IIR_order1 (a1, a2, x, y, N, stridex, stridey) @@ -262,7 +262,7 @@ int M, N; __complex__ float *hr, *hc; int Nhr, Nhc; - intp *instrides, *outstrides; + npy_intp *instrides, *outstrides; { int m, n; __complex__ float *tmpmem; Modified: trunk/Lib/signal/D_bspline_util.c =================================================================== --- trunk/Lib/signal/D_bspline_util.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/signal/D_bspline_util.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -21,10 +21,10 @@ void D_IIR_order2_cascade(double,double,double,double,double*,double*,int,int,int); int D_IIR_forback1(double,double,double*,double*,int,int,int,double); void D_FIR_mirror_symmetric(double*,double*,int,double*,int,int,int); -int D_separable_2Dconvolve_mirror(double*,double*,int,int,double*,double*,int,int,intp*,intp*); +int D_separable_2Dconvolve_mirror(double*,double*,int,int,double*,double*,int,int,npy_intp*,npy_intp*); int D_IIR_forback2(double,double,double*,double*,int,int,int,double); -int D_cubic_spline2D(double*,double*,int,int,double,intp*,intp*,double); -int D_quadratic_spline2D(double*,double*,int,int,double,intp*,intp*,double); +int D_cubic_spline2D(double*,double*,int,int,double,npy_intp*,npy_intp*,double); +int D_quadratic_spline2D(double*,double*,int,int,double,npy_intp*,npy_intp*,double); /* Implement the following difference equation */ /* y[n] = a1 * x[n] + a2 * y[n-1] */ @@ -264,7 +264,7 @@ int M, N; double *hr, *hc; int Nhr, Nhc; - intp *instrides, *outstrides; + npy_intp *instrides, *outstrides; { int m, n; double *tmpmem; @@ -505,7 +505,7 @@ double *coeffs; int M, N; double lambda; - intp *strides, *cstrides; + npy_intp *strides, *cstrides; double precision; { double r, omega; @@ -598,7 +598,7 @@ double *coeffs; int M, N; double lambda; - intp *strides, *cstrides; + npy_intp *strides, *cstrides; double precision; { double r; Modified: trunk/Lib/signal/S_bspline_util.c =================================================================== --- trunk/Lib/signal/S_bspline_util.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/signal/S_bspline_util.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -5,7 +5,7 @@ #include #include #define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" +#include "numpy/noprefix.h" void compute_root_from_lambda(double, double *, double *); Modified: trunk/Lib/signal/Z_bspline_util.c =================================================================== --- trunk/Lib/signal/Z_bspline_util.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/signal/Z_bspline_util.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -18,7 +18,7 @@ void Z_IIR_order2_cascade (__complex__ double,__complex__ double,__complex__ double,__complex__ double,__complex__ double*,__complex__ double*,int,int,int); int Z_IIR_forback1(__complex__ double,__complex__ double,__complex__ double*,__complex__ double*,int,int,int,double); void Z_FIR_mirror_symmetric(__complex__ double*,__complex__ double*,int,__complex__ double*,int,int,int); -int Z_separable_2Dconvolve_mirror(__complex__ double*,__complex__ double*,int,int,__complex__ double*,__complex__ double*,int,int,intp*,intp*); +int Z_separable_2Dconvolve_mirror(__complex__ double*,__complex__ double*,int,int,__complex__ double*,__complex__ double*,int,int,npy_intp*,npy_intp*); /* Implement the following difference equation */ /* y[n] = a1 * x[n] + a2 * y[n-1] */ @@ -260,7 +260,7 @@ int M, N; __complex__ double *hr, *hc; int Nhr, Nhc; - intp *instrides, *outstrides; + npy_intp *instrides, *outstrides; { int m, n; __complex__ double *tmpmem; Modified: trunk/Lib/signal/medianfilter.c =================================================================== --- trunk/Lib/signal/medianfilter.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/signal/medianfilter.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -9,7 +9,7 @@ #include "Python.h" #define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" +#include "numpy/noprefix.h" void f_medfilt2(float*,float*,intp*,intp*); void d_medfilt2(double*,double*,intp*,intp*); Modified: trunk/Lib/signal/sigtools.h =================================================================== --- trunk/Lib/signal/sigtools.h 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/signal/sigtools.h 2006-07-08 01:51:06 UTC (rev 2054) @@ -1,5 +1,5 @@ #include "Python.h" -#include "numpy/arrayobject.h" +#include "numpy/noprefix.h" #define BOUNDARY_MASK 12 #define OUTSIZE_MASK 3 Modified: trunk/Lib/signal/splinemodule.c =================================================================== --- trunk/Lib/signal/splinemodule.c 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/signal/splinemodule.c 2006-07-08 01:51:06 UTC (rev 2054) @@ -13,31 +13,31 @@ #define RANK(arr) ((arr)->nd) #define ISCONTIGUOUS(m) ((m)->flags & CONTIGUOUS) -static void convert_strides(intp*,intp*,int,int); +static void convert_strides(npy_intp*,npy_intp*,int,int); -extern int S_cubic_spline2D(float*,float*,int,int,double,intp*,intp*,float); -extern int S_quadratic_spline2D(float*,float*,int,int,double,intp*,intp*,float); +extern int S_cubic_spline2D(float*,float*,int,int,double,npy_intp*,npy_intp*,float); +extern int S_quadratic_spline2D(float*,float*,int,int,double,npy_intp*,npy_intp*,float); extern int S_IIR_forback1(float,float,float*,float*,int,int,int,float); extern int S_IIR_forback2(double,double,float*,float*,int,int,int,float); -extern int S_separable_2Dconvolve_mirror(float*,float*,int,int,float*,float*,int,int,intp*,intp*); +extern int S_separable_2Dconvolve_mirror(float*,float*,int,int,float*,float*,int,int,npy_intp*,npy_intp*); -extern int D_cubic_spline2D(double*,double*,int,int,double,intp*,intp*,double); -extern int D_quadratic_spline2D(double*,double*,int,int,double,intp*,intp*,double); +extern int D_cubic_spline2D(double*,double*,int,int,double,npy_intp*,npy_intp*,double); +extern int D_quadratic_spline2D(double*,double*,int,int,double,npy_intp*,npy_intp*,double); extern int D_IIR_forback1(double,double,double*,double*,int,int,int,double); extern int D_IIR_forback2(double,double,double*,double*,int,int,int,double); -extern int D_separable_2Dconvolve_mirror(double*,double*,int,int,double*,double*,int,int,intp*,intp*); +extern int D_separable_2Dconvolve_mirror(double*,double*,int,int,double*,double*,int,int,npy_intp*,npy_intp*); #ifdef __GNUC__ extern int C_IIR_forback1(__complex__ float,__complex__ float,__complex__ float*,__complex__ float*,int,int,int,float); -extern int C_separable_2Dconvolve_mirror(__complex__ float*,__complex__ float*,int,int,__complex__ float*,__complex__ float*,int,int,intp*,intp*); +extern int C_separable_2Dconvolve_mirror(__complex__ float*,__complex__ float*,int,int,__complex__ float*,__complex__ float*,int,int,npy_intp*,npy_intp*); extern int Z_IIR_forback1(__complex__ double,__complex__ double,__complex__ double*,__complex__ double*,int,int,int,double); -extern int Z_separable_2Dconvolve_mirror(__complex__ double*,__complex__ double*,int,int,__complex__ double*,__complex__ double*,int,int,intp*,intp*); +extern int Z_separable_2Dconvolve_mirror(__complex__ double*,__complex__ double*,int,int,__complex__ double*,__complex__ double*,int,int,npy_intp*,npy_intp*); #endif static void -convert_strides(intp* instrides,intp* convstrides,int size,int N) +convert_strides(npy_intp* instrides,npy_intp* convstrides,int size,int N) { - int n; intp bitshift; + int n; npy_intp bitshift; bitshift = -1; @@ -69,7 +69,7 @@ double lambda = 0.0; double precision = -1.0; int thetype, M, N, retval=0; - intp outstrides[2], instrides[2]; + npy_intp outstrides[2], instrides[2]; if (!PyArg_ParseTuple(args, "O|dd", &image, &lambda, &precision)) return NULL; @@ -126,7 +126,7 @@ double lambda = 0.0; double precision = -1.0; int thetype, M, N, retval=0; - intp outstrides[2], instrides[2]; + npy_intp outstrides[2], instrides[2]; if (!PyArg_ParseTuple(args, "O|dd", &image, &lambda, &precision)) return NULL; @@ -182,7 +182,7 @@ PyObject *image=NULL, *hrow=NULL, *hcol=NULL; PyArrayObject *a_image=NULL, *a_hrow=NULL, *a_hcol=NULL, *out=NULL; int thetype, M, N, ret; - intp outstrides[2], instrides[2]; + npy_intp outstrides[2], instrides[2]; if (!PyArg_ParseTuple(args, "OOO", &image, &hrow, &hcol)) return NULL; @@ -291,7 +291,7 @@ Py_complex c0, z1; double precision = -1.0; int thetype, N, ret; - intp outstrides, instrides; + npy_intp outstrides, instrides; if (!PyArg_ParseTuple(args, "ODD|d", &sig, &c0, &z1, &precision)) return NULL; @@ -411,7 +411,7 @@ double r, omega; double precision = -1.0; int thetype, N, ret; - intp outstrides, instrides; + npy_intp outstrides, instrides; if (!PyArg_ParseTuple(args, "Odd|d", &sig, &r, &omega, &precision)) return NULL; Modified: trunk/Lib/special/ufunc_extras.h =================================================================== --- trunk/Lib/special/ufunc_extras.h 2006-07-07 22:15:59 UTC (rev 2053) +++ trunk/Lib/special/ufunc_extras.h 2006-07-08 01:51:06 UTC (rev 2054) @@ -1,5 +1,5 @@ #include "Python.h" -#include "numpy/arrayobject.h" +#include "numpy/noprefix.h" typedef int IntFunc_d_dd(double x, double *y, double *z); typedef int IntFunc_d_dddd(double v, double *w, double *x, double *y, double *z); From scipy-svn at scipy.org Fri Jul 7 22:22:01 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 7 Jul 2006 21:22:01 -0500 (CDT) Subject: [Scipy-svn] r2055 - in trunk/Lib: . integrate ndimage ndimage/src signal weave Message-ID: <20060708022201.2BBEE780335@new.scipy.org> Author: oliphant Date: 2006-07-07 21:21:54 -0500 (Fri, 07 Jul 2006) New Revision: 2055 Modified: trunk/Lib/integrate/__odepack.h trunk/Lib/ndimage/setup.py trunk/Lib/ndimage/src/nd_image.h trunk/Lib/setup.py trunk/Lib/signal/splinemodule.c trunk/Lib/weave/c_spec.py trunk/Lib/weave/standard_array_spec.py Log: Fix so scipy installs and runs tests. Modified: trunk/Lib/integrate/__odepack.h =================================================================== --- trunk/Lib/integrate/__odepack.h 2006-07-08 01:51:06 UTC (rev 2054) +++ trunk/Lib/integrate/__odepack.h 2006-07-08 02:21:54 UTC (rev 2055) @@ -185,7 +185,7 @@ lrn = 20 + nyh*(mxordn+1) + 3*neq; lrs = 20 + nyh*(mxords+1) + 3*neq + lmat; - *lrw = MAX(lrn,lrs); + *lrw = NPY_MAX(lrn,lrs); *liw = 20 + neq; return 0; Modified: trunk/Lib/ndimage/setup.py =================================================================== --- trunk/Lib/ndimage/setup.py 2006-07-08 01:51:06 UTC (rev 2054) +++ trunk/Lib/ndimage/setup.py 2006-07-08 02:21:54 UTC (rev 2055) @@ -1,5 +1,6 @@ from numpy.distutils.core import setup from numpy.distutils.misc_util import Configuration +from numpy.numarray import get_numarray_include_dirs def configuration(parent_package='', top_path=None): @@ -10,7 +11,7 @@ "src/ni_fourier.c","src/ni_interpolation.c", "src/ni_measure.c", "src/ni_morphology.c","src/ni_support.c"], - include_dirs=['src'], + include_dirs=['src']+get_numarray_include_dirs(), ) config.add_data_dir('tests') Modified: trunk/Lib/ndimage/src/nd_image.h =================================================================== --- trunk/Lib/ndimage/src/nd_image.h 2006-07-08 01:51:06 UTC (rev 2054) +++ trunk/Lib/ndimage/src/nd_image.h 2006-07-08 02:21:54 UTC (rev 2055) @@ -38,7 +38,7 @@ #include "Python.h" #include "numpy/libnumarray.h" -#define NI_MAXDIM MAXDIM +#define NI_MAXDIM NPY_MAXDIMS int NI_GetArrayRank(PyArrayObject*); NumarrayType NI_GetArrayType(PyArrayObject*); Modified: trunk/Lib/setup.py =================================================================== --- trunk/Lib/setup.py 2006-07-08 01:51:06 UTC (rev 2054) +++ trunk/Lib/setup.py 2006-07-08 02:21:54 UTC (rev 2055) @@ -19,7 +19,7 @@ config.add_subpackage('sparse') config.add_subpackage('special') config.add_subpackage('stats') - #config.add_subpackage('ndimage') + config.add_subpackage('ndimage') config.add_subpackage('weave') config.make_svn_version_py() # installs __svn_version__.py config.make_config_py() Modified: trunk/Lib/signal/splinemodule.c =================================================================== --- trunk/Lib/signal/splinemodule.c 2006-07-08 01:51:06 UTC (rev 2054) +++ trunk/Lib/signal/splinemodule.c 2006-07-08 02:21:54 UTC (rev 2055) @@ -11,7 +11,7 @@ #define OBJECTTYPE(arr) ((arr)->descr->type_num) #define BASEOBJ(arr) ((PyArrayObject *)((arr)->base)) #define RANK(arr) ((arr)->nd) -#define ISCONTIGUOUS(m) ((m)->flags & CONTIGUOUS) +#define ISCONTIGUOUS(m) ((m)->flags & NPY_CONTIGUOUS) static void convert_strides(npy_intp*,npy_intp*,int,int); @@ -74,7 +74,7 @@ if (!PyArg_ParseTuple(args, "O|dd", &image, &lambda, &precision)) return NULL; thetype = PyArray_ObjectType(image, PyArray_FLOAT); - thetype = MIN(thetype, PyArray_DOUBLE); + thetype = NPY_MIN(thetype, PyArray_DOUBLE); a_image = (PyArrayObject *)PyArray_FromObject(image, thetype, 2, 2); if (a_image == NULL) goto fail; @@ -133,7 +133,7 @@ if (lambda != 0.0) PYERR("Smoothing spline not yet implemented."); thetype = PyArray_ObjectType(image, PyArray_FLOAT); - thetype = MIN(thetype, PyArray_DOUBLE); + thetype = NPY_MIN(thetype, PyArray_DOUBLE); a_image = (PyArrayObject *)PyArray_FromObject(image, thetype, 2, 2); if (a_image == NULL) goto fail; @@ -187,7 +187,7 @@ if (!PyArg_ParseTuple(args, "OOO", &image, &hrow, &hcol)) return NULL; thetype = PyArray_ObjectType(image, PyArray_FLOAT); - thetype = MIN(thetype, PyArray_CDOUBLE); + thetype = NPY_MIN(thetype, PyArray_CDOUBLE); a_image = (PyArrayObject *)PyArray_FromObject(image, thetype, 2, 2); a_hrow = (PyArrayObject *)PyArray_ContiguousFromObject(hrow, thetype, 1, 1); a_hcol = (PyArrayObject *)PyArray_ContiguousFromObject(hcol, thetype, 1, 1); @@ -297,7 +297,7 @@ return NULL; thetype = PyArray_ObjectType(sig, PyArray_FLOAT); - thetype = MIN(thetype, PyArray_CDOUBLE); + thetype = NPY_MIN(thetype, PyArray_CDOUBLE); a_sig = (PyArrayObject *)PyArray_FromObject(sig, thetype, 1, 1); if ((a_sig == NULL)) goto fail; @@ -417,7 +417,7 @@ return NULL; thetype = PyArray_ObjectType(sig, PyArray_FLOAT); - thetype = MIN(thetype, PyArray_DOUBLE); + thetype = NPY_MIN(thetype, PyArray_DOUBLE); a_sig = (PyArrayObject *)PyArray_FromObject(sig, thetype, 1, 1); if ((a_sig == NULL)) goto fail; Modified: trunk/Lib/weave/c_spec.py =================================================================== --- trunk/Lib/weave/c_spec.py 2006-07-08 01:51:06 UTC (rev 2054) +++ trunk/Lib/weave/c_spec.py 2006-07-08 02:21:54 UTC (rev 2055) @@ -293,7 +293,7 @@ num_to_c_types[type(1.)] = 'double' num_to_c_types[type(1.+1.j)] = 'std::complex ' # !! hmmm. The following is likely unsafe... -num_to_c_types[type(1L)] = 'longlong' +num_to_c_types[type(1L)] = 'npy_longlong' #---------------------------------------------------------------------------- # Numeric array Python numeric --> C type maps @@ -302,22 +302,22 @@ num_to_c_types['G'] = 'std::complex ' num_to_c_types['F'] = 'std::complex ' num_to_c_types['D'] = 'std::complex ' -num_to_c_types['g'] = 'longdouble' +num_to_c_types['g'] = 'npy_longdouble' num_to_c_types['f'] = 'float' num_to_c_types['d'] = 'double' num_to_c_types['b'] = 'char' -num_to_c_types['B'] = 'uchar' -num_to_c_types['B'] = 'ubyte' # numpy +num_to_c_types['B'] = 'npy_uchar' +num_to_c_types['B'] = 'npy_ubyte' # numpy num_to_c_types['h'] = 'short' -num_to_c_types['H'] = 'ushort' +num_to_c_types['H'] = 'npy_ushort' num_to_c_types['i'] = 'int' -num_to_c_types['I'] = 'uint' +num_to_c_types['I'] = 'npy_uint' num_to_c_types['l'] = 'long' -num_to_c_types['L'] = 'ulong' +num_to_c_types['L'] = 'npy_ulong' -num_to_c_types['q'] = 'longlong' -num_to_c_types['Q'] = 'ulonglong' +num_to_c_types['q'] = 'npy_longlong' +num_to_c_types['Q'] = 'npy_ulonglong' class scalar_converter(common_base_converter): def init_info(self): Modified: trunk/Lib/weave/standard_array_spec.py =================================================================== --- trunk/Lib/weave/standard_array_spec.py 2006-07-08 01:51:06 UTC (rev 2054) +++ trunk/Lib/weave/standard_array_spec.py 2006-07-08 02:21:54 UTC (rev 2055) @@ -169,8 +169,8 @@ '%(c_type)s %(array_name)s = %(var_convert)s;\n' \ 'conversion_numpy_check_type(%(array_name)s,%(num_typecode)s,"%(name)s");\n' \ '%(_code2_)s' \ - 'intp* N%(name)s = %(array_name)s->dimensions;\n' \ - 'intp* S%(name)s = %(array_name)s->strides;\n' \ + 'npy_intp* N%(name)s = %(array_name)s->dimensions;\n' \ + 'npy_intp* S%(name)s = %(array_name)s->strides;\n' \ 'int D%(name)s = %(array_name)s->nd;\n' \ '%(num_type)s* %(name)s = (%(num_type)s*) %(array_name)s->data;\n' code = code % res From scipy-svn at scipy.org Fri Jul 7 22:23:07 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 7 Jul 2006 21:23:07 -0500 (CDT) Subject: [Scipy-svn] r2056 - trunk/Lib/ndimage/src Message-ID: <20060708022307.7DCAF780941@new.scipy.org> Author: oliphant Date: 2006-07-07 21:23:05 -0500 (Fri, 07 Jul 2006) New Revision: 2056 Removed: trunk/Lib/ndimage/src/numcompat.c trunk/Lib/ndimage/src/numcompat.h Log: Remove unneeded numcompat.c and numcompat.h files Deleted: trunk/Lib/ndimage/src/numcompat.c =================================================================== --- trunk/Lib/ndimage/src/numcompat.c 2006-07-08 02:21:54 UTC (rev 2055) +++ trunk/Lib/ndimage/src/numcompat.c 2006-07-08 02:23:05 UTC (rev 2056) @@ -1,15 +0,0 @@ - -#include "nd_image.h" - -PyArrayObject * -NA_NewArray(void *buffer, NumarrayType type, int ndim, ...) -{ - int i; - maybelong shape[MAXDIM]; - va_list ap; - va_start(ap, ndim); - for(i=0; i Author: oliphant Date: 2006-07-08 04:29:48 -0500 (Sat, 08 Jul 2006) New Revision: 2057 Modified: trunk/Lib/optimize/linesearch.py trunk/Lib/optimize/optimize.py trunk/Lib/sandbox/netcdf/_netcdf.c Log: Make a few changes to optimize. Modified: trunk/Lib/optimize/linesearch.py =================================================================== --- trunk/Lib/optimize/linesearch.py 2006-07-08 02:23:05 UTC (rev 2056) +++ trunk/Lib/optimize/linesearch.py 2006-07-08 09:29:48 UTC (rev 2057) @@ -28,8 +28,8 @@ xtol = 1e-14 amin = 1e-8 - isave = numpy.zeros((2,), numpy.int32) - dsave = numpy.zeros((13,), numpy.float64) + isave = numpy.zeros((2,), numpy.intc) + dsave = numpy.zeros((13,), float) task = 'START' fval = old_fval gval = gfk Modified: trunk/Lib/optimize/optimize.py =================================================================== --- trunk/Lib/optimize/optimize.py 2006-07-08 02:23:05 UTC (rev 2056) +++ trunk/Lib/optimize/optimize.py 2006-07-08 09:29:48 UTC (rev 2057) @@ -624,7 +624,7 @@ gfk = myfprime(x0) k = 0 N = len(x0) - I = numpy.eye(N) + I = numpy.eye(N,dtype=int) Hk = I old_fval = f(x0) old_old_fval = old_fval + 5000 @@ -644,8 +644,9 @@ line_search(f,myfprime,xk,pk,gfk, old_fval,old_old_fval) if alpha_k is None: - raise ValueError, "Line-search failing..." - + # This line search also failed to find a better solution. + warnflag = 2 + break xkp1 = xk + alpha_k * pk if retall: allvecs.append(xkp1) @@ -1579,7 +1580,7 @@ Jout = vecfunc(*grid) Nshape = shape(Jout) indx = argmin(Jout.ravel()) - Nindx = zeros(N) + Nindx = zeros(N,int) xmin = zeros(N,float) for k in range(N-1,-1,-1): thisN = Nshape[k] Modified: trunk/Lib/sandbox/netcdf/_netcdf.c =================================================================== --- trunk/Lib/sandbox/netcdf/_netcdf.c 2006-07-08 02:23:05 UTC (rev 2056) +++ trunk/Lib/sandbox/netcdf/_netcdf.c 2006-07-08 09:29:48 UTC (rev 2057) @@ -168,7 +168,7 @@ */ int data_types[] = {-1, /* not used */ - PyArray_SBYTE, /* signed 8-bit int */ + PyArray_BYTE, /* signed 8-bit int */ PyArray_CHAR, /* 8-bit character */ PyArray_SHORT, /* 16-bit signed int */ PyArray_INT, /* 32-bit signed int */ From scipy-svn at scipy.org Sun Jul 9 19:03:49 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sun, 9 Jul 2006 18:03:49 -0500 (CDT) Subject: [Scipy-svn] r2058 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060709230349.A3B4C1DBF11@new.scipy.org> Author: fullung Date: 2006-07-09 18:03:35 -0500 (Sun, 09 Jul 2006) New Revision: 2058 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_oneclass.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Classification test and some minor fixes. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-08 09:29:48 UTC (rev 2057) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-09 23:03:35 UTC (rev 2058) @@ -39,7 +39,7 @@ """ def p(x): xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - return libsvm.svm_predict(self.model, xptr) + return int(libsvm.svm_predict(self.model, xptr)) return map(p, dataset.data) def predict_values(self, dataset): Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-08 09:29:48 UTC (rev 2057) +++ trunk/Lib/sandbox/svm/model.py 2006-07-09 23:03:35 UTC (rev 2058) @@ -54,10 +54,6 @@ def fit(self, dataset): # XXX don't poke around in dataset's internals - - # no reference to the svm_problem is kept because a svm_model - # only requires some parameters and the support vectors chosen - # from the dataset problem = libsvm.svm_problem() problem.l = len(dataset.data) y = (c_double*problem.l)() @@ -71,10 +67,15 @@ model = libsvm.svm_train(problem, self.param) - # XXX because libsvm only does a shallow copy of the - # svm_parameter into the model, we have to make sure that a - # reference to weight labels and weights are kept somewhere + # weight parametes are no longer required, so remove to them + # as the data they point to might disappear when this object + # is deallocated + model.contents.param.nr_weight = 0 + model.contents.param.weight = None + model.contents.param.weight_label = None + # results keep a refence to the dataset because the svm_model + # refers to some of its vectors as the support vectors return self.Results(model, dataset) def _check_problem_param(self, problem, param): Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-08 09:29:48 UTC (rev 2057) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-09 23:03:35 UTC (rev 2058) @@ -17,7 +17,7 @@ Model(Kernel, 1.0, weights) model = Model(Kernel, cost=1.0, weights=weights) - def check_c_train(self): + def check_c_basics(self): labels = [0, 1, 1, 2] x = [N.array([0, 0]), N.array([0, 1]), @@ -33,6 +33,45 @@ results.predict(testdata) results.predict_values(testdata) + def check_c_more(self): + labels = [0, 1, 1, 2] + x = [N.array([0, 0]), + N.array([0, 1]), + N.array([1, 0]), + N.array([1, 1])] + traindata = LibSvmClassificationDataSet(zip(labels, x)) + cost = 10.0 + weights = [(1, 10.0)] + testdata = LibSvmTestDataSet(x) + + kernels = [ + LinearKernel(), + PolynomialKernel(3, traindata.gamma, 0.0), + RBFKernel(traindata.gamma) + ] + expected_rhos = [ + [-0.999349, -1.0, -3.0], + [0.375, -1.0, -1.153547], + [0.671181, 0.0, -0.671133] + ] + expected_errors = [0, 1, 0] + + for kernel, expected_rho, expected_error in \ + zip(kernels, expected_rhos, expected_errors): + model = LibSvmCClassificationModel(kernel, cost, weights) + results = model.fit(traindata) + + self.assertEqual(results.labels, [0, 1, 2]) + #self.assertEqual(model.nSV, [1, 2, 1]) + + # XXX decimal=4 to suppress slight differences in values + # calculated for rho on Windows with MSVC 7.1 and on + # Fedora Core 4 with GCC 4.0.0. + assert_array_almost_equal(results.rho, expected_rho, decimal=4) + + predictions = N.array(results.predict(testdata)) + self.assertEqual(N.sum(predictions != labels), expected_error) + def check_nu_train(self): pass Modified: trunk/Lib/sandbox/svm/tests/test_oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-08 09:29:48 UTC (rev 2057) +++ trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-09 23:03:35 UTC (rev 2058) @@ -18,11 +18,11 @@ N.array([0, 1]), N.array([1, 0]), N.array([1, 1])] - dataset = LibSvmOneClassDataSet(x) + triandata = LibSvmOneClassDataSet(x) Model = LibSvmOneClassModel model = Model(LinearKernel()) - results = model.fit(dataset) + results = model.fit(traindata) testdata = LibSvmTestDataSet(x) results.predict(testdata) Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-08 09:29:48 UTC (rev 2057) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-09 23:03:35 UTC (rev 2058) @@ -22,11 +22,11 @@ N.array([0, 1]), N.array([1, 0]), N.array([1, 1])] - dataset = LibSvmRegressionDataSet(zip(y, x)) + traindata = LibSvmRegressionDataSet(zip(y, x)) Model = LibSvmEpsilonRegressionModel model = Model(LinearKernel()) - results = model.fit(dataset) + results = model.fit(traindata) testdata = LibSvmTestDataSet(x) results.predict(testdata) From scipy-svn at scipy.org Mon Jul 10 16:42:33 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 15:42:33 -0500 (CDT) Subject: [Scipy-svn] r2068 - trunk/Lib/sandbox/svm/libsvm-2.82 Message-ID: <20060710204233.5E3AD39C02D@new.scipy.org> Author: fullung Date: 2006-07-10 15:42:24 -0500 (Mon, 10 Jul 2006) New Revision: 2068 Added: trunk/Lib/sandbox/svm/libsvm-2.82/SConstruct Log: SConstruct file to build shared library until numpy.distutils gets sorted out. Added: trunk/Lib/sandbox/svm/libsvm-2.82/SConstruct =================================================================== --- trunk/Lib/sandbox/svm/libsvm-2.82/SConstruct 2006-07-10 20:39:54 UTC (rev 2067) +++ trunk/Lib/sandbox/svm/libsvm-2.82/SConstruct 2006-07-10 20:42:24 UTC (rev 2068) @@ -0,0 +1,3 @@ +env = Environment() +env.Replace(CXXFLAGS=['-O2','-Wall','-ansi','-pedantic']) +env.SharedLibrary('libsvm_', ['svm.cpp']) From scipy-svn at scipy.org Mon Jul 10 16:40:15 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 15:40:15 -0500 (CDT) Subject: [Scipy-svn] r2067 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060710204015.917D639C02F@new.scipy.org> Author: fullung Date: 2006-07-10 15:39:54 -0500 (Mon, 10 Jul 2006) New Revision: 2067 Added: trunk/Lib/sandbox/svm/tests/test_all.py Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Cross-validation for classification and regression. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-10 15:01:03 UTC (rev 2066) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-10 20:39:54 UTC (rev 2067) @@ -118,6 +118,27 @@ self.param.weight = \ cast(self.weights.ctypes.data, POINTER(c_double)) + def cross_validate(self, dataset, nr_fold): + """ + Perform cross-validation to determine the suitability of + chosen model parameters. + + Data are separated to nr_fold folds. Each fold is validated + against a model trained using the data from the remaining + (nr_fold-1) folds. + + This function returns the percentage of data that was + classified correctly over all the experiments. + """ + problem, y, x = self._create_problem(dataset) + target = N.empty((len(dataset.data),), dtype=N.float64) + tp = cast(target.ctypes.data, POINTER(c_double)) + libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) + total_correct = 0. + for x, t in zip(dataset.data, target): + if x[0] == int(t): total_correct += 1 + return 100.0 * total_correct / len(dataset.data) + class LibSvmCClassificationModel(LibSvmClassificationModel): """ A model for C-SV classification. Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-10 15:01:03 UTC (rev 2066) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-10 20:39:54 UTC (rev 2067) @@ -28,8 +28,6 @@ class LibSvmClassificationDataSet(LibSvmDataSet): def __init__(self, origdata): labels = N.array(map(lambda x: x[0], origdata), dtype=N.intc) - assert N.alltrue(labels >= 0), \ - 'labels must be non-negative integers' labels.sort() self.labels = labels Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-10 15:01:03 UTC (rev 2066) +++ trunk/Lib/sandbox/svm/model.py 2006-07-10 20:39:54 UTC (rev 2067) @@ -57,7 +57,7 @@ self.param = param - def fit(self, dataset): + def _create_problem(self, dataset): # XXX don't poke around in dataset's internals problem = libsvm.svm_problem() problem.l = len(dataset.data) @@ -69,7 +69,13 @@ problem.x = cast(addressof(x), POINTER(POINTER(libsvm.svm_node))) problem.y = cast(addressof(y), POINTER(c_double)) self._check_problem_param(problem, self.param) + # XXX keep references to y and x inside problem, if ctypes allows + # it (need to confirm this) + return problem, y, x + def fit(self, dataset): + problem, y, x = self._create_problem(dataset) + model = libsvm.svm_train(problem, self.param) # weight parametes are no longer required, so remove to them Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-10 15:01:03 UTC (rev 2066) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-10 20:39:54 UTC (rev 2067) @@ -3,7 +3,8 @@ 'LibSvmNuRegressionModel' ] -from ctypes import cast, POINTER +import numpy as N +from ctypes import cast, POINTER, c_double from model import LibSvmModel import libsvm @@ -46,7 +47,51 @@ """ return self.sigma -class LibSvmEpsilonRegressionModel(LibSvmModel): +class LibSvmRegressionModel(LibSvmModel): + Results = LibSvmRegressionResults + + def __init__(self, kernel, **kwargs): + LibSvmModel.__init__(self, kernel, **kwargs) + + def cross_validate(self, dataset, nr_fold): + """ + Perform cross-validation to determine the suitability of + chosen model parameters. + + Data are separated to nr_fold folds. Each fold is validated + against a model trained using the data from the remaining + (nr_fold-1) folds. + + This function returns a 2-tuple containing the mean squared + error and the squared correlation coefficient. + """ + + problem, y, x = self._create_problem(dataset) + target = N.empty((len(dataset.data),), dtype=N.float64) + tp = cast(target.ctypes.data, POINTER(c_double)) + libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) + + total_error = sumv = sumy = sumvv = sumyy = sumvy = 0. + for i in range(len(dataset.data)): + v = target[i] + y = dataset.data[i][0] + sumv = sumv + v + sumy = sumy + y + sumvv = sumvv + v * v + sumyy = sumyy + y * y + sumvy = sumvy + v * y + total_error = total_error + (v-y) * (v-y) + + # mean squared error + mse = total_error / len(dataset.data) + # squared correlation coefficient + l = len(dataset.data) + scc = ((l*sumvy - sumv*sumy) * (l*sumvy - sumv*sumy)) / \ + ((l*sumvv - sumv*sumv) * (l*sumyy - sumy*sumy)) + + return mse, scc + +class LibSvmEpsilonRegressionModel(LibSvmRegressionModel): """ A model for epsilon-SV regression. @@ -58,10 +103,8 @@ Prediction. """ - Results = LibSvmRegressionResults - def __init__(self, kernel, epsilon=0.1, cost=1.0, **kwargs): - LibSvmModel.__init__(self, kernel, **kwargs) + LibSvmRegressionModel.__init__(self, kernel, **kwargs) self.epsilon = epsilon self.cost = cost self.param.svm_type = libsvm.EPSILON_SVR @@ -69,17 +112,15 @@ self.param.C = cost self.param.probability = True -class LibSvmNuRegressionModel(LibSvmModel): +class LibSvmNuRegressionModel(LibSvmRegressionModel): """ A model for nu-SV regression. See also: Schoelkopf, et al. New Support Vector Algorithms. """ - Results = LibSvmRegressionResults - def __init__(self, kernel, nu=0.5, cost=1.0, **kwargs): - LibSvmModel.__init__(self, kernel, **kwargs) + LibSvmRegressionModel.__init__(self, kernel, **kwargs) self.nu = nu self.cost = cost self.param.svm_type = libsvm.NU_SVR Added: trunk/Lib/sandbox/svm/tests/test_all.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-10 15:01:03 UTC (rev 2066) +++ trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-10 20:39:54 UTC (rev 2067) @@ -0,0 +1,8 @@ +from test_regression import * +from test_classification import * +from test_dataset import * +from test_oneclass import * +from test_libsvm import * + +if __name__ == '__main__': + NumpyTest().run() Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-10 15:01:03 UTC (rev 2066) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-10 20:39:54 UTC (rev 2067) @@ -15,8 +15,14 @@ weights = [(2, 10.0), (1, 20.0), (0, 30.0)] Model(Kernel, weights=weights) Model(Kernel, 1.0, weights) - model = Model(Kernel, cost=1.0, weights=weights) + Model(Kernel, cost=1.0, weights=weights) + Model = LibSvmNuClassificationModel + Model(Kernel) + Model(Kernel, nu=0.5) + Model(Kernel, weights=weights) + Model(Kernel, 0.5, weights) + def check_c_basics(self): labels = [0, 1, 1, 2] x = [N.array([0, 0]), @@ -94,6 +100,17 @@ results = model.fit(traindata) results.predict_probability(testdata) + def check_cross_validate(self): + labels = ([-1] * 50) + ([1] * 50) + x = N.randn(len(labels), 10) + traindata = LibSvmClassificationDataSet(zip(labels, x)) + kernel = LinearKernel() + model = LibSvmCClassificationModel(kernel) + nr_fold = 10 + pcorr = model.cross_validate(traindata, nr_fold) + # XXX check cross-validation with and without probability + # output enabled + def check_nu_train(self): pass Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-10 15:01:03 UTC (rev 2066) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-10 20:39:54 UTC (rev 2067) @@ -1,8 +1,3 @@ -import sys -import os -sys.path.insert(0, '..') -sys.path.insert(0, os.path.join('..','..')) - from numpy.testing import * import numpy as N @@ -21,6 +16,13 @@ model = Model(Kernel, shrinking=False) self.assert_(not model.shrinking) + Model = LibSvmNuRegressionModel + Model(Kernel) + Model(Kernel, nu=0.5) + model = Model(Kernel, 0.5, cache_size=60, tolerance=0.005) + self.assertEqual(model.cache_size, 60) + self.assertAlmostEqual(model.tolerance, 0.005) + def check_epsilon_train(self): y = [10., 20., 30., 40.] x = [N.array([0, 0]), @@ -65,6 +67,15 @@ predictions = results.predict(testdata) assert_array_almost_equal(predictions, expected_y) + def check_cross_validate(self): + y = N.randn(100) + x = N.randn(len(y), 10) + traindata = LibSvmRegressionDataSet(zip(y, x)) + kernel = LinearKernel() + model = LibSvmEpsilonRegressionModel(kernel) + nr_fold = 10 + mse, scc = model.cross_validate(traindata, nr_fold) + def check_nu_train(self): pass From scipy-svn at scipy.org Mon Jul 10 16:56:06 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 15:56:06 -0500 (CDT) Subject: [Scipy-svn] r2069 - trunk/Lib/sandbox/svm/tests Message-ID: <20060710205606.2183239C099@new.scipy.org> Author: fullung Date: 2006-07-10 15:55:59 -0500 (Mon, 10 Jul 2006) New Revision: 2069 Modified: trunk/Lib/sandbox/svm/tests/test_regression.py Log: Tweak so that test passes with GCC. Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-10 20:42:24 UTC (rev 2068) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-10 20:55:59 UTC (rev 2069) @@ -65,7 +65,12 @@ model = LibSvmEpsilonRegressionModel(kernel, epsilon, cost) results = model.fit(traindata) predictions = results.predict(testdata) - assert_array_almost_equal(predictions, expected_y) + # look at differences instead of using assertAlmostEqual + # due to slight differences between answers obtained on + # Windows with MSVC 7.1 and on Fedora Core 5 with GCC + # 4.1.1. + diff = N.absolute(predictions - expected_y) + self.assert_(N.alltrue(diff < 1e-3)) def check_cross_validate(self): y = N.randn(100) From scipy-svn at scipy.org Mon Jul 10 18:06:08 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 17:06:08 -0500 (CDT) Subject: [Scipy-svn] r2070 - trunk/Lib/sandbox/svm Message-ID: <20060710220608.ECFC339C3F5@new.scipy.org> Author: fullung Date: 2006-07-10 17:05:57 -0500 (Mon, 10 Jul 2006) New Revision: 2070 Added: trunk/Lib/sandbox/svm/setup.py Log: First attempt at a setup.py. Added: trunk/Lib/sandbox/svm/setup.py =================================================================== --- trunk/Lib/sandbox/svm/setup.py 2006-07-10 20:55:59 UTC (rev 2069) +++ trunk/Lib/sandbox/svm/setup.py 2006-07-10 22:05:57 UTC (rev 2070) @@ -0,0 +1,9 @@ +def configuration(parent_package='', top_path=None, package_name='svm'): + from numpy.distutils.misc_util import Configuration + config = Configuration(package_name,parent_package,top_path) + config.add_subpackage('*') + return config + +if __name__ == '__main__': + from numpy.distutils.core import setup + setup(**configuration(top_path='', package_name='scipy.sandbox.svm').todict()) From scipy-svn at scipy.org Mon Jul 10 18:09:36 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 17:09:36 -0500 (CDT) Subject: [Scipy-svn] r2071 - trunk/Lib/sandbox/svm Message-ID: <20060710220936.AF09F39C42C@new.scipy.org> Author: fullung Date: 2006-07-10 17:09:29 -0500 (Mon, 10 Jul 2006) New Revision: 2071 Modified: trunk/Lib/sandbox/svm/README Log: Howto for getting the test suite to run. Modified: trunk/Lib/sandbox/svm/README =================================================================== --- trunk/Lib/sandbox/svm/README 2006-07-10 22:05:57 UTC (rev 2070) +++ trunk/Lib/sandbox/svm/README 2006-07-10 22:09:29 UTC (rev 2071) @@ -8,3 +8,39 @@ For more information, see: http://students.ee.sun.ac.za/~albert/pylibsvm/ + + +To run the test suite, do the following: + +1. Build the libsvm_ library in the libsvm-2.82 directory. + +On Windows: + +Start the Visual Studio .NET 2003 Command Prompt +cd libsvm-2.82 +nmake -f Makefile.win +copy libsvm_.dll .. + +On Linux: + +cd libsvm-2.82 +scons +cp libsvm_.so .. + +2. Run the tests in the tests directory. + +On Windows: + +cd tests +set PYTHONPATH=..;..\.. +test_classification.py +... +test_all.py + +On Linux: + +cd tests +export PYTHONPATH=..:../.. +python test_classification.py +... +python test_all.py From scipy-svn at scipy.org Mon Jul 10 18:21:29 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 17:21:29 -0500 (CDT) Subject: [Scipy-svn] r2072 - trunk/Lib/sandbox/svm Message-ID: <20060710222129.3194B39C4A2@new.scipy.org> Author: fullung Date: 2006-07-10 17:21:13 -0500 (Mon, 10 Jul 2006) New Revision: 2072 Modified: trunk/Lib/sandbox/svm/README Log: Minor updates. Modified: trunk/Lib/sandbox/svm/README =================================================================== --- trunk/Lib/sandbox/svm/README 2006-07-10 22:09:29 UTC (rev 2071) +++ trunk/Lib/sandbox/svm/README 2006-07-10 22:21:13 UTC (rev 2072) @@ -27,10 +27,23 @@ scons cp libsvm_.so .. -2. Run the tests in the tests directory. +2. Install NumPy SVN r2780 or later. +3. Install ctypes 0.9.9.6 or later. + On Windows: +http://sourceforge.net/project/showfiles.php?group_id=71702 + +On Linux, one of: + +yum install python-ctypes +apt-get install python-ctypes + +4. Run the tests in the tests directory. + +On Windows: + cd tests set PYTHONPATH=..;..\.. test_classification.py From scipy-svn at scipy.org Mon Jul 10 23:21:18 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 22:21:18 -0500 (CDT) Subject: [Scipy-svn] r2073 - trunk/Lib/sandbox/ga Message-ID: <20060711032118.B7E4139D047@new.scipy.org> Author: timl Date: 2006-07-10 22:21:13 -0500 (Mon, 10 Jul 2006) New Revision: 2073 Modified: trunk/Lib/sandbox/ga/algorithm.py Log: remove unused imports Modified: trunk/Lib/sandbox/ga/algorithm.py =================================================================== --- trunk/Lib/sandbox/ga/algorithm.py 2006-07-10 22:21:13 UTC (rev 2072) +++ trunk/Lib/sandbox/ga/algorithm.py 2006-07-11 03:21:13 UTC (rev 2073) @@ -2,10 +2,10 @@ import scipy.stats as stats rv = stats #import scipy.io.dumb_shelve -import string, pdb -import os, sys, string +import string +import os, sys import time, pprint, types,copy -import anydbm, dumbdbm +import dumbdbm #import thread, sync if sys.platform != 'win32': import fcntl From scipy-svn at scipy.org Mon Jul 10 23:24:21 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 22:24:21 -0500 (CDT) Subject: [Scipy-svn] r2074 - trunk/Lib/sandbox/ga Message-ID: <20060711032421.661D139D072@new.scipy.org> Author: timl Date: 2006-07-10 22:24:15 -0500 (Mon, 10 Jul 2006) New Revision: 2074 Modified: trunk/Lib/sandbox/ga/gene.py Log: remove duplicate definition of __float__ Modified: trunk/Lib/sandbox/ga/gene.py =================================================================== --- trunk/Lib/sandbox/ga/gene.py 2006-07-11 03:21:13 UTC (rev 2073) +++ trunk/Lib/sandbox/ga/gene.py 2006-07-11 03:24:15 UTC (rev 2074) @@ -344,7 +344,6 @@ def __rdiv__(self, other): try: return other.value() / self._value except AttributeError: return other / self._value - def __float__(self): return float(self._value) def __neg__(self): return -self._value def __cmp__(self, other): try: From scipy-svn at scipy.org Mon Jul 10 23:52:13 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 10 Jul 2006 22:52:13 -0500 (CDT) Subject: [Scipy-svn] r2075 - trunk/Lib/sandbox/models Message-ID: <20060711035213.5D7E839D19D@new.scipy.org> Author: timl Date: 2006-07-10 22:52:07 -0500 (Mon, 10 Jul 2006) New Revision: 2075 Modified: trunk/Lib/sandbox/models/cox.py Log: remove unused import Modified: trunk/Lib/sandbox/models/cox.py =================================================================== --- trunk/Lib/sandbox/models/cox.py 2006-07-11 03:24:15 UTC (rev 2074) +++ trunk/Lib/sandbox/models/cox.py 2006-07-11 03:52:07 UTC (rev 2075) @@ -1,5 +1,4 @@ import numpy as N -import numpy.linalg as L import survival import model import tempfile, shutil From scipy-svn at scipy.org Tue Jul 11 01:30:23 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 11 Jul 2006 00:30:23 -0500 (CDT) Subject: [Scipy-svn] r2076 - trunk/Lib/sandbox/montecarlo Message-ID: <20060711053023.1589539D88D@new.scipy.org> Author: timl Date: 2006-07-11 00:30:14 -0500 (Tue, 11 Jul 2006) New Revision: 2076 Modified: trunk/Lib/sandbox/montecarlo/montecarlo.py Log: remove unused imports Modified: trunk/Lib/sandbox/montecarlo/montecarlo.py =================================================================== --- trunk/Lib/sandbox/montecarlo/montecarlo.py 2006-07-11 03:52:07 UTC (rev 2075) +++ trunk/Lib/sandbox/montecarlo/montecarlo.py 2006-07-11 05:30:14 UTC (rev 2076) @@ -6,7 +6,6 @@ __author__ = "Ed Schofield" from __future__ import division -import random, math, bisect, string import numpy import scipy from scipy.sandbox.montecarlo._intsampler import _intsampler From scipy-svn at scipy.org Tue Jul 11 10:02:23 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 11 Jul 2006 09:02:23 -0500 (CDT) Subject: [Scipy-svn] r2077 - trunk/Lib/sandbox/odr Message-ID: <20060711140223.4ED7E39C098@new.scipy.org> Author: rkern Date: 2006-07-11 09:02:18 -0500 (Tue, 11 Jul 2006) New Revision: 2077 Added: trunk/Lib/sandbox/odr/setup.py Removed: trunk/Lib/sandbox/odr/setup_odr.py Log: Fix setup for odr. Copied: trunk/Lib/sandbox/odr/setup.py (from rev 2057, trunk/Lib/sandbox/odr/setup_odr.py) =================================================================== --- trunk/Lib/sandbox/odr/setup_odr.py 2006-07-08 09:29:48 UTC (rev 2057) +++ trunk/Lib/sandbox/odr/setup.py 2006-07-11 14:02:18 UTC (rev 2077) @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +import os,sys,re +from distutils import dep_util +from glob import glob +import warnings + +from numpy.distutils.core import Extension +from numpy.distutils.misc_util import get_path, Configuration, dot_join + +from numpy.distutils.system_info import get_info,dict_append,\ + AtlasNotFoundError,LapackNotFoundError,BlasNotFoundError,\ + LapackSrcNotFoundError,BlasSrcNotFoundError + +def configuration(parent_package='', top_path=None): + config = Configuration('odr', parent_package, top_path) + + libodr_files = ['d_odr.f', + 'd_mprec.f', + 'dlunoc.f'] + + atlas_info = get_info('atlas') + #atlas_info = {} # uncomment if ATLAS is available but want to use + # Fortran LAPACK/BLAS; useful for testing + blas_libs = [] + if not atlas_info: + warnings.warn(AtlasNotFoundError.__doc__) + blas_info = get_info('blas') + if blas_info: + libodr_files.append('d_lpk.f') + blas_libs.extend(blas_info['libraries']) + else: + warnings.warn(BlasNotFoundError.__doc__) + libodr_files.append('d_lpkbls.f') + else: + libodr_files.append('d_lpk.f') + blas_libs.extend(atlas_info['libraries']) + + libodr = [os.path.join('odrpack', x) for x in libodr_files] + config.add_library('odrpack', sources=libodr) + sources = ['__odrpack.c'] + config.add_extension('__odrpack', + sources=sources, + libraries=['odrpack']+blas_libs, + include_dirs=['.'], + library_dirs=atlas_info['library_dirs'], + ) + + return config + +if __name__ == '__main__': + from numpy.distutils.core import setup + setup(**configuration(top_path='').todict()) Deleted: trunk/Lib/sandbox/odr/setup_odr.py =================================================================== --- trunk/Lib/sandbox/odr/setup_odr.py 2006-07-11 05:30:14 UTC (rev 2076) +++ trunk/Lib/sandbox/odr/setup_odr.py 2006-07-11 14:02:18 UTC (rev 2077) @@ -1,55 +0,0 @@ -#!/usr/bin/env python - -import os,sys,re -from distutils import dep_util -from glob import glob -import warnings - -from numpy.distutils.core import Extension -from numpy.distutils.misc_util import get_path, Configuration, dot_join - -from numpy.distutils.system_info import get_info,dict_append,\ - AtlasNotFoundError,LapackNotFoundError,BlasNotFoundError,\ - LapackSrcNotFoundError,BlasSrcNotFoundError - -def configuration(parent_package=''): - package = 'odr' - config = Configuration(package,parent_package) - local_path = get_path(__name__) - - libodr_files = ['d_odr.f', - 'd_mprec.f', - 'dlunoc.f'] - - atlas_info = get_info('atlas') - #atlas_info = {} # uncomment if ATLAS is available but want to use - # Fortran LAPACK/BLAS; useful for testing - blas_libs = [] - if not atlas_info: - warnings.warn(AtlasNotFoundError.__doc__) - blas_info = get_info('blas') - if blas_info: - libodr_files.append('d_lpk.f') - blas_libs.extend(blas_info['libraries']) - else: - warnings.warn(BlasNotFoundError.__doc__) - libodr_files.append('d_lpkbls.f') - else: - libodr_files.append('d_lpk.f') - blas_libs.extend(atlas_info['libraries']) - - libodr = [os.path.join(local_path, 'odrpack', x) for x in libodr_files] - config.add_library('odrpack', sources=libodr) - sources = ['__odrpack.c'] - config.add_extension('__odrpack', - sources=sources, - libraries=['odrpack']+blas_libs, - include_dirs=[local_path], - library_dirs=atlas_info['library_dirs'], - ) - - return config - -if __name__ == '__main__': - from numpy.distutils.core import setup - setup(**configuration()) From scipy-svn at scipy.org Tue Jul 11 13:47:21 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 11 Jul 2006 12:47:21 -0500 (CDT) Subject: [Scipy-svn] r2078 - trunk/Lib/sandbox/models Message-ID: <20060711174721.4D6BC39C17A@new.scipy.org> Author: jonathan.taylor Date: 2006-07-11 12:47:07 -0500 (Tue, 11 Jul 2006) New Revision: 2078 Modified: trunk/Lib/sandbox/models/formula.py Log: small change in name of Term.__pow__ return value Modified: trunk/Lib/sandbox/models/formula.py =================================================================== --- trunk/Lib/sandbox/models/formula.py 2006-07-11 14:02:18 UTC (rev 2077) +++ trunk/Lib/sandbox/models/formula.py 2006-07-11 17:47:07 UTC (rev 2078) @@ -199,14 +199,19 @@ try: power = float(power) except: - raise ValueError, 'expecting an integer' + raise ValueError, 'expecting a float' - name = '%s^%0.2f' % (self.name, power) + if power == int(power): + name = '%s^%d' % (self.name, int(power)) + else: + name = '%s^%0.2f' % (self.name, power) - def func(namespace=terms, power=power, **extra): + def func(obj=self, namespace=terms, power=power, **extra): x = N.asarray(obj(namespace=namespace, **extra)) return N.power(x, power) - return Term(name, func=func) + value = Term(name, func=func) + value.power = power + return value class FuncQuant(Quantitative): @@ -308,6 +313,7 @@ pass else: if allvals != []: + allvals = N.concatenate(allvals) n = allvals.shape[1] allvals = N.concatenate([N.ones((1,n), N.float64), allvals]) elif nrow <= 1: From scipy-svn at scipy.org Tue Jul 11 15:14:21 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 11 Jul 2006 14:14:21 -0500 (CDT) Subject: [Scipy-svn] r2079 - trunk/Lib/sandbox/svm Message-ID: <20060711191421.B28D739C081@new.scipy.org> Author: fullung Date: 2006-07-11 14:13:55 -0500 (Tue, 11 Jul 2006) New Revision: 2079 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/regression.py Log: Let ctypes handle references to the contents of the svm_problem. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-11 17:47:07 UTC (rev 2078) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-11 19:13:55 UTC (rev 2079) @@ -130,7 +130,7 @@ This function returns the percentage of data that was classified correctly over all the experiments. """ - problem, y, x = self._create_problem(dataset) + problem = self._create_problem(dataset) target = N.empty((len(dataset.data),), dtype=N.float64) tp = cast(target.ctypes.data, POINTER(c_double)) libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-11 17:47:07 UTC (rev 2078) +++ trunk/Lib/sandbox/svm/model.py 2006-07-11 19:13:55 UTC (rev 2079) @@ -66,15 +66,13 @@ for i, (yi, xi) in enumerate(dataset.data): y[i] = yi x[i] = cast(xi.ctypes.data, POINTER(libsvm.svm_node)) - problem.x = cast(addressof(x), POINTER(POINTER(libsvm.svm_node))) - problem.y = cast(addressof(y), POINTER(c_double)) + problem.x = x + problem.y = y self._check_problem_param(problem, self.param) - # XXX keep references to y and x inside problem, if ctypes allows - # it (need to confirm this) - return problem, y, x + return problem def fit(self, dataset): - problem, y, x = self._create_problem(dataset) + problem = self._create_problem(dataset) model = libsvm.svm_train(problem, self.param) Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-11 17:47:07 UTC (rev 2078) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-11 19:13:55 UTC (rev 2079) @@ -66,7 +66,7 @@ error and the squared correlation coefficient. """ - problem, y, x = self._create_problem(dataset) + problem = self._create_problem(dataset) target = N.empty((len(dataset.data),), dtype=N.float64) tp = cast(target.ctypes.data, POINTER(c_double)) libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) From scipy-svn at scipy.org Wed Jul 12 02:02:10 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 01:02:10 -0500 (CDT) Subject: [Scipy-svn] r2080 - trunk Message-ID: <20060712060210.4270539C091@new.scipy.org> Author: fperez Date: 2006-07-12 01:02:04 -0500 (Wed, 12 Jul 2006) New Revision: 2080 Modified: trunk/setup.py Log: Remove MANIFEST file at startup time, since distutils fails to properly update it. Also add proper shebang line. Modified: trunk/setup.py =================================================================== --- trunk/setup.py 2006-07-11 19:13:55 UTC (rev 2079) +++ trunk/setup.py 2006-07-12 06:02:04 UTC (rev 2080) @@ -1,6 +1,11 @@ +#!/usr/bin/env python import os import sys +# BEFORE importing distutils, remove MANIFEST. distutils doesn't properly +# update it when the contents of directories change. +if os.path.exists('MANIFEST'): os.remove('MANIFEST') + os.environ['NO_SCIPY_IMPORT']='SciPy/setup.py' def configuration(parent_package='',top_path=None): From scipy-svn at scipy.org Wed Jul 12 04:28:40 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 03:28:40 -0500 (CDT) Subject: [Scipy-svn] r2081 - trunk/Lib/sparse/tests Message-ID: <20060712082840.B89D339C097@new.scipy.org> Author: stefan Date: 2006-07-12 03:28:32 -0500 (Wed, 12 Jul 2006) New Revision: 2081 Modified: trunk/Lib/sparse/tests/test_sparse.py Log: Use numpy.random.rand instead of numpy.rand. Modified: trunk/Lib/sparse/tests/test_sparse.py =================================================================== --- trunk/Lib/sparse/tests/test_sparse.py 2006-07-12 06:02:04 UTC (rev 2080) +++ trunk/Lib/sparse/tests/test_sparse.py 2006-07-12 08:28:32 UTC (rev 2081) @@ -259,9 +259,9 @@ """ n = 20 A = self.spmatrix((n,n), dtype=complex) - x = numpy.rand(n) - y = numpy.rand(n-1)+1j*numpy.rand(n-1) - r = numpy.rand(n) + x = numpy.random.rand(n) + y = numpy.random.rand(n-1)+1j*numpy.rand(n-1) + r = numpy.random.rand(n) for i in range(len(x)): A[i,i] = x[i] for i in range(len(y)): From scipy-svn at scipy.org Wed Jul 12 04:32:57 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 03:32:57 -0500 (CDT) Subject: [Scipy-svn] r2082 - trunk/Lib/sandbox/pysparse/tests Message-ID: <20060712083257.0EDE439C0B5@new.scipy.org> Author: stefan Date: 2006-07-12 03:32:51 -0500 (Wed, 12 Jul 2006) New Revision: 2082 Modified: trunk/Lib/sandbox/pysparse/tests/test_spmatrix.py Log: Use numpy.random.rand instead of numpy.rand. Modified: trunk/Lib/sandbox/pysparse/tests/test_spmatrix.py =================================================================== --- trunk/Lib/sandbox/pysparse/tests/test_spmatrix.py 2006-07-12 08:28:32 UTC (rev 2081) +++ trunk/Lib/sandbox/pysparse/tests/test_spmatrix.py 2006-07-12 08:32:51 UTC (rev 2082) @@ -210,7 +210,7 @@ y2 = Numeric.zeros(n, 'd') for s in range(10): #x = RandomArray.random((m, )) - x = numpy.rand((m, )) + x = numpy.random.rand((m, )) C.matvec(x, y1) B.matvec(x, t) A.matvec(t, y2) From scipy-svn at scipy.org Wed Jul 12 05:37:11 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 04:37:11 -0500 (CDT) Subject: [Scipy-svn] r2083 - trunk/Lib/sandbox/svm/tests Message-ID: <20060712093711.9255639C0C5@new.scipy.org> Author: stefan Date: 2006-07-12 04:37:02 -0500 (Wed, 12 Jul 2006) New Revision: 2083 Modified: trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: 'randn' is no longer in top level namespace. Call N.random.randn. Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-12 08:32:51 UTC (rev 2082) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-12 09:37:02 UTC (rev 2083) @@ -102,7 +102,7 @@ def check_cross_validate(self): labels = ([-1] * 50) + ([1] * 50) - x = N.randn(len(labels), 10) + x = N.random.randn(len(labels), 10) traindata = LibSvmClassificationDataSet(zip(labels, x)) kernel = LinearKernel() model = LibSvmCClassificationModel(kernel) Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-12 08:32:51 UTC (rev 2082) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-12 09:37:02 UTC (rev 2083) @@ -73,8 +73,8 @@ self.assert_(N.alltrue(diff < 1e-3)) def check_cross_validate(self): - y = N.randn(100) - x = N.randn(len(y), 10) + y = N.random.randn(100) + x = N.random.randn(len(y), 10) traindata = LibSvmRegressionDataSet(zip(y, x)) kernel = LinearKernel() model = LibSvmEpsilonRegressionModel(kernel) From scipy-svn at scipy.org Wed Jul 12 07:07:27 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 06:07:27 -0500 (CDT) Subject: [Scipy-svn] r2084 - trunk/Lib/sparse/tests Message-ID: <20060712110727.5C62739C0C6@new.scipy.org> Author: stefan Date: 2006-07-12 06:07:19 -0500 (Wed, 12 Jul 2006) New Revision: 2084 Modified: trunk/Lib/sparse/tests/test_sparse.py Log: Missed one rand. Replaced with random.rand. Modified: trunk/Lib/sparse/tests/test_sparse.py =================================================================== --- trunk/Lib/sparse/tests/test_sparse.py 2006-07-12 09:37:02 UTC (rev 2083) +++ trunk/Lib/sparse/tests/test_sparse.py 2006-07-12 11:07:19 UTC (rev 2084) @@ -260,7 +260,7 @@ n = 20 A = self.spmatrix((n,n), dtype=complex) x = numpy.random.rand(n) - y = numpy.random.rand(n-1)+1j*numpy.rand(n-1) + y = numpy.random.rand(n-1)+1j*numpy.random.rand(n-1) r = numpy.random.rand(n) for i in range(len(x)): A[i,i] = x[i] From scipy-svn at scipy.org Wed Jul 12 16:24:11 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 15:24:11 -0500 (CDT) Subject: [Scipy-svn] r2085 - in trunk/Lib: . optimize Message-ID: <20060712202411.3F8F739C066@new.scipy.org> Author: oliphant Date: 2006-07-12 15:24:08 -0500 (Wed, 12 Jul 2006) New Revision: 2085 Modified: trunk/Lib/__init__.py trunk/Lib/optimize/cobyla.py Log: Add rand and randn plus debugging prints in cobyla. Modified: trunk/Lib/__init__.py =================================================================== --- trunk/Lib/__init__.py 2006-07-12 11:07:19 UTC (rev 2084) +++ trunk/Lib/__init__.py 2006-07-12 20:24:08 UTC (rev 2085) @@ -31,9 +31,12 @@ import numpy as _num from numpy import oldnumeric from numpy import * +from numpy.random import rand, randn __all__ += ['oldnumeric']+_num.__all__ +__all__ += ['randn', 'rand'] + __doc__ += """ Contents -------- Modified: trunk/Lib/optimize/cobyla.py =================================================================== --- trunk/Lib/optimize/cobyla.py 2006-07-12 11:07:19 UTC (rev 2084) +++ trunk/Lib/optimize/cobyla.py 2006-07-12 20:24:08 UTC (rev 2085) @@ -67,9 +67,12 @@ def calcfc(x, con): f = func(x, *args) k = 0 + print "x = ", x + print "f = ", f for constraints in cons: con[k] = constraints(x, *consargs) k += 1 + print "con = ", con return f xopt = _cobyla.minimize(calcfc, m=m, x=x0, rhobeg=rhobeg, rhoend=rhoend, From scipy-svn at scipy.org Wed Jul 12 16:54:21 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 15:54:21 -0500 (CDT) Subject: [Scipy-svn] r2086 - trunk/Lib/ndimage/src Message-ID: <20060712205421.CB64039C066@new.scipy.org> Author: cookedm Date: 2006-07-12 15:54:20 -0500 (Wed, 12 Jul 2006) New Revision: 2086 Modified: trunk/Lib/ndimage/src/nd_image.c Log: Fix segfault in ndimage on 64-bit machine. 'i' was being used in PyArg_ParseTuple instead of 'l' for a npy_intp (maybelong) argument. Modified: trunk/Lib/ndimage/src/nd_image.c =================================================================== --- trunk/Lib/ndimage/src/nd_image.c 2006-07-12 20:24:08 UTC (rev 2085) +++ trunk/Lib/ndimage/src/nd_image.c 2006-07-12 20:54:20 UTC (rev 2086) @@ -1075,12 +1075,14 @@ PyObject *indices_object, *result = NULL; maybelong min_label, max_label, *result_indices = NULL, n_results; maybelong jj, nbins; + long nbins_in; double min, max; - - if (!PyArg_ParseTuple(args, "O&ddiO&O", NI_ObjectToInputArray, &input, - &min, &max, &nbins, NI_ObjectToOptionalInputArray, + + if (!PyArg_ParseTuple(args, "O&ddlO&O", NI_ObjectToInputArray, &input, + &min, &max, &nbins_in, NI_ObjectToOptionalInputArray, &labels, &indices_object)) goto exit; + nbins = nbins_in; if (!_NI_GetIndices(indices_object, &result_indices, &min_label, &max_label, &n_results)) From scipy-svn at scipy.org Wed Jul 12 18:54:33 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 17:54:33 -0500 (CDT) Subject: [Scipy-svn] r2087 - trunk/Lib/optimize/cobyla Message-ID: <20060712225433.4DCCC39C066@new.scipy.org> Author: oliphant Date: 2006-07-12 17:54:28 -0500 (Wed, 12 Jul 2006) New Revision: 2087 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: More printing tests for cobyla.f Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-12 20:54:20 UTC (rev 2086) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-12 22:54:28 UTC (rev 2087) @@ -136,6 +136,8 @@ 1 'MAXFUN limit has been reached.') GOTO 600 END IF + IF (IPRINT .EQ. 3) PRINT 51, (SIM(J,NP),J=1,IPTEM) + 51 FORMAT (/3X, 'SIM = ', 1PE13.6, 1P4E15.6) NFVALS=NFVALS+1 CALL CALCFC (N,M,X,F,CON) RESMAX=0.0d0 @@ -299,8 +301,13 @@ C Calculate the step to the new vertex and its sign. C TEMP=GAMMA*RHO*VSIG(JDROP) + IF (IPRINT .EQ. 3) PRINT 289, (SIMI(JDROP,I),I=1,IPTEM) + 289 FORMAT (/3X, 'SIMI =',1PE13.6,1P4E15.6) DO 290 I=1,N 290 DX(I)=TEMP*SIMI(JDROP,I) + IF (IPRINT .EQ. 3) PRINT 291, (DX(I),I=1,IPTEM) + 291 FORMAT (/3X, 'DX =',1PE13.6,1P4E15.6) + CVMAXP=0.0d0 CVMAXM=0.0d0 DO 310 K=1,MP From scipy-svn at scipy.org Wed Jul 12 19:26:16 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 18:26:16 -0500 (CDT) Subject: [Scipy-svn] r2088 - trunk/Lib/optimize/cobyla Message-ID: <20060712232616.0655839C066@new.scipy.org> Author: oliphant Date: 2006-07-12 18:26:13 -0500 (Wed, 12 Jul 2006) New Revision: 2088 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: More printing for cobyla.f Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-12 22:54:28 UTC (rev 2087) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-12 23:26:13 UTC (rev 2088) @@ -139,7 +139,13 @@ IF (IPRINT .EQ. 3) PRINT 51, (SIM(J,NP),J=1,IPTEM) 51 FORMAT (/3X, 'SIM = ', 1PE13.6, 1P4E15.6) NFVALS=NFVALS+1 + IF (IPRINT .EQ. 3) THEN + PRINT *, ' BEFORE: ', N, M, (X(I),I=1,N), F, (CON(I),I=1,M) + END IF CALL CALCFC (N,M,X,F,CON) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' AFTER: ', N, M, (X(I),I=1,N), F, (CON(I),I=1,M) + END IF RESMAX=0.0d0 IF (M .GT. 0) THEN DO 60 K=1,M From scipy-svn at scipy.org Wed Jul 12 23:05:42 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 22:05:42 -0500 (CDT) Subject: [Scipy-svn] r2089 - trunk/Lib/optimize/cobyla Message-ID: <20060713030542.BCD8E39C066@new.scipy.org> Author: oliphant Date: 2006-07-12 22:05:39 -0500 (Wed, 12 Jul 2006) New Revision: 2089 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: More printing... Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-12 23:26:13 UTC (rev 2088) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 03:05:39 UTC (rev 2089) @@ -131,15 +131,19 @@ C the algorithm. C 40 IF (NFVALS .GE. MAXFUN .AND. NFVALS .GT. 0) THEN - IF (IPRINT .GE. 1) PRINT 50 - 50 FORMAT (/3X,'Return from subroutine COBYLA because the ', - 1 'MAXFUN limit has been reached.') - GOTO 600 + IF (IPRINT .GE. 1) PRINT 50 + 50 FORMAT (/3X,'Return from subroutine COBYLA because the ', + 1 'MAXFUN limit has been reached.') + GOTO 600 END IF - IF (IPRINT .EQ. 3) PRINT 51, (SIM(J,NP),J=1,IPTEM) - 51 FORMAT (/3X, 'SIM = ', 1PE13.6, 1P4E15.6) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' SIM = ', (SIM(J,NP),J=1,N) + END IF NFVALS=NFVALS+1 IF (IPRINT .EQ. 3) THEN + PRINT *, ' DX = ', (DX(I),I=1,N) + END IF + IF (IPRINT .EQ. 3) THEN PRINT *, ' BEFORE: ', N, M, (X(I),I=1,N), F, (CON(I),I=1,M) END IF CALL CALCFC (N,M,X,F,CON) @@ -282,6 +286,15 @@ VETA(J)=SQRT(WETA) IF (VSIG(J) .LT. PARSIG .OR. VETA(J) .GT. PARETA) IFLAG=0 260 CONTINUE + IF (IPRINT .EQ. 3) THEN + PRINT *, ' VSIG = ', (VSIG(J),J=1,N), ' -- ', PARSIG + END IF + IF (IPRINT .EQ. 3) THEN + PRINT *, ' VETA = ', (VETA(J),J=1,N), ' -- ', PARETA + END IF + IF (IPRINT .EQ. 3) THEN + PRINT *, ' IBRNCH, IFLAG = ', IBRNCH, IFLAG + END IF C C If a new vertex is needed to improve acceptability, then decide which C vertex to drop from the simplex. @@ -307,13 +320,14 @@ C Calculate the step to the new vertex and its sign. C TEMP=GAMMA*RHO*VSIG(JDROP) - IF (IPRINT .EQ. 3) PRINT 289, (SIMI(JDROP,I),I=1,IPTEM) - 289 FORMAT (/3X, 'SIMI =',1PE13.6,1P4E15.6) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' SIMI =', (SIMI(JDROP,I),I=1,N) + END IF DO 290 I=1,N 290 DX(I)=TEMP*SIMI(JDROP,I) - IF (IPRINT .EQ. 3) PRINT 291, (DX(I),I=1,IPTEM) - 291 FORMAT (/3X, 'DX =',1PE13.6,1P4E15.6) - + IF (IPRINT .EQ. 3) THEN + PRINT *, ' DX =', (DX(I),I=1,N) + END IF CVMAXP=0.0d0 CVMAXM=0.0d0 DO 310 K=1,MP From scipy-svn at scipy.org Wed Jul 12 23:11:35 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 22:11:35 -0500 (CDT) Subject: [Scipy-svn] r2090 - trunk/Lib/optimize/cobyla Message-ID: <20060713031135.ED74939C066@new.scipy.org> Author: oliphant Date: 2006-07-12 22:11:33 -0500 (Wed, 12 Jul 2006) New Revision: 2090 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: More printing... Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 03:05:39 UTC (rev 2089) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 03:11:33 UTC (rev 2090) @@ -373,6 +373,10 @@ IVMD=IDXNEW+N CALL TRSTLP (N,M,A,CON,RHO,DX,IFULL,IACT,W(IZ),W(IZDOTA), 1 W(IVMC),W(ISDIRN),W(IDXNEW),W(IVMD)) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' **DX = ', (DX(I),I=1,N) + PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) + END IF IF (IFULL .EQ. 0) THEN TEMP=0.0d0 DO 380 I=1,N From scipy-svn at scipy.org Wed Jul 12 23:22:37 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 12 Jul 2006 22:22:37 -0500 (CDT) Subject: [Scipy-svn] r2091 - trunk/Lib/optimize/cobyla Message-ID: <20060713032237.09D9939C066@new.scipy.org> Author: oliphant Date: 2006-07-12 22:22:34 -0500 (Wed, 12 Jul 2006) New Revision: 2091 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: More printing... Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 03:11:33 UTC (rev 2090) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 03:22:34 UTC (rev 2091) @@ -287,12 +287,10 @@ IF (VSIG(J) .LT. PARSIG .OR. VETA(J) .GT. PARETA) IFLAG=0 260 CONTINUE IF (IPRINT .EQ. 3) THEN + print *, ' SIMI = ', ((SIMI(I,J),I=1,N),J=1,N) + print *, ' SIM = ', ((SIM(I,J),I=1,N),J=1,N) PRINT *, ' VSIG = ', (VSIG(J),J=1,N), ' -- ', PARSIG - END IF - IF (IPRINT .EQ. 3) THEN PRINT *, ' VETA = ', (VETA(J),J=1,N), ' -- ', PARETA - END IF - IF (IPRINT .EQ. 3) THEN PRINT *, ' IBRNCH, IFLAG = ', IBRNCH, IFLAG END IF C From scipy-svn at scipy.org Thu Jul 13 04:50:19 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 03:50:19 -0500 (CDT) Subject: [Scipy-svn] r2092 - trunk/Lib/optimize/cobyla Message-ID: <20060713085019.1D95839C037@new.scipy.org> Author: oliphant Date: 2006-07-13 03:50:16 -0500 (Thu, 13 Jul 2006) New Revision: 2092 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: more printing Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 03:22:34 UTC (rev 2091) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 08:50:16 UTC (rev 2092) @@ -61,6 +61,7 @@ C Partition the working space array W to provide the storage that is needed C for the main calculation. C + ITOTAL=N*(3*N+2*M+11)+4*M+6 MPP=M+2 ICON=1 ISIM=ICON+MPP @@ -136,14 +137,10 @@ 1 'MAXFUN limit has been reached.') GOTO 600 END IF + NFVALS=NFVALS+1 IF (IPRINT .EQ. 3) THEN PRINT *, ' SIM = ', (SIM(J,NP),J=1,N) - END IF - NFVALS=NFVALS+1 - IF (IPRINT .EQ. 3) THEN PRINT *, ' DX = ', (DX(I),I=1,N) - END IF - IF (IPRINT .EQ. 3) THEN PRINT *, ' BEFORE: ', N, M, (X(I),I=1,N), F, (CON(I),I=1,M) END IF CALL CALCFC (N,M,X,F,CON) @@ -292,6 +289,7 @@ PRINT *, ' VSIG = ', (VSIG(J),J=1,N), ' -- ', PARSIG PRINT *, ' VETA = ', (VETA(J),J=1,N), ' -- ', PARETA PRINT *, ' IBRNCH, IFLAG = ', IBRNCH, IFLAG + PRINT *, ' A = ', ((A(I,J),I=1,N),J=1,MP) END IF C C If a new vertex is needed to improve acceptability, then decide which @@ -374,6 +372,10 @@ IF (IPRINT .EQ. 3) THEN PRINT *, ' **DX = ', (DX(I),I=1,N) PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) + PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL + PRINT *, ' **CON = ', (CON(I),I=1,M) + PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,MP) + PRINT *, ' **W = ', (W(I),I=1,ITOTAL) END IF IF (IFULL .EQ. 0) THEN TEMP=0.0d0 From scipy-svn at scipy.org Thu Jul 13 04:50:54 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 03:50:54 -0500 (CDT) Subject: [Scipy-svn] r2093 - trunk/Lib/optimize/cobyla Message-ID: <20060713085054.4464A39C037@new.scipy.org> Author: oliphant Date: 2006-07-13 03:50:51 -0500 (Thu, 13 Jul 2006) New Revision: 2093 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: more printing Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 08:50:16 UTC (rev 2092) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 08:50:51 UTC (rev 2093) @@ -367,9 +367,19 @@ ISDIRN=IVMC+MP IDXNEW=ISDIRN+N IVMD=IDXNEW+N + IF (IPRINT .EQ. 3) THEN + print *, 'BEFORE trstlp:' + PRINT *, ' **DX = ', (DX(I),I=1,N) + PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) + PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL + PRINT *, ' **CON = ', (CON(I),I=1,M) + PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,MP) + PRINT *, ' **W = ', (W(I),I=1,ITOTAL) + END IF CALL TRSTLP (N,M,A,CON,RHO,DX,IFULL,IACT,W(IZ),W(IZDOTA), 1 W(IVMC),W(ISDIRN),W(IDXNEW),W(IVMD)) IF (IPRINT .EQ. 3) THEN + print *, 'AFTER trstlp:' PRINT *, ' **DX = ', (DX(I),I=1,N) PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL From scipy-svn at scipy.org Thu Jul 13 04:53:20 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 03:53:20 -0500 (CDT) Subject: [Scipy-svn] r2094 - trunk/Lib/optimize/cobyla Message-ID: <20060713085320.98B0539C032@new.scipy.org> Author: oliphant Date: 2006-07-13 03:53:18 -0500 (Thu, 13 Jul 2006) New Revision: 2094 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: more printing Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 08:50:51 UTC (rev 2093) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 08:53:18 UTC (rev 2094) @@ -374,7 +374,6 @@ PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL PRINT *, ' **CON = ', (CON(I),I=1,M) PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,MP) - PRINT *, ' **W = ', (W(I),I=1,ITOTAL) END IF CALL TRSTLP (N,M,A,CON,RHO,DX,IFULL,IACT,W(IZ),W(IZDOTA), 1 W(IVMC),W(ISDIRN),W(IDXNEW),W(IVMD)) From scipy-svn at scipy.org Thu Jul 13 04:58:34 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 03:58:34 -0500 (CDT) Subject: [Scipy-svn] r2095 - trunk/Lib/optimize/cobyla Message-ID: <20060713085834.C0E2139C032@new.scipy.org> Author: oliphant Date: 2006-07-13 03:58:32 -0500 (Thu, 13 Jul 2006) New Revision: 2095 Modified: trunk/Lib/optimize/cobyla/cobyla2.f Log: cobyla debug print Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 08:53:18 UTC (rev 2094) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-13 08:58:32 UTC (rev 2095) @@ -61,7 +61,6 @@ C Partition the working space array W to provide the storage that is needed C for the main calculation. C - ITOTAL=N*(3*N+2*M+11)+4*M+6 MPP=M+2 ICON=1 ISIM=ICON+MPP @@ -92,6 +91,7 @@ C Further, SIMI holds the inverse of the matrix that is contained in the C first N columns of SIM. C + ITOTAL=N*(3*N+2*M+11)+4*M+6 IPTEM=MIN0(N,5) IPTEMP=IPTEM+1 NP=N+1 @@ -368,16 +368,20 @@ IDXNEW=ISDIRN+N IVMD=IDXNEW+N IF (IPRINT .EQ. 3) THEN + print *, ' ' print *, 'BEFORE trstlp:' PRINT *, ' **DX = ', (DX(I),I=1,N) PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL PRINT *, ' **CON = ', (CON(I),I=1,M) PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,MP) + PRINT *, ' **W = ', (W(I),I=1,ITOTAL) + print *, ' ' END IF CALL TRSTLP (N,M,A,CON,RHO,DX,IFULL,IACT,W(IZ),W(IZDOTA), 1 W(IVMC),W(ISDIRN),W(IDXNEW),W(IVMD)) IF (IPRINT .EQ. 3) THEN + print *, ' ' print *, 'AFTER trstlp:' PRINT *, ' **DX = ', (DX(I),I=1,N) PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) @@ -385,6 +389,8 @@ PRINT *, ' **CON = ', (CON(I),I=1,M) PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,MP) PRINT *, ' **W = ', (W(I),I=1,ITOTAL) + print *, ' ' + print *, ' ' END IF IF (IFULL .EQ. 0) THEN TEMP=0.0d0 From scipy-svn at scipy.org Thu Jul 13 09:35:15 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 08:35:15 -0500 (CDT) Subject: [Scipy-svn] r2096 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060713133515.5633B39C061@new.scipy.org> Author: fullung Date: 2006-07-13 08:34:55 -0500 (Thu, 13 Jul 2006) New Revision: 2096 Modified: trunk/Lib/sandbox/svm/README trunk/Lib/sandbox/svm/libsvm.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_dataset.py trunk/Lib/sandbox/svm/tests/test_kernel.py trunk/Lib/sandbox/svm/tests/test_libsvm.py trunk/Lib/sandbox/svm/tests/test_oneclass.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Simplify libsvm wrapper and make tests run without having to set PYTHONPATH. Modified: trunk/Lib/sandbox/svm/README =================================================================== --- trunk/Lib/sandbox/svm/README 2006-07-13 08:58:32 UTC (rev 2095) +++ trunk/Lib/sandbox/svm/README 2006-07-13 13:34:55 UTC (rev 2096) @@ -45,7 +45,6 @@ On Windows: cd tests -set PYTHONPATH=..;..\.. test_classification.py ... test_all.py @@ -53,7 +52,6 @@ On Linux: cd tests -export PYTHONPATH=..:../.. python test_classification.py ... python test_all.py Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-07-13 08:58:32 UTC (rev 2095) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-07-13 13:34:55 UTC (rev 2096) @@ -9,21 +9,7 @@ 'POLY', 'RBF', 'SIGMOID', - 'PRECOMPUTED', - 'svm_node', - 'svm_parameter', - 'svm_problem', - 'svm_model', - 'svm_check_parameter', - 'svm_train', - 'svm_check_probability_model', - 'svm_predict', - 'svm_predict_values', - 'svm_predict_probability', - 'svm_get_svr_probability', - 'svm_cross_validation', - 'svm_destroy_model', - 'svm_destroy_param' + 'PRECOMPUTED' ] import numpy as N @@ -122,56 +108,45 @@ ('free_sv', c_int) ] -# svm_check_parameter -_libsvm.svm_check_parameter.restype = c_char_p -_libsvm.svm_check_parameter.argtypes = \ - [POINTER(svm_problem), POINTER(svm_parameter)] -svm_check_parameter = _libsvm.svm_check_parameter +libsvm_api = { + 'svm_check_parameter' : + (c_char_p, [POINTER(svm_problem), POINTER(svm_parameter)]), + 'svm_train' : + (POINTER(svm_model), [POINTER(svm_problem), POINTER(svm_parameter)]), + 'svm_check_probability_model' : + (None, [POINTER(svm_model)]), + 'svm_predict' : + (c_double, [POINTER(svm_model), POINTER(svm_node)]), + 'svm_predict_values' : + (None, [POINTER(svm_model), POINTER(svm_node), POINTER(c_double)]), + 'svm_predict_probability' : + (c_double, [POINTER(svm_model), POINTER(svm_node), POINTER(c_double)]), + 'svm_get_svr_probability' : + (c_double, [POINTER(svm_model)]), + 'svm_cross_validation' : + (None, + [POINTER(svm_problem), POINTER(svm_parameter), c_int, POINTER(c_double)]), + 'svm_destroy_model' : + (None, [POINTER(svm_model)]) + } -# svm_train -_libsvm.svm_train.argtypes = \ - [POINTER(svm_problem), POINTER(svm_parameter)] -_libsvm.svm_train.restype = POINTER(svm_model) -svm_train = _libsvm.svm_train +import inspect +for f, (restype, argtypes) in libsvm_api.iteritems(): + func = getattr(_libsvm, f) + func.restype = restype + func.argtypes = argtypes + inspect.currentframe().f_locals[f] = func -# svm_check_probability_model -_libsvm.svm_check_probability_model.argtypes = [POINTER(svm_model)] -svm_check_probability_model = _libsvm.svm_check_probability_model - -# svm_predict -_libsvm.svm_predict.argtypes = [POINTER(svm_model), POINTER(svm_node)] -_libsvm.svm_predict.restype = c_double -svm_predict = _libsvm.svm_predict - -# svm_predict_values -_libsvm.svm_predict_values.argtypes = \ - [POINTER(svm_model), POINTER(svm_node), POINTER(c_double)] -svm_predict_values = _libsvm.svm_predict_values - -# svm_predict_probability -_libsvm.svm_predict_probability.restype = c_double -_libsvm.svm_predict_probability.argtypes = \ - [POINTER(svm_model), POINTER(svm_node), POINTER(c_double)] -svm_predict_probability = _libsvm.svm_predict_probability - -# svm_get_svr_probability -_libsvm.svm_get_svr_probability.restype = c_double -_libsvm.svm_get_svr_probability.argtypes = [POINTER(svm_model)] -svm_get_svr_probability = _libsvm.svm_get_svr_probability - -# svm_cross_validation -_libsvm.svm_cross_validation.argtypes = [ - POINTER(svm_problem), - POINTER(svm_parameter), - c_int, - POINTER(c_double) - ] -svm_cross_validation = _libsvm.svm_cross_validation - -# svm_destroy_model -_libsvm.svm_destroy_model.argtypes = [POINTER(svm_model)] -svm_destroy_model = _libsvm.svm_destroy_model - -# svm_destroy_param -_libsvm.svm_destroy_param.argtypes = [POINTER(svm_parameter)] -svm_destroy_param = _libsvm.svm_destroy_param +__all__ = [ + 'svm_node_dtype', + 'C_SVC', + 'NU_SVC', + 'ONE_CLASS', + 'EPSILON_SVR', + 'NU_SVR', + 'LINEAR', + 'POLY', + 'RBF', + 'SIGMOID', + 'PRECOMPUTED' + ] + libsvm_api.keys() Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-13 08:58:32 UTC (rev 2095) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-13 13:34:55 UTC (rev 2096) @@ -1,11 +1,15 @@ -from numpy.testing import * import numpy as N +from numpy.testing import * +set_local_path('../..') + from svm.classification import * from svm.dataset import LibSvmClassificationDataSet from svm.dataset import LibSvmTestDataSet from svm.kernel import * +restore_path() + class test_classification(NumpyTestCase): def check_basics(self): Model = LibSvmCClassificationModel Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-13 08:58:32 UTC (rev 2095) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-13 13:34:55 UTC (rev 2096) @@ -1,10 +1,14 @@ -from numpy.testing import * import numpy as N +from numpy.testing import * +set_local_path('../..') + from svm.dataset import convert_to_svm_node from svm.dataset import * from svm.libsvm import svm_node_dtype +restore_path() + class test_dataset(NumpyTestCase): def check_convert_dict(self): x = N.array([(-1,0.)], dtype=svm_node_dtype) Modified: trunk/Lib/sandbox/svm/tests/test_kernel.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_kernel.py 2006-07-13 08:58:32 UTC (rev 2095) +++ trunk/Lib/sandbox/svm/tests/test_kernel.py 2006-07-13 13:34:55 UTC (rev 2096) @@ -1,8 +1,12 @@ -from numpy.testing import * import numpy as N +from numpy.testing import * +set_local_path('../..') + from svm.kernel import * +restore_path() + class test_kernel(NumpyTestCase): def check_linear_kernel(self): kernel = LinearKernel() Modified: trunk/Lib/sandbox/svm/tests/test_libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_libsvm.py 2006-07-13 08:58:32 UTC (rev 2095) +++ trunk/Lib/sandbox/svm/tests/test_libsvm.py 2006-07-13 13:34:55 UTC (rev 2096) @@ -1,8 +1,12 @@ -from numpy.testing import * import numpy as N +from numpy.testing import * +set_local_path('../..') + import svm.libsvm as libsvm +restore_path() + class test_libsvm(NumpyTestCase): def check_svm_node(self): node = libsvm.svm_node() Modified: trunk/Lib/sandbox/svm/tests/test_oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-13 08:58:32 UTC (rev 2095) +++ trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-13 13:34:55 UTC (rev 2096) @@ -1,11 +1,15 @@ -from numpy.testing import * import numpy as N +from numpy.testing import * +set_local_path('../..') + from svm.oneclass import * from svm.dataset import LibSvmOneClassDataSet from svm.dataset import LibSvmTestDataSet from svm.kernel import * +restore_path() + class test_oneclass(NumpyTestCase): def check_basics(self): Model = LibSvmOneClassModel Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-13 08:58:32 UTC (rev 2095) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-13 13:34:55 UTC (rev 2096) @@ -1,11 +1,15 @@ -from numpy.testing import * import numpy as N +from numpy.testing import * +set_local_path('../..') + from svm.regression import * from svm.dataset import LibSvmRegressionDataSet from svm.dataset import LibSvmTestDataSet from svm.kernel import * +restore_path() + class test_regression(NumpyTestCase): def check_basics(self): Model = LibSvmEpsilonRegressionModel From scipy-svn at scipy.org Thu Jul 13 10:22:21 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 09:22:21 -0500 (CDT) Subject: [Scipy-svn] r2097 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060713142221.B9AFD39C061@new.scipy.org> Author: fullung Date: 2006-07-13 09:22:09 -0500 (Thu, 13 Jul 2006) New Revision: 2097 Modified: trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/tests/test_dataset.py Log: Dataset for making precomputed kernels. Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-13 13:34:55 UTC (rev 2096) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-13 14:22:09 UTC (rev 2097) @@ -20,6 +20,34 @@ return 1.0 / maxlen gamma = property(getgamma, 'Gamma parameter for RBF kernel') + def precompute(self): + return LibSvmPrecomputedDataSet(self.data) + +class LibSvmPrecomputedDataSet: + def __init__(self, origdata): + # XXX look at using a list of vectors instead of a matrix when + # the size of the precomputed dataset gets huge. This should + # avoid problems with heap fragmentation, especially on + # Windows. + + n = len(origdata) + # extra columns for id and end of record marker + grammat = N.empty((n, n+2), dtype=libsvm.svm_node_dtype) + # calculate Gram matrix + for i, (y1, x1) in enumerate(origdata): + # set id and end of record fields + grammat[i,0], grammat[i,-1] = (0, i), (-1, 0.0) + for j, (y2, x2) in enumerate(origdata[i:]): + # Gram matrix is symmetric, so calculate dot product + # once and store it in both required locations + z = svm_node_dot(x1, x2) + # fix index so we assign to the right place + j += i + grammat[i, j+1]['value'] = z + grammat[j, i+1]['value'] = z + self.grammat = grammat + self.data = zip(map(lambda x: x[0], origdata), grammat) + class LibSvmRegressionDataSet(LibSvmDataSet): def __init__(self, origdata): data = map(lambda x: (x[0], convert_to_svm_node(x[1])), origdata) @@ -59,3 +87,16 @@ assert len(x) == len(N.unique(y[:-1]['index'])), \ 'indexes must be unique' return y + +def svm_node_dot(x, y): + # associate node indexes with array indexes + xidx = dict(zip(x['index'][:-1],range(0,len(x)))) + yidx = dict(zip(y['index'][:-1],range(0,len(y)))) + # indexes in either vector + indexes = N.unique(N.hstack([x['index'],y['index']])) + z = 0. + for j in indexes: + if j in xidx and j in yidx: + # dot if index is present in both vectors + z += x['value'][xidx[j]]*y['value'][yidx[j]] + return z Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-13 13:34:55 UTC (rev 2096) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-13 14:22:09 UTC (rev 2097) @@ -3,8 +3,8 @@ from numpy.testing import * set_local_path('../..') -from svm.dataset import convert_to_svm_node from svm.dataset import * +from svm.dataset import convert_to_svm_node, svm_node_dot from svm.libsvm import svm_node_dtype restore_path() @@ -59,5 +59,29 @@ dataset = LibSvmOneClassDataSet(data) self.assertAlmostEqual(dataset.gamma, 0.5) +class test_svm_node_dot(NumpyTestCase): + def check_dot(self): + x = N.array([(-1,0.)], dtype=svm_node_dtype) + self.assertAlmostEqual(svm_node_dot(x, x), 0.) + + x = N.array([(1,1.),(-1,0.)], dtype=svm_node_dtype) + y = N.array([(2,2.),(-1,0.)], dtype=svm_node_dtype) + self.assertAlmostEqual(svm_node_dot(x, y), 0.) + + x = N.array([(3,2.),(-1,0.)], dtype=svm_node_dtype) + y = N.array([(3,2.),(-1,0.)], dtype=svm_node_dtype) + self.assertAlmostEqual(svm_node_dot(x, y), 4.) + +class test_precomputed_dataset(NumpyTestCase): + def check_foo(self): + y = N.random.randn(50) + x = N.random.randn(len(y), 1) + expected_dotprods = N.dot(x, N.transpose(x)) + origdata = LibSvmRegressionDataSet(zip(y, x)) + # get a new dataset containing the precomputed data + pcdata = origdata.precompute() + actual_dotprods = pcdata.grammat[:,1:-1]['value'] + assert_array_almost_equal(actual_dotprods, expected_dotprods) + if __name__ == '__main__': NumpyTest().run() From scipy-svn at scipy.org Thu Jul 13 10:26:50 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 09:26:50 -0500 (CDT) Subject: [Scipy-svn] r2098 - trunk/Lib/weave/scxx Message-ID: <20060713142650.D70BE39C061@new.scipy.org> Author: fullung Date: 2006-07-13 09:26:39 -0500 (Thu, 13 Jul 2006) New Revision: 2098 Modified: trunk/Lib/weave/scxx/list.h trunk/Lib/weave/scxx/object.h trunk/Lib/weave/scxx/sequence.h trunk/Lib/weave/scxx/tuple.h Log: Repair Weave build on FC5. Fixes #232. Modified: trunk/Lib/weave/scxx/list.h =================================================================== --- trunk/Lib/weave/scxx/list.h 2006-07-13 14:22:09 UTC (rev 2097) +++ trunk/Lib/weave/scxx/list.h 2006-07-13 14:26:39 UTC (rev 2098) @@ -162,23 +162,23 @@ }; return *this; }; - list& list::insert(int ndx, int other) { + list& insert(int ndx, int other) { object oth = other; return insert(ndx, oth); }; - list& list::insert(int ndx, double other) { + list& insert(int ndx, double other) { object oth = other; return insert(ndx, oth); }; - list& list::insert(int ndx, std::complex& other) { + list& insert(int ndx, std::complex& other) { object oth = other; return insert(ndx, oth); }; - list& list::insert(int ndx, const char* other) { + list& insert(int ndx, const char* other) { object oth = other; return insert(ndx, oth); }; - list& list::insert(int ndx, const std::string& other) { + list& insert(int ndx, const std::string& other) { object oth = other; return insert(ndx, oth); }; Modified: trunk/Lib/weave/scxx/object.h =================================================================== --- trunk/Lib/weave/scxx/object.h 2006-07-13 14:22:09 UTC (rev 2097) +++ trunk/Lib/weave/scxx/object.h 2006-07-13 14:26:39 UTC (rev 2098) @@ -597,7 +597,7 @@ // be inlined instead of implemented i weave_imp.cpp. This // provides less automatic type checking, but is potentially faster. //------------------------------------------------------------------------- - object object::mcall(const char* nm) { + object mcall(const char* nm) { object method = attr(nm); PyObject* result = PyEval_CallObjectWithKeywords(method,NULL,NULL); if (!result) @@ -605,7 +605,7 @@ return object(lose_ref(result)); } - object object::mcall(const char* nm, object& args_tup) { + object mcall(const char* nm, object& args_tup) { object method = attr(nm); PyObject* result = PyEval_CallObjectWithKeywords(method,args_tup,NULL); if (!result) @@ -613,7 +613,7 @@ return object(lose_ref(result)); } - object object::mcall(const char* nm, object& args_tup, object& kw_dict) { + object mcall(const char* nm, object& args_tup, object& kw_dict) { object method = attr(nm); PyObject* result = PyEval_CallObjectWithKeywords(method,args_tup,kw_dict); if (!result) @@ -636,19 +636,19 @@ // // Note: see not on mcall() //------------------------------------------------------------------------- - object object::call() const { + object call() const { PyObject *rslt = PyEval_CallObjectWithKeywords(*this, NULL, NULL); if (rslt == 0) throw 1; return object(lose_ref(rslt)); } - object object::call(object& args_tup) const { + object call(object& args_tup) const { PyObject *rslt = PyEval_CallObjectWithKeywords(*this, args_tup, NULL); if (rslt == 0) throw 1; return object(lose_ref(rslt)); } - object object::call(object& args_tup, object& kw_dict) const { + object call(object& args_tup, object& kw_dict) const { PyObject *rslt = PyEval_CallObjectWithKeywords(*this, args_tup, kw_dict); if (rslt == 0) throw 1; Modified: trunk/Lib/weave/scxx/sequence.h =================================================================== --- trunk/Lib/weave/scxx/sequence.h 2006-07-13 14:22:09 UTC (rev 2097) +++ trunk/Lib/weave/scxx/sequence.h 2006-07-13 14:26:39 UTC (rev 2098) @@ -148,23 +148,23 @@ fail(PyExc_RuntimeError, "problem in in"); return (rslt==1); }; - bool sequence::in(int value) const { + bool in(int value) const { object val = value; return in(val); }; - bool sequence::in(double value) const { + bool in(double value) const { object val = value; return in(val); }; - bool sequence::in(const std::complex& value) const { + bool in(const std::complex& value) const { object val = value; return in(val); }; - bool sequence::in(const char* value) const { + bool in(const char* value) const { object val = value; return in(val); }; - bool sequence::in(const std::string& value) const { + bool in(const std::string& value) const { object val = value.c_str(); return in(val); }; @@ -179,23 +179,23 @@ fail(PyExc_IndexError, "value not found"); return rslt; }; - int sequence::index(int value) const { + int index(int value) const { object val = value; return index(val); }; - int sequence::index(double value) const { + int index(double value) const { object val = value; return index(val); }; - int sequence::index(const std::complex& value) const { + int index(const std::complex& value) const { object val = value; return index(val); }; - int sequence::index(const char* value) const { + int index(const char* value) const { object val = value; return index(val); }; - int sequence::index(const std::string& value) const { + int index(const std::string& value) const { object val = value; return index(val); }; @@ -228,32 +228,32 @@ sequence& _parent; int _ndx; public: - indexed_ref::indexed_ref(PyObject* obj, sequence& parent, int ndx) + indexed_ref(PyObject* obj, sequence& parent, int ndx) : object(obj), _parent(parent), _ndx(ndx) { }; virtual ~indexed_ref() {}; - indexed_ref& indexed_ref::operator=(const object& other) { + indexed_ref& operator=(const object& other) { grab_ref(other); _parent.set_item(_ndx, *this); return *this; }; - indexed_ref& indexed_ref::operator=(int other) { + indexed_ref& operator=(int other) { object oth = other; return operator=(oth); }; - indexed_ref& indexed_ref::operator=(double other) { + indexed_ref& operator=(double other) { object oth = other; return operator=(oth); }; - indexed_ref& indexed_ref::operator=(const std::complex& other) { + indexed_ref& operator=(const std::complex& other) { object oth = other; return operator=(oth); }; - indexed_ref& indexed_ref::operator=(const char* other) { + indexed_ref& operator=(const char* other) { object oth = other; return operator=(oth); }; - indexed_ref& indexed_ref::operator=(const std::string& other) { + indexed_ref& operator=(const std::string& other) { object oth = other; return operator=(oth); }; Modified: trunk/Lib/weave/scxx/tuple.h =================================================================== --- trunk/Lib/weave/scxx/tuple.h 2006-07-13 14:22:09 UTC (rev 2097) +++ trunk/Lib/weave/scxx/tuple.h 2006-07-13 14:26:39 UTC (rev 2098) @@ -16,7 +16,7 @@ tuple(int sz=0) : sequence (PyTuple_New(sz)) { lose_ref(_obj); } tuple(const tuple& other) : sequence(other) { } tuple(PyObject* obj) : sequence(obj) { _violentTypeCheck(); } - tuple::tuple(const list& lst) + tuple(const list& lst) : sequence (PyList_AsTuple(lst)) { lose_ref(_obj); } //------------------------------------------------------------------------- @@ -65,7 +65,7 @@ //------------------------------------------------------------------------- // operator[] -- const and non-const versions of element access. //------------------------------------------------------------------------- - indexed_ref tuple::operator [] (int i) { + indexed_ref operator [] (int i) { // get a "borrowed" refcount PyObject* o = PyTuple_GetItem(_obj, i); // don't throw error for when [] fails because it might be on left hand From scipy-svn at scipy.org Thu Jul 13 10:45:33 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 09:45:33 -0500 (CDT) Subject: [Scipy-svn] r2099 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060713144533.4675539C061@new.scipy.org> Author: fullung Date: 2006-07-13 09:45:21 -0500 (Thu, 13 Jul 2006) New Revision: 2099 Modified: trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/tests/test_dataset.py Log: Precompute with any kernel. Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-13 14:26:39 UTC (rev 2098) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-13 14:45:21 UTC (rev 2099) @@ -20,11 +20,13 @@ return 1.0 / maxlen gamma = property(getgamma, 'Gamma parameter for RBF kernel') - def precompute(self): - return LibSvmPrecomputedDataSet(self.data) + def precompute(self, kernel): + return LibSvmPrecomputedDataSet(kernel, self.data) class LibSvmPrecomputedDataSet: - def __init__(self, origdata): + def __init__(self, kernel, origdata): + self.kernel = kernel + # XXX look at using a list of vectors instead of a matrix when # the size of the precomputed dataset gets huge. This should # avoid problems with heap fragmentation, especially on @@ -40,7 +42,7 @@ for j, (y2, x2) in enumerate(origdata[i:]): # Gram matrix is symmetric, so calculate dot product # once and store it in both required locations - z = svm_node_dot(x1, x2) + z = kernel(x1, x2, svm_node_dot) # fix index so we assign to the right place j += i grammat[i, j+1]['value'] = z Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-13 14:26:39 UTC (rev 2098) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-13 14:45:21 UTC (rev 2099) @@ -4,6 +4,7 @@ set_local_path('../..') from svm.dataset import * +from svm.kernel import * from svm.dataset import convert_to_svm_node, svm_node_dot from svm.libsvm import svm_node_dtype @@ -73,15 +74,28 @@ self.assertAlmostEqual(svm_node_dot(x, y), 4.) class test_precomputed_dataset(NumpyTestCase): - def check_foo(self): - y = N.random.randn(50) - x = N.random.randn(len(y), 1) - expected_dotprods = N.dot(x, N.transpose(x)) + def check_precompute(self): + degree, gamma, coef0 = 4, 3.0, 2.0 + kernels = [ + LinearKernel(), + PolynomialKernel(degree, gamma, coef0), + RBFKernel(gamma), + SigmoidKernel(gamma, coef0) + ] + y = N.random.randn(20) + x = N.random.randn(len(y), 10) origdata = LibSvmRegressionDataSet(zip(y, x)) - # get a new dataset containing the precomputed data - pcdata = origdata.precompute() - actual_dotprods = pcdata.grammat[:,1:-1]['value'] - assert_array_almost_equal(actual_dotprods, expected_dotprods) + for kernel in kernels: + # calculate expected Gram matrix + expt_grammat = N.empty((len(y),)*2) + for i, xi in enumerate(x): + for j, xj in enumerate(x): + expt_grammat[i, j] = kernel(xi, xj, N.dot) + # get a new dataset containing the precomputed data + pcdata = origdata.precompute(kernel) + actual_grammat = pcdata.grammat[:,1:-1]['value'] + assert_array_almost_equal(actual_grammat, expt_grammat) + if __name__ == '__main__': NumpyTest().run() From scipy-svn at scipy.org Thu Jul 13 20:06:32 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 13 Jul 2006 19:06:32 -0500 (CDT) Subject: [Scipy-svn] r2100 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060714000632.EFDE739C0E3@new.scipy.org> Author: fullung Date: 2006-07-13 19:06:15 -0500 (Thu, 13 Jul 2006) New Revision: 2100 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/kernel.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_dataset.py Log: Working on support for precomputed kernels. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-13 14:45:21 UTC (rev 2099) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-14 00:06:15 UTC (rev 2100) @@ -130,7 +130,7 @@ This function returns the percentage of data that was classified correctly over all the experiments. """ - problem = self._create_problem(dataset) + problem = dataset.create_svm_problem() target = N.empty((len(dataset.data),), dtype=N.float64) tp = cast(target.ctypes.data, POINTER(c_double)) libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-13 14:45:21 UTC (rev 2099) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-14 00:06:15 UTC (rev 2100) @@ -5,6 +5,7 @@ 'LibSvmTestDataSet' ] +from ctypes import c_double, POINTER, cast import numpy as N import libsvm @@ -23,33 +24,96 @@ def precompute(self, kernel): return LibSvmPrecomputedDataSet(kernel, self.data) + def create_svm_problem(self): + problem = libsvm.svm_problem() + problem.l = len(self.data) + y = (c_double*problem.l)() + x = (POINTER(libsvm.svm_node)*problem.l)() + for i, (yi, xi) in enumerate(self.data): + y[i] = yi + x[i] = cast(xi.ctypes.data, POINTER(libsvm.svm_node)) + problem.x = x + problem.y = y + return problem + class LibSvmPrecomputedDataSet: - def __init__(self, kernel, origdata): + def __init__(self, kernel, origdata=None): self.kernel = kernel + self.origdata = origdata + if origdata is None: return - # XXX look at using a list of vectors instead of a matrix when - # the size of the precomputed dataset gets huge. This should - # avoid problems with heap fragmentation, especially on - # Windows. + self.iddatamap = {} + # Create Gram matrix as a list of vectors that have extra + # entries for id and end of record marker. n = len(origdata) - # extra columns for id and end of record marker - grammat = N.empty((n, n+2), dtype=libsvm.svm_node_dtype) - # calculate Gram matrix + grammat = [N.empty((n+2,), dtype=libsvm.svm_node_dtype) + for i in range(n)] + self.grammat = grammat + + # Calculate Gram matrix. Refer to Kernel::kernel_precomputed + # in svm.cpp to see how this precomputed setup works. for i, (y1, x1) in enumerate(origdata): - # set id and end of record fields - grammat[i,0], grammat[i,-1] = (0, i), (-1, 0.0) + id = i + 1 + # XXX possible numpy bug + #grammat[i][[0,-1]] = (0, id), (-1, 0.0) + grammat[i][0] = 0, id + grammat[i][-1] = -1, 0.0 for j, (y2, x2) in enumerate(origdata[i:]): # Gram matrix is symmetric, so calculate dot product # once and store it in both required locations z = kernel(x1, x2, svm_node_dot) # fix index so we assign to the right place j += i - grammat[i, j+1]['value'] = z - grammat[j, i+1]['value'] = z - self.grammat = grammat - self.data = zip(map(lambda x: x[0], origdata), grammat) + grammat[i][j+1] = 0, z + grammat[j][i+1] = 0, z + # Map id to original vector so that we can find it again + # after the model has been trained. libsvm essentially + # provides the ids of the support vectors. + self.iddatamap[id] = x1 + + def getdata(self): + return zip(map(lambda x: x[0], self.origdata), self.grammat) + data = property(getdata) + def combine_inplace(self, dataset): + """ + Combine this dataset with another dataset by calculating the + new part of the Gram matrix in place. + """ + # XXX N.resize is our friend here + raise NotImplementedError + + def combine(self, dataset): + """ + Combine this dataset with another dataset by extending the + Gram matrix with the new inner products into a new matrix. + """ + n = len(self.origdata) + len(dataset.data) + newgrammat = [] + + # copy original Gram matrix + for i in range(len(self.origdata)): + row = N.empty((n,), dtype=libsvm.svm_node_dtype) + row[:-1] = self.grammat[i] + newgrammat.append(row) + + # copy id->vector map + newiddatamap = dict(self.iddatamap.items()) + + # prepare Gram matrix for new data + for i in range(len(dataset.data)): + id = i + len(self.origdata) + 1 + row = N.empty((n,), dtype=libsvm.svm_node_dtype) + row[[0,-1]] = (0, id), (-1, 0.0) + newgrammat.append(row) + newiddatamap[id] = dataset.data[i][1] + + newdataset = self.__class__(self.kernel) + newdataset.origdata = self.origdata + dataset.data + newdataset.iddatamap = newiddatamap + newdataset.grammat = newgrammat + class LibSvmRegressionDataSet(LibSvmDataSet): def __init__(self, origdata): data = map(lambda x: (x[0], convert_to_svm_node(x[1])), origdata) @@ -75,7 +139,7 @@ def convert_to_svm_node(x): y = N.empty(len(x)+1, dtype=libsvm.svm_node_dtype) - y[-1] = (-1, 0.) + y[-1] = -1, 0. if isinstance(x, dict): x = x.items() if isinstance(x, list): Modified: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-07-13 14:45:21 UTC (rev 2099) +++ trunk/Lib/sandbox/svm/kernel.py 2006-07-14 00:06:15 UTC (rev 2100) @@ -3,17 +3,24 @@ 'PolynomialKernel', 'RBFKernel', 'SigmoidKernel', - 'CustomKernel' + 'CustomKernel', + 'PrecomputedKernel' ] import numpy as N +import libsvm + class LinearKernel: + def __init__(self): + self.kernel_type = libsvm.LINEAR + def __call__(self, x, y, dot): return dot(x, y) class PolynomialKernel: def __init__(self, degree, gamma, coef0): + self.kernel_type = libsvm.POLY self.degree = degree self.gamma = gamma self.coef0 = coef0 @@ -31,6 +38,7 @@ class RBFKernel: def __init__(self, gamma): + self.kernel_type = libsvm.RBF self.gamma = gamma def __call__(self, x, y, dot): @@ -39,6 +47,7 @@ class SigmoidKernel: def __init__(self, gamma, coef0): + self.kernel_type = libsvm.SIGMOID self.gamma = gamma self.coef0 = coef0 @@ -47,7 +56,12 @@ class CustomKernel: def __init__(self, f): + self.kernel_type = libsvm.PRECOMPUTED self.f = f def __call__(self, x, y, dot): return self.f(x, y, dot) + +class PrecomputedKernel: + def __init__(self): + self.kernel_type = libsvm.PRECOMPUTED Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-13 14:45:21 UTC (rev 2099) +++ trunk/Lib/sandbox/svm/model.py 2006-07-14 00:06:15 UTC (rev 2100) @@ -24,65 +24,35 @@ self.shrinking = shrinking self.cache_size = cache_size + # kernel parameters param = libsvm.svm_parameter() + param.kernel_type = kernel.kernel_type + param.degree = getattr(kernel, 'degree', 0) + param.gamma = getattr(kernel, 'gamma', 0.0) + param.coef0 = getattr(kernel, 'coef0', 0.0) - if isinstance(kernel, LinearKernel): - param.kernel_type = libsvm.LINEAR - elif isinstance(kernel, PolynomialKernel): - param.kernel_type = libsvm.POLY - param.degree = kernel.degree - param.gamma = kernel.gamma - param.coef0 = kernel.coef0 - elif isinstance(kernel, RBFKernel): - param.kernel_type = libsvm.RBF - param.gamma = kernel.gamma - elif isinstance(kernel, SigmoidKernel): - param.kernel_type = libsvm.SIGMOID - param.gamma = kernel.gamma - param.coef0 = kernel.coef0 - else: - raise ValueError, 'unknown kernel type' - + # other parameters param.eps = tolerance param.shrinking = shrinking param.cache_size = cache_size - # set defaults for optional parameters + + # defaults for optional parameters param.nr_weight = 0 - #param.weight = None - #param.weight_label = None - # XXX workaround for bug in ctypes 0.9.9.6 param.weight = c_double_null_ptr param.weight_label = c_int_null_ptr param.probability = False self.param = param - def _create_problem(self, dataset): - # XXX don't poke around in dataset's internals - problem = libsvm.svm_problem() - problem.l = len(dataset.data) - y = (c_double*problem.l)() - x = (POINTER(libsvm.svm_node)*problem.l)() - for i, (yi, xi) in enumerate(dataset.data): - y[i] = yi - x[i] = cast(xi.ctypes.data, POINTER(libsvm.svm_node)) - problem.x = x - problem.y = y - self._check_problem_param(problem, self.param) - return problem - def fit(self, dataset): - problem = self._create_problem(dataset) + problem = dataset.create_svm_problem() + self._check_problem_param(problem, self.param) model = libsvm.svm_train(problem, self.param) - # weight parametes are no longer required, so remove to them - # as the data they point to might disappear when this object - # is deallocated + # weights are no longer required, so remove to them as the + # data they point to might disappear model.contents.param.nr_weight = 0 - # XXX workaround for bug in ctypes 0.9.9.6 - #model.contents.param.weight = None - #model.contents.param.weight_label = None model.contents.param.weight = c_double_null_ptr model.contents.param.weight_label = c_int_null_ptr Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-13 14:45:21 UTC (rev 2099) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-14 00:06:15 UTC (rev 2100) @@ -66,7 +66,7 @@ error and the squared correlation coefficient. """ - problem = self._create_problem(dataset) + problem = dataset.create_svm_problem() target = N.empty((len(dataset.data),), dtype=N.float64) tp = cast(target.ctypes.data, POINTER(c_double)) libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-13 14:45:21 UTC (rev 2099) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-14 00:06:15 UTC (rev 2100) @@ -82,7 +82,7 @@ RBFKernel(gamma), SigmoidKernel(gamma, coef0) ] - y = N.random.randn(20) + y = N.random.randn(10) x = N.random.randn(len(y), 10) origdata = LibSvmRegressionDataSet(zip(y, x)) @@ -94,8 +94,24 @@ expt_grammat[i, j] = kernel(xi, xj, N.dot) # get a new dataset containing the precomputed data pcdata = origdata.precompute(kernel) - actual_grammat = pcdata.grammat[:,1:-1]['value'] - assert_array_almost_equal(actual_grammat, expt_grammat) + for i, row in enumerate(pcdata.grammat): + valuerow = row[1:-1]['value'] + assert_array_almost_equal(valuerow, expt_grammat[i]) + def check_combine(self): + kernel = LinearKernel() + + y1 = N.random.randn(2) + x1 = N.random.randn(len(y1), 2) + origdata = LibSvmRegressionDataSet(zip(y1, x1)) + pcdata = origdata.precompute(kernel) + + y2 = N.random.randn(1) + x2 = N.random.randn(len(y2), x1.shape[1]) + moredata = LibSvmRegressionDataSet(zip(y2, x2)) + + #pcdata.combine(moredata) + #pcdata.copy_and_extend(moredata) + if __name__ == '__main__': NumpyTest().run() From scipy-svn at scipy.org Fri Jul 14 05:05:35 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 14 Jul 2006 04:05:35 -0500 (CDT) Subject: [Scipy-svn] r2101 - trunk/Lib/linalg Message-ID: <20060714090535.A3EEF39C0EC@new.scipy.org> Author: cookedm Date: 2006-07-14 04:05:30 -0500 (Fri, 14 Jul 2006) New Revision: 2101 Modified: trunk/Lib/linalg/basic.py trunk/Lib/linalg/blas.py trunk/Lib/linalg/decomp.py trunk/Lib/linalg/flinalg.py trunk/Lib/linalg/iterative.py trunk/Lib/linalg/lapack.py Log: scipy.linalg: redo imports so python tests/test_* would work Modified: trunk/Lib/linalg/basic.py =================================================================== --- trunk/Lib/linalg/basic.py 2006-07-14 00:06:15 UTC (rev 2100) +++ trunk/Lib/linalg/basic.py 2006-07-14 09:05:30 UTC (rev 2101) @@ -20,7 +20,7 @@ import numpy from numpy import asarray_chkfinite, outer, concatenate, reshape, single from numpy import matrix as Matrix -import calc_lwork +from scipy.linalg import calc_lwork class LinAlgError(Exception): pass Modified: trunk/Lib/linalg/blas.py =================================================================== --- trunk/Lib/linalg/blas.py 2006-07-14 00:06:15 UTC (rev 2100) +++ trunk/Lib/linalg/blas.py 2006-07-14 09:05:30 UTC (rev 2101) @@ -11,8 +11,8 @@ # replaced with the available one. If none is available, exception # is raised at the first attempt to use the resources. -import fblas -import cblas +from scipy.linalg import fblas +from scipy.linalg import cblas if hasattr(cblas,'empty_module'): cblas = fblas elif hasattr(fblas,'empty_module'): Modified: trunk/Lib/linalg/decomp.py =================================================================== --- trunk/Lib/linalg/decomp.py 2006-07-14 00:06:15 UTC (rev 2100) +++ trunk/Lib/linalg/decomp.py 2006-07-14 09:05:30 UTC (rev 2101) @@ -20,7 +20,7 @@ from lapack import get_lapack_funcs from blas import get_blas_funcs from flinalg import get_flinalg_funcs -import calc_lwork +from scipy.linalg import calc_lwork import numpy from numpy import array, asarray_chkfinite, asarray, diag, zeros, ones, \ single, isfinite, inexact Modified: trunk/Lib/linalg/flinalg.py =================================================================== --- trunk/Lib/linalg/flinalg.py 2006-07-14 00:06:15 UTC (rev 2100) +++ trunk/Lib/linalg/flinalg.py 2006-07-14 09:05:30 UTC (rev 2101) @@ -12,9 +12,10 @@ try: import _flinalg except ImportError: - from numpy.distutils.misc_util import PostponedException - _flinalg = PostponedException() - print _flinalg.__doc__ + _flinalg = None +# from numpy.distutils.misc_util import PostponedException +# _flinalg = PostponedException() +# print _flinalg.__doc__ has_column_major_storage = lambda a:0 def has_column_major_storage(arr): Modified: trunk/Lib/linalg/iterative.py =================================================================== --- trunk/Lib/linalg/iterative.py 2006-07-14 00:06:15 UTC (rev 2100) +++ trunk/Lib/linalg/iterative.py 2006-07-14 09:05:30 UTC (rev 2101) @@ -10,7 +10,7 @@ __all__ = ['bicg','bicgstab','cg','cgs','gmres','qmr'] -import _iterative +from scipy.linalg import _iterative import numpy as sb try: Modified: trunk/Lib/linalg/lapack.py =================================================================== --- trunk/Lib/linalg/lapack.py 2006-07-14 00:06:15 UTC (rev 2100) +++ trunk/Lib/linalg/lapack.py 2006-07-14 09:05:30 UTC (rev 2101) @@ -14,8 +14,8 @@ import numpy -import flapack -import clapack +from scipy.linalg import flapack +from scipy.linalg import clapack _use_force_clapack = 1 if hasattr(clapack,'empty_module'): clapack = flapack From scipy-svn at scipy.org Fri Jul 14 08:59:28 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 14 Jul 2006 07:59:28 -0500 (CDT) Subject: [Scipy-svn] r2102 - trunk/Lib/sandbox/svm Message-ID: <20060714125928.B5D6F39C105@new.scipy.org> Author: fullung Date: 2006-07-14 07:59:13 -0500 (Fri, 14 Jul 2006) New Revision: 2102 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/kernel.py trunk/Lib/sandbox/svm/libsvm.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/regression.py Log: Reformat code to conform to PEP 8. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-14 09:05:30 UTC (rev 2101) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-14 12:59:13 UTC (rev 2102) @@ -1,3 +1,9 @@ +from ctypes import cast, POINTER, c_int, c_double +import numpy as N + +from model import LibSvmModel +import libsvm + __all__ = [ 'LibSvmCClassificationModel', 'LibSvmNuClassificationModel', @@ -3,10 +9,4 @@ ] -import numpy as N -from ctypes import cast, POINTER, c_int, c_double - -from model import LibSvmModel -import libsvm - class LibSvmClassificationResults: def __init__(self, model, dataset): Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-14 09:05:30 UTC (rev 2101) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-14 12:59:13 UTC (rev 2102) @@ -1,3 +1,8 @@ +from ctypes import c_double, POINTER, cast +import numpy as N + +import libsvm + __all__ = [ 'LibSvmRegressionDataSet', 'LibSvmClassificationDataSet', @@ -5,11 +10,6 @@ 'LibSvmTestDataSet' ] -from ctypes import c_double, POINTER, cast -import numpy as N - -import libsvm - class LibSvmDataSet: def __init__(self, data): self.data = data Modified: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-07-14 09:05:30 UTC (rev 2101) +++ trunk/Lib/sandbox/svm/kernel.py 2006-07-14 12:59:13 UTC (rev 2102) @@ -1,3 +1,7 @@ +import numpy as N + +import libsvm + __all__ = [ 'LinearKernel', 'PolynomialKernel', @@ -7,10 +11,6 @@ 'PrecomputedKernel' ] -import numpy as N - -import libsvm - class LinearKernel: def __init__(self): self.kernel_type = libsvm.LINEAR Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-07-14 09:05:30 UTC (rev 2101) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-07-14 12:59:13 UTC (rev 2102) @@ -1,16 +1,4 @@ -__all__ = [ - 'svm_node_dtype', - 'C_SVC', - 'NU_SVC', - 'ONE_CLASS', - 'EPSILON_SVR', - 'NU_SVR', - 'LINEAR', - 'POLY', - 'RBF', - 'SIGMOID', - 'PRECOMPUTED' - ] +import inspect import numpy as N from ctypes import c_int, c_double, POINTER, Structure, c_char_p @@ -130,7 +118,6 @@ (None, [POINTER(svm_model)]) } -import inspect for f, (restype, argtypes) in libsvm_api.iteritems(): func = getattr(_libsvm, f) func.restype = restype Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-14 09:05:30 UTC (rev 2101) +++ trunk/Lib/sandbox/svm/model.py 2006-07-14 12:59:13 UTC (rev 2102) @@ -1,3 +1,8 @@ +from ctypes import * + +from kernel import * +import libsvm + __all__ = [ 'LibSvmModel' ] @@ -2,11 +7,8 @@ -from ctypes import * c_double_null_ptr = POINTER(c_double)() c_int_null_ptr = POINTER(c_int)() -from kernel import * -import libsvm - class LibSvmModel: - def __init__(self, kernel, tolerance=0.001, shrinking=True, cache_size=40): + def __init__(self, kernel, + tolerance=0.001, shrinking=True, cache_size=40): """ Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-14 09:05:30 UTC (rev 2101) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-14 12:59:13 UTC (rev 2102) @@ -1,12 +1,12 @@ -__all__ = [ - 'LibSvmOneClassModel' - ] - from ctypes import cast, POINTER, byref, c_double from model import LibSvmModel import libsvm +__all__ = [ + 'LibSvmOneClassModel' + ] + class LibSvmOneClassResults: def __init__(self, model, dataset): self.model = model Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-14 09:05:30 UTC (rev 2101) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-14 12:59:13 UTC (rev 2102) @@ -1,3 +1,9 @@ +from ctypes import cast, POINTER, c_double +import numpy as N + +from model import LibSvmModel +import libsvm + __all__ = [ 'LibSvmEpsilonRegressionModel', 'LibSvmNuRegressionModel' @@ -3,10 +9,4 @@ ] -import numpy as N -from ctypes import cast, POINTER, c_double - -from model import LibSvmModel -import libsvm - # XXX document why get_svr_probability could be useful From scipy-svn at scipy.org Fri Jul 14 17:22:10 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 14 Jul 2006 16:22:10 -0500 (CDT) Subject: [Scipy-svn] r2103 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060714212210.0865339C105@new.scipy.org> Author: fullung Date: 2006-07-14 16:21:55 -0500 (Fri, 14 Jul 2006) New Revision: 2103 Modified: trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_dataset.py trunk/Lib/sandbox/svm/tests/test_kernel.py trunk/Lib/sandbox/svm/tests/test_libsvm.py trunk/Lib/sandbox/svm/tests/test_oneclass.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Dataset for dealing with precomputed kernels. Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-14 12:59:13 UTC (rev 2102) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-14 21:21:55 UTC (rev 2103) @@ -44,34 +44,31 @@ self.iddatamap = {} - # Create Gram matrix as a list of vectors that have extra - # entries for id and end of record marker. + # Create Gram matrix as a list of vectors which an extra entry + # for the id field. n = len(origdata) - grammat = [N.empty((n+2,), dtype=libsvm.svm_node_dtype) + grammat = [N.empty((n+1,), dtype=libsvm.svm_node_dtype) for i in range(n)] self.grammat = grammat # Calculate Gram matrix. Refer to Kernel::kernel_precomputed # in svm.cpp to see how this precomputed setup works. - for i, (y1, x1) in enumerate(origdata): + for i, (yi, xi) in enumerate(origdata): id = i + 1 - # XXX possible numpy bug - #grammat[i][[0,-1]] = (0, id), (-1, 0.0) grammat[i][0] = 0, id - grammat[i][-1] = -1, 0.0 - for j, (y2, x2) in enumerate(origdata[i:]): + # Map id to original vector so that we can find it again + # after the model has been trained. libsvm essentially + # provides the ids of the support vectors. + self.iddatamap[id] = xi + for j, (yj, xj) in enumerate(origdata[i:]): # Gram matrix is symmetric, so calculate dot product # once and store it in both required locations - z = kernel(x1, x2, svm_node_dot) + z = self.kernel(xi, xj, svm_node_dot) # fix index so we assign to the right place j += i - grammat[i][j+1] = 0, z - grammat[j][i+1] = 0, z - # Map id to original vector so that we can find it again - # after the model has been trained. libsvm essentially - # provides the ids of the support vectors. - self.iddatamap[id] = x1 - + grammat[i][j + 1] = 0, z + grammat[j][i + 1] = 0, z + def getdata(self): return zip(map(lambda x: x[0], self.origdata), self.grammat) data = property(getdata) @@ -89,30 +86,45 @@ Combine this dataset with another dataset by extending the Gram matrix with the new inner products into a new matrix. """ - n = len(self.origdata) + len(dataset.data) + n = len(self.origdata) + len(dataset.data) + 1 newgrammat = [] # copy original Gram matrix for i in range(len(self.origdata)): - row = N.empty((n,), dtype=libsvm.svm_node_dtype) - row[:-1] = self.grammat[i] - newgrammat.append(row) + newrow = N.zeros((n,), dtype=libsvm.svm_node_dtype) + oldrow = self.grammat[i] + newrow[:len(oldrow)] = oldrow + newgrammat.append(newrow) - # copy id->vector map - newiddatamap = dict(self.iddatamap.items()) - # prepare Gram matrix for new data for i in range(len(dataset.data)): - id = i + len(self.origdata) + 1 - row = N.empty((n,), dtype=libsvm.svm_node_dtype) - row[[0,-1]] = (0, id), (-1, 0.0) + row = N.zeros((n,), dtype=libsvm.svm_node_dtype) newgrammat.append(row) - newiddatamap[id] = dataset.data[i][1] + newiddatamap = dict(self.iddatamap.items()) + m = len(self.origdata) + for i, (yi, xi) in enumerate(dataset.data): + i += m + for j, (yj, xj) in enumerate(self.origdata): + z = self.kernel(xi, xj, svm_node_dot) + newgrammat[i][j + 1] = 0, z + newgrammat[j][i + 1] = 0, z + for i, (yi, xi) in enumerate(dataset.data): + k = m + i + id = k + 1 + newgrammat[k][0] = 0, id + newiddatamap[id] = xi + for j, (yj, xj) in enumerate(dataset.data[i:]): + z = self.kernel(xi, xj, svm_node_dot) + j += k + newgrammat[k][j + 1] = 0, z + newgrammat[j][k + 1] = 0, z + newdataset = self.__class__(self.kernel) newdataset.origdata = self.origdata + dataset.data newdataset.iddatamap = newiddatamap newdataset.grammat = newgrammat + return newdataset class LibSvmRegressionDataSet(LibSvmDataSet): def __init__(self, origdata): Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-14 12:59:13 UTC (rev 2102) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-14 21:21:55 UTC (rev 2103) @@ -1,13 +1,11 @@ +from numpy.testing import * import numpy as N -from numpy.testing import * set_local_path('../..') - from svm.classification import * from svm.dataset import LibSvmClassificationDataSet from svm.dataset import LibSvmTestDataSet from svm.kernel import * - restore_path() class test_classification(NumpyTestCase): Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-14 12:59:13 UTC (rev 2102) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-14 21:21:55 UTC (rev 2103) @@ -1,13 +1,11 @@ +from numpy.testing import * import numpy as N -from numpy.testing import * set_local_path('../..') - from svm.dataset import * from svm.kernel import * from svm.dataset import convert_to_svm_node, svm_node_dot from svm.libsvm import svm_node_dtype - restore_path() class test_dataset(NumpyTestCase): @@ -95,23 +93,30 @@ # get a new dataset containing the precomputed data pcdata = origdata.precompute(kernel) for i, row in enumerate(pcdata.grammat): - valuerow = row[1:-1]['value'] + valuerow = row[1:]['value'] assert_array_almost_equal(valuerow, expt_grammat[i]) def check_combine(self): kernel = LinearKernel() - y1 = N.random.randn(2) - x1 = N.random.randn(len(y1), 2) + y1 = N.random.randn(10) + x1 = N.random.randn(len(y1), 10) origdata = LibSvmRegressionDataSet(zip(y1, x1)) pcdata = origdata.precompute(kernel) - y2 = N.random.randn(1) + y2 = N.random.randn(5) x2 = N.random.randn(len(y2), x1.shape[1]) moredata = LibSvmRegressionDataSet(zip(y2, x2)) + morepcdata = pcdata.combine(moredata) - #pcdata.combine(moredata) - #pcdata.copy_and_extend(moredata) + expt_grammat = N.empty((len(y1) + len(y2),)*2) + x = N.vstack([x1,x2]) + for i, xi in enumerate(x): + for j, xj in enumerate(x): + expt_grammat[i, j] = kernel(xi, xj, N.dot) + for i, row in enumerate(morepcdata.grammat): + valuerow = row[1:]['value'] + assert_array_almost_equal(valuerow, expt_grammat[i]) if __name__ == '__main__': NumpyTest().run() Modified: trunk/Lib/sandbox/svm/tests/test_kernel.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_kernel.py 2006-07-14 12:59:13 UTC (rev 2102) +++ trunk/Lib/sandbox/svm/tests/test_kernel.py 2006-07-14 21:21:55 UTC (rev 2103) @@ -1,10 +1,8 @@ +from numpy.testing import * import numpy as N -from numpy.testing import * set_local_path('../..') - from svm.kernel import * - restore_path() class test_kernel(NumpyTestCase): Modified: trunk/Lib/sandbox/svm/tests/test_libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_libsvm.py 2006-07-14 12:59:13 UTC (rev 2102) +++ trunk/Lib/sandbox/svm/tests/test_libsvm.py 2006-07-14 21:21:55 UTC (rev 2103) @@ -1,10 +1,8 @@ +from numpy.testing import * import numpy as N -from numpy.testing import * set_local_path('../..') - import svm.libsvm as libsvm - restore_path() class test_libsvm(NumpyTestCase): Modified: trunk/Lib/sandbox/svm/tests/test_oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-14 12:59:13 UTC (rev 2102) +++ trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-14 21:21:55 UTC (rev 2103) @@ -1,13 +1,11 @@ +from numpy.testing import * import numpy as N -from numpy.testing import * set_local_path('../..') - from svm.oneclass import * from svm.dataset import LibSvmOneClassDataSet from svm.dataset import LibSvmTestDataSet from svm.kernel import * - restore_path() class test_oneclass(NumpyTestCase): Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-14 12:59:13 UTC (rev 2102) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-14 21:21:55 UTC (rev 2103) @@ -1,13 +1,11 @@ +from numpy.testing import * import numpy as N -from numpy.testing import * set_local_path('../..') - from svm.regression import * from svm.dataset import LibSvmRegressionDataSet from svm.dataset import LibSvmTestDataSet from svm.kernel import * - restore_path() class test_regression(NumpyTestCase): From scipy-svn at scipy.org Fri Jul 14 17:55:52 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 14 Jul 2006 16:55:52 -0500 (CDT) Subject: [Scipy-svn] r2104 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060714215552.7FD4E39C105@new.scipy.org> Author: fullung Date: 2006-07-14 16:55:34 -0500 (Fri, 14 Jul 2006) New Revision: 2104 Added: trunk/Lib/sandbox/svm/tests/test_precomputed.py Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/libsvm.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_all.py Log: Precomputed model training. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-14 21:21:55 UTC (rev 2103) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-14 21:55:34 UTC (rev 2104) @@ -130,7 +130,7 @@ This function returns the percentage of data that was classified correctly over all the experiments. """ - problem = dataset.create_svm_problem() + problem = dataset._create_svm_problem() target = N.empty((len(dataset.data),), dtype=N.float64) tp = cast(target.ctypes.data, POINTER(c_double)) libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-14 21:21:55 UTC (rev 2103) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-14 21:55:34 UTC (rev 2104) @@ -1,4 +1,3 @@ -from ctypes import c_double, POINTER, cast import numpy as N import libsvm @@ -24,18 +23,13 @@ def precompute(self, kernel): return LibSvmPrecomputedDataSet(kernel, self.data) - def create_svm_problem(self): - problem = libsvm.svm_problem() - problem.l = len(self.data) - y = (c_double*problem.l)() - x = (POINTER(libsvm.svm_node)*problem.l)() - for i, (yi, xi) in enumerate(self.data): - y[i] = yi - x[i] = cast(xi.ctypes.data, POINTER(libsvm.svm_node)) - problem.x = x - problem.y = y - return problem + def _create_svm_problem(self): + return libsvm.create_svm_problem(self.data) + def _update_svm_parameter(self, param): + # XXX we can handle gamma=None here + pass + class LibSvmPrecomputedDataSet: def __init__(self, kernel, origdata=None): self.kernel = kernel @@ -126,6 +120,12 @@ newdataset.grammat = newgrammat return newdataset + def _create_svm_problem(self): + return libsvm.create_svm_problem(self.data) + + def _update_svm_parameter(self, param): + param.kernel_type = libsvm.PRECOMPUTED + class LibSvmRegressionDataSet(LibSvmDataSet): def __init__(self, origdata): data = map(lambda x: (x[0], convert_to_svm_node(x[1])), origdata) Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-07-14 21:21:55 UTC (rev 2103) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-07-14 21:55:34 UTC (rev 2104) @@ -1,7 +1,7 @@ import inspect +from ctypes import * import numpy as N -from ctypes import c_int, c_double, POINTER, Structure, c_char_p _libsvm = N.ctypes_load_library('libsvm_', __file__) @@ -124,6 +124,18 @@ func.argtypes = argtypes inspect.currentframe().f_locals[f] = func +def create_svm_problem(data): + problem = svm_problem() + problem.l = len(data) + y = (c_double*problem.l)() + x = (POINTER(svm_node)*problem.l)() + for i, (yi, xi) in enumerate(data): + y[i] = yi + x[i] = cast(xi.ctypes.data, POINTER(svm_node)) + problem.x = x + problem.y = y + return problem + __all__ = [ 'svm_node_dtype', 'C_SVC', Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-14 21:21:55 UTC (rev 2103) +++ trunk/Lib/sandbox/svm/model.py 2006-07-14 21:55:34 UTC (rev 2104) @@ -1,4 +1,4 @@ -from ctypes import * +from ctypes import POINTER, c_double, c_int from kernel import * import libsvm @@ -47,9 +47,10 @@ self.param = param def fit(self, dataset): - problem = dataset.create_svm_problem() + problem = dataset._create_svm_problem() + dataset._update_svm_parameter(self.param) + self._check_problem_param(problem, self.param) - self._check_problem_param(problem, self.param) model = libsvm.svm_train(problem, self.param) # weights are no longer required, so remove to them as the @@ -58,8 +59,12 @@ model.contents.param.weight = c_double_null_ptr model.contents.param.weight_label = c_int_null_ptr - # results keep a refence to the dataset because the svm_model - # refers to some of its vectors as the support vectors + # results keep a reference to the dataset because the + # svm_model refers to some of its vectors as the support + # vectors + # XXX we can hide an id in the end of record marker so that we + # can figure out which support vectors to keep references to + # even when not using precomputed kernels return self.Results(model, dataset) def _check_problem_param(self, problem, param): Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-14 21:21:55 UTC (rev 2103) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-14 21:55:34 UTC (rev 2104) @@ -66,7 +66,7 @@ error and the squared correlation coefficient. """ - problem = dataset.create_svm_problem() + problem = dataset._create_svm_problem() target = N.empty((len(dataset.data),), dtype=N.float64) tp = cast(target.ctypes.data, POINTER(c_double)) libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) Modified: trunk/Lib/sandbox/svm/tests/test_all.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-14 21:21:55 UTC (rev 2103) +++ trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-14 21:55:34 UTC (rev 2104) @@ -3,6 +3,7 @@ from test_dataset import * from test_oneclass import * from test_libsvm import * +from test_precomputed import * if __name__ == '__main__': NumpyTest().run() Added: trunk/Lib/sandbox/svm/tests/test_precomputed.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-14 21:21:55 UTC (rev 2103) +++ trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-14 21:55:34 UTC (rev 2104) @@ -0,0 +1,45 @@ +from numpy.testing import * +import numpy as N + +set_local_path('../..') +from svm.regression import * +from svm.dataset import * +from svm.kernel import LinearKernel +restore_path() + +class test_precomputed(NumpyTestCase): + def check_precomputed(self): + kernel = LinearKernel() + + # this dataset remains constant + y1 = N.random.randn(50) + x1 = N.random.randn(len(y1), 10) + data1 = LibSvmRegressionDataSet(zip(y1, x1)) + pcdata1 = data1.precompute(kernel) + + # in a typical problem, this dataset would be smaller than the + # part that remains constant and would differ for each model + y2 = N.random.randn(5) + x2 = N.random.randn(len(y2), x1.shape[1]) + data2 = LibSvmRegressionDataSet(zip(y2, x2)) + + pcdata12 = pcdata1.combine(data2) + model = LibSvmEpsilonRegressionModel(kernel) + results = model.fit(pcdata12) + + # reference model, calculated without involving the + # precomputed Gram matrix + refy = N.concatenate([y1, y2]) + refx = N.vstack([x1, x2]) + refdata = LibSvmRegressionDataSet(zip(refy, refx)) + model = LibSvmEpsilonRegressionModel(kernel) + refresults = model.fit(refdata) + + self.assertAlmostEqual(results.rho, refresults.rho) + assert_array_almost_equal(results.sv_coef, refresults.sv_coef) + + # XXX sigmas don't match yet. need to find out why. + #self.assertAlmostEqual(results.sigma, refresults.sigma) + +if __name__ == '__main__': + NumpyTest().run() From scipy-svn at scipy.org Fri Jul 14 18:26:18 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 14 Jul 2006 17:26:18 -0500 (CDT) Subject: [Scipy-svn] r2105 - trunk/Lib/misc Message-ID: <20060714222618.7409B39C105@new.scipy.org> Author: cookedm Date: 2006-07-14 17:26:16 -0500 (Fri, 14 Jul 2006) New Revision: 2105 Modified: trunk/Lib/misc/pilutil.py Log: Handle 16-bit images from PIL Modified: trunk/Lib/misc/pilutil.py =================================================================== --- trunk/Lib/misc/pilutil.py 2006-07-14 21:55:34 UTC (rev 2104) +++ trunk/Lib/misc/pilutil.py 2006-07-14 22:26:16 UTC (rev 2105) @@ -69,9 +69,10 @@ type = uint8 if mode == 'F': type = numpy.float32 - if mode == 'I': + elif mode == 'I': type = numpy.uint32 - type = 'I' + elif mode == 'I;16': + type = numpy.uint16 arr = numpy.fromstring(str,type) shape = list(im.size) shape.reverse() From scipy-svn at scipy.org Fri Jul 14 18:45:11 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 14 Jul 2006 17:45:11 -0500 (CDT) Subject: [Scipy-svn] r2106 - trunk/Lib/sandbox/svm Message-ID: <20060714224511.0787A39C105@new.scipy.org> Author: fullung Date: 2006-07-14 17:45:02 -0500 (Fri, 14 Jul 2006) New Revision: 2106 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/kernel.py trunk/Lib/sandbox/svm/regression.py Log: More cleanups to conform to PEP 8. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-14 22:26:16 UTC (rev 2105) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-14 22:45:02 UTC (rev 2106) @@ -61,7 +61,7 @@ count = 0 d = {} for i in range(len(self.labels)): - for j in range(i+1, len(self.labels)): + for j in range(i + 1, len(self.labels)): d[self.labels[i], self.labels[j]] = v[count] d[self.labels[j], self.labels[i]] = -v[count] count += 1 Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-14 22:26:16 UTC (rev 2105) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-14 22:45:02 UTC (rev 2106) @@ -41,7 +41,7 @@ # Create Gram matrix as a list of vectors which an extra entry # for the id field. n = len(origdata) - grammat = [N.empty((n+1,), dtype=libsvm.svm_node_dtype) + grammat = [N.empty((n + 1,), dtype=libsvm.svm_node_dtype) for i in range(n)] self.grammat = grammat @@ -150,7 +150,7 @@ self.data = map(lambda x: convert_to_svm_node(x), origdata) def convert_to_svm_node(x): - y = N.empty(len(x)+1, dtype=libsvm.svm_node_dtype) + y = N.empty(len(x) + 1, dtype=libsvm.svm_node_dtype) y[-1] = -1, 0. if isinstance(x, dict): x = x.items() @@ -158,7 +158,7 @@ x.sort(cmp=lambda x,y: cmp(x[0],y[0])) y[:-1] = x else: - y['index'][:-1] = N.arange(1,len(x)+1) + y['index'][:-1] = N.arange(1,len(x) + 1) y['value'][:-1] = x assert N.alltrue(y[:-1]['index'] >= 1), \ 'indexes must be positive' @@ -176,5 +176,5 @@ for j in indexes: if j in xidx and j in yidx: # dot if index is present in both vectors - z += x['value'][xidx[j]]*y['value'][yidx[j]] + z += x['value'][xidx[j]] * y['value'][yidx[j]] return z Modified: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-07-14 22:26:16 UTC (rev 2105) +++ trunk/Lib/sandbox/svm/kernel.py 2006-07-14 22:45:02 UTC (rev 2106) @@ -26,7 +26,7 @@ self.coef0 = coef0 def __call__(self, x, y, dot): - base = self.gamma*dot(x, y) + self.coef0 + base = self.gamma * dot(x, y) + self.coef0 tmp = base ret = 1.0 t = self.degree @@ -42,8 +42,8 @@ self.gamma = gamma def __call__(self, x, y, dot): - z = dot(x, x) + dot(y, y) - 2*dot(x, y) - return N.exp(-self.gamma*z) + z = dot(x, x) + dot(y, y) - 2 * dot(x, y) + return N.exp(-self.gamma * z) class SigmoidKernel: def __init__(self, gamma, coef0): @@ -52,7 +52,7 @@ self.coef0 = coef0 def __call__(self, x, y, dot): - return N.tanh(self.gamma*dot(x, y)+self.coef0) + return N.tanh(self.gamma * dot(x, y) + self.coef0) class CustomKernel: def __init__(self, f): Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-14 22:26:16 UTC (rev 2105) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-14 22:45:02 UTC (rev 2106) @@ -80,14 +80,14 @@ sumvv = sumvv + v * v sumyy = sumyy + y * y sumvy = sumvy + v * y - total_error = total_error + (v-y) * (v-y) + total_error = total_error + (v - y) * (v - y) # mean squared error mse = total_error / len(dataset.data) # squared correlation coefficient l = len(dataset.data) - scc = ((l*sumvy - sumv*sumy) * (l*sumvy - sumv*sumy)) / \ - ((l*sumvv - sumv*sumv) * (l*sumyy - sumy*sumy)) + scc = ((l * sumvy - sumv * sumy) * (l * sumvy - sumv * sumy)) / \ + ((l * sumvv - sumv*sumv) * (l * sumyy - sumy * sumy)) return mse, scc From scipy-svn at scipy.org Mon Jul 17 15:04:52 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 17 Jul 2006 14:04:52 -0500 (CDT) Subject: [Scipy-svn] r2107 - trunk/Lib/optimize/cobyla Message-ID: <20060717190452.ADAF639C02E@new.scipy.org> Author: oliphant Date: 2006-07-17 14:04:48 -0500 (Mon, 17 Jul 2006) New Revision: 2107 Modified: trunk/Lib/optimize/cobyla/cobyla2.f trunk/Lib/optimize/cobyla/trstlp.f Log: More printing to debug cobyla2.f Modified: trunk/Lib/optimize/cobyla/cobyla2.f =================================================================== --- trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-14 22:45:02 UTC (rev 2106) +++ trunk/Lib/optimize/cobyla/cobyla2.f 2006-07-17 19:04:48 UTC (rev 2107) @@ -367,31 +367,8 @@ ISDIRN=IVMC+MP IDXNEW=ISDIRN+N IVMD=IDXNEW+N - IF (IPRINT .EQ. 3) THEN - print *, ' ' - print *, 'BEFORE trstlp:' - PRINT *, ' **DX = ', (DX(I),I=1,N) - PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) - PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL - PRINT *, ' **CON = ', (CON(I),I=1,M) - PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,MP) - PRINT *, ' **W = ', (W(I),I=1,ITOTAL) - print *, ' ' - END IF CALL TRSTLP (N,M,A,CON,RHO,DX,IFULL,IACT,W(IZ),W(IZDOTA), - 1 W(IVMC),W(ISDIRN),W(IDXNEW),W(IVMD)) - IF (IPRINT .EQ. 3) THEN - print *, ' ' - print *, 'AFTER trstlp:' - PRINT *, ' **DX = ', (DX(I),I=1,N) - PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) - PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL - PRINT *, ' **CON = ', (CON(I),I=1,M) - PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,MP) - PRINT *, ' **W = ', (W(I),I=1,ITOTAL) - print *, ' ' - print *, ' ' - END IF + 1 W(IVMC),W(ISDIRN),W(IDXNEW),W(IVMD),IPRINT) IF (IFULL .EQ. 0) THEN TEMP=0.0d0 DO 380 I=1,N Modified: trunk/Lib/optimize/cobyla/trstlp.f =================================================================== --- trunk/Lib/optimize/cobyla/trstlp.f 2006-07-14 22:45:02 UTC (rev 2106) +++ trunk/Lib/optimize/cobyla/trstlp.f 2006-07-17 19:04:48 UTC (rev 2107) @@ -1,6 +1,6 @@ C------------------------------------------------------------------------------ SUBROUTINE TRSTLP (N,M,A,B,RHO,DX,IFULL,IACT,Z,ZDOTA,VMULTC, - 1 SDIRN,DXNEW,VMULTD) + 1 SDIRN,DXNEW,VMULTD,IPRINT) IMPLICIT DOUBLE PRECISION (A-H,O-Z) DIMENSION A(N,*),B(*),DX(*),IACT(*),Z(N,*),ZDOTA(*), 1 VMULTC(*),SDIRN(*),DXNEW(*),VMULTD(*) @@ -43,6 +43,23 @@ C constraint violations by one simultaneously. C + IF (IPRINT .EQ. 3) THEN + print *, ' ' + print *, 'BEFORE trstlp:' + PRINT *, ' **DX = ', (DX(I),I=1,N) + PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) + PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL + PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,M+1) + PRINT *, ' **B = ', (B(I),I=1,M) + PRINT *, ' **Z = ', ((Z(I,K),I=1,N),K=1,N) + PRINT *, ' **ZDOTA = ', (ZDOTA(I),I=1,N) + PRINT *, ' **VMULTC = ', (VMULTC(I),I=1,M+1) + PRINT *, ' **SDIRN = ', (SDIRN(I),I=1,N) + PRINT *, ' **DXNEW = ', (DXNEW(I),I=1,N) + PRINT *, ' **VMULTD = ', (VMULTD(I),I=1,M+1) + PRINT *, ' ' + END IF + ICON=0 NACTX=0 RESOLD=0 @@ -67,6 +84,7 @@ IACT(K)=K 40 VMULTC(K)=RESMAX-B(K) END IF + PRINT *, ' 1. VMULTC = ', (VMULTC(I),I=1,M+1) IF (RESMAX .EQ. 0.0d0) GOTO 480 DO 50 I=1,N 50 SDIRN(I)=0.0d0 @@ -162,9 +180,9 @@ DO 140 I=1,N TEMP=Z(I,K)*DXNEW(I) ZDOTV=ZDOTV+TEMP - 140 ZDVABS=ZDVABS+ABS(TEMP) - ACCA=ZDVABS+0.1d0*ABS(ZDOTV) - ACCB=ZDVABS+0.2d0*ABS(ZDOTV) + 140 ZDVABS=ZDVABS+DABS(TEMP) + ACCA=ZDVABS+0.1d0*DABS(ZDOTV) + ACCB=ZDVABS+0.2d0*DABS(ZDOTV) IF (ZDVABS .LT. ACCA .AND. ACCA .LT. ACCB) THEN TEMP=ZDOTV/ZDOTA(K) IF (TEMP .GT. 0.0d0 .AND. IACT(K) .LE. M) THEN @@ -185,6 +203,7 @@ END IF K=K-1 IF (K .GT. 0) GOTO 130 + PRINT *, ' 1. VMULTD = ', (VMULTD(I),I=1,M+1) IF (RATIO .LT. 0.0d0) GOTO 490 C C Revise the Lagrange multipliers and reorder the active constraints so @@ -193,6 +212,7 @@ C DO 160 K=1,NACT 160 VMULTC(K)=DMAX1(0.0d0,VMULTC(K)-RATIO*VMULTD(K)) + PRINT *, ' 2. VMULTC = ', (VMULTC(I),I=1,M+1) IF (ICON .LT. NACT) THEN ISAVE=IACT(ICON) VSAVE=VMULTC(ICON) @@ -369,6 +389,7 @@ GOTO 390 END IF IF (MCON .GT. M) VMULTD(NACT)=DMAX1(0.0d0,VMULTD(NACT)) + PRINT *, ' 2. VMULTD = ', (VMULTD(I),I=1,M+1) C C Complete VMULTC by finding the new constraint residuals. C @@ -379,16 +400,17 @@ DO 440 K=KL,MCON KK=IACT(K) SUM=RESMAX-B(KK) - SUMABS=RESMAX+ABS(B(KK)) + SUMABS=RESMAX+DABS(B(KK)) DO 430 I=1,N TEMP=A(I,KK)*DXNEW(I) SUM=SUM+TEMP - 430 SUMABS=SUMABS+ABS(TEMP) - ACCA=SUMABS+0.1*ABS(SUM) - ACCB=SUMABS+0.2*ABS(SUM) + 430 SUMABS=SUMABS+DABS(TEMP) + ACCA=SUMABS+0.1*DABS(SUM) + ACCB=SUMABS+0.2*DABS(SUM) IF (SUMABS .GE. ACCA .OR. ACCA .GE. ACCB) SUM=0.0 440 VMULTD(K)=SUM END IF + PRINT *, ' 3. VMULTD = ', (VMULTD(I),I=1,M+1) C C Calculate the fraction of the step from DX to DXNEW that will be taken. C @@ -411,6 +433,7 @@ 460 DX(I)=TEMP*DX(I)+RATIO*DXNEW(I) DO 470 K=1,MCON 470 VMULTC(K)=DMAX1(0.0d0,TEMP*VMULTC(K)+RATIO*VMULTD(K)) + PRINT *, ' 3. VMULTC = ', (VMULTC(I),I=1,M+1) IF (MCON .EQ. M) RESMAX=RESOLD+RATIO*(RESMAX-RESOLD) C C If the full step is not acceptable then begin another iteration. @@ -429,7 +452,24 @@ C 490 IF (MCON .EQ. M) GOTO 480 IFULL=0 - 500 RETURN + 500 CONTINUE + IF (IPRINT .EQ. 3) THEN + print *, ' ' + print *, 'AFTER trstlp:' + PRINT *, ' **DX = ', (DX(I),I=1,N) + PRINT *, ' **IACT = ', (IACT(I),I=1,M+1) + PRINT *, 'M,N,RHO,IFULL =', M, N, RHO, IFULL + PRINT *, ' **A = ', ((A(I,K),I=1,N),K=1,M+1) + PRINT *, ' **B = ', (B(I),I=1,M) + PRINT *, ' **Z = ', ((Z(I,K),I=1,N),K=1,N) + PRINT *, ' **ZDOTA = ', (ZDOTA(I),I=1,N) + PRINT *, ' **VMULTC = ', (VMULTC(I),I=1,M+1) + PRINT *, ' **SDIRN = ', (SDIRN(I),I=1,N) + PRINT *, ' **DXNEW = ', (DXNEW(I),I=1,N) + PRINT *, ' **VMULTD = ', (VMULTD(I),I=1,M+1) + PRINT *, ' ' + END IF +C 500 RETURN END subroutine s360_380(DXNEW,DX,STEP,SDIRN,N,M,MCON,RESMAX, From scipy-svn at scipy.org Mon Jul 17 16:22:10 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 17 Jul 2006 15:22:10 -0500 (CDT) Subject: [Scipy-svn] r2108 - trunk/Lib/optimize/cobyla Message-ID: <20060717202210.2D3E139C02E@new.scipy.org> Author: oliphant Date: 2006-07-17 15:22:07 -0500 (Mon, 17 Jul 2006) New Revision: 2108 Modified: trunk/Lib/optimize/cobyla/trstlp.f Log: more cobyla/trstlp.f changes. Modified: trunk/Lib/optimize/cobyla/trstlp.f =================================================================== --- trunk/Lib/optimize/cobyla/trstlp.f 2006-07-17 19:04:48 UTC (rev 2107) +++ trunk/Lib/optimize/cobyla/trstlp.f 2006-07-17 20:22:07 UTC (rev 2108) @@ -134,15 +134,15 @@ DO 110 I=1,N TEMP=Z(I,K)*DXNEW(I) SP=SP+TEMP - 110 SPABS=SPABS+ABS(TEMP) - ACCA=SPABS+0.1d0*ABS(SP) - ACCB=SPABS+0.2d0*ABS(SP) + 110 SPABS=SPABS+DABS(TEMP) + ACCA=SPABS+0.1d0*DABS(SP) + ACCB=SPABS+0.2d0*DABS(SP) IF (SPABS .GE. ACCA .OR. ACCA .GE. ACCB) SP=0.0d0 IF (TOT .EQ. 0.0d0) THEN TOT=SP ELSE KP=K+1 - TEMP=SQRT(SP*SP+TOT*TOT) + TEMP=DSQRT(SP*SP+TOT*TOT) ALPHA=SP/TEMP BETA=TOT/TEMP TOT=TEMP @@ -158,6 +158,9 @@ C Add the new constraint if this can be done without a deletion from the C active set. C + IF (IPRINT .EQ. 3) THEN + PRINT *, 'TOT, NACT, ICON = ', TOT, NACT, ICON + END IF IF (TOT .NE. 0.0d0) THEN NACT=NACT+1 ZDOTA(NACT)=TOT From scipy-svn at scipy.org Mon Jul 17 17:51:17 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 17 Jul 2006 16:51:17 -0500 (CDT) Subject: [Scipy-svn] r2109 - trunk/Lib/optimize/cobyla Message-ID: <20060717215117.7869C39C02D@new.scipy.org> Author: oliphant Date: 2006-07-17 16:51:13 -0500 (Mon, 17 Jul 2006) New Revision: 2109 Modified: trunk/Lib/optimize/cobyla/trstlp.f Log: More print statements. Modified: trunk/Lib/optimize/cobyla/trstlp.f =================================================================== --- trunk/Lib/optimize/cobyla/trstlp.f 2006-07-17 20:22:07 UTC (rev 2108) +++ trunk/Lib/optimize/cobyla/trstlp.f 2006-07-17 21:51:13 UTC (rev 2109) @@ -2,6 +2,7 @@ SUBROUTINE TRSTLP (N,M,A,B,RHO,DX,IFULL,IACT,Z,ZDOTA,VMULTC, 1 SDIRN,DXNEW,VMULTD,IPRINT) IMPLICIT DOUBLE PRECISION (A-H,O-Z) + DOUBLE PRECISION TEMP DIMENSION A(N,*),B(*),DX(*),IACT(*),Z(N,*),ZDOTA(*), 1 VMULTC(*),SDIRN(*),DXNEW(*),VMULTD(*) C @@ -104,6 +105,7 @@ DO 80 I=1,N 80 OPTNEW=OPTNEW-DX(I)*A(I,MCON) END IF + PRINT *, ' ICOUNT, OPTNEW, OPTOLD = ', ICOUNT, OPTNEW, OPTOLD IF (ICOUNT .EQ. 0 .OR. OPTNEW .LT. OPTOLD) THEN OPTOLD=OPTNEW NACTX=NACT @@ -126,8 +128,9 @@ KK=IACT(ICON) DO 90 I=1,N 90 DXNEW(I)=A(I,KK) - TOT=0.0d0 + TOT=0.0D0 K=N + print *, ' k, nact, dxnew =', k, nact, (dxnew(i),i=1,n) 100 IF (K .GT. NACT) THEN SP=0.0d0 SPABS=0.0d0 @@ -137,9 +140,11 @@ 110 SPABS=SPABS+DABS(TEMP) ACCA=SPABS+0.1d0*DABS(SP) ACCB=SPABS+0.2d0*DABS(SP) - IF (SPABS .GE. ACCA .OR. ACCA .GE. ACCB) SP=0.0d0 - IF (TOT .EQ. 0.0d0) THEN + print *, ' sp, spabs, acca, accb', sp, spabs, acca, accb + IF ((SPABS .GE. ACCA) .OR. (ACCA .GE. ACCB)) SP=0.0D0 + IF (TOT .EQ. 0.0D0) THEN TOT=SP + print *, ' simple tot = ', tot ELSE KP=K+1 TEMP=DSQRT(SP*SP+TOT*TOT) @@ -150,6 +155,7 @@ TEMP=ALPHA*Z(I,K)+BETA*Z(I,KP) Z(I,KP)=ALPHA*Z(I,KP)-BETA*Z(I,K) 120 Z(I,K)=TEMP + print *, ' k, alpha, beta, tot', k, alpha, beta, tot END IF K=K-1 GOTO 100 @@ -159,7 +165,7 @@ C active set. C IF (IPRINT .EQ. 3) THEN - PRINT *, 'TOT, NACT, ICON = ', TOT, NACT, ICON + PRINT *, '*TOT, NACT, ICON = ', TOT, NACT, ICON END IF IF (TOT .NE. 0.0d0) THEN NACT=NACT+1 @@ -438,6 +444,8 @@ 470 VMULTC(K)=DMAX1(0.0d0,TEMP*VMULTC(K)+RATIO*VMULTD(K)) PRINT *, ' 3. VMULTC = ', (VMULTC(I),I=1,M+1) IF (MCON .EQ. M) RESMAX=RESOLD+RATIO*(RESMAX-RESOLD) + PRINT *, ' RESMAX, MCON, M, ICON = ', + 1 RESMAX, MCON, M, ICON C C If the full step is not acceptable then begin another iteration. C Otherwise switch to stage two or end the calculation. From scipy-svn at scipy.org Mon Jul 17 19:03:22 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 17 Jul 2006 18:03:22 -0500 (CDT) Subject: [Scipy-svn] r2110 - in trunk/Lib/optimize: . cobyla Message-ID: <20060717230322.0CB7639C02D@new.scipy.org> Author: oliphant Date: 2006-07-17 18:03:19 -0500 (Mon, 17 Jul 2006) New Revision: 2110 Modified: trunk/Lib/optimize/cobyla.py trunk/Lib/optimize/cobyla/trstlp.f Log: Fix up comparison. Seems to fix cobyla test. Modified: trunk/Lib/optimize/cobyla/trstlp.f =================================================================== --- trunk/Lib/optimize/cobyla/trstlp.f 2006-07-17 21:51:13 UTC (rev 2109) +++ trunk/Lib/optimize/cobyla/trstlp.f 2006-07-17 23:03:19 UTC (rev 2110) @@ -85,7 +85,9 @@ IACT(K)=K 40 VMULTC(K)=RESMAX-B(K) END IF - PRINT *, ' 1. VMULTC = ', (VMULTC(I),I=1,M+1) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' 1. VMULTC = ', (VMULTC(I),I=1,M+1) + END IF IF (RESMAX .EQ. 0.0d0) GOTO 480 DO 50 I=1,N 50 SDIRN(I)=0.0d0 @@ -105,7 +107,9 @@ DO 80 I=1,N 80 OPTNEW=OPTNEW-DX(I)*A(I,MCON) END IF - PRINT *, ' ICOUNT, OPTNEW, OPTOLD = ', ICOUNT, OPTNEW, OPTOLD + IF (IPRINT .EQ. 3) THEN + PRINT *, ' ICOUNT, OPTNEW, OPTOLD = ', ICOUNT, OPTNEW, OPTOLD + END IF IF (ICOUNT .EQ. 0 .OR. OPTNEW .LT. OPTOLD) THEN OPTOLD=OPTNEW NACTX=NACT @@ -130,7 +134,6 @@ 90 DXNEW(I)=A(I,KK) TOT=0.0D0 K=N - print *, ' k, nact, dxnew =', k, nact, (dxnew(i),i=1,n) 100 IF (K .GT. NACT) THEN SP=0.0d0 SPABS=0.0d0 @@ -140,11 +143,9 @@ 110 SPABS=SPABS+DABS(TEMP) ACCA=SPABS+0.1d0*DABS(SP) ACCB=SPABS+0.2d0*DABS(SP) - print *, ' sp, spabs, acca, accb', sp, spabs, acca, accb IF ((SPABS .GE. ACCA) .OR. (ACCA .GE. ACCB)) SP=0.0D0 IF (TOT .EQ. 0.0D0) THEN TOT=SP - print *, ' simple tot = ', tot ELSE KP=K+1 TEMP=DSQRT(SP*SP+TOT*TOT) @@ -155,7 +156,6 @@ TEMP=ALPHA*Z(I,K)+BETA*Z(I,KP) Z(I,KP)=ALPHA*Z(I,KP)-BETA*Z(I,K) 120 Z(I,K)=TEMP - print *, ' k, alpha, beta, tot', k, alpha, beta, tot END IF K=K-1 GOTO 100 @@ -212,7 +212,9 @@ END IF K=K-1 IF (K .GT. 0) GOTO 130 - PRINT *, ' 1. VMULTD = ', (VMULTD(I),I=1,M+1) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' 1. VMULTD = ', (VMULTD(I),I=1,M+1) + END IF IF (RATIO .LT. 0.0d0) GOTO 490 C C Revise the Lagrange multipliers and reorder the active constraints so @@ -221,7 +223,9 @@ C DO 160 K=1,NACT 160 VMULTC(K)=DMAX1(0.0d0,VMULTC(K)-RATIO*VMULTD(K)) - PRINT *, ' 2. VMULTC = ', (VMULTC(I),I=1,M+1) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' 2. VMULTC = ', (VMULTC(I),I=1,M+1) + END IF IF (ICON .LT. NACT) THEN ISAVE=IACT(ICON) VSAVE=VMULTC(ICON) @@ -398,7 +402,9 @@ GOTO 390 END IF IF (MCON .GT. M) VMULTD(NACT)=DMAX1(0.0d0,VMULTD(NACT)) - PRINT *, ' 2. VMULTD = ', (VMULTD(I),I=1,M+1) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' 2. VMULTD = ', (VMULTD(I),I=1,M+1) + END IF C C Complete VMULTC by finding the new constraint residuals. C @@ -419,14 +425,19 @@ IF (SUMABS .GE. ACCA .OR. ACCA .GE. ACCB) SUM=0.0 440 VMULTD(K)=SUM END IF - PRINT *, ' 3. VMULTD = ', (VMULTD(I),I=1,M+1) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' 3. VMULTD = ', (VMULTD(I),I=1,M+1) + END IF C C Calculate the fraction of the step from DX to DXNEW that will be taken. C RATIO=1.0d0 ICON=0 +C + EPS = 2.2E-16 +C EPS = 0.0D0 DO 450 K=1,MCON - IF (VMULTD(K) .LT. 0.0d0) THEN + IF (VMULTD(K) .LT. -EPS) THEN TEMP=VMULTC(K)/(VMULTC(K)-VMULTD(K)) IF (TEMP .LT. RATIO) THEN RATIO=TEMP @@ -442,10 +453,14 @@ 460 DX(I)=TEMP*DX(I)+RATIO*DXNEW(I) DO 470 K=1,MCON 470 VMULTC(K)=DMAX1(0.0d0,TEMP*VMULTC(K)+RATIO*VMULTD(K)) - PRINT *, ' 3. VMULTC = ', (VMULTC(I),I=1,M+1) + IF (IPRINT .EQ. 3) THEN + PRINT *, ' 3. VMULTC = ', (VMULTC(I),I=1,M+1) + END IF IF (MCON .EQ. M) RESMAX=RESOLD+RATIO*(RESMAX-RESOLD) - PRINT *, ' RESMAX, MCON, M, ICON = ', - 1 RESMAX, MCON, M, ICON + IF (IPRINT .EQ. 3) THEN + PRINT *, ' RESMAX, MCON, M, ICON = ', + 1 RESMAX, MCON, M, ICON + END IF C C If the full step is not acceptable then begin another iteration. C Otherwise switch to stage two or end the calculation. Modified: trunk/Lib/optimize/cobyla.py =================================================================== --- trunk/Lib/optimize/cobyla.py 2006-07-17 21:51:13 UTC (rev 2109) +++ trunk/Lib/optimize/cobyla.py 2006-07-17 23:03:19 UTC (rev 2110) @@ -67,12 +67,9 @@ def calcfc(x, con): f = func(x, *args) k = 0 - print "x = ", x - print "f = ", f for constraints in cons: con[k] = constraints(x, *consargs) k += 1 - print "con = ", con return f xopt = _cobyla.minimize(calcfc, m=m, x=x0, rhobeg=rhobeg, rhoend=rhoend, From scipy-svn at scipy.org Mon Jul 17 20:25:50 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 17 Jul 2006 19:25:50 -0500 (CDT) Subject: [Scipy-svn] r2111 - trunk/Lib/optimize/cobyla Message-ID: <20060718002550.2485139C02D@new.scipy.org> Author: oliphant Date: 2006-07-17 19:25:47 -0500 (Mon, 17 Jul 2006) New Revision: 2111 Modified: trunk/Lib/optimize/cobyla/trstlp.f Log: Change test to fix vmultd(k) to 0.0 when |vmultd(k)| < eps Modified: trunk/Lib/optimize/cobyla/trstlp.f =================================================================== --- trunk/Lib/optimize/cobyla/trstlp.f 2006-07-17 23:03:19 UTC (rev 2110) +++ trunk/Lib/optimize/cobyla/trstlp.f 2006-07-18 00:25:47 UTC (rev 2111) @@ -435,9 +435,9 @@ ICON=0 C EPS = 2.2E-16 -C EPS = 0.0D0 DO 450 K=1,MCON - IF (VMULTD(K) .LT. -EPS) THEN + IF (VMULTD(K) .GT. -EPS .AND. VMULTD(K) .LT. EPS) VMULTD(K)=0.0D0 + IF (VMULTD(K) .LT. 0.0D0) THEN TEMP=VMULTC(K)/(VMULTC(K)-VMULTD(K)) IF (TEMP .LT. RATIO) THEN RATIO=TEMP From scipy-svn at scipy.org Wed Jul 19 08:41:32 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 19 Jul 2006 07:41:32 -0500 (CDT) Subject: [Scipy-svn] r2112 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060719124132.D1C7739C0B6@new.scipy.org> Author: fullung Date: 2006-07-19 07:41:03 -0500 (Wed, 19 Jul 2006) New Revision: 2112 Added: trunk/Lib/sandbox/svm/predict.py Modified: trunk/Lib/sandbox/svm/__init__.py trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/libsvm.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_dataset.py trunk/Lib/sandbox/svm/tests/test_oneclass.py trunk/Lib/sandbox/svm/tests/test_precomputed.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Factored out prediction logic so that we can support precomputed and custom kernels. Modified: trunk/Lib/sandbox/svm/__init__.py =================================================================== --- trunk/Lib/sandbox/svm/__init__.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/__init__.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -20,3 +20,4 @@ from oneclass import * from dataset import * from kernel import * +from predict import * Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -7,57 +7,42 @@ __all__ = [ 'LibSvmCClassificationModel', 'LibSvmNuClassificationModel', + 'LibSvmClassificationResults' ] class LibSvmClassificationResults: - def __init__(self, model, dataset): - self.model = model - # keep a reference to the dataset here because the support - # vectors point to some or all of the vectors in the dataset - self.dataset = dataset - # XXX this is probably suboptimal when training many models - # that each only have a few support vectors. look at some - # options when we do memory optimization. - - model = model.contents - self.nr_class = model.nr_class - self.labels = model.labels[:self.nr_class] - self.rho = model.rho[:self.nr_class*(self.nr_class-1)/2] - self.nSV = model.nSV[:self.nr_class] - sv_coef = N.empty((self.nr_class-1, model.l), dtype=N.float64) - for i, c in enumerate(model.sv_coef[:self.nr_class-1]): - sv_coef[i,:] = c[:model.l] + def __init__(self, model, traindataset, PredictorType): + modelc = model.contents + self.nr_class = modelc.nr_class + self.labels = modelc.labels[:self.nr_class] + nrho = self.nr_class * (self.nr_class - 1) / 2 + self.rho = modelc.rho[:nrho] + self.nSV = modelc.nSV[:self.nr_class] + sv_coef = N.empty((self.nr_class - 1, modelc.l), dtype=N.float64) + for i, c in enumerate(modelc.sv_coef[:self.nr_class - 1]): + sv_coef[i,:] = c[:modelc.l] self.sv_coef = sv_coef + self.predictor = PredictorType(model, traindataset) - def __del__(self): - libsvm.svm_destroy_model(self.model) - def predict(self, dataset): """ This function does classification on a test vector x and returns the label of the predicted class. """ - def p(x): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - return int(libsvm.svm_predict(self.model, xptr)) - return map(p, dataset.data) + return [self.predictor.predict(x) for x in dataset] def predict_values(self, dataset): """ - This function does classification on a test vector x and + This function does classification on a test dataset and returns decision values. For training data with nr_class classes, this function returns - nr_class*(nr_class-1)/2 decision values in a dictionary. The - keys of the dictionary are 2-tuples, one for each combination - of two class labels. + nr_class*(nr_class-1)/2 decision values in a dictionary for + each item in the test dataset. The keys of the dictionary are + 2-tuples, one for each combination of two class labels. """ - def p(x): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - n = self.nr_class*(self.nr_class-1)/2 - v = N.empty((n,), dtype=N.float64) - vptr = cast(v.ctypes.data, POINTER(c_double)) - libsvm.svm_predict_values(self.model, xptr, vptr) + n = self.nr_class * (self.nr_class - 1) / 2 + def p(v): count = 0 d = {} for i in range(len(self.labels)): @@ -66,24 +51,25 @@ d[self.labels[j], self.labels[i]] = -v[count] count += 1 return d - return map(p, dataset.data) + vs = [self.predictor.predict_values(x, n) for x in dataset] + return [p(v) for v in vs] def predict_probability(self, dataset): """ - This function does classification on a test vector x for a + This function does classification on a test dataset for a model with probability information. - This function returns a 2-tuple. The first item is the label - of the class with the highest probability. The second item is - a dictionary that associated labels with class probabilities. + This function returns a list of 2-tuples. The first item in + each tuple is the label of the class with the highest + probability. The second item is a dictionary that associated + labels with class probabilities. """ def p(x): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - prob_estimates = N.empty((self.nr_class,), dtype=N.float64) - peptr = cast(prob_estimates.ctypes.data, POINTER(c_double)) - label = libsvm.svm_predict_probability(self.model, xptr, peptr) - return int(label), dict(zip(self.labels, prob_estimates)) - return map(p, dataset.data) + n = self.nr_class + label, prob_estimates = \ + self.predictor.predict_probability(x, self.nr_class) + return label, prob_estimates + return [p(x) for x in dataset] class LibSvmClassificationModel(LibSvmModel): """ @@ -105,7 +91,6 @@ def __init__(self, kernel, weights, **kwargs): LibSvmModel.__init__(self, kernel, **kwargs) if weights is not None: - # XXX check whether labels need to be sorted self.weight_labels = N.empty((len(weights),), dtype=N.intp) self.weights = N.empty((len(weights),), dtype=N.float64) for i, (label, weight) in enumerate(weights): @@ -120,8 +105,8 @@ def cross_validate(self, dataset, nr_fold): """ - Perform cross-validation to determine the suitability of - chosen model parameters. + Perform stratified cross-validation to determine the + suitability of chosen model parameters. Data are separated to nr_fold folds. Each fold is validated against a model trained using the data from the remaining @@ -136,7 +121,9 @@ libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) total_correct = 0. for x, t in zip(dataset.data, target): - if x[0] == int(t): total_correct += 1 + if x[0] == int(t): + total_correct += 1 + # XXX also return results from folds in a list return 100.0 * total_correct / len(dataset.data) class LibSvmCClassificationModel(LibSvmClassificationModel): Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -13,6 +13,12 @@ def __init__(self, data): self.data = data + def __iter__(self): + return self.data.__iter__() + + def __len__(self): + return len(self.data) + def getgamma(self): maxlen = 0 for y, x in self.data: @@ -149,6 +155,9 @@ def __init__(self, origdata): self.data = map(lambda x: convert_to_svm_node(x), origdata) + def __iter__(self): + return self.data.__iter__() + def convert_to_svm_node(x): y = N.empty(len(x) + 1, dtype=libsvm.svm_node_dtype) y[-1] = -1, 0. Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -3,6 +3,10 @@ from ctypes import * import numpy as N +__all__ = [ + 'svm_node_dtype' + ] + _libsvm = N.ctypes_load_library('libsvm_', __file__) svm_node_dtype = \ @@ -113,7 +117,8 @@ (c_double, [POINTER(svm_model)]), 'svm_cross_validation' : (None, - [POINTER(svm_problem), POINTER(svm_parameter), c_int, POINTER(c_double)]), + [POINTER(svm_problem), POINTER(svm_parameter), c_int, + POINTER(c_double)]), 'svm_destroy_model' : (None, [POINTER(svm_model)]) } @@ -135,17 +140,3 @@ problem.x = x problem.y = y return problem - -__all__ = [ - 'svm_node_dtype', - 'C_SVC', - 'NU_SVC', - 'ONE_CLASS', - 'EPSILON_SVR', - 'NU_SVR', - 'LINEAR', - 'POLY', - 'RBF', - 'SIGMOID', - 'PRECOMPUTED' - ] + libsvm_api.keys() Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/model.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -7,9 +7,6 @@ 'LibSvmModel' ] -c_double_null_ptr = POINTER(c_double)() -c_int_null_ptr = POINTER(c_int)() - class LibSvmModel: def __init__(self, kernel, tolerance=0.001, shrinking=True, cache_size=40): @@ -40,33 +37,19 @@ # defaults for optional parameters param.nr_weight = 0 - param.weight = c_double_null_ptr - param.weight_label = c_int_null_ptr + param.weight = POINTER(c_double)() + param.weight_label = POINTER(c_int)() param.probability = False self.param = param - def fit(self, dataset): + def fit(self, dataset, ResultType, PredictorType): problem = dataset._create_svm_problem() dataset._update_svm_parameter(self.param) self._check_problem_param(problem, self.param) - model = libsvm.svm_train(problem, self.param) + return ResultType(model, dataset, PredictorType) - # weights are no longer required, so remove to them as the - # data they point to might disappear - model.contents.param.nr_weight = 0 - model.contents.param.weight = c_double_null_ptr - model.contents.param.weight_label = c_int_null_ptr - - # results keep a reference to the dataset because the - # svm_model refers to some of its vectors as the support - # vectors - # XXX we can hide an id in the end of record marker so that we - # can figure out which support vectors to keep references to - # even when not using precomputed kernels - return self.Results(model, dataset) - def _check_problem_param(self, problem, param): error_msg = libsvm.svm_check_parameter(problem, param) if error_msg: Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -1,32 +1,24 @@ -from ctypes import cast, POINTER, byref, c_double - from model import LibSvmModel import libsvm __all__ = [ - 'LibSvmOneClassModel' + 'LibSvmOneClassModel', + 'LibSvmOneClassResults' ] class LibSvmOneClassResults: - def __init__(self, model, dataset): - self.model = model - self.dataset = dataset - model = model.contents - self.rho = model.rho[0] - self.sv_coef = model.sv_coef[0][:model.l] + def __init__(self, model, traindataset, PredictorType): + modelc = model.contents + self.rho = modelc.rho[0] + self.sv_coef = modelc.sv_coef[0][:modelc.l] + self.predictor = PredictorType(model, traindataset) - def __del__(self): - libsvm.svm_destroy_model(self.model) - def predict(self, dataset): """ This function returns a list of boolean values which indicate whether the test vectors form part of the distribution. """ - def p(x): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - return libsvm.svm_predict(self.model, xptr) > 0 - return map(p, dataset.data) + return [self.predictor.predict(x) > 0 for x in dataset] def predict_values(self, dataset): """ @@ -38,12 +30,7 @@ distribution, while a non-positive value indicates that is is not. """ - def p(x): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - v = c_double() - libsvm.svm_predict_values(self.model, xptr, byref(v)) - return v.value - return map(p, dataset.data) + return [self.predictor.predict_values(x, 1) for x in dataset] class LibSvmOneClassModel(LibSvmModel): """ Added: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -0,0 +1,80 @@ +from ctypes import cast, POINTER, c_double +import numpy as N + +import libsvm + +__all__ = [ + 'LibSvmPredictor', + 'LibSvmPrecomputedPredictor', + 'LibSvmSparsePredictor', + 'PrecomputedSparsePredictor', + 'DensePredictor' + ] + +class LibSvmPredictor: + def __init__(self, model, dataset): + self.model = model + + def __del__(self): + libsvm.svm_destroy_model(self.model) + + def predict(self, x): + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + return libsvm.svm_predict(self.model, xptr) + + def predict_values(self, x, n): + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + v = N.empty((n,), dtype=N.float64) + vptr = cast(v.ctypes.data, POINTER(c_double)) + libsvm.svm_predict_values(self.model, xptr, vptr) + return v + + def predict_probability(self, x, n): + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + pe = N.empty((n,), dtype=N.float64) + peptr = cast(pe.ctypes.data, POINTER(c_double)) + label = libsvm.svm_predict_probability(self.model, xptr, peptr) + return label, pe + +class LibSvmPrecomputedPredictor: + def __init__(self, model, dataset): + raise NotImplementedError + + def predict(self): + raise NotImplementedError + + def predict_values(self): + raise NotImplementedError + + def predict_probability(self): + raise NotImplementedError + +class LibSvmSparsePredictor: + def __init__(self, model, dataset): + raise NotImplementedError + + def predict_values(self): + raise NotImplementedError + + def predict_probability(self): + raise NotImplementedError + +class PrecomputedSparsePredictor: + def __init__(self, model, dataset): + raise NotImplementedError + + def predict_values(self): + raise NotImplementedError + + def predict_probability(self): + raise NotImplementedError + +class DensePredictor: + def __init__(self, model, dataset): + raise NotImplementedError + + def predict_values(self): + raise NotImplementedError + + def predict_probability(self): + raise NotImplementedError Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -6,33 +6,27 @@ __all__ = [ 'LibSvmEpsilonRegressionModel', - 'LibSvmNuRegressionModel' + 'LibSvmNuRegressionModel', + 'LibSvmRegressionResults' ] # XXX document why get_svr_probability could be useful class LibSvmRegressionResults: - def __init__(self, model, dataset): - self.model = model - self.dataset = dataset - model = model.contents - self.rho = model.rho[0] - self.sv_coef = model.sv_coef[0][:model.l] - if model.probA: - self.sigma = model.probA[0] + def __init__(self, model, traindataset, PredictorType): + modelc = model.contents + self.rho = modelc.rho[0] + self.sv_coef = modelc.sv_coef[0][:modelc.l] + if modelc.probA: + self.sigma = modelc.probA[0] + self.predictor = PredictorType(model, traindataset) - def __del__(self): - libsvm.svm_destroy_model(self.model) - def predict(self, dataset): """ This function does regression on a test vector x and returns the function value of x calculated using the model. """ - def p(x): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - return libsvm.svm_predict(self.model, xptr) - return map(p, dataset.data) + return [self.predictor.predict(x) for x in dataset] def get_svr_probability(self): """ @@ -55,8 +49,8 @@ def cross_validate(self, dataset, nr_fold): """ - Perform cross-validation to determine the suitability of - chosen model parameters. + Perform stratified cross-validation to determine the + suitability of chosen model parameters. Data are separated to nr_fold folds. Each fold is validated against a model trained using the data from the remaining Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -3,45 +3,50 @@ set_local_path('../..') from svm.classification import * -from svm.dataset import LibSvmClassificationDataSet -from svm.dataset import LibSvmTestDataSet +from svm.dataset import LibSvmClassificationDataSet, LibSvmTestDataSet from svm.kernel import * +from svm.predict import * restore_path() class test_classification(NumpyTestCase): def check_basics(self): Model = LibSvmCClassificationModel - Kernel = LinearKernel() - Model(Kernel) - Model(Kernel, cost=1.0) + kernel = LinearKernel() + Model(kernel) + Model(kernel, cost=1.0) weights = [(2, 10.0), (1, 20.0), (0, 30.0)] - Model(Kernel, weights=weights) - Model(Kernel, 1.0, weights) - Model(Kernel, cost=1.0, weights=weights) + Model(kernel, weights=weights) + Model(kernel, 1.0, weights) + Model(kernel, cost=1.0, weights=weights) Model = LibSvmNuClassificationModel - Model(Kernel) - Model(Kernel, nu=0.5) - Model(Kernel, weights=weights) - Model(Kernel, 0.5, weights) + Model(kernel) + Model(kernel, nu=0.5) + Model(kernel, weights=weights) + Model(kernel, 0.5, weights) def check_c_basics(self): + ModelType = LibSvmCClassificationModel + ResultType = LibSvmClassificationResults + PredictorType = LibSvmPredictor + labels = [0, 1, 1, 2] x = [N.array([0, 0]), N.array([0, 1]), N.array([1, 0]), N.array([1, 1])] traindata = LibSvmClassificationDataSet(zip(labels, x)) - - Model = LibSvmCClassificationModel - model = Model(RBFKernel(traindata.gamma)) - results = model.fit(traindata) - + model = ModelType(RBFKernel(traindata.gamma)) + results = model.fit(traindata, ResultType, PredictorType) testdata = LibSvmTestDataSet(x) results.predict(testdata) results.predict_values(testdata) def check_c_more(self): + ModelType = LibSvmCClassificationModel + ResultType = LibSvmClassificationResults + PredictorType = LibSvmPredictor + labels = [0, 1, 1, 2] x = [N.array([0, 0]), N.array([0, 1]), @@ -66,8 +71,8 @@ for kernel, expected_rho, expected_error in \ zip(kernels, expected_rhos, expected_errors): - model = LibSvmCClassificationModel(kernel, cost, weights) - results = model.fit(traindata) + model = ModelType(kernel, cost, weights) + results = model.fit(traindata, ResultType, PredictorType) self.assertEqual(results.labels, [0, 1, 2]) #self.assertEqual(model.nSV, [1, 2, 1]) @@ -81,6 +86,10 @@ self.assertEqual(N.sum(predictions != labels), expected_error) def check_c_probability(self): + ModelType = LibSvmCClassificationModel + ResultType = LibSvmClassificationResults + PredictorType = LibSvmPredictor + labels = [0, 1, 1, 2] x = [N.array([0, 0]), N.array([0, 1]), @@ -98,8 +107,8 @@ ] for kernel in kernels: - model = LibSvmCClassificationModel(kernel, cost, weights) - results = model.fit(traindata) + model = ModelType(kernel, cost, weights) + results = model.fit(traindata, ResultType, PredictorType) results.predict_probability(testdata) def check_cross_validate(self): Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -45,6 +45,10 @@ data = [(1.0, N.arange(5))] dataset = LibSvmRegressionDataSet(data) self.assertAlmostEqual(dataset.gamma, 0.2) + self.assertEqual(len(dataset), len(data)) + for i, x in enumerate(dataset): + self.assertEqual(data[i][0], x[0]) + assert_array_equal(data[i][1], x[1]['value'][:-1]) def check_classification(self): data = [(1, N.arange(4)), (2, N.arange(10))] @@ -52,11 +56,18 @@ self.assertAlmostEqual(dataset.gamma, 0.1) self.assert_(1 in dataset.labels) self.assert_(2 in dataset.labels) + self.assertEqual(len(dataset), len(data)) + for i, x in enumerate(dataset): + self.assertEqual(data[i][0], x[0]) + assert_array_equal(data[i][1], x[1]['value'][:-1]) def check_oneclass(self): data = [N.arange(2)] dataset = LibSvmOneClassDataSet(data) self.assertAlmostEqual(dataset.gamma, 0.5) + self.assertEqual(len(dataset), len(data)) + for i, x in enumerate(dataset): + assert_array_equal(data[i], x[1]['value'][:-1]) class test_svm_node_dot(NumpyTestCase): def check_dot(self): Modified: trunk/Lib/sandbox/svm/tests/test_oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -2,35 +2,40 @@ import numpy as N set_local_path('../..') +from svm.dataset import LibSvmOneClassDataSet, LibSvmTestDataSet +from svm.kernel import * from svm.oneclass import * -from svm.dataset import LibSvmOneClassDataSet -from svm.dataset import LibSvmTestDataSet -from svm.kernel import * +from svm.predict import * restore_path() class test_oneclass(NumpyTestCase): def check_basics(self): - Model = LibSvmOneClassModel - Kernel = LinearKernel() - Model(Kernel) - Model(Kernel, nu=1.0) + ModelType = LibSvmOneClassModel + kernel = LinearKernel() + ModelType(kernel) + ModelType(kernel, nu=1.0) def check_train(self): + ModelType = LibSvmOneClassModel + ResultType = LibSvmOneClassResults + PredictorType = LibSvmPredictor + x = [N.array([0, 0]), N.array([0, 1]), N.array([1, 0]), N.array([1, 1])] traindata = LibSvmOneClassDataSet(x) - - Model = LibSvmOneClassModel - model = Model(LinearKernel()) - results = model.fit(traindata) - + model = ModelType(LinearKernel()) + results = model.fit(traindata, ResultType, PredictorType) testdata = LibSvmTestDataSet(x) results.predict(testdata) results.predict_values(testdata) def check_more(self): + ModelType = LibSvmOneClassModel + ResultType = LibSvmOneClassResults + PredictorType = LibSvmPredictor + x = [N.array([0, 0]), N.array([0, 1]), N.array([1, 0]), @@ -51,8 +56,8 @@ ] for kernel, expected_pred in zip(kernels, expected_preds): - model = LibSvmOneClassModel(kernel, nu) - results = model.fit(traindata) + model = ModelType(kernel, nu) + results = model.fit(traindata, ResultType, PredictorType) pred = results.predict(testdata) self.assertEqual(results.predict(testdata), expected_pred) values = results.predict_values(testdata) Modified: trunk/Lib/sandbox/svm/tests/test_precomputed.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -2,13 +2,17 @@ import numpy as N set_local_path('../..') -from svm.regression import * from svm.dataset import * from svm.kernel import LinearKernel +from svm.predict import * +from svm.regression import * restore_path() class test_precomputed(NumpyTestCase): def check_precomputed(self): + ModelType = LibSvmEpsilonRegressionModel + ResultType = LibSvmRegressionResults + kernel = LinearKernel() # this dataset remains constant @@ -25,18 +29,19 @@ pcdata12 = pcdata1.combine(data2) model = LibSvmEpsilonRegressionModel(kernel) - results = model.fit(pcdata12) + results = model.fit(pcdata12, ResultType, LibSvmPredictor) # reference model, calculated without involving the # precomputed Gram matrix refy = N.concatenate([y1, y2]) refx = N.vstack([x1, x2]) refdata = LibSvmRegressionDataSet(zip(refy, refx)) - model = LibSvmEpsilonRegressionModel(kernel) - refresults = model.fit(refdata) + model = ModelType(kernel) + #refresults = model.fit(refdata, ResultType, + # LibSvmPrecomputedPredictor) - self.assertAlmostEqual(results.rho, refresults.rho) - assert_array_almost_equal(results.sv_coef, refresults.sv_coef) + #self.assertAlmostEqual(results.rho, refresults.rho) + #assert_array_almost_equal(results.sv_coef, refresults.sv_coef) # XXX sigmas don't match yet. need to find out why. #self.assertAlmostEqual(results.sigma, refresults.sigma) Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-18 00:25:47 UTC (rev 2111) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-19 12:41:03 UTC (rev 2112) @@ -2,30 +2,34 @@ import numpy as N set_local_path('../..') +from svm.dataset import LibSvmRegressionDataSet, LibSvmTestDataSet +from svm.kernel import * +from svm.predict import * from svm.regression import * -from svm.dataset import LibSvmRegressionDataSet -from svm.dataset import LibSvmTestDataSet -from svm.kernel import * restore_path() class test_regression(NumpyTestCase): def check_basics(self): Model = LibSvmEpsilonRegressionModel - Kernel = LinearKernel() - Model(Kernel) - Model(Kernel, epsilon=0.1) - Model(Kernel, cost=1.0) - model = Model(Kernel, shrinking=False) + kernel = LinearKernel() + Model(kernel) + Model(kernel, epsilon=0.1) + Model(kernel, cost=1.0) + model = Model(kernel, shrinking=False) self.assert_(not model.shrinking) Model = LibSvmNuRegressionModel - Model(Kernel) - Model(Kernel, nu=0.5) - model = Model(Kernel, 0.5, cache_size=60, tolerance=0.005) + Model(kernel) + Model(kernel, nu=0.5) + model = Model(kernel, 0.5, cache_size=60, tolerance=0.005) self.assertEqual(model.cache_size, 60) self.assertAlmostEqual(model.tolerance, 0.005) def check_epsilon_train(self): + ModelType = LibSvmEpsilonRegressionModel + ResultType = LibSvmRegressionResults + PredictorType = LibSvmPredictor + y = [10., 20., 30., 40.] x = [N.array([0, 0]), N.array([0, 1]), @@ -33,15 +37,16 @@ N.array([1, 1])] traindata = LibSvmRegressionDataSet(zip(y, x)) testdata = LibSvmTestDataSet(x) - - Model = LibSvmEpsilonRegressionModel - model = Model(LinearKernel()) - - results = model.fit(traindata) + model = ModelType(LinearKernel()) + results = model.fit(traindata, ResultType, PredictorType) results.predict(testdata) results.get_svr_probability() def check_epsilon_more(self): + ModelType = LibSvmEpsilonRegressionModel + ResultType = LibSvmRegressionResults + PredictorType = LibSvmPredictor + y = [0.0, 1.0, 1.0, 2.0] x = [N.array([0, 0]), N.array([0, 1]), @@ -64,8 +69,8 @@ ] for kernel, expected_y in zip(kernels, expected_ys): - model = LibSvmEpsilonRegressionModel(kernel, epsilon, cost) - results = model.fit(traindata) + model = ModelType(kernel, epsilon, cost) + results = model.fit(traindata, ResultType, PredictorType) predictions = results.predict(testdata) # look at differences instead of using assertAlmostEqual # due to slight differences between answers obtained on From scipy-svn at scipy.org Wed Jul 19 09:17:29 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 19 Jul 2006 08:17:29 -0500 (CDT) Subject: [Scipy-svn] r2113 - trunk/Lib/sandbox/svm Message-ID: <20060719131729.14B5939C0B3@new.scipy.org> Author: fullung Date: 2006-07-19 08:17:15 -0500 (Wed, 19 Jul 2006) New Revision: 2113 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/regression.py Log: minor Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-19 12:41:03 UTC (rev 2112) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-19 13:17:15 UTC (rev 2113) @@ -86,8 +86,6 @@ Machines. """ - Results = LibSvmClassificationResults - def __init__(self, kernel, weights, **kwargs): LibSvmModel.__init__(self, kernel, **kwargs) if weights is not None: Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-19 12:41:03 UTC (rev 2112) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-19 13:17:15 UTC (rev 2113) @@ -40,8 +40,6 @@ High-Dimensional Distribution. """ - Results = LibSvmOneClassResults - def __init__(self, kernel, nu=0.5, **kwargs): """ Parameters: Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-19 12:41:03 UTC (rev 2112) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-19 13:17:15 UTC (rev 2113) @@ -42,8 +42,6 @@ return self.sigma class LibSvmRegressionModel(LibSvmModel): - Results = LibSvmRegressionResults - def __init__(self, kernel, **kwargs): LibSvmModel.__init__(self, kernel, **kwargs) From scipy-svn at scipy.org Thu Jul 20 02:09:27 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 20 Jul 2006 01:09:27 -0500 (CDT) Subject: [Scipy-svn] r2114 - in trunk/Lib/sandbox: . stsci stsci/convolve stsci/convolve/lib stsci/convolve/src stsci/image stsci/image/lib stsci/image/src Message-ID: <20060720060927.32B9F39C0EF@new.scipy.org> Author: oliphant Date: 2006-07-20 01:09:14 -0500 (Thu, 20 Jul 2006) New Revision: 2114 Added: trunk/Lib/sandbox/stsci/ trunk/Lib/sandbox/stsci/convolve/ trunk/Lib/sandbox/stsci/convolve/lib/ trunk/Lib/sandbox/stsci/convolve/lib/Convolve.py trunk/Lib/sandbox/stsci/convolve/lib/__init__.py trunk/Lib/sandbox/stsci/convolve/lib/iraf_frame.py trunk/Lib/sandbox/stsci/convolve/setup.py trunk/Lib/sandbox/stsci/convolve/src/ trunk/Lib/sandbox/stsci/convolve/src/_correlatemodule.c trunk/Lib/sandbox/stsci/image/ trunk/Lib/sandbox/stsci/image/lib/ trunk/Lib/sandbox/stsci/image/lib/__init__.py trunk/Lib/sandbox/stsci/image/lib/_image.py trunk/Lib/sandbox/stsci/image/lib/combine.py trunk/Lib/sandbox/stsci/image/setup.py trunk/Lib/sandbox/stsci/image/src/ trunk/Lib/sandbox/stsci/image/src/_combinemodule.c trunk/Lib/sandbox/stsci/setup.py Log: Added stsci modules to sandbox. Added: trunk/Lib/sandbox/stsci/convolve/lib/Convolve.py =================================================================== --- trunk/Lib/sandbox/stsci/convolve/lib/Convolve.py 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/convolve/lib/Convolve.py 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,391 @@ +import numpy as num +import _correlate +import numpy.dft as dft +import iraf_frame + +VALID = 0 +SAME = 1 +FULL = 2 +PASS = 3 + +convolution_modes = { + "valid":0, + "same":1, + "full":2, + "pass":3, + } + +def _condition_inputs(data, kernel): + data, kernel = num.asarray(data), num.asarray(kernel) + if num.rank(data) == 0: + data.shape = (1,) + if num.rank(kernel) == 0: + kernel.shape = (1,) + if num.rank(data) > 1 or num.rank(kernel) > 1: + raise ValueError("arrays must be 1D") + if len(data) < len(kernel): + data, kernel = kernel, data + return data, kernel + +def correlate(data, kernel, mode=FULL): + """correlate(data, kernel, mode=FULL) + + >>> correlate(num.arange(8), [1, 2], mode=VALID) + array([ 2, 5, 8, 11, 14, 17, 20]) + >>> correlate(num.arange(8), [1, 2], mode=SAME) + array([ 0, 2, 5, 8, 11, 14, 17, 20]) + >>> correlate(num.arange(8), [1, 2], mode=FULL) + array([ 0, 2, 5, 8, 11, 14, 17, 20, 7]) + >>> correlate(num.arange(8), [1, 2, 3], mode=VALID) + array([ 8, 14, 20, 26, 32, 38]) + >>> correlate(num.arange(8), [1, 2, 3], mode=SAME) + array([ 3, 8, 14, 20, 26, 32, 38, 20]) + >>> correlate(num.arange(8), [1, 2, 3], mode=FULL) + array([ 0, 3, 8, 14, 20, 26, 32, 38, 20, 7]) + >>> correlate(num.arange(8), [1, 2, 3, 4, 5, 6], mode=VALID) + array([ 70, 91, 112]) + >>> correlate(num.arange(8), [1, 2, 3, 4, 5, 6], mode=SAME) + array([ 17, 32, 50, 70, 91, 112, 85, 60]) + >>> correlate(num.arange(8), [1, 2, 3, 4, 5, 6], mode=FULL) + array([ 0, 6, 17, 32, 50, 70, 91, 112, 85, 60, 38, 20, 7]) + >>> correlate(num.arange(8), 1+1j) + Traceback (most recent call last): + ... + TypeError: array cannot be safely cast to required type + + """ + data, kernel = _condition_inputs(data, kernel) + lenk = len(kernel) + halfk = int(lenk/2) + even = (lenk % 2 == 0) + kdata = [0] * lenk + + if mode in convolution_modes.keys(): + mode = convolution_modes[ mode ] + + result_type = max(kernel.dtype.name, data.dtype.name) + + if mode == VALID: + wdata = num.concatenate((kdata, data, kdata)) + result = wdata.astype(result_type) + _correlate.Correlate1d(kernel, wdata, result) + return result[lenk+halfk:-lenk-halfk+even] + elif mode == SAME: + wdata = num.concatenate((kdata, data, kdata)) + result = wdata.astype(result_type) + _correlate.Correlate1d(kernel, wdata, result) + return result[lenk:-lenk] + elif mode == FULL: + wdata = num.concatenate((kdata, data, kdata)) + result = wdata.astype(result_type) + _correlate.Correlate1d(kernel, wdata, result) + return result[halfk+1:-halfk-1+even] + elif mode == PASS: + result = data.astype(result_type) + _correlate.Correlate1d(kernel, data, result) + return result + else: + raise ValueError("Invalid convolution mode.") + +cross_correlate = correlate + +pix_modes = { + "nearest" : 0, + "reflect": 1, + "wrap" : 2, + "constant": 3 + } + +def convolve(data, kernel, mode=FULL): + """convolve(data, kernel, mode=FULL) + Returns the discrete, linear convolution of 1-D + sequences a and v; mode can be 0 (VALID), 1 (SAME), or 2 (FULL) + to specify size of the resulting sequence. + + >>> convolve(num.arange(8), [1, 2], mode=VALID) + array([ 1, 4, 7, 10, 13, 16, 19]) + >>> convolve(num.arange(8), [1, 2], mode=SAME) + array([ 0, 1, 4, 7, 10, 13, 16, 19]) + >>> convolve(num.arange(8), [1, 2], mode=FULL) + array([ 0, 1, 4, 7, 10, 13, 16, 19, 14]) + >>> convolve(num.arange(8), [1, 2, 3], mode=VALID) + array([ 4, 10, 16, 22, 28, 34]) + >>> convolve(num.arange(8), [1, 2, 3], mode=SAME) + array([ 1, 4, 10, 16, 22, 28, 34, 32]) + >>> convolve(num.arange(8), [1, 2, 3], mode=FULL) + array([ 0, 1, 4, 10, 16, 22, 28, 34, 32, 21]) + >>> convolve(num.arange(8), [1, 2, 3, 4, 5, 6], mode=VALID) + array([35, 56, 77]) + >>> convolve(num.arange(8), [1, 2, 3, 4, 5, 6], mode=SAME) + array([ 4, 10, 20, 35, 56, 77, 90, 94]) + >>> convolve(num.arange(8), [1, 2, 3, 4, 5, 6], mode=FULL) + array([ 0, 1, 4, 10, 20, 35, 56, 77, 90, 94, 88, 71, 42]) + >>> convolve([1.,2.], num.arange(10.)) + array([ 0., 1., 4., 7., 10., 13., 16., 19., 22., 25., 18.]) + """ + data, kernel = _condition_inputs(data, kernel) + if len(data) >= len(kernel): + return correlate(data, kernel[::-1], mode) + else: + return correlate(kernel, data[::-1], mode) + + +def _gaussian(sigma, mew, npoints, sigmas): + ox = num.arange(mew-sigmas*sigma, + mew+sigmas*sigma, + 2*sigmas*sigma/npoints, type=num.float64) + x = ox-mew + x /= sigma + x = x * x + x *= -1/2 + x = num.exp(x) + return ox, 1/(sigma * num.sqrt(2*num.pi)) * x + +def _correlate2d_fft(data0, kernel0, output=None, mode="nearest", cval=0.0): + """_correlate2d_fft does 2d correlation of 'data' with 'kernel', storing + the result in 'output' using the FFT to perform the correlation. + + supported 'mode's include: + 'nearest' elements beyond boundary come from nearest edge pixel. + 'wrap' elements beyond boundary come from the opposite array edge. + 'reflect' elements beyond boundary come from reflection on same array edge. + 'constant' elements beyond boundary are set to 'cval' + """ + shape = data0.shape + kshape = kernel0.shape + oversized = (num.array(shape) + num.array(kshape)) + + dy = kshape[0] // 2 + dx = kshape[1] // 2 + + kernel = num.zeros(oversized, dtype=num.float64) + kernel[:kshape[0], :kshape[1]] = kernel0[::-1,::-1] # convolution <-> correlation + data = iraf_frame.frame(data0, oversized, mode=mode, cval=cval) + + complex_result = (isinstance(data, num.complexfloating) or + isinstance(kernel, num.complexfloating)) + + Fdata = dft.fft2(data) + del data + + Fkernel = dft.fft2(kernel) + del kernel + + num.multiply(Fdata, Fkernel, Fdata) + del Fkernel + + if complex_result: + convolved = dft.irfft2( Fdata, s=oversized) + else: + convolved = dft.irfft2( Fdata, s=oversized) + + result = convolved[ kshape[0]-1:shape[0]+kshape[0]-1, kshape[1]-1:shape[1]+kshape[1]-1 ] + + if output is not None: + output._copyFrom( result ) + else: + return result + + +def _correlate2d_naive(data, kernel, output=None, mode="nearest", cval=0.0): + return _correlate.Correlate2d(kernel, data, output, pix_modes[mode], cval) + +def _fix_data_kernel(data, kernel): + """The _correlate.Correlate2d C-code can only handle kernels which + fit inside the data array. Since convolution and correlation are + commutative, _fix_data_kernel reverses kernel and data if necessary + and panics if there's no good order. + """ + data, kernel = map(num.asarray, [data, kernel]) + if num.rank(data) == 0: + data.shape = (1,1) + elif num.rank(data) == 1: + data.shape = (1,) + data.shape + if num.rank(kernel) == 0: + kernel.shape = (1,1) + elif num.rank(kernel) == 1: + kernel.shape = (1,) + kernel.shape + if (kernel.shape[0] > data.shape[0] and + kernel.shape[1] > data.shape[1]): + kernel, data = data, kernel + elif (kernel.shape[0] <= data.shape[0] and + kernel.shape[1] <= data.shape[1]): + pass + return data, kernel + +def correlate2d(data, kernel, output=None, mode="nearest", cval=0.0, fft=0): + """correlate2d does 2d correlation of 'data' with 'kernel', storing + the result in 'output'. + + supported 'mode's include: + 'nearest' elements beyond boundary come from nearest edge pixel. + 'wrap' elements beyond boundary come from the opposite array edge. + 'reflect' elements beyond boundary come from reflection on same array edge. + 'constant' elements beyond boundary are set to 'cval' + + If fft is True, the correlation is performed using the FFT, else the + correlation is performed using the naive approach. + + >>> a = num.arange(20*20) + >>> a = a.reshape((20,20)) + >>> b = num.ones((5,5), dtype=num.float64) + >>> rn = correlate2d(a, b, fft=0) + >>> rf = correlate2d(a, b, fft=1) + >>> num.alltrue(num.ravel(rn-rf<1e-10)) + True + """ + data, kernel = _fix_data_kernel(data, kernel) + if fft: + return _correlate2d_fft(data, kernel, output, mode, cval) + else: + a = _correlate2d_naive(data, kernel, output, mode, cval) + #a = a.byteswap() + return a + +def convolve2d(data, kernel, output=None, mode="nearest", cval=0.0, fft=0): + """convolve2d does 2d convolution of 'data' with 'kernel', storing + the result in 'output'. + + supported 'mode's include: + 'nearest' elements beyond boundary come from nearest edge pixel. + 'wrap' elements beyond boundary come from the opposite array edge. + 'reflect' elements beyond boundary come from reflection on same array edge. + 'constant' elements beyond boundary are set to 'cval' + + >>> a = num.arange(20*20) + >>> a = a.reshape((20,20)) + >>> b = num.ones((5,5), dtype=num.float64) + >>> rn = convolve2d(a, b, fft=0) + >>> rf = convolve2d(a, b, fft=1) + >>> num.alltrue(num.ravel(rn-rf<1e-10)) + True + """ + data, kernel = _fix_data_kernel(data, kernel) + kernel = kernel[::-1,::-1] # convolution -> correlation + if fft: + return _correlate2d_fft(data, kernel, output, mode, cval) + else: + return _correlate2d_naive(data, kernel, output, mode, cval) + +def _boxcar(data, output, boxshape, mode, cval): + if len(boxshape) == 1: + _correlate.Boxcar2d(data[num.newaxis,...], 1, boxshape[0], + output[num.newaxis,...], mode, cval) + elif len(boxshape) == 2: + _correlate.Boxcar2d(data, boxshape[0], boxshape[1], output, mode, cval) + else: + raise ValueError("boxshape must be a 1D or 2D shape.") + +def boxcar(data, boxshape, output=None, mode="nearest", cval=0.0): + """boxcar computes a 1D or 2D boxcar filter on every 1D or 2D subarray of data. + + 'boxshape' is a tuple of integers specifying the dimensions of the filter: e.g. (3,3) + + if 'output' is specified, it should be the same shape as 'data' and + None will be returned. + + supported 'mode's include: + 'nearest' elements beyond boundary come from nearest edge pixel. + 'wrap' elements beyond boundary come from the opposite array edge. + 'reflect' elements beyond boundary come from reflection on same array edge. + 'constant' elements beyond boundary are set to 'cval' + + >>> boxcar(num.array([10, 0, 0, 0, 0, 0, 1000]), (3,), mode="nearest").astype(num.longlong) + array([ 6, 3, 0, 0, 0, 333, 666], dtype=int64) + >>> boxcar(num.array([10, 0, 0, 0, 0, 0, 1000]), (3,), mode="wrap").astype(num.longlong) + array([336, 3, 0, 0, 0, 333, 336], dtype=int64) + >>> boxcar(num.array([10, 0, 0, 0, 0, 0, 1000]), (3,), mode="reflect").astype(num.longlong) + array([ 6, 3, 0, 0, 0, 333, 666], dtype=int64) + >>> boxcar(num.array([10, 0, 0, 0, 0, 0, 1000]), (3,), mode="constant").astype(num.longlong) + array([ 3, 3, 0, 0, 0, 333, 333], dtype=int64) + >>> a = num.zeros((10,10)) + >>> a[0,0] = 100 + >>> a[5,5] = 1000 + >>> a[9,9] = 10000 + >>> boxcar(a, (3,3)).astype(num.longlong) + array([[ 44, 22, 0, 0, 0, 0, 0, 0, 0, 0], + [ 22, 11, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 1111, 2222], + [ 0, 0, 0, 0, 0, 0, 0, 0, 2222, 4444]], dtype=int64) + >>> boxcar(a, (3,3), mode="wrap").astype(num.longlong) + array([[1122, 11, 0, 0, 0, 0, 0, 0, 1111, 1122], + [ 11, 11, 0, 0, 0, 0, 0, 0, 0, 11], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [1111, 0, 0, 0, 0, 0, 0, 0, 1111, 1111], + [1122, 11, 0, 0, 0, 0, 0, 0, 1111, 1122]], dtype=int64) + >>> boxcar(a, (3,3), mode="reflect").astype(num.longlong) + array([[ 44, 22, 0, 0, 0, 0, 0, 0, 0, 0], + [ 22, 11, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 1111, 2222], + [ 0, 0, 0, 0, 0, 0, 0, 0, 2222, 4444]], dtype=int64) + >>> boxcar(a, (3,3), mode="constant").astype(num.longlong) + array([[ 11, 11, 0, 0, 0, 0, 0, 0, 0, 0], + [ 11, 11, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 111, 111, 111, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 1111, 1111], + [ 0, 0, 0, 0, 0, 0, 0, 0, 1111, 1111]], dtype=int64) + + >>> a = num.zeros((10,10)) + >>> a[3:6,3:6] = 111 + >>> boxcar(a, (3,3)).astype(num.longlong) + array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 12, 24, 37, 24, 12, 0, 0, 0], + [ 0, 0, 24, 49, 74, 49, 24, 0, 0, 0], + [ 0, 0, 37, 74, 111, 74, 37, 0, 0, 0], + [ 0, 0, 24, 49, 74, 49, 24, 0, 0, 0], + [ 0, 0, 12, 24, 37, 24, 12, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64) + """ + mode = pix_modes[ mode ] + if output is None: + woutput = data.astype(num.float64) + else: + woutput = output + _fbroadcast(_boxcar, len(boxshape), data.shape, + (data, woutput), (boxshape, mode, cval)) + if output is None: + return woutput + +def _fbroadcast(f, N, shape, args, params=()): + """_fbroadcast(f, N, args, shape, params=()) calls 'f' for each of the + 'N'-dimensional inner subnumarray of 'args'. Each subarray has + .shape == 'shape'[-N:]. There are a total of product(shape[:-N]) + calls to 'f'. + """ + if len(shape) == N: + apply(f, tuple(args)+params) + else: + for i in range(shape[0]): + _fbroadcast(f, N, shape[1:], [x[i] for x in args], params) + +def test(): + import doctest, Convolve + return doctest.testmod(Convolve) + +if __name__ == "__main__": + print test() Added: trunk/Lib/sandbox/stsci/convolve/lib/__init__.py =================================================================== --- trunk/Lib/sandbox/stsci/convolve/lib/__init__.py 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/convolve/lib/__init__.py 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,2 @@ +from Convolve import * +import iraf_frame Added: trunk/Lib/sandbox/stsci/convolve/lib/iraf_frame.py =================================================================== --- trunk/Lib/sandbox/stsci/convolve/lib/iraf_frame.py 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/convolve/lib/iraf_frame.py 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,192 @@ +import numpy as num + +"""This module defines the function frame() which creates +a framed copy of an input array with the boundary pixels +defined according to the IRAF boundary modes: 'nearest', +'reflect', 'wrap', and 'constant.' +""" + +def frame_nearest(a, shape, cval=None): + + """frame_nearest creates an oversized copy of 'a' with new 'shape' + and the contents of 'a' in the center. The boundary pixels are + copied from the nearest edge pixel in 'a'. + + >>> a = num.arange(16, shape=(4,4)) + >>> frame_nearest(a, (8,8)) + array([[ 0, 0, 0, 1, 2, 3, 3, 3], + [ 0, 0, 0, 1, 2, 3, 3, 3], + [ 0, 0, 0, 1, 2, 3, 3, 3], + [ 4, 4, 4, 5, 6, 7, 7, 7], + [ 8, 8, 8, 9, 10, 11, 11, 11], + [12, 12, 12, 13, 14, 15, 15, 15], + [12, 12, 12, 13, 14, 15, 15, 15], + [12, 12, 12, 13, 14, 15, 15, 15]]) + + """ + + b = num.zeros(shape, dtype=a.dtype) + delta = (num.array(b.shape) - num.array(a.shape)) + dy = delta[0] // 2 + dx = delta[1] // 2 + my = a.shape[0] + dy + mx = a.shape[1] + dx + + b[dy:my, dx:mx] = a # center + b[:dy,dx:mx] = a[0:1,:] # top + b[my:,dx:mx] = a[-1:,:] # bottom + b[dy:my, :dx] = a[:, 0:1] # left + b[dy:my, mx:] = a[:, -1:] # right + b[:dy, :dx] = a[0,0] # topleft + b[:dy, mx:] = a[0,-1] # topright + b[my:, :dx] = a[-1, 0] # bottomleft + b[my:, mx:] = a[-1, -1] # bottomright + + return b + +def frame_reflect(a, shape, cval=None): + + """frame_reflect creates an oversized copy of 'a' with new 'shape' + and the contents of 'a' in the center. The boundary pixels are + reflected from the nearest edge pixels in 'a'. + + >>> a = num.arange(16, shape=(4,4)) + >>> frame_reflect(a, (8,8)) + array([[ 5, 4, 4, 5, 6, 7, 7, 6], + [ 1, 0, 0, 1, 2, 3, 3, 2], + [ 1, 0, 0, 1, 2, 3, 3, 2], + [ 5, 4, 4, 5, 6, 7, 7, 6], + [ 9, 8, 8, 9, 10, 11, 11, 10], + [13, 12, 12, 13, 14, 15, 15, 14], + [13, 12, 12, 13, 14, 15, 15, 14], + [ 9, 8, 8, 9, 10, 11, 11, 10]]) + """ + + b = num.zeros(shape, typecode=a.type()) + delta = (num.array(b.shape) - num.array(a.shape)) + dy = delta[0] // 2 + dx = delta[1] // 2 + my = a.shape[0] + dy + mx = a.shape[1] + dx + sy = delta[0] - dy + sx = delta[1] - dx + + b[dy:my, dx:mx] = a # center + b[:dy,dx:mx] = a[:dy,:][::-1,:] # top + b[my:,dx:mx] = a[-sy:,:][::-1,:] # bottom + b[dy:my,:dx] = a[:,:dx][:,::-1] # left + b[dy:my,mx:] = a[:,-sx:][:,::-1] # right + b[:dy,:dx] = a[:dy,:dx][::-1,::-1] # topleft + b[:dy,mx:] = a[:dy,-sx:][::-1,::-1] # topright + b[my:,:dx] = a[-sy:,:dx][::-1,::-1] # bottomleft + b[my:,mx:] = a[-sy:,-sx:][::-1,::-1] # bottomright + return b + +def frame_wrap(a, shape, cval=None): + """frame_wrap creates an oversized copy of 'a' with new 'shape' + and the contents of 'a' in the center. The boundary pixels are + wrapped around to the opposite edge pixels in 'a'. + + >>> a = num.arange(16, shape=(4,4)) + >>> frame_wrap(a, (8,8)) + array([[10, 11, 8, 9, 10, 11, 8, 9], + [14, 15, 12, 13, 14, 15, 12, 13], + [ 2, 3, 0, 1, 2, 3, 0, 1], + [ 6, 7, 4, 5, 6, 7, 4, 5], + [10, 11, 8, 9, 10, 11, 8, 9], + [14, 15, 12, 13, 14, 15, 12, 13], + [ 2, 3, 0, 1, 2, 3, 0, 1], + [ 6, 7, 4, 5, 6, 7, 4, 5]]) + + """ + + b = num.zeros(shape, typecode=a.type()) + delta = (num.array(b.shape) - num.array(a.shape)) + dy = delta[0] // 2 + dx = delta[1] // 2 + my = a.shape[0] + dy + mx = a.shape[1] + dx + sy = delta[0] - dy + sx = delta[1] - dx + + b[dy:my, dx:mx] = a # center + b[:dy,dx:mx] = a[-dy:,:] # top + b[my:,dx:mx] = a[:sy,:] # bottom + b[dy:my,:dx] = a[:,-dx:] # left + b[dy:my,mx:] = a[:, :sx] # right + b[:dy,:dx] = a[-dy:,-dx:] # topleft + b[:dy,mx:] = a[-dy:,:sx ] # topright + b[my:,:dx] = a[:sy, -dx:] # bottomleft + b[my:,mx:] = a[:sy, :sx] # bottomright + return b + +def frame_constant(a, shape, cval=0): + """frame_nearest creates an oversized copy of 'a' with new 'shape' + and the contents of 'a' in the center. The boundary pixels are + copied from the nearest edge pixel in 'a'. + + >>> a = num.arange(16, shape=(4,4)) + >>> frame_constant(a, (8,8), cval=42) + array([[42, 42, 42, 42, 42, 42, 42, 42], + [42, 42, 42, 42, 42, 42, 42, 42], + [42, 42, 0, 1, 2, 3, 42, 42], + [42, 42, 4, 5, 6, 7, 42, 42], + [42, 42, 8, 9, 10, 11, 42, 42], + [42, 42, 12, 13, 14, 15, 42, 42], + [42, 42, 42, 42, 42, 42, 42, 42], + [42, 42, 42, 42, 42, 42, 42, 42]]) + + """ + + b = num.zeros(shape, typecode=a.type()) + delta = (num.array(b.shape) - num.array(a.shape)) + dy = delta[0] // 2 + dx = delta[1] // 2 + my = a.shape[0] + dy + mx = a.shape[1] + dx + + b[dy:my, dx:mx] = a # center + b[:dy,dx:mx] = cval # top + b[my:,dx:mx] = cval # bottom + b[dy:my, :dx] = cval # left + b[dy:my, mx:] = cval # right + b[:dy, :dx] = cval # topleft + b[:dy, mx:] = cval # topright + b[my:, :dx] = cval # bottomleft + b[my:, mx:] = cval # bottomright + return b + +_frame_dispatch = { "nearest": frame_nearest, + "reflect": frame_reflect, + "wrap": frame_wrap, + "constant" : frame_constant } + +def frame(a, shape, mode="nearest", cval=0.0): + + """frame creates an oversized copy of 'a' with new 'shape', with + extra pixels being supplied according to IRAF boundary mode, + 'mode'. """ + + try: + f = _frame_dispatch[mode] + except KeyError: + raise ValueError('invalid IRAF boundary mode: "%s"' % mode) + + return f(a, shape, cval) + +def unframe(a, shape): + + """unframe extracts the center slice of framed array 'a' which had + 'shape' prior to framing.""" + + delta = num.array(a.shape) - num.array(shape) + dy = delta[0]//2 + dx = delta[1]//2 + my = shape[0] + dy + mx = shape[1] + dx + return a[dy:my, dx:mx] + +def test(): + import doctest, iraf_frame + return doctest.testmod(iraf_frame) + Added: trunk/Lib/sandbox/stsci/convolve/setup.py =================================================================== --- trunk/Lib/sandbox/stsci/convolve/setup.py 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/convolve/setup.py 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,24 @@ +#!/usr/bin/env python +import numpy + +def configuration(parent_package='',top_path=None): + from numpy.distutils.misc_util import Configuration + config = Configuration('convolve',parent_package,top_path, + package_path='lib', + author='Todd Miller', + author_email = 'help at stsci.edu', + description = 'image array convolution functions', + version = '0.1' + ) + + config.add_extension('_correlate', + sources=["src/_correlatemodule.c"], + define_macros = [('NUMPY', '1')], + include_dirs = [numpy.get_numarray_include()]) + return config + +if __name__ == "__main__": + from numpy.distutils.core import setup + config = configuration(top_path='').todict() + setup(**config) + Added: trunk/Lib/sandbox/stsci/convolve/src/_correlatemodule.c =================================================================== --- trunk/Lib/sandbox/stsci/convolve/src/_correlatemodule.c 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/convolve/src/_correlatemodule.c 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,687 @@ +#include "Python.h" + +#include +#include +#include +#include + +#include "numpy/libnumarray.h" + +typedef enum +{ + PIX_NEAREST, + PIX_REFLECT, + PIX_WRAP, + PIX_CONSTANT +} PixMode; + +typedef struct +{ + PixMode mode; + long rows, cols; + Float64 constval; + Float64 *data; +} PixData; + +static long +SlowCoord(long x, long maxx, PixMode m) +{ + switch(m) { + case PIX_NEAREST: + if (x < 0) x = 0; + if (x >= maxx) x = maxx-1; + return x; + case PIX_REFLECT: + if (x < 0) x = -x-1; + if (x >= maxx) x = maxx - (x - maxx) - 1; + return x; + case PIX_WRAP: + if (x < 0) x += maxx; + if (x >= maxx) x -= maxx; + return x; + case PIX_CONSTANT: /* handled in SlowPix, suppress warnings */ + break; + } + return x; +} + +static Float64 +SlowPix(long r, long c, PixData *p) +{ + long fr, fc; + if (p->mode == PIX_CONSTANT) { + if ((r < 0) || (r >= p->rows) || (c < 0) || (c >= p->cols)) + return p->constval; + else { + fr = r; + fc = c; + } + } else { + fr = SlowCoord(r, p->rows, p->mode); + fc = SlowCoord(c, p->cols, p->mode); + } + return p->data[fr*p->cols + fc]; +} + +static int +_reject_complex(PyObject *a) +{ + NumarrayType t; + if ((a == Py_None) || (a == NULL)) + return 0; + t = NA_NumarrayType(a); + if (t < 0) { + PyErr_Clear(); + return 0; + } + if (t == tComplex32 || t == tComplex64) { + PyErr_Format(PyExc_TypeError, + "function doesn't support complex arrays."); + return 1; + } + return 0; +} + +static void +Correlate1d(long ksizex, Float64 *kernel, + long dsizex, Float64 *data, + Float64 *correlated) +{ + long xc; + long halfk = ksizex/2; + + for(xc=0; xcnd != 1) || (data->nd != 1)) { + PyErr_Format(PyExc_ValueError, + "Correlate1d: numarray must have exactly 1 dimension."); + goto _fail; + } + + if (!NA_ShapeEqual(data, correlated)) { + PyErr_Format(PyExc_ValueError, + "Correlate1d: data and output must have identical length."); + goto _fail; + } + + Correlate1d(kernel->dimensions[0], NA_OFFSETDATA(kernel), + data->dimensions[0], NA_OFFSETDATA(data), + NA_OFFSETDATA(correlated)); + + Py_DECREF(kernel); + Py_DECREF(data); + + /* Align, Byteswap, Contiguous, Typeconvert */ + return NA_ReturnOutput(ocorrelated, correlated); + + _fail: + Py_XDECREF(kernel); + Py_XDECREF(data); + Py_XDECREF(correlated); + return NULL; +} + +/* SlowCorrelate computes 2D correlation near the boundaries of an array. +The output array shares the same dimensions as the input array, the latter +fully described by PixData. + +The region defined by rmin,rmax,cmin,cmax is assumed to contain only valid +coordinates. However, access to the input array is performed using SlowPix +because pixels reachable via "kernel offsets" may be at invalid coordinates. +*/ +static void +SlowCorrelate2d(long rmin, long rmax, long cmin, long cmax, + long krows, long kcols, Float64 *kernel, + PixData *pix, Float64 *output) +{ + long kr, kc, r, c; + long halfkrows = krows/2; + long halfkcols = kcols/2; + + for(r=rmin; rcols+c] = temp; + } + } +} + +static void +Correlate2d(long krows, long kcols, Float64 *kernel, + long drows, long dcols, Float64 *data, Float64 *correlated, + PixMode mode, Float64 cval) +{ + long ki, kj, di, dj; + long halfkrows = krows/2; + long halfkcols = kcols/2; + + PixData pix; + pix.mode = mode; + pix.data = data; + pix.constval = cval; + pix.rows = drows; + pix.cols = dcols; + + /* Compute the boundaries using SlowPix */ + + SlowCorrelate2d(0, halfkrows, 0, dcols, + krows, kcols, kernel, &pix, correlated); /* top */ + SlowCorrelate2d(drows-halfkrows, drows, 0, dcols, + krows, kcols, kernel, &pix, correlated); /* bottom */ + SlowCorrelate2d(halfkrows, drows-halfkrows, 0, halfkcols, + krows, kcols, kernel, &pix, correlated); /* left */ + SlowCorrelate2d(halfkrows, drows-halfkrows, dcols-halfkcols, dcols, + krows, kcols, kernel, &pix, correlated); /* right */ + + /* Correlate the center data using unchecked array access */ + for(di=halfkrows; di PIX_CONSTANT)) + return PyErr_Format(PyExc_ValueError, + "Correlate2d: mode value not in range(%d,%d)", + PIX_NEAREST, PIX_CONSTANT); + + /* Align, Byteswap, Contiguous, Typeconvert */ + kernel = NA_InputArray(okernel, tFloat64, C_ARRAY); + data = NA_InputArray(odata, tFloat64, C_ARRAY); + correlated = NA_OptionalOutputArray(ocorrelated, tFloat64, C_ARRAY, + data); + + if (!kernel || !data || !correlated) + goto _fail; + + if ((kernel->nd != 2) || (data->nd != 2) || (correlated->nd != 2)) { + PyErr_Format(PyExc_ValueError, "Correlate2d: inputs must have 2 dimensions."); + goto _fail; + } + + if (!NA_ShapeEqual(data, correlated)) { + PyErr_Format(PyExc_ValueError, + "Correlate2d: data and output numarray need identical shapes."); + goto _fail; + } + + if (_reject_complex(okernel) || _reject_complex(odata) || + _reject_complex(ocorrelated)) + goto _fail; + + Correlate2d(kernel->dimensions[0], kernel->dimensions[1], + NA_OFFSETDATA(kernel), + data->dimensions[0], data->dimensions[1], + NA_OFFSETDATA(data), + NA_OFFSETDATA(correlated), + mode, cval); + + Py_DECREF(kernel); + Py_DECREF(data); + + /* Align, Byteswap, Contiguous, Typeconvert */ + return NA_ReturnOutput(ocorrelated, correlated); + + _fail: + Py_XDECREF(kernel); + Py_XDECREF(data); + Py_XDECREF(correlated); + return NULL; +} + +void Shift2d( long rows, long cols, Float64 *data, long dx, long dy, Float64 *output, int mode, Float64 cval) +{ + long r, c; + PixData pix; + pix.mode = mode; + pix.constval = cval; + pix.rows = rows; + pix.cols = cols; + pix.data = data; + + for(r=0; r PIX_CONSTANT)) + return PyErr_Format(PyExc_ValueError, + "Shift2d: mode value not in range(%d,%d)", + PIX_NEAREST, PIX_CONSTANT); + + /* Align, Byteswap, Contiguous, Typeconvert */ + data = NA_InputArray(odata, tFloat64, C_ARRAY); + output = NA_OptionalOutputArray(ooutput, tFloat64, C_ARRAY, + data); + + if (!data || !output) + goto _fail; + + if (_reject_complex(odata) || _reject_complex(ooutput)) + goto _fail; + + if ((data->nd != 2)) { + PyErr_Format(PyExc_ValueError, + "Shift2d: numarray must have 2 dimensions."); + goto _fail; + } + + if (!NA_ShapeEqual(data, output)) { + PyErr_Format(PyExc_ValueError, + "Shift2d: data and output numarray need identical shapes."); + goto _fail; + } + + /* Invert sign of deltas to match sense of 2x2 correlation. */ + Shift2d( data->dimensions[0], data->dimensions[1], NA_OFFSETDATA(data), + -dx, -dy, NA_OFFSETDATA(output), mode, cval); + + Py_XDECREF(data); + + /* Align, Byteswap, Contiguous, Typeconvert */ + return NA_ReturnOutput(ooutput, output); + _fail: + Py_XDECREF(data); + Py_XDECREF(output); + return NULL; +} + +typedef struct s_BoxData BoxData; + +typedef Float64 (*SumColFunc)(long,long,BoxData*); +typedef Float64 (*SumBoxFunc)(long,long,BoxData*); + +struct s_BoxData { + PixData pix; + long krows, kcols; + SumColFunc sumcol; + SumBoxFunc sumbox; +}; + +static Float64 +SlowSumCol(long r, long c, BoxData *D) +{ + Float64 sum = 0; + long i, krows = D->krows; + for(i=0; ipix); + } + return sum; +} + +static Float64 +SlowSumBox(long r, long c, BoxData *D) +{ + long i, j; + Float64 sum = 0; + for(i=0; ikrows; i++) + for(j=0; jkcols; j++) + sum += SlowPix(r+i, c+j, &D->pix); + return sum; +} + +static Float64 +FastSumCol(long r, long c, BoxData *D) +{ + Float64 sum = 0; + long krows = D->krows; + long cols = D->pix.cols; + Float64 *data = D->pix.data; + + data += r*cols + c; + for(; krows--; data += cols) { + sum += *data; + } + return sum; +} + +static Float64 +FastSumBox(long r, long c, BoxData *D) +{ + long i, j; + Float64 sum = 0; + long cols = D->pix.cols; + Float64 *data = D->pix.data; + data += r*cols + c; + for(i=0; ikrows; i++, data += cols-D->kcols) + for(j=0; jkcols; j++, data++) + sum += *data; + return sum; +} + +static long bound(long x, long max) +{ + if (x < 0) return 0; + else if (x > max) return max; + else return x; +} + +static void +BoxFunc(long rmin, long rmax, long cmin, long cmax, Float64 *output, BoxData *D) +{ + long r, c; + long krows2 = D->krows/2; + long kcols2 = D->kcols/2; + long kcolseven = !(D->kcols & 1); + long rows = D->pix.rows; + long cols = D->pix.cols; + + rmin = bound(rmin, rows); + rmax = bound(rmax, rows); + cmin = bound(cmin, cols); + cmax = bound(cmax, cols); + + for(r=rmin; rsumbox(r - krows2, cmin - kcols2, D); + for(c=cmin; csumcol(r - krows2, c - kcols2, D); + sum += D->sumcol(r - krows2, c + kcols2 - kcolseven + 1, D); + } + } +} + +/* BoxFuncI computes a boxcar incrementally, using a formula independent of + the size of the boxcar. Each incremental step is based on dropping a + whole column of the "back" of the boxcar, and adding in a new column in + the "front". The sums of these columns are further optimized by realizing + they can be computed from their counterparts one element above by adding in + bottom corners and subtracting out top corners. + + incremental pixel layout: B C where S is the unknown, and A, B, C are known neighbors + A S each of these refers to the output array + + S = A + a1 - a0 where a0 and a1 are column vectors with *bottom* elements { a, d } + C - B = b1 - b0 where b0 and b1 are column vectors with *top* elements { b, g } + column vectors and corner elements refer to the input array + + offset matrix layout: b g where b is actually in b0 + [b0] [b1] g " b1 + [a0] S [a1] a " a0 + a d d is actually in a1 + + a0 = b0 - b + a column vector a0 is b0 dropping top element b and adding bottom a + a1 = b1 - g + d column vector a1 is b1 dropping top element g and adding bottom d + + S = A + (b1 - g + f) - (b0 - b + a) by substitution + S = A + (b1 - b0) - g + d + b - a rearranging additions + S = A + C - B - g + d + b - a by substitution +*/ +static void +BoxFuncI(long rmin, long rmax, long cmin, long cmax, Float64 *output, BoxData *D) +{ + long r, c; + long krows2 = D->krows/2; + long kcols2 = D->kcols/2; + long krowseven = !(D->krows & 1); + long kcolseven = !(D->kcols & 1); + long rows = D->pix.rows; + long cols = D->pix.cols; + Float64 *input = D->pix.data; + + rmin = bound(rmin, rows); + rmax = bound(rmax, rows); + cmin = bound(cmin, cols); + cmax = bound(cmax, cols); + + for(r=rmin; r 0."); + goto _fail; + } + + if ((mode < PIX_NEAREST) || (mode > PIX_CONSTANT)) { + PyErr_Format(PyExc_ValueError, + "Boxcar2d: mode value not in range(%d,%d)", + PIX_NEAREST, PIX_CONSTANT); + goto _fail; + } + + if ((data->nd != 2)|| (output->nd != 2)) { + PyErr_Format(PyExc_ValueError, + "Boxcar2d: numarray must have 2 dimensions."); + goto _fail; + } + + if (!NA_ShapeEqual(data, output)) { + PyErr_Format(PyExc_ValueError, + "Boxcar2d: data and output numarray need identical shapes."); + goto _fail; + } + + if ((kcols <=0) || (krows <= 0)) { + PyErr_Format(PyExc_ValueError, + "Boxcar2d: invalid data shape."); + goto _fail; + } + if ((kcols > data->dimensions[1]) || (krows > data->dimensions[0])) { + PyErr_Format(PyExc_ValueError, "Boxcar2d: boxcar shape incompatible with" + " data shape."); + goto _fail; + } + + Boxcar2d(krows, kcols, data->dimensions[0], data->dimensions[1], + NA_OFFSETDATA(data), NA_OFFSETDATA(output), mode, cval); + + Py_XDECREF(data); + + /* Align, Byteswap, Contiguous, Typeconvert */ + return NA_ReturnOutput(ooutput, output); + _fail: + Py_XDECREF(data); + Py_XDECREF(output); + return NULL; +} + +static PyMethodDef _correlateMethods[] = { + {"Correlate1d", Py_Correlate1d, METH_VARARGS}, + {"Correlate2d", (PyCFunction) Py_Correlate2d, METH_VARARGS | METH_KEYWORDS}, + {"Shift2d", (PyCFunction) Py_Shift2d, METH_VARARGS | METH_KEYWORDS, + "Shift2d shifts and image by an integer number of pixels, and uses IRAF compatible modes for the boundary pixels."}, + {"Boxcar2d", (PyCFunction) Py_Boxcar2d, METH_VARARGS | METH_KEYWORDS, + "Boxcar2d computes a sliding 2D boxcar average on a 2D array"}, + {NULL, NULL} /* Sentinel */ +}; + +/* platform independent*/ +#ifdef MS_WIN32 +__declspec(dllexport) +#endif + +void init_correlate(void) +{ + PyObject *m, *d; + m = Py_InitModule("_correlate", _correlateMethods); + d = PyModule_GetDict(m); + import_libnumarray(); +} + +/* + * Local Variables: + * mode: C + * c-file-style: "python" + * End: + */ Property changes on: trunk/Lib/sandbox/stsci/convolve/src/_correlatemodule.c ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/Lib/sandbox/stsci/image/lib/__init__.py =================================================================== --- trunk/Lib/sandbox/stsci/image/lib/__init__.py 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/image/lib/__init__.py 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,29 @@ +import sys +from _image import * +from combine import * + + +if sys.version_info < (2,4): + def test(): + import doctest, _image, combine + + t = doctest.Tester(globs = globals()) + + t.rundict(_image.__dict__, "_image") + t.rundict(combine.__dict__, "combine") + + return t.summarize() + +else: + def test(): + import doctest, _image, combine + + finder=doctest.DocTestFinder() + tests=finder.find(_image) + tests.extend(finder.find(combine)) + + runner=doctest.DocTestRunner(verbose=False) + + for test in tests: + runner.run(test) + return runner.summarize() Added: trunk/Lib/sandbox/stsci/image/lib/_image.py =================================================================== --- trunk/Lib/sandbox/stsci/image/lib/_image.py 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/image/lib/_image.py 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,62 @@ +import numpy as num +import convolve +import convolve._correlate as _correlate +import numpy.linalg.linalg as MLab + +def _translate(a, dx, dy, output=None, mode="nearest", cval=0.0): + """_translate does positive sub-pixel shifts using bilinear interpolation.""" + + assert 0 <= dx < 1.0 + assert 0 <= dy < 1.0 + + w = (1-dy) * (1-dx) + x = (1-dy) * dx + y = (1-dx) * dy + z = dx * dy + + kernel = num.array([ + [ z, y ], + [ x, w ], + ]) + + return convolve.correlate2d(a, kernel, output, mode, cval) + +def translate(a, sdx, sdy, output=None, mode="nearest", cval=0.0): + """translate performs a translation of 'a' by (sdx, sdy) + storing the result in 'output'. + + sdx, sdy are float values. + + supported 'mode's include: + 'nearest' elements beyond boundary come from nearest edge pixel. + 'wrap' elements beyond boundary come from the opposite array edge. + 'reflect' elements beyond boundary come from reflection on same array edge. + 'constant' elements beyond boundary are set to 'cval' + """ + a = num.asarray(a) + + sdx, sdy = -sdx, -sdy # Flip sign to match IRAF sign convention + + # _translate works "backwords" due to implementation of 2x2 correlation. + if sdx >= 0 and sdy >= 0: + rotation = 2 + dx, dy = abs(sdx), abs(sdy) + elif sdy < 0 and sdx >= 0: + rotation = 1 + dx, dy = abs(sdy), abs(sdx) + elif sdx < 0 and sdy >= 0: + rotation = 3 + dx, dy = abs(sdy), abs(sdx) + elif sdx < 0 and sdy < 0: + rotation = 0 + dx, dy = abs(sdx), abs(sdy) + + b = MLab.rot90(a, rotation) + c = _correlate.Shift2d(b, int(dx), int(dy), + mode=convolve.pix_modes[mode]) + d = _translate(c, dx % 1, dy % 1, output, mode, cval) + if output is not None: + output._copyFrom(MLab.rot90(output, -rotation%4)) + else: + return MLab.rot90(d, -rotation % 4).astype(a.type()) + Added: trunk/Lib/sandbox/stsci/image/lib/combine.py =================================================================== --- trunk/Lib/sandbox/stsci/image/lib/combine.py 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/image/lib/combine.py 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,269 @@ +import numpy as num +from _combine import combine as _comb +import operator as _operator + + +def _combine_f(funcstr, arrays, output=None, outtype=None, nlow=0, nhigh=0, badmasks=None): + arrays = [ num.asarray(a) for a in arrays ] + shape = arrays[0].shape + if output is None: + if outtype is not None: + out = arrays[0].astype(outtype) + else: + out = arrays[0].copy() + else: + out = output + for a in tuple(arrays[1:])+(out,): + if a.shape != shape: + raise ValueError("all arrays must have identical shapes") + _comb(arrays, out, nlow, nhigh, badmasks, funcstr) + if output is None: + return out + +def median( arrays, output=None, outtype=None, nlow=0, nhigh=0, badmasks=None): + """median() nominally computes the median pixels for a stack of + identically shaped images. + + arrays specifies a sequence of inputs arrays, which are nominally a + stack of identically shaped images. + + output may be used to specify the output array. If none is specified, + either arrays[0] is copied or a new array of type 'outtype' + is created. + + outtype specifies the type of the output array when no 'output' is + specified. + + nlow specifies the number of pixels to be excluded from median + on the low end of the pixel stack. + + nhigh specifies the number of pixels to be excluded from median + on the high end of the pixel stack. + + badmasks specifies boolean arrays corresponding to 'arrays', where true + indicates that a particular pixel is not to be included in the + median calculation. + + >>> a = num.arange(4) + >>> a = a.reshape((2,2)) + >>> arrays = [a*16, a*4, a*2, a*8] + >>> median(arrays) + array([[ 0, 6], + [12, 18]]) + >>> median(arrays, nhigh=1) + array([[ 0, 4], + [ 8, 12]]) + >>> median(arrays, nlow=1) + array([[ 0, 8], + [16, 24]]) + >>> median(arrays, outtype=num.float32) + array([[ 0., 6.], + [ 12., 18.]], dtype=float32) + >>> bm = num.zeros((4,2,2), dtype=num.bool8) + >>> bm[2,...] = 1 + >>> median(arrays, badmasks=bm) + array([[ 0, 8], + [16, 24]]) + >>> median(arrays, badmasks=threshhold(arrays, high=25)) + array([[ 0, 6], + [ 8, 12]]) + """ + return _combine_f("median", arrays, output, outtype, nlow, nhigh, badmasks) + +def average( arrays, output=None, outtype=None, nlow=0, nhigh=0, badmasks=None): + """average() nominally computes the average pixel value for a stack of + identically shaped images. + + arrays specifies a sequence of inputs arrays, which are nominally a + stack of identically shaped images. + + output may be used to specify the output array. If none is specified, + either arrays[0] is copied or a new array of type 'outtype' + is created. + + outtype specifies the type of the output array when no 'output' is + specified. + + nlow specifies the number of pixels to be excluded from average + on the low end of the pixel stack. + + nhigh specifies the number of pixels to be excluded from average + on the high end of the pixel stack. + + badmasks specifies boolean arrays corresponding to 'arrays', where true + indicates that a particular pixel is not to be included in the + average calculation. + + >>> a = num.arange(4) + >>> a = a.reshape((2,2)) + >>> arrays = [a*16, a*4, a*2, a*8] + >>> average(arrays) + array([[ 0, 7], + [15, 22]]) + >>> average(arrays, nhigh=1) + array([[ 0, 4], + [ 9, 14]]) + >>> average(arrays, nlow=1) + array([[ 0, 9], + [18, 28]]) + >>> average(arrays, outtype=num.float32) + array([[ 0. , 7.5], + [ 15. , 22.5]], dtype=float32) + >>> bm = num.zeros((4,2,2), dtype=num.bool8) + >>> bm[2,...] = 1 + >>> average(arrays, badmasks=bm) + array([[ 0, 9], + [18, 28]]) + >>> average(arrays, badmasks=threshhold(arrays, high=25)) + array([[ 0, 7], + [ 9, 14]]) + + """ + return _combine_f("average", arrays, output, outtype, nlow, nhigh, badmasks) + +def minimum( arrays, output=None, outtype=None, nlow=0, nhigh=0, badmasks=None): + """minimum() nominally computes the minimum pixel value for a stack of + identically shaped images. + + arrays specifies a sequence of inputs arrays, which are nominally a + stack of identically shaped images. + + output may be used to specify the output array. If none is specified, + either arrays[0] is copied or a new array of type 'outtype' + is created. + + outtype specifies the type of the output array when no 'output' is + specified. + + nlow specifies the number of pixels to be excluded from minimum + on the low end of the pixel stack. + + nhigh specifies the number of pixels to be excluded from minimum + on the high end of the pixel stack. + + badmasks specifies boolean arrays corresponding to 'arrays', where true + indicates that a particular pixel is not to be included in the + minimum calculation. + + >>> a = num.arange(4) + >>> a = a.reshape((2,2)) + >>> arrays = [a*16, a*4, a*2, a*8] + >>> minimum(arrays) + array([[0, 2], + [4, 6]]) + >>> minimum(arrays, nhigh=1) + array([[0, 2], + [4, 6]]) + >>> minimum(arrays, nlow=1) + array([[ 0, 4], + [ 8, 12]]) + >>> minimum(arrays, outtype=num.float32) + array([[ 0., 2.], + [ 4., 6.]], dtype=float32) + >>> bm = num.zeros((4,2,2), dtype=num.bool8) + >>> bm[2,...] = 1 + >>> minimum(arrays, badmasks=bm) + array([[ 0, 4], + [ 8, 12]]) + >>> minimum(arrays, badmasks=threshhold(arrays, low=10)) + array([[ 0, 16], + [16, 12]]) + + """ + return _combine_f("minimum", arrays, output, outtype, nlow, nhigh, badmasks) + +def threshhold(arrays, low=None, high=None, outputs=None): + """threshhold() computes a boolean array 'outputs' with + corresponding elements for each element of arrays. The + boolean value is true where each of the arrays values + is < the low or >= the high threshholds. + + >>> a=num.arange(100) + >>> a=a.reshape((10,10)) + >>> (threshhold(a, 1, 50)).astype(num.int8) + array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8) + >>> (threshhold([ range(10)]*10, 3, 7)).astype(num.int8) + array([[1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 0, 0, 1, 1, 1]], dtype=int8) + >>> (threshhold(a, high=50)).astype(num.int8) + array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8) + >>> (threshhold(a, low=50)).astype(num.int8) + array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int8) + + """ + + if not isinstance(arrays[0], num.ndarray): + return threshhold( num.asarray(arrays), low, high, outputs) + + if outputs is None: + outs = num.zeros(shape=(len(arrays),)+arrays[0].shape, + dtype=num.bool8) + else: + outs = outputs + + for i in range(len(arrays)): + a, out = arrays[i], outs[i] + out[:] = 0 + + if high is not None: + num.greater_equal(a, high, out) + if low is not None: + num.logical_or(out, a < low, out) + else: + if low is not None: + num.less(a, low, out) + + if outputs is None: + return outs + +def _bench(): + """time a 10**6 element median""" + import time + a = num.arange(10**6) + a = a.reshape((1000, 1000)) + arrays = [a*2, a*64, a*16, a*8] + t0 = time.clock() + median(arrays) + print "maskless:", time.clock()-t0 + + a = num.arange(10**6) + a = a.reshape((1000, 1000)) + arrays = [a*2, a*64, a*16, a*8] + t0 = time.clock() + median(arrays, badmasks=num.zeros((1000,1000), dtype=num.bool8)) + print "masked:", time.clock()-t0 Added: trunk/Lib/sandbox/stsci/image/setup.py =================================================================== --- trunk/Lib/sandbox/stsci/image/setup.py 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/image/setup.py 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,23 @@ +#!/usr/bin/env python +import numpy + +def configuration(parent_package='',top_path=None): + from numpy.distutils.misc_util import Configuration + config = Configuration('image',parent_package,top_path, + package_path='lib', + author='Todd Miller', + author_email = 'help at stsci.edu', + description = 'image array manipulation functions', + version = '0.1' + ) + config.add_extension('_combine', + sources=["src/_combinemodule.c"], + define_macros = [('NUMPY', '1')], + include_dirs = [numpy.get_numarray_include()]) + return config + +if __name__ == "__main__": + from numpy.distutils.core import setup + config = configuration(top_path='').todict() + setup(**config) + Property changes on: trunk/Lib/sandbox/stsci/image/setup.py ___________________________________________________________________ Name: svn:executable + * Added: trunk/Lib/sandbox/stsci/image/src/_combinemodule.c =================================================================== --- trunk/Lib/sandbox/stsci/image/src/_combinemodule.c 2006-07-19 13:17:15 UTC (rev 2113) +++ trunk/Lib/sandbox/stsci/image/src/_combinemodule.c 2006-07-20 06:09:14 UTC (rev 2114) @@ -0,0 +1,247 @@ +#include "Python.h" + +#include +#include +#include +#include + +#include "numpy/libnumarray.h" + +#define MAX_ARRAYS 1024 + +static PyObject *_Error; + +typedef Float64 (*combiner)(int, int, int, Float64 temp[MAX_ARRAYS]); + + +static int +_mask_and_sort(int ninputs, int index, Float64 **inputs, Bool **masks, + Float64 temp[MAX_ARRAYS]) +{ + int i, j, goodpix; + if (masks) { + for (i=j=0; idimensions[dim]; + + /* Allocate and convert 1 temporary row at a time */ + for(i=0; idata; + if (masks) { + for(i=0; idata; + } + toutput = (Float64 *) output->data; + + for(j=0; jdimensions[dim]; i++) { + for(j=0; jdata += inputs[j]->strides[dim]*i; + if (masks) { + masks[j]->data += masks[j]->strides[dim]*i; + } + } + output->data += output->strides[dim]*i; + _combine(f, dim+1, maxdim, ninputs, nlow, nhigh, + inputs, masks, output); + for(j=0; jdata -= inputs[j]->strides[dim]*i; + if (masks) { + masks[j]->data -= masks[j]->strides[dim]*i; + } + } + output->data -= output->strides[dim]*i; + } + } + return 0; +} + +typedef struct +{ + char *name; + combiner fptr; +} fmapping; + +static fmapping functions[] = { + {"median", _inner_median}, + {"average", _inner_average}, + {"minimum", _inner_minimum}, +}; + + +static PyObject * +_Py_combine(PyObject *obj, PyObject *args, PyObject *kw) +{ + PyObject *arrays, *output; + int nlow=0, nhigh=0, narrays; + PyObject *badmasks=Py_None; + char *keywds[] = { "arrays", "output", "nlow", "nhigh", + "badmasks", "kind", NULL }; + char *kind; + combiner f; + PyArrayObject *arr[MAX_ARRAYS], *bmk[MAX_ARRAYS], *toutput; + int i; + + if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|iiOs:combine", keywds, + &arrays, &output, &nlow, &nhigh, &badmasks, &kind)) + return NULL; + + narrays = PySequence_Length(arrays); + if (narrays < 0) + return PyErr_Format( + PyExc_TypeError, "combine: arrays is not a sequence"); + if (narrays > MAX_ARRAYS) + return PyErr_Format( + PyExc_TypeError, "combine: too many arrays."); + + for(i=0; ind, narrays, nlow, nhigh, + arr, (badmasks != Py_None ? bmk : NULL), + toutput) < 0) + return NULL; + + for(i=0; i Author: oliphant Date: 2006-07-20 01:15:12 -0500 (Thu, 20 Jul 2006) New Revision: 2115 Added: trunk/Lib/sandbox/stsci/__init__.py Modified: trunk/Lib/sandbox/stsci/image/lib/_image.py Log: Fix up stsci package. Added: trunk/Lib/sandbox/stsci/__init__.py =================================================================== Modified: trunk/Lib/sandbox/stsci/image/lib/_image.py =================================================================== --- trunk/Lib/sandbox/stsci/image/lib/_image.py 2006-07-20 06:09:14 UTC (rev 2114) +++ trunk/Lib/sandbox/stsci/image/lib/_image.py 2006-07-20 06:15:12 UTC (rev 2115) @@ -1,7 +1,7 @@ import numpy as num -import convolve -import convolve._correlate as _correlate -import numpy.linalg.linalg as MLab +import scipy.sandbox.stsci.convolve as convolve +import scipy.sandbox.stsci.convolve._correlate as _correlate +MLab=num def _translate(a, dx, dy, output=None, mode="nearest", cval=0.0): """_translate does positive sub-pixel shifts using bilinear interpolation.""" From scipy-svn at scipy.org Fri Jul 21 09:56:27 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 21 Jul 2006 08:56:27 -0500 (CDT) Subject: [Scipy-svn] r2116 - in trunk/Lib/weave: examples scxx Message-ID: <20060721135627.B91B839C00C@new.scipy.org> Author: stefan Date: 2006-07-21 08:56:13 -0500 (Fri, 21 Jul 2006) New Revision: 2116 Modified: trunk/Lib/weave/examples/binary_search.py trunk/Lib/weave/examples/dict_sort.py trunk/Lib/weave/examples/object.py trunk/Lib/weave/scxx/object.h trunk/Lib/weave/scxx/sequence.h Log: Fixes in weave, contributed by Pierre Barbier. Modified: trunk/Lib/weave/examples/binary_search.py =================================================================== --- trunk/Lib/weave/examples/binary_search.py 2006-07-20 06:15:12 UTC (rev 2115) +++ trunk/Lib/weave/examples/binary_search.py 2006-07-21 13:56:13 UTC (rev 2116) @@ -19,7 +19,7 @@ sys.path.insert(0,'..') #from compiler import inline_tools import scipy.weave.inline_tools as inline_tools -from bisect import bisect +from bisect import bisect_left as bisect import types def c_int_search(seq,t,chk=1): Modified: trunk/Lib/weave/examples/dict_sort.py =================================================================== --- trunk/Lib/weave/examples/dict_sort.py 2006-07-20 06:15:12 UTC (rev 2115) +++ trunk/Lib/weave/examples/dict_sort.py 2006-07-21 13:56:13 UTC (rev 2116) @@ -46,7 +46,9 @@ keys.sort(); int N = keys.length(); for(int i = 0; i < N;i++) - items[i] = adict[keys[i]]; + { + items[i] = adict[int( keys[i] )]; + } return_val = items; """ return inline_tools.inline(code,['adict'],verbose=1) Modified: trunk/Lib/weave/examples/object.py =================================================================== --- trunk/Lib/weave/examples/object.py 2006-07-20 06:15:12 UTC (rev 2115) +++ trunk/Lib/weave/examples/object.py 2006-07-21 13:56:13 UTC (rev 2116) @@ -41,7 +41,7 @@ // find obj length and accesss each of its items std::cout << "UserList items: "; for(i = 0; i < obj.length(); i++) - std::cout << obj[i] << " "; + std::cout << obj[i].str() << " "; std::cout << std::endl; // assign new values to each of its items for(i = 0; i < obj.length(); i++) Modified: trunk/Lib/weave/scxx/object.h =================================================================== --- trunk/Lib/weave/scxx/object.h 2006-07-20 06:15:12 UTC (rev 2115) +++ trunk/Lib/weave/scxx/object.h 2006-07-21 13:56:13 UTC (rev 2116) @@ -726,7 +726,7 @@ }; object is_tuple() const { - return PyDict_Check(_obj) == 1; + return PyTuple_Check(_obj) == 1; }; object is_dict() const { Modified: trunk/Lib/weave/scxx/sequence.h =================================================================== --- trunk/Lib/weave/scxx/sequence.h 2006-07-20 06:15:12 UTC (rev 2115) +++ trunk/Lib/weave/scxx/sequence.h 2006-07-21 13:56:13 UTC (rev 2116) @@ -257,6 +257,10 @@ object oth = other; return operator=(oth); }; + indexed_ref& operator=( const indexed_ref& other ) { + object oth = other; + return operator=( oth ); + } }; From scipy-svn at scipy.org Mon Jul 24 04:21:01 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 24 Jul 2006 03:21:01 -0500 (CDT) Subject: [Scipy-svn] r2117 - trunk/Lib/linsolve Message-ID: <20060724082101.19A3A39C099@new.scipy.org> Author: rc Date: 2006-07-24 03:20:57 -0500 (Mon, 24 Jul 2006) New Revision: 2117 Modified: trunk/Lib/linsolve/linsolve.py Log: spsolve() fix for default solver Modified: trunk/Lib/linsolve/linsolve.py =================================================================== --- trunk/Lib/linsolve/linsolve.py 2006-07-21 13:56:13 UTC (rev 2116) +++ trunk/Lib/linsolve/linsolve.py 2006-07-24 08:20:57 UTC (rev 2117) @@ -65,8 +65,11 @@ else: mat, csc = _toCS_superLU( A ) - ftype, lastel, data, index0, index1 = \ - mat.ftype, mat.nnz, mat.data, mat.rowind, mat.indptr + if csc: + index0 = mat.rowind + else: + index0 = mat.colind + ftype, lastel, data, index1 = mat.ftype, mat.nnz, mat.data, mat.indptr gssv = eval('_superlu.' + ftype + 'gssv') print "data-ftype: %s compared to data %s" % (ftype, data.dtype.char) print "Calling _superlu.%sgssv" % ftype From scipy-svn at scipy.org Mon Jul 24 12:27:57 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 24 Jul 2006 11:27:57 -0500 (CDT) Subject: [Scipy-svn] r2118 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060724162757.D5C1339C0A3@new.scipy.org> Author: fullung Date: 2006-07-24 11:27:34 -0500 (Mon, 24 Jul 2006) New Revision: 2118 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/libsvm.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/predict.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_all.py trunk/Lib/sandbox/svm/tests/test_precomputed.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Prediction refactoring in progress. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -11,7 +11,7 @@ ] class LibSvmClassificationResults: - def __init__(self, model, traindataset, PredictorType): + def __init__(self, model, traindataset, kernel, PredictorType): modelc = model.contents self.nr_class = modelc.nr_class self.labels = modelc.labels[:self.nr_class] @@ -22,14 +22,14 @@ for i, c in enumerate(modelc.sv_coef[:self.nr_class - 1]): sv_coef[i,:] = c[:modelc.l] self.sv_coef = sv_coef - self.predictor = PredictorType(model, traindataset) + self.predictor = PredictorType(model, traindataset, kernel) def predict(self, dataset): """ This function does classification on a test vector x and returns the label of the predicted class. """ - return [self.predictor.predict(x) for x in dataset] + return [int(self.predictor.predict(x)) for x in dataset] def predict_values(self, dataset): """ @@ -68,7 +68,7 @@ n = self.nr_class label, prob_estimates = \ self.predictor.predict_probability(x, self.nr_class) - return label, prob_estimates + return int(label), prob_estimates return [p(x) for x in dataset] class LibSvmClassificationModel(LibSvmModel): Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -69,6 +69,9 @@ grammat[i][j + 1] = 0, z grammat[j][i + 1] = 0, z + def __getitem__(self, id): + return self.iddatamap[id] + def getdata(self): return zip(map(lambda x: x[0], self.origdata), self.grammat) data = property(getdata) Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -12,7 +12,7 @@ svm_node_dtype = \ N.dtype({'names' : ['index', 'value'], 'formats' : [N.intc, N.float64]}, - align=1) + align=True) # svm types C_SVC = 0 Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/model.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -48,7 +48,7 @@ dataset._update_svm_parameter(self.param) self._check_problem_param(problem, self.param) model = libsvm.svm_train(problem, self.param) - return ResultType(model, dataset, PredictorType) + return ResultType(model, dataset, self.kernel, PredictorType) def _check_problem_param(self, problem, param): error_msg = libsvm.svm_check_parameter(problem, param) Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -7,11 +7,11 @@ ] class LibSvmOneClassResults: - def __init__(self, model, traindataset, PredictorType): + def __init__(self, model, traindataset, kernel, PredictorType): modelc = model.contents self.rho = modelc.rho[0] self.sv_coef = modelc.sv_coef[0][:modelc.l] - self.predictor = PredictorType(model, traindataset) + self.predictor = PredictorType(model, traindataset, kernel) def predict(self, dataset): """ Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -1,18 +1,18 @@ from ctypes import cast, POINTER, c_double import numpy as N +from dataset import svm_node_dot import libsvm __all__ = [ 'LibSvmPredictor', 'LibSvmPrecomputedPredictor', - 'LibSvmSparsePredictor', - 'PrecomputedSparsePredictor', - 'DensePredictor' + 'LibSvmPyPredictor', + 'LibSvmPrecomputedPyPredictor' ] class LibSvmPredictor: - def __init__(self, model, dataset): + def __init__(self, model, dataset, kernel): self.model = model def __del__(self): @@ -37,44 +37,91 @@ return label, pe class LibSvmPrecomputedPredictor: - def __init__(self, model, dataset): - raise NotImplementedError + def __init__(self, model, dataset, kernel): + self.kernel = kernel + self.model = model + modelc = model.contents + ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] + support_vectors = [dataset[id] for id in ids] + self.support_vectors = support_vectors + # fix support vector ids in precomputed data + for i in range(modelc.l): + modelc.SV[i][0].value = i - def predict(self): - raise NotImplementedError + def __del__(self): + libsvm.svm_destroy_model(self.model) - def predict_values(self): - raise NotImplementedError + def _create_gramvec(self, x): + gramvec = N.zeros((self.model.contents.l,), + dtype=libsvm.svm_node_dtype) + for i, sv in enumerate(self.support_vectors): + gramvec[i]['value'] = self.kernel(x, sv, svm_node_dot) + return gramvec - def predict_probability(self): - raise NotImplementedError + def predict(self, x): + g = self._create_gramvec(x) + gptr = cast(g.ctypes.data, POINTER(libsvm.svm_node)) + return libsvm.svm_predict(self.model, gptr) -class LibSvmSparsePredictor: - def __init__(self, model, dataset): - raise NotImplementedError + def predict_values(self, x, n): + g = self._create_gramvec(x) + gptr = cast(g.ctypes.data, POINTER(libsvm.svm_node)) + v = N.empty((n,), dtype=N.float64) + vptr = cast(v.ctypes.data, POINTER(c_double)) + libsvm.svm_predict(self.model, gptr, vptr) + return v - def predict_values(self): - raise NotImplementedError + def predict_probability(self, x, n): + g = self._create_gramvec(x) + gptr = cast(g.ctypes.data, POINTER(libsvm.svm_node)) + pe = N.empty((n,), dtype=N.float64) + peptr = cast(pe.ctypes.data, POINTER(c_double)) + label = libsvm.svm_predict_probability(self.model, gptr, peptr) + return label, pe - def predict_probability(self): +class LibSvmPyPredictor: + def __init__(self, model, dataset, kernel): + self.kernel = kernel + modelc = model.contents + support_vectors = [modelc.SV[i] for i in range(modelc.l)] + # XXX need to map back from these support vector pointers to + # the original numpy arrays raise NotImplementedError + #libsvm.svm_destroy_model(model) -class PrecomputedSparsePredictor: - def __init__(self, model, dataset): + def predict(self, x): raise NotImplementedError - def predict_values(self): - raise NotImplementedError + def predict_values(self, x, n): + raise NotImplementedError - def predict_probability(self): - raise NotImplementedError + def predict_probability(self, x, n): + raise NotImplementedError -class DensePredictor: - def __init__(self, model, dataset): - raise NotImplementedError +class LibSvmPrecomputedPyPredictor: + def __init__(self, model, dataset, kernel): + self.kernel = kernel + modelc = model.contents - def predict_values(self): - raise NotImplementedError + # XXX regression-only hack for now + self.rho = modelc.rho[0] + self.sv_coef = modelc.sv_coef[0][:modelc.l] - def predict_probability(self): + ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] + support_vectors = [dataset[id] for id in ids] + self.support_vectors = support_vectors + libsvm.svm_destroy_model(model) + + def predict(self, x): + # XXX regression-only hack for now + return self.predict_values(x, 1) + + def predict_values(self, x, n): + z = -self.rho + # XXX possible optimization: izip + for sv_coef, sv in zip(self.sv_coef, self.support_vectors): + z += sv_coef * self.kernel(x, sv, svm_node_dot) + return z + + def predict_probability(self, x, n): raise NotImplementedError Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -13,13 +13,13 @@ # XXX document why get_svr_probability could be useful class LibSvmRegressionResults: - def __init__(self, model, traindataset, PredictorType): + def __init__(self, model, traindataset, kernel, PredictorType): modelc = model.contents self.rho = modelc.rho[0] self.sv_coef = modelc.sv_coef[0][:modelc.l] if modelc.probA: self.sigma = modelc.probA[0] - self.predictor = PredictorType(model, traindataset) + self.predictor = PredictorType(model, traindataset, kernel) def predict(self, dataset): """ Modified: trunk/Lib/sandbox/svm/tests/test_all.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -1,9 +1,9 @@ -from test_regression import * from test_classification import * -from test_dataset import * +#from test_dataset import * +from test_libsvm import * from test_oneclass import * -from test_libsvm import * from test_precomputed import * +from test_regression import * if __name__ == '__main__': NumpyTest().run() Modified: trunk/Lib/sandbox/svm/tests/test_precomputed.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -2,14 +2,72 @@ import numpy as N set_local_path('../..') +from svm.classification import * from svm.dataset import * -from svm.kernel import LinearKernel +from svm.kernel import * from svm.predict import * from svm.regression import * restore_path() class test_precomputed(NumpyTestCase): - def check_precomputed(self): + def _create_regression_data(self): + y1 = N.random.randn(50) + x1 = N.random.randn(len(y1), 10) + data1 = LibSvmRegressionDataSet(zip(y1, x1)) + pcdata1 = data1.precompute(kernel) + y2 = N.random.randn(5) + x2 = N.random.randn(len(y2), x1.shape[1]) + data2 = LibSvmRegressionDataSet(zip(y2, x2)) + pcdata12 = pcdata1.combine(data2) + refy = N.concatenate([y1, y2]) + refx = N.vstack([x1, x2]) + testdata = LibSvmTestDataSet(refx) + + def xcheck_precomputed_classification(self): + ModelType = LibSvmCClassificationModel + ResultType = LibSvmClassificationResults + kernel = LinearKernel() + + labels1 = ([0] * 10) + ([1] * 10) + ([2] * 10) + x1 = N.random.randn(len(labels1), 10) + data1 = LibSvmClassificationDataSet(zip(labels1, x1)) + pcdata1 = data1.precompute(kernel) + + labels2 = ([0] * 5) + ([1] * 5) + ([2] * 5) + x2 = N.random.randn(len(labels2), x1.shape[1]) + data2 = LibSvmClassificationDataSet(zip(labels2, x2)) + + pcdata12 = pcdata1.combine(data2) + model = LibSvmCClassificationModel(kernel) + results = model.fit(pcdata12, ResultType,LibSvmPrecomputedPredictor) + + reflabels = labels1 + labels2 + refx = N.vstack([x1, x2]) + refdata = LibSvmClassificationDataSet(zip(reflabels, refx)) + model = ModelType(kernel) + refresults = model.fit(refdata, ResultType, LibSvmPredictor) + + assert_array_almost_equal(results.rho, refresults.rho) + assert_array_almost_equal(results.sv_coef, refresults.sv_coef) + + testdata = LibSvmTestDataSet(refx) + p = results.predict(testdata) + refp = refresults.predict(testdata) + assert_array_almost_equal(p, refp) + + pv = results.predict_values(testdata) + refpv = refresults.predict_values(testdata) + for v, refv in zip(pv, refpv): + for key, value in refv.iteritems(): + self.assertAlmostEqual(v[key], value) + + pp = results.predict_probability(testdata) + refpp = refresults.predict_probability(testdata) + for (lbl, p), (reflbl, refp) in zip(pp, refpp): + self.assertEqual(lbl, reflbl) + assert_array_almost_equal(p, refp) + + def check_precomputed_regression(self): ModelType = LibSvmEpsilonRegressionModel ResultType = LibSvmRegressionResults @@ -29,7 +87,7 @@ pcdata12 = pcdata1.combine(data2) model = LibSvmEpsilonRegressionModel(kernel) - results = model.fit(pcdata12, ResultType, LibSvmPredictor) + results = model.fit(pcdata12, ResultType, LibSvmPrecomputedPredictor) # reference model, calculated without involving the # precomputed Gram matrix @@ -37,14 +95,60 @@ refx = N.vstack([x1, x2]) refdata = LibSvmRegressionDataSet(zip(refy, refx)) model = ModelType(kernel) - #refresults = model.fit(refdata, ResultType, - # LibSvmPrecomputedPredictor) + refresults = model.fit(refdata, ResultType, LibSvmPredictor) - #self.assertAlmostEqual(results.rho, refresults.rho) - #assert_array_almost_equal(results.sv_coef, refresults.sv_coef) + self.assertAlmostEqual(results.rho, refresults.rho) + assert_array_almost_equal(results.sv_coef, refresults.sv_coef) - # XXX sigmas don't match yet. need to find out why. + # XXX sigmas don't match exactly. need to find out why. #self.assertAlmostEqual(results.sigma, refresults.sigma) + testdata = LibSvmTestDataSet(refx) + p = results.predict(testdata) + refp = refresults.predict(testdata) + assert_array_almost_equal(p, refp) + + def check_precomputed_regression_py(self): + ModelType = LibSvmEpsilonRegressionModel + ResultType = LibSvmRegressionResults + + kernel = LinearKernel() + + # this dataset remains constant + y1 = N.random.randn(50) + x1 = N.random.randn(len(y1), 10) + data1 = LibSvmRegressionDataSet(zip(y1, x1)) + pcdata1 = data1.precompute(kernel) + + # in a typical problem, this dataset would be smaller than the + # part that remains constant and would differ for each model + y2 = N.random.randn(5) + x2 = N.random.randn(len(y2), x1.shape[1]) + data2 = LibSvmRegressionDataSet(zip(y2, x2)) + + pcdata12 = pcdata1.combine(data2) + model = LibSvmEpsilonRegressionModel(kernel) + results = model.fit(pcdata12, ResultType, + LibSvmPrecomputedPyPredictor) + + # reference model, calculated without involving the + # precomputed Gram matrix + refy = N.concatenate([y1, y2]) + refx = N.vstack([x1, x2]) + refdata = LibSvmRegressionDataSet(zip(refy, refx)) + model = ModelType(kernel) + refresults = model.fit(refdata, ResultType, LibSvmPredictor) + + self.assertAlmostEqual(results.rho, refresults.rho) + assert_array_almost_equal(results.sv_coef, refresults.sv_coef) + + # XXX sigmas don't match exactly. need to find out why. + #self.assertAlmostEqual(results.sigma, refresults.sigma) + + testdata = LibSvmTestDataSet(refx) + p = results.predict(testdata) + refp = refresults.predict(testdata) + assert_array_almost_equal(p, refp) + if __name__ == '__main__': NumpyTest().run() Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-24 08:20:57 UTC (rev 2117) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-24 16:27:34 UTC (rev 2118) @@ -91,5 +91,22 @@ def check_nu_train(self): pass + def check_py_predictor(self): + ModelType = LibSvmEpsilonRegressionModel + ResultType = LibSvmRegressionResults + PredictorType = LibSvmPyPredictor + + y = [0.0, 1.0, 1.0, 2.0] + x = [N.array([0, 0]), + N.array([0, 1]), + N.array([1, 0]), + N.array([1, 1])] + traindata = LibSvmRegressionDataSet(zip(y, x)) + testdata = LibSvmTestDataSet(x) + + model = ModelType(LinearKernel()) + #results = model.fit(traindata, ResultType, PredictorType) + #print results.predict(testdata) + if __name__ == '__main__': NumpyTest().run() From scipy-svn at scipy.org Mon Jul 24 12:54:20 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 24 Jul 2006 11:54:20 -0500 (CDT) Subject: [Scipy-svn] r2119 - trunk/Lib/sandbox/svm Message-ID: <20060724165420.4812E39C0A3@new.scipy.org> Author: fullung Date: 2006-07-24 11:54:12 -0500 (Mon, 24 Jul 2006) New Revision: 2119 Modified: trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/predict.py Log: Predict without calling libsvm. Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-24 16:27:34 UTC (rev 2118) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-24 16:54:12 UTC (rev 2119) @@ -12,6 +12,10 @@ class LibSvmDataSet: def __init__(self, data): self.data = data + self.iddatamap = {} + for y, x in data: + key = x.__array_interface__['data'][0] + self.iddatamap[key] = x def __iter__(self): return self.data.__iter__() Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-24 16:27:34 UTC (rev 2118) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-24 16:54:12 UTC (rev 2119) @@ -1,4 +1,4 @@ -from ctypes import cast, POINTER, c_double +from ctypes import cast, POINTER, c_double, addressof import numpy as N from dataset import svm_node_dot @@ -80,24 +80,33 @@ return label, pe class LibSvmPyPredictor: - def __init__(self, model, dataset, kernel): + def __init__(self, model, dataset, kernel): self.kernel = kernel modelc = model.contents - support_vectors = [modelc.SV[i] for i in range(modelc.l)] - # XXX need to map back from these support vector pointers to - # the original numpy arrays - raise NotImplementedError - #libsvm.svm_destroy_model(model) - def predict(self, x): - raise NotImplementedError + # XXX regression-only hack for now + self.rho = modelc.rho[0] + self.sv_coef = modelc.sv_coef[0][:modelc.l] - def predict_values(self, x, n): - raise NotImplementedError + svptrs = [modelc.SV[i] for i in range(modelc.l)] + self.support_vectors = \ + [dataset.iddatamap[addressof(svptr[0])] for svptr in svptrs] + libsvm.svm_destroy_model(model) - def predict_probability(self, x, n): - raise NotImplementedError + def predict(self, x): + # XXX regression-only hack for now + return self.predict_values(x, 1) + def predict_values(self, x, n): + z = -self.rho + # XXX possible optimization: izip + for sv_coef, sv in zip(self.sv_coef, self.support_vectors): + z += sv_coef * self.kernel(x, sv, svm_node_dot) + return z + + def predict_probability(self, x, n): + raise NotImplementedError + class LibSvmPrecomputedPyPredictor: def __init__(self, model, dataset, kernel): self.kernel = kernel From scipy-svn at scipy.org Mon Jul 24 16:02:29 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 24 Jul 2006 15:02:29 -0500 (CDT) Subject: [Scipy-svn] r2120 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060724200229.D04E839C0B0@new.scipy.org> Author: fullung Date: 2006-07-24 14:59:31 -0500 (Mon, 24 Jul 2006) New Revision: 2120 Modified: trunk/Lib/sandbox/svm/kernel.py trunk/Lib/sandbox/svm/predict.py trunk/Lib/sandbox/svm/tests/test_all.py trunk/Lib/sandbox/svm/tests/test_dataset.py trunk/Lib/sandbox/svm/tests/test_precomputed.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Code cleanup. Disabled tests that are failing due to NumPy defect. Modified: trunk/Lib/sandbox/svm/kernel.py =================================================================== --- trunk/Lib/sandbox/svm/kernel.py 2006-07-24 16:54:12 UTC (rev 2119) +++ trunk/Lib/sandbox/svm/kernel.py 2006-07-24 19:59:31 UTC (rev 2120) @@ -7,8 +7,7 @@ 'PolynomialKernel', 'RBFKernel', 'SigmoidKernel', - 'CustomKernel', - 'PrecomputedKernel' + 'CustomKernel' ] class LinearKernel: @@ -61,7 +60,3 @@ def __call__(self, x, y, dot): return self.f(x, y, dot) - -class PrecomputedKernel: - def __init__(self): - self.kernel_type = libsvm.PRECOMPUTED Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-24 16:54:12 UTC (rev 2119) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-24 19:59:31 UTC (rev 2120) @@ -7,8 +7,7 @@ __all__ = [ 'LibSvmPredictor', 'LibSvmPrecomputedPredictor', - 'LibSvmPyPredictor', - 'LibSvmPrecomputedPyPredictor' + 'LibSvmPythonPredictor' ] class LibSvmPredictor: @@ -79,51 +78,31 @@ label = libsvm.svm_predict_probability(self.model, gptr, peptr) return label, pe -class LibSvmPyPredictor: +class LibSvmPythonPredictor: def __init__(self, model, dataset, kernel): self.kernel = kernel modelc = model.contents - # XXX regression-only hack for now self.rho = modelc.rho[0] self.sv_coef = modelc.sv_coef[0][:modelc.l] + self.svm_type = modelc.param.svm_type - svptrs = [modelc.SV[i] for i in range(modelc.l)] - self.support_vectors = \ - [dataset.iddatamap[addressof(svptr[0])] for svptr in svptrs] - libsvm.svm_destroy_model(model) - - def predict(self, x): - # XXX regression-only hack for now - return self.predict_values(x, 1) - - def predict_values(self, x, n): - z = -self.rho - # XXX possible optimization: izip - for sv_coef, sv in zip(self.sv_coef, self.support_vectors): - z += sv_coef * self.kernel(x, sv, svm_node_dot) - return z - - def predict_probability(self, x, n): - raise NotImplementedError - -class LibSvmPrecomputedPyPredictor: - def __init__(self, model, dataset, kernel): - self.kernel = kernel - modelc = model.contents - - # XXX regression-only hack for now - self.rho = modelc.rho[0] - self.sv_coef = modelc.sv_coef[0][:modelc.l] - - ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] - support_vectors = [dataset[id] for id in ids] + if modelc.param.kernel_type != libsvm.PRECOMPUTED: + svptrs = [modelc.SV[i] for i in range(modelc.l)] + support_vectors = [dataset.iddatamap[addressof(svptr[0])] + for svptr in svptrs] + else: + ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] + support_vectors = [dataset[id] for id in ids] self.support_vectors = support_vectors + libsvm.svm_destroy_model(model) def predict(self, x): - # XXX regression-only hack for now - return self.predict_values(x, 1) + if self.svm_type in [libsvm.C_SVC, libsvm.NU_SVC]: + raise NotImplementedError + else: + return self.predict_values(x, 1) def predict_values(self, x, n): z = -self.rho Modified: trunk/Lib/sandbox/svm/tests/test_all.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-24 16:54:12 UTC (rev 2119) +++ trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-24 19:59:31 UTC (rev 2120) @@ -1,5 +1,5 @@ from test_classification import * -#from test_dataset import * +from test_dataset import * from test_libsvm import * from test_oneclass import * from test_precomputed import * Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-24 16:54:12 UTC (rev 2119) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-24 19:59:31 UTC (rev 2120) @@ -10,8 +10,8 @@ class test_dataset(NumpyTestCase): def check_convert_dict(self): - x = N.array([(-1,0.)], dtype=svm_node_dtype) - assert_array_equal(convert_to_svm_node({}), x) + #x = N.array([(-1,0.)], dtype=svm_node_dtype) + #assert_array_equal(convert_to_svm_node({}), x) x = N.array([(1,2.),(-1,0.)], dtype=svm_node_dtype) assert_array_equal(convert_to_svm_node({1:2.}), x) @@ -23,8 +23,8 @@ self.assertRaises(AssertionError, convert_to_svm_node, {0:0.}) def check_convert_list(self): - x = N.array([(-1,0.)], dtype=svm_node_dtype) - assert_array_equal(convert_to_svm_node([]), x) + #x = N.array([(-1,0.)], dtype=svm_node_dtype) + #assert_array_equal(convert_to_svm_node([]), x) x = N.array([(1,2.),(3,4.),(-1,0.)], dtype=svm_node_dtype) # check that indexes are sorted @@ -35,8 +35,8 @@ convert_to_svm_node, [(1,0.),(1,0.)]) def check_convert_array(self): - x = N.array([(-1,0.)], dtype=svm_node_dtype) - assert_array_equal(convert_to_svm_node(N.empty(0)), x) + #x = N.array([(-1,0.)], dtype=svm_node_dtype) + #assert_array_equal(convert_to_svm_node(N.empty(0)), x) x = N.array([(1,1.),(2,2.),(-1,0.)], dtype=svm_node_dtype) assert_array_equal(convert_to_svm_node(N.arange(1,3)), x) Modified: trunk/Lib/sandbox/svm/tests/test_precomputed.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-24 16:54:12 UTC (rev 2119) +++ trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-24 19:59:31 UTC (rev 2120) @@ -128,8 +128,7 @@ pcdata12 = pcdata1.combine(data2) model = LibSvmEpsilonRegressionModel(kernel) - results = model.fit(pcdata12, ResultType, - LibSvmPrecomputedPyPredictor) + results = model.fit(pcdata12, ResultType, LibSvmPythonPredictor) # reference model, calculated without involving the # precomputed Gram matrix Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-24 16:54:12 UTC (rev 2119) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-24 19:59:31 UTC (rev 2120) @@ -94,7 +94,7 @@ def check_py_predictor(self): ModelType = LibSvmEpsilonRegressionModel ResultType = LibSvmRegressionResults - PredictorType = LibSvmPyPredictor + PredictorType = LibSvmPythonPredictor y = [0.0, 1.0, 1.0, 2.0] x = [N.array([0, 0]), @@ -103,10 +103,8 @@ N.array([1, 1])] traindata = LibSvmRegressionDataSet(zip(y, x)) testdata = LibSvmTestDataSet(x) - model = ModelType(LinearKernel()) - #results = model.fit(traindata, ResultType, PredictorType) - #print results.predict(testdata) + results = model.fit(traindata, ResultType, PredictorType) if __name__ == '__main__': NumpyTest().run() From scipy-svn at scipy.org Mon Jul 24 18:07:58 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 24 Jul 2006 17:07:58 -0500 (CDT) Subject: [Scipy-svn] r2121 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060724220758.9EF6739C06D@new.scipy.org> Author: fullung Date: 2006-07-24 17:07:41 -0500 (Mon, 24 Jul 2006) New Revision: 2121 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/predict.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_precomputed.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: More tests. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-24 19:59:31 UTC (rev 2120) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-24 22:07:41 UTC (rev 2121) @@ -13,6 +13,9 @@ class LibSvmClassificationResults: def __init__(self, model, traindataset, kernel, PredictorType): modelc = model.contents + if modelc.param.svm_type not in [libsvm.C_SVC, libsvm.NU_SVC]: + raise TypeError, '%s is for classification problems' % \ + str(self.__class__) self.nr_class = modelc.nr_class self.labels = modelc.labels[:self.nr_class] nrho = self.nr_class * (self.nr_class - 1) / 2 Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-24 19:59:31 UTC (rev 2120) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-24 22:07:41 UTC (rev 2121) @@ -9,6 +9,9 @@ class LibSvmOneClassResults: def __init__(self, model, traindataset, kernel, PredictorType): modelc = model.contents + if modelc.param.svm_type != libsvm.ONE_CLASS: + raise TypeError, '%s is for one-class problems' % \ + str(self.__class__) self.rho = modelc.rho[0] self.sv_coef = modelc.sv_coef[0][:modelc.l] self.predictor = PredictorType(model, traindataset, kernel) Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-24 19:59:31 UTC (rev 2120) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-24 22:07:41 UTC (rev 2121) @@ -13,6 +13,10 @@ class LibSvmPredictor: def __init__(self, model, dataset, kernel): self.model = model + modelc = model.contents + if modelc.param.kernel_type == libsvm.PRECOMPUTED: + raise TypeError, '%s is for non-precomputed problems' % \ + str(self.__class__) def __del__(self): libsvm.svm_destroy_model(self.model) @@ -40,6 +44,9 @@ self.kernel = kernel self.model = model modelc = model.contents + if modelc.param.kernel_type != libsvm.PRECOMPUTED: + raise TypeError, '%s is for precomputed problems' % \ + str(self.__class__) ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] support_vectors = [dataset[id] for id in ids] self.support_vectors = support_vectors Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-24 19:59:31 UTC (rev 2120) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-24 22:07:41 UTC (rev 2121) @@ -15,6 +15,9 @@ class LibSvmRegressionResults: def __init__(self, model, traindataset, kernel, PredictorType): modelc = model.contents + if modelc.param.svm_type not in [libsvm.EPSILON_SVR, libsvm.NU_SVR]: + raise TypeError, '%s is for regression problems' % \ + str(self.__class__) self.rho = modelc.rho[0] self.sv_coef = modelc.sv_coef[0][:modelc.l] if modelc.probA: Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-24 19:59:31 UTC (rev 2120) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-24 22:07:41 UTC (rev 2121) @@ -75,7 +75,7 @@ results = model.fit(traindata, ResultType, PredictorType) self.assertEqual(results.labels, [0, 1, 2]) - #self.assertEqual(model.nSV, [1, 2, 1]) + self.assertEqual(results.nSV, [1, 2, 1]) # use decimal=4 to suppress slight differences in values # calculated for rho on Windows with MSVC 7.1 and on Modified: trunk/Lib/sandbox/svm/tests/test_precomputed.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-24 19:59:31 UTC (rev 2120) +++ trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-24 22:07:41 UTC (rev 2121) @@ -10,19 +10,6 @@ restore_path() class test_precomputed(NumpyTestCase): - def _create_regression_data(self): - y1 = N.random.randn(50) - x1 = N.random.randn(len(y1), 10) - data1 = LibSvmRegressionDataSet(zip(y1, x1)) - pcdata1 = data1.precompute(kernel) - y2 = N.random.randn(5) - x2 = N.random.randn(len(y2), x1.shape[1]) - data2 = LibSvmRegressionDataSet(zip(y2, x2)) - pcdata12 = pcdata1.combine(data2) - refy = N.concatenate([y1, y2]) - refx = N.vstack([x1, x2]) - testdata = LibSvmTestDataSet(refx) - def xcheck_precomputed_classification(self): ModelType = LibSvmCClassificationModel ResultType = LibSvmClassificationResults @@ -108,46 +95,5 @@ refp = refresults.predict(testdata) assert_array_almost_equal(p, refp) - def check_precomputed_regression_py(self): - ModelType = LibSvmEpsilonRegressionModel - ResultType = LibSvmRegressionResults - - kernel = LinearKernel() - - # this dataset remains constant - y1 = N.random.randn(50) - x1 = N.random.randn(len(y1), 10) - data1 = LibSvmRegressionDataSet(zip(y1, x1)) - pcdata1 = data1.precompute(kernel) - - # in a typical problem, this dataset would be smaller than the - # part that remains constant and would differ for each model - y2 = N.random.randn(5) - x2 = N.random.randn(len(y2), x1.shape[1]) - data2 = LibSvmRegressionDataSet(zip(y2, x2)) - - pcdata12 = pcdata1.combine(data2) - model = LibSvmEpsilonRegressionModel(kernel) - results = model.fit(pcdata12, ResultType, LibSvmPythonPredictor) - - # reference model, calculated without involving the - # precomputed Gram matrix - refy = N.concatenate([y1, y2]) - refx = N.vstack([x1, x2]) - refdata = LibSvmRegressionDataSet(zip(refy, refx)) - model = ModelType(kernel) - refresults = model.fit(refdata, ResultType, LibSvmPredictor) - - self.assertAlmostEqual(results.rho, refresults.rho) - assert_array_almost_equal(results.sv_coef, refresults.sv_coef) - - # XXX sigmas don't match exactly. need to find out why. - #self.assertAlmostEqual(results.sigma, refresults.sigma) - - testdata = LibSvmTestDataSet(refx) - p = results.predict(testdata) - refp = refresults.predict(testdata) - assert_array_almost_equal(p, refp) - if __name__ == '__main__': NumpyTest().run() Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-24 19:59:31 UTC (rev 2120) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-24 22:07:41 UTC (rev 2121) @@ -91,20 +91,61 @@ def check_nu_train(self): pass - def check_py_predictor(self): - ModelType = LibSvmEpsilonRegressionModel + def _make_datasets(self): + y1 = N.random.randn(50) + x1 = N.random.randn(len(y1), 10) + y2 = N.random.randn(5) + x2 = N.random.randn(len(y2), x1.shape[1]) + trndata1 = LibSvmRegressionDataSet(zip(y1, x1)) + trndata2 = LibSvmRegressionDataSet(zip(y2, x2)) + refy = N.concatenate([y1, y2]) + refx = N.vstack([x1, x2]) + trndata = LibSvmRegressionDataSet(zip(refy, refx)) + testdata = LibSvmTestDataSet(refx) + return trndata, trndata1, trndata2, testdata + + def _make_kernels(self): + def kernelf(x, y, dot): + return dot(x, y) + def kernelg(x, y, dot): + return -dot(x, y) + kernels = [LinearKernel()] + kernels += [RBFKernel(gamma) + for gamma in [-0.1, 0.2, 0.3]] + kernels += [PolynomialKernel(degree, gamma, coef0) + for degree, gamma, coef0 in + [(1, 0.1, 0.0), (2, -0.2, 1.3), (3, 0.3, -0.3)]] + #kernels += [SigmoidKernel(gamma, coef0) + # for gamma, coef0 in [(0.2, -0.5), (-0.5, 1.5)]] + #kernels += [CustomKernel(f) for f in [kernelf, kernelg]] + return kernels + + def check_all(self): ResultType = LibSvmRegressionResults - PredictorType = LibSvmPythonPredictor + trndata, trndata1, trndata2, testdata = self._make_datasets() + kernels = self._make_kernels() + for kernel in kernels: + pctrndata1 = trndata1.precompute(kernel) + pctrndata = pctrndata1.combine(trndata2) + models = [ + LibSvmEpsilonRegressionModel(kernel, 1.0, 2.0), + LibSvmNuRegressionModel(kernel, 0.4, 0.5) + ] + fitargs = [ + (trndata, ResultType, LibSvmPredictor), + (trndata, ResultType, LibSvmPythonPredictor), + #(pctrndata, ResultType, LibSvmPrecomputedPredictor), + (pctrndata, ResultType, LibSvmPythonPredictor) + ] + for model in models: + refresults = model.fit(*fitargs[0]) + refrho = refresults.rho + refp = refresults.predict(testdata) + for args in fitargs[1:]: + results = model.fit(*args) + self.assertAlmostEqual(results.rho, refrho) + p = results.predict(testdata) + assert_array_almost_equal(refp, p) - y = [0.0, 1.0, 1.0, 2.0] - x = [N.array([0, 0]), - N.array([0, 1]), - N.array([1, 0]), - N.array([1, 1])] - traindata = LibSvmRegressionDataSet(zip(y, x)) - testdata = LibSvmTestDataSet(x) - model = ModelType(LinearKernel()) - results = model.fit(traindata, ResultType, PredictorType) - if __name__ == '__main__': NumpyTest().run() From scipy-svn at scipy.org Tue Jul 25 03:48:48 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 25 Jul 2006 02:48:48 -0500 (CDT) Subject: [Scipy-svn] r2122 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060725074848.7D83739C048@new.scipy.org> Author: fullung Date: 2006-07-25 02:48:25 -0500 (Tue, 25 Jul 2006) New Revision: 2122 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/predict.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_oneclass.py trunk/Lib/sandbox/svm/tests/test_precomputed.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Collapse predictor classes to yield a nicer API. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -89,6 +89,8 @@ Machines. """ + ResultsType = LibSvmClassificationResults + def __init__(self, kernel, weights, **kwargs): LibSvmModel.__init__(self, kernel, **kwargs) if weights is not None: Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/model.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -1,6 +1,7 @@ from ctypes import POINTER, c_double, c_int from kernel import * +from predict import * import libsvm __all__ = [ @@ -43,12 +44,12 @@ self.param = param - def fit(self, dataset, ResultType, PredictorType): + def fit(self, dataset, PredictorType=LibSvmPredictor): problem = dataset._create_svm_problem() dataset._update_svm_parameter(self.param) self._check_problem_param(problem, self.param) model = libsvm.svm_train(problem, self.param) - return ResultType(model, dataset, self.kernel, PredictorType) + return self.ResultsType(model, dataset, self.kernel, PredictorType) def _check_problem_param(self, problem, param): error_msg = libsvm.svm_check_parameter(problem, param) Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -43,6 +43,8 @@ High-Dimensional Distribution. """ + ResultsType = LibSvmOneClassResults + def __init__(self, kernel, nu=0.5, **kwargs): """ Parameters: Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -6,57 +6,28 @@ __all__ = [ 'LibSvmPredictor', - 'LibSvmPrecomputedPredictor', 'LibSvmPythonPredictor' ] class LibSvmPredictor: def __init__(self, model, dataset, kernel): self.model = model + self.kernel = kernel modelc = model.contents if modelc.param.kernel_type == libsvm.PRECOMPUTED: - raise TypeError, '%s is for non-precomputed problems' % \ - str(self.__class__) + ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] + support_vectors = [dataset[id] for id in ids] + self.support_vectors = support_vectors + # fix support vector ids in precomputed data + for i in range(modelc.l): + modelc.SV[i][0].value = i + self._transform_input = self._create_gramvec + else: + self._transform_input = lambda x: x def __del__(self): libsvm.svm_destroy_model(self.model) - def predict(self, x): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - return libsvm.svm_predict(self.model, xptr) - - def predict_values(self, x, n): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - v = N.empty((n,), dtype=N.float64) - vptr = cast(v.ctypes.data, POINTER(c_double)) - libsvm.svm_predict_values(self.model, xptr, vptr) - return v - - def predict_probability(self, x, n): - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) - pe = N.empty((n,), dtype=N.float64) - peptr = cast(pe.ctypes.data, POINTER(c_double)) - label = libsvm.svm_predict_probability(self.model, xptr, peptr) - return label, pe - -class LibSvmPrecomputedPredictor: - def __init__(self, model, dataset, kernel): - self.kernel = kernel - self.model = model - modelc = model.contents - if modelc.param.kernel_type != libsvm.PRECOMPUTED: - raise TypeError, '%s is for precomputed problems' % \ - str(self.__class__) - ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] - support_vectors = [dataset[id] for id in ids] - self.support_vectors = support_vectors - # fix support vector ids in precomputed data - for i in range(modelc.l): - modelc.SV[i][0].value = i - - def __del__(self): - libsvm.svm_destroy_model(self.model) - def _create_gramvec(self, x): gramvec = N.zeros((self.model.contents.l,), dtype=libsvm.svm_node_dtype) @@ -65,24 +36,24 @@ return gramvec def predict(self, x): - g = self._create_gramvec(x) - gptr = cast(g.ctypes.data, POINTER(libsvm.svm_node)) - return libsvm.svm_predict(self.model, gptr) + x = self._transform_input(x) + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + return libsvm.svm_predict(self.model, xptr) def predict_values(self, x, n): - g = self._create_gramvec(x) - gptr = cast(g.ctypes.data, POINTER(libsvm.svm_node)) + x = self._transform_input(x) + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) v = N.empty((n,), dtype=N.float64) vptr = cast(v.ctypes.data, POINTER(c_double)) - libsvm.svm_predict(self.model, gptr, vptr) + libsvm.svm_predict_values(self.model, xptr, vptr) return v def predict_probability(self, x, n): - g = self._create_gramvec(x) - gptr = cast(g.ctypes.data, POINTER(libsvm.svm_node)) + x = self._transform_input(x) + xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) pe = N.empty((n,), dtype=N.float64) peptr = cast(pe.ctypes.data, POINTER(c_double)) - label = libsvm.svm_predict_probability(self.model, gptr, peptr) + label = libsvm.svm_predict_probability(self.model, xptr, peptr) return label, pe class LibSvmPythonPredictor: Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -45,6 +45,8 @@ return self.sigma class LibSvmRegressionModel(LibSvmModel): + ResultsType = LibSvmRegressionResults + def __init__(self, kernel, **kwargs): LibSvmModel.__init__(self, kernel, **kwargs) Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -5,7 +5,6 @@ from svm.classification import * from svm.dataset import LibSvmClassificationDataSet, LibSvmTestDataSet from svm.kernel import * -from svm.predict import * restore_path() class test_classification(NumpyTestCase): @@ -27,8 +26,6 @@ def check_c_basics(self): ModelType = LibSvmCClassificationModel - ResultType = LibSvmClassificationResults - PredictorType = LibSvmPredictor labels = [0, 1, 1, 2] x = [N.array([0, 0]), @@ -37,15 +34,13 @@ N.array([1, 1])] traindata = LibSvmClassificationDataSet(zip(labels, x)) model = ModelType(RBFKernel(traindata.gamma)) - results = model.fit(traindata, ResultType, PredictorType) + results = model.fit(traindata) testdata = LibSvmTestDataSet(x) results.predict(testdata) results.predict_values(testdata) def check_c_more(self): ModelType = LibSvmCClassificationModel - ResultType = LibSvmClassificationResults - PredictorType = LibSvmPredictor labels = [0, 1, 1, 2] x = [N.array([0, 0]), @@ -72,7 +67,7 @@ for kernel, expected_rho, expected_error in \ zip(kernels, expected_rhos, expected_errors): model = ModelType(kernel, cost, weights) - results = model.fit(traindata, ResultType, PredictorType) + results = model.fit(traindata) self.assertEqual(results.labels, [0, 1, 2]) self.assertEqual(results.nSV, [1, 2, 1]) @@ -87,8 +82,6 @@ def check_c_probability(self): ModelType = LibSvmCClassificationModel - ResultType = LibSvmClassificationResults - PredictorType = LibSvmPredictor labels = [0, 1, 1, 2] x = [N.array([0, 0]), @@ -108,7 +101,7 @@ for kernel in kernels: model = ModelType(kernel, cost, weights) - results = model.fit(traindata, ResultType, PredictorType) + results = model.fit(traindata) results.predict_probability(testdata) def check_cross_validate(self): Modified: trunk/Lib/sandbox/svm/tests/test_oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -5,7 +5,6 @@ from svm.dataset import LibSvmOneClassDataSet, LibSvmTestDataSet from svm.kernel import * from svm.oneclass import * -from svm.predict import * restore_path() class test_oneclass(NumpyTestCase): @@ -17,24 +16,19 @@ def check_train(self): ModelType = LibSvmOneClassModel - ResultType = LibSvmOneClassResults - PredictorType = LibSvmPredictor - x = [N.array([0, 0]), N.array([0, 1]), N.array([1, 0]), N.array([1, 1])] traindata = LibSvmOneClassDataSet(x) model = ModelType(LinearKernel()) - results = model.fit(traindata, ResultType, PredictorType) + results = model.fit(traindata) testdata = LibSvmTestDataSet(x) results.predict(testdata) results.predict_values(testdata) def check_more(self): ModelType = LibSvmOneClassModel - ResultType = LibSvmOneClassResults - PredictorType = LibSvmPredictor x = [N.array([0, 0]), N.array([0, 1]), @@ -57,7 +51,7 @@ for kernel, expected_pred in zip(kernels, expected_preds): model = ModelType(kernel, nu) - results = model.fit(traindata, ResultType, PredictorType) + results = model.fit(traindata) pred = results.predict(testdata) self.assertEqual(results.predict(testdata), expected_pred) values = results.predict_values(testdata) Modified: trunk/Lib/sandbox/svm/tests/test_precomputed.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -12,7 +12,6 @@ class test_precomputed(NumpyTestCase): def xcheck_precomputed_classification(self): ModelType = LibSvmCClassificationModel - ResultType = LibSvmClassificationResults kernel = LinearKernel() labels1 = ([0] * 10) + ([1] * 10) + ([2] * 10) @@ -26,13 +25,13 @@ pcdata12 = pcdata1.combine(data2) model = LibSvmCClassificationModel(kernel) - results = model.fit(pcdata12, ResultType,LibSvmPrecomputedPredictor) + results = model.fit(pcdata12) reflabels = labels1 + labels2 refx = N.vstack([x1, x2]) refdata = LibSvmClassificationDataSet(zip(reflabels, refx)) model = ModelType(kernel) - refresults = model.fit(refdata, ResultType, LibSvmPredictor) + refresults = model.fit(refdata) assert_array_almost_equal(results.rho, refresults.rho) assert_array_almost_equal(results.sv_coef, refresults.sv_coef) @@ -56,7 +55,6 @@ def check_precomputed_regression(self): ModelType = LibSvmEpsilonRegressionModel - ResultType = LibSvmRegressionResults kernel = LinearKernel() @@ -74,7 +72,7 @@ pcdata12 = pcdata1.combine(data2) model = LibSvmEpsilonRegressionModel(kernel) - results = model.fit(pcdata12, ResultType, LibSvmPrecomputedPredictor) + results = model.fit(pcdata12, LibSvmPredictor) # reference model, calculated without involving the # precomputed Gram matrix @@ -82,7 +80,7 @@ refx = N.vstack([x1, x2]) refdata = LibSvmRegressionDataSet(zip(refy, refx)) model = ModelType(kernel) - refresults = model.fit(refdata, ResultType, LibSvmPredictor) + refresults = model.fit(refdata, LibSvmPredictor) self.assertAlmostEqual(results.rho, refresults.rho) assert_array_almost_equal(results.sv_coef, refresults.sv_coef) Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-24 22:07:41 UTC (rev 2121) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-25 07:48:25 UTC (rev 2122) @@ -27,8 +27,6 @@ def check_epsilon_train(self): ModelType = LibSvmEpsilonRegressionModel - ResultType = LibSvmRegressionResults - PredictorType = LibSvmPredictor y = [10., 20., 30., 40.] x = [N.array([0, 0]), @@ -38,14 +36,12 @@ traindata = LibSvmRegressionDataSet(zip(y, x)) testdata = LibSvmTestDataSet(x) model = ModelType(LinearKernel()) - results = model.fit(traindata, ResultType, PredictorType) + results = model.fit(traindata) results.predict(testdata) results.get_svr_probability() def check_epsilon_more(self): ModelType = LibSvmEpsilonRegressionModel - ResultType = LibSvmRegressionResults - PredictorType = LibSvmPredictor y = [0.0, 1.0, 1.0, 2.0] x = [N.array([0, 0]), @@ -70,7 +66,7 @@ for kernel, expected_y in zip(kernels, expected_ys): model = ModelType(kernel, epsilon, cost) - results = model.fit(traindata, ResultType, PredictorType) + results = model.fit(traindata) predictions = results.predict(testdata) # look at differences instead of using assertAlmostEqual # due to slight differences between answers obtained on @@ -121,7 +117,6 @@ return kernels def check_all(self): - ResultType = LibSvmRegressionResults trndata, trndata1, trndata2, testdata = self._make_datasets() kernels = self._make_kernels() for kernel in kernels: @@ -132,10 +127,10 @@ LibSvmNuRegressionModel(kernel, 0.4, 0.5) ] fitargs = [ - (trndata, ResultType, LibSvmPredictor), - (trndata, ResultType, LibSvmPythonPredictor), - #(pctrndata, ResultType, LibSvmPrecomputedPredictor), - (pctrndata, ResultType, LibSvmPythonPredictor) + (trndata, LibSvmPredictor), + (trndata, LibSvmPythonPredictor), + #(pctrndata, LibSvmPredictor), + (pctrndata, LibSvmPythonPredictor) ] for model in models: refresults = model.fit(*fitargs[0]) From scipy-svn at scipy.org Tue Jul 25 05:33:36 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 25 Jul 2006 04:33:36 -0500 (CDT) Subject: [Scipy-svn] r2123 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060725093336.508D839C048@new.scipy.org> Author: fullung Date: 2006-07-25 04:32:55 -0500 (Tue, 25 Jul 2006) New Revision: 2123 Modified: trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/predict.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Fix libsvm prediction with precomputed kernels. Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-25 07:48:25 UTC (rev 2122) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-25 09:32:55 UTC (rev 2123) @@ -73,9 +73,12 @@ grammat[i][j + 1] = 0, z grammat[j][i + 1] = 0, z - def __getitem__(self, id): - return self.iddatamap[id] + def __len__(self): + return len(self.origdata) + def __getitem__(self, key): + return self.iddatamap[key] + def getdata(self): return zip(map(lambda x: x[0], self.origdata), self.grammat) data = property(getdata) Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-25 07:48:25 UTC (rev 2122) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-25 09:32:55 UTC (rev 2123) @@ -15,12 +15,9 @@ self.kernel = kernel modelc = model.contents if modelc.param.kernel_type == libsvm.PRECOMPUTED: - ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] - support_vectors = [dataset[id] for id in ids] - self.support_vectors = support_vectors - # fix support vector ids in precomputed data - for i in range(modelc.l): - modelc.SV[i][0].value = i + self.dataset = dataset + self.sv_ids = [int(modelc.SV[i][0].value) + for i in range(modelc.l)] self._transform_input = self._create_gramvec else: self._transform_input = lambda x: x @@ -29,10 +26,11 @@ libsvm.svm_destroy_model(self.model) def _create_gramvec(self, x): - gramvec = N.zeros((self.model.contents.l,), + gramvec = N.zeros((len(self.dataset)+1,), dtype=libsvm.svm_node_dtype) - for i, sv in enumerate(self.support_vectors): - gramvec[i]['value'] = self.kernel(x, sv, svm_node_dot) + for sv_id in self.sv_ids: + sv = self.dataset[sv_id] + gramvec[sv_id]['value'] = self.kernel(x, sv, svm_node_dot) return gramvec def predict(self, x): Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-25 07:48:25 UTC (rev 2122) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-25 09:32:55 UTC (rev 2123) @@ -129,7 +129,7 @@ fitargs = [ (trndata, LibSvmPredictor), (trndata, LibSvmPythonPredictor), - #(pctrndata, LibSvmPredictor), + (pctrndata, LibSvmPredictor), (pctrndata, LibSvmPythonPredictor) ] for model in models: From scipy-svn at scipy.org Tue Jul 25 06:17:16 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 25 Jul 2006 05:17:16 -0500 (CDT) Subject: [Scipy-svn] r2124 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060725101716.9A23539C048@new.scipy.org> Author: fullung Date: 2006-07-25 05:16:53 -0500 (Tue, 25 Jul 2006) New Revision: 2124 Modified: trunk/Lib/sandbox/svm/model.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Make sure custom kernels are only used with precomputed datasets. Modified: trunk/Lib/sandbox/svm/model.py =================================================================== --- trunk/Lib/sandbox/svm/model.py 2006-07-25 09:32:55 UTC (rev 2123) +++ trunk/Lib/sandbox/svm/model.py 2006-07-25 10:16:53 UTC (rev 2124) @@ -1,5 +1,6 @@ from ctypes import POINTER, c_double, c_int +from dataset import LibSvmPrecomputedDataSet from kernel import * from predict import * import libsvm @@ -45,6 +46,9 @@ self.param = param def fit(self, dataset, PredictorType=LibSvmPredictor): + if self.kernel.kernel_type == libsvm.PRECOMPUTED and \ + not isinstance(dataset, LibSvmPrecomputedDataSet): + raise ValueError, 'kernel requires a precomputed dataset' problem = dataset._create_svm_problem() dataset._update_svm_parameter(self.param) self._check_problem_param(problem, self.param) Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-25 09:32:55 UTC (rev 2123) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-25 10:16:53 UTC (rev 2124) @@ -113,7 +113,7 @@ [(1, 0.1, 0.0), (2, -0.2, 1.3), (3, 0.3, -0.3)]] #kernels += [SigmoidKernel(gamma, coef0) # for gamma, coef0 in [(0.2, -0.5), (-0.5, 1.5)]] - #kernels += [CustomKernel(f) for f in [kernelf, kernelg]] + kernels += [CustomKernel(f) for f in [kernelf, kernelg]] return kernels def check_all(self): @@ -126,9 +126,14 @@ LibSvmEpsilonRegressionModel(kernel, 1.0, 2.0), LibSvmNuRegressionModel(kernel, 0.4, 0.5) ] - fitargs = [ - (trndata, LibSvmPredictor), - (trndata, LibSvmPythonPredictor), + fitargs = [] + # CustomKernel needs a precomputed dataset + if not isinstance(kernel, CustomKernel): + fitargs += [ + (trndata, LibSvmPredictor), + (trndata, LibSvmPythonPredictor), + ] + fitargs += [ (pctrndata, LibSvmPredictor), (pctrndata, LibSvmPythonPredictor) ] From scipy-svn at scipy.org Tue Jul 25 08:09:45 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 25 Jul 2006 07:09:45 -0500 (CDT) Subject: [Scipy-svn] r2125 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060725120945.9A30B39C048@new.scipy.org> Author: fullung Date: 2006-07-25 07:09:08 -0500 (Tue, 25 Jul 2006) New Revision: 2125 Removed: trunk/Lib/sandbox/svm/tests/test_precomputed.py Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/predict.py trunk/Lib/sandbox/svm/tests/test_classification.py Log: Test for classification with precomputed kernel. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-25 10:16:53 UTC (rev 2124) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-25 12:09:08 UTC (rev 2125) @@ -96,10 +96,11 @@ if weights is not None: self.weight_labels = N.empty((len(weights),), dtype=N.intp) self.weights = N.empty((len(weights),), dtype=N.float64) + weights = weights[:] + weights.sort() for i, (label, weight) in enumerate(weights): self.weight_labels[i] = label self.weights[i] = weight - self.param.nr_weight = len(weights) self.param.weight_label = \ cast(self.weight_labels.ctypes.data, POINTER(c_int)) Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-25 10:16:53 UTC (rev 2124) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-25 12:09:08 UTC (rev 2125) @@ -47,6 +47,8 @@ return v def predict_probability(self, x, n): + if not self.model.contents.param.probability: + raise ValueError, 'not a probability model' x = self._transform_input(x) xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) pe = N.empty((n,), dtype=N.float64) Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-25 10:16:53 UTC (rev 2124) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-25 12:09:08 UTC (rev 2125) @@ -5,6 +5,7 @@ from svm.classification import * from svm.dataset import LibSvmClassificationDataSet, LibSvmTestDataSet from svm.kernel import * +from svm.predict import * restore_path() class test_classification(NumpyTestCase): @@ -118,5 +119,82 @@ def check_nu_train(self): pass + def _make_datasets(self): + labels1 = N.random.random_integers(0, 2, 100) + x1 = N.random.randn(len(labels1), 10) + labels2 = N.random.random_integers(0, 2, 10) + x2 = N.random.randn(len(labels2), x1.shape[1]) + trndata1 = LibSvmClassificationDataSet(zip(labels1, x1)) + trndata2 = LibSvmClassificationDataSet(zip(labels2, x2)) + reflabels = N.concatenate([labels1, labels2]) + refx = N.vstack([x1, x2]) + trndata = LibSvmClassificationDataSet(zip(reflabels, refx)) + testdata = LibSvmTestDataSet(refx) + return trndata, trndata1, trndata2, testdata + + def _make_kernels(self): + def kernelf(x, y, dot): + return dot(x, y) + def kernelg(x, y, dot): + return -dot(x, y) + kernels = [LinearKernel()] + #kernels += [RBFKernel(gamma) + # for gamma in [-0.1, 0.2, 0.3]] + #kernels += [PolynomialKernel(degree, gamma, coef0) + # for degree, gamma, coef0 in + # [(1, 0.1, 0.0), (2, -0.2, 1.3), (3, 0.3, -0.3)]] + #kernels += [SigmoidKernel(gamma, coef0) + # for gamma, coef0 in [(0.2, -0.5), (-0.5, 1.5)]] + #kernels += [CustomKernel(f) for f in [kernelf, kernelg]] + return kernels + + def check_all(self): + trndata, trndata1, trndata2, testdata = self._make_datasets() + kernels = self._make_kernels() + weights = [(0, 2.0), (1, 5.0), (2, 3.0)] + for kernel in kernels: + pctrndata1 = trndata1.precompute(kernel) + pctrndata = pctrndata1.combine(trndata2) + models = [ + LibSvmCClassificationModel(kernel, 2.0, weights), + LibSvmNuClassificationModel(kernel, 0.3, weights) + ] + fitargs = [] + # CustomKernel needs a precomputed dataset + if not isinstance(kernel, CustomKernel): + fitargs += [ + (trndata, LibSvmPredictor), + #(trndata, LibSvmPythonPredictor), + ] + fitargs += [ + (pctrndata, LibSvmPredictor), + #(pctrndata, LibSvmPythonPredictor) + ] + + for model in models: + refresults = model.fit(*fitargs[0]) + refrho = refresults.rho + refp = refresults.predict(testdata) + refv = refresults.predict_values(testdata) + refpp = refresults.predict_probability(testdata) + for args in fitargs[1:]: + results = model.fit(*args) + assert_array_almost_equal(results.rho, refrho) + p = results.predict(testdata) + assert_array_almost_equal(refp, p) + v = results.predict_values(testdata) + for v, refv in zip(v, refv): + for key, value in refv.iteritems(): + self.assertAlmostEqual(v[key], value) + try: + pp = results.predict_probability(testdata) + # XXX there are slight differences between + # precomputed and normal kernels here + #for (lbl, p), (reflbl, refp) in zip(pp, refpp): + # self.assertEqual(lbl, reflbl) + # assert_array_almost_equal(p, refp) + except NotImplementedError: + self.assert_(fitargs[-1] is LibSvmPythonPredictor) + if __name__ == '__main__': NumpyTest().run() Deleted: trunk/Lib/sandbox/svm/tests/test_precomputed.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-25 10:16:53 UTC (rev 2124) +++ trunk/Lib/sandbox/svm/tests/test_precomputed.py 2006-07-25 12:09:08 UTC (rev 2125) @@ -1,97 +0,0 @@ -from numpy.testing import * -import numpy as N - -set_local_path('../..') -from svm.classification import * -from svm.dataset import * -from svm.kernel import * -from svm.predict import * -from svm.regression import * -restore_path() - -class test_precomputed(NumpyTestCase): - def xcheck_precomputed_classification(self): - ModelType = LibSvmCClassificationModel - kernel = LinearKernel() - - labels1 = ([0] * 10) + ([1] * 10) + ([2] * 10) - x1 = N.random.randn(len(labels1), 10) - data1 = LibSvmClassificationDataSet(zip(labels1, x1)) - pcdata1 = data1.precompute(kernel) - - labels2 = ([0] * 5) + ([1] * 5) + ([2] * 5) - x2 = N.random.randn(len(labels2), x1.shape[1]) - data2 = LibSvmClassificationDataSet(zip(labels2, x2)) - - pcdata12 = pcdata1.combine(data2) - model = LibSvmCClassificationModel(kernel) - results = model.fit(pcdata12) - - reflabels = labels1 + labels2 - refx = N.vstack([x1, x2]) - refdata = LibSvmClassificationDataSet(zip(reflabels, refx)) - model = ModelType(kernel) - refresults = model.fit(refdata) - - assert_array_almost_equal(results.rho, refresults.rho) - assert_array_almost_equal(results.sv_coef, refresults.sv_coef) - - testdata = LibSvmTestDataSet(refx) - p = results.predict(testdata) - refp = refresults.predict(testdata) - assert_array_almost_equal(p, refp) - - pv = results.predict_values(testdata) - refpv = refresults.predict_values(testdata) - for v, refv in zip(pv, refpv): - for key, value in refv.iteritems(): - self.assertAlmostEqual(v[key], value) - - pp = results.predict_probability(testdata) - refpp = refresults.predict_probability(testdata) - for (lbl, p), (reflbl, refp) in zip(pp, refpp): - self.assertEqual(lbl, reflbl) - assert_array_almost_equal(p, refp) - - def check_precomputed_regression(self): - ModelType = LibSvmEpsilonRegressionModel - - kernel = LinearKernel() - - # this dataset remains constant - y1 = N.random.randn(50) - x1 = N.random.randn(len(y1), 10) - data1 = LibSvmRegressionDataSet(zip(y1, x1)) - pcdata1 = data1.precompute(kernel) - - # in a typical problem, this dataset would be smaller than the - # part that remains constant and would differ for each model - y2 = N.random.randn(5) - x2 = N.random.randn(len(y2), x1.shape[1]) - data2 = LibSvmRegressionDataSet(zip(y2, x2)) - - pcdata12 = pcdata1.combine(data2) - model = LibSvmEpsilonRegressionModel(kernel) - results = model.fit(pcdata12, LibSvmPredictor) - - # reference model, calculated without involving the - # precomputed Gram matrix - refy = N.concatenate([y1, y2]) - refx = N.vstack([x1, x2]) - refdata = LibSvmRegressionDataSet(zip(refy, refx)) - model = ModelType(kernel) - refresults = model.fit(refdata, LibSvmPredictor) - - self.assertAlmostEqual(results.rho, refresults.rho) - assert_array_almost_equal(results.sv_coef, refresults.sv_coef) - - # XXX sigmas don't match exactly. need to find out why. - #self.assertAlmostEqual(results.sigma, refresults.sigma) - - testdata = LibSvmTestDataSet(refx) - p = results.predict(testdata) - refp = refresults.predict(testdata) - assert_array_almost_equal(p, refp) - -if __name__ == '__main__': - NumpyTest().run() From scipy-svn at scipy.org Tue Jul 25 09:28:54 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 25 Jul 2006 08:28:54 -0500 (CDT) Subject: [Scipy-svn] r2126 - in trunk/Lib/sandbox/svm: . libsvm-2.82 tests Message-ID: <20060725132854.122E239C015@new.scipy.org> Author: fullung Date: 2006-07-25 08:28:26 -0500 (Tue, 25 Jul 2006) New Revision: 2126 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_classification.py trunk/Lib/sandbox/svm/tests/test_regression.py Log: Don't train probability models by default. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-25 12:09:08 UTC (rev 2125) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-25 13:28:26 UTC (rev 2126) @@ -142,7 +142,8 @@ Recognition. """ - def __init__(self, kernel, cost=1.0, weights=None, **kwargs): + def __init__(self, kernel, + cost=1.0, weights=None, probability=False, **kwargs): """ Parameters: @@ -153,9 +154,7 @@ self.cost = cost self.param.svm_type = libsvm.C_SVC self.param.C = cost - # always train probability model parameters - # XXX review this decision when we do performance optimization - self.param.probability = True + self.param.probability = probability class LibSvmNuClassificationModel(LibSvmClassificationModel): """ @@ -167,7 +166,8 @@ - Scholkopf, et al. New Support Vector Algorithms. """ - def __init__(self, kernel, nu=0.5, weights=None, **kwargs): + def __init__(self, kernel, + nu=0.5, weights=None, probability=False, **kwargs): """ Parameters: @@ -178,6 +178,4 @@ self.nu = nu self.param.svm_type = libsvm.NU_SVC self.param.nu = nu - # always train probability model parameters - # XXX review this decision when we do performance optimization - self.param.probability = True + self.param.probability = probability Modified: trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win =================================================================== --- trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win 2006-07-25 12:09:08 UTC (rev 2125) +++ trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win 2006-07-25 13:28:26 UTC (rev 2126) @@ -5,8 +5,8 @@ CXXFLAGSALL = -nologo -EHsc -GS -W3 -Wp64 $(CPPFLAGS) CXXFLAGSDBG = -MDd -Od -Z7 -RTCcsu CXXFLAGSOPT = -MD -O2 -CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSDBG) -#CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSOPT) +#CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSDBG) +CXXFLAGS = $(CXXFLAGSALL) $(CXXFLAGSOPT) LINKFLAGSALL = /nologo /DLL LINKFLAGSDBG = /DEBUG Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-25 12:09:08 UTC (rev 2125) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-25 13:28:26 UTC (rev 2126) @@ -100,14 +100,15 @@ Prediction. """ - def __init__(self, kernel, epsilon=0.1, cost=1.0, **kwargs): + def __init__(self, kernel, epsilon=0.1, cost=1.0, + probability=False, **kwargs): LibSvmRegressionModel.__init__(self, kernel, **kwargs) self.epsilon = epsilon self.cost = cost self.param.svm_type = libsvm.EPSILON_SVR self.param.p = epsilon self.param.C = cost - self.param.probability = True + self.param.probability = probability class LibSvmNuRegressionModel(LibSvmRegressionModel): """ @@ -116,11 +117,12 @@ See also: Schoelkopf, et al. New Support Vector Algorithms. """ - def __init__(self, kernel, nu=0.5, cost=1.0, **kwargs): + def __init__(self, kernel, + nu=0.5, cost=1.0, probability=False, **kwargs): LibSvmRegressionModel.__init__(self, kernel, **kwargs) self.nu = nu self.cost = cost self.param.svm_type = libsvm.NU_SVR self.param.nu = nu self.param.C = cost - self.param.probability = True + self.param.probability = probability Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-25 12:09:08 UTC (rev 2125) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-25 13:28:26 UTC (rev 2126) @@ -101,7 +101,7 @@ ] for kernel in kernels: - model = ModelType(kernel, cost, weights) + model = ModelType(kernel, cost, weights, True) results = model.fit(traindata) results.predict_probability(testdata) @@ -156,8 +156,8 @@ pctrndata1 = trndata1.precompute(kernel) pctrndata = pctrndata1.combine(trndata2) models = [ - LibSvmCClassificationModel(kernel, 2.0, weights), - LibSvmNuClassificationModel(kernel, 0.3, weights) + LibSvmCClassificationModel(kernel, 2.0, weights, True), + LibSvmNuClassificationModel(kernel, 0.3, weights, True) ] fitargs = [] # CustomKernel needs a precomputed dataset Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-25 12:09:08 UTC (rev 2125) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-25 13:28:26 UTC (rev 2126) @@ -35,7 +35,7 @@ N.array([1, 1])] traindata = LibSvmRegressionDataSet(zip(y, x)) testdata = LibSvmTestDataSet(x) - model = ModelType(LinearKernel()) + model = ModelType(LinearKernel(), probability=True) results = model.fit(traindata) results.predict(testdata) results.get_svr_probability() From scipy-svn at scipy.org Tue Jul 25 19:20:32 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 25 Jul 2006 18:20:32 -0500 (CDT) Subject: [Scipy-svn] r2127 - in trunk/Lib/sandbox/svm: . libsvm-2.82 Message-ID: <20060725232032.EF67939C0BF@new.scipy.org> Author: fullung Date: 2006-07-25 18:20:21 -0500 (Tue, 25 Jul 2006) New Revision: 2127 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/libsvm-2.82/svm.h trunk/Lib/sandbox/svm/libsvm.py trunk/Lib/sandbox/svm/predict.py trunk/Lib/sandbox/svm/regression.py Log: Use .ctypes.data_as instead of ctypes.cast. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-25 13:28:26 UTC (rev 2126) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-25 23:20:21 UTC (rev 2127) @@ -1,4 +1,4 @@ -from ctypes import cast, POINTER, c_int, c_double +from ctypes import POINTER, c_int, c_double import numpy as N from model import LibSvmModel @@ -103,9 +103,9 @@ self.weights[i] = weight self.param.nr_weight = len(weights) self.param.weight_label = \ - cast(self.weight_labels.ctypes.data, POINTER(c_int)) + self.weight_labels.ctypes.data_as(POINTER(c_int)) self.param.weight = \ - cast(self.weights.ctypes.data, POINTER(c_double)) + self.weights.ctypes.data_as(POINTER(c_double)) def cross_validate(self, dataset, nr_fold): """ @@ -121,7 +121,7 @@ """ problem = dataset._create_svm_problem() target = N.empty((len(dataset.data),), dtype=N.float64) - tp = cast(target.ctypes.data, POINTER(c_double)) + tp = target.ctypes.data_as(POINTER(c_double)) libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) total_correct = 0. for x, t in zip(dataset.data, target): Modified: trunk/Lib/sandbox/svm/libsvm-2.82/svm.h =================================================================== --- trunk/Lib/sandbox/svm/libsvm-2.82/svm.h 2006-07-25 13:28:26 UTC (rev 2126) +++ trunk/Lib/sandbox/svm/libsvm-2.82/svm.h 2006-07-25 23:20:21 UTC (rev 2127) @@ -6,10 +6,10 @@ #define LIBSVM_API __declspec(dllexport) #else #define LIBSVM_API __declspec(dllimport) -#endif /* _LIBSVM_EXPORTS */ +#endif /* LIBSVM_EXPORTS */ #else #define LIBSVM_API -#endif /* _WIN32 */ +#endif /* LIBSVM_DLL */ #ifdef __cplusplus extern "C" { Modified: trunk/Lib/sandbox/svm/libsvm.py =================================================================== --- trunk/Lib/sandbox/svm/libsvm.py 2006-07-25 13:28:26 UTC (rev 2126) +++ trunk/Lib/sandbox/svm/libsvm.py 2006-07-25 23:20:21 UTC (rev 2127) @@ -136,7 +136,7 @@ x = (POINTER(svm_node)*problem.l)() for i, (yi, xi) in enumerate(data): y[i] = yi - x[i] = cast(xi.ctypes.data, POINTER(svm_node)) + x[i] = xi.ctypes.data_as(POINTER(svm_node)) problem.x = x problem.y = y return problem Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-25 13:28:26 UTC (rev 2126) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-25 23:20:21 UTC (rev 2127) @@ -1,4 +1,4 @@ -from ctypes import cast, POINTER, c_double, addressof +from ctypes import POINTER, c_double, addressof import numpy as N from dataset import svm_node_dot @@ -35,14 +35,14 @@ def predict(self, x): x = self._transform_input(x) - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + xptr = x.ctypes.data_as(POINTER(libsvm.svm_node)) return libsvm.svm_predict(self.model, xptr) def predict_values(self, x, n): x = self._transform_input(x) - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + xptr = x.ctypes.data_as(POINTER(libsvm.svm_node)) v = N.empty((n,), dtype=N.float64) - vptr = cast(v.ctypes.data, POINTER(c_double)) + vptr = v.ctypes.data_as(POINTER(c_double)) libsvm.svm_predict_values(self.model, xptr, vptr) return v @@ -50,9 +50,9 @@ if not self.model.contents.param.probability: raise ValueError, 'not a probability model' x = self._transform_input(x) - xptr = cast(x.ctypes.data, POINTER(libsvm.svm_node)) + xptr = x.ctypes.data_as(POINTER(libsvm.svm_node)) pe = N.empty((n,), dtype=N.float64) - peptr = cast(pe.ctypes.data, POINTER(c_double)) + peptr = pe.ctypes.data_as(POINTER(c_double)) label = libsvm.svm_predict_probability(self.model, xptr, peptr) return label, pe Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-25 13:28:26 UTC (rev 2126) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-25 23:20:21 UTC (rev 2127) @@ -1,4 +1,4 @@ -from ctypes import cast, POINTER, c_double +from ctypes import POINTER, c_double import numpy as N from model import LibSvmModel @@ -65,7 +65,7 @@ problem = dataset._create_svm_problem() target = N.empty((len(dataset.data),), dtype=N.float64) - tp = cast(target.ctypes.data, POINTER(c_double)) + tp = target.ctypes.data_as(POINTER(c_double)) libsvm.svm_cross_validation(problem, self.param, nr_fold, tp) total_error = sumv = sumy = sumvv = sumyy = sumvy = 0. From scipy-svn at scipy.org Tue Jul 25 19:44:16 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 25 Jul 2006 18:44:16 -0500 (CDT) Subject: [Scipy-svn] r2128 - trunk/Lib/sandbox/svm/libsvm-2.82 Message-ID: <20060725234416.1F91539C093@new.scipy.org> Author: fullung Date: 2006-07-25 18:44:09 -0500 (Tue, 25 Jul 2006) New Revision: 2128 Modified: trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win Log: Use release build linker options. Modified: trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win =================================================================== --- trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win 2006-07-25 23:20:21 UTC (rev 2127) +++ trunk/Lib/sandbox/svm/libsvm-2.82/Makefile.win 2006-07-25 23:44:09 UTC (rev 2128) @@ -11,8 +11,8 @@ LINKFLAGSALL = /nologo /DLL LINKFLAGSDBG = /DEBUG LINKFLAGSOPT = -LINKFLAGS = $(LINKFLAGSALL) $(LINKFLAGSDBG) -#LINKFLAGS = $(LINKFLAGSALL) $(LINKFLAGSOPT) +#LINKFLAGS = $(LINKFLAGSALL) $(LINKFLAGSDBG) +LINKFLAGS = $(LINKFLAGSALL) $(LINKFLAGSOPT) all: libsvm_.dll From scipy-svn at scipy.org Wed Jul 26 04:40:43 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 26 Jul 2006 03:40:43 -0500 (CDT) Subject: [Scipy-svn] r2129 - trunk Message-ID: <20060726084043.63F3839C0E0@new.scipy.org> Author: edschofield Date: 2006-07-26 03:40:39 -0500 (Wed, 26 Jul 2006) New Revision: 2129 Modified: trunk/INSTALL.txt Log: Updated INSTALL.txt. Modified: trunk/INSTALL.txt =================================================================== --- trunk/INSTALL.txt 2006-07-25 23:44:09 UTC (rev 2128) +++ trunk/INSTALL.txt 2006-07-26 08:40:39 UTC (rev 2129) @@ -32,13 +32,15 @@ __ http://www.python.org -2) NumPy__ 0.9.6 or newer +2) NumPy__ 1.0b1 or newer + Debian package: python-numpy + __ http://www.numpy.org/ 3) Complete LAPACK__ library (see NOTES 1, 2, 3) - Debian packages: atlas2-headers atlas2-base atlas2-base-dev + Debian packages: atlas3-headers atlas3-base atlas3-base-dev Various SciPy packages do linear algebra computations using the LAPACK routines. SciPy's setup.py scripts can use number of different LAPACK From scipy-svn at scipy.org Wed Jul 26 06:45:41 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 26 Jul 2006 05:45:41 -0500 (CDT) Subject: [Scipy-svn] r2130 - trunk/Lib/maxentropy Message-ID: <20060726104541.1EF9439C0EA@new.scipy.org> Author: edschofield Date: 2006-07-26 05:45:37 -0500 (Wed, 26 Jul 2006) New Revision: 2130 Modified: trunk/Lib/maxentropy/maxentropy.py Log: Small doc fix for maxentropy.py Modified: trunk/Lib/maxentropy/maxentropy.py =================================================================== --- trunk/Lib/maxentropy/maxentropy.py 2006-07-26 08:40:39 UTC (rev 2129) +++ trunk/Lib/maxentropy/maxentropy.py 2006-07-26 10:45:37 UTC (rev 2130) @@ -953,7 +953,8 @@ entire sample space, since q(w,x) = 0 for all w,x not in the training set. - The entropy dual function equals the negative log likelihood. + The entropy dual function is proportional to the negative log + likelihood. Compare to the entropy dual of an unconditional model: L(theta) = log(Z) - theta^T . K From scipy-svn at scipy.org Wed Jul 26 06:46:44 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 26 Jul 2006 05:46:44 -0500 (CDT) Subject: [Scipy-svn] r2131 - trunk/Lib/sparse Message-ID: <20060726104644.C53D739C0EA@new.scipy.org> Author: edschofield Date: 2006-07-26 05:46:41 -0500 (Wed, 26 Jul 2006) New Revision: 2131 Modified: trunk/Lib/sparse/info.py Log: Doc fixes for sparse/info.py Modified: trunk/Lib/sparse/info.py =================================================================== --- trunk/Lib/sparse/info.py 2006-07-26 10:45:37 UTC (rev 2130) +++ trunk/Lib/sparse/info.py 2006-07-26 10:46:41 UTC (rev 2131) @@ -14,12 +14,13 @@ (4) dok_matrix: Dictionary of Keys format To construct a matrix efficiently, use either lil_matrix (recommended) or -dok_matrix. The lil_matrix class supports basic slicing and fancy +dok_matrix. The lil_matrix class supports basic slicing and fancy indexing with a similar syntax to NumPy arrays. To perform manipulations such as multiplication or inversion, first -convert the matrix to either CSC or CSR format. lil_matrix format is -row-based, so conversion to CSR very most efficient. +convert the matrix to either CSC or CSR format. The lil_matrix format is +row-based, so conversion to CSR is efficient, whereas conversion to CSC +is less so. Example: Construct a 10x1000 lil_matrix and add some values to it: From scipy-svn at scipy.org Wed Jul 26 08:38:40 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 26 Jul 2006 07:38:40 -0500 (CDT) Subject: [Scipy-svn] r2132 - trunk/Lib/sparse Message-ID: <20060726123840.1D9DA39C043@new.scipy.org> Author: edschofield Date: 2006-07-26 07:38:37 -0500 (Wed, 26 Jul 2006) New Revision: 2132 Modified: trunk/Lib/sparse/sparse.py Log: Better exception handling for unsupported sparse matrix indexing (ticket #226). Cast zeros in lil_matrix objects to correct data type. Modified: trunk/Lib/sparse/sparse.py =================================================================== --- trunk/Lib/sparse/sparse.py 2006-07-26 10:46:41 UTC (rev 2131) +++ trunk/Lib/sparse/sparse.py 2006-07-26 12:38:37 UTC (rev 2132) @@ -2449,6 +2449,8 @@ elif isinstance(i, int): if not (i>=0 and i=0 and j Author: fullung Date: 2006-07-26 08:18:54 -0500 (Wed, 26 Jul 2006) New Revision: 2133 Modified: trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/tests/test_classification.py Log: Extend classification tests. Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-26 12:38:37 UTC (rev 2132) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-26 13:18:54 UTC (rev 2133) @@ -165,6 +165,9 @@ def __init__(self, origdata): self.data = map(lambda x: convert_to_svm_node(x), origdata) + def __len__(self): + return len(self.data) + def __iter__(self): return self.data.__iter__() Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-26 12:38:37 UTC (rev 2132) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-26 13:18:54 UTC (rev 2133) @@ -10,101 +10,93 @@ class test_classification(NumpyTestCase): def check_basics(self): - Model = LibSvmCClassificationModel kernel = LinearKernel() - Model(kernel) - Model(kernel, cost=1.0) + # C-SVC + ModelType = LibSvmCClassificationModel + ModelType(kernel) + ModelType(kernel, cost=1.0) weights = [(2, 10.0), (1, 20.0), (0, 30.0)] - Model(kernel, weights=weights) - Model(kernel, 1.0, weights) - Model(kernel, cost=1.0, weights=weights) + ModelType(kernel, weights=weights) + ModelType(kernel, 1.0, weights) + ModelType(kernel, cost=1.0, weights=weights) + # nu-SVC + ModelType = LibSvmNuClassificationModel + ModelType(kernel) + ModelType(kernel, nu=0.5) + ModelType(kernel, weights=weights) + ModelType(kernel, 0.5, weights) - Model = LibSvmNuClassificationModel - Model(kernel) - Model(kernel, nu=0.5) - Model(kernel, weights=weights) - Model(kernel, 0.5, weights) - - def check_c_basics(self): - ModelType = LibSvmCClassificationModel - + def _make_basic_datasets(self): labels = [0, 1, 1, 2] x = [N.array([0, 0]), N.array([0, 1]), N.array([1, 0]), N.array([1, 1])] traindata = LibSvmClassificationDataSet(zip(labels, x)) - model = ModelType(RBFKernel(traindata.gamma)) + testdata = LibSvmTestDataSet(x) + return traindata, testdata + + def check_c_basics(self): + traindata, testdata = self._make_basic_datasets() + kernel = RBFKernel(traindata.gamma) + model = LibSvmCClassificationModel(kernel) results = model.fit(traindata) - testdata = LibSvmTestDataSet(x) - results.predict(testdata) + p = results.predict(testdata) + assert_array_equal(p, [1, 1, 1, 1]) results.predict_values(testdata) - def check_c_more(self): - ModelType = LibSvmCClassificationModel - - labels = [0, 1, 1, 2] - x = [N.array([0, 0]), - N.array([0, 1]), - N.array([1, 0]), - N.array([1, 1])] - traindata = LibSvmClassificationDataSet(zip(labels, x)) - cost = 10.0 - weights = [(1, 10.0)] - testdata = LibSvmTestDataSet(x) - + def _make_basic_kernels(self, gamma): kernels = [ LinearKernel(), - PolynomialKernel(3, traindata.gamma, 0.0), - RBFKernel(traindata.gamma) + PolynomialKernel(3, gamma, 0.0), + RBFKernel(gamma) ] - expected_rhos = [ - [-0.999349, -1.0, -3.0], - [0.375, -1.0, -1.153547], - [0.671181, 0.0, -0.671133] - ] - expected_errors = [0, 1, 0] + return kernels - for kernel, expected_rho, expected_error in \ - zip(kernels, expected_rhos, expected_errors): - model = ModelType(kernel, cost, weights) + def _classify_basic(self, ModelType, + modelargs, expected_rhos, expected_ps): + traindata, testdata = self._make_basic_datasets() + kernels = self._make_basic_kernels(traindata.gamma) + for kernel, expected_rho, expected_p in \ + zip(kernels, expected_rhos, expected_ps): + args = (kernel,) + modelargs + model = ModelType(*args) results = model.fit(traindata) - self.assertEqual(results.labels, [0, 1, 2]) - self.assertEqual(results.nSV, [1, 2, 1]) - - # use decimal=4 to suppress slight differences in values - # calculated for rho on Windows with MSVC 7.1 and on - # Fedora Core 4 with GCC 4.0.0. + # decimal=4 due to compiler-dependent variations in rho assert_array_almost_equal(results.rho, expected_rho, decimal=4) + p = N.array(results.predict(testdata)) + assert_array_equal(p, expected_p) - predictions = N.array(results.predict(testdata)) - self.assertEqual(N.sum(predictions != labels), expected_error) + def check_c_more(self): + cost = 10.0 + weights = [(1, 10.0)] + modelargs = cost, weights + expected_rhos = [[-0.999349, -1.0, -3.0], + [0.375, -1.0, -1.153547], + [0.671181, 0.0, -0.671133]] + expected_ps = [[0, 1, 1, 2], [1, 1, 1, 2], [0, 1, 1, 2]] + self._classify_basic(LibSvmCClassificationModel, + modelargs, expected_rhos, expected_ps) def check_c_probability(self): - ModelType = LibSvmCClassificationModel - - labels = [0, 1, 1, 2] - x = [N.array([0, 0]), - N.array([0, 1]), - N.array([1, 0]), - N.array([1, 1])] - traindata = LibSvmClassificationDataSet(zip(labels, x)) + traindata, testdata = self._make_basic_datasets() + nu = 0.5 cost = 10.0 weights = [(1, 10.0)] - testdata = LibSvmTestDataSet(x) - - kernels = [ - LinearKernel(), - PolynomialKernel(3, traindata.gamma, 0.0), - RBFKernel(traindata.gamma) + kernels = self._make_basic_kernels(traindata.gamma) + models = [ + (LibSvmCClassificationModel, (cost, weights)), + (LibSvmNuClassificationModel, (nu, weights)) ] + for ModelType, modelargs in models: + for kernel in kernels: + args = (kernel,) + modelargs + kwargs = {'probability' : True} + model = ModelType(*args, **kwargs) + results = model.fit(traindata) + results.predict_probability(testdata) - for kernel in kernels: - model = ModelType(kernel, cost, weights, True) - results = model.fit(traindata) - results.predict_probability(testdata) - def check_cross_validate(self): labels = ([-1] * 50) + ([1] * 50) x = N.random.randn(len(labels), 10) @@ -116,9 +108,26 @@ # XXX check cross-validation with and without probability # output enabled - def check_nu_train(self): - pass + def check_nu_basics(self): + traindata, testdata = self._make_basic_datasets() + kernel = RBFKernel(traindata.gamma) + model = LibSvmNuClassificationModel(kernel) + results = model.fit(traindata) + p = results.predict(testdata) + assert_array_equal(p, [0, 1, 1, 2]) + v = results.predict_values(testdata) + def check_nu_more(self): + nu = 0.5 + weights = [(1, 10.0)] + modelargs = nu, weights + expected_rhos = [[-1.0, -1.0, -3.0], + [-1.0, -1.0, -1.15384846], + [0.6712142, 0.0, -0.6712142]] + expected_ps = [[0, 1, 1, 2]] * 3 + self._classify_basic(LibSvmNuClassificationModel, + modelargs, expected_rhos, expected_ps) + def _make_datasets(self): labels1 = N.random.random_integers(0, 2, 100) x1 = N.random.randn(len(labels1), 10) From scipy-svn at scipy.org Wed Jul 26 09:32:12 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 26 Jul 2006 08:32:12 -0500 (CDT) Subject: [Scipy-svn] r2134 - trunk/Lib/sparse Message-ID: <20060726133212.44F5639C0EA@new.scipy.org> Author: edschofield Date: 2006-07-26 08:32:09 -0500 (Wed, 26 Jul 2006) New Revision: 2134 Modified: trunk/Lib/sparse/sparse.py Log: Allowed indexing into sparse matrices with integer indices of a different size to that of Python int. Fixes ticket #218. Modified: trunk/Lib/sparse/sparse.py =================================================================== --- trunk/Lib/sparse/sparse.py 2006-07-26 13:18:54 UTC (rev 2133) +++ trunk/Lib/sparse/sparse.py 2006-07-26 13:32:09 UTC (rev 2134) @@ -915,7 +915,7 @@ func = getattr(sparsetools, self.ftype+'cscgetel') ind, val = func(self.data, self.rowind, self.indptr, row, col) return val - elif isinstance(key, int): + elif isintlike(key): # Was: return self.data[key] # If this is allowed, it should return the relevant row, as for # dense matrices (and __len__ should be supported again). @@ -1472,9 +1472,7 @@ func = getattr(sparsetools, self.ftype+'cscgetel') ind, val = func(self.data, self.colind, self.indptr, col, row) return val - elif isinstance(key, int): - # If an integer index is allowed, it should return the relevant row, - # as for dense matrices (and __len__ should be supported again). + elif isintlike(key): return self[key, :] else: raise IndexError, "invalid index" @@ -1702,7 +1700,7 @@ """ try: i, j = key - assert isinstance(i, int) and isinstance(j, int) + assert isintlike(i) and isintlike(j) except (AssertionError, TypeError, ValueError): raise IndexError, "index must be a pair of integers" try: @@ -1724,15 +1722,15 @@ i, j = key # Bounds checking - if isinstance(i, int): + if isintlike(i): if i < 0 or i >= self.shape[0]: raise IndexError, "index out of bounds" - if isinstance(j, int): + if isintlike(j): if j < 0 or j >= self.shape[1]: raise IndexError, "index out of bounds" # First deal with the case where both i and j are integers - if isinstance(i, int) and isinstance(j, int): + if isintlike(i) and isintlike(j): return dict.get(self, key, 0.) else: # Either i or j is a slice, sequence, or invalid. If i is a slice @@ -1745,12 +1743,12 @@ seq = i else: # Make sure i is an integer. (But allow it to be a subclass of int). - if not isinstance(i, int): + if not isintlike(i): raise TypeError, "index must be a pair of integers or slices" seq = None if seq is not None: # i is a seq - if isinstance(j, int): + if isintlike(j): # Create a new matrix of the correct dimensions first = seq[0] last = seq[-1] @@ -1815,17 +1813,17 @@ i, j = key # First deal with the case where both i and j are integers - if isinstance(i, int) and isinstance(j, int): + if isintlike(i) and isintlike(j): if i < 0 or i >= self.shape[0] or j < 0 or j >= self.shape[1]: raise IndexError, "index out of bounds" - if isinstance(value, int) and value == 0: + if isintlike(value) and value == 0: if key in self: # get rid of it something already there del self[key] else: # Ensure value is a single element, not a sequence - if isinstance(value, float) or isinstance(value, int) or \ - isinstance(value, complex): - dict.__setitem__(self, key, value) + if isinstance(value, float) or isintlike(value) or \ + isinstance(value, complex): + dict.__setitem__(self, key, self.dtype.type(value)) newrows = max(self.shape[0], int(key[0])+1) newcols = max(self.shape[1], int(key[1])+1) self.shape = (newrows, newcols) @@ -1842,7 +1840,7 @@ seq = i else: # Make sure i is an integer. (But allow it to be a subclass of int). - if not isinstance(i, int): + if not isintlike(i): raise TypeError, "index must be a pair of integers or slices" seq = None if seq is not None: @@ -2232,7 +2230,7 @@ elif arg1 is None: # clumsy! We should make ALL arguments # keyword arguments instead! # Initialize an empty matrix. - if not isinstance(dims, tuple) or not isinstance(dims[0], int): + if not isinstance(dims, tuple) or not isintlike(dims[0]): raise TypeError, "dimensions not understood" self.shape = dims self.dtype = getdtype(dtype, default=float) @@ -2446,7 +2444,7 @@ if type(i) is slice: raise IndexError, "lil_matrix supports slices only of a single row" # TODO: add support for this, like in __setitem__ - elif isinstance(i, int): + elif isintlike(i): if not (i>=0 and i=0 and j=0 and i=0 and j Author: edschofield Date: 2006-07-26 08:36:01 -0500 (Wed, 26 Jul 2006) New Revision: 2135 Added: tags/0.5.0/ Log: Created 0.5.0 branch Copied: tags/0.5.0 (from rev 2134, trunk) From scipy-svn at scipy.org Wed Jul 26 09:44:19 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 26 Jul 2006 08:44:19 -0500 (CDT) Subject: [Scipy-svn] r2136 - in tags/0.5.0/Lib: . fftpack linalg special weave Message-ID: <20060726134419.6416539C043@new.scipy.org> Author: edschofield Date: 2006-07-26 08:44:08 -0500 (Wed, 26 Jul 2006) New Revision: 2136 Modified: tags/0.5.0/Lib/fftpack/fftpack_version.py tags/0.5.0/Lib/linalg/linalg_version.py tags/0.5.0/Lib/special/special_version.py tags/0.5.0/Lib/version.py tags/0.5.0/Lib/weave/weave_version.py Log: Updated version numbers; removed SVN version magic for 0.5.0 release Modified: tags/0.5.0/Lib/fftpack/fftpack_version.py =================================================================== --- tags/0.5.0/Lib/fftpack/fftpack_version.py 2006-07-26 13:36:01 UTC (rev 2135) +++ tags/0.5.0/Lib/fftpack/fftpack_version.py 2006-07-26 13:44:08 UTC (rev 2136) @@ -1,6 +1,5 @@ major = 0 -minor = 4 -micro = 3 +minor = 5 +micro = 0 - fftpack_version = '%(major)d.%(minor)d.%(micro)d' % (locals ()) Modified: tags/0.5.0/Lib/linalg/linalg_version.py =================================================================== --- tags/0.5.0/Lib/linalg/linalg_version.py 2006-07-26 13:36:01 UTC (rev 2135) +++ tags/0.5.0/Lib/linalg/linalg_version.py 2006-07-26 13:44:08 UTC (rev 2136) @@ -1,5 +1,5 @@ major = 0 -minor = 4 -micro = 9 +minor = 5 +micro = 0 linalg_version = '%(major)d.%(minor)d.%(micro)d' % (locals ()) Modified: tags/0.5.0/Lib/special/special_version.py =================================================================== --- tags/0.5.0/Lib/special/special_version.py 2006-07-26 13:36:01 UTC (rev 2135) +++ tags/0.5.0/Lib/special/special_version.py 2006-07-26 13:44:08 UTC (rev 2136) @@ -1,9 +1,6 @@ major = 0 -minor = 4 -micro = 9 -release_level = 'beta' +minor = 5 +micro = 0 -from __svn_version__ import svn_version - -special_version = '%(major)d.%(minor)d.%(micro)d_%(svn_version)s'\ +special_version = '%(major)d.%(minor)d.%(micro)d'\ % (locals ()) Modified: tags/0.5.0/Lib/version.py =================================================================== --- tags/0.5.0/Lib/version.py 2006-07-26 13:36:01 UTC (rev 2135) +++ tags/0.5.0/Lib/version.py 2006-07-26 13:44:08 UTC (rev 2136) @@ -1,13 +1 @@ version = '0.5.0' - -import os -svn_version_file = os.path.join(os.path.dirname(__file__), - '__svn_version__.py') - -if os.path.isfile(svn_version_file): - import imp - svn = imp.load_module('scipy.__svn_version__', - open(svn_version_file), - svn_version_file, - ('.py','U',1)) - version += '.'+svn.version Modified: tags/0.5.0/Lib/weave/weave_version.py =================================================================== --- tags/0.5.0/Lib/weave/weave_version.py 2006-07-26 13:36:01 UTC (rev 2135) +++ tags/0.5.0/Lib/weave/weave_version.py 2006-07-26 13:44:08 UTC (rev 2136) @@ -1,12 +1,6 @@ major = 0 -minor = 4 -micro = 9 -#release_level = 'alpha' -release_level = '' +minor = 5 +micro = 0 -if release_level: - weave_version = '%(major)d.%(minor)d.%(micro)d_%(release_level)s'\ - % (locals ()) -else: - weave_version = '%(major)d.%(minor)d.%(micro)d'\ - % (locals ()) +weave_version = '%(major)d.%(minor)d.%(micro)d'\ + % (locals ()) From scipy-svn at scipy.org Wed Jul 26 17:45:38 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 26 Jul 2006 16:45:38 -0500 (CDT) Subject: [Scipy-svn] r2137 - trunk/Lib/weave Message-ID: <20060726214538.AFD1B39C00A@new.scipy.org> Author: stefan Date: 2006-07-26 16:45:21 -0500 (Wed, 26 Jul 2006) New Revision: 2137 Modified: trunk/Lib/weave/swig2_spec.py trunk/Lib/weave/swigptr2.py Log: Make weave work with new versions of SWIG (for Prabhu Ramachandran). Modified: trunk/Lib/weave/swig2_spec.py =================================================================== --- trunk/Lib/weave/swig2_spec.py 2006-07-26 13:44:08 UTC (rev 2136) +++ trunk/Lib/weave/swig2_spec.py 2006-07-26 21:45:21 UTC (rev 2137) @@ -264,6 +264,8 @@ self.support_code.append(swigptr2.swigptr2_code_v1) elif rv == 2: self.support_code.append(swigptr2.swigptr2_code_v2) + elif rv == 3: + self.support_code.append(swigptr2.swigptr2_code_v3) else: raise AssertionError, "Unsupported version of the SWIG runtime:", rv Modified: trunk/Lib/weave/swigptr2.py =================================================================== --- trunk/Lib/weave/swigptr2.py 2006-07-26 13:44:08 UTC (rev 2136) +++ trunk/Lib/weave/swigptr2.py 2006-07-26 21:45:21 UTC (rev 2137) @@ -3,7 +3,7 @@ # The code is basically all copied out from the SWIG wrapper code but # it has been hand edited for brevity. # -# Prabhu Ramachandran +# Prabhu Ramachandran ###################################################################### # This is for SWIG-1.3.x where x < 22. @@ -2969,3 +2969,2488 @@ """ + +###################################################################### +# This is for SWIG-1.3.28 and higher. +# SWIG_RUNTIME_VERSION == "3" + +# All this does is to include the contents of the file generated by +# this command: +# swig -python -external-runtime +swigptr2_code_v3 = """ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 1.3.30 + * + * This file is not intended to be easily readable and contains a number of + * coding conventions designed to improve portability and efficiency. Do not make + * changes to this file unless you know what you are doing--modify the SWIG + * interface file instead. + * ----------------------------------------------------------------------------- */ + +/* ----------------------------------------------------------------------------- + * This section contains generic SWIG labels for method/variable + * declarations/attributes, and other compiler dependent labels. + * ----------------------------------------------------------------------------- */ + +/* template workaround for compilers that cannot correctly implement the C++ standard */ +#ifndef SWIGTEMPLATEDISAMBIGUATOR +# if defined(__SUNPRO_CC) +# if (__SUNPRO_CC <= 0x560) +# define SWIGTEMPLATEDISAMBIGUATOR template +# else +# define SWIGTEMPLATEDISAMBIGUATOR +# endif +# else +# define SWIGTEMPLATEDISAMBIGUATOR +# endif +#endif + +/* inline attribute */ +#ifndef SWIGINLINE +# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__)) +# define SWIGINLINE inline +# else +# define SWIGINLINE +# endif +#endif + +/* attribute recognised by some compilers to avoid 'unused' warnings */ +#ifndef SWIGUNUSED +# if defined(__GNUC__) +# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) +# define SWIGUNUSED __attribute__ ((__unused__)) +# else +# define SWIGUNUSED +# endif +# elif defined(__ICC) +# define SWIGUNUSED __attribute__ ((__unused__)) +# else +# define SWIGUNUSED +# endif +#endif + +#ifndef SWIGUNUSEDPARM +# ifdef __cplusplus +# define SWIGUNUSEDPARM(p) +# else +# define SWIGUNUSEDPARM(p) p SWIGUNUSED +# endif +#endif + +/* internal SWIG method */ +#ifndef SWIGINTERN +# define SWIGINTERN static SWIGUNUSED +#endif + +/* internal inline SWIG method */ +#ifndef SWIGINTERNINLINE +# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE +#endif + +/* exporting methods */ +#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) +# ifndef GCC_HASCLASSVISIBILITY +# define GCC_HASCLASSVISIBILITY +# endif +#endif + +#ifndef SWIGEXPORT +# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +# if defined(STATIC_LINKED) +# define SWIGEXPORT +# else +# define SWIGEXPORT __declspec(dllexport) +# endif +# else +# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) +# define SWIGEXPORT __attribute__ ((visibility("default"))) +# else +# define SWIGEXPORT +# endif +# endif +#endif + +/* calling conventions for Windows */ +#ifndef SWIGSTDCALL +# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +# define SWIGSTDCALL __stdcall +# else +# define SWIGSTDCALL +# endif +#endif + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) +# define _CRT_SECURE_NO_DEPRECATE +#endif +/* Errors in SWIG */ +#define SWIG_UnknownError -1 +#define SWIG_IOError -2 +#define SWIG_RuntimeError -3 +#define SWIG_IndexError -4 +#define SWIG_TypeError -5 +#define SWIG_DivisionByZero -6 +#define SWIG_OverflowError -7 +#define SWIG_SyntaxError -8 +#define SWIG_ValueError -9 +#define SWIG_SystemError -10 +#define SWIG_AttributeError -11 +#define SWIG_MemoryError -12 +#define SWIG_NullReferenceError -13 + + +/* Errors in SWIG */ +#define SWIG_UnknownError -1 +#define SWIG_IOError -2 +#define SWIG_RuntimeError -3 +#define SWIG_IndexError -4 +#define SWIG_TypeError -5 +#define SWIG_DivisionByZero -6 +#define SWIG_OverflowError -7 +#define SWIG_SyntaxError -8 +#define SWIG_ValueError -9 +#define SWIG_SystemError -10 +#define SWIG_AttributeError -11 +#define SWIG_MemoryError -12 +#define SWIG_NullReferenceError -13 + + +/* ----------------------------------------------------------------------------- + * swigrun.swg + * + * This file contains generic CAPI SWIG runtime support for pointer + * type checking. + * ----------------------------------------------------------------------------- */ + +/* This should only be incremented when either the layout of swig_type_info changes, + or for whatever reason, the runtime changes incompatibly */ +#define SWIG_RUNTIME_VERSION "3" + +/* define SWIG_TYPE_TABLE_NAME as "SWIG_TYPE_TABLE" */ +#ifdef SWIG_TYPE_TABLE +# define SWIG_QUOTE_STRING(x) #x +# define SWIG_EXPAND_AND_QUOTE_STRING(x) SWIG_QUOTE_STRING(x) +# define SWIG_TYPE_TABLE_NAME SWIG_EXPAND_AND_QUOTE_STRING(SWIG_TYPE_TABLE) +#else +# define SWIG_TYPE_TABLE_NAME +#endif + +/* + You can use the SWIGRUNTIME and SWIGRUNTIMEINLINE macros for + creating a static or dynamic library from the swig runtime code. + In 99.9% of the cases, swig just needs to declare them as 'static'. + + But only do this if is strictly necessary, ie, if you have problems + with your compiler or so. +*/ + +#ifndef SWIGRUNTIME +# define SWIGRUNTIME SWIGINTERN +#endif + +#ifndef SWIGRUNTIMEINLINE +# define SWIGRUNTIMEINLINE SWIGRUNTIME SWIGINLINE +#endif + +/* Generic buffer size */ +#ifndef SWIG_BUFFER_SIZE +# define SWIG_BUFFER_SIZE 1024 +#endif + +/* Flags for pointer conversions */ +#define SWIG_POINTER_DISOWN 0x1 + +/* Flags for new pointer objects */ +#define SWIG_POINTER_OWN 0x1 + + +/* + Flags/methods for returning states. + + The swig conversion methods, as ConvertPtr, return and integer + that tells if the conversion was successful or not. And if not, + an error code can be returned (see swigerrors.swg for the codes). + + Use the following macros/flags to set or process the returning + states. + + In old swig versions, you usually write code as: + + if (SWIG_ConvertPtr(obj,vptr,ty.flags) != -1) { + // success code + } else { + //fail code + } + + Now you can be more explicit as: + + int res = SWIG_ConvertPtr(obj,vptr,ty.flags); + if (SWIG_IsOK(res)) { + // success code + } else { + // fail code + } + + that seems to be the same, but now you can also do + + Type *ptr; + int res = SWIG_ConvertPtr(obj,(void **)(&ptr),ty.flags); + if (SWIG_IsOK(res)) { + // success code + if (SWIG_IsNewObj(res) { + ... + delete *ptr; + } else { + ... + } + } else { + // fail code + } + + I.e., now SWIG_ConvertPtr can return new objects and you can + identify the case and take care of the deallocation. Of course that + requires also to SWIG_ConvertPtr to return new result values, as + + int SWIG_ConvertPtr(obj, ptr,...) { + if () { + if () { + *ptr = ; + return SWIG_NEWOBJ; + } else { + *ptr = ; + return SWIG_OLDOBJ; + } + } else { + return SWIG_BADOBJ; + } + } + + Of course, returning the plain '0(success)/-1(fail)' still works, but you can be + more explicit by returning SWIG_BADOBJ, SWIG_ERROR or any of the + swig errors code. + + Finally, if the SWIG_CASTRANK_MODE is enabled, the result code + allows to return the 'cast rank', for example, if you have this + + int food(double) + int fooi(int); + + and you call + + food(1) // cast rank '1' (1 -> 1.0) + fooi(1) // cast rank '0' + + just use the SWIG_AddCast()/SWIG_CheckState() + + + */ +#define SWIG_OK (0) +#define SWIG_ERROR (-1) +#define SWIG_IsOK(r) (r >= 0) +#define SWIG_ArgError(r) ((r != SWIG_ERROR) ? r : SWIG_TypeError) + +/* The CastRankLimit says how many bits are used for the cast rank */ +#define SWIG_CASTRANKLIMIT (1 << 8) +/* The NewMask denotes the object was created (using new/malloc) */ +#define SWIG_NEWOBJMASK (SWIG_CASTRANKLIMIT << 1) +/* The TmpMask is for in/out typemaps that use temporal objects */ +#define SWIG_TMPOBJMASK (SWIG_NEWOBJMASK << 1) +/* Simple returning values */ +#define SWIG_BADOBJ (SWIG_ERROR) +#define SWIG_OLDOBJ (SWIG_OK) +#define SWIG_NEWOBJ (SWIG_OK | SWIG_NEWOBJMASK) +#define SWIG_TMPOBJ (SWIG_OK | SWIG_TMPOBJMASK) +/* Check, add and del mask methods */ +#define SWIG_AddNewMask(r) (SWIG_IsOK(r) ? (r | SWIG_NEWOBJMASK) : r) +#define SWIG_DelNewMask(r) (SWIG_IsOK(r) ? (r & ~SWIG_NEWOBJMASK) : r) +#define SWIG_IsNewObj(r) (SWIG_IsOK(r) && (r & SWIG_NEWOBJMASK)) +#define SWIG_AddTmpMask(r) (SWIG_IsOK(r) ? (r | SWIG_TMPOBJMASK) : r) +#define SWIG_DelTmpMask(r) (SWIG_IsOK(r) ? (r & ~SWIG_TMPOBJMASK) : r) +#define SWIG_IsTmpObj(r) (SWIG_IsOK(r) && (r & SWIG_TMPOBJMASK)) + + +/* Cast-Rank Mode */ +#if defined(SWIG_CASTRANK_MODE) +# ifndef SWIG_TypeRank +# define SWIG_TypeRank unsigned long +# endif +# ifndef SWIG_MAXCASTRANK /* Default cast allowed */ +# define SWIG_MAXCASTRANK (2) +# endif +# define SWIG_CASTRANKMASK ((SWIG_CASTRANKLIMIT) -1) +# define SWIG_CastRank(r) (r & SWIG_CASTRANKMASK) +SWIGINTERNINLINE int SWIG_AddCast(int r) { + return SWIG_IsOK(r) ? ((SWIG_CastRank(r) < SWIG_MAXCASTRANK) ? (r + 1) : SWIG_ERROR) : r; +} +SWIGINTERNINLINE int SWIG_CheckState(int r) { + return SWIG_IsOK(r) ? SWIG_CastRank(r) + 1 : 0; +} +#else /* no cast-rank mode */ +# define SWIG_AddCast +# define SWIG_CheckState(r) (SWIG_IsOK(r) ? 1 : 0) +#endif + + + + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void *(*swig_converter_func)(void *); +typedef struct swig_type_info *(*swig_dycast_func)(void **); + +/* Structure to store inforomation on one type */ +typedef struct swig_type_info { + const char *name; /* mangled name of this type */ + const char *str; /* human readable name of this type */ + swig_dycast_func dcast; /* dynamic cast function down a hierarchy */ + struct swig_cast_info *cast; /* linked list of types that can cast into this type */ + void *clientdata; /* language specific type data */ + int owndata; /* flag if the structure owns the clientdata */ +} swig_type_info; + +/* Structure to store a type and conversion function used for casting */ +typedef struct swig_cast_info { + swig_type_info *type; /* pointer to type that is equivalent to this type */ + swig_converter_func converter; /* function to cast the void pointers */ + struct swig_cast_info *next; /* pointer to next cast in linked list */ + struct swig_cast_info *prev; /* pointer to the previous cast */ +} swig_cast_info; + +/* Structure used to store module information + * Each module generates one structure like this, and the runtime collects + * all of these structures and stores them in a circularly linked list.*/ +typedef struct swig_module_info { + swig_type_info **types; /* Array of pointers to swig_type_info structures that are in this module */ + size_t size; /* Number of types in this module */ + struct swig_module_info *next; /* Pointer to next element in circularly linked list */ + swig_type_info **type_initial; /* Array of initially generated type structures */ + swig_cast_info **cast_initial; /* Array of initially generated casting structures */ + void *clientdata; /* Language specific module data */ +} swig_module_info; + +/* + Compare two type names skipping the space characters, therefore + "char*" == "char *" and "Class" == "Class", etc. + + Return 0 when the two name types are equivalent, as in + strncmp, but skipping ' '. +*/ +SWIGRUNTIME int +SWIG_TypeNameComp(const char *f1, const char *l1, + const char *f2, const char *l2) { + for (;(f1 != l1) && (f2 != l2); ++f1, ++f2) { + while ((*f1 == ' ') && (f1 != l1)) ++f1; + while ((*f2 == ' ') && (f2 != l2)) ++f2; + if (*f1 != *f2) return (*f1 > *f2) ? 1 : -1; + } + return (l1 - f1) - (l2 - f2); +} + +/* + Check type equivalence in a name list like ||... + Return 0 if not equal, 1 if equal +*/ +SWIGRUNTIME int +SWIG_TypeEquiv(const char *nb, const char *tb) { + int equiv = 0; + const char* te = tb + strlen(tb); + const char* ne = nb; + while (!equiv && *ne) { + for (nb = ne; *ne; ++ne) { + if (*ne == '|') break; + } + equiv = (SWIG_TypeNameComp(nb, ne, tb, te) == 0) ? 1 : 0; + if (*ne) ++ne; + } + return equiv; +} + +/* + Check type equivalence in a name list like ||... + Return 0 if equal, -1 if nb < tb, 1 if nb > tb +*/ +SWIGRUNTIME int +SWIG_TypeCompare(const char *nb, const char *tb) { + int equiv = 0; + const char* te = tb + strlen(tb); + const char* ne = nb; + while (!equiv && *ne) { + for (nb = ne; *ne; ++ne) { + if (*ne == '|') break; + } + equiv = (SWIG_TypeNameComp(nb, ne, tb, te) == 0) ? 1 : 0; + if (*ne) ++ne; + } + return equiv; +} + + +/* think of this as a c++ template<> or a scheme macro */ +#define SWIG_TypeCheck_Template(comparison, ty) \ + if (ty) { \ + swig_cast_info *iter = ty->cast; \ + while (iter) { \ + if (comparison) { \ + if (iter == ty->cast) return iter; \ + /* Move iter to the top of the linked list */ \ + iter->prev->next = iter->next; \ + if (iter->next) \ + iter->next->prev = iter->prev; \ + iter->next = ty->cast; \ + iter->prev = 0; \ + if (ty->cast) ty->cast->prev = iter; \ + ty->cast = iter; \ + return iter; \ + } \ + iter = iter->next; \ + } \ + } \ + return 0 + +/* + Check the typename +*/ +SWIGRUNTIME swig_cast_info * +SWIG_TypeCheck(const char *c, swig_type_info *ty) { + SWIG_TypeCheck_Template(strcmp(iter->type->name, c) == 0, ty); +} + +/* Same as previous function, except strcmp is replaced with a pointer comparison */ +SWIGRUNTIME swig_cast_info * +SWIG_TypeCheckStruct(swig_type_info *from, swig_type_info *into) { + SWIG_TypeCheck_Template(iter->type == from, into); +} + +/* + Cast a pointer up an inheritance hierarchy +*/ +SWIGRUNTIMEINLINE void * +SWIG_TypeCast(swig_cast_info *ty, void *ptr) { + return ((!ty) || (!ty->converter)) ? ptr : (*ty->converter)(ptr); +} + +/* + Dynamic pointer casting. Down an inheritance hierarchy +*/ +SWIGRUNTIME swig_type_info * +SWIG_TypeDynamicCast(swig_type_info *ty, void **ptr) { + swig_type_info *lastty = ty; + if (!ty || !ty->dcast) return ty; + while (ty && (ty->dcast)) { + ty = (*ty->dcast)(ptr); + if (ty) lastty = ty; + } + return lastty; +} + +/* + Return the name associated with this type +*/ +SWIGRUNTIMEINLINE const char * +SWIG_TypeName(const swig_type_info *ty) { + return ty->name; +} + +/* + Return the pretty name associated with this type, + that is an unmangled type name in a form presentable to the user. +*/ +SWIGRUNTIME const char * +SWIG_TypePrettyName(const swig_type_info *type) { + /* The "str" field contains the equivalent pretty names of the + type, separated by vertical-bar characters. We choose + to print the last name, as it is often (?) the most + specific. */ + if (!type) return NULL; + if (type->str != NULL) { + const char *last_name = type->str; + const char *s; + for (s = type->str; *s; s++) + if (*s == '|') last_name = s+1; + return last_name; + } + else + return type->name; +} + +/* + Set the clientdata field for a type +*/ +SWIGRUNTIME void +SWIG_TypeClientData(swig_type_info *ti, void *clientdata) { + swig_cast_info *cast = ti->cast; + /* if (ti->clientdata == clientdata) return; */ + ti->clientdata = clientdata; + + while (cast) { + if (!cast->converter) { + swig_type_info *tc = cast->type; + if (!tc->clientdata) { + SWIG_TypeClientData(tc, clientdata); + } + } + cast = cast->next; + } +} +SWIGRUNTIME void +SWIG_TypeNewClientData(swig_type_info *ti, void *clientdata) { + SWIG_TypeClientData(ti, clientdata); + ti->owndata = 1; +} + +/* + Search for a swig_type_info structure only by mangled name + Search is a O(log #types) + + We start searching at module start, and finish searching when start == end. + Note: if start == end at the beginning of the function, we go all the way around + the circular list. +*/ +SWIGRUNTIME swig_type_info * +SWIG_MangledTypeQueryModule(swig_module_info *start, + swig_module_info *end, + const char *name) { + swig_module_info *iter = start; + do { + if (iter->size) { + register size_t l = 0; + register size_t r = iter->size - 1; + do { + /* since l+r >= 0, we can (>> 1) instead (/ 2) */ + register size_t i = (l + r) >> 1; + const char *iname = iter->types[i]->name; + if (iname) { + register int compare = strcmp(name, iname); + if (compare == 0) { + return iter->types[i]; + } else if (compare < 0) { + if (i) { + r = i - 1; + } else { + break; + } + } else if (compare > 0) { + l = i + 1; + } + } else { + break; /* should never happen */ + } + } while (l <= r); + } + iter = iter->next; + } while (iter != end); + return 0; +} + +/* + Search for a swig_type_info structure for either a mangled name or a human readable name. + It first searches the mangled names of the types, which is a O(log #types) + If a type is not found it then searches the human readable names, which is O(#types). + + We start searching at module start, and finish searching when start == end. + Note: if start == end at the beginning of the function, we go all the way around + the circular list. +*/ +SWIGRUNTIME swig_type_info * +SWIG_TypeQueryModule(swig_module_info *start, + swig_module_info *end, + const char *name) { + /* STEP 1: Search the name field using binary search */ + swig_type_info *ret = SWIG_MangledTypeQueryModule(start, end, name); + if (ret) { + return ret; + } else { + /* STEP 2: If the type hasn't been found, do a complete search + of the str field (the human readable name) */ + swig_module_info *iter = start; + do { + register size_t i = 0; + for (; i < iter->size; ++i) { + if (iter->types[i]->str && (SWIG_TypeEquiv(iter->types[i]->str, name))) + return iter->types[i]; + } + iter = iter->next; + } while (iter != end); + } + + /* neither found a match */ + return 0; +} + +/* + Pack binary data into a string +*/ +SWIGRUNTIME char * +SWIG_PackData(char *c, void *ptr, size_t sz) { + static const char hex[17] = "0123456789abcdef"; + register const unsigned char *u = (unsigned char *) ptr; + register const unsigned char *eu = u + sz; + for (; u != eu; ++u) { + register unsigned char uu = *u; + *(c++) = hex[(uu & 0xf0) >> 4]; + *(c++) = hex[uu & 0xf]; + } + return c; +} + +/* + Unpack binary data from a string +*/ +SWIGRUNTIME const char * +SWIG_UnpackData(const char *c, void *ptr, size_t sz) { + register unsigned char *u = (unsigned char *) ptr; + register const unsigned char *eu = u + sz; + for (; u != eu; ++u) { + register char d = *(c++); + register unsigned char uu; + if ((d >= '0') && (d <= '9')) + uu = ((d - '0') << 4); + else if ((d >= 'a') && (d <= 'f')) + uu = ((d - ('a'-10)) << 4); + else + return (char *) 0; + d = *(c++); + if ((d >= '0') && (d <= '9')) + uu |= (d - '0'); + else if ((d >= 'a') && (d <= 'f')) + uu |= (d - ('a'-10)); + else + return (char *) 0; + *u = uu; + } + return c; +} + +/* + Pack 'void *' into a string buffer. +*/ +SWIGRUNTIME char * +SWIG_PackVoidPtr(char *buff, void *ptr, const char *name, size_t bsz) { + char *r = buff; + if ((2*sizeof(void *) + 2) > bsz) return 0; + *(r++) = '_'; + r = SWIG_PackData(r,&ptr,sizeof(void *)); + if (strlen(name) + 1 > (bsz - (r - buff))) return 0; + strcpy(r,name); + return buff; +} + +SWIGRUNTIME const char * +SWIG_UnpackVoidPtr(const char *c, void **ptr, const char *name) { + if (*c != '_') { + if (strcmp(c,"NULL") == 0) { + *ptr = (void *) 0; + return name; + } else { + return 0; + } + } + return SWIG_UnpackData(++c,ptr,sizeof(void *)); +} + +SWIGRUNTIME char * +SWIG_PackDataName(char *buff, void *ptr, size_t sz, const char *name, size_t bsz) { + char *r = buff; + size_t lname = (name ? strlen(name) : 0); + if ((2*sz + 2 + lname) > bsz) return 0; + *(r++) = '_'; + r = SWIG_PackData(r,ptr,sz); + if (lname) { + strncpy(r,name,lname+1); + } else { + *r = 0; + } + return buff; +} + +SWIGRUNTIME const char * +SWIG_UnpackDataName(const char *c, void *ptr, size_t sz, const char *name) { + if (*c != '_') { + if (strcmp(c,"NULL") == 0) { + memset(ptr,0,sz); + return name; + } else { + return 0; + } + } + return SWIG_UnpackData(++c,ptr,sz); +} + +#ifdef __cplusplus +} +#endif +/* Python.h has to appear first */ +#include + +/* Add PyOS_snprintf for old Pythons */ +#if PY_VERSION_HEX < 0x02020000 +# if defined(_MSC_VER) || defined(__BORLANDC__) || defined(_WATCOM) +# define PyOS_snprintf _snprintf +# else +# define PyOS_snprintf snprintf +# endif +#endif + +/* A crude PyString_FromFormat implementation for old Pythons */ +#if PY_VERSION_HEX < 0x02020000 + +#ifndef SWIG_PYBUFFER_SIZE +# define SWIG_PYBUFFER_SIZE 1024 +#endif + +static PyObject * +PyString_FromFormat(const char *fmt, ...) { + va_list ap; + char buf[SWIG_PYBUFFER_SIZE * 2]; + int res; + va_start(ap, fmt); + res = vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + return (res < 0 || res >= (int)sizeof(buf)) ? 0 : PyString_FromString(buf); +} +#endif + +/* Add PyObject_Del for old Pythons */ +#if PY_VERSION_HEX < 0x01060000 +# define PyObject_Del(op) PyMem_DEL((op)) +#endif +#ifndef PyObject_DEL +# define PyObject_DEL PyObject_Del +#endif + +/* A crude PyExc_StopIteration exception for old Pythons */ +#if PY_VERSION_HEX < 0x02020000 +# ifndef PyExc_StopIteration +# define PyExc_StopIteration PyExc_RuntimeError +# endif +# ifndef PyObject_GenericGetAttr +# define PyObject_GenericGetAttr 0 +# endif +#endif +/* Py_NotImplemented is defined in 2.1 and up. */ +#if PY_VERSION_HEX < 0x02010000 +# ifndef Py_NotImplemented +# define Py_NotImplemented PyExc_RuntimeError +# endif +#endif + + +/* A crude PyString_AsStringAndSize implementation for old Pythons */ +#if PY_VERSION_HEX < 0x02010000 +# ifndef PyString_AsStringAndSize +# define PyString_AsStringAndSize(obj, s, len) {*s = PyString_AsString(obj); *len = *s ? strlen(*s) : 0;} +# endif +#endif + +/* PySequence_Size for old Pythons */ +#if PY_VERSION_HEX < 0x02000000 +# ifndef PySequence_Size +# define PySequence_Size PySequence_Length +# endif +#endif + + +/* PyBool_FromLong for old Pythons */ +#if PY_VERSION_HEX < 0x02030000 +static +PyObject *PyBool_FromLong(long ok) +{ + PyObject *result = ok ? Py_True : Py_False; + Py_INCREF(result); + return result; +} +#endif + +/* ----------------------------------------------------------------------------- + * error manipulation + * ----------------------------------------------------------------------------- */ + +SWIGRUNTIME PyObject* +SWIG_Python_ErrorType(int code) { + PyObject* type = 0; + switch(code) { + case SWIG_MemoryError: + type = PyExc_MemoryError; + break; + case SWIG_IOError: + type = PyExc_IOError; + break; + case SWIG_RuntimeError: + type = PyExc_RuntimeError; + break; + case SWIG_IndexError: + type = PyExc_IndexError; + break; + case SWIG_TypeError: + type = PyExc_TypeError; + break; + case SWIG_DivisionByZero: + type = PyExc_ZeroDivisionError; + break; + case SWIG_OverflowError: + type = PyExc_OverflowError; + break; + case SWIG_SyntaxError: + type = PyExc_SyntaxError; + break; + case SWIG_ValueError: + type = PyExc_ValueError; + break; + case SWIG_SystemError: + type = PyExc_SystemError; + break; + case SWIG_AttributeError: + type = PyExc_AttributeError; + break; + default: + type = PyExc_RuntimeError; + } + return type; +} + + +SWIGRUNTIME void +SWIG_Python_AddErrorMsg(const char* mesg) +{ + PyObject *type = 0; + PyObject *value = 0; + PyObject *traceback = 0; + + if (PyErr_Occurred()) PyErr_Fetch(&type, &value, &traceback); + if (value) { + PyObject *old_str = PyObject_Str(value); + PyErr_Clear(); + Py_XINCREF(type); + PyErr_Format(type, "%s %s", PyString_AsString(old_str), mesg); + Py_DECREF(old_str); + Py_DECREF(value); + } else { + PyErr_Format(PyExc_RuntimeError, mesg); + } +} + + +#if defined(SWIG_PYTHON_NO_THREADS) +# if defined(SWIG_PYTHON_THREADS) +# undef SWIG_PYTHON_THREADS +# endif +#endif +#if defined(SWIG_PYTHON_THREADS) /* Threading support is enabled */ +# if !defined(SWIG_PYTHON_USE_GIL) && !defined(SWIG_PYTHON_NO_USE_GIL) +# if (PY_VERSION_HEX >= 0x02030000) /* For 2.3 or later, use the PyGILState calls */ +# define SWIG_PYTHON_USE_GIL +# endif +# endif +# if defined(SWIG_PYTHON_USE_GIL) /* Use PyGILState threads calls */ +# ifndef SWIG_PYTHON_INITIALIZE_THREADS +# define SWIG_PYTHON_INITIALIZE_THREADS PyEval_InitThreads() +# endif +# ifdef __cplusplus /* C++ code */ + class SWIG_Python_Thread_Block { + bool status; + PyGILState_STATE state; + public: + void end() { if (status) { PyGILState_Release(state); status = false;} } + SWIG_Python_Thread_Block() : status(true), state(PyGILState_Ensure()) {} + ~SWIG_Python_Thread_Block() { end(); } + }; + class SWIG_Python_Thread_Allow { + bool status; + PyThreadState *save; + public: + void end() { if (status) { PyEval_RestoreThread(save); status = false; }} + SWIG_Python_Thread_Allow() : status(true), save(PyEval_SaveThread()) {} + ~SWIG_Python_Thread_Allow() { end(); } + }; +# define SWIG_PYTHON_THREAD_BEGIN_BLOCK SWIG_Python_Thread_Block _swig_thread_block +# define SWIG_PYTHON_THREAD_END_BLOCK _swig_thread_block.end() +# define SWIG_PYTHON_THREAD_BEGIN_ALLOW SWIG_Python_Thread_Allow _swig_thread_allow +# define SWIG_PYTHON_THREAD_END_ALLOW _swig_thread_allow.end() +# else /* C code */ +# define SWIG_PYTHON_THREAD_BEGIN_BLOCK PyGILState_STATE _swig_thread_block = PyGILState_Ensure() +# define SWIG_PYTHON_THREAD_END_BLOCK PyGILState_Release(_swig_thread_block) +# define SWIG_PYTHON_THREAD_BEGIN_ALLOW PyThreadState *_swig_thread_allow = PyEval_SaveThread() +# define SWIG_PYTHON_THREAD_END_ALLOW PyEval_RestoreThread(_swig_thread_allow) +# endif +# else /* Old thread way, not implemented, user must provide it */ +# if !defined(SWIG_PYTHON_INITIALIZE_THREADS) +# define SWIG_PYTHON_INITIALIZE_THREADS +# endif +# if !defined(SWIG_PYTHON_THREAD_BEGIN_BLOCK) +# define SWIG_PYTHON_THREAD_BEGIN_BLOCK +# endif +# if !defined(SWIG_PYTHON_THREAD_END_BLOCK) +# define SWIG_PYTHON_THREAD_END_BLOCK +# endif +# if !defined(SWIG_PYTHON_THREAD_BEGIN_ALLOW) +# define SWIG_PYTHON_THREAD_BEGIN_ALLOW +# endif +# if !defined(SWIG_PYTHON_THREAD_END_ALLOW) +# define SWIG_PYTHON_THREAD_END_ALLOW +# endif +# endif +#else /* No thread support */ +# define SWIG_PYTHON_INITIALIZE_THREADS +# define SWIG_PYTHON_THREAD_BEGIN_BLOCK +# define SWIG_PYTHON_THREAD_END_BLOCK +# define SWIG_PYTHON_THREAD_BEGIN_ALLOW +# define SWIG_PYTHON_THREAD_END_ALLOW +#endif +/* ----------------------------------------------------------------------------- + * Python API portion that goes into the runtime + * ----------------------------------------------------------------------------- */ + +#ifdef __cplusplus +extern "C" { +#if 0 +} /* cc-mode */ +#endif +#endif + +/* ----------------------------------------------------------------------------- + * Constant declarations + * ----------------------------------------------------------------------------- */ + +/* Constant Types */ +#define SWIG_PY_POINTER 4 +#define SWIG_PY_BINARY 5 + +/* Constant information structure */ +typedef struct swig_const_info { + int type; + char *name; + long lvalue; + double dvalue; + void *pvalue; + swig_type_info **ptype; +} swig_const_info; + +#ifdef __cplusplus +#if 0 +{ /* cc-mode */ +#endif +} +#endif + +/* ----------------------------------------------------------------------------- + * See the LICENSE file for information on copyright, usage and redistribution + * of SWIG, and the README file for authors - http://www.swig.org/release.html. + * + * pyrun.swg + * + * This file contains the runtime support for Python modules + * and includes code for managing global variables and pointer + * type checking. + * + * ----------------------------------------------------------------------------- */ + +/* Common SWIG API */ + +/* for raw pointers */ +#define SWIG_Python_ConvertPtr(obj, pptr, type, flags) SWIG_Python_ConvertPtrAndOwn(obj, pptr, type, flags, 0) +#define SWIG_ConvertPtr(obj, pptr, type, flags) SWIG_Python_ConvertPtr(obj, pptr, type, flags) +#define SWIG_ConvertPtrAndOwn(obj,pptr,type,flags,own) SWIG_Python_ConvertPtrAndOwn(obj, pptr, type, flags, own) +#define SWIG_NewPointerObj(ptr, type, flags) SWIG_Python_NewPointerObj(ptr, type, flags) +#define SWIG_CheckImplicit(ty) SWIG_Python_CheckImplicit(ty) +#define SWIG_AcquirePtr(ptr, src) SWIG_Python_AcquirePtr(ptr, src) +#define swig_owntype int + +/* for raw packed data */ +#define SWIG_ConvertPacked(obj, ptr, sz, ty) SWIG_Python_ConvertPacked(obj, ptr, sz, ty) +#define SWIG_NewPackedObj(ptr, sz, type) SWIG_Python_NewPackedObj(ptr, sz, type) + +/* for class or struct pointers */ +#define SWIG_ConvertInstance(obj, pptr, type, flags) SWIG_ConvertPtr(obj, pptr, type, flags) +#define SWIG_NewInstanceObj(ptr, type, flags) SWIG_NewPointerObj(ptr, type, flags) + +/* for C or C++ function pointers */ +#define SWIG_ConvertFunctionPtr(obj, pptr, type) SWIG_Python_ConvertFunctionPtr(obj, pptr, type) +#define SWIG_NewFunctionPtrObj(ptr, type) SWIG_Python_NewPointerObj(ptr, type, 0) + +/* for C++ member pointers, ie, member methods */ +#define SWIG_ConvertMember(obj, ptr, sz, ty) SWIG_Python_ConvertPacked(obj, ptr, sz, ty) +#define SWIG_NewMemberObj(ptr, sz, type) SWIG_Python_NewPackedObj(ptr, sz, type) + + +/* Runtime API */ + +#define SWIG_GetModule(clientdata) SWIG_Python_GetModule() +#define SWIG_SetModule(clientdata, pointer) SWIG_Python_SetModule(pointer) +#define SWIG_NewClientData(obj) PySwigClientData_New(obj) + +#define SWIG_SetErrorObj SWIG_Python_SetErrorObj +#define SWIG_SetErrorMsg SWIG_Python_SetErrorMsg +#define SWIG_ErrorType(code) SWIG_Python_ErrorType(code) +#define SWIG_Error(code, msg) SWIG_Python_SetErrorMsg(SWIG_ErrorType(code), msg) +#define SWIG_fail goto fail + + +/* Runtime API implementation */ + +/* Error manipulation */ + +SWIGINTERN void +SWIG_Python_SetErrorObj(PyObject *errtype, PyObject *obj) { + SWIG_PYTHON_THREAD_BEGIN_BLOCK; + PyErr_SetObject(errtype, obj); + Py_DECREF(obj); + SWIG_PYTHON_THREAD_END_BLOCK; +} + +SWIGINTERN void +SWIG_Python_SetErrorMsg(PyObject *errtype, const char *msg) { + SWIG_PYTHON_THREAD_BEGIN_BLOCK; + PyErr_SetString(errtype, (char *) msg); + SWIG_PYTHON_THREAD_END_BLOCK; +} + +#define SWIG_Python_Raise(obj, type, desc) SWIG_Python_SetErrorObj(SWIG_Python_ExceptionType(desc), obj) + +/* Set a constant value */ + +SWIGINTERN void +SWIG_Python_SetConstant(PyObject *d, const char *name, PyObject *obj) { + PyDict_SetItemString(d, (char*) name, obj); + Py_DECREF(obj); +} + +/* Append a value to the result obj */ + +SWIGINTERN PyObject* +SWIG_Python_AppendOutput(PyObject* result, PyObject* obj) { +#if !defined(SWIG_PYTHON_OUTPUT_TUPLE) + if (!result) { + result = obj; + } else if (result == Py_None) { + Py_DECREF(result); + result = obj; + } else { + if (!PyList_Check(result)) { + PyObject *o2 = result; + result = PyList_New(1); + PyList_SetItem(result, 0, o2); + } + PyList_Append(result,obj); + Py_DECREF(obj); + } + return result; +#else + PyObject* o2; + PyObject* o3; + if (!result) { + result = obj; + } else if (result == Py_None) { + Py_DECREF(result); + result = obj; + } else { + if (!PyTuple_Check(result)) { + o2 = result; + result = PyTuple_New(1); + PyTuple_SET_ITEM(result, 0, o2); + } + o3 = PyTuple_New(1); + PyTuple_SET_ITEM(o3, 0, obj); + o2 = result; + result = PySequence_Concat(o2, o3); + Py_DECREF(o2); + Py_DECREF(o3); + } + return result; +#endif +} + +/* Unpack the argument tuple */ + +SWIGINTERN int +SWIG_Python_UnpackTuple(PyObject *args, const char *name, int min, int max, PyObject **objs) +{ + if (!args) { + if (!min && !max) { + return 1; + } else { + PyErr_Format(PyExc_TypeError, "%s expected %s%d arguments, got none", + name, (min == max ? "" : "at least "), min); + return 0; + } + } + if (!PyTuple_Check(args)) { + PyErr_SetString(PyExc_SystemError, "UnpackTuple() argument list is not a tuple"); + return 0; + } else { + register int l = PyTuple_GET_SIZE(args); + if (l < min) { + PyErr_Format(PyExc_TypeError, "%s expected %s%d arguments, got %d", + name, (min == max ? "" : "at least "), min, l); + return 0; + } else if (l > max) { + PyErr_Format(PyExc_TypeError, "%s expected %s%d arguments, got %d", + name, (min == max ? "" : "at most "), max, l); + return 0; + } else { + register int i; + for (i = 0; i < l; ++i) { + objs[i] = PyTuple_GET_ITEM(args, i); + } + for (; l < max; ++l) { + objs[l] = 0; + } + return i + 1; + } + } +} + +/* A functor is a function object with one single object argument */ +#if PY_VERSION_HEX >= 0x02020000 +#define SWIG_Python_CallFunctor(functor, obj) PyObject_CallFunctionObjArgs(functor, obj, NULL); +#else +#define SWIG_Python_CallFunctor(functor, obj) PyObject_CallFunction(functor, "O", obj); +#endif + +/* + Helper for static pointer initialization for both C and C++ code, for example + static PyObject *SWIG_STATIC_POINTER(MyVar) = NewSomething(...); +*/ +#ifdef __cplusplus +#define SWIG_STATIC_POINTER(var) var +#else +#define SWIG_STATIC_POINTER(var) var = 0; if (!var) var +#endif + +/* ----------------------------------------------------------------------------- + * Pointer declarations + * ----------------------------------------------------------------------------- */ + +/* Flags for new pointer objects */ +#define SWIG_POINTER_NOSHADOW (SWIG_POINTER_OWN << 1) +#define SWIG_POINTER_NEW (SWIG_POINTER_NOSHADOW | SWIG_POINTER_OWN) + +#define SWIG_POINTER_IMPLICIT_CONV (SWIG_POINTER_DISOWN << 1) + +#ifdef __cplusplus +extern "C" { +#if 0 +} /* cc-mode */ +#endif +#endif + +/* How to access Py_None */ +#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +# ifndef SWIG_PYTHON_NO_BUILD_NONE +# ifndef SWIG_PYTHON_BUILD_NONE +# define SWIG_PYTHON_BUILD_NONE +# endif +# endif +#endif + +#ifdef SWIG_PYTHON_BUILD_NONE +# ifdef Py_None +# undef Py_None +# define Py_None SWIG_Py_None() +# endif +SWIGRUNTIMEINLINE PyObject * +_SWIG_Py_None(void) +{ + PyObject *none = Py_BuildValue((char*)""); + Py_DECREF(none); + return none; +} +SWIGRUNTIME PyObject * +SWIG_Py_None(void) +{ + static PyObject *SWIG_STATIC_POINTER(none) = _SWIG_Py_None(); + return none; +} +#endif + +/* The python void return value */ + +SWIGRUNTIMEINLINE PyObject * +SWIG_Py_Void(void) +{ + PyObject *none = Py_None; + Py_INCREF(none); + return none; +} + +/* PySwigClientData */ + +typedef struct { + PyObject *klass; + PyObject *newraw; + PyObject *newargs; + PyObject *destroy; + int delargs; + int implicitconv; +} PySwigClientData; + +SWIGRUNTIMEINLINE int +SWIG_Python_CheckImplicit(swig_type_info *ty) +{ + PySwigClientData *data = (PySwigClientData *)ty->clientdata; + return data ? data->implicitconv : 0; +} + +SWIGRUNTIMEINLINE PyObject * +SWIG_Python_ExceptionType(swig_type_info *desc) { + PySwigClientData *data = desc ? (PySwigClientData *) desc->clientdata : 0; + PyObject *klass = data ? data->klass : 0; + return (klass ? klass : PyExc_RuntimeError); +} + + +SWIGRUNTIME PySwigClientData * +PySwigClientData_New(PyObject* obj) +{ + if (!obj) { + return 0; + } else { + PySwigClientData *data = (PySwigClientData *)malloc(sizeof(PySwigClientData)); + /* the klass element */ + data->klass = obj; + Py_INCREF(data->klass); + /* the newraw method and newargs arguments used to create a new raw instance */ + if (PyClass_Check(obj)) { + data->newraw = 0; + data->newargs = obj; + Py_INCREF(obj); + } else { +#if (PY_VERSION_HEX < 0x02020000) + data->newraw = 0; +#else + data->newraw = PyObject_GetAttrString(data->klass, (char *)"__new__"); +#endif + if (data->newraw) { + Py_INCREF(data->newraw); + data->newargs = PyTuple_New(1); + PyTuple_SetItem(data->newargs, 0, obj); + } else { + data->newargs = obj; + } + Py_INCREF(data->newargs); + } + /* the destroy method, aka as the C++ delete method */ + data->destroy = PyObject_GetAttrString(data->klass, (char *)"__swig_destroy__"); + if (PyErr_Occurred()) { + PyErr_Clear(); + data->destroy = 0; + } + if (data->destroy) { + int flags; + Py_INCREF(data->destroy); + flags = PyCFunction_GET_FLAGS(data->destroy); +#ifdef METH_O + data->delargs = !(flags & (METH_O)); +#else + data->delargs = 0; +#endif + } else { + data->delargs = 0; + } + data->implicitconv = 0; + return data; + } +} + +SWIGRUNTIME void +PySwigClientData_Del(PySwigClientData* data) +{ + Py_XDECREF(data->newraw); + Py_XDECREF(data->newargs); + Py_XDECREF(data->destroy); +} + +/* =============== PySwigObject =====================*/ + +typedef struct { + PyObject_HEAD + void *ptr; + swig_type_info *ty; + int own; + PyObject *next; +} PySwigObject; + +SWIGRUNTIME PyObject * +PySwigObject_long(PySwigObject *v) +{ + return PyLong_FromVoidPtr(v->ptr); +} + +SWIGRUNTIME PyObject * +PySwigObject_format(const char* fmt, PySwigObject *v) +{ + PyObject *res = NULL; + PyObject *args = PyTuple_New(1); + if (args) { + if (PyTuple_SetItem(args, 0, PySwigObject_long(v)) == 0) { + PyObject *ofmt = PyString_FromString(fmt); + if (ofmt) { + res = PyString_Format(ofmt,args); + Py_DECREF(ofmt); + } + Py_DECREF(args); + } + } + return res; +} + +SWIGRUNTIME PyObject * +PySwigObject_oct(PySwigObject *v) +{ + return PySwigObject_format("%o",v); +} + +SWIGRUNTIME PyObject * +PySwigObject_hex(PySwigObject *v) +{ + return PySwigObject_format("%x",v); +} + +SWIGRUNTIME PyObject * +#ifdef METH_NOARGS +PySwigObject_repr(PySwigObject *v) +#else +PySwigObject_repr(PySwigObject *v, PyObject *args) +#endif +{ + const char *name = SWIG_TypePrettyName(v->ty); + PyObject *hex = PySwigObject_hex(v); + PyObject *repr = PyString_FromFormat("", name, PyString_AsString(hex)); + Py_DECREF(hex); + if (v->next) { +#ifdef METH_NOARGS + PyObject *nrep = PySwigObject_repr((PySwigObject *)v->next); +#else + PyObject *nrep = PySwigObject_repr((PySwigObject *)v->next, args); +#endif + PyString_ConcatAndDel(&repr,nrep); + } + return repr; +} + +SWIGRUNTIME int +PySwigObject_print(PySwigObject *v, FILE *fp, int SWIGUNUSEDPARM(flags)) +{ +#ifdef METH_NOARGS + PyObject *repr = PySwigObject_repr(v); +#else + PyObject *repr = PySwigObject_repr(v, NULL); +#endif + if (repr) { + fputs(PyString_AsString(repr), fp); + Py_DECREF(repr); + return 0; + } else { + return 1; + } +} + +SWIGRUNTIME PyObject * +PySwigObject_str(PySwigObject *v) +{ + char result[SWIG_BUFFER_SIZE]; + return SWIG_PackVoidPtr(result, v->ptr, v->ty->name, sizeof(result)) ? + PyString_FromString(result) : 0; +} + +SWIGRUNTIME int +PySwigObject_compare(PySwigObject *v, PySwigObject *w) +{ + void *i = v->ptr; + void *j = w->ptr; + return (i < j) ? -1 : ((i > j) ? 1 : 0); +} + +SWIGRUNTIME PyTypeObject* _PySwigObject_type(void); + +SWIGRUNTIME PyTypeObject* +PySwigObject_type(void) { + static PyTypeObject *SWIG_STATIC_POINTER(type) = _PySwigObject_type(); + return type; +} + +SWIGRUNTIMEINLINE int +PySwigObject_Check(PyObject *op) { + return ((op)->ob_type == PySwigObject_type()) + || (strcmp((op)->ob_type->tp_name,"PySwigObject") == 0); +} + +SWIGRUNTIME PyObject * +PySwigObject_New(void *ptr, swig_type_info *ty, int own); + +SWIGRUNTIME void +PySwigObject_dealloc(PyObject *v) +{ + PySwigObject *sobj = (PySwigObject *) v; + PyObject *next = sobj->next; + if (sobj->own) { + swig_type_info *ty = sobj->ty; + PySwigClientData *data = ty ? (PySwigClientData *) ty->clientdata : 0; + PyObject *destroy = data ? data->destroy : 0; + if (destroy) { + /* destroy is always a VARARGS method */ + PyObject *res; + if (data->delargs) { + /* we need to create a temporal object to carry the destroy operation */ + PyObject *tmp = PySwigObject_New(sobj->ptr, ty, 0); + res = SWIG_Python_CallFunctor(destroy, tmp); + Py_DECREF(tmp); + } else { + PyCFunction meth = PyCFunction_GET_FUNCTION(destroy); + PyObject *mself = PyCFunction_GET_SELF(destroy); + res = ((*meth)(mself, v)); + } + Py_XDECREF(res); + } else { + const char *name = SWIG_TypePrettyName(ty); +#if !defined(SWIG_PYTHON_SILENT_MEMLEAK) + printf("swig/python detected a memory leak of type '%s', no destructor found.\\n", name); +#endif + } + } + Py_XDECREF(next); + PyObject_DEL(v); +} + +SWIGRUNTIME PyObject* +PySwigObject_append(PyObject* v, PyObject* next) +{ + PySwigObject *sobj = (PySwigObject *) v; +#ifndef METH_O + PyObject *tmp = 0; + if (!PyArg_ParseTuple(next,(char *)"O:append", &tmp)) return NULL; + next = tmp; +#endif + if (!PySwigObject_Check(next)) { + return NULL; + } + sobj->next = next; + Py_INCREF(next); + return SWIG_Py_Void(); +} + +SWIGRUNTIME PyObject* +#ifdef METH_NOARGS +PySwigObject_next(PyObject* v) +#else +PySwigObject_next(PyObject* v, PyObject *SWIGUNUSEDPARM(args)) +#endif +{ + PySwigObject *sobj = (PySwigObject *) v; + if (sobj->next) { + Py_INCREF(sobj->next); + return sobj->next; + } else { + return SWIG_Py_Void(); + } +} + +SWIGINTERN PyObject* +#ifdef METH_NOARGS +PySwigObject_disown(PyObject *v) +#else +PySwigObject_disown(PyObject* v, PyObject *SWIGUNUSEDPARM(args)) +#endif +{ + PySwigObject *sobj = (PySwigObject *)v; + sobj->own = 0; + return SWIG_Py_Void(); +} + +SWIGINTERN PyObject* +#ifdef METH_NOARGS +PySwigObject_acquire(PyObject *v) +#else +PySwigObject_acquire(PyObject* v, PyObject *SWIGUNUSEDPARM(args)) +#endif +{ + PySwigObject *sobj = (PySwigObject *)v; + sobj->own = SWIG_POINTER_OWN; + return SWIG_Py_Void(); +} + +SWIGINTERN PyObject* +PySwigObject_own(PyObject *v, PyObject *args) +{ + PyObject *val = 0; +#if (PY_VERSION_HEX < 0x02020000) + if (!PyArg_ParseTuple(args,(char *)"|O:own",&val)) +#else + if (!PyArg_UnpackTuple(args, (char *)"own", 0, 1, &val)) +#endif + { + return NULL; + } + else + { + PySwigObject *sobj = (PySwigObject *)v; + PyObject *obj = PyBool_FromLong(sobj->own); + if (val) { +#ifdef METH_NOARGS + if (PyObject_IsTrue(val)) { + PySwigObject_acquire(v); + } else { + PySwigObject_disown(v); + } +#else + if (PyObject_IsTrue(val)) { + PySwigObject_acquire(v,args); + } else { + PySwigObject_disown(v,args); + } +#endif + } + return obj; + } +} + +#ifdef METH_O +static PyMethodDef +swigobject_methods[] = { + {(char *)"disown", (PyCFunction)PySwigObject_disown, METH_NOARGS, (char *)"releases ownership of the pointer"}, + {(char *)"acquire", (PyCFunction)PySwigObject_acquire, METH_NOARGS, (char *)"aquires ownership of the pointer"}, + {(char *)"own", (PyCFunction)PySwigObject_own, METH_VARARGS, (char *)"returns/sets ownership of the pointer"}, + {(char *)"append", (PyCFunction)PySwigObject_append, METH_O, (char *)"appends another 'this' object"}, + {(char *)"next", (PyCFunction)PySwigObject_next, METH_NOARGS, (char *)"returns the next 'this' object"}, + {(char *)"__repr__",(PyCFunction)PySwigObject_repr, METH_NOARGS, (char *)"returns object representation"}, + {0, 0, 0, 0} +}; +#else +static PyMethodDef +swigobject_methods[] = { + {(char *)"disown", (PyCFunction)PySwigObject_disown, METH_VARARGS, (char *)"releases ownership of the pointer"}, + {(char *)"acquire", (PyCFunction)PySwigObject_acquire, METH_VARARGS, (char *)"aquires ownership of the pointer"}, + {(char *)"own", (PyCFunction)PySwigObject_own, METH_VARARGS, (char *)"returns/sets ownership of the pointer"}, + {(char *)"append", (PyCFunction)PySwigObject_append, METH_VARARGS, (char *)"appends another 'this' object"}, + {(char *)"next", (PyCFunction)PySwigObject_next, METH_VARARGS, (char *)"returns the next 'this' object"}, + {(char *)"__repr__",(PyCFunction)PySwigObject_repr, METH_VARARGS, (char *)"returns object representation"}, + {0, 0, 0, 0} +}; +#endif + +#if PY_VERSION_HEX < 0x02020000 +SWIGINTERN PyObject * +PySwigObject_getattr(PySwigObject *sobj,char *name) +{ + return Py_FindMethod(swigobject_methods, (PyObject *)sobj, name); +} +#endif + +SWIGRUNTIME PyTypeObject* +_PySwigObject_type(void) { + static char swigobject_doc[] = "Swig object carries a C/C++ instance pointer"; + + static PyNumberMethods PySwigObject_as_number = { + (binaryfunc)0, /*nb_add*/ + (binaryfunc)0, /*nb_subtract*/ + (binaryfunc)0, /*nb_multiply*/ + (binaryfunc)0, /*nb_divide*/ + (binaryfunc)0, /*nb_remainder*/ + (binaryfunc)0, /*nb_divmod*/ + (ternaryfunc)0,/*nb_power*/ + (unaryfunc)0, /*nb_negative*/ + (unaryfunc)0, /*nb_positive*/ + (unaryfunc)0, /*nb_absolute*/ + (inquiry)0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + (coercion)0, /*nb_coerce*/ + (unaryfunc)PySwigObject_long, /*nb_int*/ + (unaryfunc)PySwigObject_long, /*nb_long*/ + (unaryfunc)0, /*nb_float*/ + (unaryfunc)PySwigObject_oct, /*nb_oct*/ + (unaryfunc)PySwigObject_hex, /*nb_hex*/ +#if PY_VERSION_HEX >= 0x02020000 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* nb_inplace_add -> nb_inplace_true_divide */ +#elif PY_VERSION_HEX >= 0x02000000 + 0,0,0,0,0,0,0,0,0,0,0 /* nb_inplace_add -> nb_inplace_or */ +#endif + }; + + static PyTypeObject pyswigobject_type; + static int type_init = 0; + if (!type_init) { + const PyTypeObject tmp + = { + PyObject_HEAD_INIT(NULL) + 0, /* ob_size */ + (char *)"PySwigObject", /* tp_name */ + sizeof(PySwigObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)PySwigObject_dealloc, /* tp_dealloc */ + (printfunc)PySwigObject_print, /* tp_print */ +#if PY_VERSION_HEX < 0x02020000 + (getattrfunc)PySwigObject_getattr, /* tp_getattr */ +#else + (getattrfunc)0, /* tp_getattr */ +#endif + (setattrfunc)0, /* tp_setattr */ + (cmpfunc)PySwigObject_compare, /* tp_compare */ + (reprfunc)PySwigObject_repr, /* tp_repr */ + &PySwigObject_as_number, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + (hashfunc)0, /* tp_hash */ + (ternaryfunc)0, /* tp_call */ + (reprfunc)PySwigObject_str, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + swigobject_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ +#if PY_VERSION_HEX >= 0x02020000 + 0, /* tp_iter */ + 0, /* tp_iternext */ + swigobject_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ +#endif +#if PY_VERSION_HEX >= 0x02030000 + 0, /* tp_del */ +#endif +#ifdef COUNT_ALLOCS + 0,0,0,0 /* tp_alloc -> tp_next */ +#endif + }; + pyswigobject_type = tmp; + pyswigobject_type.ob_type = &PyType_Type; + type_init = 1; + } + return &pyswigobject_type; +} + +SWIGRUNTIME PyObject * +PySwigObject_New(void *ptr, swig_type_info *ty, int own) +{ + PySwigObject *sobj = PyObject_NEW(PySwigObject, PySwigObject_type()); + if (sobj) { + sobj->ptr = ptr; + sobj->ty = ty; + sobj->own = own; + sobj->next = 0; + } + return (PyObject *)sobj; +} + +/* ----------------------------------------------------------------------------- + * Implements a simple Swig Packed type, and use it instead of string + * ----------------------------------------------------------------------------- */ + +typedef struct { + PyObject_HEAD + void *pack; + swig_type_info *ty; + size_t size; +} PySwigPacked; + +SWIGRUNTIME int +PySwigPacked_print(PySwigPacked *v, FILE *fp, int SWIGUNUSEDPARM(flags)) +{ + char result[SWIG_BUFFER_SIZE]; + fputs("pack, v->size, 0, sizeof(result))) { + fputs("at ", fp); + fputs(result, fp); + } + fputs(v->ty->name,fp); + fputs(">", fp); + return 0; +} + +SWIGRUNTIME PyObject * +PySwigPacked_repr(PySwigPacked *v) +{ + char result[SWIG_BUFFER_SIZE]; + if (SWIG_PackDataName(result, v->pack, v->size, 0, sizeof(result))) { + return PyString_FromFormat("", result, v->ty->name); + } else { + return PyString_FromFormat("", v->ty->name); + } +} + +SWIGRUNTIME PyObject * +PySwigPacked_str(PySwigPacked *v) +{ + char result[SWIG_BUFFER_SIZE]; + if (SWIG_PackDataName(result, v->pack, v->size, 0, sizeof(result))){ + return PyString_FromFormat("%s%s", result, v->ty->name); + } else { + return PyString_FromString(v->ty->name); + } +} + +SWIGRUNTIME int +PySwigPacked_compare(PySwigPacked *v, PySwigPacked *w) +{ + size_t i = v->size; + size_t j = w->size; + int s = (i < j) ? -1 : ((i > j) ? 1 : 0); + return s ? s : strncmp((char *)v->pack, (char *)w->pack, 2*v->size); +} + +SWIGRUNTIME PyTypeObject* _PySwigPacked_type(void); + +SWIGRUNTIME PyTypeObject* +PySwigPacked_type(void) { + static PyTypeObject *SWIG_STATIC_POINTER(type) = _PySwigPacked_type(); + return type; +} + +SWIGRUNTIMEINLINE int +PySwigPacked_Check(PyObject *op) { + return ((op)->ob_type == _PySwigPacked_type()) + || (strcmp((op)->ob_type->tp_name,"PySwigPacked") == 0); +} + +SWIGRUNTIME void +PySwigPacked_dealloc(PyObject *v) +{ + if (PySwigPacked_Check(v)) { + PySwigPacked *sobj = (PySwigPacked *) v; + free(sobj->pack); + } + PyObject_DEL(v); +} + +SWIGRUNTIME PyTypeObject* +_PySwigPacked_type(void) { + static char swigpacked_doc[] = "Swig object carries a C/C++ instance pointer"; + static PyTypeObject pyswigpacked_type; + static int type_init = 0; + if (!type_init) { + const PyTypeObject tmp + = { + PyObject_HEAD_INIT(NULL) + 0, /* ob_size */ + (char *)"PySwigPacked", /* tp_name */ + sizeof(PySwigPacked), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)PySwigPacked_dealloc, /* tp_dealloc */ + (printfunc)PySwigPacked_print, /* tp_print */ + (getattrfunc)0, /* tp_getattr */ + (setattrfunc)0, /* tp_setattr */ + (cmpfunc)PySwigPacked_compare, /* tp_compare */ + (reprfunc)PySwigPacked_repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + (hashfunc)0, /* tp_hash */ + (ternaryfunc)0, /* tp_call */ + (reprfunc)PySwigPacked_str, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + swigpacked_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ +#if PY_VERSION_HEX >= 0x02020000 + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ +#endif +#if PY_VERSION_HEX >= 0x02030000 + 0, /* tp_del */ +#endif +#ifdef COUNT_ALLOCS + 0,0,0,0 /* tp_alloc -> tp_next */ +#endif + }; + pyswigpacked_type = tmp; + pyswigpacked_type.ob_type = &PyType_Type; + type_init = 1; + } + return &pyswigpacked_type; +} + +SWIGRUNTIME PyObject * +PySwigPacked_New(void *ptr, size_t size, swig_type_info *ty) +{ + PySwigPacked *sobj = PyObject_NEW(PySwigPacked, PySwigPacked_type()); + if (sobj) { + void *pack = malloc(size); + if (pack) { + memcpy(pack, ptr, size); + sobj->pack = pack; + sobj->ty = ty; + sobj->size = size; + } else { + PyObject_DEL((PyObject *) sobj); + sobj = 0; + } + } + return (PyObject *) sobj; +} + +SWIGRUNTIME swig_type_info * +PySwigPacked_UnpackData(PyObject *obj, void *ptr, size_t size) +{ + if (PySwigPacked_Check(obj)) { + PySwigPacked *sobj = (PySwigPacked *)obj; + if (sobj->size != size) return 0; + memcpy(ptr, sobj->pack, size); + return sobj->ty; + } else { + return 0; + } +} + +/* ----------------------------------------------------------------------------- + * pointers/data manipulation + * ----------------------------------------------------------------------------- */ + +SWIGRUNTIMEINLINE PyObject * +_SWIG_This(void) +{ + return PyString_FromString("this"); +} + +SWIGRUNTIME PyObject * +SWIG_This(void) +{ + static PyObject *SWIG_STATIC_POINTER(swig_this) = _SWIG_This(); + return swig_this; +} + +/* #define SWIG_PYTHON_SLOW_GETSET_THIS */ + +SWIGRUNTIME PySwigObject * +SWIG_Python_GetSwigThis(PyObject *pyobj) +{ + if (PySwigObject_Check(pyobj)) { + return (PySwigObject *) pyobj; + } else { + PyObject *obj = 0; +#if (!defined(SWIG_PYTHON_SLOW_GETSET_THIS) && (PY_VERSION_HEX >= 0x02030000)) + if (PyInstance_Check(pyobj)) { + obj = _PyInstance_Lookup(pyobj, SWIG_This()); + } else { + PyObject **dictptr = _PyObject_GetDictPtr(pyobj); + if (dictptr != NULL) { + PyObject *dict = *dictptr; + obj = dict ? PyDict_GetItem(dict, SWIG_This()) : 0; + } else { +#ifdef PyWeakref_CheckProxy + if (PyWeakref_CheckProxy(pyobj)) { + PyObject *wobj = PyWeakref_GET_OBJECT(pyobj); + return wobj ? SWIG_Python_GetSwigThis(wobj) : 0; + } +#endif + obj = PyObject_GetAttr(pyobj,SWIG_This()); + if (obj) { + Py_DECREF(obj); + } else { + if (PyErr_Occurred()) PyErr_Clear(); + return 0; + } + } + } +#else + obj = PyObject_GetAttr(pyobj,SWIG_This()); + if (obj) { + Py_DECREF(obj); + } else { + if (PyErr_Occurred()) PyErr_Clear(); + return 0; + } +#endif + if (obj && !PySwigObject_Check(obj)) { + /* a PyObject is called 'this', try to get the 'real this' + PySwigObject from it */ + return SWIG_Python_GetSwigThis(obj); + } + return (PySwigObject *)obj; + } +} + +/* Acquire a pointer value */ + +SWIGRUNTIME int +SWIG_Python_AcquirePtr(PyObject *obj, int own) { + if (own) { + PySwigObject *sobj = SWIG_Python_GetSwigThis(obj); + if (sobj) { + int oldown = sobj->own; + sobj->own = own; + return oldown; + } + } + return 0; +} + +/* Convert a pointer value */ + +SWIGRUNTIME int +SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int flags, int *own) { + if (!obj) return SWIG_ERROR; + if (obj == Py_None) { + if (ptr) *ptr = 0; + return SWIG_OK; + } else { + PySwigObject *sobj = SWIG_Python_GetSwigThis(obj); + while (sobj) { + void *vptr = sobj->ptr; + if (ty) { + swig_type_info *to = sobj->ty; + if (to == ty) { + /* no type cast needed */ + if (ptr) *ptr = vptr; + break; + } else { + swig_cast_info *tc = SWIG_TypeCheck(to->name,ty); + if (!tc) { + sobj = (PySwigObject *)sobj->next; + } else { + if (ptr) *ptr = SWIG_TypeCast(tc,vptr); + break; + } + } + } else { + if (ptr) *ptr = vptr; + break; + } + } + if (sobj) { + if (own) *own = sobj->own; + if (flags & SWIG_POINTER_DISOWN) { + sobj->own = 0; + } + return SWIG_OK; + } else { + int res = SWIG_ERROR; + if (flags & SWIG_POINTER_IMPLICIT_CONV) { + PySwigClientData *data = ty ? (PySwigClientData *) ty->clientdata : 0; + if (data && !data->implicitconv) { + PyObject *klass = data->klass; + if (klass) { + PyObject *impconv; + data->implicitconv = 1; /* avoid recursion and call 'explicit' constructors*/ + impconv = SWIG_Python_CallFunctor(klass, obj); + data->implicitconv = 0; + if (PyErr_Occurred()) { + PyErr_Clear(); + impconv = 0; + } + if (impconv) { + PySwigObject *iobj = SWIG_Python_GetSwigThis(impconv); + if (iobj) { + void *vptr; + res = SWIG_Python_ConvertPtrAndOwn((PyObject*)iobj, &vptr, ty, 0, 0); + if (SWIG_IsOK(res)) { + if (ptr) { + *ptr = vptr; + /* transfer the ownership to 'ptr' */ + iobj->own = 0; + res = SWIG_AddCast(res); + res = SWIG_AddNewMask(res); + } else { + res = SWIG_AddCast(res); + } + } + } + Py_DECREF(impconv); + } + } + } + } + return res; + } + } +} + +/* Convert a function ptr value */ + +SWIGRUNTIME int +SWIG_Python_ConvertFunctionPtr(PyObject *obj, void **ptr, swig_type_info *ty) { + if (!PyCFunction_Check(obj)) { + return SWIG_ConvertPtr(obj, ptr, ty, 0); + } else { + void *vptr = 0; + + /* here we get the method pointer for callbacks */ + const char *doc = (((PyCFunctionObject *)obj) -> m_ml -> ml_doc); + const char *desc = doc ? strstr(doc, "swig_ptr: ") : 0; + if (desc) { + desc = ty ? SWIG_UnpackVoidPtr(desc + 10, &vptr, ty->name) : 0; + if (!desc) return SWIG_ERROR; + } + if (ty) { + swig_cast_info *tc = SWIG_TypeCheck(desc,ty); + if (!tc) return SWIG_ERROR; + *ptr = SWIG_TypeCast(tc,vptr); + } else { + *ptr = vptr; + } + return SWIG_OK; + } +} + +/* Convert a packed value value */ + +SWIGRUNTIME int +SWIG_Python_ConvertPacked(PyObject *obj, void *ptr, size_t sz, swig_type_info *ty) { + swig_type_info *to = PySwigPacked_UnpackData(obj, ptr, sz); + if (!to) return SWIG_ERROR; + if (ty) { + if (to != ty) { + /* check type cast? */ + swig_cast_info *tc = SWIG_TypeCheck(to->name,ty); + if (!tc) return SWIG_ERROR; + } + } + return SWIG_OK; +} + +/* ----------------------------------------------------------------------------- + * Create a new pointer object + * ----------------------------------------------------------------------------- */ + +/* + Create a new instance object, whitout calling __init__, and set the + 'this' attribute. +*/ + +SWIGRUNTIME PyObject* +SWIG_Python_NewShadowInstance(PySwigClientData *data, PyObject *swig_this) +{ +#if (PY_VERSION_HEX >= 0x02020000) + PyObject *inst = 0; + PyObject *newraw = data->newraw; + if (newraw) { + inst = PyObject_Call(newraw, data->newargs, NULL); + if (inst) { +#if !defined(SWIG_PYTHON_SLOW_GETSET_THIS) + PyObject **dictptr = _PyObject_GetDictPtr(inst); + if (dictptr != NULL) { + PyObject *dict = *dictptr; + if (dict == NULL) { + dict = PyDict_New(); + *dictptr = dict; + PyDict_SetItem(dict, SWIG_This(), swig_this); + } + } +#else + PyObject *key = SWIG_This(); + PyObject_SetAttr(inst, key, swig_this); +#endif + } + } else { + PyObject *dict = PyDict_New(); + PyDict_SetItem(dict, SWIG_This(), swig_this); + inst = PyInstance_NewRaw(data->newargs, dict); + Py_DECREF(dict); + } + return inst; +#else +#if (PY_VERSION_HEX >= 0x02010000) + PyObject *inst; + PyObject *dict = PyDict_New(); + PyDict_SetItem(dict, SWIG_This(), swig_this); + inst = PyInstance_NewRaw(data->newargs, dict); + Py_DECREF(dict); + return (PyObject *) inst; +#else + PyInstanceObject *inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type); + if (inst == NULL) { + return NULL; + } + inst->in_class = (PyClassObject *)data->newargs; + Py_INCREF(inst->in_class); + inst->in_dict = PyDict_New(); + if (inst->in_dict == NULL) { + Py_DECREF(inst); + return NULL; + } +#ifdef Py_TPFLAGS_HAVE_WEAKREFS + inst->in_weakreflist = NULL; +#endif +#ifdef Py_TPFLAGS_GC + PyObject_GC_Init(inst); +#endif + PyDict_SetItem(inst->in_dict, SWIG_This(), swig_this); + return (PyObject *) inst; +#endif +#endif +} + +SWIGRUNTIME void +SWIG_Python_SetSwigThis(PyObject *inst, PyObject *swig_this) +{ + PyObject *dict; +#if (PY_VERSION_HEX >= 0x02020000) && !defined(SWIG_PYTHON_SLOW_GETSET_THIS) + PyObject **dictptr = _PyObject_GetDictPtr(inst); + if (dictptr != NULL) { + dict = *dictptr; + if (dict == NULL) { + dict = PyDict_New(); + *dictptr = dict; + } + PyDict_SetItem(dict, SWIG_This(), swig_this); + return; + } +#endif + dict = PyObject_GetAttrString(inst, (char*)"__dict__"); + PyDict_SetItem(dict, SWIG_This(), swig_this); + Py_DECREF(dict); +} + + +SWIGINTERN PyObject * +SWIG_Python_InitShadowInstance(PyObject *args) { + PyObject *obj[2]; + if (!SWIG_Python_UnpackTuple(args,(char*)"swiginit", 2, 2, obj)) { + return NULL; + } else { + PySwigObject *sthis = SWIG_Python_GetSwigThis(obj[0]); + if (sthis) { + PySwigObject_append((PyObject*) sthis, obj[1]); + } else { + SWIG_Python_SetSwigThis(obj[0], obj[1]); + } + return SWIG_Py_Void(); + } +} + +/* Create a new pointer object */ + +SWIGRUNTIME PyObject * +SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) { + if (!ptr) { + return SWIG_Py_Void(); + } else { + int own = (flags & SWIG_POINTER_OWN) ? SWIG_POINTER_OWN : 0; + PyObject *robj = PySwigObject_New(ptr, type, own); + PySwigClientData *clientdata = type ? (PySwigClientData *)(type->clientdata) : 0; + if (clientdata && !(flags & SWIG_POINTER_NOSHADOW)) { + PyObject *inst = SWIG_Python_NewShadowInstance(clientdata, robj); + if (inst) { + Py_DECREF(robj); + robj = inst; + } + } + return robj; + } +} + +/* Create a new packed object */ + +SWIGRUNTIMEINLINE PyObject * +SWIG_Python_NewPackedObj(void *ptr, size_t sz, swig_type_info *type) { + return ptr ? PySwigPacked_New((void *) ptr, sz, type) : SWIG_Py_Void(); +} + +/* -----------------------------------------------------------------------------* + * Get type list + * -----------------------------------------------------------------------------*/ + +#ifdef SWIG_LINK_RUNTIME +void *SWIG_ReturnGlobalTypeList(void *); +#endif + +SWIGRUNTIME swig_module_info * +SWIG_Python_GetModule(void) { + static void *type_pointer = (void *)0; + /* first check if module already created */ + if (!type_pointer) { +#ifdef SWIG_LINK_RUNTIME + type_pointer = SWIG_ReturnGlobalTypeList((void *)0); +#else + type_pointer = PyCObject_Import((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION, + (char*)"type_pointer" SWIG_TYPE_TABLE_NAME); + if (PyErr_Occurred()) { + PyErr_Clear(); + type_pointer = (void *)0; + } +#endif + } + return (swig_module_info *) type_pointer; +} + +#if PY_MAJOR_VERSION < 2 +/* PyModule_AddObject function was introduced in Python 2.0. The following function + is copied out of Python/modsupport.c in python version 2.3.4 */ +SWIGINTERN int +PyModule_AddObject(PyObject *m, char *name, PyObject *o) +{ + PyObject *dict; + if (!PyModule_Check(m)) { + PyErr_SetString(PyExc_TypeError, + "PyModule_AddObject() needs module as first arg"); + return SWIG_ERROR; + } + if (!o) { + PyErr_SetString(PyExc_TypeError, + "PyModule_AddObject() needs non-NULL value"); + return SWIG_ERROR; + } + + dict = PyModule_GetDict(m); + if (dict == NULL) { + /* Internal error -- modules must have a dict! */ + PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__", + PyModule_GetName(m)); + return SWIG_ERROR; + } + if (PyDict_SetItemString(dict, name, o)) + return SWIG_ERROR; + Py_DECREF(o); + return SWIG_OK; +} +#endif + +SWIGRUNTIME void +SWIG_Python_DestroyModule(void *vptr) +{ + swig_module_info *swig_module = (swig_module_info *) vptr; + swig_type_info **types = swig_module->types; + size_t i; + for (i =0; i < swig_module->size; ++i) { + swig_type_info *ty = types[i]; + if (ty->owndata) { + PySwigClientData *data = (PySwigClientData *) ty->clientdata; + if (data) PySwigClientData_Del(data); + } + } + Py_DECREF(SWIG_This()); +} + +SWIGRUNTIME void +SWIG_Python_SetModule(swig_module_info *swig_module) { + static PyMethodDef swig_empty_runtime_method_table[] = { {NULL, NULL, 0, NULL} };/* Sentinel */ + + PyObject *module = Py_InitModule((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION, + swig_empty_runtime_method_table); + PyObject *pointer = PyCObject_FromVoidPtr((void *) swig_module, SWIG_Python_DestroyModule); + if (pointer && module) { + PyModule_AddObject(module, (char*)"type_pointer" SWIG_TYPE_TABLE_NAME, pointer); + } else { + Py_XDECREF(pointer); + } +} + +/* The python cached type query */ +SWIGRUNTIME PyObject * +SWIG_Python_TypeCache(void) { + static PyObject *SWIG_STATIC_POINTER(cache) = PyDict_New(); + return cache; +} + +SWIGRUNTIME swig_type_info * +SWIG_Python_TypeQuery(const char *type) +{ + PyObject *cache = SWIG_Python_TypeCache(); + PyObject *key = PyString_FromString(type); + PyObject *obj = PyDict_GetItem(cache, key); + swig_type_info *descriptor; + if (obj) { + descriptor = (swig_type_info *) PyCObject_AsVoidPtr(obj); + } else { + swig_module_info *swig_module = SWIG_Python_GetModule(); + descriptor = SWIG_TypeQueryModule(swig_module, swig_module, type); + if (descriptor) { + obj = PyCObject_FromVoidPtr(descriptor, NULL); + PyDict_SetItem(cache, key, obj); + Py_DECREF(obj); + } + } + Py_DECREF(key); + return descriptor; +} + +/* + For backward compatibility only +*/ +#define SWIG_POINTER_EXCEPTION 0 +#define SWIG_arg_fail(arg) SWIG_Python_ArgFail(arg) +#define SWIG_MustGetPtr(p, type, argnum, flags) SWIG_Python_MustGetPtr(p, type, argnum, flags) + +SWIGRUNTIME int +SWIG_Python_AddErrMesg(const char* mesg, int infront) +{ + if (PyErr_Occurred()) { + PyObject *type = 0; + PyObject *value = 0; + PyObject *traceback = 0; + PyErr_Fetch(&type, &value, &traceback); + if (value) { + PyObject *old_str = PyObject_Str(value); + Py_XINCREF(type); + PyErr_Clear(); + if (infront) { + PyErr_Format(type, "%s %s", mesg, PyString_AsString(old_str)); + } else { + PyErr_Format(type, "%s %s", PyString_AsString(old_str), mesg); + } + Py_DECREF(old_str); + } + return 1; + } else { + return 0; + } +} + +SWIGRUNTIME int +SWIG_Python_ArgFail(int argnum) +{ + if (PyErr_Occurred()) { + /* add information about failing argument */ + char mesg[256]; + PyOS_snprintf(mesg, sizeof(mesg), "argument number %d:", argnum); + return SWIG_Python_AddErrMesg(mesg, 1); + } else { + return 0; + } +} + +SWIGRUNTIMEINLINE const char * +PySwigObject_GetDesc(PyObject *self) +{ + PySwigObject *v = (PySwigObject *)self; + swig_type_info *ty = v ? v->ty : 0; + return ty ? ty->str : (char*)""; +} + +SWIGRUNTIME void +SWIG_Python_TypeError(const char *type, PyObject *obj) +{ + if (type) { +#if defined(SWIG_COBJECT_TYPES) + if (obj && PySwigObject_Check(obj)) { + const char *otype = (const char *) PySwigObject_GetDesc(obj); + if (otype) { + PyErr_Format(PyExc_TypeError, "a '%s' is expected, 'PySwigObject(%s)' is received", + type, otype); + return; + } + } else +#endif + { + const char *otype = (obj ? obj->ob_type->tp_name : 0); + if (otype) { + PyObject *str = PyObject_Str(obj); + const char *cstr = str ? PyString_AsString(str) : 0; + if (cstr) { + PyErr_Format(PyExc_TypeError, "a '%s' is expected, '%s(%s)' is received", + type, otype, cstr); + } else { + PyErr_Format(PyExc_TypeError, "a '%s' is expected, '%s' is received", + type, otype); + } + Py_XDECREF(str); + return; + } + } + PyErr_Format(PyExc_TypeError, "a '%s' is expected", type); + } else { + PyErr_Format(PyExc_TypeError, "unexpected type is received"); + } +} + + +/* Convert a pointer value, signal an exception on a type mismatch */ +SWIGRUNTIME void * +SWIG_Python_MustGetPtr(PyObject *obj, swig_type_info *ty, int argnum, int flags) { + void *result; + if (SWIG_Python_ConvertPtr(obj, &result, ty, flags) == -1) { + PyErr_Clear(); + if (flags & SWIG_POINTER_EXCEPTION) { + SWIG_Python_TypeError(SWIG_TypePrettyName(ty), obj); + SWIG_Python_ArgFail(argnum); + } + } + return result; +} + + +#ifdef __cplusplus +#if 0 +{ /* cc-mode */ +#endif +} +#endif +/* -----------------------------------------------------------------------------* + Standard SWIG API for use inside user code. + + Don't include this file directly, run the command + swig -python -external-runtime + Also, read the Modules chapter of the SWIG Manual. + + * -----------------------------------------------------------------------------*/ + +#ifdef SWIG_MODULE_CLIENTDATA_TYPE + +SWIGRUNTIMEINLINE swig_type_info * +SWIG_TypeQuery(SWIG_MODULE_CLIENTDATA_TYPE clientdata, const char *name) { + swig_module_info *module = SWIG_GetModule(clientdata); + return SWIG_TypeQueryModule(module, module, name); +} + +SWIGRUNTIMEINLINE swig_type_info * +SWIG_MangledTypeQuery(SWIG_MODULE_CLIENTDATA_TYPE clientdata, const char *name) { + swig_module_info *module = SWIG_GetModule(clientdata); + return SWIG_MangledTypeQueryModule(module, module, name); +} + +#else + +SWIGRUNTIMEINLINE swig_type_info * +SWIG_TypeQuery(const char *name) { + swig_module_info *module = SWIG_GetModule(NULL); + return SWIG_TypeQueryModule(module, module, name); +} + +SWIGRUNTIMEINLINE swig_type_info * +SWIG_MangledTypeQuery(const char *name) { + swig_module_info *module = SWIG_GetModule(NULL); + return SWIG_MangledTypeQueryModule(module, module, name); +} + +#endif + +""" From scipy-svn at scipy.org Wed Jul 26 19:21:41 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 26 Jul 2006 18:21:41 -0500 (CDT) Subject: [Scipy-svn] r2138 - trunk/Lib/sandbox/svm/tests Message-ID: <20060726232141.19CE239C06F@new.scipy.org> Author: fullung Date: 2006-07-26 18:21:34 -0500 (Wed, 26 Jul 2006) New Revision: 2138 Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py Log: Enable tests that exposed NumPy defect. Modified: trunk/Lib/sandbox/svm/tests/test_dataset.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-26 21:45:21 UTC (rev 2137) +++ trunk/Lib/sandbox/svm/tests/test_dataset.py 2006-07-26 23:21:34 UTC (rev 2138) @@ -10,8 +10,8 @@ class test_dataset(NumpyTestCase): def check_convert_dict(self): - #x = N.array([(-1,0.)], dtype=svm_node_dtype) - #assert_array_equal(convert_to_svm_node({}), x) + x = N.array([(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node({}), x) x = N.array([(1,2.),(-1,0.)], dtype=svm_node_dtype) assert_array_equal(convert_to_svm_node({1:2.}), x) @@ -23,8 +23,8 @@ self.assertRaises(AssertionError, convert_to_svm_node, {0:0.}) def check_convert_list(self): - #x = N.array([(-1,0.)], dtype=svm_node_dtype) - #assert_array_equal(convert_to_svm_node([]), x) + x = N.array([(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node([]), x) x = N.array([(1,2.),(3,4.),(-1,0.)], dtype=svm_node_dtype) # check that indexes are sorted @@ -35,8 +35,8 @@ convert_to_svm_node, [(1,0.),(1,0.)]) def check_convert_array(self): - #x = N.array([(-1,0.)], dtype=svm_node_dtype) - #assert_array_equal(convert_to_svm_node(N.empty(0)), x) + x = N.array([(-1,0.)], dtype=svm_node_dtype) + assert_array_equal(convert_to_svm_node(N.empty(0)), x) x = N.array([(1,1.),(2,2.),(-1,0.)], dtype=svm_node_dtype) assert_array_equal(convert_to_svm_node(N.arange(1,3)), x) From scipy-svn at scipy.org Thu Jul 27 05:50:31 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 27 Jul 2006 04:50:31 -0500 (CDT) Subject: [Scipy-svn] r2139 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060727095031.1158639C00F@new.scipy.org> Author: fullung Date: 2006-07-27 04:50:14 -0500 (Thu, 27 Jul 2006) New Revision: 2139 Modified: trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/tests/test_oneclass.py Log: Improve one-class tests. Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-26 23:21:34 UTC (rev 2138) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-27 09:50:14 UTC (rev 2139) @@ -33,7 +33,7 @@ distribution, while a non-positive value indicates that is is not. """ - return [self.predictor.predict_values(x, 1) for x in dataset] + return [self.predictor.predict_values(x, 1)[0] for x in dataset] class LibSvmOneClassModel(LibSvmModel): """ Modified: trunk/Lib/sandbox/svm/tests/test_oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-26 23:21:34 UTC (rev 2138) +++ trunk/Lib/sandbox/svm/tests/test_oneclass.py 2006-07-27 09:50:14 UTC (rev 2139) @@ -14,30 +14,29 @@ ModelType(kernel) ModelType(kernel, nu=1.0) - def check_train(self): - ModelType = LibSvmOneClassModel + def _make_basic_datasets(self): x = [N.array([0, 0]), N.array([0, 1]), N.array([1, 0]), N.array([1, 1])] traindata = LibSvmOneClassDataSet(x) + testdata = LibSvmTestDataSet(x) + return traindata, testdata + + def check_train(self): + ModelType = LibSvmOneClassModel + traindata, testdata = self._make_basic_datasets() model = ModelType(LinearKernel()) results = model.fit(traindata) - testdata = LibSvmTestDataSet(x) - results.predict(testdata) - results.predict_values(testdata) + p = results.predict(testdata) + assert_array_equal(p, [False, False, False, True]) + v = results.predict_values(testdata) + assert_array_equal(v, [-0.5, 0.0, 0.0, 0.5]) def check_more(self): + traindata, testdata = self._make_basic_datasets() ModelType = LibSvmOneClassModel - - x = [N.array([0, 0]), - N.array([0, 1]), - N.array([1, 0]), - N.array([1, 1])] - traindata = LibSvmOneClassDataSet(x) nu = 0.5 - testdata = LibSvmTestDataSet(x) - kernels = [ LinearKernel(), PolynomialKernel(3, traindata.gamma, 0.0), @@ -48,7 +47,6 @@ [False, False, False, True], [True, False, False, False] ] - for kernel, expected_pred in zip(kernels, expected_preds): model = ModelType(kernel, nu) results = model.fit(traindata) From scipy-svn at scipy.org Thu Jul 27 06:02:37 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 27 Jul 2006 05:02:37 -0500 (CDT) Subject: [Scipy-svn] r2140 - trunk/Lib/sandbox/svm/tests Message-ID: <20060727100237.28C3A39C00F@new.scipy.org> Author: fullung Date: 2006-07-27 05:02:23 -0500 (Thu, 27 Jul 2006) New Revision: 2140 Modified: trunk/Lib/sandbox/svm/tests/test_regression.py Log: Extend tests to check nu-SVR. Modified: trunk/Lib/sandbox/svm/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-27 09:50:14 UTC (rev 2139) +++ trunk/Lib/sandbox/svm/tests/test_regression.py 2006-07-27 10:02:23 UTC (rev 2140) @@ -27,7 +27,6 @@ def check_epsilon_train(self): ModelType = LibSvmEpsilonRegressionModel - y = [10., 20., 30., 40.] x = [N.array([0, 0]), N.array([0, 1]), @@ -40,38 +39,46 @@ results.predict(testdata) results.get_svr_probability() - def check_epsilon_more(self): - ModelType = LibSvmEpsilonRegressionModel - - y = [0.0, 1.0, 1.0, 2.0] + def _make_basic_datasets(self): + labels = [0, 1.0, 1.0, 2.0] x = [N.array([0, 0]), N.array([0, 1]), N.array([1, 0]), N.array([1, 1])] - epsilon = 0.1 - cost = 10.0 - traindata = LibSvmRegressionDataSet(zip(y, x)) + traindata = LibSvmRegressionDataSet(zip(labels, x)) testdata = LibSvmTestDataSet(x) + return traindata, testdata + def _make_basic_kernels(self, gamma): kernels = [ LinearKernel(), - PolynomialKernel(3, traindata.gamma, 0.0), - RBFKernel(traindata.gamma) + PolynomialKernel(3, gamma, 0.0), + RBFKernel(gamma) ] + return kernels + + def check_epsilon_more(self): + ModelType = LibSvmEpsilonRegressionModel + epsilon = 0.1 + cost = 10.0 + modelargs = epsilon, cost expected_ys = [ N.array([0.1, 1.0, 1.0, 1.9]), N.array([0.24611273, 0.899866638, 0.90006681, 1.90006681]), N.array([0.1, 1.0, 1.0, 1.9]) ] + self._regression_basic(ModelType, modelargs, expected_ys) + def _regression_basic(self, ModelType, modelargs, expected_ys): + traindata, testdata = self._make_basic_datasets() + kernels = self._make_basic_kernels(traindata.gamma) for kernel, expected_y in zip(kernels, expected_ys): - model = ModelType(kernel, epsilon, cost) + args = (kernel,) + modelargs + model = ModelType(*args) results = model.fit(traindata) predictions = results.predict(testdata) - # look at differences instead of using assertAlmostEqual - # due to slight differences between answers obtained on - # Windows with MSVC 7.1 and on Fedora Core 5 with GCC - # 4.1.1. + # use differences instead of assertAlmostEqual due to + # compiler-dependent variations in these values diff = N.absolute(predictions - expected_y) self.assert_(N.alltrue(diff < 1e-3)) @@ -84,8 +91,17 @@ nr_fold = 10 mse, scc = model.cross_validate(traindata, nr_fold) - def check_nu_train(self): - pass + def check_nu_more(self): + ModelType = LibSvmNuRegressionModel + nu = 0.4 + cost = 10.0 + modelargs = nu, cost + expected_ys = [ + N.array([0.0, 1.0, 1.0, 2.0]), + N.array([0.2307521, 0.7691364, 0.76930371, 1.769304]), + N.array([0.0, 1.0, 1.0, 2.0]) + ] + self._regression_basic(ModelType, modelargs, expected_ys) def _make_datasets(self): y1 = N.random.randn(50) From scipy-svn at scipy.org Thu Jul 27 15:59:36 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 27 Jul 2006 14:59:36 -0500 (CDT) Subject: [Scipy-svn] r2141 - trunk/Lib/special Message-ID: <20060727195936.41F2D39C035@new.scipy.org> Author: cookedm Date: 2006-07-27 14:59:34 -0500 (Thu, 27 Jul 2006) New Revision: 2141 Modified: trunk/Lib/special/basic.py Log: fix doc string of scipy.special.basic.lpmn to be associated legendre functions of the first kind. Modified: trunk/Lib/special/basic.py =================================================================== --- trunk/Lib/special/basic.py 2006-07-27 10:02:23 UTC (rev 2140) +++ trunk/Lib/special/basic.py 2006-07-27 19:59:34 UTC (rev 2141) @@ -471,7 +471,7 @@ return fc[:km] def lpmn(m,n,z): - """Associated Legendre functions of the second kind, Pmn(z) and its + """Associated Legendre functions of the first kind, Pmn(z) and its derivative, Pmn'(z) of order m and degree n. Returns two arrays of size (m+1,n+1) containing Pmn(z) and Pmn'(z) for all orders from 0..m and degrees from 0..n. From scipy-svn at scipy.org Thu Jul 27 20:10:58 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 27 Jul 2006 19:10:58 -0500 (CDT) Subject: [Scipy-svn] r2142 - in trunk/Lib/sandbox/svm: . tests Message-ID: <20060728001058.7855F39C035@new.scipy.org> Author: fullung Date: 2006-07-27 19:10:27 -0500 (Thu, 27 Jul 2006) New Revision: 2142 Modified: trunk/Lib/sandbox/svm/classification.py trunk/Lib/sandbox/svm/dataset.py trunk/Lib/sandbox/svm/oneclass.py trunk/Lib/sandbox/svm/predict.py trunk/Lib/sandbox/svm/regression.py trunk/Lib/sandbox/svm/tests/test_all.py trunk/Lib/sandbox/svm/tests/test_classification.py Log: Classification in Python in preparation for some optimization. Modified: trunk/Lib/sandbox/svm/classification.py =================================================================== --- trunk/Lib/sandbox/svm/classification.py 2006-07-27 19:59:34 UTC (rev 2141) +++ trunk/Lib/sandbox/svm/classification.py 2006-07-28 00:10:27 UTC (rev 2142) @@ -74,6 +74,9 @@ return int(label), prob_estimates return [p(x) for x in dataset] + def compact(self): + self.predictor.compact() + class LibSvmClassificationModel(LibSvmModel): """ A model for support vector classification. Modified: trunk/Lib/sandbox/svm/dataset.py =================================================================== --- trunk/Lib/sandbox/svm/dataset.py 2006-07-27 19:59:34 UTC (rev 2141) +++ trunk/Lib/sandbox/svm/dataset.py 2006-07-28 00:10:27 UTC (rev 2142) @@ -16,13 +16,9 @@ for y, x in data: key = x.__array_interface__['data'][0] self.iddatamap[key] = x + self.__len__ = self.data.__len__ + self.__iter__ = self.data.__iter__ - def __iter__(self): - return self.data.__iter__() - - def __len__(self): - return len(self.data) - def getgamma(self): maxlen = 0 for y, x in self.data: @@ -164,13 +160,10 @@ class LibSvmTestDataSet: def __init__(self, origdata): self.data = map(lambda x: convert_to_svm_node(x), origdata) + self.__len__ = self.data.__len__ + self.__iter__ = self.data.__iter__ + self.__getitem__ = self.data.__getitem__ - def __len__(self): - return len(self.data) - - def __iter__(self): - return self.data.__iter__() - def convert_to_svm_node(x): y = N.empty(len(x) + 1, dtype=libsvm.svm_node_dtype) y[-1] = -1, 0. Modified: trunk/Lib/sandbox/svm/oneclass.py =================================================================== --- trunk/Lib/sandbox/svm/oneclass.py 2006-07-27 19:59:34 UTC (rev 2141) +++ trunk/Lib/sandbox/svm/oneclass.py 2006-07-28 00:10:27 UTC (rev 2142) @@ -35,6 +35,9 @@ """ return [self.predictor.predict_values(x, 1)[0] for x in dataset] + def compact(self): + self.predictor.compact() + class LibSvmOneClassModel(LibSvmModel): """ A model for distribution estimation (one-class SVM). Modified: trunk/Lib/sandbox/svm/predict.py =================================================================== --- trunk/Lib/sandbox/svm/predict.py 2006-07-27 19:59:34 UTC (rev 2141) +++ trunk/Lib/sandbox/svm/predict.py 2006-07-28 00:10:27 UTC (rev 2142) @@ -56,14 +56,29 @@ label = libsvm.svm_predict_probability(self.model, xptr, peptr) return label, pe + def compact(self): + raise NotImplementedError + class LibSvmPythonPredictor: def __init__(self, model, dataset, kernel): self.kernel = kernel modelc = model.contents - - self.rho = modelc.rho[0] - self.sv_coef = modelc.sv_coef[0][:modelc.l] self.svm_type = modelc.param.svm_type + if self.svm_type in [libsvm.C_SVC, libsvm.NU_SVC]: + self.nr_class = modelc.nr_class + self.labels = modelc.labels[:self.nr_class] + nrho = self.nr_class * (self.nr_class - 1) / 2 + self.rho = modelc.rho[:nrho] + self.sv_coef = [modelc.sv_coef[i][:modelc.l] + for i in range(self.nr_class - 1)] + self.nSV = [modelc.nSV[i] for i in range(self.nr_class)] + start = N.zeros((self.nr_class,), N.intc) + for i in range(1, self.nr_class): + start[i] = start[i - 1] + modelc.nSV[i - 1] + self.start = start + else: + self.rho = modelc.rho[0] + self.sv_coef = modelc.sv_coef[0][:modelc.l] if modelc.param.kernel_type != libsvm.PRECOMPUTED: svptrs = [modelc.SV[i] for i in range(modelc.l)] @@ -73,21 +88,55 @@ ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] support_vectors = [dataset[id] for id in ids] self.support_vectors = support_vectors - libsvm.svm_destroy_model(model) def predict(self, x): if self.svm_type in [libsvm.C_SVC, libsvm.NU_SVC]: - raise NotImplementedError + n = self.nr_class * (self.nr_class - 1) / 2 + dec_values = self.predict_values(x, n) + vote = N.zeros((self.nr_class,), N.intc) + pos = 0 + for i in range(self.nr_class): + for j in range(i + 1, self.nr_class): + if dec_values[pos] > 0: + vote[i] += 1 + else: + vote[j] += 1 + pos += 1 + return self.labels[vote.argmax()] else: return self.predict_values(x, 1) def predict_values(self, x, n): - z = -self.rho - # XXX possible optimization: izip - for sv_coef, sv in zip(self.sv_coef, self.support_vectors): - z += sv_coef * self.kernel(x, sv, svm_node_dot) - return z + if self.svm_type in [libsvm.C_SVC, libsvm.NU_SVC]: + kvalue = N.empty((len(self.support_vectors),)) + for i, sv in enumerate(self.support_vectors): + kvalue[i] = self.kernel(x, sv, svm_node_dot) + p = 0 + dec_values = N.empty((n,)) + for i in range(self.nr_class): + for j in range(i + 1, self.nr_class): + sum = 0 + si, sj = self.start[i], self.start[j] + ci, cj = self.nSV[i], self.nSV[j] + coef1 = self.sv_coef[j - 1] + coef2 = self.sv_coef[i] + sum = -self.rho[p] + for k in range(ci): + sum += coef1[si + k] * kvalue[si + k] + for k in range(cj): + sum += coef2[sj + k] * kvalue[sj + k] + dec_values[p] = sum + p += 1 + return dec_values + else: + z = -self.rho + for sv_coef, sv in zip(self.sv_coef, self.support_vectors): + z += sv_coef * self.kernel(x, sv, svm_node_dot) + return z def predict_probability(self, x, n): raise NotImplementedError + + def compact(self): + raise NotImplementedError Modified: trunk/Lib/sandbox/svm/regression.py =================================================================== --- trunk/Lib/sandbox/svm/regression.py 2006-07-27 19:59:34 UTC (rev 2141) +++ trunk/Lib/sandbox/svm/regression.py 2006-07-28 00:10:27 UTC (rev 2142) @@ -44,6 +44,9 @@ """ return self.sigma + def compact(self): + self.predictor.compact() + class LibSvmRegressionModel(LibSvmModel): ResultsType = LibSvmRegressionResults Modified: trunk/Lib/sandbox/svm/tests/test_all.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-27 19:59:34 UTC (rev 2141) +++ trunk/Lib/sandbox/svm/tests/test_all.py 2006-07-28 00:10:27 UTC (rev 2142) @@ -1,8 +1,8 @@ from test_classification import * from test_dataset import * +from test_kernel import * from test_libsvm import * from test_oneclass import * -from test_precomputed import * from test_regression import * if __name__ == '__main__': Modified: trunk/Lib/sandbox/svm/tests/test_classification.py =================================================================== --- trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-27 19:59:34 UTC (rev 2141) +++ trunk/Lib/sandbox/svm/tests/test_classification.py 2006-07-28 00:10:27 UTC (rev 2142) @@ -205,5 +205,26 @@ except NotImplementedError: self.assert_(fitargs[-1] is LibSvmPythonPredictor) + def check_python_predict(self): + traindata, testdata = self._make_basic_datasets() + kernel = LinearKernel() + cost = 10.0 + weights = [(1, 10.0)] + model = LibSvmCClassificationModel(kernel, cost, weights) + + refresults = model.fit(traindata) + results = model.fit(traindata, LibSvmPythonPredictor) + + refv = refresults.predict_values(testdata) + v = results.predict_values(testdata) + self.assertEqual(len(refv), len(v)) + for pred, refpred in zip(v, refv): + for key, value in refpred.iteritems(): + assert_array_almost_equal(value, pred[key]) + + refp = refresults.predict(testdata) + p = results.predict(testdata) + assert_array_equal(p, refp) + if __name__ == '__main__': NumpyTest().run()