From scipy-svn at scipy.org Mon Oct 2 04:17:54 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 2 Oct 2006 03:17:54 -0500 (CDT) Subject: [Scipy-svn] r2239 - trunk/Lib/stats Message-ID: <20061002081754.26E5939C0EF@new.scipy.org> Author: stefan Date: 2006-10-02 03:17:40 -0500 (Mon, 02 Oct 2006) New Revision: 2239 Modified: trunk/Lib/stats/stats.py Log: Fix typo. Modified: trunk/Lib/stats/stats.py =================================================================== --- trunk/Lib/stats/stats.py 2006-09-30 23:39:22 UTC (rev 2238) +++ trunk/Lib/stats/stats.py 2006-10-02 08:17:40 UTC (rev 2239) @@ -1216,7 +1216,7 @@ a = asarray(a).copy() mask = zeros(a.shape, dtype=bool) if threshmin is not None: - mask = (a < threshmin) + mask |= (a < threshmin) if threshmax is not None: mask |= (a > threshmax) a[mask] = newval From scipy-svn at scipy.org Thu Oct 5 01:55:20 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 5 Oct 2006 00:55:20 -0500 (CDT) Subject: [Scipy-svn] r2240 - in trunk/Lib: sandbox signal Message-ID: <20061005055520.630F439C00A@new.scipy.org> Author: oliphant Date: 2006-10-05 00:55:13 -0500 (Thu, 05 Oct 2006) New Revision: 2240 Added: trunk/Lib/sandbox/oliphant/ Modified: trunk/Lib/signal/signaltools.py Log: Add flat top window Modified: trunk/Lib/signal/signaltools.py =================================================================== --- trunk/Lib/signal/signaltools.py 2006-10-02 08:17:40 UTC (rev 2239) +++ trunk/Lib/signal/signaltools.py 2006-10-05 05:55:13 UTC (rev 2240) @@ -668,7 +668,25 @@ w = w[:-1] return w +def flattop(M,sym=1): + """The M-point Flat top window. + """ + if M < 1: + return array([]) + if M == 1: + return ones(1,'d') + odd = M % 2 + if not sym and not odd: + M = M+1 + a = [0.2156, 0.4160, 0.2781, 0.0836, 0.0069] + n = arange(0,M) + fac = n*2*pi/(M-1.0) + w = a[0] - a[1]*cos(fac) + a[2]*cos(2*fac) - a[3]*cos(3*fac) + a[4]*cos(4*fac) + if not sym and not odd: + w = w[:-1] + return w + def bartlett(M,sym=1): """The M-point Bartlett window. """ @@ -734,6 +752,8 @@ w = w[:-1] return w + + def kaiser(M,beta,sym=1): """Returns a Kaiser window of length M with shape parameter beta. """ @@ -1214,7 +1234,8 @@ winfunc = nuttall elif winstr in ['barthann', 'brthan', 'bth']: winfunc = barthann - + elif winstr in ['flattop', 'flat', 'flt']: + winfunc = flattop elif winstr in ['kaiser', 'ksr']: winfunc = kaiser elif winstr in ['gaussian', 'gauss', 'gss']: From scipy-svn at scipy.org Thu Oct 5 09:09:28 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 5 Oct 2006 08:09:28 -0500 (CDT) Subject: [Scipy-svn] r2241 - in trunk/Lib/sandbox/numexpr: . tests Message-ID: <20061005130928.6D77839C00A@new.scipy.org> Author: tim_hochberg Date: 2006-10-04 23:08:32 -0500 (Wed, 04 Oct 2006) New Revision: 2241 Modified: trunk/Lib/sandbox/numexpr/interpreter.c trunk/Lib/sandbox/numexpr/tests/test_numexpr.py trunk/Lib/sandbox/numexpr/timing.py Log: Make copies of record arrays when they are not aligned correctly. Modified: trunk/Lib/sandbox/numexpr/interpreter.c =================================================================== --- trunk/Lib/sandbox/numexpr/interpreter.c 2006-10-05 05:55:13 UTC (rev 2240) +++ trunk/Lib/sandbox/numexpr/interpreter.c 2006-10-05 04:08:32 UTC (rev 2241) @@ -1082,10 +1082,11 @@ } } else { PyObject *origA = a; - /* Check discontiguous strides appear only on the last dimension. */ - for (j = PyArray_NDIM(a)-2; j >= 0; j--) { - if (PyArray_STRIDE(a, j) != - PyArray_STRIDE(a, j+1) * PyArray_DIM(a, j+1)) { + int inner_size = -1; + /* Check array is contiguous */ + for (j = PyArray_NDIM(a)-1; j >= 0; j--) { + if ((inner_size == -1 && PyArray_STRIDE(a, j) % PyArray_ITEMSIZE(a)) || + (inner_size != -1 && PyArray_STRIDE(a, j) != inner_size)) { intp dims[1] = {BLOCK_SIZE1}; inddata[i+1].count = PyArray_NDIM(a); inddata[i+1].findex = -1; @@ -1100,6 +1101,7 @@ PyTuple_SET_ITEM(a_inputs, i+2*n_inputs, a); /* steals reference */ break; } + inner_size = PyArray_STRIDE(a, j) * PyArray_DIM(a, j); } self->memsteps[i+1] = PyArray_STRIDE(a, PyArray_NDIM(a)-1); Modified: trunk/Lib/sandbox/numexpr/tests/test_numexpr.py =================================================================== --- trunk/Lib/sandbox/numexpr/tests/test_numexpr.py 2006-10-05 05:55:13 UTC (rev 2240) +++ trunk/Lib/sandbox/numexpr/tests/test_numexpr.py 2006-10-05 04:08:32 UTC (rev 2241) @@ -139,6 +139,13 @@ a = arange(100).reshape(10,10)[::2] b = arange(50).reshape(5,10) assert_array_equal(evaluate("a+b"), a+b) + c = empty([10], dtype=[('c1', int32), ('c2', uint16)]) + c['c1'] = arange(10) + c['c2'].fill(0xaaaa) + c1 = c['c1'] + a0 = a[0] + assert_array_equal(evaluate("c1"), c1) + assert_array_equal(evaluate("a0+c1"), a0+c1) def check_broadcasting(self): Modified: trunk/Lib/sandbox/numexpr/timing.py =================================================================== --- trunk/Lib/sandbox/numexpr/timing.py 2006-10-05 05:55:13 UTC (rev 2240) +++ trunk/Lib/sandbox/numexpr/timing.py 2006-10-05 04:08:32 UTC (rev 2241) @@ -58,7 +58,7 @@ try: from scipy.weave import blitz except: pass from numexpr import evaluate -a = arange(%f) +a = arange(2*%f)[::2] b = arange(%f) result = arange(%f) """ % ((array_size,)*3) @@ -70,7 +70,7 @@ try: from scipy.weave import blitz except: pass from numexpr import evaluate -a = arange(%f) +a = arange(2*%f)[::2] b = arange(%f) result = arange(%f) """ % ((array_size,)*3) @@ -82,7 +82,7 @@ try: from scipy.weave import blitz except: pass from numexpr import evaluate -a = arange(%f, dtype=float) +a = arange(2*%f, dtype=float)[::2] b = arange(%f, dtype=float) result = arange(%f, dtype=float) """ % ((array_size,)*3) From scipy-svn at scipy.org Fri Oct 6 14:25:44 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Fri, 6 Oct 2006 13:25:44 -0500 (CDT) Subject: [Scipy-svn] r2242 - trunk/Lib/signal Message-ID: <20061006182544.BA6B539C0DA@new.scipy.org> Author: stefan Date: 2006-10-06 13:25:18 -0500 (Fri, 06 Oct 2006) New Revision: 2242 Modified: trunk/Lib/signal/info.py trunk/Lib/signal/signaltools.py Log: Clean up signal documentation. Modified: trunk/Lib/signal/info.py =================================================================== --- trunk/Lib/signal/info.py 2006-10-05 04:08:32 UTC (rev 2241) +++ trunk/Lib/signal/info.py 2006-10-06 18:25:18 UTC (rev 2242) @@ -11,7 +11,6 @@ correlate2d -- 2-dimensional correlation (more options). sepfir2d -- Convolve with a 2-D separable FIR filter. - B-splines: bspline -- B-spline basis function of order n. @@ -35,20 +34,26 @@ deconvolve -- 1-d deconvolution using lfilter. - hilbert --- Compute the analytic signal of a 1-d signal. - get_window --- Create FIR window. + hilbert -- Compute the analytic signal of a 1-d signal. + get_window -- Create FIR window. - detrend --- Remove linear and/or constant trends from data. + detrend -- Remove linear and/or constant trends from data. + resample -- Resample using Fourier method. Filter design: - remez -- Optimal FIR filter design. - firwin --- Windowed FIR filter design. - iirdesign --- IIR filter design given bands and gains - iirfilter --- IIR filter design given order and critical frequencies - freqs --- Analog filter frequency response - freqz --- Digital filter frequency response + remez -- Optimal FIR filter design. + firwin -- Windowed FIR filter design. + iirdesign -- IIR filter design given bands and gains. + iirfilter -- IIR filter design given order and critical frequencies. + freqs -- Analog filter frequency response. + freqz -- Digital filter frequency response. + unique_roots -- Unique roots and their multiplicities. + residue -- Partial fraction expansion of b(s) / a(s). + residuez -- Partial fraction expansion of b(z) / a(z). + invres -- Inverse partial fraction expansion. + Matlab-style IIR filter design: butter (buttord) -- Butterworth @@ -59,31 +64,50 @@ Linear Systems: - lti -- linear time invariant system object. - lsim -- continuous-time simulation of output to linear system. + lti -- linear time invariant system object. + lsim -- continuous-time simulation of output to linear system. impulse -- impulse response of linear, time-invariant (LTI) system. - step -- step response of continous-time LTI system. + step -- step response of continous-time LTI system. LTI Reresentations: tf2zpk -- transfer function to zero-pole-gain. zpk2tf -- zero-pole-gain to transfer function. - tf2ss -- transfer function to state-space. - ss2tf -- state-pace to transfer function. + tf2ss -- transfer function to state-space. + ss2tf -- state-pace to transfer function. zpk2ss -- zero-pole-gain to state-space. ss2zpk -- state-space to pole-zero-gain. Waveforms: - sawtooth -- Periodic sawtooth - square -- Square wave + sawtooth -- Periodic sawtooth + square -- Square wave gausspulse -- Gaussian modulated sinusoid - chirp -- Frequency swept cosine signal + chirp -- Frequency swept cosine signal + Window functions: + + boxcar -- Boxcar window + triang -- Triangular window + parzen -- Parzen window + bohman -- Bohman window + blackman -- Blackman window + blackmanharris -- Minimum 4-term Blackman-Harris window + nuttall -- Nuttall's minimum 4-term Blackman-Harris window + flattop -- Flat top window + bartlett -- Bartlett window + hann -- Hann window + barthann -- Bartlett-Hann window + hamming -- Hamming window + kaiser -- Kaiser window + gaussian -- Gaussian window + general_gaussian -- Generalized Gaussian window + slepian -- Slepian window + Wavelets: - daub -- return low-pass filter for daubechies wavelets - qmf -- return quadrature mirror filter from low-pass + daub -- return low-pass filter for daubechies wavelets + qmf -- return quadrature mirror filter from low-pass cascade -- compute scaling function and wavelet from coefficients """ Modified: trunk/Lib/signal/signaltools.py =================================================================== --- trunk/Lib/signal/signaltools.py 2006-10-05 04:08:32 UTC (rev 2241) +++ trunk/Lib/signal/signaltools.py 2006-10-06 18:25:18 UTC (rev 2242) @@ -89,6 +89,7 @@ def fftconvolve(in1, in2, mode="full"): """Convolve two N-dimensional arrays using FFT. See convolve. + """ s1 = array(in1.shape) s2 = array(in2.shape) @@ -367,6 +368,7 @@ out -- An array the same size as input containing the median filtered result. + """ image = asarray(input) if kernel_size is None: @@ -509,6 +511,7 @@ The output vector zi contains zi = {z_0[-1], z_1[-1], ..., z_K-1[-1]} where K=max(M,N). + """ N = size(a)-1 M = size(b)-1 @@ -536,6 +539,7 @@ def deconvolve(signal, divisor): """Deconvolves divisor out of signal. + """ num = atleast_1d(signal) den = atleast_1d(divisor) @@ -554,11 +558,13 @@ def boxcar(M,sym=1): """The M-point boxcar window. + """ return ones(M, float) def triang(M,sym=1): """The M-point triangular window. + """ if M < 1: return array([]) @@ -580,7 +586,8 @@ return w def parzen(M,sym=1): - """The M-point Parzen window + """The M-point Parzen window. + """ if M < 1: return array([]) @@ -600,7 +607,8 @@ return w def bohman(M,sym=1): - """The M-point Bohman window + """The M-point Bohman window. + """ if M < 1: return array([]) @@ -618,6 +626,7 @@ def blackman(M,sym=1): """The M-point Blackman window. + """ if M < 1: return array([]) @@ -634,6 +643,7 @@ def nuttall(M,sym=1): """A minimum 4-term Blackman-Harris window according to Nuttall. + """ if M < 1: return array([]) @@ -652,6 +662,7 @@ def blackmanharris(M,sym=1): """The M-point minimum 4-term Blackman-Harris window. + """ if M < 1: return array([]) @@ -670,6 +681,7 @@ def flattop(M,sym=1): """The M-point Flat top window. + """ if M < 1: return array([]) @@ -689,6 +701,7 @@ def bartlett(M,sym=1): """The M-point Bartlett window. + """ if M < 1: return array([]) @@ -705,6 +718,7 @@ def hanning(M,sym=1): """The M-point Hanning window. + """ if M < 1: return array([]) @@ -719,8 +733,11 @@ w = w[:-1] return w +hann = hanning + def barthann(M,sym=1): """Return the M-point modified Bartlett-Hann window. + """ if M < 1: return array([]) @@ -738,6 +755,7 @@ def hamming(M,sym=1): """The M-point Hamming window. + """ if M < 1: return array([]) @@ -755,7 +773,8 @@ def kaiser(M,beta,sym=1): - """Returns a Kaiser window of length M with shape parameter beta. + """Return a Kaiser window of length M with shape parameter beta. + """ if M < 1: return array([]) @@ -772,7 +791,8 @@ return w def gaussian(M,std,sym=1): - """Returns a Gaussian window of length M with standard-deviation std. + """Return a Gaussian window of length M with standard-deviation std. + """ if M < 1: return array([]) @@ -789,11 +809,12 @@ return w def general_gaussian(M,p,sig,sym=1): - """Returns a window with a generalized Gaussian shape. + """Return a window with a generalized Gaussian shape. exp(-0.5*(x/sig)**(2*p)) half power point is at (2*log(2)))**(1/(2*p))*sig + """ if M < 1: return array([]) @@ -810,6 +831,9 @@ def slepian(M,width,sym=1): + """Return the M-point slepian window. + + """ if (M*width > 27.38): raise ValueError, "Cannot reliably obtain slepian sequences for"\ " M*width > 27.38." @@ -1327,6 +1351,7 @@ If bp is given, then it is a sequence of points at which to break a piecewise-linear fit to the data. + """ if type not in ['linear','l','constant','c']: raise ValueError, "Trend type must be linear or constant" From scipy-svn at scipy.org Sat Oct 7 09:34:11 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 7 Oct 2006 08:34:11 -0500 (CDT) Subject: [Scipy-svn] r2243 - trunk/Lib/io Message-ID: <20061007133411.6C41B39C141@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-10-07 08:34:06 -0500 (Sat, 07 Oct 2006) New Revision: 2243 Modified: trunk/Lib/io/mio4.py trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py Log: First draft of downcasting machinery Modified: trunk/Lib/io/mio4.py =================================================================== --- trunk/Lib/io/mio4.py 2006-10-06 18:25:18 UTC (rev 2242) +++ trunk/Lib/io/mio4.py 2006-10-07 13:34:06 UTC (rev 2243) @@ -321,9 +321,12 @@ if scipy.sparse.issparse(arr): return Mat4SparseWriter(stream, arr, name) arr = array(arr) - if arr.dtype.hasobject: + dtt = arr.dtype.type + if dtt is object_: raise TypeError, 'Cannot save object arrays in Mat4' - if arr.dtype.kind in ('U', 'S'): + elif dtt is void: + raise TypeError, 'Cannot save void type arrays' + elif dtt in (unicode_, string_): return Mat4CharWriter(stream, arr, name) else: return Mat4NumericWriter(stream, arr, name) Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-10-06 18:25:18 UTC (rev 2242) +++ trunk/Lib/io/mio5.py 2006-10-07 13:34:06 UTC (rev 2243) @@ -33,6 +33,11 @@ from miobase import * +try: # Python 2.3 support + from sets import Set as set +except: + pass + miINT8 = 1 miUINT8 = 2 miINT16 = 3 Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-10-06 18:25:18 UTC (rev 2242) +++ trunk/Lib/io/miobase.py 2006-10-07 13:34:06 UTC (rev 2243) @@ -370,3 +370,129 @@ ''' Base class for Mat file writers ''' def __init__(self, file_stream): self.file_stream = file_stream + + +class DownCaster(object): + ''' Downcasts arrays ''' + + def __init__(self, + type_list=None, + rtol=1.0000000000000001e-05, + atol=1e-08): + ''' Set types for which we are attempting to downcast ''' + def_dict = self.default_dt_dict() + if type_list is None: + self.dt_dict = def_dict + else: + dt_dict = {} + for T in type_list: + T = dtype(T).type + dt_dict[T] = def_dict[T] + self.dt_dict = dt_dict + self.rtol = rtol + self.atol = atol + + def default_dt_dict(self): + d_dict = {} + for sc_type in ('complex','float'): + t_list = sctypes[sc_type] + for T in t_list: + dt = dtype(T) + d_dict[T] = { + 'kind': dt.kind, + 'size': dt.itemsize} + for T in sctypes['int']: + dt = dtype(T) + sz = dt.itemsize + bits = sz*8-1 + end = 2**bits + d_dict[T] = { + 'kind': dt.kind, + 'size': sz, + 'min': -end, + 'max': end-1 + } + for T in sctypes['uint']: + dt = dtype(T) + sz = dt.itemsize + bits = sz*8 + end = 2**bits + d_dict[T] = { + 'kind': dt.kind, + 'size': sz, + 'min': 0, + 'max': end + } + return d_dict + + def storage_criterion(self, maxstorage, kinds, cmp_func=lambda x, y: x <= y): + D = {} + for k, v in self.dt_dict.items(): + if v['kind'] in kinds: + sz = v['size'] + if cmp_func(sz, maxstorage): + D[k] = sz + I = D.items() + I.sort(lambda x, y: cmp(x[1], y[1])) + return I + + def smaller_same_kind(self, arr): + dts = self.storage_criterion(arr.dtype.itemsize, + (arr.dtype.kind,), + lambda x, y: x < y) + ret_arr = arr + for T in dts: + test_arr = arr.astype(T) + if allclose(test_arr, arr, self.rtol, self.atol): + ret_arr = test_arr + else: + break + return ret_arr + + + def smallest_int_type(self, mx, mn): + dt = None + for k, v in self.dt_dict.items(): + if v['kind'] in ('i', 'u'): + if v['max'] >= mx and v['min'] <= mn: + c_sz = v['size'] + if dt is None or c_sz < sz: + dt = k + sz = c_sz + return dt + + def downcast(self, arr): + dtk = arr.dtype.kind + if dtk == 'c': + return self.downcast_complex(arr) + elif dtk == 'f': + return self.downcast_float(arr) + elif dtk in ('u', 'i'): + return self.downcast_integer(arr) + else: + raise TypeError, 'Do not recognize array kind %s' % dtk + + def downcast_complex(self, arr): + # can we downcast to float? + flts = self.storage_criterion(arr.dtype.itemsize / 2, + ('f'), + lambda x, y: x <=y)[0] + test_arr = arr.astype(flt) + if allclose(arr, test_arr, self.rtol, self.atol): + return self.downcast_float(test_arr) + # try downcasting to another complex type + return self.smaller_same_kind(arr) + + def downcast_float(self, arr): + # Try integer + test_arr = self.downcast_integer(arr) + if allclose(arr, test_arr, self.rtol, self.atol): + return test_arr + # Otherwise descend the float types + return self.smaller_same_kind(arr) + + def downcast_integer(self, arr): + mx = amax(arr) + mn = amin(arr) + idt = self.smallest_int_type(mx, mn) + return arr.astype(idt) From scipy-svn at scipy.org Sat Oct 7 11:32:04 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 7 Oct 2006 10:32:04 -0500 (CDT) Subject: [Scipy-svn] r2244 - trunk/Lib/interpolate Message-ID: <20061007153204.90A3B39C26B@new.scipy.org> Author: stefan Date: 2006-10-07 10:31:49 -0500 (Sat, 07 Oct 2006) New Revision: 2244 Modified: trunk/Lib/interpolate/interpolate.py Log: In interp2d, ensure that the argument passed to bisplrep is a meshgrid. Modified: trunk/Lib/interpolate/interpolate.py =================================================================== --- trunk/Lib/interpolate/interpolate.py 2006-10-07 13:34:06 UTC (rev 2243) +++ trunk/Lib/interpolate/interpolate.py 2006-10-07 15:31:49 UTC (rev 2244) @@ -8,7 +8,7 @@ from numpy import shape, sometrue, rank, array, transpose, \ swapaxes, searchsorted, clip, take, ones, putmask, less, greater, \ - logical_or, atleast_1d, atleast_2d + logical_or, atleast_1d, atleast_2d, meshgrid import numpy as np import fitpack @@ -55,10 +55,12 @@ ------ ValueError when inputs are invalid. - """ - + """ self.x = atleast_1d(x).copy() self.y = atleast_1d(y).copy() + if rank(self.x) == 1 and rank(self.y) == 1: + # Prepare meshgrid for bisplrep + x, y = meshgrid(x,y) if rank(self.x) > 2 or rank(self.y) > 2: raise ValueError("One of the input arrays is not 1-d or 2-d.") if rank(self.x) == 2: From scipy-svn at scipy.org Sat Oct 7 12:22:19 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 7 Oct 2006 11:22:19 -0500 (CDT) Subject: [Scipy-svn] r2245 - trunk/Lib/interpolate Message-ID: <20061007162219.00E1939C278@new.scipy.org> Author: stefan Date: 2006-10-07 11:21:58 -0500 (Sat, 07 Oct 2006) New Revision: 2245 Modified: trunk/Lib/interpolate/interpolate.py Log: In interp2d, require unambiguous specification of coordinates and data. This change may break some working code, but the behaviour is now more consistent and closer to what one would intuitively expect. Modified: trunk/Lib/interpolate/interpolate.py =================================================================== --- trunk/Lib/interpolate/interpolate.py 2006-10-07 15:31:49 UTC (rev 2244) +++ trunk/Lib/interpolate/interpolate.py 2006-10-07 16:21:58 UTC (rev 2245) @@ -8,7 +8,7 @@ from numpy import shape, sometrue, rank, array, transpose, \ swapaxes, searchsorted, clip, take, ones, putmask, less, greater, \ - logical_or, atleast_1d, atleast_2d, meshgrid + logical_or, atleast_1d, atleast_2d, meshgrid, ravel import numpy as np import fitpack @@ -34,11 +34,25 @@ Parameters ---------- - x : 1D array or 2D meshgrid array - y : 1D array or 2D meshgrid array - Arrays defining the coordinates of a 2D grid. - z : 2D array - The values of the interpolated function on the grid points. + x : 1D array + y : 1D array + Arrays defining the coordinates of a 2D grid. If the + points lie on a regular grid, x and y can simply specify + the rows and colums, i.e. + + x = [0,1,2] y = [0,1,2] + + otherwise x and y must specify the full coordinates, i.e. + + x = [0,1,2,0,1.5,2,0,1,2] y = [0,1,2,0,1,2,0,1,2] + + If x and y are 2-dimensional, they are flattened (allowing + the use of meshgrid, for example). + + z : 1D array + The values of the interpolated function on the grid + points. If z is a 2-dimensional array, it is flattened. + kind : 'linear', 'cubic', 'quintic' The kind of interpolation to use. copy : bool @@ -56,27 +70,27 @@ ValueError when inputs are invalid. """ - self.x = atleast_1d(x).copy() - self.y = atleast_1d(y).copy() - if rank(self.x) == 1 and rank(self.y) == 1: - # Prepare meshgrid for bisplrep - x, y = meshgrid(x,y) - if rank(self.x) > 2 or rank(self.y) > 2: - raise ValueError("One of the input arrays is not 1-d or 2-d.") - if rank(self.x) == 2: - self.x = self.x[:,0] - if rank(self.y) == 2: - self.y = self.y[0] - self.z = array(z,copy=True) - if rank(z) != 2: - raise ValueError("Grid values is not a 2-d array.") + self.x, self.y, self.z = map(ravel, map(array, [x, y, z])) + if not (rank(self.x) == 1 and rank(self.y) == 1 and rank(self.z) == 1): + raise ValueError("One of the input arrays is not 1-d.") + if len(self.x) != len(self.y): + raise ValueError("x and y must have equal lengths") + if len(self.z) == len(self.x) * len(self.y): + print "di" + self.x, self.y = meshgrid(x,y) + self.x, self.y = map(ravel, [self.x, self.y]) + print self.x, self.y, self.z + if len(self.z) != len(self.x): + raise ValueError("Invalid length for input z") + try: kx = ky = {'linear' : 1, 'cubic' : 3, 'quintic' : 5}[kind] except KeyError: raise ValueError("Unsupported interpolation type.") - self.tck = fitpack.bisplrep(x, y, z, kx=kx, ky=ky, s=0.) + + self.tck = fitpack.bisplrep(self.x, self.y, self.z, kx=kx, ky=ky, s=0.) def __call__(self,x,y,dx=0,dy=0): """ Interpolate the function. From scipy-svn at scipy.org Sat Oct 7 12:23:00 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 7 Oct 2006 11:23:00 -0500 (CDT) Subject: [Scipy-svn] r2246 - trunk/Lib/interpolate Message-ID: <20061007162300.B5DDC39C278@new.scipy.org> Author: stefan Date: 2006-10-07 11:22:48 -0500 (Sat, 07 Oct 2006) New Revision: 2246 Modified: trunk/Lib/interpolate/interpolate.py Log: Remove debugging prints. Modified: trunk/Lib/interpolate/interpolate.py =================================================================== --- trunk/Lib/interpolate/interpolate.py 2006-10-07 16:21:58 UTC (rev 2245) +++ trunk/Lib/interpolate/interpolate.py 2006-10-07 16:22:48 UTC (rev 2246) @@ -76,10 +76,8 @@ if len(self.x) != len(self.y): raise ValueError("x and y must have equal lengths") if len(self.z) == len(self.x) * len(self.y): - print "di" self.x, self.y = meshgrid(x,y) self.x, self.y = map(ravel, [self.x, self.y]) - print self.x, self.y, self.z if len(self.z) != len(self.x): raise ValueError("Invalid length for input z") From scipy-svn at scipy.org Sat Oct 7 22:12:20 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Sat, 7 Oct 2006 21:12:20 -0500 (CDT) Subject: [Scipy-svn] r2247 - trunk/Lib/interpolate Message-ID: <20061008021220.9EC3E39C039@new.scipy.org> Author: stefan Date: 2006-10-07 21:12:06 -0500 (Sat, 07 Oct 2006) New Revision: 2247 Modified: trunk/Lib/interpolate/interpolate.py Log: More concise rank checking in interp2d. Modified: trunk/Lib/interpolate/interpolate.py =================================================================== --- trunk/Lib/interpolate/interpolate.py 2006-10-07 16:22:48 UTC (rev 2246) +++ trunk/Lib/interpolate/interpolate.py 2006-10-08 02:12:06 UTC (rev 2247) @@ -71,7 +71,7 @@ """ self.x, self.y, self.z = map(ravel, map(array, [x, y, z])) - if not (rank(self.x) == 1 and rank(self.y) == 1 and rank(self.z) == 1): + if not map(rank, [self.x, self.y, self.z]) == [1,1,1]: raise ValueError("One of the input arrays is not 1-d.") if len(self.x) != len(self.y): raise ValueError("x and y must have equal lengths") From scipy-svn at scipy.org Mon Oct 9 09:47:08 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 9 Oct 2006 08:47:08 -0500 (CDT) Subject: [Scipy-svn] r2248 - trunk/Lib/io Message-ID: <20061009134708.72B7E39C016@new.scipy.org> Author: matthew.brett at gmail.com Date: 2006-10-09 08:47:02 -0500 (Mon, 09 Oct 2006) New Revision: 2248 Modified: trunk/Lib/io/mio5.py trunk/Lib/io/miobase.py Log: More progress on implementing Mat5 write Modified: trunk/Lib/io/mio5.py =================================================================== --- trunk/Lib/io/mio5.py 2006-10-08 02:12:06 UTC (rev 2247) +++ trunk/Lib/io/mio5.py 2006-10-09 13:47:02 UTC (rev 2248) @@ -524,8 +524,23 @@ class Mat5MatrixWriter(MatStreamWriter): + mat_tag = zeros((), mdtypes_template['tag_full']) + mat_tag['mdtype'] = miMATRIX + + def __init__(self, file_stream, arr, name, is_global=False): + super(Mat5MatrixWriter, self).__init__(file_stream, arr, name) + self.is_global = is_global + + def write_dtype(self, arr): + self.file_stream.write(arr.tostring) + + def write_element(self, arr): + # check if small element works - do it + # write tag, data + pass + def write_header(self, mclass, - is_global, + is_global=False, is_complex=False, is_logical=False, nzmax=0): @@ -534,21 +549,27 @@ @is_global - True if matrix is global @is_complex - True is matrix is complex @is_logical - True if matrix is logical + nzmax - max non zero elements for sparse arrays ''' - dims = self.arr.shape - header = empty((), mdtypes_template['header']) - M = not ByteOrder.little_endian - O = 0 - header['mopt'] = (M * 1000 + - O * 100 + - P * 10 + - T) - header['mrows'] = dims[0] - header['ncols'] = dims[1] - header['imagf'] = imagf - header['namlen'] = len(self.name) + 1 - self.write_bytes(header) - self.write_string(self.name + '\0') + self._mat_tag_pos = self.file_stream.tell() + self.write_dtype(self.mat_tag) + # write array flags (complex, global, logical, class, nzmax) + af = zeros((), mdtypes_template['array_flags']) + af['data_type'] = miUINT32 + af['byte_count'] = 8 + flags = is_complex << 3 | is_global << 2 | is_logical << 1 + af['flags_class'] = mclass | flags << 8 + af['nzmax'] = nzmax + self.write_dtype(af) + self.write_element(array(self.arr.shape, dtype='i4')) + self.write_element(self.name) + + def update_matrix_tag(self): + curr_pos = self.file_stream.tell() + self.file_stream.seek(self._mat_tag_pos) + self.mat_tag['byte_count'] = curr_pos - self._mat_tag_pos - 8 + self.write_dtype(self.mat_tag) + self.file_stream.seek(curr_pos) def write(self): assert False, 'Not implemented' @@ -559,10 +580,6 @@ def write(self): # identify matlab type for array # make at least 2d - # write miMATRIX tag - # write array flags (complex, global, logical, class, nzmax) - # dimensions - # array name # maybe downcast array to smaller matlab type # write real # write imaginary @@ -611,75 +628,84 @@ T=mxSPARSE_CLASS, dims=ijd.shape) self.write_bytes(ijd) - - -def matrix_writer_factory(stream, arr, name, unicode_strings=False, is_global=False): - ''' Factory function to return matrix writer given variable to write - @stream - file or file-like stream to write to - @arr - array to write - @name - name in matlab (TM) workspace - ''' - if have_sparse: - if scipy.sparse.issparse(arr): - return Mat5SparseWriter(stream, arr, name, is_global) - arr = array(arr) - if arr.dtype.hasobject: - types, arr_type = classify_mobjects(arr) - if arr_type == 'c': - return Mat5CellWriter(stream, arr, name, is_global, types) - elif arr_type == 's': - return Mat5StructWriter(stream, arr, name, is_global) - elif arr_type == 'o': - return Mat5ObjectWriter(stream, arr, name, is_global) - if arr.dtype.kind in ('U', 'S'): - if unicode_strings: - return Mat5UniCharWriter(stream, arr, name, is_global) + + +class Mat5WriterGetter(object): + ''' Wraps stream and options, provides methods for getting Writer objects ''' + def __init__(self, stream, unicode_strings): + self.stream = stream + self.unicode_strings = unicode_strings + + def rewind(self): + self.stream.seek(0) + + def matrix_writer_factory(self, arr, name, is_global=False): + ''' Factory function to return matrix writer given variable to write + @stream - file or file-like stream to write to + @arr - array to write + @name - name in matlab (TM) workspace + ''' + if have_sparse: + if scipy.sparse.issparse(arr): + return Mat5SparseWriter(self.stream, arr, name, is_global) + arr = array(arr) + if arr.dtype.hasobject: + types, arr_type = classify_mobjects(arr) + if arr_type == 'c': + return Mat5CellWriter(self.stream, arr, name, is_global, types) + elif arr_type == 's': + return Mat5StructWriter(self.stream, arr, name, is_global) + elif arr_type == 'o': + return Mat5ObjectWriter(self.stream, arr, name, is_global) + if arr.dtype.kind in ('U', 'S'): + if self.unicode_strings: + return Mat5UniCharWriter(self.stream, arr, name, is_global) + else: + return Mat5IntCharWriter(self.stream, arr, name, is_global) else: - return Mat5IntCharWriter(stream, arr, name, is_global) - else: - return Mat5NumericWriter(stream, arr, name, is_global) + return Mat5NumericWriter(self.stream, arr, name, is_global) -def classify_mobjects(objarr): - ''' Function to classify objects passed for writing - returns - types - S1 array of same shape as objarr with codes for each object - i - invalid object - a - ndarray - s - matlab struct - o - matlab object - arr_type - one of - c - cell array - s - struct array - o - object array - ''' - N = objarr.size - types = empty((N,), dtype='S1') - types[:] = 'i' - type_set = set() - flato = objarr.flat - for i in range(N): - obj = flato[i] - if isinstance(obj, ndarray): - types[i] = 'a' - continue - try: - fns = tuple(obj._fieldnames) - except AttributeError: - continue - try: - cn = obj._classname - except AttributeError: - types[i] = 's' - type_set.add(fns) - continue - types[i] = 'o' - type_set.add((cn, fns)) - arr_type = 'c' - if len(set(types))==1 and len(type_set) == 1: - arr_type = types[0] - return types.reshape(objarr.shape), arr_type - - + def classify_mobjects(self, objarr): + ''' Function to classify objects passed for writing + returns + types - S1 array of same shape as objarr with codes for each object + i - invalid object + a - ndarray + s - matlab struct + o - matlab object + arr_type - one of + c - cell array + s - struct array + o - object array + ''' + N = objarr.size + types = empty((N,), dtype='S1') + types[:] = 'i' + type_set = set() + flato = objarr.flat + for i in range(N): + obj = flato[i] + if isinstance(obj, ndarray): + types[i] = 'a' + continue + try: + fns = tuple(obj._fieldnames) + except AttributeError: + continue + try: + cn = obj._classname + except AttributeError: + types[i] = 's' + type_set.add(fns) + continue + types[i] = 'o' + type_set.add((cn, fns)) + arr_type = 'c' + if len(set(types))==1 and len(type_set) == 1: + arr_type = types[0] + return types.reshape(objarr.shape), arr_type + + class MatFile5Writer(MatFileWriter): ''' Class for writing mat5 files ''' def __init__(self, file_stream, @@ -688,22 +714,32 @@ global_vars=None): super(MatFile5Writer, self).__init__(file_stream) self.do_compression = do_compression - self.unicode_strings = unicode_strings if global_vars: self.global_vars = global_vars else: self.global_vars = [] + self.writer_getter = Mat5WriterGetter( + StringIO, + unicode_strings) + + def get_unicode_strings(self): + return self.write_getter.unicode_strings + def set_unicode_strings(self, unicode_strings): + self.writer_getter.unicode_strings = unicode_strings + unicode_strings = property(get_unicode_strings, + set_unicode_strings, + None, + 'get/set unicode strings property') def put_variables(self, mdict): for name, var in mdict.items(): is_global = name in self.global_vars - stream = StringIO() - matrix_writer_factory(stream, - var, - name, - is_global, - self.unicode_strings, - ).write() + self.writer_getter.rewind() + self.writer_getter.matrix_writer_factory( + var, + name, + is_global, + ).write() if self.do_compression: str = zlib.compress(stream.getvalue()) tag = empty((), mdtypes_template['tag_full']) Modified: trunk/Lib/io/miobase.py =================================================================== --- trunk/Lib/io/miobase.py 2006-10-08 02:12:06 UTC (rev 2247) +++ trunk/Lib/io/miobase.py 2006-10-09 13:47:02 UTC (rev 2248) @@ -391,7 +391,27 @@ self.dt_dict = dt_dict self.rtol = rtol self.atol = atol - + + def eps(self, dt): + ''' Calculate machine precision for datatype + + Machine precision defined as difference between X and smallest + encodable number greater than X, where X is usually 1. + + Input can be datatype, in which case X=1, or X. + ''' + try: + dt = dtype(dt) + start = array(1, dt) + except TypeError: + start = array(dt) + dt = start.dtype + two = array(2, dt) + e = start.copy() + while (e / two + start) > start: + e = e / two + return e + def default_dt_dict(self): d_dict = {} for sc_type in ('complex','float'): @@ -474,10 +494,9 @@ def downcast_complex(self, arr): # can we downcast to float? - flts = self.storage_criterion(arr.dtype.itemsize / 2, - ('f'), - lambda x, y: x <=y)[0] - test_arr = arr.astype(flt) + fts = self.dt_arrs['float'] + flts = flts[flts['storage'] <= arr.dtype.itemsize] + test_arr = arr.astype(flt[0]['type']) if allclose(arr, test_arr, self.rtol, self.atol): return self.downcast_float(test_arr) # try downcasting to another complex type From scipy-svn at scipy.org Mon Oct 9 18:38:01 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 9 Oct 2006 17:38:01 -0500 (CDT) Subject: [Scipy-svn] r2249 - trunk/Lib/interpolate Message-ID: <20061009223801.D051B39C016@new.scipy.org> Author: stefan Date: 2006-10-09 17:37:42 -0500 (Mon, 09 Oct 2006) New Revision: 2249 Modified: trunk/Lib/interpolate/fitpack.pyf trunk/Lib/interpolate/fitpack2.py Log: Provide 'splint' with a 'c'-parameter of correct length. Modified: trunk/Lib/interpolate/fitpack.pyf =================================================================== --- trunk/Lib/interpolate/fitpack.pyf 2006-10-09 13:47:02 UTC (rev 2248) +++ trunk/Lib/interpolate/fitpack.pyf 2006-10-09 22:37:42 UTC (rev 2249) @@ -73,7 +73,7 @@ ! y = splev(t,c,k,x) real*8 dimension(n),intent(in) :: t integer intent(hide),depend(t) :: n=len(t) - real*8 dimension(n-k-1),depend(n,k),check(len(c)==n-k-1),intent(in) :: c + real*8 dimension(n),depend(n,k),check(len(c)==n),intent(in) :: c integer :: k real*8 dimension(m),intent(in) :: x real*8 dimension(m),depend(m),intent(out) :: y @@ -85,7 +85,7 @@ ! dy = splder(t,c,k,x,[nu]) real*8 dimension(n) :: t integer depend(t),intent(hide) :: n=len(t) - real*8 dimension(n-k-1),depend(n,k),check(len(c)==n-k-1),intent(in) :: c + real*8 dimension(n),depend(n,k),check(len(c)==n),intent(in) :: c integer :: k integer depend(k),check(0<=nu && nu<=k) :: nu = 1 real*8 dimension(m) :: x Modified: trunk/Lib/interpolate/fitpack2.py =================================================================== --- trunk/Lib/interpolate/fitpack2.py 2006-10-09 13:47:02 UTC (rev 2248) +++ trunk/Lib/interpolate/fitpack2.py 2006-10-09 22:37:42 UTC (rev 2249) @@ -93,7 +93,7 @@ def _reset_class(self): data = self._data n,t,c,k,ier = data[7],data[8],data[9],data[5],data[-1] - self._eval_args = t[:n],c[:n-k-1],k + self._eval_args = t[:n],c[:n],k if ier==0: # the spline returned has a residual sum of squares fp # such that abs(fp-s)/s <= tol with tol a relative From scipy-svn at scipy.org Mon Oct 9 20:30:21 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Mon, 9 Oct 2006 19:30:21 -0500 (CDT) Subject: [Scipy-svn] r2250 - trunk/Lib/stats Message-ID: <20061010003021.CDAB039C0B8@new.scipy.org> Author: oliphant Date: 2006-10-09 19:30:16 -0500 (Mon, 09 Oct 2006) New Revision: 2250 Modified: trunk/Lib/stats/continuous.lyx trunk/Lib/stats/morestats.py Log: Fix bayes_mvs a tad. Modified: trunk/Lib/stats/continuous.lyx =================================================================== --- trunk/Lib/stats/continuous.lyx 2006-10-09 22:37:42 UTC (rev 2249) +++ trunk/Lib/stats/continuous.lyx 2006-10-10 00:30:16 UTC (rev 2250) @@ -690,6 +690,39 @@ \layout Subsection +Median and mode +\layout Standard + +The mean, +\begin_inset Formula $m_{n}$ +\end_inset + + is defined as the point at which half of the density is on one side and + half on the other. + In other words, +\begin_inset Formula $F\left(m_{n}\right)=\frac{1}{2}$ +\end_inset + + so that +\begin_inset Formula \[ +m_{n}=G\left(\frac{1}{2}\right).\] + +\end_inset + + In addition, the mode, +\begin_inset Formula $m_{d}$ +\end_inset + +, is defined as the value for which the probability density function reaches + it's peak +\begin_inset Formula \[ +m_{d}=\arg\max_{x}f\left(x\right).\] + +\end_inset + + +\layout Subsection + Fitting data \layout Standard @@ -2296,7 +2329,8 @@ \mu & = & \frac{\Gamma\left(a+\frac{1}{c}\right)}{\Gamma\left(a\right)}\\ \mu_{2} & = & \frac{\Gamma\left(a+\frac{2}{c}\right)}{\Gamma\left(a\right)}-\mu^{2}\\ \gamma_{1} & = & \frac{\Gamma\left(a+\frac{3}{c}\right)/\Gamma\left(a\right)-3\mu\mu_{2}-\mu^{3}}{\mu_{2}^{3/2}}\\ -\gamma_{2} & = & \frac{\Gamma\left(a+\frac{4}{c}\right)/\Gamma\left(a\right)-4\mu\mu_{3}-6\mu^{2}\mu_{2}-\mu^{4}}{\mu_{2}^{2}}-3\end{eqnarray*} +\gamma_{2} & = & \frac{\Gamma\left(a+\frac{4}{c}\right)/\Gamma\left(a\right)-4\mu\mu_{3}-6\mu^{2}\mu_{2}-\mu^{4}}{\mu_{2}^{2}}-3\\ +m_{d} & = & \left(\frac{ac-1}{c}\right)^{1/c}.\end{eqnarray*} \end_inset @@ -2905,6 +2939,12 @@ \end_inset +\begin_inset Formula \[ +m_{d}=\frac{1}{a+1}\] + +\end_inset + + \layout Standard Modified: trunk/Lib/stats/morestats.py =================================================================== --- trunk/Lib/stats/morestats.py 2006-10-09 22:37:42 UTC (rev 2249) +++ trunk/Lib/stats/morestats.py 2006-10-10 00:30:16 UTC (rev 2250) @@ -16,6 +16,7 @@ import numpy import types import scipy.optimize as optimize +import scipy.special as special import futil import numpy as sb @@ -50,17 +51,18 @@ Assumes 1-d data all has same mean and variance and uses Jeffrey's prior for variance and std. - alpha gives the probability that the returned interval contains the true parameter. Uses peak of conditional pdf as starting center. Returns (peak, (a, b)) for each of mean, variance and standard deviation. + Requires 2 or more data-points. """ x = ravel(data) n = len(x) assert(n > 1) + assert(alpha < 1 and alpha > 0) n = float(n) xbar = sb.add.reduce(x)/n C = sb.add.reduce(x*x)/n - xbar*xbar @@ -73,29 +75,35 @@ mp = xbar # fac = n*C/2.0 - peak = 2/(n+1.) - a = (n-1)/2.0 - F_peak = distributions.invgamma.cdf(peak,a) + a = (n-1)/2.0 + if (n > 3): # use mean of distribution as center + peak = 2/(n-3.0) + F_peak = distributions.invgamma.cdf(peak,a) + else: # use median + F_peak = -1.0 + if (F_peak < alpha/2.0): + peak = distributions.invgamma.ppf(0.5,a) + F_peak = 0.5 q1 = F_peak - alpha/2.0 q2 = F_peak + alpha/2.0 - if (q1 < 0): # non-symmetric area - q2 = alpha - va = 0.0 - else: - va = fac*distributions.invgamma.ppf(q1,a) + if (q2 > 1): q2 = 1.0 + va = fac*distributions.invgamma.ppf(q1,a) vb = fac*distributions.invgamma.ppf(q2,a) vp = peak*fac # fac = sqrt(fac) - peak = sqrt(2./n) - F_peak = distributions.gengamma.cdf(peak,a,-2) + if (n > 2): + peak = special.gamma(a-0.5) / special.gamma(a) + F_peak = distributions.gengamma.cdf(peak,a,-2) + else: # use median + F_peak = -1.0 + if (F_peak < alpha/2.0): + peak = distributions.gengamma.ppf(0.5,a,-2) + F_peak = 0.5 q1 = F_peak - alpha/2.0 q2 = F_peak + alpha/2.0 - if (q1 < 0): - q2 = alpha - sta = 0.0 - else: - sta = fac*distributions.gengamma.ppf(q1,a,-2) + if (q2 > 1): q2 = 1.0 + sta = fac*distributions.gengamma.ppf(q1,a,-2) stb = fac*distributions.gengamma.ppf(q2,a,-2) stp = peak*fac From scipy-svn at scipy.org Tue Oct 10 01:53:47 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 10 Oct 2006 00:53:47 -0500 (CDT) Subject: [Scipy-svn] r2251 - in trunk/Lib: sandbox/stats stats Message-ID: <20061010055347.0D12339C02F@new.scipy.org> Author: oliphant Date: 2006-10-10 00:53:33 -0500 (Tue, 10 Oct 2006) New Revision: 2251 Modified: trunk/Lib/sandbox/stats/anova.py trunk/Lib/stats/distributions.py trunk/Lib/stats/morestats.py Log: Fix sandbox/anova a bit. Fix distributions gengamma and invgamma to use gammaln and exp to avoid precision loss. Clean up bayes_mvs which returns mean, variance, and standard deviation with confidence intervals using Jeffrey's priors Modified: trunk/Lib/sandbox/stats/anova.py =================================================================== --- trunk/Lib/sandbox/stats/anova.py 2006-10-10 00:30:16 UTC (rev 2250) +++ trunk/Lib/sandbox/stats/anova.py 2006-10-10 05:53:33 UTC (rev 2251) @@ -2,6 +2,8 @@ # this module. No care has been taken to ensure that it works in its current # state. At minimum, you will have to figure out what it needs to import to work. +from numpy import sum + def anova(data, effects=['A','B','C','D','E','F','G','H','I','J','K']): """ Prints the results of single-variable between- and within-subject ANOVA @@ -355,8 +357,8 @@ sourceNarray = apply_over_axes(hmean, Narray,btwnonsourcedims) ## Calc grand average (ga,axis=0), used for ALL effects - ga = sum((sourceMarray*sourceNarray)/ - sum(sourceNarray,axis=0),axis=0) + ga = sum((sourceMarray*sourceNarray)/ \ + sum(sourceNarray,axis=0),axis=0) ga = reshape(ga,ones(len(Marray.shape))) ## If GRAND interaction, use harmonic mean of ALL cell Ns Modified: trunk/Lib/stats/distributions.py =================================================================== --- trunk/Lib/stats/distributions.py 2006-10-10 00:30:16 UTC (rev 2250) +++ trunk/Lib/stats/distributions.py 2006-10-10 05:53:33 UTC (rev 2251) @@ -18,6 +18,7 @@ import numpy import numpy.random as mtrand from numpy import flatnonzero as nonzero +from scipy.special import gammaln as gamln __all__ = [ 'rv_continuous', @@ -1707,7 +1708,7 @@ def _argcheck(self, a, c): return (a > 0) & (c != 0) def _pdf(self, x, a, c): - return abs(c)* x**(c*a-1) / special.gamma(a) * exp(-x**c) + return abs(c)* exp((c*a-1)*log(x)-x**c- special.gammaln(a)) def _cdf(self, x, a, c): val = special.gammainc(a,x**c) cond = c + 0*val @@ -1973,13 +1974,13 @@ class invgamma_gen(rv_continuous): def _pdf(self, x, a): - return x**(-a-1) / special.gamma(a) * exp(-1.0/x) + return exp(-(a+1)*log(x)-special.gammaln(a) - 1.0/x) def _cdf(self, x, a): return 1.0-special.gammainc(a, 1.0/x) def _ppf(self, q, a): return 1.0/special.gammaincinv(a,1-q) def _munp(self, n, a): - return special.gamma(a-n) / special.gamma(a) + return exp(special.gammaln(a-n) - special.gammaln(a)) def _entropy(self, a): return a - (a+1.0)*special.psi(a) + special.gammaln(a) invgamma = invgamma_gen(a=0.0, name='invgamma',longname="An inverted gamma", @@ -2232,7 +2233,7 @@ # class loggamma_gen(rv_continuous): def _pdf(self, x, c): - return exp(c*x-exp(x))/ special.gamma(c) + return exp(c*x-exp(x)-special.gammaln(c)) def _cdf(self, x, c): return special.gammainc(c, exp(x))/ special.gamma(c) def _ppf(self, q, c): @@ -2446,20 +2447,21 @@ return mtrand.noncentral_f(dfn,dfd,nc,self._size) def _pdf(self, x, dfn, dfd, nc): n1,n2 = dfn, dfd - Px = exp(-nc/2+nc*n1*x/(2*(n2+n1*x))) + term = -nc/2+nc*n1*x/(2*(n2+n1*x)) + gamln(n1/2.)+gamln(1+n2/2.) + term -= gamln((n1+n2)/2.0) + Px = exp(term) Px *= n1**(n1/2) * n2**(n2/2) * x**(n1/2-1) Px *= (n2+n1*x)**(-(n1+n2)/2) - Px *= special.gamma(n1/2)*special.gamma(1+n2/2) Px *= special.assoc_laguerre(-nc*n1*x/(2.0*(n2+n1*x)),n2/2,n1/2-1) - Px /= special.beta(n1/2,n2/2)*special.gamma((n1+n2)/2.0) + Px /= special.beta(n1/2,n2/2) def _cdf(self, x, dfn, dfd, nc): return special.ncfdtr(dfn,dfd,nc,x) def _ppf(self, q, dfn, dfd, nc): return special.ncfdtri(dfn, dfd, nc, q) def _munp(self, n, dfn, dfd, nc): val = (dfn *1.0/dfd)**n - val *= gam(n+0.5*dfn)*gam(0.5*dfd-n) / gam(dfd*0.5) - val *= exp(-nc / 2.0) + term = gamln(n+0.5*dfn) + gamln(0.5*dfd-n) - gamln(dfd*0.5) + val *= exp(-nc / 2.0+term) val *= special.hyp1f1(n+0.5*dfn, 0.5*dfn, 0.5*nc) return val def _stats(self, dfn, dfd, nc): @@ -2528,8 +2530,9 @@ x2 = x*x ncx2 = nc*nc*x2 fac1 = n + x2 - Px = n**(n/2) * special.gamma(n+1) - Px /= arr(2.0**n*exp(nc*nc/2)*fac1**(n/2)*special.gamma(n/2)) + trm1 = n/2.*log(n) + gamln(n+1) + trm1 -= n*log(2)+nc*nc/2.+(n/2.)*log(fac1)+gamln(n/2.) + Px = exp(trm1) valF = ncx2 / (2*fac1) trm1 = sqrt(2)*nc*x*special.hyp1f1(n/2+1,1.5,valF) trm1 /= arr(fac1*special.gamma((n+1)/2)) Modified: trunk/Lib/stats/morestats.py =================================================================== --- trunk/Lib/stats/morestats.py 2006-10-10 00:30:16 UTC (rev 2250) +++ trunk/Lib/stats/morestats.py 2006-10-10 05:53:33 UTC (rev 2251) @@ -40,6 +40,32 @@ ### Bayesian confidence intervals for mean, variance, std ########################################################## +# assume distributions are gaussian with given means and variances. +def _gauss_mvs(x, n, alpha): + xbar = x.mean() + C = x.var() + val = distributions.norm.ppf((1+alpha)/2.0) + # mean is a Gaussian with mean xbar and variance C/n + mp = xbar + fac0 = sqrt(C/n) + term = fac0*val + ma = mp - term + mb = mp + term + # var is a Gaussian with mean C and variance 2*C*C/n + vp = C + fac1 = sqrt(2.0/n)*C + term = fac1*val + va = vp - term + vb = vp + term + # std is a Gaussian with mean sqrt(C) and variance C/(2*n) + st = sqrt(C) + fac2 = sqrt(0.5)*fac0 + term = fac2*val + sta = st - term + stb = st + term + return mp, (ma, mb), vp, (va, vb), st, (sta, stb) + + ## Assumes all is known is that mean, and std (variance,axis=0) exist ## and are the same for all the data. Uses Jeffrey's prior ## @@ -51,12 +77,14 @@ Assumes 1-d data all has same mean and variance and uses Jeffrey's prior for variance and std. - alpha gives the probability that the returned interval contains + + alpha gives the probability that the returned confidence interval contains the true parameter. - Uses peak of conditional pdf as starting center. + Uses mean of conditional pdf as center estimate + (but centers confidence interval on the median) - Returns (peak, (a, b)) for each of mean, variance and standard deviation. + Returns (center, (a, b)) for each of mean, variance and standard deviation. Requires 2 or more data-points. """ x = ravel(data) @@ -64,48 +92,43 @@ assert(n > 1) assert(alpha < 1 and alpha > 0) n = float(n) - xbar = sb.add.reduce(x)/n - C = sb.add.reduce(x*x)/n - xbar*xbar - # + if (n > 1000): # just a guess. The curves look similar at this point. + return _gauss_mvs(x, n, alpha) + xbar = x.mean() + C = x.var() + # mean fac = sqrt(C/(n-1)) tval = distributions.t.ppf((1+alpha)/2.0,n-1) delta = fac*tval ma = xbar - delta mb = xbar + delta mp = xbar - # + # var fac = n*C/2.0 - a = (n-1)/2.0 - if (n > 3): # use mean of distribution as center - peak = 2/(n-3.0) - F_peak = distributions.invgamma.cdf(peak,a) - else: # use median - F_peak = -1.0 - if (F_peak < alpha/2.0): + a = (n-1)/2 + if (n < 4): peak = distributions.invgamma.ppf(0.5,a) - F_peak = 0.5 - q1 = F_peak - alpha/2.0 - q2 = F_peak + alpha/2.0 - if (q2 > 1): q2 = 1.0 + else: + peak = 2.0/(n-3.0) + q1 = (1-alpha)/2.0 + q2 = (1+alpha)/2.0 va = fac*distributions.invgamma.ppf(q1,a) vb = fac*distributions.invgamma.ppf(q2,a) vp = peak*fac - # + # std fac = sqrt(fac) - if (n > 2): - peak = special.gamma(a-0.5) / special.gamma(a) - F_peak = distributions.gengamma.cdf(peak,a,-2) - else: # use median - F_peak = -1.0 - if (F_peak < alpha/2.0): + if (n < 3): peak = distributions.gengamma.ppf(0.5,a,-2) - F_peak = 0.5 - q1 = F_peak - alpha/2.0 - q2 = F_peak + alpha/2.0 - if (q2 > 1): q2 = 1.0 + stp = fac*peak + else: + ndiv2 = (n-1)/2.0 + term = special.gammaln(ndiv2-0.5)-special.gammaln(ndiv2) + term += (log(n)+log(C)-log(2.0))*0.5 + stp = exp(term) + q1 = (1-alpha)/2.0 + q2 = (1+alpha)/2.0 sta = fac*distributions.gengamma.ppf(q1,a,-2) stb = fac*distributions.gengamma.ppf(q2,a,-2) - stp = peak*fac return (mp,(ma,mb)),(vp,(va,vb)),(stp,(sta,stb)) From scipy-svn at scipy.org Tue Oct 10 13:37:21 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Tue, 10 Oct 2006 12:37:21 -0500 (CDT) Subject: [Scipy-svn] r2252 - trunk/Lib/interpolate Message-ID: <20061010173721.AB7C439C175@new.scipy.org> Author: stefan Date: 2006-10-10 12:36:34 -0500 (Tue, 10 Oct 2006) New Revision: 2252 Modified: trunk/Lib/interpolate/fitpack.py Log: Let splrep return tck as a tuple. Modified: trunk/Lib/interpolate/fitpack.py =================================================================== --- trunk/Lib/interpolate/fitpack.py 2006-10-10 05:53:33 UTC (rev 2251) +++ trunk/Lib/interpolate/fitpack.py 2006-10-10 17:36:34 UTC (rev 2252) @@ -376,7 +376,7 @@ n,c,fp,ier = dfitpack.curfit(task, x, y, w, t, wrk, iwrk, xb, xe, k, s) else: n,c,fp,ier = dfitpack.percur(task, x, y, w, t, wrk, iwrk, k, s) - tck = [t[:n],c[:n-k-1],k] + tck = (t[:n],c[:n-k-1],k) if ier<=0 and not quiet: print _iermess[ier][0] print "\tk=%d n=%d m=%d fp=%f s=%f"%(k,len(t),m,fp,s) From scipy-svn at scipy.org Wed Oct 11 16:44:57 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 11 Oct 2006 15:44:57 -0500 (CDT) Subject: [Scipy-svn] r2253 - trunk/Lib Message-ID: <20061011204457.8AD6039C00A@new.scipy.org> Author: oliphant Date: 2006-10-11 15:44:55 -0500 (Wed, 11 Oct 2006) New Revision: 2253 Modified: trunk/Lib/__init__.py Log: Add back scimath to scipy. Modified: trunk/Lib/__init__.py =================================================================== --- trunk/Lib/__init__.py 2006-10-10 17:36:34 UTC (rev 2252) +++ trunk/Lib/__init__.py 2006-10-11 20:44:55 UTC (rev 2253) @@ -32,6 +32,7 @@ from numpy import oldnumeric from numpy import * from numpy.random import rand, randn +from numpy.lib.scimath import * __all__ += ['oldnumeric']+_num.__all__ From scipy-svn at scipy.org Wed Oct 11 19:29:20 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Wed, 11 Oct 2006 18:29:20 -0500 (CDT) Subject: [Scipy-svn] r2254 - in trunk/Lib/interpolate: . tests Message-ID: <20061011232920.639F839C00A@new.scipy.org> Author: stefan Date: 2006-10-11 18:28:59 -0500 (Wed, 11 Oct 2006) New Revision: 2254 Modified: trunk/Lib/interpolate/fitpack.pyf trunk/Lib/interpolate/fitpack2.py trunk/Lib/interpolate/tests/test_fitpack.py Log: Add RectBivariateSpline [for John Travers]. Fix long lines. Modified: trunk/Lib/interpolate/fitpack.pyf =================================================================== --- trunk/Lib/interpolate/fitpack.pyf 2006-10-11 20:44:55 UTC (rev 2253) +++ trunk/Lib/interpolate/fitpack.pyf 2006-10-11 23:28:59 UTC (rev 2254) @@ -63,6 +63,12 @@ int b2 = (bx<=by?bx+v-ky:by+u-kx); return u*v*(b2+1)+b2; } + +static int calc_regrid_lwrk(int mx, int my, int kx, int ky, + int nxest, int nyest) { + int u = MAX(my, nxest); + return 4+nxest*(my+2*kx+5)+nyest*(2*ky+5)+mx*(kx+1)+my*(ky+1)+u; +} ''' interface @@ -411,7 +417,46 @@ :: kwrk=m+(nx-2*kx-1)*(ny-2*ky-1) integer intent(out) :: ier end subroutine surfit_lsq + + subroutine regrid_smth(iopt,mx,x,my,y,z,xb,xe,yb,ye,kx,ky,s,& + nxest,nyest,nx,tx,ny,ty,c,fp,wrk,lwrk,iwrk,kwrk,ier) + ! nx,tx,ny,ty,c,fp,ier = regrid_smth(x,y,z,[xb,xe,yb,ye,kx,ky,s]) + fortranname regrid + + integer intent(hide) :: iopt=0 + integer intent(hide),depend(x,kx),check(mx>kx) :: mx=len(x) + real*8 dimension(mx) :: x + integer intent(hide),depend(y,ky),check(my>ky) :: my=len(y) + real*8 dimension(my) :: y + real*8 dimension(mx*my),depend(mx,my),check(len(z)==mx*my) :: z + real*8 optional,depend(x,mx) :: xb=dmin(x,mx) + real*8 optional,depend(x,mx) :: xe=dmax(x,mx) + real*8 optional,depend(y,my) :: yb=dmin(y,my) + real*8 optional,depend(y,my) :: ye=dmax(y,my) + integer optional,check(1<=kx && kx<=5) :: kx = 3 + integer optional,check(1<=ky && ky<=5) :: ky = 3 + real*8 optional,check(0.0<=s) :: s = 0.0 + integer intent(hide),depend(kx,mx),check(nxest>=2*(kx+1)) & + :: nxest = mx+kx+1 + integer intent(hide),depend(ky,my),check(nyest>=2*(ky+1)) & + :: nyest = my+ky+1 + integer intent(out) :: nx + real*8 dimension(nxest),intent(out),depend(nxest) :: tx + integer intent(out) :: ny + real*8 dimension(nyest),intent(out),depend(nyest) :: ty + real*8 dimension((nxest-kx-1)*(nyest-ky-1)), & + depend(kx,ky,nxest,nyest),intent(out) :: c + real*8 intent(out) :: fp + real*8 dimension(lwrk),intent(cache,hide),depend(lwrk) :: wrk + integer intent(hide),depend(mx,my,kx,ky,nxest,nyest) & + :: lwrk=calc_regrid_lwrk(mx,my,kx,ky,nxest,nyest) + integer dimension(kwrk),depend(kwrk),intent(cache,hide) :: iwrk + integer intent(hide),depend(mx,my,nxest,nyest) & + :: kwrk=3+mx+my+nxest+nyest + integer intent(out) :: ier + end subroutine regrid_smth + end interface end python module dfitpack Modified: trunk/Lib/interpolate/fitpack2.py =================================================================== --- trunk/Lib/interpolate/fitpack2.py 2006-10-11 20:44:55 UTC (rev 2253) +++ trunk/Lib/interpolate/fitpack2.py 2006-10-11 23:28:59 UTC (rev 2254) @@ -13,10 +13,11 @@ 'LSQUnivariateSpline', 'LSQBivariateSpline', - 'SmoothBivariateSpline'] + 'SmoothBivariateSpline', + 'RectBivariateSpline'] import warnings -from numpy import zeros, concatenate, alltrue +from numpy import zeros, concatenate, alltrue, ravel, all, diff import dfitpack @@ -134,6 +135,7 @@ def set_smoothing_factor(self, s): """ Continue spline computation with the given smoothing factor s and with the knots found at the last call. + """ data = self._data if data[6]==-1: @@ -150,8 +152,9 @@ def __call__(self, x, nu=None): """ Evaluate spline (or its nu-th derivative) at positions x. - Note: x can be unordered but the evaluation is - more efficient if x is (partially) ordered. + Note: x can be unordered but the evaluation is more efficient + if x is (partially) ordered. + """ if nu is None: return dfitpack.splev(*(self._eval_args+(x,))) @@ -166,14 +169,15 @@ return data[8][k:n-k] def get_coeffs(self): - """ Return spline coefficients.""" + """Return spline coefficients.""" data = self._data k,n = data[5],data[7] return data[9][:n-k-1] def get_residual(self): - """ Return weighted sum of squared residuals of the spline + """Return weighted sum of squared residuals of the spline approximation: sum ((w[i]*(y[i]-s(x[i])))**2,axis=0) + """ return self._data[10] @@ -199,11 +203,15 @@ z,m,ier = dfitpack.sproot(*self._eval_args[:2]) assert ier==0,`ier` return z[:m] - raise NotImplementedError,'finding roots unsupported for non-cubic splines' + raise NotImplementedError,\ + 'finding roots unsupported for non-cubic splines' class InterpolatedUnivariateSpline(UnivariateSpline): - """ Interpolated univariate spline approximation. Identical to UnivariateSpline with less error checking.""" + """ Interpolated univariate spline approximation. Identical to + UnivariateSpline with less error checking. + """ + def __init__(self, x, y, w=None, bbox = [None]*2, k=3): """ Input: @@ -223,8 +231,12 @@ self._reset_class() class LSQUnivariateSpline(UnivariateSpline): - """ Weighted least-squares univariate spline approximation. Appears to be identical to UnivariateSpline with more error checking.""" + """ Weighted least-squares univariate spline + approximation. Appears to be identical to UnivariateSpline with + more error checking. + """ + def __init__(self, x, y, t, w=None, bbox = [None]*2, k=3): """ Input: @@ -361,7 +373,8 @@ w - positive 1-d sequence of weights bbox - 4-sequence specifying the boundary of the rectangular approximation domain. - By default, bbox=[min(x,tx),max(x,tx),min(y,ty),max(y,ty)] + By default, bbox=[min(x,tx),max(x,tx), + min(y,ty),max(y,ty)] kx,ky=3,3 - degrees of the bivariate spline. s - positive smoothing factor defined for estimation condition: @@ -374,9 +387,10 @@ equations. 0 < eps < 1, default is 1e-16. """ xb,xe,yb,ye = bbox - nx,tx,ny,ty,c,fp,wrk1,ier = dfitpack.surfit_smth(x,y,z,w,\ - xb,xe,yb,ye,\ - kx,ky,s=s,eps=eps,lwrk2=1) + nx,tx,ny,ty,c,fp,wrk1,ier = dfitpack.surfit_smth(x,y,z,w, + xb,xe,yb,ye, + kx,ky,s=s, + eps=eps,lwrk2=1) if ier in [0,-1,-2]: # normal return pass else: @@ -410,7 +424,8 @@ w - positive 1-d sequence of weights bbox - 4-sequence specifying the boundary of the rectangular approximation domain. - By default, bbox=[min(x,tx),max(x,tx),min(y,ty),max(y,ty)] + By default, bbox=[min(x,tx),max(x,tx), + min(y,ty),max(y,ty)] kx,ky=3,3 - degrees of the bivariate spline. eps - a threshold for determining the effective rank of an over-determined linear system of @@ -443,3 +458,63 @@ self.fp = fp self.tck = tx1,ty1,c self.degrees = kx,ky + +class RectBivariateSpline(BivariateSpline): + """ Bivariate spline approximation over a rectangular mesh. + + Can be used for both smoothing or interpolating data. + + See also: + + SmoothBivariateSpline - a smoothing bivariate spline for scattered data + bisplrep, bisplev - an older wrapping of FITPACK + UnivariateSpline - a similar class for univariate spline interpolation + """ + + def __init__(self, x, y, z, + bbox = [None]*4, kx=3, ky=3, s=0): + """ + Input: + x,y - 1-d sequences of coordinates in strictly ascending order + z - 2-d array of data with shape (x.size,y.size) + Optional input: + bbox - 4-sequence specifying the boundary of + the rectangular approximation domain. + By default, bbox=[min(x,tx),max(x,tx), + min(y,ty),max(y,ty)] + kx,ky=3,3 - degrees of the bivariate spline. + s - positive smoothing factor defined for + estimation condition: + sum((w[i]*(z[i]-s(x[i],y[i])))**2,axis=0) <= s + Default s=0 which is for interpolation + """ + x,y = ravel(x),ravel(y) + if not all(diff(x) > 0.0): + raise TypeError,'x must be strictly increasing' + if not all(diff(y) > 0.0): + raise TypeError,'y must be strictly increasing' + if not ((x.min() == x[0]) and (x.max() == x[-1])): + raise TypeError, 'x must be strictly ascending' + if not ((y.min() == y[0]) and (y.max() == y[-1])): + raise TypeError, 'y must be strictly ascending' + if not x.size == z.shape[0]: + raise TypeError,\ + 'x dimension of z must have same number of elements as x' + if not y.size == z.shape[1]: + raise TypeError,\ + 'y dimension of z must have same number of elements as y' + z = ravel(z) + xb,xe,yb,ye = bbox + nx,tx,ny,ty,c,fp,ier = dfitpack.regrid_smth(x,y,z, + xb,xe,yb,ye, + kx,ky,s) + if ier in [0,-1,-2]: # normal return + pass + else: + message = _surfit_messages.get(ier,'ier=%s' % (ier)) + warnings.warn(message) + + self.fp = fp + self.tck = tx[:nx],ty[:ny],c[:(nx-kx-1)*(ny-ky-1)] + self.degrees = kx,ky + Modified: trunk/Lib/interpolate/tests/test_fitpack.py =================================================================== --- trunk/Lib/interpolate/tests/test_fitpack.py 2006-10-11 20:44:55 UTC (rev 2253) +++ trunk/Lib/interpolate/tests/test_fitpack.py 2006-10-11 23:28:59 UTC (rev 2254) @@ -14,10 +14,12 @@ import sys from numpy.testing import * +from numpy import array set_package_path() from interpolate.fitpack2 import UnivariateSpline,LSQUnivariateSpline,\ InterpolatedUnivariateSpline -from interpolate.fitpack2 import LSQBivariateSpline, SmoothBivariateSpline +from interpolate.fitpack2 import LSQBivariateSpline, SmoothBivariateSpline,\ + RectBivariateSpline restore_path() class test_UnivariateSpline(ScipyTestCase): @@ -73,5 +75,13 @@ assert_almost_equal(lut.get_residual(),0.0) assert_array_almost_equal(lut([1,1.5,2],[1,1.5]),[[0,0],[1,1],[2,2]]) +class test_RectBivariateSpline(ScipyTestCase): + def check_defaults(self): + x = array([1,2,3,4,5]) + y = array([1,2,3,4,5]) + z = array([[1,2,1,2,1],[1,2,1,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,2,1,2,1]]) + lut = RectBivariateSpline(x,y,z) + assert_array_almost_equal(lut(x,y),z) + if __name__ == "__main__": ScipyTest().run() From scipy-svn at scipy.org Thu Oct 12 02:48:49 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 01:48:49 -0500 (CDT) Subject: [Scipy-svn] r2255 - trunk/Lib/sandbox/models/tests Message-ID: <20061012064849.0BDAA39C01C@new.scipy.org> Author: timl Date: 2006-10-12 01:48:34 -0500 (Thu, 12 Oct 2006) New Revision: 2255 Modified: trunk/Lib/sandbox/models/tests/test_formula.py trunk/Lib/sandbox/models/tests/test_glm.py trunk/Lib/sandbox/models/tests/test_regression.py trunk/Lib/sandbox/models/tests/test_utils.py Log: update unit tests in sandbox.models to work with the scipy testing framework Modified: trunk/Lib/sandbox/models/tests/test_formula.py =================================================================== --- trunk/Lib/sandbox/models/tests/test_formula.py 2006-10-11 23:28:59 UTC (rev 2254) +++ trunk/Lib/sandbox/models/tests/test_formula.py 2006-10-12 06:48:34 UTC (rev 2255) @@ -3,10 +3,11 @@ import numpy.random as R import numpy.linalg as L import scipy, string +from numpy.testing import * -from models import utils, formula, contrast +from scipy.sandbox.models import utils, formula, contrast -class TermTest(unittest.TestCase): +class test_Term(ScipyTestCase): def test_init(self): t1 = formula.Term("trivial") @@ -41,7 +42,7 @@ f = intercept * t1 self.assertEqual(str(f), str(formula.Formula(t1))) -class FormulaTest(unittest.TestCase): +class test_Formula(ScipyTestCase): def setUp(self): self.X = R.standard_normal((40,10)) @@ -72,7 +73,7 @@ self.formula += prod x = self.formula.design(namespace=self.namespace) col = self.formula.termcolumns(prod, dict=False) - scipy.testing.assert_almost_equal(N.squeeze(x[:,col]), self.X[:,0] * self.X[:,2]) + assert_almost_equal(N.squeeze(x[:,col]), self.X[:,0] * self.X[:,2]) def test_contrast1(self): term = self.terms[0] + self.terms[2] @@ -81,7 +82,7 @@ col1 = self.formula.termcolumns(self.terms[0], dict=False) col2 = self.formula.termcolumns(self.terms[1], dict=False) test = [[1] + [0]*9, [0]*2 + [1] + [0]*7] - scipy.testing.assert_almost_equal(c.matrix, test) + assert_almost_equal(c.matrix, test) def test_contrast2(self): @@ -91,7 +92,7 @@ c = contrast.Contrast(term, self.formula) c.getmatrix(namespace=self.namespace) test = [0]*2 + [1] + [0]*7 - scipy.testing.assert_almost_equal(c.matrix, test) + assert_almost_equal(c.matrix, test) def test_contrast3(self): @@ -124,4 +125,4 @@ if __name__ == '__main__': - unittest.main() + ScipyTest.run() Modified: trunk/Lib/sandbox/models/tests/test_glm.py =================================================================== --- trunk/Lib/sandbox/models/tests/test_glm.py 2006-10-11 23:28:59 UTC (rev 2254) +++ trunk/Lib/sandbox/models/tests/test_glm.py 2006-10-12 06:48:34 UTC (rev 2255) @@ -1,26 +1,29 @@ -import models as S +import scipy.sandbox.models as S import unittest import numpy.random as R import numpy as N +from numpy.testing import * +from scipy.sandbox.models.glm import Model W = R.standard_normal -class RegressionTest(unittest.TestCase): - def testLogistic(self): +class test_Regression(ScipyTestCase): + + def check_Logistic(self): X = W((40,10)) Y = N.greater(W((40,)), 0) family = S.family.Binomial() - model = S.glm.GeneralizedLinearModel(design=X, family=S.family.Binomial()) + model = Model(design=X, family=S.family.Binomial()) results = model.fit(Y) self.assertEquals(results.df_resid, 30) - def testLogisticdegenerate(self): + def check_Logisticdegenerate(self): X = W((40,10)) X[:,0] = X[:,1] + X[:,2] Y = N.greater(W((40,)), 0) family = S.family.Binomial() - model = S.glm.GeneralizedLinearModel(design=X, family=S.family.Binomial()) + model = Model(design=X, family=S.family.Binomial()) results = model.fit(Y) self.assertEquals(results.df_resid, 31) @@ -29,6 +32,5 @@ suite = unittest.makeSuite(RegressionTest) return suite - -if __name__ == '__main__': - unittest.main() +if __name__ == "__main__": + ScipyTest().run() Modified: trunk/Lib/sandbox/models/tests/test_regression.py =================================================================== --- trunk/Lib/sandbox/models/tests/test_regression.py 2006-10-11 23:28:59 UTC (rev 2254) +++ trunk/Lib/sandbox/models/tests/test_regression.py 2006-10-12 06:48:34 UTC (rev 2255) @@ -1,10 +1,11 @@ import unittest from numpy.random import standard_normal from scipy.sandbox.models.regression import OLSModel, ARModel +from numpy.testing import * W = standard_normal -class RegressionTest(unittest.TestCase): +class test_Regression(ScipyTestCase): def testOLS(self): X = W((40,10)) @@ -42,4 +43,4 @@ if __name__ == '__main__': - unittest.main() + ScipyTest.run() Modified: trunk/Lib/sandbox/models/tests/test_utils.py =================================================================== --- trunk/Lib/sandbox/models/tests/test_utils.py 2006-10-11 23:28:59 UTC (rev 2254) +++ trunk/Lib/sandbox/models/tests/test_utils.py 2006-10-12 06:48:34 UTC (rev 2255) @@ -2,20 +2,20 @@ import numpy as N import numpy.random as R import scipy - +from numpy.testing import * from scipy.sandbox.models import utils -class UtilsTest(unittest.TestCase): +class test_Utils(ScipyTestCase): def test_recipr(self): X = N.array([[2,1],[-1,0]]) Y = utils.recipr(X) - scipy.testing.assert_almost_equal(Y, N.array([[0.5,1],[0,0]])) + assert_almost_equal(Y, N.array([[0.5,1],[0,0]])) def test_recipr0(self): X = N.array([[2,1],[-4,0]]) Y = utils.recipr0(X) - scipy.testing.assert_almost_equal(Y, N.array([[0.5,1],[-0.25,0]])) + assert_almost_equal(Y, N.array([[0.5,1],[-0.25,0]])) def test_rank(self): X = R.standard_normal((40,10)) @@ -41,7 +41,7 @@ x = N.arange(20) y = N.arange(20) f = utils.StepFunction(x, y) - scipy.testing.assert_almost_equal(f( N.array([[3.2,4.5],[24,-3.1]]) ), [[ 3, 4], [19, 0]]) + assert_almost_equal(f( N.array([[3.2,4.5],[24,-3.1]]) ), [[ 3, 4], [19, 0]]) def test_StepFunctionBadShape(self): x = N.arange(20) @@ -52,4 +52,4 @@ self.assertRaises(ValueError, utils.StepFunction, x, y) if __name__ == '__main__': - unittest.main() + ScipyTest.run() From scipy-svn at scipy.org Thu Oct 12 02:50:52 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 01:50:52 -0500 (CDT) Subject: [Scipy-svn] r2256 - trunk/Lib/sandbox/models Message-ID: <20061012065052.1459839C01C@new.scipy.org> Author: timl Date: 2006-10-12 01:50:41 -0500 (Thu, 12 Oct 2006) New Revision: 2256 Modified: trunk/Lib/sandbox/models/survival.py Log: rearrange the SurvivalTime class hierarchy Modified: trunk/Lib/sandbox/models/survival.py =================================================================== --- trunk/Lib/sandbox/models/survival.py 2006-10-12 06:48:34 UTC (rev 2255) +++ trunk/Lib/sandbox/models/survival.py 2006-10-12 06:50:41 UTC (rev 2256) @@ -1,17 +1,18 @@ import numpy as N class SurvivalTime: - pass - -class RightCensored(SurvivalTime): - def __init__(self, time, delta): self.time, self.delta = time, delta def atrisk(self, time): + raise NotImplementedError + +class RightCensored(SurvivalTime): + + def atrisk(self, time): return N.less_equal.outer(time, self.time) -class LeftCensored(RightCensored): +class LeftCensored(SurvivalTime): def atrisk(self, time): return N.greater_equal.outer(time, self.time) From scipy-svn at scipy.org Thu Oct 12 02:56:11 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 01:56:11 -0500 (CDT) Subject: [Scipy-svn] r2257 - trunk/Lib/sandbox/models Message-ID: <20061012065611.2470639C01C@new.scipy.org> Author: timl Date: 2006-10-12 01:56:02 -0500 (Thu, 12 Oct 2006) New Revision: 2257 Modified: trunk/Lib/sandbox/models/cox.py Log: remove unused variable. add FIXME for bug Modified: trunk/Lib/sandbox/models/cox.py =================================================================== --- trunk/Lib/sandbox/models/cox.py 2006-10-12 06:50:41 UTC (rev 2256) +++ trunk/Lib/sandbox/models/cox.py 2006-10-12 06:56:02 UTC (rev 2257) @@ -60,8 +60,6 @@ def initialize(self, subjects): - v = [(s.delta, s.time) for s in subjects] - self.failures = {} for i in range(len(subjects)): s = subjects[i] @@ -158,6 +156,9 @@ raise NotImplementedError, 'Cox tie breaking method not implemented' else: raise NotImplementedError, 'tie breaking method not recognized' + # FIXME: score is an int. it has no shape + # is it that we shouldn't be using an int above + # or that we shouldn't be looking at shape here if score.shape == (): score = N.array([score]) return score From scipy-svn at scipy.org Thu Oct 12 03:02:42 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 02:02:42 -0500 (CDT) Subject: [Scipy-svn] r2258 - trunk/Lib/sandbox/models Message-ID: <20061012070242.4D43539C01C@new.scipy.org> Author: timl Date: 2006-10-12 02:02:33 -0500 (Thu, 12 Oct 2006) New Revision: 2258 Modified: trunk/Lib/sandbox/models/formula.py Log: clean up code and identify bugs Modified: trunk/Lib/sandbox/models/formula.py =================================================================== --- trunk/Lib/sandbox/models/formula.py 2006-10-12 06:56:02 UTC (rev 2257) +++ trunk/Lib/sandbox/models/formula.py 2006-10-12 07:02:33 UTC (rev 2258) @@ -1,4 +1,4 @@ -import types, re, string, csv, copy +import types, copy import numpy as N terms = {} @@ -82,7 +82,7 @@ val = val(namespace=namespace, **extra) elif callable(val): val = val(**extra) - else: + else: val = self.func(namespace=namespace, **extra) val = N.asarray(val) return N.squeeze(val) @@ -110,6 +110,7 @@ def func(namespace=terms, key=key): v = namespace[self._name] + # FIXME: n is not defined here col = [float(self.keys.index(v[i])) for i in range(n)] return N.array(col) Term.__init__(self, self.name, func=func) @@ -214,7 +215,6 @@ return value class FuncQuant(Quantitative): - """ A Term for a quantitative function of a Term. """ @@ -231,6 +231,7 @@ def func(namespace=terms, f=self.f): x = namespace[x.name] return f(x) + # FIXME: quant is not defined here. try: termname = '%s(%s)' % (f.func_name, quant.name) except: @@ -432,7 +433,7 @@ name = '%s*%s' % (str(selfnames[r]), str(othernames[s])) pieces = name.split('*') pieces.sort() - name = string.join(pieces, '*') + name = '*'.join(pieces) names.append(name) def func(namespace=terms, selfterm=self.terms[i], otherterm=other.terms[j], **extra): From scipy-svn at scipy.org Thu Oct 12 03:03:52 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 02:03:52 -0500 (CDT) Subject: [Scipy-svn] r2259 - trunk/Lib/sandbox/models Message-ID: <20061012070352.D843939C01C@new.scipy.org> Author: timl Date: 2006-10-12 02:03:45 -0500 (Thu, 12 Oct 2006) New Revision: 2259 Modified: trunk/Lib/sandbox/models/mixed.py Log: remove unused imports Modified: trunk/Lib/sandbox/models/mixed.py =================================================================== --- trunk/Lib/sandbox/models/mixed.py 2006-10-12 07:02:33 UTC (rev 2258) +++ trunk/Lib/sandbox/models/mixed.py 2006-10-12 07:03:45 UTC (rev 2259) @@ -1,7 +1,6 @@ -import UserDict import numpy as N import numpy.linalg as L -from formula import Term, Formula, I +from formula import Formula, I class Unit: From scipy-svn at scipy.org Thu Oct 12 03:04:46 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 02:04:46 -0500 (CDT) Subject: [Scipy-svn] r2260 - trunk/Lib/sandbox/models Message-ID: <20061012070446.6048139C01C@new.scipy.org> Author: timl Date: 2006-10-12 02:04:38 -0500 (Thu, 12 Oct 2006) New Revision: 2260 Modified: trunk/Lib/sandbox/models/contrast.py Log: split long lines, remove unused function parameters Modified: trunk/Lib/sandbox/models/contrast.py =================================================================== --- trunk/Lib/sandbox/models/contrast.py 2006-10-12 07:03:45 UTC (rev 2259) +++ trunk/Lib/sandbox/models/contrast.py 2006-10-12 07:04:38 UTC (rev 2260) @@ -23,9 +23,11 @@ def __str__(self): if hasattr(self, 'F'): - return '' % (`self.F`, self.df_denom, self.df_num) + return '' % \ + (`self.F`, self.df_denom, self.df_num) else: - return '' % (`self.effect`, `self.sd`, `self.t`, self.df_denom) + return '' % \ + (`self.effect`, `self.sd`, `self.t`, self.df_denom) class Contrast: @@ -52,7 +54,7 @@ """ - def __init__(self, term, formula, name='', **keywords): + def __init__(self, term, formula, name=''): self.term = term self.formula = formula if name is '': @@ -61,7 +63,8 @@ self.name = name def __str__(self): - return '' % `{'term':str(self.term), 'formula':str(self.formula)}` + return '' % \ + `{'term':str(self.term), 'formula':str(self.formula)}` def getmatrix(self, evaldesign=True, **keywords): """ @@ -92,7 +95,7 @@ except: self.rank = 1 -def contrastfromcols(T, D, pseudo=None, warn=True): +def contrastfromcols(T, D, pseudo=None): """ From an n x p design matrix D and a matrix T, tries to determine a p x q contrast matrix C which From scipy-svn at scipy.org Thu Oct 12 03:06:09 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 02:06:09 -0500 (CDT) Subject: [Scipy-svn] r2261 - trunk/Lib/sandbox/models Message-ID: <20061012070609.01E5A39C01C@new.scipy.org> Author: timl Date: 2006-10-12 02:06:02 -0500 (Thu, 12 Oct 2006) New Revision: 2261 Modified: trunk/Lib/sandbox/models/glm.py Log: bugfix: GeneralizedLinearModel is now just Model Modified: trunk/Lib/sandbox/models/glm.py =================================================================== --- trunk/Lib/sandbox/models/glm.py 2006-10-12 07:04:38 UTC (rev 2260) +++ trunk/Lib/sandbox/models/glm.py 2006-10-12 07:06:02 UTC (rev 2261) @@ -41,7 +41,7 @@ """ Continue iterating, or has convergence been obtained? """ - if self.iter >= GeneralizedLinearModel.niter: + if self.iter >= Model.niter: return False curdev = self.deviance(results=results) From scipy-svn at scipy.org Thu Oct 12 03:06:54 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 02:06:54 -0500 (CDT) Subject: [Scipy-svn] r2262 - trunk/Lib/sandbox/models Message-ID: <20061012070654.C8A8239C01C@new.scipy.org> Author: timl Date: 2006-10-12 02:06:48 -0500 (Thu, 12 Oct 2006) New Revision: 2262 Modified: trunk/Lib/sandbox/models/__init__.py Log: add a test() function to sandbox.models Modified: trunk/Lib/sandbox/models/__init__.py =================================================================== --- trunk/Lib/sandbox/models/__init__.py 2006-10-12 07:06:02 UTC (rev 2261) +++ trunk/Lib/sandbox/models/__init__.py 2006-10-12 07:06:48 UTC (rev 2262) @@ -6,7 +6,12 @@ from glm import Model as glm from rlm import Model as rlm + + import unittest def suite(): return unittest.TestSuite([tests.suite()]) + +from numpy.testing import ScipyTest +test = ScipyTest().test From scipy-svn at scipy.org Thu Oct 12 03:08:01 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 02:08:01 -0500 (CDT) Subject: [Scipy-svn] r2263 - trunk/Lib/sandbox/models Message-ID: <20061012070801.6156439C01C@new.scipy.org> Author: timl Date: 2006-10-12 02:07:53 -0500 (Thu, 12 Oct 2006) New Revision: 2263 Modified: trunk/Lib/sandbox/models/regression.py Log: bug fixes Modified: trunk/Lib/sandbox/models/regression.py =================================================================== --- trunk/Lib/sandbox/models/regression.py 2006-10-12 07:06:48 UTC (rev 2262) +++ trunk/Lib/sandbox/models/regression.py 2006-10-12 07:07:53 UTC (rev 2263) @@ -38,10 +38,10 @@ Z = self.whiten(Y) - lfit = Results(L.lstsq(self.wdesign, Z)[0]) + lfit = Results(L.lstsq(self.wdesign, Z)[0], Y) lfit.predict = N.dot(self.design, lfit.beta) - lfit.Y = Y + def fit(self, Y, **keywords): """ Full \'fit\' of the model including estimate of covariance matrix, (whitened) @@ -51,7 +51,7 @@ Z = self.whiten(Y) - lfit = Results(N.dot(self.calc_beta, Z), + lfit = Results(N.dot(self.calc_beta, Z), Y, normalized_cov_beta=self.normalized_cov_beta) lfit.df_resid = self.df_resid @@ -60,7 +60,6 @@ lfit.scale = N.add.reduce(lfit.resid**2) / lfit.df_resid lfit.Z = Z # just in case - lfit.Y = Y return lfit @@ -112,19 +111,22 @@ It handles the output of contrasts, estimates of covariance, etc. """ + def __init__(self, beta, Y, normalized_cov_beta=None, scale=1.): + LikelihoodModelResults.__init__(self, beta, normalized_cov_beta, scale) + self.Y = Y + def norm_resid(self): """ Residuals, normalized to have unit length. Note: residuals are whitened residuals. - """ if not hasattr(self, 'resid'): raise ValueError, 'need normalized residuals to estimate standard deviation' sdd = utils.recipr(self.sd) / N.sqrt(self.df) - norm_resid = self.resid * N.multiply.outer(N.ones(Y.shape[0]), sdd) + norm_resid = self.resid * N.multiply.outer(N.ones(self.Y.shape[0]), sdd) return norm_resid def predict(self, design): @@ -140,11 +142,11 @@ """ self.Ssq = N.std(self.Z,axis=0)**2 ratio = self.scale / self.Ssq - if not adjusted: ratio *= ((Y.shape[0] - 1) / self.df_resid) + if not adjusted: ratio *= ((self.Y.shape[0] - 1) / self.df_resid) return 1 - ratio -def isestimable(C, D, pinv=None, warn=True): +def isestimable(C, D): """ From an q x p contrast matrix C and an n x p design matrix D, checks if the contrast C is estimable by looking at the rank of hstack([C,D]) and From scipy-svn at scipy.org Thu Oct 12 03:09:07 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 02:09:07 -0500 (CDT) Subject: [Scipy-svn] r2264 - trunk/Lib/sandbox/models Message-ID: <20061012070907.BBE5F39C01C@new.scipy.org> Author: timl Date: 2006-10-12 02:09:00 -0500 (Thu, 12 Oct 2006) New Revision: 2264 Modified: trunk/Lib/sandbox/models/setup.py Log: add the tests directory to setup.py Modified: trunk/Lib/sandbox/models/setup.py =================================================================== --- trunk/Lib/sandbox/models/setup.py 2006-10-12 07:07:53 UTC (rev 2263) +++ trunk/Lib/sandbox/models/setup.py 2006-10-12 07:09:00 UTC (rev 2264) @@ -3,6 +3,9 @@ config = Configuration(package_name,parent_package,top_path) config.add_subpackage('*') + + config.add_data_dir('tests') + return config From scipy-svn at scipy.org Thu Oct 12 09:43:03 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 08:43:03 -0500 (CDT) Subject: [Scipy-svn] r2265 - trunk/Lib/sandbox Message-ID: <20061012134303.1634039C082@new.scipy.org> Author: cdavid Date: 2006-10-12 08:42:53 -0500 (Thu, 12 Oct 2006) New Revision: 2265 Added: trunk/Lib/sandbox/pyem/ Log: Intial creation of pyem pkg into sandbox From scipy-svn at scipy.org Thu Oct 12 09:45:12 2006 From: scipy-svn at scipy.org (scipy-svn at scipy.org) Date: Thu, 12 Oct 2006 08:45:12 -0500 (CDT) Subject: [Scipy-svn] r2266 - trunk/Lib/sandbox/pyem Message-ID: <20061012134512.CA40C39C082@new.scipy.org> Author: cdavid Date: 2006-10-12 08:45:05 -0500 (Thu, 12 Oct 2006) New Revision: 2266 Added: trunk/Lib/sandbox/pyem/Makefile trunk/Lib/sandbox/pyem/c_gmm.pyx trunk/Lib/sandbox/pyem/c_numpy.pxd trunk/Lib/sandbox/pyem/c_python.pxd trunk/Lib/sandbox/pyem/densities.py trunk/Lib/sandbox/pyem/gmm_em.py Log: [pyem @ david at ar.media.kyoto-u.ac.jp-20060608085958-db646b48140fb363] Initial commit David Cournapeau | 2006-06-08 17:59:58 +0900 (Thu, 08 Jun 2006) Added: trunk/Lib/sandbox/pyem/Makefile =================================================================== --- trunk/Lib/sandbox/pyem/Makefile 2006-10-12 13:42:53 UTC (rev 2265) +++ trunk/Lib/sandbox/pyem/Makefile 2006-10-12 13:45:05 UTC (rev 2266) @@ -0,0 +1,26 @@ +CC = colorgcc +LD = gcc + +PYREX = python2.4-pyrexc + +PYTHONINC = -I/usr/include/python2.4 +NUMPYINC = -I/usr/lib/python2.4/site-packages/numpy/core/include +OPTIMS = -O3 -ffast-math -march=pentium4 +WARN = -W -Wall + +CFLAGS = $(PYTHONINC) $(NUMPYINC) $(OPTIMS) $(WARN) + +c_gmm.so: c_gmm.o + $(LD) -shared -o $@ $< -Wl,-soname,$@ + +c_gmm.o: c_gmm.c + $(CC) -c $(CFLAGS) -fPIC $< + +c_gmm.c: c_gmm.pyx c_numpy.pxd c_python.pxd + $(PYREX) $< + +clean: + rm -f c_gmm.c + rm -f *.o + rm -f *.so + rm -f *.pyc Added: trunk/Lib/sandbox/pyem/c_gmm.pyx =================================================================== --- trunk/Lib/sandbox/pyem/c_gmm.pyx 2006-10-12 13:42:53 UTC (rev 2265) +++ trunk/Lib/sandbox/pyem/c_gmm.pyx 2006-10-12 13:45:05 UTC (rev 2266) @@ -0,0 +1,66 @@ +# Last Change: Tue May 16 11:00 AM 2006 J + +cimport c_numpy +cimport c_python +import numpy + +c_numpy.import_array() + +# pyrex version of _vq. Much faster in high dimension/high number of K +# (ie more than 3-4) +def _vq(data, init): + (n, d) = data.shape + label = numpy.zeros(n, int) + _imp_vq(data, init, label) + + return label + +def _imp_vq(c_numpy.ndarray data, c_numpy.ndarray init, c_numpy.ndarray label): + cdef int n + cdef int d + cdef int nc + cdef int i + cdef int j + cdef int k + cdef int *labeld + cdef double *da, *code + cdef double dist + cdef double acc + + n = data.dimensions[0] + d = data.dimensions[1] + nc = init.dimensions[0] + + if not data.dtype == numpy.dtype(numpy.float64): + print '_vq not (yet) implemented for dtype %s'%dtype.name + return + da = (data.data) + + if not init.dtype == numpy.dtype(numpy.float64): + print '_vq not (yet) implemented for dtype %s'%dtype.name + return + code = (init.data) + + if not label.dtype == numpy.dtype(numpy.int32): + print '_vq not (yet) implemented for dtype %s'%dtype.name + return + labeld = (label.data) + + for i from 0<=i