From numpy-svn at scipy.org Mon Mar 1 07:01:27 2010 From: numpy-svn at scipy.org (Earline) Date: Mon, 1 Mar 2010 06:01:27 -0600 (CST) Subject: [Numpy-svn] Delivery Status Notification Message-ID: <20100301120127.B8DDC39CC81@scipy.org> An HTML attachment was scrubbed... URL: From numpy-svn at scipy.org Tue Mar 2 14:28:30 2010 From: numpy-svn at scipy.org (Maricela) Date: Tue, 2 Mar 2010 13:28:30 -0600 (CST) Subject: [Numpy-svn] Message #626692 Message-ID: <20100302192830.2D1A039CB9A@scipy.org> An HTML attachment was scrubbed... URL: From numpy-svn at scipy.org Tue Mar 2 14:59:36 2010 From: numpy-svn at scipy.org (Ramon) Date: Tue, 2 Mar 2010 13:59:36 -0600 (CST) Subject: [Numpy-svn] Order status #589433 Message-ID: <20100302195936.44FF039CB7A@scipy.org> An HTML attachment was scrubbed... URL: From numpy-svn at scipy.org Wed Mar 3 03:16:25 2010 From: numpy-svn at scipy.org (Kelsey) Date: Wed, 3 Mar 2010 02:16:25 -0600 (CST) Subject: [Numpy-svn] Message #154442 Message-ID: <20100303081625.A245F39CBC9@scipy.org> An HTML attachment was scrubbed... URL: From numpy-svn at scipy.org Wed Mar 3 19:44:51 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Wed, 3 Mar 2010 18:44:51 -0600 (CST) Subject: [Numpy-svn] r8271 - trunk Message-ID: <20100304004451.EDA7339CAF3@scipy.org> Author: cdavid Date: 2010-03-03 18:44:51 -0600 (Wed, 03 Mar 2010) New Revision: 8271 Modified: trunk/pavement.py Log: BUG: fix paver execution on windows 7 for python 2.6. Modified: trunk/pavement.py =================================================================== --- trunk/pavement.py 2010-02-28 03:47:04 UTC (rev 8270) +++ trunk/pavement.py 2010-03-04 00:44:51 UTC (rev 8271) @@ -128,7 +128,9 @@ "2.6": ["C:\Python26\python.exe"], "2.5": ["C:\Python25\python.exe"], } - WINDOWS_ENV = {} + # XXX: find out which env variable is necessary to avoid the pb with python + # 2.6 and random module when importing tempfile + WINDOWS_ENV = os.environ MAKENSIS = ["makensis"] else: WINDOWS_PYTHON = { From numpy-svn at scipy.org Wed Mar 3 19:45:44 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Wed, 3 Mar 2010 18:45:44 -0600 (CST) Subject: [Numpy-svn] r8272 - branches/1.4.x Message-ID: <20100304004544.258AB39CAF3@scipy.org> Author: cdavid Date: 2010-03-03 18:45:44 -0600 (Wed, 03 Mar 2010) New Revision: 8272 Modified: branches/1.4.x/pavement.py Log: BUG: fix paver execution on windows 7 for python 2.6. (cherry picked from commit 68fa6c03d92d8fa42ec9c4978c0a8e577775f292) Modified: branches/1.4.x/pavement.py =================================================================== --- branches/1.4.x/pavement.py 2010-03-04 00:44:51 UTC (rev 8271) +++ branches/1.4.x/pavement.py 2010-03-04 00:45:44 UTC (rev 8272) @@ -128,7 +128,9 @@ "2.6": ["C:\Python26\python.exe"], "2.5": ["C:\Python25\python.exe"], } - WINDOWS_ENV = {} + # XXX: find out which env variable is necessary to avoid the pb with python + # 2.6 and random module when importing tempfile + WINDOWS_ENV = os.environ MAKENSIS = ["makensis"] else: WINDOWS_PYTHON = { From numpy-svn at scipy.org Sat Mar 6 11:42:41 2010 From: numpy-svn at scipy.org (Admin Pillstore) Date: Sat, 6 Mar 2010 10:42:41 -0600 (CST) Subject: [Numpy-svn] Online Shopping Message-ID: <20100306164241.0564E39CBFD@scipy.org> An HTML attachment was scrubbed... URL: From numpy-svn at scipy.org Sat Mar 6 12:24:02 2010 From: numpy-svn at scipy.org (Admin Pillstore) Date: Sat, 6 Mar 2010 11:24:02 -0600 (CST) Subject: [Numpy-svn] Looking to do some online shopping? Message-ID: <20100306172402.4904039CC0D@scipy.org> An HTML attachment was scrubbed... URL: From numpy-svn at scipy.org Sat Mar 6 14:10:14 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 13:10:14 -0600 (CST) Subject: [Numpy-svn] r8273 - in trunk/numpy/f2py: . tests tests/array_from_pyobj tests/array_from_pyobj/tests tests/c tests/f77 tests/f90 tests/mixed tests/src tests/src/array_from_pyobj tests/src/mixed Message-ID: <20100306191014.2A2D739CB09@scipy.org> Author: ptvirtan Date: 2010-03-06 13:10:14 -0600 (Sat, 06 Mar 2010) New Revision: 8273 Added: trunk/numpy/f2py/tests/src/ trunk/numpy/f2py/tests/src/array_from_pyobj/ trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c trunk/numpy/f2py/tests/src/mixed/ trunk/numpy/f2py/tests/src/mixed/foo.f trunk/numpy/f2py/tests/src/mixed/foo_fixed.f90 trunk/numpy/f2py/tests/src/mixed/foo_free.f90 trunk/numpy/f2py/tests/test_array_from_pyobj.py trunk/numpy/f2py/tests/test_callback.py trunk/numpy/f2py/tests/test_mixed.py trunk/numpy/f2py/tests/test_return_character.py trunk/numpy/f2py/tests/test_return_complex.py trunk/numpy/f2py/tests/test_return_integer.py trunk/numpy/f2py/tests/test_return_logical.py trunk/numpy/f2py/tests/test_return_real.py trunk/numpy/f2py/tests/util.py Removed: trunk/numpy/f2py/tests/array_from_pyobj/__init__.py trunk/numpy/f2py/tests/array_from_pyobj/setup.py trunk/numpy/f2py/tests/array_from_pyobj/tests/test_array_from_pyobj.py trunk/numpy/f2py/tests/array_from_pyobj/wrapmodule.c trunk/numpy/f2py/tests/c/return_real.py trunk/numpy/f2py/tests/f77/callback.py trunk/numpy/f2py/tests/f77/return_character.py trunk/numpy/f2py/tests/f77/return_complex.py trunk/numpy/f2py/tests/f77/return_integer.py trunk/numpy/f2py/tests/f77/return_logical.py trunk/numpy/f2py/tests/f77/return_real.py trunk/numpy/f2py/tests/f90/return_character.py trunk/numpy/f2py/tests/f90/return_complex.py trunk/numpy/f2py/tests/f90/return_integer.py trunk/numpy/f2py/tests/f90/return_logical.py trunk/numpy/f2py/tests/f90/return_real.py trunk/numpy/f2py/tests/mixed/foo.f trunk/numpy/f2py/tests/mixed/foo_fixed.f90 trunk/numpy/f2py/tests/mixed/foo_free.f90 trunk/numpy/f2py/tests/mixed/run.py trunk/numpy/f2py/tests/run_all.py Modified: trunk/numpy/f2py/setup.py Log: ENH: f2py: convert test suite to Nose form Rewrite F2Py's test suite, so that it is run as a part of Numpy's tests. These tests require compiling extension modules on-the-fly, so I added a small helper module for that. Modified: trunk/numpy/f2py/setup.py =================================================================== --- trunk/numpy/f2py/setup.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/setup.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -31,6 +31,7 @@ config = Configuration('f2py', parent_package, top_path) config.add_data_dir('docs') + config.add_data_dir('tests') config.add_data_files('src/fortranobject.c', 'src/fortranobject.h', Deleted: trunk/numpy/f2py/tests/array_from_pyobj/__init__.py =================================================================== Deleted: trunk/numpy/f2py/tests/array_from_pyobj/setup.py =================================================================== --- trunk/numpy/f2py/tests/array_from_pyobj/setup.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/array_from_pyobj/setup.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,25 +0,0 @@ -import os -def configuration(parent_name='',top_path=None): - from numpy.distutils.misc_util import Configuration - - config = Configuration('array_from_pyobj',parent_name,top_path) - #import numpy.f2py as f2py - #f2pydir=os.path.dirname(os.path.abspath(f2py.__file__)) - f2pydir=os.path.join(config.local_path,'..','..') - fobjhsrc = os.path.join(f2pydir,'src','fortranobject.h') - fobjcsrc = os.path.join(f2pydir,'src','fortranobject.c') - config.add_extension('wrap', - sources = ['wrapmodule.c',fobjcsrc], - include_dirs = [os.path.dirname(fobjhsrc)], - depends = [fobjhsrc,fobjcsrc], - define_macros = [('DEBUG_COPY_ND_ARRAY',1), - #('F2PY_REPORT_ON_ARRAY_COPY',1), - #('F2PY_REPORT_ATEXIT',1) - ] - ) - - return config - -if __name__ == "__main__": - from numpy.distutils.core import setup - setup(configuration=configuration) Deleted: trunk/numpy/f2py/tests/array_from_pyobj/tests/test_array_from_pyobj.py =================================================================== --- trunk/numpy/f2py/tests/array_from_pyobj/tests/test_array_from_pyobj.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/array_from_pyobj/tests/test_array_from_pyobj.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,513 +0,0 @@ -import unittest -import sys -import copy - -from numpy.testing import * -from numpy import array, alltrue, ndarray, asarray, can_cast,zeros, dtype -from numpy.core.multiarray import typeinfo -from array_from_pyobj import wrap - -def flags_info(arr): - flags = wrap.array_attrs(arr)[6] - return flags2names(flags) - -def flags2names(flags): - info = [] - for flagname in ['CONTIGUOUS','FORTRAN','OWNDATA','ENSURECOPY', - 'ENSUREARRAY','ALIGNED','NOTSWAPPED','WRITEABLE', - 'UPDATEIFCOPY','BEHAVED','BEHAVED_RO', - 'CARRAY','FARRAY' - ]: - if abs(flags) & getattr(wrap,flagname): - info.append(flagname) - return info - -class Intent: - def __init__(self,intent_list=[]): - self.intent_list = intent_list[:] - flags = 0 - for i in intent_list: - if i=='optional': - flags |= wrap.F2PY_OPTIONAL - else: - flags |= getattr(wrap,'F2PY_INTENT_'+i.upper()) - self.flags = flags - def __getattr__(self,name): - name = name.lower() - if name=='in_': name='in' - return self.__class__(self.intent_list+[name]) - def __str__(self): - return 'intent(%s)' % (','.join(self.intent_list)) - def __repr__(self): - return 'Intent(%r)' % (self.intent_list) - def is_intent(self,*names): - for name in names: - if name not in self.intent_list: - return False - return True - def is_intent_exact(self,*names): - return len(self.intent_list)==len(names) and self.is_intent(*names) - -intent = Intent() - -class Type(object): - - _type_names = ['BOOL','BYTE','UBYTE','SHORT','USHORT','INT','UINT', - 'LONG','ULONG','LONGLONG','ULONGLONG', - 'FLOAT','DOUBLE','LONGDOUBLE','CFLOAT','CDOUBLE', - 'CLONGDOUBLE'] - _type_cache = {} - - _cast_dict = {'BOOL':['BOOL']} - _cast_dict['BYTE'] = _cast_dict['BOOL'] + ['BYTE'] - _cast_dict['UBYTE'] = _cast_dict['BOOL'] + ['UBYTE'] - _cast_dict['BYTE'] = ['BYTE'] - _cast_dict['UBYTE'] = ['UBYTE'] - _cast_dict['SHORT'] = _cast_dict['BYTE'] + ['UBYTE','SHORT'] - _cast_dict['USHORT'] = _cast_dict['UBYTE'] + ['BYTE','USHORT'] - _cast_dict['INT'] = _cast_dict['SHORT'] + ['USHORT','INT'] - _cast_dict['UINT'] = _cast_dict['USHORT'] + ['SHORT','UINT'] - - _cast_dict['LONG'] = _cast_dict['INT'] + ['LONG'] - _cast_dict['ULONG'] = _cast_dict['UINT'] + ['ULONG'] - - _cast_dict['LONGLONG'] = _cast_dict['LONG'] + ['LONGLONG'] - _cast_dict['ULONGLONG'] = _cast_dict['ULONG'] + ['ULONGLONG'] - - _cast_dict['FLOAT'] = _cast_dict['SHORT'] + ['USHORT','FLOAT'] - _cast_dict['DOUBLE'] = _cast_dict['INT'] + ['UINT','FLOAT','DOUBLE'] - _cast_dict['LONGDOUBLE'] = _cast_dict['LONG'] + ['ULONG','FLOAT','DOUBLE','LONGDOUBLE'] - - _cast_dict['CFLOAT'] = _cast_dict['FLOAT'] + ['CFLOAT'] - _cast_dict['CDOUBLE'] = _cast_dict['DOUBLE'] + ['CFLOAT','CDOUBLE'] - _cast_dict['CLONGDOUBLE'] = _cast_dict['LONGDOUBLE'] + ['CFLOAT','CDOUBLE','CLONGDOUBLE'] - - - def __new__(cls,name): - if isinstance(name,dtype): - dtype0 = name - name = None - for n,i in typeinfo.items(): - if isinstance(i,tuple) and dtype0.type is i[-1]: - name = n - break - obj = cls._type_cache.get(name.upper(),None) - if obj is not None: - return obj - obj = object.__new__(cls) - obj._init(name) - cls._type_cache[name.upper()] = obj - return obj - - def _init(self,name): - self.NAME = name.upper() - self.type_num = getattr(wrap,'PyArray_'+self.NAME) - assert_equal(self.type_num,typeinfo[self.NAME][1]) - self.dtype = typeinfo[self.NAME][-1] - self.elsize = typeinfo[self.NAME][2] / 8 - self.dtypechar = typeinfo[self.NAME][0] - - def cast_types(self): - return map(self.__class__,self._cast_dict[self.NAME]) - - def all_types(self): - return map(self.__class__,self._type_names) - - def smaller_types(self): - bits = typeinfo[self.NAME][3] - types = [] - for name in self._type_names: - if typeinfo[name][3]bits: - types.append(Type(name)) - return types - -class Array: - def __init__(self,typ,dims,intent,obj): - self.type = typ - self.dims = dims - self.intent = intent - self.obj_copy = copy.deepcopy(obj) - self.obj = obj - - # arr.dtypechar may be different from typ.dtypechar - self.arr = wrap.call(typ.type_num,dims,intent.flags,obj) - - self.arr_attr = wrap.array_attrs(self.arr) - - if len(dims)>1: - if self.intent.is_intent('c'): - assert intent.flags & wrap.F2PY_INTENT_C - assert not self.arr.flags['FORTRAN'],`self.arr.flags,obj.flags` - assert self.arr.flags['CONTIGUOUS'] - assert not self.arr_attr[6] & wrap.FORTRAN - else: - assert not intent.flags & wrap.F2PY_INTENT_C - assert self.arr.flags['FORTRAN'] - assert not self.arr.flags['CONTIGUOUS'] - assert self.arr_attr[6] & wrap.FORTRAN - - if obj is None: - self.pyarr = None - self.pyarr_attr = None - return - - if intent.is_intent('cache'): - assert isinstance(obj,ndarray),`type(obj)` - self.pyarr = array(obj).reshape(*dims) - - else: - self.pyarr = array(array(obj, - dtype = typ.dtypechar).reshape(*dims), - fortran=not self.intent.is_intent('c')) - assert self.pyarr.dtype.char==typ.dtypechar,\ - `self.pyarr.dtype.char,typ.dtypechar` - assert self.pyarr.flags['OWNDATA'] - self.pyarr_attr = wrap.array_attrs(self.pyarr) - - if len(dims)>1: - if self.intent.is_intent('c'): - assert not self.pyarr.flags['FORTRAN'] - assert self.pyarr.flags['CONTIGUOUS'] - assert not self.pyarr_attr[6] & wrap.FORTRAN - else: - assert self.pyarr.flags['FORTRAN'] - assert not self.pyarr.flags['CONTIGUOUS'] - assert self.pyarr_attr[6] & wrap.FORTRAN - - - assert self.arr_attr[1]==self.pyarr_attr[1] # nd - assert self.arr_attr[2]==self.pyarr_attr[2] # dimensions - if self.arr_attr[1]<=1: - assert self.arr_attr[3]==self.pyarr_attr[3],\ - `self.arr_attr[3],self.pyarr_attr[3],self.arr.tostring(),self.pyarr.tostring()` # strides - assert self.arr_attr[5][-2:]==self.pyarr_attr[5][-2:],\ - `self.arr_attr[5],self.pyarr_attr[5]` # descr - assert self.arr_attr[6]==self.pyarr_attr[6],\ - `self.arr_attr[6],self.pyarr_attr[6],flags2names(0*self.arr_attr[6]-self.pyarr_attr[6]),flags2names(self.arr_attr[6]),intent` # flags - - if intent.is_intent('cache'): - assert self.arr_attr[5][3]>=self.type.elsize,\ - `self.arr_attr[5][3],self.type.elsize` - else: - assert self.arr_attr[5][3]==self.type.elsize,\ - `self.arr_attr[5][3],self.type.elsize` - assert self.arr_equal(self.pyarr,self.arr) - - if isinstance(self.obj,ndarray): - if typ.elsize==Type(obj.dtype).elsize: - if not intent.is_intent('copy') and self.arr_attr[1]<=1: - assert self.has_shared_memory() - - def arr_equal(self,arr1,arr2): - if arr1.shape != arr2.shape: - return False - s = arr1==arr2 - return alltrue(s.flatten()) - - def __str__(self): - return str(self.arr) - - def has_shared_memory(self): - """Check that created array shares data with input array. - """ - if self.obj is self.arr: - return True - if not isinstance(self.obj,ndarray): - return False - obj_attr = wrap.array_attrs(self.obj) - return obj_attr[0]==self.arr_attr[0] - -################################################## - -class test_intent(unittest.TestCase): - def test_in_out(self): - assert_equal(str(intent.in_.out),'intent(in,out)') - assert intent.in_.c.is_intent('c') - assert not intent.in_.c.is_intent_exact('c') - assert intent.in_.c.is_intent_exact('c','in') - assert intent.in_.c.is_intent_exact('in','c') - assert not intent.in_.is_intent('c') - -class _test_shared_memory: - num2seq = [1,2] - num23seq = [[1,2,3],[4,5,6]] - def test_in_from_2seq(self): - a = self.array([2],intent.in_,self.num2seq) - assert not a.has_shared_memory() - - def test_in_from_2casttype(self): - for t in self.type.cast_types(): - obj = array(self.num2seq,dtype=t.dtype) - a = self.array([len(self.num2seq)],intent.in_,obj) - if t.elsize==self.type.elsize: - assert a.has_shared_memory(),`self.type.dtype,t.dtype` - else: - assert not a.has_shared_memory(),`t.dtype` - - def test_inout_2seq(self): - obj = array(self.num2seq,dtype=self.type.dtype) - a = self.array([len(self.num2seq)],intent.inout,obj) - assert a.has_shared_memory() - - try: - a = self.array([2],intent.in_.inout,self.num2seq) - except TypeError,msg: - if not str(msg).startswith('failed to initialize intent(inout|inplace|cache) array'): - raise - else: - raise SystemError,'intent(inout) should have failed on sequence' - - def test_f_inout_23seq(self): - obj = array(self.num23seq,dtype=self.type.dtype,fortran=1) - shape = (len(self.num23seq),len(self.num23seq[0])) - a = self.array(shape,intent.in_.inout,obj) - assert a.has_shared_memory() - - obj = array(self.num23seq,dtype=self.type.dtype,fortran=0) - shape = (len(self.num23seq),len(self.num23seq[0])) - try: - a = self.array(shape,intent.in_.inout,obj) - except ValueError,msg: - if not str(msg).startswith('failed to initialize intent(inout) array'): - raise - else: - raise SystemError,'intent(inout) should have failed on improper array' - - def test_c_inout_23seq(self): - obj = array(self.num23seq,dtype=self.type.dtype) - shape = (len(self.num23seq),len(self.num23seq[0])) - a = self.array(shape,intent.in_.c.inout,obj) - assert a.has_shared_memory() - - def test_in_copy_from_2casttype(self): - for t in self.type.cast_types(): - obj = array(self.num2seq,dtype=t.dtype) - a = self.array([len(self.num2seq)],intent.in_.copy,obj) - assert not a.has_shared_memory(),`t.dtype` - - def test_c_in_from_23seq(self): - a = self.array([len(self.num23seq),len(self.num23seq[0])], - intent.in_,self.num23seq) - assert not a.has_shared_memory() - - def test_in_from_23casttype(self): - for t in self.type.cast_types(): - obj = array(self.num23seq,dtype=t.dtype) - a = self.array([len(self.num23seq),len(self.num23seq[0])], - intent.in_,obj) - assert not a.has_shared_memory(),`t.dtype` - - def test_f_in_from_23casttype(self): - for t in self.type.cast_types(): - obj = array(self.num23seq,dtype=t.dtype,fortran=1) - a = self.array([len(self.num23seq),len(self.num23seq[0])], - intent.in_,obj) - if t.elsize==self.type.elsize: - assert a.has_shared_memory(),`t.dtype` - else: - assert not a.has_shared_memory(),`t.dtype` - - def test_c_in_from_23casttype(self): - for t in self.type.cast_types(): - obj = array(self.num23seq,dtype=t.dtype) - a = self.array([len(self.num23seq),len(self.num23seq[0])], - intent.in_.c,obj) - if t.elsize==self.type.elsize: - assert a.has_shared_memory(),`t.dtype` - else: - assert not a.has_shared_memory(),`t.dtype` - - def test_f_copy_in_from_23casttype(self): - for t in self.type.cast_types(): - obj = array(self.num23seq,dtype=t.dtype,fortran=1) - a = self.array([len(self.num23seq),len(self.num23seq[0])], - intent.in_.copy,obj) - assert not a.has_shared_memory(),`t.dtype` - - def test_c_copy_in_from_23casttype(self): - for t in self.type.cast_types(): - obj = array(self.num23seq,dtype=t.dtype) - a = self.array([len(self.num23seq),len(self.num23seq[0])], - intent.in_.c.copy,obj) - assert not a.has_shared_memory(),`t.dtype` - - def test_in_cache_from_2casttype(self): - for t in self.type.all_types(): - if t.elsize != self.type.elsize: - continue - obj = array(self.num2seq,dtype=t.dtype) - shape = (len(self.num2seq),) - a = self.array(shape,intent.in_.c.cache,obj) - assert a.has_shared_memory(),`t.dtype` - - a = self.array(shape,intent.in_.cache,obj) - assert a.has_shared_memory(),`t.dtype` - - obj = array(self.num2seq,dtype=t.dtype,fortran=1) - a = self.array(shape,intent.in_.c.cache,obj) - assert a.has_shared_memory(),`t.dtype` - - a = self.array(shape,intent.in_.cache,obj) - assert a.has_shared_memory(),`t.dtype` - - try: - a = self.array(shape,intent.in_.cache,obj[::-1]) - except ValueError,msg: - if not str(msg).startswith('failed to initialize intent(cache) array'): - raise - else: - raise SystemError,'intent(cache) should have failed on multisegmented array' - def test_in_cache_from_2casttype_failure(self): - for t in self.type.all_types(): - if t.elsize >= self.type.elsize: - continue - obj = array(self.num2seq,dtype=t.dtype) - shape = (len(self.num2seq),) - try: - a = self.array(shape,intent.in_.cache,obj) - except ValueError,msg: - if not str(msg).startswith('failed to initialize intent(cache) array'): - raise - else: - raise SystemError,'intent(cache) should have failed on smaller array' - - def test_cache_hidden(self): - shape = (2,) - a = self.array(shape,intent.cache.hide,None) - assert a.arr.shape==shape - - shape = (2,3) - a = self.array(shape,intent.cache.hide,None) - assert a.arr.shape==shape - - shape = (-1,3) - try: - a = self.array(shape,intent.cache.hide,None) - except ValueError,msg: - if not str(msg).startswith('failed to create intent(cache|hide)|optional array'): - raise - else: - raise SystemError,'intent(cache) should have failed on undefined dimensions' - - def test_hidden(self): - shape = (2,) - a = self.array(shape,intent.hide,None) - assert a.arr.shape==shape - assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) - - shape = (2,3) - a = self.array(shape,intent.hide,None) - assert a.arr.shape==shape - assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) - assert a.arr.flags['FORTRAN'] and not a.arr.flags['CONTIGUOUS'] - - shape = (2,3) - a = self.array(shape,intent.c.hide,None) - assert a.arr.shape==shape - assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) - assert not a.arr.flags['FORTRAN'] and a.arr.flags['CONTIGUOUS'] - - shape = (-1,3) - try: - a = self.array(shape,intent.hide,None) - except ValueError,msg: - if not str(msg).startswith('failed to create intent(cache|hide)|optional array'): - raise - else: - raise SystemError,'intent(hide) should have failed on undefined dimensions' - - def test_optional_none(self): - shape = (2,) - a = self.array(shape,intent.optional,None) - assert a.arr.shape==shape - assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) - - shape = (2,3) - a = self.array(shape,intent.optional,None) - assert a.arr.shape==shape - assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) - assert a.arr.flags['FORTRAN'] and not a.arr.flags['CONTIGUOUS'] - - shape = (2,3) - a = self.array(shape,intent.c.optional,None) - assert a.arr.shape==shape - assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) - assert not a.arr.flags['FORTRAN'] and a.arr.flags['CONTIGUOUS'] - - def test_optional_from_2seq(self): - obj = self.num2seq - shape = (len(obj),) - a = self.array(shape,intent.optional,obj) - assert a.arr.shape==shape - assert not a.has_shared_memory() - - def test_optional_from_23seq(self): - obj = self.num23seq - shape = (len(obj),len(obj[0])) - a = self.array(shape,intent.optional,obj) - assert a.arr.shape==shape - assert not a.has_shared_memory() - - a = self.array(shape,intent.optional.c,obj) - assert a.arr.shape==shape - assert not a.has_shared_memory() - - def test_inplace(self): - obj = array(self.num23seq,dtype=self.type.dtype) - assert not obj.flags['FORTRAN'] and obj.flags['CONTIGUOUS'] - shape = obj.shape - a = self.array(shape,intent.inplace,obj) - assert obj[1][2]==a.arr[1][2],`obj,a.arr` - a.arr[1][2]=54 - assert obj[1][2]==a.arr[1][2]==array(54,dtype=self.type.dtype),`obj,a.arr` - assert a.arr is obj - assert obj.flags['FORTRAN'] # obj attributes are changed inplace! - assert not obj.flags['CONTIGUOUS'] - - def test_inplace_from_casttype(self): - for t in self.type.cast_types(): - if t is self.type: - continue - obj = array(self.num23seq,dtype=t.dtype) - assert obj.dtype.type==t.dtype - assert obj.dtype.type is not self.type.dtype - assert not obj.flags['FORTRAN'] and obj.flags['CONTIGUOUS'] - shape = obj.shape - a = self.array(shape,intent.inplace,obj) - assert obj[1][2]==a.arr[1][2],`obj,a.arr` - a.arr[1][2]=54 - assert obj[1][2]==a.arr[1][2]==array(54,dtype=self.type.dtype),`obj,a.arr` - assert a.arr is obj - assert obj.flags['FORTRAN'] # obj attributes are changed inplace! - assert not obj.flags['CONTIGUOUS'] - assert obj.dtype.type is self.type.dtype # obj type is changed inplace! - - -for t in Type._type_names: - exec '''\ -class test_%s_gen(unittest.TestCase, - _test_shared_memory - ): - type = Type(%r) - array = lambda self,dims,intent,obj: Array(Type(%r),dims,intent,obj) -''' % (t,t,t) - -if __name__ == "__main__": - run_module_suite() Deleted: trunk/numpy/f2py/tests/array_from_pyobj/wrapmodule.c =================================================================== --- trunk/numpy/f2py/tests/array_from_pyobj/wrapmodule.c 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/array_from_pyobj/wrapmodule.c 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,196 +0,0 @@ -/* File: wrapmodule.c - * This file is auto-generated with f2py (version:2_1330). - * Hand edited by Pearu. - * f2py is a Fortran to Python Interface Generator (FPIG), Second Edition, - * written by Pearu Peterson . - * See http://cens.ioc.ee/projects/f2py2e/ - * Generation date: Fri Oct 21 22:41:12 2005 - * $Revision:$ - * $Date:$ - * Do not edit this file directly unless you know what you are doing!!! - */ -#ifdef __cplusplus -extern "C" { -#endif - -/*********************** See f2py2e/cfuncs.py: includes ***********************/ -#include "Python.h" -#include "fortranobject.h" -#include - -static PyObject *wrap_error; -static PyObject *wrap_module; - -/************************************ call ************************************/ -static char doc_f2py_rout_wrap_call[] = "\ -Function signature:\n\ - arr = call(type_num,dims,intent,obj)\n\ -Required arguments:\n" -" type_num : input int\n" -" dims : input int-sequence\n" -" intent : input int\n" -" obj : input python object\n" -"Return objects:\n" -" arr : array"; -static PyObject *f2py_rout_wrap_call(PyObject *capi_self, - PyObject *capi_args) { - PyObject * volatile capi_buildvalue = NULL; - int type_num = 0; - intp *dims = NULL; - PyObject *dims_capi = Py_None; - int rank = 0; - int intent = 0; - PyArrayObject *capi_arr_tmp = NULL; - PyObject *arr_capi = Py_None; - int i; - - if (!PyArg_ParseTuple(capi_args,"iOiO|:wrap.call",\ - &type_num,&dims_capi,&intent,&arr_capi)) - return NULL; - rank = PySequence_Length(dims_capi); - dims = malloc(rank*sizeof(intp)); - for (i=0;idata); - dimensions = PyTuple_New(arr->nd); - strides = PyTuple_New(arr->nd); - for (i=0;ind;++i) { - PyTuple_SetItem(dimensions,i,PyInt_FromLong(arr->dimensions[i])); - PyTuple_SetItem(strides,i,PyInt_FromLong(arr->strides[i])); - } - return Py_BuildValue("siOOO(cciii)ii",s,arr->nd, - dimensions,strides, - (arr->base==NULL?Py_None:arr->base), - arr->descr->kind, - arr->descr->type, - arr->descr->type_num, - arr->descr->elsize, - arr->descr->alignment, - arr->flags, - PyArray_ITEMSIZE(arr)); -} - -static PyMethodDef f2py_module_methods[] = { - - {"call",f2py_rout_wrap_call,METH_VARARGS,doc_f2py_rout_wrap_call}, - {"array_attrs",f2py_rout_wrap_attrs,METH_VARARGS,doc_f2py_rout_wrap_attrs}, - {NULL,NULL} -}; - -PyMODINIT_FUNC initwrap(void) { - PyObject *m,*d, *s; - m = wrap_module = Py_InitModule("wrap", f2py_module_methods); - PyFortran_Type.ob_type = &PyType_Type; - import_array(); - if (PyErr_Occurred()) - Py_FatalError("can't initialize module wrap (failed to import numpy)"); - d = PyModule_GetDict(m); - s = PyString_FromString("This module 'wrap' is auto-generated with f2py (version:2_1330).\nFunctions:\n" -" arr = call(type_num,dims,intent,obj)\n" -"."); - PyDict_SetItemString(d, "__doc__", s); - wrap_error = PyErr_NewException ("wrap.error", NULL, NULL); - Py_DECREF(s); - PyDict_SetItemString(d, "F2PY_INTENT_IN", PyInt_FromLong(F2PY_INTENT_IN)); - PyDict_SetItemString(d, "F2PY_INTENT_INOUT", PyInt_FromLong(F2PY_INTENT_INOUT)); - PyDict_SetItemString(d, "F2PY_INTENT_OUT", PyInt_FromLong(F2PY_INTENT_OUT)); - PyDict_SetItemString(d, "F2PY_INTENT_HIDE", PyInt_FromLong(F2PY_INTENT_HIDE)); - PyDict_SetItemString(d, "F2PY_INTENT_CACHE", PyInt_FromLong(F2PY_INTENT_CACHE)); - PyDict_SetItemString(d, "F2PY_INTENT_COPY", PyInt_FromLong(F2PY_INTENT_COPY)); - PyDict_SetItemString(d, "F2PY_INTENT_C", PyInt_FromLong(F2PY_INTENT_C)); - PyDict_SetItemString(d, "F2PY_OPTIONAL", PyInt_FromLong(F2PY_OPTIONAL)); - PyDict_SetItemString(d, "F2PY_INTENT_INPLACE", PyInt_FromLong(F2PY_INTENT_INPLACE)); - PyDict_SetItemString(d, "PyArray_BOOL", PyInt_FromLong(PyArray_BOOL)); - PyDict_SetItemString(d, "PyArray_BYTE", PyInt_FromLong(PyArray_BYTE)); - PyDict_SetItemString(d, "PyArray_UBYTE", PyInt_FromLong(PyArray_UBYTE)); - PyDict_SetItemString(d, "PyArray_SHORT", PyInt_FromLong(PyArray_SHORT)); - PyDict_SetItemString(d, "PyArray_USHORT", PyInt_FromLong(PyArray_USHORT)); - PyDict_SetItemString(d, "PyArray_INT", PyInt_FromLong(PyArray_INT)); - PyDict_SetItemString(d, "PyArray_UINT", PyInt_FromLong(PyArray_UINT)); - PyDict_SetItemString(d, "PyArray_INTP", PyInt_FromLong(PyArray_INTP)); - PyDict_SetItemString(d, "PyArray_UINTP", PyInt_FromLong(PyArray_UINTP)); - PyDict_SetItemString(d, "PyArray_LONG", PyInt_FromLong(PyArray_LONG)); - PyDict_SetItemString(d, "PyArray_ULONG", PyInt_FromLong(PyArray_ULONG)); - PyDict_SetItemString(d, "PyArray_LONGLONG", PyInt_FromLong(PyArray_LONGLONG)); - PyDict_SetItemString(d, "PyArray_ULONGLONG", PyInt_FromLong(PyArray_ULONGLONG)); - PyDict_SetItemString(d, "PyArray_FLOAT", PyInt_FromLong(PyArray_FLOAT)); - PyDict_SetItemString(d, "PyArray_DOUBLE", PyInt_FromLong(PyArray_DOUBLE)); - PyDict_SetItemString(d, "PyArray_LONGDOUBLE", PyInt_FromLong(PyArray_LONGDOUBLE)); - PyDict_SetItemString(d, "PyArray_CFLOAT", PyInt_FromLong(PyArray_CFLOAT)); - PyDict_SetItemString(d, "PyArray_CDOUBLE", PyInt_FromLong(PyArray_CDOUBLE)); - PyDict_SetItemString(d, "PyArray_CLONGDOUBLE", PyInt_FromLong(PyArray_CLONGDOUBLE)); - PyDict_SetItemString(d, "PyArray_OBJECT", PyInt_FromLong(PyArray_OBJECT)); - PyDict_SetItemString(d, "PyArray_STRING", PyInt_FromLong(PyArray_STRING)); - PyDict_SetItemString(d, "PyArray_UNICODE", PyInt_FromLong(PyArray_UNICODE)); - PyDict_SetItemString(d, "PyArray_VOID", PyInt_FromLong(PyArray_VOID)); - PyDict_SetItemString(d, "PyArray_NTYPES", PyInt_FromLong(PyArray_NTYPES)); - PyDict_SetItemString(d, "PyArray_NOTYPE", PyInt_FromLong(PyArray_NOTYPE)); - PyDict_SetItemString(d, "PyArray_UDERDEF", PyInt_FromLong(PyArray_USERDEF)); - - PyDict_SetItemString(d, "CONTIGUOUS", PyInt_FromLong(CONTIGUOUS)); - PyDict_SetItemString(d, "FORTRAN", PyInt_FromLong(FORTRAN)); - PyDict_SetItemString(d, "OWNDATA", PyInt_FromLong(OWNDATA)); - PyDict_SetItemString(d, "FORCECAST", PyInt_FromLong(FORCECAST)); - PyDict_SetItemString(d, "ENSURECOPY", PyInt_FromLong(ENSURECOPY)); - PyDict_SetItemString(d, "ENSUREARRAY", PyInt_FromLong(ENSUREARRAY)); - PyDict_SetItemString(d, "ALIGNED", PyInt_FromLong(ALIGNED)); - PyDict_SetItemString(d, "WRITEABLE", PyInt_FromLong(WRITEABLE)); - PyDict_SetItemString(d, "UPDATEIFCOPY", PyInt_FromLong(UPDATEIFCOPY)); - - PyDict_SetItemString(d, "BEHAVED", PyInt_FromLong(NPY_BEHAVED)); - PyDict_SetItemString(d, "BEHAVED_NS", PyInt_FromLong(NPY_BEHAVED_NS)); - PyDict_SetItemString(d, "CARRAY", PyInt_FromLong(NPY_CARRAY)); - PyDict_SetItemString(d, "FARRAY", PyInt_FromLong(NPY_FARRAY)); - PyDict_SetItemString(d, "CARRAY_RO", PyInt_FromLong(NPY_CARRAY_RO)); - PyDict_SetItemString(d, "FARRAY_RO", PyInt_FromLong(NPY_FARRAY_RO)); - PyDict_SetItemString(d, "DEFAULT", PyInt_FromLong(NPY_DEFAULT)); - PyDict_SetItemString(d, "UPDATE_ALL", PyInt_FromLong(NPY_UPDATE_ALL)); - - if (PyErr_Occurred()) - Py_FatalError("can't initialize module wrap"); - -#ifdef F2PY_REPORT_ATEXIT - on_exit(f2py_report_on_exit,(void*)"array_from_pyobj.wrap.call"); -#endif - -} -#ifdef __cplusplus -} -#endif Deleted: trunk/numpy/f2py/tests/c/return_real.py =================================================================== --- trunk/numpy/f2py/tests/c/return_real.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/c/return_real.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,107 +0,0 @@ -__usage__ = """ -Run: - python return_real.py [] -Examples: - python return_real.py --fcompiler=Gnu --no-wrap-functions - python return_real.py --quiet -""" - -import f2py2e -from Numeric import array - -def build(f2py_opts): - try: - import c_ext_return_real - except ImportError: - assert not f2py2e.compile('''\ -python module c_ext_return_real -usercode \'\'\' -float t4(float value) { return value; } -void s4(float *t4, float value) { *t4 = value; } -double t8(double value) { return value; } -void s8(double *t8, double value) { *t8 = value; } -\'\'\' -interface - function t4(value) - real*4 intent(c) :: t4,value - end - function t8(value) - real*8 intent(c) :: t8,value - end - subroutine s4(t4,value) - intent(c) s4 - real*4 intent(out) :: t4 - real*4 intent(c) :: value - end - subroutine s8(t8,value) - intent(c) s8 - real*8 intent(out) :: t8 - real*8 intent(c) :: value - end -end interface -end python module c_ext_return_real -''','c_ext_return_real',f2py_opts,source_fn='c_ret_real.pyf') - - from c_ext_return_real import t4,t8,s4,s8 - test_functions = [t4,t8,s4,s8] - return test_functions - -def runtest(t): - import sys - if t.__doc__.split()[0] in ['t0','t4','s0','s4']: - err = 1e-5 - else: - err = 0.0 - assert abs(t(234)-234.0)<=err - assert abs(t(234.6)-234.6)<=err - assert abs(t(234l)-234.0)<=err - if sys.version[:3]<'2.3': - assert abs(t(234.6+3j)-234.6)<=err - assert abs(t('234')-234)<=err - assert abs(t('234.6')-234.6)<=err - assert abs(t(-234)+234)<=err - assert abs(t([234])-234)<=err - assert abs(t((234,))-234.)<=err - assert abs(t(array(234))-234.)<=err - assert abs(t(array([234]))-234.)<=err - assert abs(t(array([[234]]))-234.)<=err - assert abs(t(array([234],'1'))+22)<=err - assert abs(t(array([234],'s'))-234.)<=err - assert abs(t(array([234],'i'))-234.)<=err - assert abs(t(array([234],'l'))-234.)<=err - assert abs(t(array([234],'b'))-234.)<=err - assert abs(t(array([234],'f'))-234.)<=err - assert abs(t(array([234],'d'))-234.)<=err - if sys.version[:3]<'2.3': - assert abs(t(array([234+3j],'F'))-234.)<=err - assert abs(t(array([234],'D'))-234.)<=err - if t.__doc__.split()[0] in ['t0','t4','s0','s4']: - assert t(1e200)==t(1e300) # inf - - try: raise RuntimeError,`t(array([234],'c'))` - except ValueError: pass - try: raise RuntimeError,`t('abc')` - except ValueError: pass - - try: raise RuntimeError,`t([])` - except IndexError: pass - try: raise RuntimeError,`t(())` - except IndexError: pass - - try: raise RuntimeError,`t(t)` - except TypeError: pass - try: raise RuntimeError,`t({})` - except TypeError: pass - - try: - try: raise RuntimeError,`t(10l**400)` - except OverflowError: pass - except RuntimeError: - r = t(10l**400); assert `r` in ['inf','Infinity'],`r` - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py2e.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py2e.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/f77/callback.py =================================================================== --- trunk/numpy/f2py/tests/f77/callback.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f77/callback.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,98 +0,0 @@ -__usage__ = """ -Run: - python callback.py [] -Examples: - python callback.py --fcompiler=Gnu --no-wrap-functions - python callback.py --quiet -""" - -import f2py2e -import math -import sys -from Numeric import array - -def build(f2py_opts): - try: - import f77_ext_callback - except ImportError: - assert not f2py2e.compile('''\ - subroutine t(fun,a) - integer a -cf2py intent(out) a - external fun - call fun(a) - end - - subroutine func(a) -cf2py intent(in,out) a - integer a - a = a + 11 - end - - subroutine func0(a) -cf2py intent(out) a - integer a - a = 11 - end - - subroutine t2(a) -cf2py intent(callback) fun - integer a -cf2py intent(out) a - external fun - call fun(a) - end - -''','f77_ext_callback',f2py_opts,source_fn='f77_callback.f') - - from f77_ext_callback import t,t2 - test_functions = [t,t2] - return test_functions - -def runtest(t): - r = t(lambda : 4) - assert r==4,`r` - r = t(lambda a:5,fun_extra_args=(6,)) - assert r==5,`r` - r = t(lambda a:a,fun_extra_args=(6,)) - assert r==6,`r` - r = t(lambda a:5+a,fun_extra_args=(7,)) - assert r==12,`r` - if sys.version[:3]>='2.3': - r = t(lambda a:math.degrees(a),fun_extra_args=(math.pi,)) - assert r==180,`r` - r = t(math.degrees,fun_extra_args=(math.pi,)) - assert r==180,`r` - from f77_ext_callback import func,func0 - r = t(func,fun_extra_args=(6,)) - assert r==17,`r` - r = t(func0) - assert r==11,`r` - r = t(func0._cpointer) - assert r==11,`r` - class A: - def __call__(self): - return 7 - def mth(self): - return 9 - a = A() - r = t(a) - assert r==7,`r` - r = t(a.mth) - assert r==9,`r` - -if __name__=='__main__': - #import libwadpy - status = 1 - try: - repeat,f2py_opts = f2py2e.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py2e.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' - status = 0 - finally: - if status: - print '*'*20 - print 'Running f2py2e.diagnose' - import f2py2e.diagnose - f2py2e.diagnose.run() Deleted: trunk/numpy/f2py/tests/f77/return_character.py =================================================================== --- trunk/numpy/f2py/tests/f77/return_character.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f77/return_character.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,99 +0,0 @@ -__usage__ = """ -Run: - python return_character.py [] -Examples: - python return_character.py --fcompiler=Gnu --no-wrap-functions - python return_character.py --quiet -""" - -import sys -import f2py2e -from Numeric import array - -def build(f2py_opts): - try: - import f77_ext_return_character - except ImportError: - assert not f2py2e.compile('''\ - function t0(value) - character value - character t0 - t0 = value - end - function t1(value) - character*1 value - character*1 t1 - t1 = value - end - function t5(value) - character*5 value - character*5 t5 - t5 = value - end - function ts(value) - character*(*) value - character*(*) ts - ts = value - end - - subroutine s0(t0,value) - character value - character t0 -cf2py intent(out) t0 - t0 = value - end - subroutine s1(t1,value) - character*1 value - character*1 t1 -cf2py intent(out) t1 - t1 = value - end - subroutine s5(t5,value) - character*5 value - character*5 t5 -cf2py intent(out) t5 - t5 = value - end - subroutine ss(ts,value) - character*(*) value - character*10 ts -cf2py intent(out) ts - ts = value - end -''','f77_ext_return_character',f2py_opts,source_fn='f77_ret_char.f') - - from f77_ext_return_character import t0,t1,t5,s0,s1,s5,ss - test_functions = [t0,t1,t5,s0,s1,s5,ss] - if sys.platform!='win32': # this is acctually compiler dependent case - from f77_ext_return_character import ts - test_functions.append(ts) - - return test_functions - -def runtest(t): - tname = t.__doc__.split()[0] - if tname in ['t0','t1','s0','s1']: - assert t(23)=='2' - r = t('ab');assert r=='a',`r` - r = t(array('ab'));assert r=='a',`r` - r = t(array(77,'1'));assert r=='M',`r` - try: raise RuntimeError,`t(array([77,87]))` - except ValueError: pass - try: raise RuntimeError,`t(array(77))` - except ValueError: pass - elif tname in ['ts','ss']: - assert t(23)=='23 ',`t(23)` - assert t('123456789abcdef')=='123456789a' - elif tname in ['t5','s5']: - assert t(23)=='23 ',`t(23)` - assert t('ab')=='ab ',`t('ab')` - assert t('123456789abcdef')=='12345' - else: - raise NotImplementedError - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py2e.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py2e.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/f77/return_complex.py =================================================================== --- trunk/numpy/f2py/tests/f77/return_complex.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f77/return_complex.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,124 +0,0 @@ -__usage__ = """ -Run: - python return_complex.py [] -Examples: - python return_complex.py --fcompiler=Gnu --no-wrap-functions - python return_complex.py --quiet -""" - -import f2py2e -from Numeric import array - -def build(f2py_opts): - try: - import f77_ext_return_complex - except ImportError: - assert not f2py2e.compile('''\ - function t0(value) - complex value - complex t0 - t0 = value - end - function t8(value) - complex*8 value - complex*8 t8 - t8 = value - end - function t16(value) - complex*16 value - complex*16 t16 - t16 = value - end - function td(value) - double complex value - double complex td - td = value - end - - subroutine s0(t0,value) - complex value - complex t0 -cf2py intent(out) t0 - t0 = value - end - subroutine s8(t8,value) - complex*8 value - complex*8 t8 -cf2py intent(out) t8 - t8 = value - end - subroutine s16(t16,value) - complex*16 value - complex*16 t16 -cf2py intent(out) t16 - t16 = value - end - subroutine sd(td,value) - double complex value - double complex td -cf2py intent(out) td - td = value - end -''','f77_ext_return_complex',f2py_opts) - - from f77_ext_return_complex import t0,t8,t16,td,s0,s8,s16,sd - test_functions = [t0,t8,t16,td,s0,s8,s16,sd] - return test_functions - - -def runtest(t): - tname = t.__doc__.split()[0] - if tname in ['t0','t8','s0','s8']: - err = 1e-5 - else: - err = 0.0 - assert abs(t(234j)-234.0j)<=err - assert abs(t(234.6)-234.6)<=err - assert abs(t(234l)-234.0)<=err - assert abs(t(234.6+3j)-(234.6+3j))<=err - #assert abs(t('234')-234.)<=err - #assert abs(t('234.6')-234.6)<=err - assert abs(t(-234)+234.)<=err - assert abs(t([234])-234.)<=err - assert abs(t((234,))-234.)<=err - assert abs(t(array(234))-234.)<=err - assert abs(t(array(23+4j,'F'))-(23+4j))<=err - assert abs(t(array([234]))-234.)<=err - assert abs(t(array([[234]]))-234.)<=err - assert abs(t(array([234],'1'))+22.)<=err - assert abs(t(array([234],'s'))-234.)<=err - assert abs(t(array([234],'i'))-234.)<=err - assert abs(t(array([234],'l'))-234.)<=err - assert abs(t(array([234],'b'))-234.)<=err - assert abs(t(array([234],'f'))-234.)<=err - assert abs(t(array([234],'d'))-234.)<=err - assert abs(t(array([234+3j],'F'))-(234+3j))<=err - assert abs(t(array([234],'D'))-234.)<=err - - try: raise RuntimeError,`t(array([234],'c'))` - except TypeError: pass - try: raise RuntimeError,`t('abc')` - except TypeError: pass - - try: raise RuntimeError,`t([])` - except IndexError: pass - try: raise RuntimeError,`t(())` - except IndexError: pass - - try: raise RuntimeError,`t(t)` - except TypeError: pass - try: raise RuntimeError,`t({})` - except TypeError: pass - - try: - try: raise RuntimeError,`t(10l**400)` - except OverflowError: pass - except RuntimeError: - r = t(10l**400); assert `r` in ['(inf+0j)','(Infinity+0j)'],`r` - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py2e.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py2e.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/f77/return_integer.py =================================================================== --- trunk/numpy/f2py/tests/f77/return_integer.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f77/return_integer.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,147 +0,0 @@ -__usage__ = """ -Run: - python return_integer.py [] -Examples: - python return_integer.py --fcompiler=Gnu --no-wrap-functions - python return_integer.py --quiet -""" - -import numpy.f2py as f2py2e -from numpy import array - -def build(f2py_opts): - try: - import f77_ext_return_integer - except ImportError: - assert not f2py2e.compile('''\ - function t0(value) - integer value - integer t0 - t0 = value - end - function t1(value) - integer*1 value - integer*1 t1 - t1 = value - end - function t2(value) - integer*2 value - integer*2 t2 - t2 = value - end - function t4(value) - integer*4 value - integer*4 t4 - t4 = value - end - function t8(value) - integer*8 value - integer*8 t8 - t8 = value - end - - subroutine s0(t0,value) - integer value - integer t0 -cf2py intent(out) t0 - t0 = value - end - subroutine s1(t1,value) - integer*1 value - integer*1 t1 -cf2py intent(out) t1 - t1 = value - end - subroutine s2(t2,value) - integer*2 value - integer*2 t2 -cf2py intent(out) t2 - t2 = value - end - subroutine s4(t4,value) - integer*4 value - integer*4 t4 -cf2py intent(out) t4 - t4 = value - end - subroutine s8(t8,value) - integer*8 value - integer*8 t8 -cf2py intent(out) t8 - t8 = value - end - -''','f77_ext_return_integer',f2py_opts,source_fn='f77_ret_int.f') - - from f77_ext_return_integer import t0,t1,t2,t4,t8,s0,s1,s2,s4,s8 - test_functions = [t0,t1,t2,t4,t8,s0,s1,s2,s4,s8] - return test_functions - -def runtest(t): - import sys - assert t(123)==123,`t(123)` - assert t(123.6)==123 - assert t(123l)==123 - if sys.version[:3]<'2.3': - assert t(123.6+3j)==123 - assert t('123')==123 - assert t(-123)==-123 - assert t([123])==123 - assert t((123,))==123 - assert t(array(123))==123 - assert t(array([123]))==123 - assert t(array([[123]]))==123 - assert t(array([123],'b'))==123 - assert t(array([123],'h'))==123 - assert t(array([123],'i'))==123 - assert t(array([123],'l'))==123 - assert t(array([123],'B'))==123 - assert t(array([123],'f'))==123 - assert t(array([123],'d'))==123 - if sys.version[:3]<'2.3': - assert t(array([123+3j],'F'))==123 - assert t(array([123],'D'))==123 - - - try: raise RuntimeError,`t(array([123],'c'))` - except ValueError: pass - try: raise RuntimeError,`t('abc')` - except ValueError: pass - - try: raise RuntimeError,`t([])` - except IndexError: pass - try: raise RuntimeError,`t(())` - except IndexError: pass - - try: raise RuntimeError,`t(t)` - except TypeError: pass - try: raise RuntimeError,`t({})` - except TypeError: pass - - if t.__doc__.split()[0] in ['t8','s8']: - try: raise RuntimeError,`t(100000000000000000000000l)` - except OverflowError: pass - try: raise RuntimeError,`t(10000000011111111111111.23)` - except OverflowError: pass - else: - if sys.version[:3]<'2.3': - try: raise RuntimeError,`t(10000000000000l)` - except OverflowError: pass - try: raise RuntimeError,`t(10000000000.23)` - except OverflowError: pass - -if __name__=='__main__': - #import libwadpy - status = 1 - try: - repeat,f2py_opts = f2py2e.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py2e.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' - status = 0 - finally: - if status: - print '*'*20 - print 'Running f2py2e.diagnose' - import numpy.f2py.diagnose as diagnose - #diagnose.run() Deleted: trunk/numpy/f2py/tests/f77/return_logical.py =================================================================== --- trunk/numpy/f2py/tests/f77/return_logical.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f77/return_logical.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,133 +0,0 @@ -__usage__ = """ -Run: - python return_logical.py [] -Examples: - python return_logical.py --fcompiler=Gnu --no-wrap-functions - python return_logical.py --quiet -""" - -import f2py2e -from Numeric import array -try: True -except NameError: - True = 1 - False = 0 - -def build(f2py_opts): - try: - import f77_ext_return_logical - except ImportError: - assert not f2py2e.compile('''\ - function t0(value) - logical value - logical t0 - t0 = value - end - function t1(value) - logical*1 value - logical*1 t1 - t1 = value - end - function t2(value) - logical*2 value - logical*2 t2 - t2 = value - end - function t4(value) - logical*4 value - logical*4 t4 - t4 = value - end -c function t8(value) -c logical*8 value -c logical*8 t8 -c t8 = value -c end - - subroutine s0(t0,value) - logical value - logical t0 -cf2py intent(out) t0 - t0 = value - end - subroutine s1(t1,value) - logical*1 value - logical*1 t1 -cf2py intent(out) t1 - t1 = value - end - subroutine s2(t2,value) - logical*2 value - logical*2 t2 -cf2py intent(out) t2 - t2 = value - end - subroutine s4(t4,value) - logical*4 value - logical*4 t4 -cf2py intent(out) t4 - t4 = value - end -c subroutine s8(t8,value) -c logical*8 value -c logical*8 t8 -cf2py intent(out) t8 -c t8 = value -c end -''','f77_ext_return_logical',f2py_opts) - - #from f77_ext_return_logical import t0,t1,t2,t4,t8,s0,s1,s2,s4,s8 - #test_functions = [t0,t1,t2,t4,t8,s0,s1,s2,s4,s8] - from f77_ext_return_logical import t0,t1,t2,t4,s0,s1,s2,s4 - test_functions = [t0,t1,t2,t4,s0,s1,s2,s4] - return test_functions - -def runtest(t): - assert t(True)==1,`t(True)` - assert t(False)==0,`t(False)` - assert t(0)==0 - assert t(None)==0 - assert t(0.0)==0 - assert t(0j)==0 - assert t(1j)==1 - assert t(234)==1 - assert t(234.6)==1 - assert t(234l)==1 - assert t(234.6+3j)==1 - assert t('234')==1 - assert t('aaa')==1 - assert t('')==0 - assert t([])==0 - assert t(())==0 - assert t({})==0 - assert t(t)==1 - assert t(-234)==1 - assert t(10l**100)==1 - assert t([234])==1 - assert t((234,))==1 - assert t(array(234))==1 - assert t(array([234]))==1 - assert t(array([[234]]))==1 - assert t(array([234],'1'))==1 - assert t(array([234],'s'))==1 - assert t(array([234],'i'))==1 - assert t(array([234],'l'))==1 - assert t(array([234],'b'))==1 - assert t(array([234],'f'))==1 - assert t(array([234],'d'))==1 - assert t(array([234+3j],'F'))==1 - assert t(array([234],'D'))==1 - assert t(array(0))==0 - assert t(array([0]))==0 - assert t(array([[0]]))==0 - assert t(array([0j]))==0 - assert t(array([1]))==1 - assert t(array([0,0]))==0 - assert t(array([0,1]))==1 #XXX: is this expected? - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py2e.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py2e.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/f77/return_real.py =================================================================== --- trunk/numpy/f2py/tests/f77/return_real.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f77/return_real.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,126 +0,0 @@ -__usage__ = """ -Run: - python return_real.py [] -Examples: - python return_real.py --fcompiler=Gnu --no-wrap-functions - python return_real.py --quiet -""" - -import numpy.f2py as f2py2e -from numpy import array - -def build(f2py_opts): - try: - import f77_ext_return_real - except ImportError: - assert not f2py2e.compile('''\ - function t0(value) - real value - real t0 - t0 = value - end - function t4(value) - real*4 value - real*4 t4 - t4 = value - end - function t8(value) - real*8 value - real*8 t8 - t8 = value - end - function td(value) - double precision value - double precision td - td = value - end - - subroutine s0(t0,value) - real value - real t0 -cf2py intent(out) t0 - t0 = value - end - subroutine s4(t4,value) - real*4 value - real*4 t4 -cf2py intent(out) t4 - t4 = value - end - subroutine s8(t8,value) - real*8 value - real*8 t8 -cf2py intent(out) t8 - t8 = value - end - subroutine sd(td,value) - double precision value - double precision td -cf2py intent(out) td - td = value - end -''','f77_ext_return_real',f2py_opts,source_fn='f77_ret_real.f') - - from f77_ext_return_real import t0,t4,t8,td,s0,s4,s8,sd - test_functions = [t0,t4,t8,td,s0,s4,s8,sd] - return test_functions - -def runtest(t): - import sys - if t.__doc__.split()[0] in ['t0','t4','s0','s4']: - err = 1e-5 - else: - err = 0.0 - assert abs(t(234)-234.0)<=err - assert abs(t(234.6)-234.6)<=err - assert abs(t(234l)-234.0)<=err - if sys.version[:3]<'2.3': - assert abs(t(234.6+3j)-234.6)<=err - assert abs(t('234')-234)<=err - assert abs(t('234.6')-234.6)<=err - assert abs(t(-234)+234)<=err - assert abs(t([234])-234)<=err - assert abs(t((234,))-234.)<=err - assert abs(t(array(234))-234.)<=err - assert abs(t(array([234]))-234.)<=err - assert abs(t(array([[234]]))-234.)<=err - assert abs(t(array([234],'b'))+22)<=err - assert abs(t(array([234],'h'))-234.)<=err - assert abs(t(array([234],'i'))-234.)<=err - assert abs(t(array([234],'l'))-234.)<=err - assert abs(t(array([234],'B'))-234.)<=err - assert abs(t(array([234],'f'))-234.)<=err - assert abs(t(array([234],'d'))-234.)<=err - if sys.version[:3]<'2.3': - assert abs(t(array([234+3j],'F'))-234.)<=err - assert abs(t(array([234],'D'))-234.)<=err - if t.__doc__.split()[0] in ['t0','t4','s0','s4']: - assert t(1e200)==t(1e300) # inf - - try: raise RuntimeError,`t(array([234],'c'))` - except ValueError: pass - try: raise RuntimeError,`t('abc')` - except ValueError: pass - - try: raise RuntimeError,`t([])` - except IndexError: pass - try: raise RuntimeError,`t(())` - except IndexError: pass - - try: raise RuntimeError,`t(t)` - except TypeError: pass - try: raise RuntimeError,`t({})` - except TypeError: pass - - try: - try: raise RuntimeError,`t(10l**400)` - except OverflowError: pass - except RuntimeError: - r = t(10l**400); assert `r` in ['inf','Infinity'],`r` - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py2e.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py2e.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/f90/return_character.py =================================================================== --- trunk/numpy/f2py/tests/f90/return_character.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f90/return_character.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,105 +0,0 @@ -__usage__ = """ -Run: - python return_character.py [] -Examples: - python return_character.py --fcompiler=Gnu --no-wrap-functions - python return_character.py --quiet -""" - -import numpy.f2py as f2py -from numpy import array - -def build(f2py_opts): - try: - import f90_ext_return_character - except ImportError: - assert not f2py.compile('''\ -module f90_return_char - contains - function t0(value) - character :: value - character :: t0 - t0 = value - end function t0 - function t1(value) - character(len=1) :: value - character(len=1) :: t1 - t1 = value - end function t1 - function t5(value) - character(len=5) :: value - character(len=5) :: t5 - t5 = value - end function t5 - function ts(value) - character(len=*) :: value - character(len=10) :: ts - ts = value - end function ts - - subroutine s0(t0,value) - character :: value - character :: t0 -!f2py intent(out) t0 - t0 = value - end subroutine s0 - subroutine s1(t1,value) - character(len=1) :: value - character(len=1) :: t1 -!f2py intent(out) t1 - t1 = value - end subroutine s1 - subroutine s5(t5,value) - character(len=5) :: value - character(len=5) :: t5 -!f2py intent(out) t5 - t5 = value - end subroutine s5 - subroutine ss(ts,value) - character(len=*) :: value - character(len=10) :: ts -!f2py intent(out) ts - ts = value - end subroutine ss -end module f90_return_char -''','f90_ext_return_character',f2py_opts,source_fn='f90_ret_char.f90') - - from f90_ext_return_character import f90_return_char as m - test_functions = [m.t0,m.t1,m.t5,m.ts,m.s0,m.s1,m.s5,m.ss] - return test_functions - - -def runtest(t): - tname = t.__doc__.split()[0] - if tname in ['t0','t1','s0','s1']: - assert t(23)=='2' - r = t('ab');assert r=='a',`r` - r = t(array('ab'));assert r=='a',`r` - r = t(array(77,'l'));assert r=='M',`r` - - try: raise RuntimeError,`t(array([77,87]))` - except ValueError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 1" - - try: raise RuntimeError,`t(array(77))` - except ValueError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 2" - - elif tname in ['ts','ss']: - assert t(23)=='23 ',`t(23)` - assert t('123456789abcdef')=='123456789a',`t('123456789abcdef')` - elif tname in ['t5','s5']: - assert t(23)=='23 ' - assert t('ab')=='ab ' - assert t('123456789abcdef')=='12345' - else: - raise NotImplementedError - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/f90/return_complex.py =================================================================== --- trunk/numpy/f2py/tests/f90/return_complex.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f90/return_complex.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,141 +0,0 @@ -__usage__ = """ -Run: - python return_complex.py [] -Examples: - python return_complex.py --quiet -""" - -import numpy.f2py as f2py -from numpy import array - -def build(f2py_opts): - try: - import f90_ext_return_complex - except ImportError: - assert not f2py.compile('''\ -module f90_return_complex - contains - function t0(value) - complex :: value - complex :: t0 - t0 = value - end function t0 - function t8(value) - complex(kind=4) :: value - complex(kind=4) :: t8 - t8 = value - end function t8 - function t16(value) - complex(kind=8) :: value - complex(kind=8) :: t16 - t16 = value - end function t16 - function td(value) - double complex :: value - double complex :: td - td = value - end function td - - subroutine s0(t0,value) - complex :: value - complex :: t0 -!f2py intent(out) t0 - t0 = value - end subroutine s0 - subroutine s8(t8,value) - complex(kind=4) :: value - complex(kind=4) :: t8 -!f2py intent(out) t8 - t8 = value - end subroutine s8 - subroutine s16(t16,value) - complex(kind=8) :: value - complex(kind=8) :: t16 -!f2py intent(out) t16 - t16 = value - end subroutine s16 - subroutine sd(td,value) - double complex :: value - double complex :: td -!f2py intent(out) td - td = value - end subroutine sd -end module f90_return_complex -''','f90_ext_return_complex',f2py_opts,source_fn='f90_ret_cmlx.f90') - - from f90_ext_return_complex import f90_return_complex as m - test_functions = [m.t0,m.t8,m.t16,m.td,m.s0,m.s8,m.s16,m.sd] - return test_functions - - -def runtest(t): - tname = t.__doc__.split()[0] - if tname in ['t0','t8','s0','s8']: - err = 1e-5 - else: - err = 0.0 - #assert abs(t(234j)-234.0j)<=err - assert abs(t(234.6)-234.6)<=err - assert abs(t(234l)-234.0)<=err - assert abs(t(234.6+3j)-(234.6+3j))<=err - #assert abs(t('234')-234.)<=err - #assert abs(t('234.6')-234.6)<=err - assert abs(t(-234)+234.)<=err - assert abs(t([234])-234.)<=err - assert abs(t((234,))-234.)<=err - assert abs(t(array(234))-234.)<=err - assert abs(t(array(23+4j,'F'))-(23+4j))<=err - assert abs(t(array([234]))-234.)<=err - assert abs(t(array([[234]]))-234.)<=err - assert abs(t(array([234],'b'))+22.)<=err - assert abs(t(array([234],'h'))-234.)<=err - assert abs(t(array([234],'i'))-234.)<=err - assert abs(t(array([234],'l'))-234.)<=err - assert abs(t(array([234],'q'))-234.)<=err - assert abs(t(array([234],'f'))-234.)<=err - assert abs(t(array([234],'d'))-234.)<=err - assert abs(t(array([234+3j],'F'))-(234+3j))<=err - assert abs(t(array([234],'D'))-234.)<=err - - try: raise RuntimeError,`t(array([234],'c'))` - except TypeError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 1" - - try: raise RuntimeError,`t('abc')` - except TypeError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 2" - - try: raise RuntimeError,`t([])` - except IndexError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 3" - - try: raise RuntimeError,`t(())` - except IndexError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 4" - - try: raise RuntimeError,`t(t)` - except TypeError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 5" - - try: raise RuntimeError,`t({})` - except TypeError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 6" - - try: - try: raise RuntimeError,`t(10l**400)` - except OverflowError: pass - except RuntimeError: - r = t(10l**400); assert `r` in ['(inf+0j)','(Infinity+0j)'],`r` - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/f90/return_integer.py =================================================================== --- trunk/numpy/f2py/tests/f90/return_integer.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f90/return_integer.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,165 +0,0 @@ -# XXX: investigate cases that are disabled under win32 -# - -__usage__ = """ -Run: - python return_integer.py [] -Examples: - python return_integer.py --quiet -""" - -import sys -import numpy.f2py as f2py -from numpy import array - -def build(f2py_opts): - try: - import f90_ext_return_integer - except ImportError: - assert not f2py.compile('''\ -module f90_return_integer - contains - function t0(value) - integer :: value - integer :: t0 - t0 = value - end function t0 - function t1(value) - integer(kind=1) :: value - integer(kind=1) :: t1 - t1 = value - end function t1 - function t2(value) - integer(kind=2) :: value - integer(kind=2) :: t2 - t2 = value - end function t2 - function t4(value) - integer(kind=4) :: value - integer(kind=4) :: t4 - t4 = value - end function t4 - function t8(value) - integer(kind=8) :: value - integer(kind=8) :: t8 - t8 = value - end function t8 - - subroutine s0(t0,value) - integer :: value - integer :: t0 -!f2py intent(out) t0 - t0 = value - end subroutine s0 - subroutine s1(t1,value) - integer(kind=1) :: value - integer(kind=1) :: t1 -!f2py intent(out) t1 - t1 = value - end subroutine s1 - subroutine s2(t2,value) - integer(kind=2) :: value - integer(kind=2) :: t2 -!f2py intent(out) t2 - t2 = value - end subroutine s2 - subroutine s4(t4,value) - integer(kind=4) :: value - integer(kind=4) :: t4 -!f2py intent(out) t4 - t4 = value - end subroutine s4 - subroutine s8(t8,value) - integer(kind=8) :: value - integer(kind=8) :: t8 -!f2py intent(out) t8 - t8 = value - end subroutine s8 -end module f90_return_integer -''','f90_ext_return_integer',f2py_opts,source_fn='f90_ret_int.f90') - - from f90_ext_return_integer import f90_return_integer as m - test_functions = [m.t0,m.t1,m.t2,m.t4,m.t8,m.s0,m.s1,m.s2,m.s4,m.s8] - return test_functions - -def runtest(t): - tname = t.__doc__.split()[0] - assert t(123)==123 - assert t(123.6)==123 - assert t(123l)==123 - if sys.version[:3]<='2.2': - assert t(123.6+3j)==123 - assert t('123')==123 - assert t(-123)==-123 - assert t([123])==123 - assert t((123,))==123 - assert t(array(123))==123 - assert t(array([123]))==123 - assert t(array([[123]]))==123 - assert t(array([123],'b'))==123 - assert t(array([123],'h'))==123 - assert t(array([123],'i'))==123 - assert t(array([123],'l'))==123 - assert t(array([123],'q'))==123 - assert t(array([123],'f'))==123 - assert t(array([123],'d'))==123 - if sys.version[:3]<='2.2': - assert t(array([123+3j],'F'))==123 - assert t(array([123],'D'))==123 - - try: raise RuntimeError,`t(array([123],'c'))` - except ValueError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 1" - - try: raise RuntimeError,`t('abc')` - except ValueError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 2" - - try: raise RuntimeError,`t([])` - except IndexError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 3" - - try: raise RuntimeError,`t(())` - except IndexError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 4" - - try: raise RuntimeError,`t(t)` - except TypeError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 5" - - try: raise RuntimeError,`t({})` - except TypeError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 6" - - if tname in ['t8','s8']: - try: raise RuntimeError,`t(100000000000000000000000l)` - except OverflowError: pass - try: raise RuntimeError,`t(10000000011111111111111.23)` - except OverflowError: pass - else: - if sys.version[:3]<='2.2': - try: raise RuntimeError,`t(10000000000000l)` - except OverflowError: pass - try: raise RuntimeError,`t(10000000000.23)` - except OverflowError: pass - -if __name__=='__main__': - #import libwadpy - status = 1 - try: - repeat,f2py_opts = f2py.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' - status = 0 - finally: - if status: - print '*'*20 - print 'Running f2py.diagnose' - f2py.diagnose.run() Deleted: trunk/numpy/f2py/tests/f90/return_logical.py =================================================================== --- trunk/numpy/f2py/tests/f90/return_logical.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f90/return_logical.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,138 +0,0 @@ -__usage__ = """ -Run: - python return_logical.py [] -Examples: - python return_logical.py --quiet -""" - -import numpy.f2py as f2py -from numpy import array - -try: True -except NameError: - True = 1 - False = 0 - -def build(f2py_opts): - try: - import f90_ext_return_logical - except ImportError: - assert not f2py.compile('''\ -module f90_return_logical - contains - function t0(value) - logical :: value - logical :: t0 - t0 = value - end function t0 - function t1(value) - logical(kind=1) :: value - logical(kind=1) :: t1 - t1 = value - end function t1 - function t2(value) - logical(kind=2) :: value - logical(kind=2) :: t2 - t2 = value - end function t2 - function t4(value) - logical(kind=4) :: value - logical(kind=4) :: t4 - t4 = value - end function t4 - function t8(value) - logical(kind=8) :: value - logical(kind=8) :: t8 - t8 = value - end function t8 - - subroutine s0(t0,value) - logical :: value - logical :: t0 -!f2py intent(out) t0 - t0 = value - end subroutine s0 - subroutine s1(t1,value) - logical(kind=1) :: value - logical(kind=1) :: t1 -!f2py intent(out) t1 - t1 = value - end subroutine s1 - subroutine s2(t2,value) - logical(kind=2) :: value - logical(kind=2) :: t2 -!f2py intent(out) t2 - t2 = value - end subroutine s2 - subroutine s4(t4,value) - logical(kind=4) :: value - logical(kind=4) :: t4 -!f2py intent(out) t4 - t4 = value - end subroutine s4 - subroutine s8(t8,value) - logical(kind=8) :: value - logical(kind=8) :: t8 -!f2py intent(out) t8 - t8 = value - end subroutine s8 -end module f90_return_logical -''','f90_ext_return_logical',f2py_opts,source_fn='f90_ret_log.f90') - - from f90_ext_return_logical import f90_return_logical as m - test_functions = [m.t0,m.t1,m.t2,m.t4,m.t8,m.s0,m.s1,m.s2,m.s4,m.s8] - return test_functions - - - - -def runtest(t): - assert t(True)==1,`t(True)` - assert t(False)==0,`t(False)` - assert t(0)==0 - assert t(None)==0 - assert t(0.0)==0 - assert t(0j)==0 - assert t(1j)==1 - assert t(234)==1 - assert t(234.6)==1 - assert t(234l)==1 - assert t(234.6+3j)==1 - assert t('234')==1 - assert t('aaa')==1 - assert t('')==0 - assert t([])==0 - assert t(())==0 - assert t({})==0 - assert t(t)==1 - assert t(-234)==1 - assert t(10l**100)==1 - assert t([234])==1 - assert t((234,))==1 - assert t(array(234))==1 - assert t(array([234]))==1 - assert t(array([[234]]))==1 - assert t(array([234],'b'))==1 - assert t(array([234],'h'))==1 - assert t(array([234],'i'))==1 - assert t(array([234],'l'))==1 - assert t(array([234],'q'))==1 - assert t(array([234],'f'))==1 - assert t(array([234],'d'))==1 - assert t(array([234+3j],'F'))==1 - assert t(array([234],'D'))==1 - assert t(array(0))==0 - assert t(array([0]))==0 - assert t(array([[0]]))==0 - assert t(array([0j]))==0 - assert t(array([1]))==1 - # The call itself raises an error. - #assert t(array([0,0])) == 0 # fails - #assert t(array([1,1])) == 1 # fails - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/f90/return_real.py =================================================================== --- trunk/numpy/f2py/tests/f90/return_real.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/f90/return_real.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,144 +0,0 @@ -__usage__ = """ -Run: - python return_real.py [] -Examples: - python return_real.py --quiet -""" - -import sys -import numpy.f2py as f2py -from numpy import array - -def build(f2py_opts): - try: - import f90_ext_return_real - except ImportError: - assert not f2py.compile('''\ -module f90_return_real - contains - function t0(value) - real :: value - real :: t0 - t0 = value - end function t0 - function t4(value) - real(kind=4) :: value - real(kind=4) :: t4 - t4 = value - end function t4 - function t8(value) - real(kind=8) :: value - real(kind=8) :: t8 - t8 = value - end function t8 - function td(value) - double precision :: value - double precision :: td - td = value - end function td - - subroutine s0(t0,value) - real :: value - real :: t0 -!f2py intent(out) t0 - t0 = value - end subroutine s0 - subroutine s4(t4,value) - real(kind=4) :: value - real(kind=4) :: t4 -!f2py intent(out) t4 - t4 = value - end subroutine s4 - subroutine s8(t8,value) - real(kind=8) :: value - real(kind=8) :: t8 -!f2py intent(out) t8 - t8 = value - end subroutine s8 - subroutine sd(td,value) - double precision :: value - double precision :: td -!f2py intent(out) td - td = value - end subroutine sd -end module f90_return_real -''','f90_ext_return_real',f2py_opts,source_fn='f90_ret_real.f90') - - from f90_ext_return_real import f90_return_real as m - test_functions = [m.t0,m.t4,m.t8,m.td,m.s0,m.s4,m.s8,m.sd] - return test_functions - -def runtest(t): - tname = t.__doc__.split()[0] - if tname in ['t0','t4','s0','s4']: - err = 1e-5 - else: - err = 0.0 - assert abs(t(234)-234.0)<=err - assert abs(t(234.6)-234.6)<=err - assert abs(t(234l)-234.0)<=err - if sys.version[:3]<='2.2': - assert abs(t(234.6+3j)-234.6)<=err,`t(234.6+3j)` - assert abs(t('234')-234)<=err - assert abs(t('234.6')-234.6)<=err - assert abs(t(-234)+234)<=err - assert abs(t([234])-234)<=err - assert abs(t((234,))-234.)<=err - assert abs(t(array(234))-234.)<=err - assert abs(t(array([234]))-234.)<=err - assert abs(t(array([[234]]))-234.)<=err - assert abs(t(array([234],'b'))+22)<=err - assert abs(t(array([234],'h'))-234.)<=err - assert abs(t(array([234],'i'))-234.)<=err - assert abs(t(array([234],'l'))-234.)<=err - assert abs(t(array([234],'q'))-234.)<=err - assert abs(t(array([234],'f'))-234.)<=err - assert abs(t(array([234],'d'))-234.)<=err - if sys.version[:3]<='2.2': - assert abs(t(array([234+3j],'F'))-234.)<=err,`t(array([234+3j],'F'))` - assert abs(t(array([234],'D'))-234.)<=err,`t(array([234],'D'))` - if tname in ['t0','t4','s0','s4']: - assert t(1e200)==t(1e300) # inf - - try: raise RuntimeError,`t(array([234],'c'))` - except ValueError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 1" - - try: raise RuntimeError,`t('abc')` - except ValueError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 2" - - try: raise RuntimeError,`t([])` - except IndexError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 3" - - try: raise RuntimeError,`t(())` - except IndexError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 4" - - try: raise RuntimeError,`t(t)` - except TypeError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 5" - - try: raise RuntimeError,`t({})` - except TypeError: pass - except RuntimeError: print "Failed Error" - except: print "Wrong Error Type 6" - - try: - try: raise RuntimeError,`t(10l**400)` - except OverflowError: pass - except RuntimeError: - r = t(10l**400); assert `r` in ['inf','Infinity'],`r` - -if __name__=='__main__': - #import libwadpy - repeat,f2py_opts = f2py.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/mixed/foo.f =================================================================== --- trunk/numpy/f2py/tests/mixed/foo.f 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/mixed/foo.f 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,5 +0,0 @@ - subroutine bar11(a) -cf2py intent(out) a - integer a - a = 11 - end Deleted: trunk/numpy/f2py/tests/mixed/foo_fixed.f90 =================================================================== --- trunk/numpy/f2py/tests/mixed/foo_fixed.f90 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/mixed/foo_fixed.f90 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,8 +0,0 @@ - module foo_fixed - contains - subroutine bar12(a) -!f2py intent(out) a - integer a - a = 12 - end subroutine bar12 - end module foo_fixed Deleted: trunk/numpy/f2py/tests/mixed/foo_free.f90 =================================================================== --- trunk/numpy/f2py/tests/mixed/foo_free.f90 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/mixed/foo_free.f90 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,8 +0,0 @@ -module foo_free -contains - subroutine bar13(a) - !f2py intent(out) a - integer a - a = 13 - end subroutine bar13 -end module foo_free Deleted: trunk/numpy/f2py/tests/mixed/run.py =================================================================== --- trunk/numpy/f2py/tests/mixed/run.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/mixed/run.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,49 +0,0 @@ -#!/usr/bin/env python -__usage__ = """ -Run: - python run.py [] -Examples: - python run.py --quiet -""" - -import os -import sys -import f2py2e -from Numeric import array - -def build(f2py_opts): - try: - import mixed_f77_f90 - except: - d,b=os.path.split(sys.argv[0]) - files = ['foo.f','foo_fixed.f90','foo_free.f90'] - files = [os.path.join(d,f) for f in files] - files = ' '.join(files) - args = ' -c -m mixed_f77_f90 %s %s'%(files,f2py_opts) - c = '%s -c "import f2py2e;f2py2e.main()" %s' %(sys.executable,args) - s = os.system(c) - assert not s - from mixed_f77_f90 import bar11 - test_functions = [bar11] - from mixed_f77_f90 import foo_fixed as m - test_functions.append(m.bar12) - from mixed_f77_f90 import foo_free as m - test_functions.append(m.bar13) - return test_functions - -def runtest(t): - tname = t.__doc__.split()[0] - if tname=='bar11': - assert t()==11 - elif tname=='bar12': - assert t()==12 - elif tname=='bar13': - assert t()==13 - else: - raise NotImplementedError - -if __name__=='__main__': - repeat,f2py_opts = f2py2e.f2py_testing.cmdline() - test_functions = build(f2py_opts) - f2py2e.f2py_testing.run(runtest,test_functions,repeat) - print 'ok' Deleted: trunk/numpy/f2py/tests/run_all.py =================================================================== --- trunk/numpy/f2py/tests/run_all.py 2010-03-04 00:45:44 UTC (rev 8272) +++ trunk/numpy/f2py/tests/run_all.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -1,55 +0,0 @@ -#!/usr/bin/env python - -import os,sys - -opts = sys.argv[1:] -if not opts: - opts = ['10','--quiet'] - -NUMARRAY = "-DNUMARRAY" in sys.argv - -test_f77_files = [\ - 'f77/return_integer.py', - 'f77/return_logical.py', - 'f77/return_real.py', - 'f77/return_complex.py', - 'f77/callback.py', - ] - -if not NUMARRAY: # no support for character yet in numarray - test_f77_files.append('f77/return_character.py') - -test_f90_files = [\ - 'f90/return_integer.py', - 'f90/return_logical.py', - 'f90/return_real.py', - 'f90/return_complex.py', - 'f90/return_character.py', - 'mixed/run.py', - ] - -test_files = test_f77_files - -if NUMARRAY: - print >>sys.stderr,"NOTE: f2py for numarray does not support"\ - " f90 or character arrays." -else: - test_files += test_f90_files - -py_path = os.environ.get('PYTHONPATH') -if py_path is None: - py_path = '.' -else: - py_path = os.pathsep.join(['.',py_path]) -os.environ['PYTHONPATH'] = py_path - -for f in test_files: - print "**********************************************" - ff = os.path.join(sys.path[0],f) - args = [sys.executable,ff]+opts - print "Running",' '.join(args) - status = os.spawnve(os.P_WAIT,sys.executable,args,os.environ) - if status: - print 'TEST FAILURE (status=%s)' % (status) - if f=='f90/return_integer.py': - sys.exit() Copied: trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c (from rev 8271, trunk/numpy/f2py/tests/array_from_pyobj/wrapmodule.c) =================================================================== --- trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c (rev 0) +++ trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,196 @@ +/* File: wrapmodule.c + * This file is auto-generated with f2py (version:2_1330). + * Hand edited by Pearu. + * f2py is a Fortran to Python Interface Generator (FPIG), Second Edition, + * written by Pearu Peterson . + * See http://cens.ioc.ee/projects/f2py2e/ + * Generation date: Fri Oct 21 22:41:12 2005 + * $Revision:$ + * $Date:$ + * Do not edit this file directly unless you know what you are doing!!! + */ +#ifdef __cplusplus +extern "C" { +#endif + +/*********************** See f2py2e/cfuncs.py: includes ***********************/ +#include "Python.h" +#include "fortranobject.h" +#include + +static PyObject *wrap_error; +static PyObject *wrap_module; + +/************************************ call ************************************/ +static char doc_f2py_rout_wrap_call[] = "\ +Function signature:\n\ + arr = call(type_num,dims,intent,obj)\n\ +Required arguments:\n" +" type_num : input int\n" +" dims : input int-sequence\n" +" intent : input int\n" +" obj : input python object\n" +"Return objects:\n" +" arr : array"; +static PyObject *f2py_rout_wrap_call(PyObject *capi_self, + PyObject *capi_args) { + PyObject * volatile capi_buildvalue = NULL; + int type_num = 0; + npy_intp *dims = NULL; + PyObject *dims_capi = Py_None; + int rank = 0; + int intent = 0; + PyArrayObject *capi_arr_tmp = NULL; + PyObject *arr_capi = Py_None; + int i; + + if (!PyArg_ParseTuple(capi_args,"iOiO|:wrap.call",\ + &type_num,&dims_capi,&intent,&arr_capi)) + return NULL; + rank = PySequence_Length(dims_capi); + dims = malloc(rank*sizeof(npy_intp)); + for (i=0;idata); + dimensions = PyTuple_New(arr->nd); + strides = PyTuple_New(arr->nd); + for (i=0;ind;++i) { + PyTuple_SetItem(dimensions,i,PyInt_FromLong(arr->dimensions[i])); + PyTuple_SetItem(strides,i,PyInt_FromLong(arr->strides[i])); + } + return Py_BuildValue("siOOO(cciii)ii",s,arr->nd, + dimensions,strides, + (arr->base==NULL?Py_None:arr->base), + arr->descr->kind, + arr->descr->type, + arr->descr->type_num, + arr->descr->elsize, + arr->descr->alignment, + arr->flags, + PyArray_ITEMSIZE(arr)); +} + +static PyMethodDef f2py_module_methods[] = { + + {"call",f2py_rout_wrap_call,METH_VARARGS,doc_f2py_rout_wrap_call}, + {"array_attrs",f2py_rout_wrap_attrs,METH_VARARGS,doc_f2py_rout_wrap_attrs}, + {NULL,NULL} +}; + +PyMODINIT_FUNC inittest_array_from_pyobj_ext(void) { + PyObject *m,*d, *s; + m = wrap_module = Py_InitModule("test_array_from_pyobj_ext", f2py_module_methods); + PyFortran_Type.ob_type = &PyType_Type; + import_array(); + if (PyErr_Occurred()) + Py_FatalError("can't initialize module wrap (failed to import numpy)"); + d = PyModule_GetDict(m); + s = PyString_FromString("This module 'wrap' is auto-generated with f2py (version:2_1330).\nFunctions:\n" +" arr = call(type_num,dims,intent,obj)\n" +"."); + PyDict_SetItemString(d, "__doc__", s); + wrap_error = PyErr_NewException ("wrap.error", NULL, NULL); + Py_DECREF(s); + PyDict_SetItemString(d, "F2PY_INTENT_IN", PyInt_FromLong(F2PY_INTENT_IN)); + PyDict_SetItemString(d, "F2PY_INTENT_INOUT", PyInt_FromLong(F2PY_INTENT_INOUT)); + PyDict_SetItemString(d, "F2PY_INTENT_OUT", PyInt_FromLong(F2PY_INTENT_OUT)); + PyDict_SetItemString(d, "F2PY_INTENT_HIDE", PyInt_FromLong(F2PY_INTENT_HIDE)); + PyDict_SetItemString(d, "F2PY_INTENT_CACHE", PyInt_FromLong(F2PY_INTENT_CACHE)); + PyDict_SetItemString(d, "F2PY_INTENT_COPY", PyInt_FromLong(F2PY_INTENT_COPY)); + PyDict_SetItemString(d, "F2PY_INTENT_C", PyInt_FromLong(F2PY_INTENT_C)); + PyDict_SetItemString(d, "F2PY_OPTIONAL", PyInt_FromLong(F2PY_OPTIONAL)); + PyDict_SetItemString(d, "F2PY_INTENT_INPLACE", PyInt_FromLong(F2PY_INTENT_INPLACE)); + PyDict_SetItemString(d, "PyArray_BOOL", PyInt_FromLong(PyArray_BOOL)); + PyDict_SetItemString(d, "PyArray_BYTE", PyInt_FromLong(PyArray_BYTE)); + PyDict_SetItemString(d, "PyArray_UBYTE", PyInt_FromLong(PyArray_UBYTE)); + PyDict_SetItemString(d, "PyArray_SHORT", PyInt_FromLong(PyArray_SHORT)); + PyDict_SetItemString(d, "PyArray_USHORT", PyInt_FromLong(PyArray_USHORT)); + PyDict_SetItemString(d, "PyArray_INT", PyInt_FromLong(PyArray_INT)); + PyDict_SetItemString(d, "PyArray_UINT", PyInt_FromLong(PyArray_UINT)); + PyDict_SetItemString(d, "PyArray_INTP", PyInt_FromLong(PyArray_INTP)); + PyDict_SetItemString(d, "PyArray_UINTP", PyInt_FromLong(PyArray_UINTP)); + PyDict_SetItemString(d, "PyArray_LONG", PyInt_FromLong(PyArray_LONG)); + PyDict_SetItemString(d, "PyArray_ULONG", PyInt_FromLong(PyArray_ULONG)); + PyDict_SetItemString(d, "PyArray_LONGLONG", PyInt_FromLong(PyArray_LONGLONG)); + PyDict_SetItemString(d, "PyArray_ULONGLONG", PyInt_FromLong(PyArray_ULONGLONG)); + PyDict_SetItemString(d, "PyArray_FLOAT", PyInt_FromLong(PyArray_FLOAT)); + PyDict_SetItemString(d, "PyArray_DOUBLE", PyInt_FromLong(PyArray_DOUBLE)); + PyDict_SetItemString(d, "PyArray_LONGDOUBLE", PyInt_FromLong(PyArray_LONGDOUBLE)); + PyDict_SetItemString(d, "PyArray_CFLOAT", PyInt_FromLong(PyArray_CFLOAT)); + PyDict_SetItemString(d, "PyArray_CDOUBLE", PyInt_FromLong(PyArray_CDOUBLE)); + PyDict_SetItemString(d, "PyArray_CLONGDOUBLE", PyInt_FromLong(PyArray_CLONGDOUBLE)); + PyDict_SetItemString(d, "PyArray_OBJECT", PyInt_FromLong(PyArray_OBJECT)); + PyDict_SetItemString(d, "PyArray_STRING", PyInt_FromLong(PyArray_STRING)); + PyDict_SetItemString(d, "PyArray_UNICODE", PyInt_FromLong(PyArray_UNICODE)); + PyDict_SetItemString(d, "PyArray_VOID", PyInt_FromLong(PyArray_VOID)); + PyDict_SetItemString(d, "PyArray_NTYPES", PyInt_FromLong(PyArray_NTYPES)); + PyDict_SetItemString(d, "PyArray_NOTYPE", PyInt_FromLong(PyArray_NOTYPE)); + PyDict_SetItemString(d, "PyArray_UDERDEF", PyInt_FromLong(PyArray_USERDEF)); + + PyDict_SetItemString(d, "CONTIGUOUS", PyInt_FromLong(NPY_CONTIGUOUS)); + PyDict_SetItemString(d, "FORTRAN", PyInt_FromLong(NPY_FORTRAN)); + PyDict_SetItemString(d, "OWNDATA", PyInt_FromLong(NPY_OWNDATA)); + PyDict_SetItemString(d, "FORCECAST", PyInt_FromLong(NPY_FORCECAST)); + PyDict_SetItemString(d, "ENSURECOPY", PyInt_FromLong(NPY_ENSURECOPY)); + PyDict_SetItemString(d, "ENSUREARRAY", PyInt_FromLong(NPY_ENSUREARRAY)); + PyDict_SetItemString(d, "ALIGNED", PyInt_FromLong(NPY_ALIGNED)); + PyDict_SetItemString(d, "WRITEABLE", PyInt_FromLong(NPY_WRITEABLE)); + PyDict_SetItemString(d, "UPDATEIFCOPY", PyInt_FromLong(NPY_UPDATEIFCOPY)); + + PyDict_SetItemString(d, "BEHAVED", PyInt_FromLong(NPY_BEHAVED)); + PyDict_SetItemString(d, "BEHAVED_NS", PyInt_FromLong(NPY_BEHAVED_NS)); + PyDict_SetItemString(d, "CARRAY", PyInt_FromLong(NPY_CARRAY)); + PyDict_SetItemString(d, "FARRAY", PyInt_FromLong(NPY_FARRAY)); + PyDict_SetItemString(d, "CARRAY_RO", PyInt_FromLong(NPY_CARRAY_RO)); + PyDict_SetItemString(d, "FARRAY_RO", PyInt_FromLong(NPY_FARRAY_RO)); + PyDict_SetItemString(d, "DEFAULT", PyInt_FromLong(NPY_DEFAULT)); + PyDict_SetItemString(d, "UPDATE_ALL", PyInt_FromLong(NPY_UPDATE_ALL)); + + if (PyErr_Occurred()) + Py_FatalError("can't initialize module wrap"); + +#ifdef F2PY_REPORT_ATEXIT + on_exit(f2py_report_on_exit,(void*)"array_from_pyobj.wrap.call"); +#endif + +} +#ifdef __cplusplus +} +#endif Property changes on: trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c ___________________________________________________________________ Name: svn:keywords + Id Author Name: svn:eol-style + native Copied: trunk/numpy/f2py/tests/src/mixed/foo.f (from rev 8271, trunk/numpy/f2py/tests/mixed/foo.f) =================================================================== --- trunk/numpy/f2py/tests/src/mixed/foo.f (rev 0) +++ trunk/numpy/f2py/tests/src/mixed/foo.f 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,5 @@ + subroutine bar11(a) +cf2py intent(out) a + integer a + a = 11 + end Copied: trunk/numpy/f2py/tests/src/mixed/foo_fixed.f90 (from rev 8271, trunk/numpy/f2py/tests/mixed/foo_fixed.f90) =================================================================== --- trunk/numpy/f2py/tests/src/mixed/foo_fixed.f90 (rev 0) +++ trunk/numpy/f2py/tests/src/mixed/foo_fixed.f90 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,8 @@ + module foo_fixed + contains + subroutine bar12(a) +!f2py intent(out) a + integer a + a = 12 + end subroutine bar12 + end module foo_fixed Copied: trunk/numpy/f2py/tests/src/mixed/foo_free.f90 (from rev 8271, trunk/numpy/f2py/tests/mixed/foo_free.f90) =================================================================== --- trunk/numpy/f2py/tests/src/mixed/foo_free.f90 (rev 0) +++ trunk/numpy/f2py/tests/src/mixed/foo_free.f90 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,8 @@ +module foo_free +contains + subroutine bar13(a) + !f2py intent(out) a + integer a + a = 13 + end subroutine bar13 +end module foo_free Copied: trunk/numpy/f2py/tests/test_array_from_pyobj.py (from rev 8271, trunk/numpy/f2py/tests/array_from_pyobj/tests/test_array_from_pyobj.py) =================================================================== --- trunk/numpy/f2py/tests/test_array_from_pyobj.py (rev 0) +++ trunk/numpy/f2py/tests/test_array_from_pyobj.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,535 @@ +import unittest +import os +import sys +import copy + +from numpy.testing import * +from numpy import array, alltrue, ndarray, asarray, can_cast,zeros, dtype +from numpy.core.multiarray import typeinfo + +import util + +wrap = None +def setup(): + """ + Build the required testing extension module + + """ + global wrap + if wrap is None: + config_code = """ + config.add_extension('test_array_from_pyobj_ext', + sources=['wrapmodule.c', 'fortranobject.c'], + define_macros=[]) + """ + d = os.path.dirname(__file__) + src = [os.path.join(d, 'src', 'array_from_pyobj', 'wrapmodule.c'), + os.path.join(d, '..', 'src', 'fortranobject.c'), + os.path.join(d, '..', 'src', 'fortranobject.h')] + wrap = util.build_module_distutils(src, config_code, + 'test_array_from_pyobj_ext') + +def flags_info(arr): + flags = wrap.array_attrs(arr)[6] + return flags2names(flags) + +def flags2names(flags): + info = [] + for flagname in ['CONTIGUOUS','FORTRAN','OWNDATA','ENSURECOPY', + 'ENSUREARRAY','ALIGNED','NOTSWAPPED','WRITEABLE', + 'UPDATEIFCOPY','BEHAVED','BEHAVED_RO', + 'CARRAY','FARRAY' + ]: + if abs(flags) & getattr(wrap,flagname): + info.append(flagname) + return info + +class Intent: + def __init__(self,intent_list=[]): + self.intent_list = intent_list[:] + flags = 0 + for i in intent_list: + if i=='optional': + flags |= wrap.F2PY_OPTIONAL + else: + flags |= getattr(wrap,'F2PY_INTENT_'+i.upper()) + self.flags = flags + def __getattr__(self,name): + name = name.lower() + if name=='in_': name='in' + return self.__class__(self.intent_list+[name]) + def __str__(self): + return 'intent(%s)' % (','.join(self.intent_list)) + def __repr__(self): + return 'Intent(%r)' % (self.intent_list) + def is_intent(self,*names): + for name in names: + if name not in self.intent_list: + return False + return True + def is_intent_exact(self,*names): + return len(self.intent_list)==len(names) and self.is_intent(*names) + +intent = Intent() + +class Type(object): + _type_names = ['BOOL','BYTE','UBYTE','SHORT','USHORT','INT','UINT', + 'LONG','ULONG','LONGLONG','ULONGLONG', + 'FLOAT','DOUBLE','LONGDOUBLE','CFLOAT','CDOUBLE', + 'CLONGDOUBLE'] + _type_cache = {} + + _cast_dict = {'BOOL':['BOOL']} + _cast_dict['BYTE'] = _cast_dict['BOOL'] + ['BYTE'] + _cast_dict['UBYTE'] = _cast_dict['BOOL'] + ['UBYTE'] + _cast_dict['BYTE'] = ['BYTE'] + _cast_dict['UBYTE'] = ['UBYTE'] + _cast_dict['SHORT'] = _cast_dict['BYTE'] + ['UBYTE','SHORT'] + _cast_dict['USHORT'] = _cast_dict['UBYTE'] + ['BYTE','USHORT'] + _cast_dict['INT'] = _cast_dict['SHORT'] + ['USHORT','INT'] + _cast_dict['UINT'] = _cast_dict['USHORT'] + ['SHORT','UINT'] + + _cast_dict['LONG'] = _cast_dict['INT'] + ['LONG'] + _cast_dict['ULONG'] = _cast_dict['UINT'] + ['ULONG'] + + _cast_dict['LONGLONG'] = _cast_dict['LONG'] + ['LONGLONG'] + _cast_dict['ULONGLONG'] = _cast_dict['ULONG'] + ['ULONGLONG'] + + _cast_dict['FLOAT'] = _cast_dict['SHORT'] + ['USHORT','FLOAT'] + _cast_dict['DOUBLE'] = _cast_dict['INT'] + ['UINT','FLOAT','DOUBLE'] + _cast_dict['LONGDOUBLE'] = _cast_dict['LONG'] + ['ULONG','FLOAT','DOUBLE','LONGDOUBLE'] + + _cast_dict['CFLOAT'] = _cast_dict['FLOAT'] + ['CFLOAT'] + _cast_dict['CDOUBLE'] = _cast_dict['DOUBLE'] + ['CFLOAT','CDOUBLE'] + _cast_dict['CLONGDOUBLE'] = _cast_dict['LONGDOUBLE'] + ['CFLOAT','CDOUBLE','CLONGDOUBLE'] + + + def __new__(cls,name): + if isinstance(name,dtype): + dtype0 = name + name = None + for n,i in typeinfo.items(): + if isinstance(i,tuple) and dtype0.type is i[-1]: + name = n + break + obj = cls._type_cache.get(name.upper(),None) + if obj is not None: + return obj + obj = object.__new__(cls) + obj._init(name) + cls._type_cache[name.upper()] = obj + return obj + + def _init(self,name): + self.NAME = name.upper() + self.type_num = getattr(wrap,'PyArray_'+self.NAME) + assert_equal(self.type_num,typeinfo[self.NAME][1]) + self.dtype = typeinfo[self.NAME][-1] + self.elsize = typeinfo[self.NAME][2] / 8 + self.dtypechar = typeinfo[self.NAME][0] + + def cast_types(self): + return map(self.__class__,self._cast_dict[self.NAME]) + + def all_types(self): + return map(self.__class__,self._type_names) + + def smaller_types(self): + bits = typeinfo[self.NAME][3] + types = [] + for name in self._type_names: + if typeinfo[name][3]bits: + types.append(Type(name)) + return types + +class Array: + def __init__(self,typ,dims,intent,obj): + self.type = typ + self.dims = dims + self.intent = intent + self.obj_copy = copy.deepcopy(obj) + self.obj = obj + + # arr.dtypechar may be different from typ.dtypechar + self.arr = wrap.call(typ.type_num,dims,intent.flags,obj) + + self.arr_attr = wrap.array_attrs(self.arr) + + if len(dims)>1: + if self.intent.is_intent('c'): + assert intent.flags & wrap.F2PY_INTENT_C + assert not self.arr.flags['FORTRAN'],`self.arr.flags,obj.flags` + assert self.arr.flags['CONTIGUOUS'] + assert not self.arr_attr[6] & wrap.FORTRAN + else: + assert not intent.flags & wrap.F2PY_INTENT_C + assert self.arr.flags['FORTRAN'] + assert not self.arr.flags['CONTIGUOUS'] + assert self.arr_attr[6] & wrap.FORTRAN + + if obj is None: + self.pyarr = None + self.pyarr_attr = None + return + + if intent.is_intent('cache'): + assert isinstance(obj,ndarray),`type(obj)` + self.pyarr = array(obj).reshape(*dims).copy() + else: + self.pyarr = array(array(obj, + dtype = typ.dtypechar).reshape(*dims), + order=self.intent.is_intent('c') and 'C' or 'F') + assert self.pyarr.dtype == typ, \ + `self.pyarr.dtype,typ` + assert self.pyarr.flags['OWNDATA'], (obj, intent) + self.pyarr_attr = wrap.array_attrs(self.pyarr) + + if len(dims)>1: + if self.intent.is_intent('c'): + assert not self.pyarr.flags['FORTRAN'] + assert self.pyarr.flags['CONTIGUOUS'] + assert not self.pyarr_attr[6] & wrap.FORTRAN + else: + assert self.pyarr.flags['FORTRAN'] + assert not self.pyarr.flags['CONTIGUOUS'] + assert self.pyarr_attr[6] & wrap.FORTRAN + + + assert self.arr_attr[1]==self.pyarr_attr[1] # nd + assert self.arr_attr[2]==self.pyarr_attr[2] # dimensions + if self.arr_attr[1]<=1: + assert self.arr_attr[3]==self.pyarr_attr[3],\ + `self.arr_attr[3],self.pyarr_attr[3],self.arr.tostring(),self.pyarr.tostring()` # strides + assert self.arr_attr[5][-2:]==self.pyarr_attr[5][-2:],\ + `self.arr_attr[5],self.pyarr_attr[5]` # descr + assert self.arr_attr[6]==self.pyarr_attr[6],\ + `self.arr_attr[6],self.pyarr_attr[6],flags2names(0*self.arr_attr[6]-self.pyarr_attr[6]),flags2names(self.arr_attr[6]),intent` # flags + + if intent.is_intent('cache'): + assert self.arr_attr[5][3]>=self.type.elsize,\ + `self.arr_attr[5][3],self.type.elsize` + else: + assert self.arr_attr[5][3]==self.type.elsize,\ + `self.arr_attr[5][3],self.type.elsize` + assert self.arr_equal(self.pyarr,self.arr) + + if isinstance(self.obj,ndarray): + if typ.elsize==Type(obj.dtype).elsize: + if not intent.is_intent('copy') and self.arr_attr[1]<=1: + assert self.has_shared_memory() + + def arr_equal(self,arr1,arr2): + if arr1.shape != arr2.shape: + return False + s = arr1==arr2 + return alltrue(s.flatten()) + + def __str__(self): + return str(self.arr) + + def has_shared_memory(self): + """Check that created array shares data with input array. + """ + if self.obj is self.arr: + return True + if not isinstance(self.obj,ndarray): + return False + obj_attr = wrap.array_attrs(self.obj) + return obj_attr[0]==self.arr_attr[0] + +################################################## + +class test_intent(unittest.TestCase): + def test_in_out(self): + assert_equal(str(intent.in_.out),'intent(in,out)') + assert intent.in_.c.is_intent('c') + assert not intent.in_.c.is_intent_exact('c') + assert intent.in_.c.is_intent_exact('c','in') + assert intent.in_.c.is_intent_exact('in','c') + assert not intent.in_.is_intent('c') + +class _test_shared_memory: + num2seq = [1,2] + num23seq = [[1,2,3],[4,5,6]] + def test_in_from_2seq(self): + a = self.array([2],intent.in_,self.num2seq) + assert not a.has_shared_memory() + + def test_in_from_2casttype(self): + for t in self.type.cast_types(): + obj = array(self.num2seq,dtype=t.dtype) + a = self.array([len(self.num2seq)],intent.in_,obj) + if t.elsize==self.type.elsize: + assert a.has_shared_memory(),`self.type.dtype,t.dtype` + else: + assert not a.has_shared_memory(),`t.dtype` + + def test_inout_2seq(self): + obj = array(self.num2seq,dtype=self.type.dtype) + a = self.array([len(self.num2seq)],intent.inout,obj) + assert a.has_shared_memory() + + try: + a = self.array([2],intent.in_.inout,self.num2seq) + except TypeError,msg: + if not str(msg).startswith('failed to initialize intent(inout|inplace|cache) array'): + raise + else: + raise SystemError,'intent(inout) should have failed on sequence' + + def test_f_inout_23seq(self): + obj = array(self.num23seq,dtype=self.type.dtype,order='F') + shape = (len(self.num23seq),len(self.num23seq[0])) + a = self.array(shape,intent.in_.inout,obj) + assert a.has_shared_memory() + + obj = array(self.num23seq,dtype=self.type.dtype,order='C') + shape = (len(self.num23seq),len(self.num23seq[0])) + try: + a = self.array(shape,intent.in_.inout,obj) + except ValueError,msg: + if not str(msg).startswith('failed to initialize intent(inout) array'): + raise + else: + raise SystemError,'intent(inout) should have failed on improper array' + + def test_c_inout_23seq(self): + obj = array(self.num23seq,dtype=self.type.dtype) + shape = (len(self.num23seq),len(self.num23seq[0])) + a = self.array(shape,intent.in_.c.inout,obj) + assert a.has_shared_memory() + + def test_in_copy_from_2casttype(self): + for t in self.type.cast_types(): + obj = array(self.num2seq,dtype=t.dtype) + a = self.array([len(self.num2seq)],intent.in_.copy,obj) + assert not a.has_shared_memory(),`t.dtype` + + def test_c_in_from_23seq(self): + a = self.array([len(self.num23seq),len(self.num23seq[0])], + intent.in_,self.num23seq) + assert not a.has_shared_memory() + + def test_in_from_23casttype(self): + for t in self.type.cast_types(): + obj = array(self.num23seq,dtype=t.dtype) + a = self.array([len(self.num23seq),len(self.num23seq[0])], + intent.in_,obj) + assert not a.has_shared_memory(),`t.dtype` + + def test_f_in_from_23casttype(self): + for t in self.type.cast_types(): + obj = array(self.num23seq,dtype=t.dtype,order='F') + a = self.array([len(self.num23seq),len(self.num23seq[0])], + intent.in_,obj) + if t.elsize==self.type.elsize: + assert a.has_shared_memory(),`t.dtype` + else: + assert not a.has_shared_memory(),`t.dtype` + + def test_c_in_from_23casttype(self): + for t in self.type.cast_types(): + obj = array(self.num23seq,dtype=t.dtype) + a = self.array([len(self.num23seq),len(self.num23seq[0])], + intent.in_.c,obj) + if t.elsize==self.type.elsize: + assert a.has_shared_memory(),`t.dtype` + else: + assert not a.has_shared_memory(),`t.dtype` + + def test_f_copy_in_from_23casttype(self): + for t in self.type.cast_types(): + obj = array(self.num23seq,dtype=t.dtype,order='F') + a = self.array([len(self.num23seq),len(self.num23seq[0])], + intent.in_.copy,obj) + assert not a.has_shared_memory(),`t.dtype` + + def test_c_copy_in_from_23casttype(self): + for t in self.type.cast_types(): + obj = array(self.num23seq,dtype=t.dtype) + a = self.array([len(self.num23seq),len(self.num23seq[0])], + intent.in_.c.copy,obj) + assert not a.has_shared_memory(),`t.dtype` + + def test_in_cache_from_2casttype(self): + for t in self.type.all_types(): + if t.elsize != self.type.elsize: + continue + obj = array(self.num2seq,dtype=t.dtype) + shape = (len(self.num2seq),) + a = self.array(shape,intent.in_.c.cache,obj) + assert a.has_shared_memory(),`t.dtype` + + a = self.array(shape,intent.in_.cache,obj) + assert a.has_shared_memory(),`t.dtype` + + obj = array(self.num2seq,dtype=t.dtype,order='F') + a = self.array(shape,intent.in_.c.cache,obj) + assert a.has_shared_memory(),`t.dtype` + + a = self.array(shape,intent.in_.cache,obj) + assert a.has_shared_memory(),`t.dtype` + + try: + a = self.array(shape,intent.in_.cache,obj[::-1]) + except ValueError,msg: + if not str(msg).startswith('failed to initialize intent(cache) array'): + raise + else: + raise SystemError,'intent(cache) should have failed on multisegmented array' + def test_in_cache_from_2casttype_failure(self): + for t in self.type.all_types(): + if t.elsize >= self.type.elsize: + continue + obj = array(self.num2seq,dtype=t.dtype) + shape = (len(self.num2seq),) + try: + a = self.array(shape,intent.in_.cache,obj) + except ValueError,msg: + if not str(msg).startswith('failed to initialize intent(cache) array'): + raise + else: + raise SystemError,'intent(cache) should have failed on smaller array' + + def test_cache_hidden(self): + shape = (2,) + a = self.array(shape,intent.cache.hide,None) + assert a.arr.shape==shape + + shape = (2,3) + a = self.array(shape,intent.cache.hide,None) + assert a.arr.shape==shape + + shape = (-1,3) + try: + a = self.array(shape,intent.cache.hide,None) + except ValueError,msg: + if not str(msg).startswith('failed to create intent(cache|hide)|optional array'): + raise + else: + raise SystemError,'intent(cache) should have failed on undefined dimensions' + + def test_hidden(self): + shape = (2,) + a = self.array(shape,intent.hide,None) + assert a.arr.shape==shape + assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) + + shape = (2,3) + a = self.array(shape,intent.hide,None) + assert a.arr.shape==shape + assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) + assert a.arr.flags['FORTRAN'] and not a.arr.flags['CONTIGUOUS'] + + shape = (2,3) + a = self.array(shape,intent.c.hide,None) + assert a.arr.shape==shape + assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) + assert not a.arr.flags['FORTRAN'] and a.arr.flags['CONTIGUOUS'] + + shape = (-1,3) + try: + a = self.array(shape,intent.hide,None) + except ValueError,msg: + if not str(msg).startswith('failed to create intent(cache|hide)|optional array'): + raise + else: + raise SystemError,'intent(hide) should have failed on undefined dimensions' + + def test_optional_none(self): + shape = (2,) + a = self.array(shape,intent.optional,None) + assert a.arr.shape==shape + assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) + + shape = (2,3) + a = self.array(shape,intent.optional,None) + assert a.arr.shape==shape + assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) + assert a.arr.flags['FORTRAN'] and not a.arr.flags['CONTIGUOUS'] + + shape = (2,3) + a = self.array(shape,intent.c.optional,None) + assert a.arr.shape==shape + assert a.arr_equal(a.arr,zeros(shape,dtype=self.type.dtype)) + assert not a.arr.flags['FORTRAN'] and a.arr.flags['CONTIGUOUS'] + + def test_optional_from_2seq(self): + obj = self.num2seq + shape = (len(obj),) + a = self.array(shape,intent.optional,obj) + assert a.arr.shape==shape + assert not a.has_shared_memory() + + def test_optional_from_23seq(self): + obj = self.num23seq + shape = (len(obj),len(obj[0])) + a = self.array(shape,intent.optional,obj) + assert a.arr.shape==shape + assert not a.has_shared_memory() + + a = self.array(shape,intent.optional.c,obj) + assert a.arr.shape==shape + assert not a.has_shared_memory() + + def test_inplace(self): + obj = array(self.num23seq,dtype=self.type.dtype) + assert not obj.flags['FORTRAN'] and obj.flags['CONTIGUOUS'] + shape = obj.shape + a = self.array(shape,intent.inplace,obj) + assert obj[1][2]==a.arr[1][2],`obj,a.arr` + a.arr[1][2]=54 + assert obj[1][2]==a.arr[1][2]==array(54,dtype=self.type.dtype),`obj,a.arr` + assert a.arr is obj + assert obj.flags['FORTRAN'] # obj attributes are changed inplace! + assert not obj.flags['CONTIGUOUS'] + + def test_inplace_from_casttype(self): + for t in self.type.cast_types(): + if t is self.type: + continue + obj = array(self.num23seq,dtype=t.dtype) + assert obj.dtype.type==t.dtype + assert obj.dtype.type is not self.type.dtype + assert not obj.flags['FORTRAN'] and obj.flags['CONTIGUOUS'] + shape = obj.shape + a = self.array(shape,intent.inplace,obj) + assert obj[1][2]==a.arr[1][2],`obj,a.arr` + a.arr[1][2]=54 + assert obj[1][2]==a.arr[1][2]==array(54,dtype=self.type.dtype),`obj,a.arr` + assert a.arr is obj + assert obj.flags['FORTRAN'] # obj attributes are changed inplace! + assert not obj.flags['CONTIGUOUS'] + assert obj.dtype.type is self.type.dtype # obj type is changed inplace! + + +for t in Type._type_names: + exec '''\ +class test_%s_gen(unittest.TestCase, + _test_shared_memory + ): + def setUp(self): + self.type = Type(%r) + array = lambda self,dims,intent,obj: Array(Type(%r),dims,intent,obj) +''' % (t,t,t) + +if __name__ == "__main__": + import nose + nose.runmodule() Added: trunk/numpy/f2py/tests/test_callback.py =================================================================== --- trunk/numpy/f2py/tests/test_callback.py (rev 0) +++ trunk/numpy/f2py/tests/test_callback.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,74 @@ +from numpy.testing import * +from numpy import array +import math +import util + +class TestF77Callback(util.F2PyTest): + code = """ + subroutine t(fun,a) + integer a +cf2py intent(out) a + external fun + call fun(a) + end + + subroutine func(a) +cf2py intent(in,out) a + integer a + a = a + 11 + end + + subroutine func0(a) +cf2py intent(out) a + integer a + a = 11 + end + + subroutine t2(a) +cf2py intent(callback) fun + integer a +cf2py intent(out) a + external fun + call fun(a) + end + """ + + def test_all(self): + for name in "t,t2".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module, name) + r = t(lambda : 4) + assert r==4,`r` + r = t(lambda a:5,fun_extra_args=(6,)) + assert r==5,`r` + r = t(lambda a:a,fun_extra_args=(6,)) + assert r==6,`r` + r = t(lambda a:5+a,fun_extra_args=(7,)) + assert r==12,`r` + r = t(lambda a:math.degrees(a),fun_extra_args=(math.pi,)) + assert r==180,`r` + r = t(math.degrees,fun_extra_args=(math.pi,)) + assert r==180,`r` + + r = t(self.module.func, fun_extra_args=(6,)) + assert r==17,`r` + r = t(self.module.func0) + assert r==11,`r` + r = t(self.module.func0._cpointer) + assert r==11,`r` + class A: + def __call__(self): + return 7 + def mth(self): + return 9 + a = A() + r = t(a) + assert r==7,`r` + r = t(a.mth) + assert r==9,`r` + +if __name__ == "__main__": + import nose + nose.runmodule() Added: trunk/numpy/f2py/tests/test_mixed.py =================================================================== --- trunk/numpy/f2py/tests/test_mixed.py (rev 0) +++ trunk/numpy/f2py/tests/test_mixed.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,24 @@ +import os +import math + +from numpy.testing import * +from numpy import array + +import util + +def _path(*a): + return os.path.join(*((os.path.dirname(__file__),) + a)) + +class TestMixed(util.F2PyTest): + sources = [_path('src', 'mixed', 'foo.f'), + _path('src', 'mixed', 'foo_fixed.f90'), + _path('src', 'mixed', 'foo_free.f90')] + + def test_all(self): + assert self.module.bar11() == 11 + assert self.module.foo_fixed.bar12() == 12 + assert self.module.foo_free.bar13() == 13 + +if __name__ == "__main__": + import nose + nose.runmodule() Added: trunk/numpy/f2py/tests/test_return_character.py =================================================================== --- trunk/numpy/f2py/tests/test_return_character.py (rev 0) +++ trunk/numpy/f2py/tests/test_return_character.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,146 @@ +from numpy.testing import * +from numpy import array +import util + +class TestReturnCharacter(util.F2PyTest): + def _check_function(self, t): + tname = t.__doc__.split()[0] + if tname in ['t0','t1','s0','s1']: + assert t(23)=='2' + r = t('ab');assert r=='a',`r` + r = t(array('ab'));assert r=='a',`r` + r = t(array(77,'u1'));assert r=='M',`r` + #assert_raises(ValueError, t, array([77,87])) + #assert_raises(ValueError, t, array(77)) + elif tname in ['ts','ss']: + assert t(23)=='23 ',`t(23)` + assert t('123456789abcdef')=='123456789a' + elif tname in ['t5','s5']: + assert t(23)=='23 ',`t(23)` + assert t('ab')=='ab ',`t('ab')` + assert t('123456789abcdef')=='12345' + else: + raise NotImplementedError + +class TestF77ReturnCharacter(TestReturnCharacter): + code = """ + function t0(value) + character value + character t0 + t0 = value + end + function t1(value) + character*1 value + character*1 t1 + t1 = value + end + function t5(value) + character*5 value + character*5 t5 + t5 = value + end + function ts(value) + character*(*) value + character*(*) ts + ts = value + end + + subroutine s0(t0,value) + character value + character t0 +cf2py intent(out) t0 + t0 = value + end + subroutine s1(t1,value) + character*1 value + character*1 t1 +cf2py intent(out) t1 + t1 = value + end + subroutine s5(t5,value) + character*5 value + character*5 t5 +cf2py intent(out) t5 + t5 = value + end + subroutine ss(ts,value) + character*(*) value + character*10 ts +cf2py intent(out) ts + ts = value + end + """ + + def test_all(self): + for name in "t0,t1,t5,s0,s1,s5,ss".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module, name) + self._check_function(t) + + +class TestF90ReturnCharacter(TestReturnCharacter): + suffix = ".f90" + code = """ +module f90_return_char + contains + function t0(value) + character :: value + character :: t0 + t0 = value + end function t0 + function t1(value) + character(len=1) :: value + character(len=1) :: t1 + t1 = value + end function t1 + function t5(value) + character(len=5) :: value + character(len=5) :: t5 + t5 = value + end function t5 + function ts(value) + character(len=*) :: value + character(len=10) :: ts + ts = value + end function ts + + subroutine s0(t0,value) + character :: value + character :: t0 +!f2py intent(out) t0 + t0 = value + end subroutine s0 + subroutine s1(t1,value) + character(len=1) :: value + character(len=1) :: t1 +!f2py intent(out) t1 + t1 = value + end subroutine s1 + subroutine s5(t5,value) + character(len=5) :: value + character(len=5) :: t5 +!f2py intent(out) t5 + t5 = value + end subroutine s5 + subroutine ss(ts,value) + character(len=*) :: value + character(len=10) :: ts +!f2py intent(out) ts + ts = value + end subroutine ss +end module f90_return_char + """ + + def test_all(self): + for name in "t0,t1,t5,ts,s0,s1,s5,ss".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module.f90_return_char, name) + self._check_function(t) + +if __name__ == "__main__": + import nose + nose.runmodule() Added: trunk/numpy/f2py/tests/test_return_complex.py =================================================================== --- trunk/numpy/f2py/tests/test_return_complex.py (rev 0) +++ trunk/numpy/f2py/tests/test_return_complex.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,172 @@ +from numpy.testing import * +from numpy import array +import util + +class TestReturnComplex(util.F2PyTest): + def _check_function(self, t): + tname = t.__doc__.split()[0] + if tname in ['t0','t8','s0','s8']: + err = 1e-5 + else: + err = 0.0 + assert abs(t(234j)-234.0j)<=err + assert abs(t(234.6)-234.6)<=err + assert abs(t(234l)-234.0)<=err + assert abs(t(234.6+3j)-(234.6+3j))<=err + #assert abs(t('234')-234.)<=err + #assert abs(t('234.6')-234.6)<=err + assert abs(t(-234)+234.)<=err + assert abs(t([234])-234.)<=err + assert abs(t((234,))-234.)<=err + assert abs(t(array(234))-234.)<=err + assert abs(t(array(23+4j,'F'))-(23+4j))<=err + assert abs(t(array([234]))-234.)<=err + assert abs(t(array([[234]]))-234.)<=err + assert abs(t(array([234],'b'))+22.)<=err + assert abs(t(array([234],'h'))-234.)<=err + assert abs(t(array([234],'i'))-234.)<=err + assert abs(t(array([234],'l'))-234.)<=err + assert abs(t(array([234],'q'))-234.)<=err + assert abs(t(array([234],'f'))-234.)<=err + assert abs(t(array([234],'d'))-234.)<=err + assert abs(t(array([234+3j],'F'))-(234+3j))<=err + assert abs(t(array([234],'D'))-234.)<=err + + #assert_raises(TypeError, t, array([234], 'a1')) + assert_raises(TypeError, t, 'abc') + + assert_raises(IndexError, t, []) + assert_raises(IndexError, t, ()) + + assert_raises(TypeError, t, t) + assert_raises(TypeError, t, {}) + + try: + r = t(10l**400) + assert `r` in ['(inf+0j)','(Infinity+0j)'],`r` + except OverflowError: + pass + + +class TestF77ReturnComplex(TestReturnComplex): + code = """ + function t0(value) + complex value + complex t0 + t0 = value + end + function t8(value) + complex*8 value + complex*8 t8 + t8 = value + end + function t16(value) + complex*16 value + complex*16 t16 + t16 = value + end + function td(value) + double complex value + double complex td + td = value + end + + subroutine s0(t0,value) + complex value + complex t0 +cf2py intent(out) t0 + t0 = value + end + subroutine s8(t8,value) + complex*8 value + complex*8 t8 +cf2py intent(out) t8 + t8 = value + end + subroutine s16(t16,value) + complex*16 value + complex*16 t16 +cf2py intent(out) t16 + t16 = value + end + subroutine sd(td,value) + double complex value + double complex td +cf2py intent(out) td + td = value + end + """ + + def test_all(self): + for name in "t0,t8,t16,td,s0,s8,s16,sd".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module, name) + self._check_function(t) + + +class TestF90ReturnComplex(TestReturnComplex): + suffix = ".f90" + code = """ +module f90_return_complex + contains + function t0(value) + complex :: value + complex :: t0 + t0 = value + end function t0 + function t8(value) + complex(kind=4) :: value + complex(kind=4) :: t8 + t8 = value + end function t8 + function t16(value) + complex(kind=8) :: value + complex(kind=8) :: t16 + t16 = value + end function t16 + function td(value) + double complex :: value + double complex :: td + td = value + end function td + + subroutine s0(t0,value) + complex :: value + complex :: t0 +!f2py intent(out) t0 + t0 = value + end subroutine s0 + subroutine s8(t8,value) + complex(kind=4) :: value + complex(kind=4) :: t8 +!f2py intent(out) t8 + t8 = value + end subroutine s8 + subroutine s16(t16,value) + complex(kind=8) :: value + complex(kind=8) :: t16 +!f2py intent(out) t16 + t16 = value + end subroutine s16 + subroutine sd(td,value) + double complex :: value + double complex :: td +!f2py intent(out) td + td = value + end subroutine sd +end module f90_return_complex + """ + + def test_all(self): + for name in "t0,t8,t16,td,s0,s8,s16,sd".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module.f90_return_complex, name) + self._check_function(t) + +if __name__ == "__main__": + import nose + nose.runmodule() Added: trunk/numpy/f2py/tests/test_return_integer.py =================================================================== --- trunk/numpy/f2py/tests/test_return_integer.py (rev 0) +++ trunk/numpy/f2py/tests/test_return_integer.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,181 @@ +from numpy.testing import * +from numpy import array +import util + +class TestReturnInteger(util.F2PyTest): + def _check_function(self, t): + assert t(123)==123,`t(123)` + assert t(123.6)==123 + assert t(123l)==123 + assert t('123')==123 + assert t(-123)==-123 + assert t([123])==123 + assert t((123,))==123 + assert t(array(123))==123 + assert t(array([123]))==123 + assert t(array([[123]]))==123 + assert t(array([123],'b'))==123 + assert t(array([123],'h'))==123 + assert t(array([123],'i'))==123 + assert t(array([123],'l'))==123 + assert t(array([123],'B'))==123 + assert t(array([123],'f'))==123 + assert t(array([123],'d'))==123 + + #assert_raises(ValueError, t, array([123],'S3')) + assert_raises(ValueError, t, 'abc') + + assert_raises(IndexError, t, []) + assert_raises(IndexError, t, ()) + + assert_raises(Exception, t, t) + assert_raises(Exception, t, {}) + + if t.__doc__.split()[0] in ['t8','s8']: + assert_raises(OverflowError, t, 100000000000000000000000l) + assert_raises(OverflowError, t, 10000000011111111111111.23) + +class TestF77ReturnInteger(TestReturnInteger): + code = """ + function t0(value) + integer value + integer t0 + t0 = value + end + function t1(value) + integer*1 value + integer*1 t1 + t1 = value + end + function t2(value) + integer*2 value + integer*2 t2 + t2 = value + end + function t4(value) + integer*4 value + integer*4 t4 + t4 = value + end + function t8(value) + integer*8 value + integer*8 t8 + t8 = value + end + + subroutine s0(t0,value) + integer value + integer t0 +cf2py intent(out) t0 + t0 = value + end + subroutine s1(t1,value) + integer*1 value + integer*1 t1 +cf2py intent(out) t1 + t1 = value + end + subroutine s2(t2,value) + integer*2 value + integer*2 t2 +cf2py intent(out) t2 + t2 = value + end + subroutine s4(t4,value) + integer*4 value + integer*4 t4 +cf2py intent(out) t4 + t4 = value + end + subroutine s8(t8,value) + integer*8 value + integer*8 t8 +cf2py intent(out) t8 + t8 = value + end + """ + + def test_all(self): + for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module, name) + self._check_function(t) + + +class TestF90ReturnInteger(TestReturnInteger): + suffix = ".f90" + code = """ +module f90_return_integer + contains + function t0(value) + integer :: value + integer :: t0 + t0 = value + end function t0 + function t1(value) + integer(kind=1) :: value + integer(kind=1) :: t1 + t1 = value + end function t1 + function t2(value) + integer(kind=2) :: value + integer(kind=2) :: t2 + t2 = value + end function t2 + function t4(value) + integer(kind=4) :: value + integer(kind=4) :: t4 + t4 = value + end function t4 + function t8(value) + integer(kind=8) :: value + integer(kind=8) :: t8 + t8 = value + end function t8 + + subroutine s0(t0,value) + integer :: value + integer :: t0 +!f2py intent(out) t0 + t0 = value + end subroutine s0 + subroutine s1(t1,value) + integer(kind=1) :: value + integer(kind=1) :: t1 +!f2py intent(out) t1 + t1 = value + end subroutine s1 + subroutine s2(t2,value) + integer(kind=2) :: value + integer(kind=2) :: t2 +!f2py intent(out) t2 + t2 = value + end subroutine s2 + subroutine s4(t4,value) + integer(kind=4) :: value + integer(kind=4) :: t4 +!f2py intent(out) t4 + t4 = value + end subroutine s4 + subroutine s8(t8,value) + integer(kind=8) :: value + integer(kind=8) :: t8 +!f2py intent(out) t8 + t8 = value + end subroutine s8 +end module f90_return_integer + """ + + def test_all(self): + for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module.f90_return_integer, name) + self._check_function(t) + +if __name__ == "__main__": + import nose + nose.runmodule() Added: trunk/numpy/f2py/tests/test_return_logical.py =================================================================== --- trunk/numpy/f2py/tests/test_return_logical.py (rev 0) +++ trunk/numpy/f2py/tests/test_return_logical.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,190 @@ +from numpy.testing import * +from numpy import array +import util + +class TestReturnLogical(util.F2PyTest): + def _check_function(self, t): + assert t(True)==1,`t(True)` + assert t(False)==0,`t(False)` + assert t(0)==0 + assert t(None)==0 + assert t(0.0)==0 + assert t(0j)==0 + assert t(1j)==1 + assert t(234)==1 + assert t(234.6)==1 + assert t(234l)==1 + assert t(234.6+3j)==1 + assert t('234')==1 + assert t('aaa')==1 + assert t('')==0 + assert t([])==0 + assert t(())==0 + assert t({})==0 + assert t(t)==1 + assert t(-234)==1 + assert t(10l**100)==1 + assert t([234])==1 + assert t((234,))==1 + assert t(array(234))==1 + assert t(array([234]))==1 + assert t(array([[234]]))==1 + assert t(array([234],'b'))==1 + assert t(array([234],'h'))==1 + assert t(array([234],'i'))==1 + assert t(array([234],'l'))==1 + assert t(array([234],'f'))==1 + assert t(array([234],'d'))==1 + assert t(array([234+3j],'F'))==1 + assert t(array([234],'D'))==1 + assert t(array(0))==0 + assert t(array([0]))==0 + assert t(array([[0]]))==0 + assert t(array([0j]))==0 + assert t(array([1]))==1 + assert_raises(ValueError, t, array([0,0])) + + +class TestF77ReturnLogical(TestReturnLogical): + code = """ + function t0(value) + logical value + logical t0 + t0 = value + end + function t1(value) + logical*1 value + logical*1 t1 + t1 = value + end + function t2(value) + logical*2 value + logical*2 t2 + t2 = value + end + function t4(value) + logical*4 value + logical*4 t4 + t4 = value + end +c function t8(value) +c logical*8 value +c logical*8 t8 +c t8 = value +c end + + subroutine s0(t0,value) + logical value + logical t0 +cf2py intent(out) t0 + t0 = value + end + subroutine s1(t1,value) + logical*1 value + logical*1 t1 +cf2py intent(out) t1 + t1 = value + end + subroutine s2(t2,value) + logical*2 value + logical*2 t2 +cf2py intent(out) t2 + t2 = value + end + subroutine s4(t4,value) + logical*4 value + logical*4 t4 +cf2py intent(out) t4 + t4 = value + end +c subroutine s8(t8,value) +c logical*8 value +c logical*8 t8 +cf2py intent(out) t8 +c t8 = value +c end + """ + + def test_all(self): + for name in "t0,t1,t2,t4,s0,s1,s2,s4".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module, name) + self._check_function(t) + +class TestF90ReturnLogical(TestReturnLogical): + suffix = ".f90" + code = """ +module f90_return_logical + contains + function t0(value) + logical :: value + logical :: t0 + t0 = value + end function t0 + function t1(value) + logical(kind=1) :: value + logical(kind=1) :: t1 + t1 = value + end function t1 + function t2(value) + logical(kind=2) :: value + logical(kind=2) :: t2 + t2 = value + end function t2 + function t4(value) + logical(kind=4) :: value + logical(kind=4) :: t4 + t4 = value + end function t4 + function t8(value) + logical(kind=8) :: value + logical(kind=8) :: t8 + t8 = value + end function t8 + + subroutine s0(t0,value) + logical :: value + logical :: t0 +!f2py intent(out) t0 + t0 = value + end subroutine s0 + subroutine s1(t1,value) + logical(kind=1) :: value + logical(kind=1) :: t1 +!f2py intent(out) t1 + t1 = value + end subroutine s1 + subroutine s2(t2,value) + logical(kind=2) :: value + logical(kind=2) :: t2 +!f2py intent(out) t2 + t2 = value + end subroutine s2 + subroutine s4(t4,value) + logical(kind=4) :: value + logical(kind=4) :: t4 +!f2py intent(out) t4 + t4 = value + end subroutine s4 + subroutine s8(t8,value) + logical(kind=8) :: value + logical(kind=8) :: t8 +!f2py intent(out) t8 + t8 = value + end subroutine s8 +end module f90_return_logical + """ + + def test_all(self): + for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): + yield self.check_function, name + + def check_function(self, name): + t = getattr(self.module.f90_return_logical, name) + self._check_function(t) + +if __name__ == "__main__": + import nose + nose.runmodule() Added: trunk/numpy/f2py/tests/test_return_real.py =================================================================== --- trunk/numpy/f2py/tests/test_return_real.py (rev 0) +++ trunk/numpy/f2py/tests/test_return_real.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,206 @@ +from numpy.testing import * +from numpy import array +import math +import util + +class TestReturnReal(util.F2PyTest): + def _check_function(self, t): + if t.__doc__.split()[0] in ['t0','t4','s0','s4']: + err = 1e-5 + else: + err = 0.0 + assert abs(t(234)-234.0)<=err + assert abs(t(234.6)-234.6)<=err + assert abs(t(234l)-234.0)<=err + assert abs(t('234')-234)<=err + assert abs(t('234.6')-234.6)<=err + assert abs(t(-234)+234)<=err + assert abs(t([234])-234)<=err + assert abs(t((234,))-234.)<=err + assert abs(t(array(234))-234.)<=err + assert abs(t(array([234]))-234.)<=err + assert abs(t(array([[234]]))-234.)<=err + assert abs(t(array([234],'b'))+22)<=err + assert abs(t(array([234],'h'))-234.)<=err + assert abs(t(array([234],'i'))-234.)<=err + assert abs(t(array([234],'l'))-234.)<=err + assert abs(t(array([234],'B'))-234.)<=err + assert abs(t(array([234],'f'))-234.)<=err + assert abs(t(array([234],'d'))-234.)<=err + if t.__doc__.split()[0] in ['t0','t4','s0','s4']: + assert t(1e200)==t(1e300) # inf + + #assert_raises(ValueError, t, array([234], 'S1')) + assert_raises(ValueError, t, 'abc') + + assert_raises(IndexError, t, []) + assert_raises(IndexError, t, ()) + + assert_raises(Exception, t, t) + assert_raises(Exception, t, {}) + + try: + r = t(10l**400) + assert `r` in ['inf','Infinity'],`r` + except OverflowError: + pass + +class TestCReturnReal(TestReturnReal): + suffix = ".pyf" + module_name = "c_ext_return_real" + code = """ +python module c_ext_return_real +usercode \'\'\' +float t4(float value) { return value; } +void s4(float *t4, float value) { *t4 = value; } +double t8(double value) { return value; } +void s8(double *t8, double value) { *t8 = value; } +\'\'\' +interface + function t4(value) + real*4 intent(c) :: t4,value + end + function t8(value) + real*8 intent(c) :: t8,value + end + subroutine s4(t4,value) + intent(c) s4 + real*4 intent(out) :: t4 + real*4 intent(c) :: value + end + subroutine s8(t8,value) + intent(c) s8 + real*8 intent(out) :: t8 + real*8 intent(c) :: value + end +end interface +end python module c_ext_return_real + """ + + def test_all(self): + for name in "t4,t8,s4,s8".split(","): + yield self.check_function, name + + def check_function(self, name): + self._check_function(getattr(self.module, name)) + +class TestF77ReturnReal(TestReturnReal): + code = """ + function t0(value) + real value + real t0 + t0 = value + end + function t4(value) + real*4 value + real*4 t4 + t4 = value + end + function t8(value) + real*8 value + real*8 t8 + t8 = value + end + function td(value) + double precision value + double precision td + td = value + end + + subroutine s0(t0,value) + real value + real t0 +cf2py intent(out) t0 + t0 = value + end + subroutine s4(t4,value) + real*4 value + real*4 t4 +cf2py intent(out) t4 + t4 = value + end + subroutine s8(t8,value) + real*8 value + real*8 t8 +cf2py intent(out) t8 + t8 = value + end + subroutine sd(td,value) + double precision value + double precision td +cf2py intent(out) td + td = value + end + """ + + def test_all(self): + for name in "t0,t4,t8,td,s0,s4,s8,sd".split(","): + yield self.check_function, name + + def check_function(self, name): + self._check_function(getattr(self.module, name)) + +class TestF90ReturnReal(TestReturnReal): + suffix = ".f90" + code = """ +module f90_return_real + contains + function t0(value) + real :: value + real :: t0 + t0 = value + end function t0 + function t4(value) + real(kind=4) :: value + real(kind=4) :: t4 + t4 = value + end function t4 + function t8(value) + real(kind=8) :: value + real(kind=8) :: t8 + t8 = value + end function t8 + function td(value) + double precision :: value + double precision :: td + td = value + end function td + + subroutine s0(t0,value) + real :: value + real :: t0 +!f2py intent(out) t0 + t0 = value + end subroutine s0 + subroutine s4(t4,value) + real(kind=4) :: value + real(kind=4) :: t4 +!f2py intent(out) t4 + t4 = value + end subroutine s4 + subroutine s8(t8,value) + real(kind=8) :: value + real(kind=8) :: t8 +!f2py intent(out) t8 + t8 = value + end subroutine s8 + subroutine sd(td,value) + double precision :: value + double precision :: td +!f2py intent(out) td + td = value + end subroutine sd +end module f90_return_real + """ + + def test_all(self): + for name in "t0,t4,t8,td,s0,s4,s8,sd".split(","): + yield self.check_function, name + + def check_function(self, name): + self._check_function(getattr(self.module.f90_return_real, name)) + + +if __name__ == "__main__": + import nose + nose.runmodule() Added: trunk/numpy/f2py/tests/util.py =================================================================== --- trunk/numpy/f2py/tests/util.py (rev 0) +++ trunk/numpy/f2py/tests/util.py 2010-03-06 19:10:14 UTC (rev 8273) @@ -0,0 +1,346 @@ +""" +Utility functions for + +- building and importing modules on test time, using a temporary location +- detecting if compilers are present + +""" + +import os +import sys +import subprocess +import tempfile +import shutil +import atexit +import textwrap +import re +import random + +import nose + +from numpy.compat import asbytes +import numpy.f2py + +try: + from hashlib import md5 +except ImportError: + from md5 import new as md5 + +# +# Maintaining a temporary module directory +# + +_module_dir = None + +def _cleanup(): + global _module_dir + if _module_dir is not None: + try: + sys.path.remove(_module_dir) + except ValueError: + pass + try: + shutil.rmtree(_module_dir) + except (IOError, OSError): + pass + _module_dir = None + +def get_module_dir(): + global _module_dir + if _module_dir is None: + _module_dir = tempfile.mkdtemp() + atexit.register(_cleanup) + if _module_dir not in sys.path: + sys.path.insert(0, _module_dir) + return _module_dir + +def get_temp_module_name(): + # Assume single-threaded, and the module dir usable only by this thread + d = get_module_dir() + for j in xrange(5403, 9999999): + name = "_test_ext_module_%d" % j + fn = os.path.join(d, name) + if name not in sys.modules and not os.path.isfile(fn+'.py'): + return name + raise RuntimeError("Failed to create a temporary module name") + +def _memoize(func): + memo = {} + def wrapper(*a, **kw): + key = repr((a, kw)) + if key not in memo: + try: + memo[key] = func(*a, **kw) + except Exception, e: + memo[key] = e + raise + ret = memo[key] + if isinstance(ret, Exception): + raise ret + return ret + wrapper.__name__ = func.__name__ + return wrapper + +# +# Building modules +# + + at _memoize +def build_module(source_files, options=[], skip=[], only=[], module_name=None): + """ + Compile and import a f2py module, built from the given files. + + """ + + code = ("import sys; sys.path = %s; import numpy.f2py as f2py2e; " + "f2py2e.main()" % repr(sys.path)) + + d = get_module_dir() + + # Copy files + dst_sources = [] + for fn in source_files: + if not os.path.isfile(fn): + raise RuntimeError("%s is not a file" % fn) + dst = os.path.join(d, os.path.basename(fn)) + shutil.copyfile(fn, dst) + dst_sources.append(dst) + + # Prepare options + if module_name is None: + module_name = get_temp_module_name() + f2py_opts = ['-c', '-m', module_name] + options + dst_sources + if skip: + f2py_opts += ['skip:'] + skip + if only: + f2py_opts += ['only:'] + only + + # Build + cwd = os.getcwd() + try: + os.chdir(d) + cmd = [sys.executable, '-c', code] + f2py_opts + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + out, err = p.communicate() + if p.returncode != 0: + raise RuntimeError("Running f2py failed: %s\n%s" + % (cmd[4:], out)) + finally: + os.chdir(cwd) + + # Partial cleanup + for fn in dst_sources: + os.unlink(fn) + + # Import + __import__(module_name) + return sys.modules[module_name] + + at _memoize +def build_code(source_code, options=[], skip=[], only=[], suffix=None, + module_name=None): + """ + Compile and import Fortran code using f2py. + + """ + if suffix is None: + suffix = '.f' + + fd, tmp_fn = tempfile.mkstemp(suffix=suffix) + os.write(fd, asbytes(source_code)) + os.close(fd) + + try: + return build_module([tmp_fn], options=options, skip=skip, only=only, + module_name=module_name) + finally: + os.unlink(tmp_fn) + +# +# Check if compilers are available at all... +# + +_compiler_status = None +def _get_compiler_status(): + global _compiler_status + if _compiler_status is not None: + return _compiler_status + + _compiler_status = (False, False, False) + + # XXX: this is really ugly. But I don't know how to invoke Distutils + # in a safer way... + code = """ +import os +import sys +sys.path = %(syspath)s + +def configuration(parent_name='',top_path=None): + global config + from numpy.distutils.misc_util import Configuration + config = Configuration('', parent_name, top_path) + return config + +from numpy.distutils.core import setup +setup(configuration=configuration) + +config_cmd = config.get_config_cmd() +have_c = config_cmd.try_compile('void foo() {}') +print('COMPILERS:%%d,%%d,%%d' %% (have_c, + config.have_f77c(), + config.have_f90c())) +sys.exit(99) +""" + code = code % dict(syspath=repr(sys.path)) + + fd, script = tempfile.mkstemp(suffix='.py') + os.write(fd, asbytes(code)) + os.close(fd) + + try: + cmd = [sys.executable, script, 'config'] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + out, err = p.communicate() + m = re.search(r'COMPILERS:(\d+),(\d+),(\d+)', out) + if m: + _compiler_status = (bool(m.group(1)), bool(m.group(2)), + bool(m.group(3))) + finally: + os.unlink(script) + + # Finished + return _compiler_status + +def has_c_compiler(): + return _get_compiler_status()[0] + +def has_f77_compiler(): + return _get_compiler_status()[1] + +def has_f90_compiler(): + return _get_compiler_status()[2] + +# +# Building with distutils +# + + at _memoize +def build_module_distutils(source_files, config_code, module_name, **kw): + """ + Build a module via distutils and import it. + + """ + from numpy.distutils.misc_util import Configuration + from numpy.distutils.core import setup + + d = get_module_dir() + + # Copy files + dst_sources = [] + for fn in source_files: + if not os.path.isfile(fn): + raise RuntimeError("%s is not a file" % fn) + dst = os.path.join(d, os.path.basename(fn)) + shutil.copyfile(fn, dst) + dst_sources.append(dst) + + # Build script + config_code = textwrap.dedent(config_code).replace("\n", "\n ") + + code = """\ +import os +import sys +sys.path = %(syspath)s + +def configuration(parent_name='',top_path=None): + from numpy.distutils.misc_util import Configuration + config = Configuration('', parent_name, top_path) + %(config_code)s + return config + +if __name__ == "__main__": + from numpy.distutils.core import setup + setup(configuration=configuration) +""" % dict(config_code=config_code, syspath = repr(sys.path)) + + script = os.path.join(d, get_temp_module_name() + '.py') + dst_sources.append(script) + f = open(script, 'wb') + f.write(asbytes(code)) + f.close() + + # Build + cwd = os.getcwd() + try: + os.chdir(d) + cmd = [sys.executable, script, 'build_ext', '-i'] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + out, err = p.communicate() + if p.returncode != 0: + raise RuntimeError("Running distutils build failed: %s\n%s" + % (cmd[4:], out)) + finally: + os.chdir(cwd) + + # Partial cleanup + for fn in dst_sources: + os.unlink(fn) + + # Import + __import__(module_name) + return sys.modules[module_name] + +# +# Unittest convenience +# + +class F2PyTest(object): + code = None + sources = None + options = [] + skip = [] + only = [] + suffix = '.f' + module = None + module_name = None + + def setUp(self): + if self.module is not None: + return + + # Check compiler availability first + if not has_c_compiler(): + raise nose.SkipTest("No C compiler available") + + codes = [] + if self.sources: + codes.extend(self.sources) + if self.code is not None: + codes.append(self.suffix) + + needs_f77 = False + needs_f90 = False + for fn in codes: + if fn.endswith('.f'): + needs_f77 = True + elif fn.endswith('.f90'): + needs_f90 = True + if needs_f77 and not has_f77_compiler(): + raise nose.SkipTest("No Fortran 77 compiler available") + if needs_f90 and not has_f90_compiler(): + raise nose.SkipTest("No Fortran 90 compiler available") + + # Build the module + if self.code is not None: + self.module = build_code(self.code, options=self.options, + skip=self.skip, only=self.only, + suffix=self.suffix, + module_name=self.module_name) + + if self.sources is not None: + self.module = build_module(self.sources, options=self.options, + skip=self.skip, only=self.only, + module_name=self.module_name) From numpy-svn at scipy.org Sat Mar 6 14:11:30 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 13:11:30 -0600 (CST) Subject: [Numpy-svn] r8274 - trunk/numpy/f2py/src Message-ID: <20100306191130.251A639CB09@scipy.org> Author: ptvirtan Date: 2010-03-06 13:11:30 -0600 (Sat, 06 Mar 2010) New Revision: 8274 Modified: trunk/numpy/f2py/src/fortranobject.c Log: BUG: f2py: ensure that ARRAY_ISCOMPATIBLE macro handles also booleans Modified: trunk/numpy/f2py/src/fortranobject.c =================================================================== --- trunk/numpy/f2py/src/fortranobject.c 2010-03-06 19:10:14 UTC (rev 8273) +++ trunk/numpy/f2py/src/fortranobject.c 2010-03-06 19:11:30 UTC (rev 8274) @@ -529,6 +529,7 @@ ( (PyArray_ISINTEGER(arr) && PyTypeNum_ISINTEGER(type_num)) \ ||(PyArray_ISFLOAT(arr) && PyTypeNum_ISFLOAT(type_num)) \ ||(PyArray_ISCOMPLEX(arr) && PyTypeNum_ISCOMPLEX(type_num)) \ + ||(PyArray_ISBOOL(arr) && PyTypeNum_ISBOOL(type_num)) \ ) extern From numpy-svn at scipy.org Sat Mar 6 14:12:31 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 13:12:31 -0600 (CST) Subject: [Numpy-svn] r8275 - trunk/numpy/distutils Message-ID: <20100306191231.DB2F239CBDC@scipy.org> Author: ptvirtan Date: 2010-03-06 13:12:31 -0600 (Sat, 06 Mar 2010) New Revision: 8275 Modified: trunk/numpy/distutils/misc_util.py Log: BUG: distutils: clean up temporary files more aggressively Modified: trunk/numpy/distutils/misc_util.py =================================================================== --- trunk/numpy/distutils/misc_util.py 2010-03-06 19:11:30 UTC (rev 8274) +++ trunk/numpy/distutils/misc_util.py 2010-03-06 19:12:31 UTC (rev 8275) @@ -7,6 +7,7 @@ import atexit import tempfile import subprocess +import shutil from distutils.errors import DistutilsError @@ -253,7 +254,7 @@ return log.debug('removing %s', _temporary_directory) try: - os.rmdir(_temporary_directory) + shutil.rmtree(_temporary_directory) except OSError: pass _temporary_directory = None From numpy-svn at scipy.org Sat Mar 6 14:13:44 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 13:13:44 -0600 (CST) Subject: [Numpy-svn] r8276 - trunk/numpy/f2py/tests Message-ID: <20100306191344.0988039CB09@scipy.org> Author: ptvirtan Date: 2010-03-06 13:13:43 -0600 (Sat, 06 Mar 2010) New Revision: 8276 Modified: trunk/numpy/f2py/tests/test_array_from_pyobj.py Log: ENH: f2py: skip also test_array_from_pyobj if no C compiler is available Modified: trunk/numpy/f2py/tests/test_array_from_pyobj.py =================================================================== --- trunk/numpy/f2py/tests/test_array_from_pyobj.py 2010-03-06 19:12:31 UTC (rev 8275) +++ trunk/numpy/f2py/tests/test_array_from_pyobj.py 2010-03-06 19:13:43 UTC (rev 8276) @@ -3,6 +3,8 @@ import sys import copy +import nose + from numpy.testing import * from numpy import array, alltrue, ndarray, asarray, can_cast,zeros, dtype from numpy.core.multiarray import typeinfo @@ -16,6 +18,11 @@ """ global wrap + + # Check compiler availability first + if not util.has_c_compiler(): + raise nose.SkipTest("No C compiler available") + if wrap is None: config_code = """ config.add_extension('test_array_from_pyobj_ext', From numpy-svn at scipy.org Sat Mar 6 14:14:47 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 13:14:47 -0600 (CST) Subject: [Numpy-svn] r8277 - trunk/numpy/core/src/private Message-ID: <20100306191447.3664239CBDC@scipy.org> Author: ptvirtan Date: 2010-03-06 13:14:47 -0600 (Sat, 06 Mar 2010) New Revision: 8277 Modified: trunk/numpy/core/src/private/npy_3kcompat.h Log: BUG: core: add a missing #include of ndarrayobject to npy_3kcompat.h Modified: trunk/numpy/core/src/private/npy_3kcompat.h =================================================================== --- trunk/numpy/core/src/private/npy_3kcompat.h 2010-03-06 19:13:43 UTC (rev 8276) +++ trunk/numpy/core/src/private/npy_3kcompat.h 2010-03-06 19:14:47 UTC (rev 8277) @@ -6,6 +6,7 @@ #include "npy_config.h" #include "numpy/npy_common.h" +#include "numpy/ndarrayobject.h" /* * PyInt -> PyLong From numpy-svn at scipy.org Sat Mar 6 14:39:09 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 13:39:09 -0600 (CST) Subject: [Numpy-svn] r8278 - in trunk/numpy/f2py: . tests Message-ID: <20100306193909.D2A5039CBDC@scipy.org> Author: ptvirtan Date: 2010-03-06 13:39:09 -0600 (Sat, 06 Mar 2010) New Revision: 8278 Modified: trunk/numpy/f2py/auxfuncs.py trunk/numpy/f2py/cb_rules.py trunk/numpy/f2py/cfuncs.py trunk/numpy/f2py/f2py2e.py trunk/numpy/f2py/tests/util.py Log: 3K: f2py: make f2py run far enough to produce output files (they don't compile yet, though, as the C code is not Py3 compatible) Modified: trunk/numpy/f2py/auxfuncs.py =================================================================== --- trunk/numpy/f2py/auxfuncs.py 2010-03-06 19:14:47 UTC (rev 8277) +++ trunk/numpy/f2py/auxfuncs.py 2010-03-06 19:39:09 UTC (rev 8278) @@ -590,22 +590,22 @@ if s and s[-1]==',': return s[:-1] return s -def replace(str,dict,defaultsep=''): - if type(dict)==types.ListType: - return map(lambda d,f=replace,sep=defaultsep,s=str:f(s,d,sep),dict) +def replace(str,d,defaultsep=''): + if type(d)==types.ListType: + return map(lambda d,f=replace,sep=defaultsep,s=str:f(s,d,sep),d) if type(str)==types.ListType: - return map(lambda s,f=replace,sep=defaultsep,d=dict:f(s,d,sep),str) - for k in 2*dict.keys(): + return map(lambda s,f=replace,sep=defaultsep,d=d:f(s,d,sep),str) + for k in 2*d.keys(): if k=='separatorsfor': continue - if 'separatorsfor' in dict and k in dict['separatorsfor']: - sep=dict['separatorsfor'][k] + if 'separatorsfor' in d and k in d['separatorsfor']: + sep=d['separatorsfor'][k] else: sep=defaultsep - if type(dict[k])==types.ListType: - str=str.replace('#%s#'%(k),sep.join(flatlist(dict[k]))) + if type(d[k])==types.ListType: + str=str.replace('#%s#'%(k),sep.join(flatlist(d[k]))) else: - str=str.replace('#%s#'%(k),dict[k]) + str=str.replace('#%s#'%(k),d[k]) return str def dictappend(rd,ar): @@ -617,7 +617,7 @@ if k[0]=='_': continue if k in rd: - if type(rd[k])==types.StringType: + if type(rd[k])==str: rd[k]=[rd[k]] if type(rd[k])==types.ListType: if type(ar[k])==types.ListType: @@ -636,11 +636,11 @@ rd[k]=ar[k] return rd -def applyrules(rules,dict,var={}): +def applyrules(rules,d,var={}): ret={} if type(rules)==types.ListType: for r in rules: - rr=applyrules(r,dict,var) + rr=applyrules(r,d,var) ret=dictappend(ret,rr) if '_break' in rr: break @@ -648,19 +648,19 @@ if '_check' in rules and (not rules['_check'](var)): return ret if 'need' in rules: - res = applyrules({'needs':rules['need']},dict,var) + res = applyrules({'needs':rules['need']},d,var) if 'needs' in res: cfuncs.append_needs(res['needs']) for k in rules.keys(): if k=='separatorsfor': ret[k]=rules[k]; continue - if type(rules[k])==types.StringType: - ret[k]=replace(rules[k],dict) + if type(rules[k])==str: + ret[k]=replace(rules[k],d) elif type(rules[k])==types.ListType: ret[k]=[] for i in rules[k]: - ar=applyrules({k:i},dict,var) + ar=applyrules({k:i},d,var) if k in ar: ret[k].append(ar[k]) elif k[0]=='_': @@ -672,19 +672,19 @@ if type(rules[k][k1])==types.ListType: for i in rules[k][k1]: if type(i)==types.DictType: - res=applyrules({'supertext':i},dict,var) + res=applyrules({'supertext':i},d,var) if 'supertext' in res: i=res['supertext'] else: i='' - ret[k].append(replace(i,dict)) + ret[k].append(replace(i,d)) else: i=rules[k][k1] if type(i)==types.DictType: - res=applyrules({'supertext':i},dict) + res=applyrules({'supertext':i},d) if 'supertext' in res: i=res['supertext'] else: i='' - ret[k].append(replace(i,dict)) + ret[k].append(replace(i,d)) else: errmess('applyrules: ignoring rule %s.\n'%`rules[k]`) if type(ret[k])==types.ListType: Modified: trunk/numpy/f2py/cb_rules.py =================================================================== --- trunk/numpy/f2py/cb_rules.py 2010-03-06 19:14:47 UTC (rev 8277) +++ trunk/numpy/f2py/cb_rules.py 2010-03-06 19:39:09 UTC (rev 8278) @@ -515,7 +515,7 @@ ar=applyrules(cb_routine_rules,rd) cfuncs.callbacks[rd['name']]=ar['body'] - if type(ar['need'])==types.StringType: + if type(ar['need'])==str: ar['need']=[ar['need']] if 'need' in rd: Modified: trunk/numpy/f2py/cfuncs.py =================================================================== --- trunk/numpy/f2py/cfuncs.py 2010-03-06 19:14:47 UTC (rev 8277) +++ trunk/numpy/f2py/cfuncs.py 2010-03-06 19:39:09 UTC (rev 8278) @@ -1075,7 +1075,7 @@ if type(need)==types.ListType: for n in need: append_needs(n,flag) - elif type(need)==types.StringType: + elif type(need)==str: if not need: return if need in includes0: n = 'includes0' Modified: trunk/numpy/f2py/f2py2e.py =================================================================== --- trunk/numpy/f2py/f2py2e.py 2010-03-06 19:14:47 UTC (rev 8277) +++ trunk/numpy/f2py/f2py2e.py 2010-03-06 19:39:09 UTC (rev 8278) @@ -285,21 +285,21 @@ f.close() return postlist -def buildmodules(list): +def buildmodules(lst): cfuncs.buildcfuncs() outmess('Building modules...\n') modules,mnames,isusedby=[],[],{} - for i in range(len(list)): - if '__user__' in list[i]['name']: - cb_rules.buildcallbacks(list[i]) + for i in range(len(lst)): + if '__user__' in lst[i]['name']: + cb_rules.buildcallbacks(lst[i]) else: - if 'use' in list[i]: - for u in list[i]['use'].keys(): + if 'use' in lst[i]: + for u in lst[i]['use'].keys(): if u not in isusedby: isusedby[u]=[] - isusedby[u].append(list[i]['name']) - modules.append(list[i]) - mnames.append(list[i]['name']) + isusedby[u].append(lst[i]['name']) + modules.append(lst[i]) + mnames.append(lst[i]['name']) ret = {} for i in range(len(mnames)): if mnames[i] in isusedby: @@ -329,7 +329,11 @@ """Run f2py as if string.join(comline_list,' ') is used as a command line. In case of using -h flag, return None. """ - reload(crackfortran) + if sys.version_info[0] >= 3: + import imp + imp.reload(crackfortran) + else: + reload(crackfortran) f2pydir=os.path.dirname(os.path.abspath(cfuncs.__file__)) fobjhsrc = os.path.join(f2pydir,'src','fortranobject.h') fobjcsrc = os.path.join(f2pydir,'src','fortranobject.c') Modified: trunk/numpy/f2py/tests/util.py =================================================================== --- trunk/numpy/f2py/tests/util.py 2010-03-06 19:14:47 UTC (rev 8277) +++ trunk/numpy/f2py/tests/util.py 2010-03-06 19:39:09 UTC (rev 8278) @@ -18,7 +18,7 @@ import nose -from numpy.compat import asbytes +from numpy.compat import asbytes, asstr import numpy.f2py try: @@ -125,7 +125,7 @@ out, err = p.communicate() if p.returncode != 0: raise RuntimeError("Running f2py failed: %s\n%s" - % (cmd[4:], out)) + % (cmd[4:], asstr(out))) finally: os.chdir(cwd) @@ -203,7 +203,7 @@ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, err = p.communicate() - m = re.search(r'COMPILERS:(\d+),(\d+),(\d+)', out) + m = re.search(asbytes(r'COMPILERS:(\d+),(\d+),(\d+)'), out) if m: _compiler_status = (bool(m.group(1)), bool(m.group(2)), bool(m.group(3))) @@ -281,7 +281,7 @@ out, err = p.communicate() if p.returncode != 0: raise RuntimeError("Running distutils build failed: %s\n%s" - % (cmd[4:], out)) + % (cmd[4:], asstr(out))) finally: os.chdir(cwd) From numpy-svn at scipy.org Sat Mar 6 14:47:18 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 13:47:18 -0600 (CST) Subject: [Numpy-svn] r8279 - trunk/numpy/f2py/tests Message-ID: <20100306194718.458AA39CBDC@scipy.org> Author: ptvirtan Date: 2010-03-06 13:47:18 -0600 (Sat, 06 Mar 2010) New Revision: 8279 Modified: trunk/numpy/f2py/tests/util.py Log: BUG: f2py: fix F90 detection in the tests Modified: trunk/numpy/f2py/tests/util.py =================================================================== --- trunk/numpy/f2py/tests/util.py 2010-03-06 19:39:09 UTC (rev 8278) +++ trunk/numpy/f2py/tests/util.py 2010-03-06 19:47:18 UTC (rev 8279) @@ -205,8 +205,8 @@ out, err = p.communicate() m = re.search(asbytes(r'COMPILERS:(\d+),(\d+),(\d+)'), out) if m: - _compiler_status = (bool(m.group(1)), bool(m.group(2)), - bool(m.group(3))) + _compiler_status = (bool(int(m.group(1))), bool(int(m.group(2))), + bool(int(m.group(3)))) finally: os.unlink(script) From numpy-svn at scipy.org Sat Mar 6 15:09:14 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 14:09:14 -0600 (CST) Subject: [Numpy-svn] r8280 - trunk/numpy/testing Message-ID: <20100306200914.241AE39CC09@scipy.org> Author: ptvirtan Date: 2010-03-06 14:09:14 -0600 (Sat, 06 Mar 2010) New Revision: 8280 Modified: trunk/numpy/testing/nosetester.py Log: ENH: testing: do not exclude array_from_pyobj from tests; it's now fixed to be nose-compatible Modified: trunk/numpy/testing/nosetester.py =================================================================== --- trunk/numpy/testing/nosetester.py 2010-03-06 19:47:18 UTC (rev 8279) +++ trunk/numpy/testing/nosetester.py 2010-03-06 20:09:14 UTC (rev 8280) @@ -239,7 +239,6 @@ argv += ['--exclude','gen_ext'] argv += ['--exclude','pyrex_ext'] argv += ['--exclude','swig_ext'] - argv += ['--exclude','array_from_pyobj'] nose = import_nose() From numpy-svn at scipy.org Sat Mar 6 18:57:58 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 17:57:58 -0600 (CST) Subject: [Numpy-svn] r8281 - trunk/numpy/f2py Message-ID: <20100306235758.CD77539CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 17:57:58 -0600 (Sat, 06 Mar 2010) New Revision: 8281 Modified: trunk/numpy/f2py/cfuncs.py Log: 3K: f2py: address a semantic difference between Py2 and Py3 Modified: trunk/numpy/f2py/cfuncs.py =================================================================== --- trunk/numpy/f2py/cfuncs.py 2010-03-06 20:09:14 UTC (rev 8280) +++ trunk/numpy/f2py/cfuncs.py 2010-03-06 23:57:58 UTC (rev 8281) @@ -1154,7 +1154,8 @@ else: out.append(outneeds[n][0]) del outneeds[n][0] - if saveout and (0 not in map(lambda x,y:x==y,saveout,outneeds[n])): + if saveout and (0 not in map(lambda x,y:x==y,saveout,outneeds[n])) \ + and outneeds[n] != []: print n,saveout errmess('get_needs: no progress in sorting needs, probably circular dependence, skipping.\n') out=out+saveout From numpy-svn at scipy.org Sat Mar 6 18:58:12 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 17:58:12 -0600 (CST) Subject: [Numpy-svn] r8282 - trunk/numpy/f2py Message-ID: <20100306235812.BC36939CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 17:58:12 -0600 (Sat, 06 Mar 2010) New Revision: 8282 Modified: trunk/numpy/f2py/cfuncs.py Log: 3K: f2py: map PyString -> PyBytes and PyInt -> PyLong on Py3 Modified: trunk/numpy/f2py/cfuncs.py =================================================================== --- trunk/numpy/f2py/cfuncs.py 2010-03-06 23:57:58 UTC (rev 8281) +++ trunk/numpy/f2py/cfuncs.py 2010-03-06 23:58:12 UTC (rev 8282) @@ -1068,6 +1068,15 @@ cppmacros[m]='#define %s(v,dims) (PyArray_SimpleNewFromData(1,dims,PyArray_CHAR,(char *)v))'%(m) +############ Automatic Python3 conversions ################### + +if sys.version_info[0] >= 3: + for key, value in cfuncs.items(): + value = value.replace('PyString', 'PyBytes') + value = value.replace('PyInt_AS_LONG', 'PyLong_AsLong') + value = value.replace('PyInt', 'PyLong') + cfuncs[key] = value + ############ Auxiliary functions for sorting needs ################### def append_needs(need,flag=1): From numpy-svn at scipy.org Sat Mar 6 18:58:28 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 17:58:28 -0600 (CST) Subject: [Numpy-svn] r8283 - trunk/numpy/core/code_generators Message-ID: <20100306235828.B4D8B39CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 17:58:28 -0600 (Sat, 06 Mar 2010) New Revision: 8283 Modified: trunk/numpy/core/code_generators/generate_numpy_api.py Log: 3K: core: fix import_array to return NULL on failure -- according to the Py3 module init function scheme Modified: trunk/numpy/core/code_generators/generate_numpy_api.py =================================================================== --- trunk/numpy/core/code_generators/generate_numpy_api.py 2010-03-06 23:58:12 UTC (rev 8282) +++ trunk/numpy/core/code_generators/generate_numpy_api.py 2010-03-06 23:58:28 UTC (rev 8283) @@ -128,8 +128,14 @@ return 0; } -#define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return; } } +#if PY_VERSION_HEX >= 0x03000000 +#define NUMPY_IMPORT_ARRAY_RETVAL NULL +#else +#define NUMPY_IMPORT_ARRAY_RETVAL +#endif +#define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } } + #define import_array1(ret) {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return ret; } } #define import_array2(msg, ret) {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, msg); return ret; } } From numpy-svn at scipy.org Sat Mar 6 18:58:48 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 17:58:48 -0600 (CST) Subject: [Numpy-svn] r8284 - trunk/numpy/f2py/tests Message-ID: <20100306235848.6D9A039CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 17:58:48 -0600 (Sat, 06 Mar 2010) New Revision: 8284 Modified: trunk/numpy/f2py/tests/test_return_character.py trunk/numpy/f2py/tests/test_return_complex.py trunk/numpy/f2py/tests/test_return_integer.py trunk/numpy/f2py/tests/test_return_logical.py trunk/numpy/f2py/tests/test_return_real.py Log: 3K: f2py: don't use test generators in the tests -- they don't really work on Nose on Py3 Modified: trunk/numpy/f2py/tests/test_return_character.py =================================================================== --- trunk/numpy/f2py/tests/test_return_character.py 2010-03-06 23:58:28 UTC (rev 8283) +++ trunk/numpy/f2py/tests/test_return_character.py 2010-03-06 23:58:48 UTC (rev 8284) @@ -1,24 +1,25 @@ from numpy.testing import * from numpy import array +from numpy.compat import asbytes import util class TestReturnCharacter(util.F2PyTest): - def _check_function(self, t): + def check_function(self, t): tname = t.__doc__.split()[0] if tname in ['t0','t1','s0','s1']: - assert t(23)=='2' - r = t('ab');assert r=='a',`r` - r = t(array('ab'));assert r=='a',`r` - r = t(array(77,'u1'));assert r=='M',`r` + assert t(23)==asbytes('2') + r = t('ab');assert r==asbytes('a'),`r` + r = t(array('ab'));assert r==asbytes('a'),`r` + r = t(array(77,'u1'));assert r==asbytes('M'),`r` #assert_raises(ValueError, t, array([77,87])) #assert_raises(ValueError, t, array(77)) elif tname in ['ts','ss']: - assert t(23)=='23 ',`t(23)` - assert t('123456789abcdef')=='123456789a' + assert t(23)==asbytes('23 '),`t(23)` + assert t('123456789abcdef')==asbytes('123456789a') elif tname in ['t5','s5']: - assert t(23)=='23 ',`t(23)` - assert t('ab')=='ab ',`t('ab')` - assert t('123456789abcdef')=='12345' + assert t(23)==asbytes('23 '),`t(23)` + assert t('ab')==asbytes('ab '),`t('ab')` + assert t('123456789abcdef')==asbytes('12345') else: raise NotImplementedError @@ -73,13 +74,8 @@ def test_all(self): for name in "t0,t1,t5,s0,s1,s5,ss".split(","): - yield self.check_function, name + self.check_function(getattr(self.module, name)) - def check_function(self, name): - t = getattr(self.module, name) - self._check_function(t) - - class TestF90ReturnCharacter(TestReturnCharacter): suffix = ".f90" code = """ @@ -135,12 +131,8 @@ def test_all(self): for name in "t0,t1,t5,ts,s0,s1,s5,ss".split(","): - yield self.check_function, name + self.check_function(getattr(self.module.f90_return_char, name)) - def check_function(self, name): - t = getattr(self.module.f90_return_char, name) - self._check_function(t) - if __name__ == "__main__": import nose nose.runmodule() Modified: trunk/numpy/f2py/tests/test_return_complex.py =================================================================== --- trunk/numpy/f2py/tests/test_return_complex.py 2010-03-06 23:58:28 UTC (rev 8283) +++ trunk/numpy/f2py/tests/test_return_complex.py 2010-03-06 23:58:48 UTC (rev 8284) @@ -3,7 +3,7 @@ import util class TestReturnComplex(util.F2PyTest): - def _check_function(self, t): + def check_function(self, t): tname = t.__doc__.split()[0] if tname in ['t0','t8','s0','s8']: err = 1e-5 @@ -99,13 +99,9 @@ def test_all(self): for name in "t0,t8,t16,td,s0,s8,s16,sd".split(","): - yield self.check_function, name + self.check_function(getattr(self.module, name)) - def check_function(self, name): - t = getattr(self.module, name) - self._check_function(t) - class TestF90ReturnComplex(TestReturnComplex): suffix = ".f90" code = """ @@ -161,12 +157,8 @@ def test_all(self): for name in "t0,t8,t16,td,s0,s8,s16,sd".split(","): - yield self.check_function, name + self.check_function(getattr(self.module.f90_return_complex, name)) - def check_function(self, name): - t = getattr(self.module.f90_return_complex, name) - self._check_function(t) - if __name__ == "__main__": import nose nose.runmodule() Modified: trunk/numpy/f2py/tests/test_return_integer.py =================================================================== --- trunk/numpy/f2py/tests/test_return_integer.py 2010-03-06 23:58:28 UTC (rev 8283) +++ trunk/numpy/f2py/tests/test_return_integer.py 2010-03-06 23:58:48 UTC (rev 8284) @@ -3,7 +3,7 @@ import util class TestReturnInteger(util.F2PyTest): - def _check_function(self, t): + def check_function(self, t): assert t(123)==123,`t(123)` assert t(123.6)==123 assert t(123l)==123 @@ -97,13 +97,9 @@ def test_all(self): for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): - yield self.check_function, name + self.check_function(getattr(self.module, name)) - def check_function(self, name): - t = getattr(self.module, name) - self._check_function(t) - class TestF90ReturnInteger(TestReturnInteger): suffix = ".f90" code = """ @@ -170,12 +166,8 @@ def test_all(self): for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): - yield self.check_function, name + self.check_function(getattr(self.module.f90_return_integer, name)) - def check_function(self, name): - t = getattr(self.module.f90_return_integer, name) - self._check_function(t) - if __name__ == "__main__": import nose nose.runmodule() Modified: trunk/numpy/f2py/tests/test_return_logical.py =================================================================== --- trunk/numpy/f2py/tests/test_return_logical.py 2010-03-06 23:58:28 UTC (rev 8283) +++ trunk/numpy/f2py/tests/test_return_logical.py 2010-03-06 23:58:48 UTC (rev 8284) @@ -3,7 +3,7 @@ import util class TestReturnLogical(util.F2PyTest): - def _check_function(self, t): + def check_function(self, t): assert t(True)==1,`t(True)` assert t(False)==0,`t(False)` assert t(0)==0 @@ -107,12 +107,8 @@ def test_all(self): for name in "t0,t1,t2,t4,s0,s1,s2,s4".split(","): - yield self.check_function, name + self.check_function(getattr(self.module, name)) - def check_function(self, name): - t = getattr(self.module, name) - self._check_function(t) - class TestF90ReturnLogical(TestReturnLogical): suffix = ".f90" code = """ @@ -179,12 +175,8 @@ def test_all(self): for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): - yield self.check_function, name + self.check_function(getattr(self.module.f90_return_logical, name)) - def check_function(self, name): - t = getattr(self.module.f90_return_logical, name) - self._check_function(t) - if __name__ == "__main__": import nose nose.runmodule() Modified: trunk/numpy/f2py/tests/test_return_real.py =================================================================== --- trunk/numpy/f2py/tests/test_return_real.py 2010-03-06 23:58:28 UTC (rev 8283) +++ trunk/numpy/f2py/tests/test_return_real.py 2010-03-06 23:58:48 UTC (rev 8284) @@ -4,7 +4,7 @@ import util class TestReturnReal(util.F2PyTest): - def _check_function(self, t): + def check_function(self, t): if t.__doc__.split()[0] in ['t0','t4','s0','s4']: err = 1e-5 else: @@ -79,11 +79,8 @@ def test_all(self): for name in "t4,t8,s4,s8".split(","): - yield self.check_function, name + self.check_function(getattr(self.module, name)) - def check_function(self, name): - self._check_function(getattr(self.module, name)) - class TestF77ReturnReal(TestReturnReal): code = """ function t0(value) @@ -135,11 +132,8 @@ def test_all(self): for name in "t0,t4,t8,td,s0,s4,s8,sd".split(","): - yield self.check_function, name + self.check_function(getattr(self.module, name)) - def check_function(self, name): - self._check_function(getattr(self.module, name)) - class TestF90ReturnReal(TestReturnReal): suffix = ".f90" code = """ @@ -195,12 +189,9 @@ def test_all(self): for name in "t0,t4,t8,td,s0,s4,s8,sd".split(","): - yield self.check_function, name + self.check_function(getattr(self.module.f90_return_real, name)) - def check_function(self, name): - self._check_function(getattr(self.module.f90_return_real, name)) - if __name__ == "__main__": import nose nose.runmodule() From numpy-svn at scipy.org Sat Mar 6 18:59:08 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 17:59:08 -0600 (CST) Subject: [Numpy-svn] r8285 - in trunk/numpy/f2py: . src Message-ID: <20100306235908.71BC339CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 17:59:08 -0600 (Sat, 06 Mar 2010) New Revision: 8285 Modified: trunk/numpy/f2py/capi_maps.py trunk/numpy/f2py/cfuncs.py trunk/numpy/f2py/rules.py trunk/numpy/f2py/src/fortranobject.c trunk/numpy/f2py/src/fortranobject.h Log: 3K: f2py: port much of f2py C code to Py3 Modified: trunk/numpy/f2py/capi_maps.py =================================================================== --- trunk/numpy/f2py/capi_maps.py 2010-03-06 23:58:48 UTC (rev 8284) +++ trunk/numpy/f2py/capi_maps.py 2010-03-06 23:59:08 UTC (rev 8285) @@ -19,6 +19,7 @@ import copy import re import os +import sys from auxfuncs import * from crackfortran import markoutercomma import cb_rules @@ -137,6 +138,11 @@ 'complex_double':'N', 'complex_long_double':'N', 'string':'z'} + +if sys.version_info[0] >= 3: + # Bytes, not Unicode strings + c2buildvalue_map['string'] = 'y' + if using_newcore: #c2buildvalue_map=??? pass Modified: trunk/numpy/f2py/cfuncs.py =================================================================== --- trunk/numpy/f2py/cfuncs.py 2010-03-06 23:58:48 UTC (rev 8284) +++ trunk/numpy/f2py/cfuncs.py 2010-03-06 23:59:08 UTC (rev 8285) @@ -604,8 +604,26 @@ \t\ttmp = obj; \t\tPy_INCREF(tmp); \t} -\telse +#if PY_VERSION_HEX >= 0x03000000 +\telse if (PyUnicode_Check(obj)) { +\t\ttmp = PyUnicode_AsASCIIString(obj); +\t} +\telse { +\t\tPyObject *tmp2; +\t\ttmp2 = PyObject_Str(obj); +\t\tif (tmp2) { +\t\t\ttmp = PyUnicode_AsASCIIString(tmp2); +\t\t\tPy_DECREF(tmp2); +\t\t} +\t\telse { +\t\t\ttmp = NULL; +\t\t} +\t} +#else +\telse { \t\ttmp = PyObject_Str(obj); +\t} +#endif \tif (tmp == NULL) goto capi_fail; \tif (*len == -1) \t\t*len = PyString_GET_SIZE(tmp); @@ -1011,7 +1029,7 @@ \t\t} \t} if (tmp_fun==NULL) { -fprintf(stderr,\"Call-back argument must be function|instance|instance.__call__|f2py-function but got %s.\\n\",(fun==NULL?\"NULL\":fun->ob_type->tp_name)); +fprintf(stderr,\"Call-back argument must be function|instance|instance.__call__|f2py-function but got %s.\\n\",(fun==NULL?\"NULL\":Py_TYPE(fun)->tp_name)); goto capi_fail; } \tif (PyObject_HasAttrString(tmp_fun,\"func_code\")) { @@ -1068,15 +1086,6 @@ cppmacros[m]='#define %s(v,dims) (PyArray_SimpleNewFromData(1,dims,PyArray_CHAR,(char *)v))'%(m) -############ Automatic Python3 conversions ################### - -if sys.version_info[0] >= 3: - for key, value in cfuncs.items(): - value = value.replace('PyString', 'PyBytes') - value = value.replace('PyInt_AS_LONG', 'PyLong_AsLong') - value = value.replace('PyInt', 'PyLong') - cfuncs[key] = value - ############ Auxiliary functions for sorting needs ################### def append_needs(need,flag=1): Modified: trunk/numpy/f2py/rules.py =================================================================== --- trunk/numpy/f2py/rules.py 2010-03-06 23:58:48 UTC (rev 8284) +++ trunk/numpy/f2py/rules.py 2010-03-06 23:59:08 UTC (rev 8285) @@ -170,18 +170,47 @@ \t{NULL,NULL} }; +#if PY_VERSION_HEX >= 0x03000000 +static struct PyModuleDef moduledef = { +\tPyModuleDef_HEAD_INIT, +\t"#modulename#", +\tNULL, +\t-1, +\tf2py_module_methods, +\tNULL, +\tNULL, +\tNULL, +\tNULL +}; +#endif + +#if PY_VERSION_HEX >= 0x03000000 +#define RETVAL m +PyObject *PyInit_#modulename#(void) { +#else +#define RETVAL PyMODINIT_FUNC init#modulename#(void) { +#endif \tint i; \tPyObject *m,*d, *s; +#if PY_VERSION_HEX >= 0x03000000 +\tm = #modulename#_module = PyModule_Create(&moduledef); +#else \tm = #modulename#_module = Py_InitModule(\"#modulename#\", f2py_module_methods); -\tPyFortran_Type.ob_type = &PyType_Type; +#endif +\tPy_TYPE(&PyFortran_Type) = &PyType_Type; \timport_array(); \tif (PyErr_Occurred()) -\t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return;} +\t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return RETVAL;} \td = PyModule_GetDict(m); \ts = PyString_FromString(\"$R"""+"""evision: $\"); \tPyDict_SetItemString(d, \"__version__\", s); -\ts = PyString_FromString(\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\"); +#if PY_VERSION_HEX >= 0x03000000 +\ts = PyUnicode_FromString( +#else +\ts = PyString_FromString( +#endif +\t\t\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\"); \tPyDict_SetItemString(d, \"__doc__\", s); \t#modulename#_error = PyErr_NewException (\"#modulename#.error\", NULL, NULL); \tPy_DECREF(s); @@ -197,6 +226,7 @@ \t\ton_exit(f2py_report_on_exit,(void*)\"#modulename#\"); #endif +\treturn RETVAL; } #ifdef __cplusplus } @@ -393,6 +423,11 @@ extern #ctype# #F_FUNC#(#name_lower#,#NAME#)(void); PyObject* o = PyDict_GetItemString(d,"#name#"); PyObject_SetAttrString(o,"_cpointer", F2PyCapsule_FromVoidPtr((void*)#F_FUNC#(#name_lower#,#NAME#),NULL)); +#if PY_VERSION_HEX >= 0x03000000 + PyObject_SetAttrString(o,"__name__", PyUnicode_FromString("#name#")); +#else + PyObject_SetAttrString(o,"__name__", PyString_FromString("#name#")); +#endif } '''}, 'need':{l_not(l_or(ismoduleroutine,isdummyroutine)):['F_WRAPPEDFUNC','F_FUNC']}, Modified: trunk/numpy/f2py/src/fortranobject.c =================================================================== --- trunk/numpy/f2py/src/fortranobject.c 2010-03-06 23:58:48 UTC (rev 8284) +++ trunk/numpy/f2py/src/fortranobject.c 2010-03-06 23:59:08 UTC (rev 8285) @@ -92,9 +92,12 @@ } +#if PY_VERSION_HEX >= 0x03000000 +#else static PyMethodDef fortran_methods[] = { {NULL, NULL} /* sentinel */ }; +#endif static PyObject * @@ -167,7 +170,11 @@ strlen(p),size); goto fail; } +#if PY_VERSION_HEX >= 0x03000000 + s = PyUnicode_FromString(p); +#else s = PyString_FromString(p); +#endif fail: free(p); return s; @@ -221,20 +228,41 @@ return fp->dict; } if (strcmp(name,"__doc__")==0) { +#if PY_VERSION_HEX >= 0x03000000 + PyObject *s = PyUnicode_FromString(""), *s2, *s3; + for (i=0;ilen;i++) { + s2 = fortran_doc(fp->defs[i]); + s3 = PyUnicode_Concat(s, s2); + Py_DECREF(s2); + Py_DECREF(s); + s = s3; + } +#else PyObject *s = PyString_FromString(""); for (i=0;ilen;i++) PyString_ConcatAndDel(&s,fortran_doc(fp->defs[i])); +#endif if (PyDict_SetItemString(fp->dict, name, s)) return NULL; return s; } if ((strcmp(name,"_cpointer")==0) && (fp->len==1)) { - PyObject *cobj = PyCObject_FromVoidPtr((void *)(fp->defs[0].data),NULL); + PyObject *cobj = F2PyCapsule_FromVoidPtr((void *)(fp->defs[0].data),NULL); if (PyDict_SetItemString(fp->dict, name, cobj)) return NULL; return cobj; } +#if PY_VERSION_HEX >= 0x03000000 + if (1) { + PyObject *str, *ret; + str = PyUnicode_FromString(name); + ret = PyObject_GenericGetAttr((PyObject *)fp, str); + Py_DECREF(str); + return ret; + } +#else return Py_FindMethod(fortran_methods, (PyObject *)fp, name); +#endif } static int @@ -322,10 +350,39 @@ return NULL; } +static PyObject * +fortran_repr(PyFortranObject *fp) +{ + PyObject *name = NULL, *repr = NULL; + name = PyObject_GetAttrString((PyObject *)fp, "__name__"); + PyErr_Clear(); +#if PY_VERSION_HEX >= 0x03000000 + if (name != NULL && PyUnicode_Check(name)) { + repr = PyUnicode_FromFormat("", name); + } + else { + repr = PyUnicode_FromString(""); + } +#else + if (name != NULL && PyString_Check(name)) { + repr = PyString_FromFormat("", PyString_AsString(name)); + } + else { + repr = PyString_FromString(""); + } +#endif + Py_XDECREF(name); + return repr; +} + PyTypeObject PyFortran_Type = { +#if PY_VERSION_HEX >= 0x03000000 + PyVarObject_HEAD_INIT(NULL, 0) +#else PyObject_HEAD_INIT(0) 0, /*ob_size*/ +#endif "fortran", /*tp_name*/ sizeof(PyFortranObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -334,8 +391,8 @@ 0, /*tp_print*/ (getattrfunc)fortran_getattr, /*tp_getattr*/ (setattrfunc)fortran_setattr, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ + 0, /*tp_compare/tp_reserved*/ + (reprfunc)fortran_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ Modified: trunk/numpy/f2py/src/fortranobject.h =================================================================== --- trunk/numpy/f2py/src/fortranobject.h 2010-03-06 23:58:48 UTC (rev 8284) +++ trunk/numpy/f2py/src/fortranobject.h 2010-03-06 23:59:08 UTC (rev 8285) @@ -12,6 +12,31 @@ #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API #include "numpy/arrayobject.h" +/* + * Python 3 support macros + */ +#if PY_VERSION_HEX >= 0x03000000 +#define PyString_Check PyBytes_Check +#define PyString_GET_SIZE PyBytes_GET_SIZE +#define PyString_AS_STRING PyBytes_AS_STRING +#define PyString_FromString PyBytes_FromString +#define PyString_ConcatAndDel PyBytes_ConcatAndDel +#define PyString_AsString PyBytes_AsString + +#define PyInt_Check PyLong_Check +#define PyInt_FromLong PyLong_FromLong +#define PyInt_AS_LONG PyLong_AsLong +#define PyInt_AsLong PyLong_AsLong + +#define PyNumber_Int PyNumber_Long +#endif + +#if (PY_VERSION_HEX < 0x02060000) +#define Py_TYPE(o) (((PyObject*)(o))->ob_type) +#define Py_REFCNT(o) (((PyObject*)(o))->ob_refcnt) +#define Py_SIZE(o) (((PyVarObject*)(o))->ob_size) +#endif + /* #ifdef F2PY_REPORT_ATEXIT_DISABLE #undef F2PY_REPORT_ATEXIT @@ -88,15 +113,15 @@ PyObject *dict; /* Fortran object attribute dictionary */ } PyFortranObject; -#define PyFortran_Check(op) ((op)->ob_type == &PyFortran_Type) -#define PyFortran_Check1(op) (0==strcmp((op)->ob_type->tp_name,"fortran")) +#define PyFortran_Check(op) (Py_TYPE(op) == &PyFortran_Type) +#define PyFortran_Check1(op) (0==strcmp(Py_TYPE(op)->tp_name,"fortran")) extern PyTypeObject PyFortran_Type; extern int F2PyDict_SetItemString(PyObject* dict, char *name, PyObject *obj); extern PyObject * PyFortranObject_New(FortranDataDef* defs, f2py_void_func init); extern PyObject * PyFortranObject_NewAsAttr(FortranDataDef* defs); -#if PY_VERSION_HEX >= 0X03010000 +#if PY_VERSION_HEX >= 0x03010000 PyObject * F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *)); void * F2PyCapsule_AsVoidPtr(PyObject *obj); @@ -146,6 +171,7 @@ extern void dump_attrs(const PyArrayObject* arr); #endif + #ifdef __cplusplus } #endif From numpy-svn at scipy.org Sat Mar 6 18:59:23 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 17:59:23 -0600 (CST) Subject: [Numpy-svn] r8286 - trunk/numpy/f2py Message-ID: <20100306235923.082FB39CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 17:59:22 -0600 (Sat, 06 Mar 2010) New Revision: 8286 Modified: trunk/numpy/f2py/cfuncs.py Log: BUG: f2py: fix infinite loops in *_from_pyobj with unicode input Modified: trunk/numpy/f2py/cfuncs.py =================================================================== --- trunk/numpy/f2py/cfuncs.py 2010-03-06 23:59:08 UTC (rev 8285) +++ trunk/numpy/f2py/cfuncs.py 2010-03-06 23:59:22 UTC (rev 8286) @@ -689,7 +689,7 @@ \t} \tif (PyComplex_Check(obj)) \t\ttmp = PyObject_GetAttrString(obj,\"real\"); -\telse if (PyString_Check(obj)) +\telse if (PyString_Check(obj) || PyUnicode_Check(obj)) \t\t/*pass*/; \telse if (PySequence_Check(obj)) \t\ttmp = PySequence_GetItem(obj,0); @@ -721,7 +721,7 @@ \t} \tif (PyComplex_Check(obj)) \t\ttmp = PyObject_GetAttrString(obj,\"real\"); -\telse if (PyString_Check(obj)) +\telse if (PyString_Check(obj) || PyUnicode_Check(obj)) \t\t/*pass*/; \telse if (PySequence_Check(obj)) \t\ttmp = PySequence_GetItem(obj,0); @@ -758,7 +758,7 @@ \t} \tif (PyComplex_Check(obj)) \t\ttmp = PyObject_GetAttrString(obj,\"real\"); -\telse if (PyString_Check(obj)) +\telse if (PyString_Check(obj) || PyUnicode_Check(obj)) \t\t/*pass*/; \telse if (PySequence_Check(obj)) \t\ttmp = PySequence_GetItem(obj,0); @@ -819,7 +819,7 @@ \t} \tif (PyComplex_Check(obj)) \t\ttmp = PyObject_GetAttrString(obj,\"real\"); -\telse if (PyString_Check(obj)) +\telse if (PyString_Check(obj) || PyUnicode_Check(obj)) \t\t/*pass*/; \telse if (PySequence_Check(obj)) \t\ttmp = PySequence_GetItem(obj,0); @@ -929,7 +929,7 @@ \t\t(*v).r = PyLong_AsDouble(obj); \t\treturn (!PyErr_Occurred()); \t} -\tif (PySequence_Check(obj) && (!PyString_Check(obj))) { +\tif (PySequence_Check(obj) && !(PyString_Check(obj) || PyUnicode_Check(obj))) { \t\tPyObject *tmp = PySequence_GetItem(obj,0); \t\tif (tmp) { \t\t\tif (complex_double_from_pyobj(v,tmp,errmess)) { From numpy-svn at scipy.org Sat Mar 6 18:59:41 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 17:59:41 -0600 (CST) Subject: [Numpy-svn] r8287 - in trunk/numpy/f2py/tests: . src/array_from_pyobj Message-ID: <20100306235941.E13F939CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 17:59:41 -0600 (Sat, 06 Mar 2010) New Revision: 8287 Modified: trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c trunk/numpy/f2py/tests/test_callback.py Log: 3K: f2py: port the array_from_pyobj test module to Py3 Modified: trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c =================================================================== --- trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c 2010-03-06 23:59:22 UTC (rev 8286) +++ trunk/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c 2010-03-06 23:59:41 UTC (rev 8287) @@ -114,10 +114,34 @@ {NULL,NULL} }; +#if PY_VERSION_HEX >= 0x03000000 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "test_array_from_pyobj_ext", + NULL, + -1, + f2py_module_methods, + NULL, + NULL, + NULL, + NULL +}; +#endif + +#if PY_VERSION_HEX >= 0x03000000 +#define RETVAL m +PyObject *PyInit_test_array_from_pyobj_ext(void) { +#else +#define RETVAL PyMODINIT_FUNC inittest_array_from_pyobj_ext(void) { +#endif PyObject *m,*d, *s; +#if PY_VERSION_HEX >= 0x03000000 + m = wrap_module = PyModule_Create(&moduledef); +#else m = wrap_module = Py_InitModule("test_array_from_pyobj_ext", f2py_module_methods); - PyFortran_Type.ob_type = &PyType_Type; +#endif + Py_TYPE(&PyFortran_Type) = &PyType_Type; import_array(); if (PyErr_Occurred()) Py_FatalError("can't initialize module wrap (failed to import numpy)"); @@ -190,6 +214,7 @@ on_exit(f2py_report_on_exit,(void*)"array_from_pyobj.wrap.call"); #endif + return RETVAL; } #ifdef __cplusplus } Modified: trunk/numpy/f2py/tests/test_callback.py =================================================================== --- trunk/numpy/f2py/tests/test_callback.py 2010-03-06 23:59:22 UTC (rev 8286) +++ trunk/numpy/f2py/tests/test_callback.py 2010-03-06 23:59:41 UTC (rev 8287) @@ -35,7 +35,7 @@ def test_all(self): for name in "t,t2".split(","): - yield self.check_function, name + self.check_function(name) def check_function(self, name): t = getattr(self.module, name) From numpy-svn at scipy.org Sat Mar 6 18:59:55 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 17:59:55 -0600 (CST) Subject: [Numpy-svn] r8288 - trunk/numpy/f2py Message-ID: <20100306235955.D674839CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 17:59:55 -0600 (Sat, 06 Mar 2010) New Revision: 8288 Modified: trunk/numpy/f2py/cfuncs.py Log: 3K: f2py: make create_cb_arglist work with Py3 functions Modified: trunk/numpy/f2py/cfuncs.py =================================================================== --- trunk/numpy/f2py/cfuncs.py 2010-03-06 23:59:41 UTC (rev 8287) +++ trunk/numpy/f2py/cfuncs.py 2010-03-06 23:59:55 UTC (rev 8288) @@ -1032,14 +1032,24 @@ fprintf(stderr,\"Call-back argument must be function|instance|instance.__call__|f2py-function but got %s.\\n\",(fun==NULL?\"NULL\":Py_TYPE(fun)->tp_name)); goto capi_fail; } +#if PY_VERSION_HEX >= 0x03000000 +\tif (PyObject_HasAttrString(tmp_fun,\"__code__\")) { +\t\tif (PyObject_HasAttrString(tmp = PyObject_GetAttrString(tmp_fun,\"__code__\"),\"co_argcount\")) +#else \tif (PyObject_HasAttrString(tmp_fun,\"func_code\")) { \t\tif (PyObject_HasAttrString(tmp = PyObject_GetAttrString(tmp_fun,\"func_code\"),\"co_argcount\")) +#endif \t\t\ttot = PyInt_AsLong(PyObject_GetAttrString(tmp,\"co_argcount\")) - di; \t\tPy_XDECREF(tmp); \t} \t/* Get the number of optional arguments */ +#if PY_VERSION_HEX >= 0x03000000 +\tif (PyObject_HasAttrString(tmp_fun,\"__defaults__\")) +\t\tif (PyTuple_Check(tmp = PyObject_GetAttrString(tmp_fun,\"__defaults__\"))) +#else \tif (PyObject_HasAttrString(tmp_fun,\"func_defaults\")) \t\tif (PyTuple_Check(tmp = PyObject_GetAttrString(tmp_fun,\"func_defaults\"))) +#endif \t\t\topt = PyTuple_Size(tmp); \t\tPy_XDECREF(tmp); \t/* Get the number of extra arguments */ From numpy-svn at scipy.org Sat Mar 6 19:00:09 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 6 Mar 2010 18:00:09 -0600 (CST) Subject: [Numpy-svn] r8289 - trunk/doc Message-ID: <20100307000009.0E21739CAF4@scipy.org> Author: ptvirtan Date: 2010-03-06 18:00:08 -0600 (Sat, 06 Mar 2010) New Revision: 8289 Modified: trunk/doc/Py3K.txt Log: 3K: doc: note that f2py is now partly ported, and that capsules are used Modified: trunk/doc/Py3K.txt =================================================================== --- trunk/doc/Py3K.txt 2010-03-06 23:59:55 UTC (rev 8288) +++ trunk/doc/Py3K.txt 2010-03-07 00:00:08 UTC (rev 8289) @@ -128,13 +128,15 @@ numpy.f2py ---------- -F2py has not yet been ported to Py3. +F2py is now mostly ported to Py3. -It imports, but it's not tested. +Simple programs seem to work, as well as its test suite (which does +not exercise all its features). .. todo:: - Port f2py to Py3 + Extend the test suite to more complicated cases, and fix bugs + encountered. Bytes vs. strings @@ -1013,9 +1015,4 @@ The PyCObject API is removed in Python 3.2, so we need to rewrite it using PyCapsule. -.. todo:: - - This probably requires adding some backwards compatibility PyCapsule_* - definitions in npy_py3kcompat.h, and using only PyCapsule_* throughout - Numpy. - +Numpy was changed to use the Capsule API. From numpy-svn at scipy.org Sun Mar 7 12:49:27 2010 From: numpy-svn at scipy.org (Jayson Stephenson) Date: Sun, 7 Mar 2010 11:49:27 -0600 (CST) Subject: [Numpy-svn] Online Shopping Message-ID: <20100307174927.2118339CB88@scipy.org> An HTML attachment was scrubbed... URL: From numpy-svn at scipy.org Sun Mar 7 20:33:44 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 7 Mar 2010 19:33:44 -0600 (CST) Subject: [Numpy-svn] r8290 - trunk/numpy/polynomial Message-ID: <20100308013344.12AB639CB63@scipy.org> Author: charris Date: 2010-03-07 19:33:43 -0600 (Sun, 07 Mar 2010) New Revision: 8290 Modified: trunk/numpy/polynomial/polytemplate.py Log: ENH: Make Chebyshev and Polynomial classes dominate ndarrays. Modified: trunk/numpy/polynomial/polytemplate.py =================================================================== --- trunk/numpy/polynomial/polytemplate.py 2010-03-07 00:00:08 UTC (rev 8289) +++ trunk/numpy/polynomial/polytemplate.py 2010-03-08 01:33:43 UTC (rev 8290) @@ -63,7 +63,10 @@ """ # Limit runaway size. T_n^m has degree n*2^m maxpower = 16 + # Default domain domain = np.array($domain) + # Don't let participate in array operations. Value doesn't matter. + __array_priority__ = 0 def __init__(self, coef, domain=$domain) : [coef, domain] = pu.as_series([coef, domain], trim=False) From numpy-svn at scipy.org Sun Mar 7 20:39:12 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 7 Mar 2010 19:39:12 -0600 (CST) Subject: [Numpy-svn] r8291 - branches/1.4.x/numpy/polynomial Message-ID: <20100308013912.54FED39CB63@scipy.org> Author: charris Date: 2010-03-07 19:39:12 -0600 (Sun, 07 Mar 2010) New Revision: 8291 Modified: branches/1.4.x/numpy/polynomial/polytemplate.py Log: ENH: Don't let Chebyshev and Polynomial participate in array operations. Modified: branches/1.4.x/numpy/polynomial/polytemplate.py =================================================================== --- branches/1.4.x/numpy/polynomial/polytemplate.py 2010-03-08 01:33:43 UTC (rev 8290) +++ branches/1.4.x/numpy/polynomial/polytemplate.py 2010-03-08 01:39:12 UTC (rev 8291) @@ -49,7 +49,10 @@ """ # Limit runaway size. T_n^m has degree n*2^m maxpower = 16 + # Default domain domain = np.array($domain) + # Don't let participate in array operations. Value doesn't matter. + __array_priority__ = 0 def __init__(self, coef, domain=$domain) : [coef, domain] = pu.as_series([coef, domain], trim=False) From numpy-svn at scipy.org Mon Mar 8 01:53:48 2010 From: numpy-svn at scipy.org (Suzette Pendleton) Date: Mon, 8 Mar 2010 00:53:48 -0600 (CST) Subject: [Numpy-svn] Buy at Worldwide Shipping Message-ID: <20100308065348.27E8939CB92@scipy.org> An HTML attachment was scrubbed... URL: From numpy-svn at scipy.org Thu Mar 11 18:32:44 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Thu, 11 Mar 2010 17:32:44 -0600 (CST) Subject: [Numpy-svn] r8292 - trunk/numpy/lib Message-ID: <20100311233244.8C4FB39CB0E@scipy.org> Author: matthew.brett at gmail.com Date: 2010-03-11 17:32:41 -0600 (Thu, 11 Mar 2010) New Revision: 8292 Modified: trunk/numpy/lib/twodim_base.py Log: DOC - fix doc error Modified: trunk/numpy/lib/twodim_base.py =================================================================== --- trunk/numpy/lib/twodim_base.py 2010-03-08 01:39:12 UTC (rev 8291) +++ trunk/numpy/lib/twodim_base.py 2010-03-11 23:32:41 UTC (rev 8292) @@ -851,7 +851,7 @@ def triu_indices_from(arr,k=0): """ - Return the indices for the lower-triangle of an (n, n) array. + Return the indices for the upper-triangle of an (n, n) array. See `triu_indices` for full details. From numpy-svn at scipy.org Sun Mar 14 14:44:37 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 14 Mar 2010 13:44:37 -0500 (CDT) Subject: [Numpy-svn] r8293 - in trunk/numpy/lib: . tests Message-ID: <20100314184437.0F34539CAF9@scipy.org> Author: darren.dale Date: 2010-03-14 13:44:36 -0500 (Sun, 14 Mar 2010) New Revision: 8293 Modified: trunk/numpy/lib/tests/test_ufunclike.py trunk/numpy/lib/ufunclike.py Log: minor change to ufunclike.fix, one less array to build and make compatible with subclasses of ndarray Modified: trunk/numpy/lib/tests/test_ufunclike.py =================================================================== --- trunk/numpy/lib/tests/test_ufunclike.py 2010-03-11 23:32:41 UTC (rev 8292) +++ trunk/numpy/lib/tests/test_ufunclike.py 2010-03-14 18:44:36 UTC (rev 8293) @@ -58,10 +58,28 @@ """ from numpy.testing import * +import numpy.core as nx +import numpy.lib.ufunclike as ufl def test(): return rundocs() +def test_fix_with_subclass(): + class MyArray(nx.ndarray): + def __new__(cls, data, metadata=None): + res = nx.array(data, copy=True).view(cls) + res.metadata = metadata + return res + def __array_wrap__(self, obj, context=None): + obj.metadata = self.metadata + return obj + + a = nx.array([1.1, -1.1]) + m = MyArray(a, metadata='foo') + f = ufl.fix(m) + assert_array_equal(f, nx.array([1,-1])) + assert isinstance(f, MyArray) + assert_equal(f.metadata, 'foo') if __name__ == "__main__": run_module_suite() Modified: trunk/numpy/lib/ufunclike.py =================================================================== --- trunk/numpy/lib/ufunclike.py 2010-03-11 23:32:41 UTC (rev 8292) +++ trunk/numpy/lib/ufunclike.py 2010-03-14 18:44:36 UTC (rev 8293) @@ -41,10 +41,10 @@ """ x = nx.asanyarray(x) - if y is None: - y = nx.zeros_like(x) y1 = nx.floor(x) y2 = nx.ceil(x) + if y is None: + y = y1 y[...] = nx.where(x >= 0, y1, y2) return y From numpy-svn at scipy.org Wed Mar 17 17:04:15 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Wed, 17 Mar 2010 16:04:15 -0500 (CDT) Subject: [Numpy-svn] r8294 - trunk/numpy/distutils Message-ID: <20100317210415.7E0F539CAE9@scipy.org> Author: pearu Date: 2010-03-17 16:04:15 -0500 (Wed, 17 Mar 2010) New Revision: 8294 Modified: trunk/numpy/distutils/ccompiler.py Log: Fix version matcher for cases where version string appears in second line (Intel Visual Compiler, IA-32, Version 11.1) Modified: trunk/numpy/distutils/ccompiler.py =================================================================== --- trunk/numpy/distutils/ccompiler.py 2010-03-14 18:44:36 UTC (rev 8293) +++ trunk/numpy/distutils/ccompiler.py 2010-03-17 21:04:15 UTC (rev 8294) @@ -400,6 +400,9 @@ """ def matcher(self, version_string): + # version string may appear in the second line, so getting rid + # of new lines: + version_string = version_string.replace('\n',' ') pos = 0 if start: m = re.match(start, version_string) From numpy-svn at scipy.org Thu Mar 18 11:23:08 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Thu, 18 Mar 2010 10:23:08 -0500 (CDT) Subject: [Numpy-svn] r8295 - in trunk/numpy/ma: . tests Message-ID: <20100318152308.A645F39CAF2@scipy.org> Author: pierregm Date: 2010-03-18 10:23:08 -0500 (Thu, 18 Mar 2010) New Revision: 8295 Modified: trunk/numpy/ma/core.py trunk/numpy/ma/tests/test_core.py trunk/numpy/ma/tests/test_old_ma.py Log: * fixed mvoid.__repr__ * fixed mvoid.__str__ for Python 2.4 (bug #1373) * add support to 'fill_value' and 'hardmask' in arange/empty/identity/zeros/ones (#1391) * intercept invalid values in `power` and derived * added empty_like/ones_like/zeros_like to the numpy.ma namespace * Allow indices as masked arrays in numpy.ma.take (#1418) * test_core : temporarily disable warnings when using numpy ufuncs on MaskedArray Modified: trunk/numpy/ma/core.py =================================================================== --- trunk/numpy/ma/core.py 2010-03-17 21:04:15 UTC (rev 8294) +++ trunk/numpy/ma/core.py 2010-03-18 15:23:08 UTC (rev 8295) @@ -2535,7 +2535,6 @@ self.dataiter[index] = getdata(value) if self.maskiter is not None: self.maskiter[index] = getmaskarray(value) -# self.ma1d[index] = value def next(self): """ @@ -2803,10 +2802,6 @@ return -# def __array_prepare__(self, obj, context=None): -# print "DEBUG: __array_prepare__ on obj", obj, " with context:", context - - def __array_wrap__(self, obj, context=None): """ Special hook for ufuncs. @@ -2885,7 +2880,7 @@ except (AttributeError, TypeError): pass # Make sure to reset the _fill_value if needed - if getattr(output, '_fill_value', None): + if getattr(output, '_fill_value', None) is not None: output._fill_value = None return output view.__doc__ = ndarray.view.__doc__ @@ -2952,6 +2947,8 @@ # A record ................ if isinstance(dout, np.void): mask = _mask[indx] +# If we can make mvoid a subclass of np.void, that'd be what we'd need +# return mvoid(dout, mask=mask) if flatten_mask(mask).any(): dout = mvoid(dout, mask=mask) else: @@ -2989,17 +2986,16 @@ # if getmask(indx) is not nomask: # msg = "Masked arrays must be filled before they can be used as indices!" # raise IndexError, msg + _data = ndarray.view(self, ndarray.__getattribute__(self, '_baseclass')) + _mask = ndarray.__getattribute__(self, '_mask') if isinstance(indx, basestring): - ndarray.__setitem__(self._data, indx, value) - if self._mask is nomask: - self._mask = make_mask_none(self.shape, self.dtype) - ndarray.__setitem__(self._mask, indx, getmask(value)) + ndarray.__setitem__(_data, indx, value) + if _mask is nomask: + self._mask = _mask = make_mask_none(self.shape, self.dtype) + _mask[indx] = getmask(value) return #........................................ -# ndgetattr = ndarray.__getattribute__ - _data = self._data _dtype = ndarray.__getattribute__(_data, 'dtype') - _mask = ndarray.__getattribute__(self, '_mask') nbfields = len(_dtype.names or ()) #........................................ if value is masked: @@ -3773,7 +3769,12 @@ "Raise self to the power other, in place." other_data = getdata(other) other_mask = getmask(other) - ndarray.__ipow__(self._data, np.where(self._mask, 1, other_data)) + err_status = np.geterr() + try: + np.seterr(divide='ignore', invalid='ignore') + ndarray.__ipow__(self._data, np.where(self._mask, 1, other_data)) + finally: + np.seterr(**err_status) invalid = np.logical_not(np.isfinite(self._data)) if invalid.any(): if self._mask is not nomask: @@ -5212,11 +5213,34 @@ out -= self.min(axis=axis, fill_value=fill_value) return out + def take(self, indices, axis=None, out=None, mode='raise'): + """ + """ + (_data, _mask) = (self._data, self._mask) + cls = type(self) + # Make sure the indices are not masked + maskindices = getattr(indices, '_mask', nomask) + if maskindices is not nomask: + indices = indices.filled(0) + # Get the data + if out is None: + out = _data.take(indices, axis=axis, mode=mode).view(cls) + else: + np.take(_data, indices, axis=axis, mode=mode, out=out) + # Get the mask + if isinstance(out, MaskedArray): + if _mask is nomask: + outmask = maskindices + else: + outmask = _mask.take(indices, axis=axis, mode=mode) + outmask |= maskindices + out.__setmask__(outmask) + return out + # Array methods --------------------------------------- copy = _arraymethod('copy') diagonal = _arraymethod('diagonal') - take = _arraymethod('take') transpose = _arraymethod('transpose') T = property(fget=lambda self:self.transpose()) swapaxes = _arraymethod('swapaxes') @@ -5487,13 +5511,16 @@ _data[()] = data _data = _data.view(self) if mask is not nomask: - try: - # Mask is already a 0D array - _data._mask = np.void(mask) - except TypeError: - # Transform the mask to a void - mdtype = make_mask_descr(dtype) - _data._mask = np.array(mask, dtype=mdtype)[()] + if isinstance(mask, np.void): + _data._mask = mask + else: + try: + # Mask is already a 0D array + _data._mask = np.void(mask) + except TypeError: + # Transform the mask to a void + mdtype = make_mask_descr(dtype) + _data._mask = np.array(mask, dtype=mdtype)[()] if fill_value is not None: _data.fill_value = fill_value return _data @@ -5510,6 +5537,10 @@ return masked return self._data[indx] + def __setitem__(self, indx, value): + self._data[indx] = value + self._mask[indx] |= getattr(value, "_mask", False) + def __str__(self): m = self._mask if (m is nomask): @@ -5523,7 +5554,7 @@ p = 'N/A' else: p = str(p) - r = [(str(_), p)[_m] for (_, _m) in zip(r, m)] + r = [(str(_), p)[int(_m)] for (_, _m) in zip(r, m)] return "(%s)" % ", ".join(r) def __repr__(self): @@ -5537,7 +5568,7 @@ if not p.enabled(): return self.filled(self.fill_value).__repr__() p = str(p) - r = [(str(_), p)[_m] for (_, _m) in zip(r, m)] + r = [(str(_), p)[int(_m)] for (_, _m) in zip(self._data.tolist(), m)] return "(%s)" % ", ".join(r) def __iter__(self): @@ -5907,10 +5938,17 @@ std = _frommethod('std') sum = _frommethod('sum') swapaxes = _frommethod('swapaxes') -take = _frommethod('take') +#take = _frommethod('take') trace = _frommethod('trace') var = _frommethod('var') +def take(a, indices, axis=None, out=None, mode='raise'): + """ + """ + a = masked_array(a) + return a.take(indices, axis=axis, out=out, mode=mode) + + #.............................................................................. def power(a, b, third=None): """ @@ -5944,7 +5982,12 @@ else: basetype = MaskedArray # Get the result and view it as a (subclass of) MaskedArray - result = np.where(m, fa, umath.power(fa, fb)).view(basetype) + err_status = np.geterr() + try: + np.seterr(divide='ignore', invalid='ignore') + result = np.where(m, fa, umath.power(fa, fb)).view(basetype) + finally: + np.seterr(**err_status) result._update_from(a) # Find where we're in trouble w/ NaNs and Infs invalid = np.logical_not(np.isfinite(result.view(ndarray))) @@ -7047,7 +7090,8 @@ class _convert2ma: - """Convert functions from numpy to numpy.ma. + """ + Convert functions from numpy to numpy.ma. Parameters ---------- @@ -7056,9 +7100,11 @@ """ __doc__ = None - def __init__(self, funcname): + # + def __init__(self, funcname, params=None): self._func = getattr(np, funcname) self.__doc__ = self.getdoc() + self._extras = params or {} # def getdoc(self): "Return the doc of the function (from the doc of the method)." @@ -7072,19 +7118,33 @@ return doc # def __call__(self, a, *args, **params): - return self._func.__call__(a, *args, **params).view(MaskedArray) + # Find the common parameters to the call and the definition + _extras = self._extras + common_params = set(params).intersection(_extras) + # Drop the common parameters from the call + for p in common_params: + _extras[p] = params.pop(p) + # Get the result + result = self._func.__call__(a, *args, **params).view(MaskedArray) + if "fill_value" in common_params: + result.fill_value = _extras.get("fill_value", None) + if "hardmask" in common_params: + result._hardmask = bool(_extras.get("hard_mask", False)) + return result -arange = _convert2ma('arange') +arange = _convert2ma('arange', params=dict(fill_value=None, hardmask=False)) clip = np.clip diff = np.diff -empty = _convert2ma('empty') +empty = _convert2ma('empty', params=dict(fill_value=None, hardmask=False)) empty_like = _convert2ma('empty_like') frombuffer = _convert2ma('frombuffer') fromfunction = _convert2ma('fromfunction') -identity = _convert2ma('identity') +identity = _convert2ma('identity', params=dict(fill_value=None, hardmask=False)) indices = np.indices -ones = _convert2ma('ones') -zeros = _convert2ma('zeros') +ones = _convert2ma('ones', params=dict(fill_value=None, hardmask=False)) +ones_like = np.ones_like squeeze = np.squeeze +zeros = _convert2ma('zeros', params=dict(fill_value=None, hardmask=False)) +zeros_like = np.zeros_like ############################################################################### Modified: trunk/numpy/ma/tests/test_core.py =================================================================== --- trunk/numpy/ma/tests/test_core.py 2010-03-17 21:04:15 UTC (rev 8294) +++ trunk/numpy/ma/tests/test_core.py 2010-03-18 15:23:08 UTC (rev 8295) @@ -196,10 +196,15 @@ def test_fix_invalid(self): "Checks fix_invalid." - data = masked_array(np.sqrt([-1., 0., 1.]), mask=[0, 0, 1]) - data_fixed = fix_invalid(data) - assert_equal(data_fixed._data, [data.fill_value, 0., 1.]) - assert_equal(data_fixed._mask, [1., 0., 1.]) + err_status_ini = np.geterr() + try: + np.seterr(invalid='ignore') + data = masked_array(np.sqrt([-1., 0., 1.]), mask=[0, 0, 1]) + data_fixed = fix_invalid(data) + assert_equal(data_fixed._data, [data.fill_value, 0., 1.]) + assert_equal(data_fixed._mask, [1., 0., 1.]) + finally: + np.seterr(**err_status_ini) def test_maskedelement(self): "Test of masked element" @@ -267,7 +272,6 @@ x2 = masked_values(x1, 3.0) assert_equal(x1, x2) assert allequal(array([0, 0, 0, 1, 0], MaskType), x2.mask) -#FIXME: Well, eh, fill_value is now a property assert_equal(3.0, x2.fill_value()) assert_equal(3.0, x2.fill_value) x1 = array([1, 'hello', 2, 3], object) x2 = np.array([1, 'hello', 2, 3], object) @@ -481,7 +485,6 @@ assert z[0] is masked assert z[1] is not masked assert z[2] is masked - # def test_oddfeatures_3(self): @@ -631,6 +634,18 @@ # w/ mask assert_equal(list(a[1]), [masked, 4]) + def test_mvoid_print(self): + "Test printing a mvoid" + mx = array([(1, 1), (2, 2)], dtype=[('a', int), ('b', int)]) + assert_equal(str(mx[0]), "(1, 1)") + mx['b'][0] = masked + ini_display = masked_print_option._display + masked_print_option.set_display("-X-") + try: + assert_equal(str(mx[0]), "(1, -X-)") + assert_equal(repr(mx[0]), "(1, -X-)") + finally: + masked_print_option.set_display(ini_display) #------------------------------------------------------------------------------ @@ -651,7 +666,11 @@ xf = np.where(m1, 1e+20, x) xm.set_fill_value(1e+20) self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf) + self.err_status = np.geterr() + np.seterr(divide='ignore', invalid='ignore') + def tearDown(self): + np.seterr(**self.err_status) def test_basic_arithmetic (self): "Test of basic arithmetic." @@ -1041,7 +1060,6 @@ assert_equal(test, np.mod(xm, ym)) assert_equal(test.mask, mask_or(mask_or(xm.mask, ym.mask), (ym == 0))) - def test_TakeTransposeInnerOuter(self): "Test of take, transpose, inner, outer products" x = arange(24) @@ -1083,7 +1101,6 @@ for funcname in funclist: npfunc = getattr(np, funcname) xmmeth = getattr(xm, funcname) - # A ndarray as explicit input output = np.empty(4, dtype=float) output.fill(-9999) @@ -1148,12 +1165,10 @@ assert_equal(test.mask, control.mask) assert_equal(a.mask, [0, 0, 0, 0, 1]) - #------------------------------------------------------------------------------ class TestMaskedArrayAttributes(TestCase): - def test_keepmask(self): "Tests the keep mask flag" x = masked_array([1, 2, 3], mask=[1, 0, 0]) @@ -1173,7 +1188,7 @@ n = [0, 0, 0, 1, 1] m = make_mask(n) xh = array(d, mask=m, hard_mask=True) - # We need to copy, to avoid updating d in xh! + # We need to copy, to avoid updating d in xh ! xs = array(d, mask=m, hard_mask=False, copy=True) xh[[1, 4]] = [10, 40] xs[[1, 4]] = [10, 40] @@ -1223,7 +1238,7 @@ xh[filled(xh < 5, False)] = 2 assert_equal(xh._data, [[1, 2], [2, 5]]) assert_equal(xh._mask, [[1, 0], [0, 0]]) - # + def test_hardmask_again(self): "Another test of hardmask" d = arange(5) @@ -1283,9 +1298,6 @@ testflat[:] = testflat[[2, 1, 0]] assert_equal(test, control) - - - #------------------------------------------------------------------------------ class TestFillingValues(TestCase): @@ -1482,15 +1494,35 @@ assert(np.isnan(f[0])) assert_equal(f[-1], default_fill_value(1.)) + def test_fillvalue_as_arguments(self): + "Test adding a fill_value parameter to empty/ones/zeros" + a = empty(3, fill_value=999.) + assert_equal(a.fill_value, 999.) + # + a = ones(3, fill_value=999., dtype=float) + assert_equal(a.fill_value, 999.) + # + a = zeros(3, fill_value=0., dtype=complex) + assert_equal(a.fill_value, 0.) + # + a = identity(3, fill_value=0., dtype=complex) + assert_equal(a.fill_value, 0.) + #------------------------------------------------------------------------------ class TestUfuncs(TestCase): "Test class for the application of ufuncs on MaskedArrays." + def setUp(self): "Base data definition." self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6), array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),) + self.err_status = np.geterr() + np.seterr(divide='ignore', invalid='ignore') + def tearDown(self): + np.seterr(**self.err_status) + def test_testUfuncRegression(self): "Tests new ufuncs on MaskedArrays." for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate', @@ -1522,7 +1554,7 @@ mr = mf(*args) assert_equal(ur.filled(0), mr.filled(0), f) assert_mask_equal(ur.mask, mr.mask, err_msg=f) - #........................ + def test_reduce(self): "Tests reduce on MaskedArrays." a = self.d[0] @@ -1531,7 +1563,7 @@ assert_equal(sum(a[:3], axis=0), 0) assert_equal(product(a, axis=0), 0) assert_equal(add.reduce(a), pi) - #........................ + def test_minmax(self): "Tests extrema on MaskedArrays." a = arange(1, 13).reshape(3, 4) @@ -1553,7 +1585,6 @@ assert_equal(test.mask, control.mask) self.assertTrue(not isinstance(test.mask, MaskedArray)) - #------------------------------------------------------------------------------ class TestMaskedArrayInPlaceArithmetics(TestCase): @@ -2109,7 +2140,7 @@ self.assertTrue(x[3] is masked) self.assertTrue(x[4] is masked) x[[1, 4]] = [10, 40] -# self.assertTrue( x.mask is not m) + #self.assertTrue(x.mask is not m) self.assertTrue(x[3] is masked) self.assertTrue(x[4] is not masked) assert_equal(x, [0, 10, 2, -1, 40]) @@ -2185,7 +2216,7 @@ aravel = a.ravel() assert_equal(a.shape, (1, 5)) assert_equal(a._mask.shape, a.shape) - # Checs that small_mask is preserved + # Checks that small_mask is preserved a = array([1, 2, 3, 4], mask=[0, 0, 0, 0], shrink=False) assert_equal(a.ravel()._mask, [0, 0, 0, 0]) # Test that the fill_value is preserved @@ -2367,7 +2398,7 @@ xlist = x.tolist() self.assertTrue(xlist[1] is None) self.assertTrue(xlist[-2] is None) - # ... on 2D + # ... on 2D x.shape = (3, 4) xlist = x.tolist() ctrl = [[0, None, 2, 3], [4, 5, 6, 7], [8, 9, None, 11]] @@ -2738,7 +2769,7 @@ class TestMaskedArrayFunctions(TestCase): "Test class for miscellaneous functions." - # + def setUp(self): x = np.array([1., 1., 1., -2., pi / 2.0, 4., 5., -10., 10., 1., 2., 3.]) y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.]) @@ -2900,7 +2931,7 @@ zm = masked_array(z, mask=[0, 1, 0, 0]) xf = np.where(m1, 1e+20, x) xm.set_fill_value(1e+20) - + # d = where(xm > 2, xm, -9) assert_equal(d, [-9., -9., -9., -9., -9., 4., -9., -9., 10., -9., -9., 3.]) assert_equal(d._mask, xm._mask) @@ -3128,15 +3159,15 @@ test = mask_or(mask, False) assert_equal(test, mask) # Using True as input. Won't work, but keep it for the kicks - #test = ma.mask_or(mask, True) - #control = np.array([(1, 1), (1, 1), (1, 1), (1, 1)], dtype=mtype) - #assert_equal(test, control) - # Using another array w/ the same dtype + # test = mask_or(mask, True) + # control = np.array([(1, 1), (1, 1), (1, 1), (1, 1)], dtype=mtype) + # assert_equal(test, control) + # Using another array w / the same dtype other = np.array([(0, 1), (0, 1), (0, 1), (0, 1)], dtype=mtype) test = mask_or(mask, other) control = np.array([(0, 1), (0, 1), (1, 1), (0, 1)], dtype=mtype) assert_equal(test, control) - # Using another array w/ a different dtype + # Using another array w / a different dtype othertype = [('A', np.bool), ('B', np.bool)] other = np.array([(0, 1), (0, 1), (0, 1), (0, 1)], dtype=othertype) try: @@ -3302,7 +3333,6 @@ assert_equal_records(a[-2]._data, a._data[-2]) assert_equal_records(a[-2]._mask, a._mask[-2]) - #------------------------------------------------------------------------------ class TestMaskedView(TestCase): @@ -3389,6 +3419,5 @@ assert_equal(np.argwhere(a), [[1], [3]]) ############################################################################### -#------------------------------------------------------------------------------ if __name__ == "__main__": run_module_suite() Modified: trunk/numpy/ma/tests/test_old_ma.py =================================================================== --- trunk/numpy/ma/tests/test_old_ma.py 2010-03-17 21:04:15 UTC (rev 8294) +++ trunk/numpy/ma/tests/test_old_ma.py 2010-03-18 15:23:08 UTC (rev 8295) @@ -10,29 +10,29 @@ from functools import reduce pi = numpy.pi -def eq(v,w, msg=''): - result = allclose(v,w) +def eq(v, w, msg=''): + result = allclose(v, w) if not result: print """Not eq:%s %s ---- -%s"""% (msg, str(v), str(w)) +%s""" % (msg, str(v), str(w)) return result class TestMa(TestCase): def setUp (self): - x=numpy.array([1.,1.,1.,-2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.]) - y=numpy.array([5.,0.,3., 2., -1., -4., 0., -10., 10., 1., 0., 3.]) + x = numpy.array([1., 1., 1., -2., pi / 2.0, 4., 5., -10., 10., 1., 2., 3.]) + y = numpy.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.]) a10 = 10. m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] - m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0 ,0, 1] + m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0 , 0, 1] xm = array(x, mask=m1) ym = array(y, mask=m2) z = numpy.array([-.5, 0., .5, .8]) - zm = array(z, mask=[0,1,0,0]) - xf = numpy.where(m1, 1.e+20, x) + zm = array(z, mask=[0, 1, 0, 0]) + xf = numpy.where(m1, 1e+20, x) s = x.shape - xm.set_fill_value(1.e+20) + xm.set_fill_value(1e+20) self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) def test_testBasic1d(self): @@ -43,15 +43,15 @@ self.assertEqual(shape(xm), s) self.assertEqual(xm.shape, s) self.assertEqual(xm.dtype, x.dtype) - self.assertEqual( xm.size , reduce(lambda x,y:x*y, s)) - self.assertEqual(count(xm) , len(m1) - reduce(lambda x,y:x+y, m1)) + self.assertEqual(xm.size , reduce(lambda x, y:x * y, s)) + self.assertEqual(count(xm) , len(m1) - reduce(lambda x, y:x + y, m1)) self.assertTrue(eq(xm, xf)) self.assertTrue(eq(filled(xm, 1.e20), xf)) self.assertTrue(eq(x, xm)) def test_testBasic2d(self): "Test of basic array creation and properties in 2 dimensions." - for s in [(4,3), (6,2)]: + for s in [(4, 3), (6, 2)]: (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d x.shape = s y.shape = s @@ -63,8 +63,8 @@ self.assertTrue(isMaskedArray(xm)) self.assertEqual(shape(xm), s) self.assertEqual(xm.shape, s) - self.assertEqual( xm.size , reduce(lambda x,y:x*y, s)) - self.assertEqual( count(xm) , len(m1) - reduce(lambda x,y:x+y, m1)) + self.assertEqual(xm.size , reduce(lambda x, y:x * y, s)) + self.assertEqual(count(xm) , len(m1) - reduce(lambda x, y:x + y, m1)) self.assertTrue(eq(xm, xf)) self.assertTrue(eq(filled(xm, 1.e20), xf)) self.assertTrue(eq(x, xm)) @@ -73,12 +73,12 @@ def test_testArithmetic (self): "Test of basic arithmetic." (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d - a2d = array([[1,2],[0,4]]) - a2dm = masked_array(a2d, [[0,0],[1,0]]) + a2d = array([[1, 2], [0, 4]]) + a2dm = masked_array(a2d, [[0, 0], [1, 0]]) self.assertTrue(eq (a2d * a2d, a2d * a2dm)) self.assertTrue(eq (a2d + a2d, a2d + a2dm)) self.assertTrue(eq (a2d - a2d, a2d - a2dm)) - for s in [(12,), (4,3), (2,6)]: + for s in [(12,), (4, 3), (2, 6)]: x = x.reshape(s) y = y.reshape(s) xm = xm.reshape(s) @@ -105,15 +105,15 @@ self.assertTrue(eq(x - a10, xm - a10)) self.assertTrue(eq(x * a10, xm * a10)) self.assertTrue(eq(x / a10, xm / a10)) - self.assertTrue(eq(x**2, xm**2)) - self.assertTrue(eq(abs(x)**2.5, abs(xm) **2.5)) - self.assertTrue(eq(x**y, xm**ym)) - self.assertTrue(eq(numpy.add(x,y), add(xm, ym))) - self.assertTrue(eq(numpy.subtract(x,y), subtract(xm, ym))) - self.assertTrue(eq(numpy.multiply(x,y), multiply(xm, ym))) + self.assertTrue(eq(x ** 2, xm ** 2)) + self.assertTrue(eq(abs(x) ** 2.5, abs(xm) ** 2.5)) + self.assertTrue(eq(x ** y, xm ** ym)) + self.assertTrue(eq(numpy.add(x, y), add(xm, ym))) + self.assertTrue(eq(numpy.subtract(x, y), subtract(xm, ym))) + self.assertTrue(eq(numpy.multiply(x, y), multiply(xm, ym))) olderr = numpy.seterr(divide='ignore', invalid='ignore') try: - self.assertTrue(eq(numpy.divide(x,y), divide(xm, ym))) + self.assertTrue(eq(numpy.divide(x, y), divide(xm, ym))) finally: numpy.seterr(**olderr) @@ -146,37 +146,37 @@ self.assertTrue (eq(numpy.arctan(z), arctan(zm))) self.assertTrue (eq(numpy.arctan2(x, y), arctan2(xm, ym))) self.assertTrue (eq(numpy.absolute(x), absolute(xm))) - self.assertTrue (eq(numpy.equal(x,y), equal(xm, ym))) - self.assertTrue (eq(numpy.not_equal(x,y), not_equal(xm, ym))) - self.assertTrue (eq(numpy.less(x,y), less(xm, ym))) - self.assertTrue (eq(numpy.greater(x,y), greater(xm, ym))) - self.assertTrue (eq(numpy.less_equal(x,y), less_equal(xm, ym))) - self.assertTrue (eq(numpy.greater_equal(x,y), greater_equal(xm, ym))) + self.assertTrue (eq(numpy.equal(x, y), equal(xm, ym))) + self.assertTrue (eq(numpy.not_equal(x, y), not_equal(xm, ym))) + self.assertTrue (eq(numpy.less(x, y), less(xm, ym))) + self.assertTrue (eq(numpy.greater(x, y), greater(xm, ym))) + self.assertTrue (eq(numpy.less_equal(x, y), less_equal(xm, ym))) + self.assertTrue (eq(numpy.greater_equal(x, y), greater_equal(xm, ym))) self.assertTrue (eq(numpy.conjugate(x), conjugate(xm))) - self.assertTrue (eq(numpy.concatenate((x,y)), concatenate((xm,ym)))) - self.assertTrue (eq(numpy.concatenate((x,y)), concatenate((x,y)))) - self.assertTrue (eq(numpy.concatenate((x,y)), concatenate((xm,y)))) - self.assertTrue (eq(numpy.concatenate((x,y,x)), concatenate((x,ym,x)))) + self.assertTrue (eq(numpy.concatenate((x, y)), concatenate((xm, ym)))) + self.assertTrue (eq(numpy.concatenate((x, y)), concatenate((x, y)))) + self.assertTrue (eq(numpy.concatenate((x, y)), concatenate((xm, y)))) + self.assertTrue (eq(numpy.concatenate((x, y, x)), concatenate((x, ym, x)))) def test_xtestCount (self): "Test count" - ott = array([0.,1.,2.,3.], mask=[1,0,0,0]) + ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0]) if sys.version_info[0] >= 3: - self.assertTrue( isinstance(count(ott), numpy.integer)) + self.assertTrue(isinstance(count(ott), numpy.integer)) else: - self.assertTrue( isinstance(count(ott), types.IntType)) + self.assertTrue(isinstance(count(ott), types.IntType)) self.assertEqual(3, count(ott)) self.assertEqual(1, count(1)) - self.assertTrue (eq(0, array(1,mask=[1]))) - ott=ott.reshape((2,2)) - assert isinstance(count(ott,0),numpy.ndarray) + self.assertTrue (eq(0, array(1, mask=[1]))) + ott = ott.reshape((2, 2)) + assert isinstance(count(ott, 0), numpy.ndarray) if sys.version_info[0] >= 3: assert isinstance(count(ott), numpy.integer) else: assert isinstance(count(ott), types.IntType) self.assertTrue (eq(3, count(ott))) - assert getmask(count(ott,0)) is nomask - self.assertTrue (eq([1,2],count(ott,0))) + assert getmask(count(ott, 0)) is nomask + self.assertTrue (eq([1, 2], count(ott, 0))) def test_testMinMax (self): "Test minimum and maximum." @@ -195,65 +195,65 @@ (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d self.assertTrue (eq(numpy.add.reduce(x), add.reduce(x))) self.assertTrue (eq(numpy.add.accumulate(x), add.accumulate(x))) - self.assertTrue (eq(4, sum(array(4),axis=0))) self.assertTrue (eq(4, sum(array(4), axis=0))) - self.assertTrue (eq(numpy.sum(x,axis=0), sum(x,axis=0))) - self.assertTrue (eq(numpy.sum(filled(xm,0),axis=0), sum(xm,axis=0))) - self.assertTrue (eq(numpy.sum(x,0), sum(x,0))) - self.assertTrue (eq(numpy.product(x,axis=0), product(x,axis=0))) - self.assertTrue (eq(numpy.product(x,0), product(x,0))) - self.assertTrue (eq(numpy.product(filled(xm,1),axis=0), - product(xm,axis=0))) + self.assertTrue (eq(4, sum(array(4), axis=0))) + self.assertTrue (eq(numpy.sum(x, axis=0), sum(x, axis=0))) + self.assertTrue (eq(numpy.sum(filled(xm, 0), axis=0), sum(xm, axis=0))) + self.assertTrue (eq(numpy.sum(x, 0), sum(x, 0))) + self.assertTrue (eq(numpy.product(x, axis=0), product(x, axis=0))) + self.assertTrue (eq(numpy.product(x, 0), product(x, 0))) + self.assertTrue (eq(numpy.product(filled(xm, 1), axis=0), + product(xm, axis=0))) if len(s) > 1: - self.assertTrue (eq(numpy.concatenate((x,y),1), - concatenate((xm,ym),1))) - self.assertTrue (eq(numpy.add.reduce(x,1), add.reduce(x,1))) - self.assertTrue (eq(numpy.sum(x,1), sum(x,1))) - self.assertTrue (eq(numpy.product(x,1), product(x,1))) + self.assertTrue (eq(numpy.concatenate((x, y), 1), + concatenate((xm, ym), 1))) + self.assertTrue (eq(numpy.add.reduce(x, 1), add.reduce(x, 1))) + self.assertTrue (eq(numpy.sum(x, 1), sum(x, 1))) + self.assertTrue (eq(numpy.product(x, 1), product(x, 1))) def test_testCI(self): "Test of conversions and indexing" - x1 = numpy.array([1,2,4,3]) - x2 = array(x1, mask = [1,0,0,0]) - x3 = array(x1, mask = [0,1,0,1]) + x1 = numpy.array([1, 2, 4, 3]) + x2 = array(x1, mask=[1, 0, 0, 0]) + x3 = array(x1, mask=[0, 1, 0, 1]) x4 = array(x1) # test conversion to strings junk, garbage = str(x2), repr(x2) - assert eq(numpy.sort(x1),sort(x2, fill_value=0)) + assert eq(numpy.sort(x1), sort(x2, fill_value=0)) # tests of indexing assert type(x2[1]) is type(x1[1]) assert x1[1] == x2[1] assert x2[0] is masked - assert eq(x1[2],x2[2]) - assert eq(x1[2:5],x2[2:5]) - assert eq(x1[:],x2[:]) + assert eq(x1[2], x2[2]) + assert eq(x1[2:5], x2[2:5]) + assert eq(x1[:], x2[:]) assert eq(x1[1:], x3[1:]) - x1[2]=9 - x2[2]=9 - assert eq(x1,x2) + x1[2] = 9 + x2[2] = 9 + assert eq(x1, x2) x1[1:3] = 99 x2[1:3] = 99 - assert eq(x1,x2) + assert eq(x1, x2) x2[1] = masked - assert eq(x1,x2) - x2[1:3]=masked - assert eq(x1,x2) + assert eq(x1, x2) + x2[1:3] = masked + assert eq(x1, x2) x2[:] = x1 x2[1] = masked - assert allequal(getmask(x2),array([0,1,0,0])) - x3[:] = masked_array([1,2,3,4],[0,1,1,0]) - assert allequal(getmask(x3), array([0,1,1,0])) - x4[:] = masked_array([1,2,3,4],[0,1,1,0]) - assert allequal(getmask(x4), array([0,1,1,0])) - assert allequal(x4, array([1,2,3,4])) - x1 = numpy.arange(5)*1.0 + assert allequal(getmask(x2), array([0, 1, 0, 0])) + x3[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0]) + assert allequal(getmask(x3), array([0, 1, 1, 0])) + x4[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0]) + assert allequal(getmask(x4), array([0, 1, 1, 0])) + assert allequal(x4, array([1, 2, 3, 4])) + x1 = numpy.arange(5) * 1.0 x2 = masked_values(x1, 3.0) - assert eq(x1,x2) - assert allequal(array([0,0,0,1,0],MaskType), x2.mask) + assert eq(x1, x2) + assert allequal(array([0, 0, 0, 1, 0], MaskType), x2.mask) assert eq(3.0, x2.fill_value) - x1 = array([1,'hello',2,3],object) - x2 = numpy.array([1,'hello',2,3],object) + x1 = array([1, 'hello', 2, 3], object) + x2 = numpy.array([1, 'hello', 2, 3], object) s1 = x1[1] s2 = x2[1] self.assertEqual(type(s2), str) @@ -263,7 +263,7 @@ def test_testCopySize(self): "Tests of some subtle points of copying and sizing." - n = [0,0,1,0,0] + n = [0, 0, 1, 0, 0] m = make_mask(n) m2 = make_mask(m) self.assertTrue(m is m2) @@ -272,53 +272,53 @@ x1 = numpy.arange(5) y1 = array(x1, mask=m) - self.assertTrue( y1._data is not x1) - self.assertTrue( allequal(x1,y1._data)) - self.assertTrue( y1.mask is m) + self.assertTrue(y1._data is not x1) + self.assertTrue(allequal(x1, y1._data)) + self.assertTrue(y1.mask is m) y1a = array(y1, copy=0) - self.assertTrue( y1a.mask is y1.mask) + self.assertTrue(y1a.mask is y1.mask) y2 = array(x1, mask=m, copy=0) - self.assertTrue( y2.mask is m) - self.assertTrue( y2[2] is masked) - y2[2]=9 - self.assertTrue( y2[2] is not masked) - self.assertTrue( y2.mask is not m) - self.assertTrue( allequal(y2.mask, 0)) + self.assertTrue(y2.mask is m) + self.assertTrue(y2[2] is masked) + y2[2] = 9 + self.assertTrue(y2[2] is not masked) + self.assertTrue(y2.mask is not m) + self.assertTrue(allequal(y2.mask, 0)) - y3 = array(x1*1.0, mask=m) - self.assertTrue(filled(y3).dtype is (x1*1.0).dtype) + y3 = array(x1 * 1.0, mask=m) + self.assertTrue(filled(y3).dtype is (x1 * 1.0).dtype) x4 = arange(4) x4[2] = masked y4 = resize(x4, (8,)) - self.assertTrue( eq(concatenate([x4,x4]), y4)) - self.assertTrue( eq(getmask(y4),[0,0,1,0,0,0,1,0])) - y5 = repeat(x4, (2,2,2,2), axis=0) - self.assertTrue( eq(y5, [0,0,1,1,2,2,3,3])) + self.assertTrue(eq(concatenate([x4, x4]), y4)) + self.assertTrue(eq(getmask(y4), [0, 0, 1, 0, 0, 0, 1, 0])) + y5 = repeat(x4, (2, 2, 2, 2), axis=0) + self.assertTrue(eq(y5, [0, 0, 1, 1, 2, 2, 3, 3])) y6 = repeat(x4, 2, axis=0) - self.assertTrue( eq(y5, y6)) + self.assertTrue(eq(y5, y6)) def test_testPut(self): "Test of put" d = arange(5) - n = [0,0,0,1,1] + n = [0, 0, 0, 1, 1] m = make_mask(n) - x = array(d, mask = m) - self.assertTrue( x[3] is masked) - self.assertTrue( x[4] is masked) - x[[1,4]] = [10,40] - self.assertTrue( x.mask is not m) - self.assertTrue( x[3] is masked) - self.assertTrue( x[4] is not masked) - self.assertTrue( eq(x, [0,10,2,-1,40])) + x = array(d, mask=m) + self.assertTrue(x[3] is masked) + self.assertTrue(x[4] is masked) + x[[1, 4]] = [10, 40] + self.assertTrue(x.mask is not m) + self.assertTrue(x[3] is masked) + self.assertTrue(x[4] is not masked) + self.assertTrue(eq(x, [0, 10, 2, -1, 40])) - x = array(d, mask = m) - x.put([0,1,2],[-1,100,200]) - self.assertTrue( eq(x, [-1,100,200,0,0])) - self.assertTrue( x[3] is masked) - self.assertTrue( x[4] is masked) + x = array(d, mask=m) + x.put([0, 1, 2], [-1, 100, 200]) + self.assertTrue(eq(x, [-1, 100, 200, 0, 0])) + self.assertTrue(x[3] is masked) + self.assertTrue(x[4] is masked) def test_testMaPut(self): (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d @@ -329,21 +329,21 @@ def test_testOddFeatures(self): "Test of other odd features" - x = arange(20); x=x.reshape(4,5) + x = arange(20); x = x.reshape(4, 5) x.flat[5] = 12 - assert x[1,0] == 12 + assert x[1, 0] == 12 z = x + 10j * x assert eq(z.real, x) - assert eq(z.imag, 10*x) - assert eq((z*conjugate(z)).real, 101*x*x) + assert eq(z.imag, 10 * x) + assert eq((z * conjugate(z)).real, 101 * x * x) z.imag[...] = 0.0 x = arange(10) x[3] = masked assert str(x[3]) == str(masked) c = x >= 8 - assert count(where(c,masked,masked)) == 0 - assert shape(where(c,masked,masked)) == c.shape + assert count(where(c, masked, masked)) == 0 + assert shape(where(c, masked, masked)) == c.shape z = where(c , x, masked) assert z.dtype is x.dtype assert z[3] is masked @@ -351,7 +351,7 @@ assert z[7] is masked assert z[8] is not masked assert z[9] is not masked - assert eq(x,z) + assert eq(x, z) z = where(c , masked, x) assert z.dtype is x.dtype assert z[3] is masked @@ -366,72 +366,72 @@ assert z[7] is not masked assert z[8] is masked assert z[9] is masked - assert eq(x,z) - x = array([1.,2.,3.,4.,5.]) - c = array([1,1,1,0,0]) + assert eq(x, z) + x = array([1., 2., 3., 4., 5.]) + c = array([1, 1, 1, 0, 0]) x[2] = masked z = where(c, x, -x) - assert eq(z, [1.,2.,0., -4., -5]) + assert eq(z, [1., 2., 0., -4., -5]) c[0] = masked z = where(c, x, -x) - assert eq(z, [1.,2.,0., -4., -5]) + assert eq(z, [1., 2., 0., -4., -5]) assert z[0] is masked assert z[1] is not masked assert z[2] is masked - assert eq(masked_where(greater(x, 2), x), masked_greater(x,2)) + assert eq(masked_where(greater(x, 2), x), masked_greater(x, 2)) assert eq(masked_where(greater_equal(x, 2), x), - masked_greater_equal(x,2)) - assert eq(masked_where(less(x, 2), x), masked_less(x,2)) - assert eq(masked_where(less_equal(x, 2), x), masked_less_equal(x,2)) - assert eq(masked_where(not_equal(x, 2), x), masked_not_equal(x,2)) - assert eq(masked_where(equal(x, 2), x), masked_equal(x,2)) - assert eq(masked_where(not_equal(x,2), x), masked_not_equal(x,2)) + masked_greater_equal(x, 2)) + assert eq(masked_where(less(x, 2), x), masked_less(x, 2)) + assert eq(masked_where(less_equal(x, 2), x), masked_less_equal(x, 2)) + assert eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)) + assert eq(masked_where(equal(x, 2), x), masked_equal(x, 2)) + assert eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)) assert eq(masked_inside(range(5), 1, 3), [0, 199, 199, 199, 4]) - assert eq(masked_outside(range(5), 1, 3),[199,1,2,3,199]) - assert eq(masked_inside(array(range(5), mask=[1,0,0,0,0]), 1, 3).mask, - [1,1,1,1,0]) - assert eq(masked_outside(array(range(5), mask=[0,1,0,0,0]), 1, 3).mask, - [1,1,0,0,1]) - assert eq(masked_equal(array(range(5), mask=[1,0,0,0,0]), 2).mask, - [1,0,1,0,0]) - assert eq(masked_not_equal(array([2,2,1,2,1], mask=[1,0,0,0,0]), 2).mask, - [1,0,1,0,1]) - assert eq(masked_where([1,1,0,0,0], [1,2,3,4,5]), [99,99,3,4,5]) - atest = ones((10,10,10), dtype=float32) + assert eq(masked_outside(range(5), 1, 3), [199, 1, 2, 3, 199]) + assert eq(masked_inside(array(range(5), mask=[1, 0, 0, 0, 0]), 1, 3).mask, + [1, 1, 1, 1, 0]) + assert eq(masked_outside(array(range(5), mask=[0, 1, 0, 0, 0]), 1, 3).mask, + [1, 1, 0, 0, 1]) + assert eq(masked_equal(array(range(5), mask=[1, 0, 0, 0, 0]), 2).mask, + [1, 0, 1, 0, 0]) + assert eq(masked_not_equal(array([2, 2, 1, 2, 1], mask=[1, 0, 0, 0, 0]), 2).mask, + [1, 0, 1, 0, 1]) + assert eq(masked_where([1, 1, 0, 0, 0], [1, 2, 3, 4, 5]), [99, 99, 3, 4, 5]) + atest = ones((10, 10, 10), dtype=float32) btest = zeros(atest.shape, MaskType) - ctest = masked_where(btest,atest) - assert eq(atest,ctest) + ctest = masked_where(btest, atest) + assert eq(atest, ctest) z = choose(c, (-x, x)) - assert eq(z, [1.,2.,0., -4., -5]) + assert eq(z, [1., 2., 0., -4., -5]) assert z[0] is masked assert z[1] is not masked assert z[2] is masked x = arange(6) x[5] = masked - y = arange(6)*10 - y[2]= masked - c = array([1,1,1,0,0,0], mask=[1,0,0,0,0,0]) + y = arange(6) * 10 + y[2] = masked + c = array([1, 1, 1, 0, 0, 0], mask=[1, 0, 0, 0, 0, 0]) cm = c.filled(1) - z = where(c,x,y) - zm = where(cm,x,y) + z = where(c, x, y) + zm = where(cm, x, y) assert eq(z, zm) assert getmask(zm) is nomask - assert eq(zm, [0,1,2,30,40,50]) + assert eq(zm, [0, 1, 2, 30, 40, 50]) z = where(c, masked, 1) - assert eq(z, [99,99,99,1,1,1]) + assert eq(z, [99, 99, 99, 1, 1, 1]) z = where(c, 1, masked) assert eq(z, [99, 1, 1, 99, 99, 99]) def test_testMinMax(self): "Test of minumum, maximum." - assert eq(minimum([1,2,3],[4,0,9]), [1,0,3]) - assert eq(maximum([1,2,3],[4,0,9]), [4,2,9]) + assert eq(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]) + assert eq(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]) x = arange(5) y = arange(5) - 2 x[3] = masked y[0] = masked - assert eq(minimum(x,y), where(less(x,y), x, y)) - assert eq(maximum(x,y), where(greater(x,y), x, y)) + assert eq(minimum(x, y), where(less(x, y), x, y)) + assert eq(maximum(x, y), where(greater(x, y), x, y)) assert minimum(x) == 0 assert maximum(x) == 4 @@ -440,17 +440,17 @@ x = arange(24) y = numpy.arange(24) x[5:6] = masked - x=x.reshape(2,3,4) - y=y.reshape(2,3,4) - assert eq(numpy.transpose(y,(2,0,1)), transpose(x,(2,0,1))) - assert eq(numpy.take(y, (2,0,1), 1), take(x, (2,0,1), 1)) - assert eq(numpy.inner(filled(x,0),filled(y,0)), + x = x.reshape(2, 3, 4) + y = y.reshape(2, 3, 4) + assert eq(numpy.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1))) + assert eq(numpy.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1)) + assert eq(numpy.inner(filled(x, 0), filled(y, 0)), inner(x, y)) - assert eq(numpy.outer(filled(x,0),filled(y,0)), + assert eq(numpy.outer(filled(x, 0), filled(y, 0)), outer(x, y)) y = array(['abc', 1, 'def', 2, 3], object) y[2] = masked - t = take(y,[0,3,4]) + t = take(y, [0, 3, 4]) assert t[0] == 'abc' assert t[1] == 2 assert t[2] == 3 @@ -463,27 +463,27 @@ xm = arange(10) xm[2] = masked x += 1 - assert eq(x, y+1) + assert eq(x, y + 1) xm += 1 - assert eq(x, y+1) + assert eq(x, y + 1) x = arange(10) xm = arange(10) xm[2] = masked x -= 1 - assert eq(x, y-1) + assert eq(x, y - 1) xm -= 1 - assert eq(xm, y-1) + assert eq(xm, y - 1) - x = arange(10)*1.0 - xm = arange(10)*1.0 + x = arange(10) * 1.0 + xm = arange(10) * 1.0 xm[2] = masked x *= 2.0 - assert eq(x, y*2) + assert eq(x, y * 2) xm *= 2.0 - assert eq(xm, y*2) + assert eq(xm, y * 2) - x = arange(10)*2 + x = arange(10) * 2 xm = arange(10) xm[2] = masked x /= 2 @@ -491,11 +491,11 @@ xm /= 2 assert eq(x, y) - x = arange(10)*1.0 - xm = arange(10)*1.0 + x = arange(10) * 1.0 + xm = arange(10) * 1.0 xm[2] = masked x /= 2.0 - assert eq(x, y/2.0) + assert eq(x, y / 2.0) xm /= arange(10) assert eq(xm, ones((10,))) @@ -503,23 +503,23 @@ xm = arange(10) xm[2] = masked x += 1. - assert eq(x, y+1.) + assert eq(x, y + 1.) def test_testPickle(self): "Test of pickling" import pickle x = arange(12) x[4:10:2] = masked - x = x.reshape(4,3) + x = x.reshape(4, 3) s = pickle.dumps(x) y = pickle.loads(s) - assert eq(x,y) + assert eq(x, y) def test_testMasked(self): "Test of masked element" - xx=arange(6) + xx = arange(6) xx[1] = masked - self.assertTrue(str(masked) == '--') + self.assertTrue(str(masked) == '--') self.assertTrue(xx[1] is masked) self.assertEqual(filled(xx[1], 0), 0) # don't know why these should raise an exception... @@ -530,77 +530,77 @@ def test_testAverage1(self): "Test of average." - ott = array([0.,1.,2.,3.], mask=[1,0,0,0]) - self.assertTrue(eq(2.0, average(ott,axis=0))) + ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0]) + self.assertTrue(eq(2.0, average(ott, axis=0))) self.assertTrue(eq(2.0, average(ott, weights=[1., 1., 2., 1.]))) - result, wts = average(ott, weights=[1.,1.,2.,1.], returned=1) + result, wts = average(ott, weights=[1., 1., 2., 1.], returned=1) self.assertTrue(eq(2.0, result)) self.assertTrue(wts == 4.0) ott[:] = masked - self.assertTrue(average(ott,axis=0) is masked) - ott = array([0.,1.,2.,3.], mask=[1,0,0,0]) - ott=ott.reshape(2,2) - ott[:,1] = masked - self.assertTrue(eq(average(ott,axis=0), [2.0, 0.0])) - self.assertTrue(average(ott,axis=1)[0] is masked) - self.assertTrue(eq([2.,0.], average(ott, axis=0))) + self.assertTrue(average(ott, axis=0) is masked) + ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0]) + ott = ott.reshape(2, 2) + ott[:, 1] = masked + self.assertTrue(eq(average(ott, axis=0), [2.0, 0.0])) + self.assertTrue(average(ott, axis=1)[0] is masked) + self.assertTrue(eq([2., 0.], average(ott, axis=0))) result, wts = average(ott, axis=0, returned=1) self.assertTrue(eq(wts, [1., 0.])) def test_testAverage2(self): "More tests of average." - w1 = [0,1,1,1,1,0] - w2 = [[0,1,1,1,1,0],[1,0,0,0,0,1]] - x=arange(6) + w1 = [0, 1, 1, 1, 1, 0] + w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]] + x = arange(6) self.assertTrue(allclose(average(x, axis=0), 2.5)) self.assertTrue(allclose(average(x, axis=0, weights=w1), 2.5)) - y=array([arange(6), 2.0*arange(6)]) + y = array([arange(6), 2.0 * arange(6)]) self.assertTrue(allclose(average(y, None), - numpy.add.reduce(numpy.arange(6))*3./12.)) - self.assertTrue(allclose(average(y, axis=0), numpy.arange(6) * 3./2.)) + numpy.add.reduce(numpy.arange(6)) * 3. / 12.)) + self.assertTrue(allclose(average(y, axis=0), numpy.arange(6) * 3. / 2.)) self.assertTrue(allclose(average(y, axis=1), - [average(x,axis=0), average(x,axis=0) * 2.0])) - self.assertTrue(allclose(average(y, None, weights=w2), 20./6.)) + [average(x, axis=0), average(x, axis=0) * 2.0])) + self.assertTrue(allclose(average(y, None, weights=w2), 20. / 6.)) self.assertTrue(allclose(average(y, axis=0, weights=w2), - [0.,1.,2.,3.,4.,10.])) + [0., 1., 2., 3., 4., 10.])) self.assertTrue(allclose(average(y, axis=1), - [average(x,axis=0), average(x,axis=0) * 2.0])) + [average(x, axis=0), average(x, axis=0) * 2.0])) m1 = zeros(6) - m2 = [0,0,1,1,0,0] - m3 = [[0,0,1,1,0,0],[0,1,1,1,1,0]] + m2 = [0, 0, 1, 1, 0, 0] + m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]] m4 = ones(6) m5 = [0, 1, 1, 1, 1, 1] - self.assertTrue(allclose(average(masked_array(x, m1),axis=0), 2.5)) - self.assertTrue(allclose(average(masked_array(x, m2),axis=0), 2.5)) - self.assertTrue(average(masked_array(x, m4),axis=0) is masked) - self.assertEqual(average(masked_array(x, m5),axis=0), 0.0) - self.assertEqual(count(average(masked_array(x, m4),axis=0)), 0) + self.assertTrue(allclose(average(masked_array(x, m1), axis=0), 2.5)) + self.assertTrue(allclose(average(masked_array(x, m2), axis=0), 2.5)) + self.assertTrue(average(masked_array(x, m4), axis=0) is masked) + self.assertEqual(average(masked_array(x, m5), axis=0), 0.0) + self.assertEqual(count(average(masked_array(x, m4), axis=0)), 0) z = masked_array(y, m3) - self.assertTrue(allclose(average(z, None), 20./6.)) - self.assertTrue(allclose(average(z, axis=0), [0.,1.,99.,99.,4.0, 7.5])) + self.assertTrue(allclose(average(z, None), 20. / 6.)) + self.assertTrue(allclose(average(z, axis=0), [0., 1., 99., 99., 4.0, 7.5])) self.assertTrue(allclose(average(z, axis=1), [2.5, 5.0])) - self.assertTrue(allclose( average(z,axis=0, weights=w2), - [0.,1., 99., 99., 4.0, 10.0])) + self.assertTrue(allclose(average(z, axis=0, weights=w2), + [0., 1., 99., 99., 4.0, 10.0])) a = arange(6) b = arange(6) * 3 - r1, w1 = average([[a,b],[b,a]], axis=1, returned=1) + r1, w1 = average([[a, b], [b, a]], axis=1, returned=1) self.assertEqual(shape(r1) , shape(w1)) self.assertEqual(r1.shape , w1.shape) - r2, w2 = average(ones((2,2,3)), axis=0, weights=[3,1], returned=1) + r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1) self.assertEqual(shape(w2) , shape(r2)) - r2, w2 = average(ones((2,2,3)), returned=1) + r2, w2 = average(ones((2, 2, 3)), returned=1) self.assertEqual(shape(w2) , shape(r2)) - r2, w2 = average(ones((2,2,3)), weights=ones((2,2,3)), returned=1) + r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1) self.assertTrue(shape(w2) == shape(r2)) - a2d = array([[1,2],[0,4]], float) - a2dm = masked_array(a2d, [[0,0],[1,0]]) + a2d = array([[1, 2], [0, 4]], float) + a2dm = masked_array(a2d, [[0, 0], [1, 0]]) a2da = average(a2d, axis=0) self.assertTrue(eq (a2da, [0.5, 3.0])) a2dma = average(a2dm, axis=0) - self.assertTrue(eq( a2dma, [1.0, 3.0])) + self.assertTrue(eq(a2dma, [1.0, 3.0])) a2dma = average(a2dm, axis=None) - self.assertTrue(eq(a2dma, 7./3.)) + self.assertTrue(eq(a2dma, 7. / 3.)) a2dma = average(a2dm, axis=1) self.assertTrue(eq(a2dma, [1.5, 4.0])) @@ -609,13 +609,19 @@ self.assertEqual(1.0, float(array(1))) self.assertEqual(1, int(array([[[1]]]))) self.assertEqual(1.0, float(array([[1]]))) - self.assertRaises(TypeError, float, array([1,1])) - self.assertRaises(ValueError, bool, array([0,1])) - self.assertRaises(ValueError, bool, array([0,0],mask=[0,1])) + self.assertRaises(TypeError, float, array([1, 1])) + self.assertRaises(ValueError, bool, array([0, 1])) + self.assertRaises(ValueError, bool, array([0, 0], mask=[0, 1])) def test_testScalarArithmetic(self): xm = array(0, mask=1) - self.assertTrue((1/array(0)).mask) + #TODO FIXME: Find out what the following raises a warning in r8247 + err_status = numpy.geterr() + try: + numpy.seterr(divide='ignore') + self.assertTrue((1 / array(0)).mask) + finally: + numpy.seterr(**err_status) self.assertTrue((1 + xm).mask) self.assertTrue((-xm).mask) self.assertTrue((-xm).mask) @@ -627,25 +633,25 @@ self.assertEqual(str(xm), str(masked_print_option)) def test_testArrayMethods(self): - a = array([1,3,2]) - b = array([1,3,2], mask=[1,0,1]) + a = array([1, 3, 2]) + b = array([1, 3, 2], mask=[1, 0, 1]) self.assertTrue(eq(a.any(), a._data.any())) self.assertTrue(eq(a.all(), a._data.all())) self.assertTrue(eq(a.argmax(), a._data.argmax())) self.assertTrue(eq(a.argmin(), a._data.argmin())) - self.assertTrue(eq(a.choose(0,1,2,3,4), a._data.choose(0,1,2,3,4))) - self.assertTrue(eq(a.compress([1,0,1]), a._data.compress([1,0,1]))) + self.assertTrue(eq(a.choose(0, 1, 2, 3, 4), a._data.choose(0, 1, 2, 3, 4))) + self.assertTrue(eq(a.compress([1, 0, 1]), a._data.compress([1, 0, 1]))) self.assertTrue(eq(a.conj(), a._data.conj())) self.assertTrue(eq(a.conjugate(), a._data.conjugate())) - m = array([[1,2],[3,4]]) + m = array([[1, 2], [3, 4]]) self.assertTrue(eq(m.diagonal(), m._data.diagonal())) self.assertTrue(eq(a.sum(), a._data.sum())) - self.assertTrue(eq(a.take([1,2]), a._data.take([1,2]))) + self.assertTrue(eq(a.take([1, 2]), a._data.take([1, 2]))) self.assertTrue(eq(m.transpose(), m._data.transpose())) def test_testArrayAttributes(self): - a = array([1,3,2]) - b = array([1,3,2], mask=[1,0,1]) + a = array([1, 3, 2]) + b = array([1, 3, 2], mask=[1, 0, 1]) self.assertEqual(a.ndim, 1) def test_testAPI(self): @@ -653,21 +659,21 @@ if m not in dir(MaskedArray) and not m.startswith('_')]) def test_testSingleElementSubscript(self): - a = array([1,3,2]) - b = array([1,3,2], mask=[1,0,1]) + a = array([1, 3, 2]) + b = array([1, 3, 2], mask=[1, 0, 1]) self.assertEqual(a[0].shape, ()) self.assertEqual(b[0].shape, ()) self.assertEqual(b[1].shape, ()) class TestUfuncs(TestCase): def setUp(self): - self.d = (array([1.0, 0, -1, pi/2]*2, mask=[0,1]+[0]*6), - array([1.0, 0, -1, pi/2]*2, mask=[1,0]+[0]*6),) + self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6), + array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),) def test_testUfuncRegression(self): f_invalid_ignore = ['sqrt', 'arctanh', 'arcsin', 'arccos', - 'arccosh', 'arctanh', 'log', 'log10','divide', + 'arccosh', 'arctanh', 'log', 'log10', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod'] for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate', 'sin', 'cos', 'tan', @@ -709,38 +715,38 @@ def test_reduce(self): a = self.d[0] - self.assertFalse(alltrue(a,axis=0)) - self.assertTrue(sometrue(a,axis=0)) - self.assertEqual(sum(a[:3],axis=0), 0) - self.assertEqual(product(a,axis=0), 0) + self.assertFalse(alltrue(a, axis=0)) + self.assertTrue(sometrue(a, axis=0)) + self.assertEqual(sum(a[:3], axis=0), 0) + self.assertEqual(product(a, axis=0), 0) def test_minmax(self): - a = arange(1,13).reshape(3,4) - amask = masked_where(a < 5,a) + a = arange(1, 13).reshape(3, 4) + amask = masked_where(a < 5, a) self.assertEqual(amask.max(), a.max()) self.assertEqual(amask.min(), 5) self.assertTrue((amask.max(0) == a.max(0)).all()) - self.assertTrue((amask.min(0) == [5,6,7,8]).all()) + self.assertTrue((amask.min(0) == [5, 6, 7, 8]).all()) self.assertTrue(amask.max(1)[0].mask) self.assertTrue(amask.min(1)[0].mask) def test_nonzero(self): for t in "?bhilqpBHILQPfdgFDGO": - x = array([1,0,2,0], mask=[0,0,1,1]) + x = array([1, 0, 2, 0], mask=[0, 0, 1, 1]) self.assertTrue(eq(nonzero(x), [0])) class TestArrayMethods(TestCase): def setUp(self): - x = numpy.array([ 8.375, 7.545, 8.828, 8.5 , 1.757, 5.928, - 8.43 , 7.78 , 9.865, 5.878, 8.979, 4.732, - 3.012, 6.022, 5.095, 3.116, 5.238, 3.957, - 6.04 , 9.63 , 7.712, 3.382, 4.489, 6.479, - 7.189, 9.645, 5.395, 4.961, 9.894, 2.893, - 7.357, 9.828, 6.272, 3.758, 6.693, 0.993]) - X = x.reshape(6,6) - XX = x.reshape(3,2,2,3) + x = numpy.array([ 8.375, 7.545, 8.828, 8.5 , 1.757, 5.928, + 8.43 , 7.78 , 9.865, 5.878, 8.979, 4.732, + 3.012, 6.022, 5.095, 3.116, 5.238, 3.957, + 6.04 , 9.63 , 7.712, 3.382, 4.489, 6.479, + 7.189, 9.645, 5.395, 4.961, 9.894, 2.893, + 7.357, 9.828, 6.272, 3.758, 6.693, 0.993]) + X = x.reshape(6, 6) + XX = x.reshape(3, 2, 2, 3) m = numpy.array([0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, @@ -748,9 +754,9 @@ 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0]) - mx = array(data=x,mask=m) - mX = array(data=X,mask=m.reshape(X.shape)) - mXX = array(data=XX,mask=m.reshape(XX.shape)) + mx = array(data=x, mask=m) + mX = array(data=X, mask=m.reshape(X.shape)) + mXX = array(data=XX, mask=m.reshape(XX.shape)) m2 = numpy.array([1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, @@ -758,73 +764,73 @@ 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1]) - m2x = array(data=x,mask=m2) - m2X = array(data=X,mask=m2.reshape(X.shape)) - m2XX = array(data=XX,mask=m2.reshape(XX.shape)) - self.d = (x,X,XX,m,mx,mX,mXX) + m2x = array(data=x, mask=m2) + m2X = array(data=X, mask=m2.reshape(X.shape)) + m2XX = array(data=XX, mask=m2.reshape(XX.shape)) + self.d = (x, X, XX, m, mx, mX, mXX) #------------------------------------------------------ def test_trace(self): - (x,X,XX,m,mx,mX,mXX,) = self.d + (x, X, XX, m, mx, mX, mXX,) = self.d mXdiag = mX.diagonal() self.assertEqual(mX.trace(), mX.diagonal().compressed().sum()) self.assertTrue(eq(mX.trace(), - X.trace() - sum(mXdiag.mask*X.diagonal(),axis=0))) + X.trace() - sum(mXdiag.mask * X.diagonal(), axis=0))) def test_clip(self): - (x,X,XX,m,mx,mX,mXX,) = self.d - clipped = mx.clip(2,8) - self.assertTrue(eq(clipped.mask,mx.mask)) - self.assertTrue(eq(clipped._data,x.clip(2,8))) - self.assertTrue(eq(clipped._data,mx._data.clip(2,8))) + (x, X, XX, m, mx, mX, mXX,) = self.d + clipped = mx.clip(2, 8) + self.assertTrue(eq(clipped.mask, mx.mask)) + self.assertTrue(eq(clipped._data, x.clip(2, 8))) + self.assertTrue(eq(clipped._data, mx._data.clip(2, 8))) def test_ptp(self): - (x,X,XX,m,mx,mX,mXX,) = self.d - (n,m) = X.shape - self.assertEqual(mx.ptp(),mx.compressed().ptp()) - rows = numpy.zeros(n,numpy.float_) - cols = numpy.zeros(m,numpy.float_) + (x, X, XX, m, mx, mX, mXX,) = self.d + (n, m) = X.shape + self.assertEqual(mx.ptp(), mx.compressed().ptp()) + rows = numpy.zeros(n, numpy.float_) + cols = numpy.zeros(m, numpy.float_) for k in range(m): - cols[k] = mX[:,k].compressed().ptp() + cols[k] = mX[:, k].compressed().ptp() for k in range(n): rows[k] = mX[k].compressed().ptp() - self.assertTrue(eq(mX.ptp(0),cols)) - self.assertTrue(eq(mX.ptp(1),rows)) + self.assertTrue(eq(mX.ptp(0), cols)) + self.assertTrue(eq(mX.ptp(1), rows)) def test_swapaxes(self): - (x,X,XX,m,mx,mX,mXX,) = self.d - mXswapped = mX.swapaxes(0,1) - self.assertTrue(eq(mXswapped[-1],mX[:,-1])) - mXXswapped = mXX.swapaxes(0,2) - self.assertEqual(mXXswapped.shape,(2,2,3,3)) + (x, X, XX, m, mx, mX, mXX,) = self.d + mXswapped = mX.swapaxes(0, 1) + self.assertTrue(eq(mXswapped[-1], mX[:, -1])) + mXXswapped = mXX.swapaxes(0, 2) + self.assertEqual(mXXswapped.shape, (2, 2, 3, 3)) def test_cumprod(self): - (x,X,XX,m,mx,mX,mXX,) = self.d + (x, X, XX, m, mx, mX, mXX,) = self.d mXcp = mX.cumprod(0) - self.assertTrue(eq(mXcp._data,mX.filled(1).cumprod(0))) + self.assertTrue(eq(mXcp._data, mX.filled(1).cumprod(0))) mXcp = mX.cumprod(1) - self.assertTrue(eq(mXcp._data,mX.filled(1).cumprod(1))) + self.assertTrue(eq(mXcp._data, mX.filled(1).cumprod(1))) def test_cumsum(self): - (x,X,XX,m,mx,mX,mXX,) = self.d + (x, X, XX, m, mx, mX, mXX,) = self.d mXcp = mX.cumsum(0) - self.assertTrue(eq(mXcp._data,mX.filled(0).cumsum(0))) + self.assertTrue(eq(mXcp._data, mX.filled(0).cumsum(0))) mXcp = mX.cumsum(1) - self.assertTrue(eq(mXcp._data,mX.filled(0).cumsum(1))) + self.assertTrue(eq(mXcp._data, mX.filled(0).cumsum(1))) def test_varstd(self): - (x,X,XX,m,mx,mX,mXX,) = self.d - self.assertTrue(eq(mX.var(axis=None),mX.compressed().var())) - self.assertTrue(eq(mX.std(axis=None),mX.compressed().std())) - self.assertTrue(eq(mXX.var(axis=3).shape,XX.var(axis=3).shape)) - self.assertTrue(eq(mX.var().shape,X.var().shape)) - (mXvar0,mXvar1) = (mX.var(axis=0), mX.var(axis=1)) + (x, X, XX, m, mx, mX, mXX,) = self.d + self.assertTrue(eq(mX.var(axis=None), mX.compressed().var())) + self.assertTrue(eq(mX.std(axis=None), mX.compressed().std())) + self.assertTrue(eq(mXX.var(axis=3).shape, XX.var(axis=3).shape)) + self.assertTrue(eq(mX.var().shape, X.var().shape)) + (mXvar0, mXvar1) = (mX.var(axis=0), mX.var(axis=1)) for k in range(6): - self.assertTrue(eq(mXvar1[k],mX[k].compressed().var())) - self.assertTrue(eq(mXvar0[k],mX[:,k].compressed().var())) + self.assertTrue(eq(mXvar1[k], mX[k].compressed().var())) + self.assertTrue(eq(mXvar0[k], mX[:, k].compressed().var())) self.assertTrue(eq(numpy.sqrt(mXvar0[k]), - mX[:,k].compressed().std())) + mX[:, k].compressed().std())) def eqmask(m1, m2): From numpy-svn at scipy.org Thu Mar 18 17:41:04 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Thu, 18 Mar 2010 16:41:04 -0500 (CDT) Subject: [Numpy-svn] r8296 - trunk/numpy/core/include/numpy Message-ID: <20100318214104.C42A439CAFD@scipy.org> Author: ptvirtan Date: 2010-03-18 16:41:03 -0500 (Thu, 18 Mar 2010) New Revision: 8296 Modified: trunk/numpy/core/include/numpy/npy_cpu.h Log: BUG: include numpyconfig.h in npy_cpu.h to address #1371 Modified: trunk/numpy/core/include/numpy/npy_cpu.h =================================================================== --- trunk/numpy/core/include/numpy/npy_cpu.h 2010-03-18 15:23:08 UTC (rev 8295) +++ trunk/numpy/core/include/numpy/npy_cpu.h 2010-03-18 21:41:03 UTC (rev 8296) @@ -18,6 +18,8 @@ #ifndef _NPY_CPUARCH_H_ #define _NPY_CPUARCH_H_ +#include "numpyconfig.h" + #if defined( __i386__ ) || defined(i386) || defined(_M_IX86) /* * __i386__ is defined by gcc and Intel compiler on Linux, @@ -96,6 +98,9 @@ ((char*)(dst))[5] = ((char*)(src))[5]; \ ((char*)(dst))[6] = ((char*)(src))[6]; \ ((char*)(dst))[7] = ((char*)(src))[7]; + #else + #error Unknown architecture, please report this to numpy maintainers with \ + information about your platform (OS, CPU and compiler) #endif #endif From numpy-svn at scipy.org Thu Mar 18 17:43:07 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Thu, 18 Mar 2010 16:43:07 -0500 (CDT) Subject: [Numpy-svn] r8297 - in trunk/numpy/lib: . tests Message-ID: <20100318214307.63ACB39CB01@scipy.org> Author: ptvirtan Date: 2010-03-18 16:43:07 -0500 (Thu, 18 Mar 2010) New Revision: 8297 Modified: trunk/numpy/lib/function_base.py trunk/numpy/lib/tests/test_function_base.py Log: BUG: lib: fix #1435, np.gradient should use zeros_like Thanks to Ryan May for the patch. Modified: trunk/numpy/lib/function_base.py =================================================================== --- trunk/numpy/lib/function_base.py 2010-03-18 21:41:03 UTC (rev 8296) +++ trunk/numpy/lib/function_base.py 2010-03-18 21:43:07 UTC (rev 8297) @@ -808,7 +808,7 @@ for axis in range(N): # select out appropriate parts for this dimension - out = zeros(f.shape, f.dtype.char) + out = np.zeros_like(f).astype(otype) slice1[axis] = slice(1, -1) slice2[axis] = slice(2, None) slice3[axis] = slice(None, -2) Modified: trunk/numpy/lib/tests/test_function_base.py =================================================================== --- trunk/numpy/lib/tests/test_function_base.py 2010-03-18 21:41:03 UTC (rev 8296) +++ trunk/numpy/lib/tests/test_function_base.py 2010-03-18 21:43:07 UTC (rev 8297) @@ -266,6 +266,11 @@ assert_raises(SyntaxError, gradient, x, array([1.,1.]), array([1.,1.]), array([1.,1.])) + def test_masked(self): + # Make sure that gradient supports subclasses like masked arrays + x = np.ma.array([[1,1],[3,4]]) + assert_equal(type(gradient(x)[0]), type(x)) + class TestAngle(TestCase): def test_basic(self): x = [1+3j,sqrt(2)/2.0+1j*sqrt(2)/2,1,1j,-1,-1j,1-3j,-1+3j] From numpy-svn at scipy.org Sun Mar 21 18:30:39 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 21 Mar 2010 17:30:39 -0500 (CDT) Subject: [Numpy-svn] r8298 - in trunk/numpy/core: src/multiarray tests Message-ID: <20100321223039.B2C1639CAF3@scipy.org> Author: ptvirtan Date: 2010-03-21 17:30:39 -0500 (Sun, 21 Mar 2010) New Revision: 8298 Modified: trunk/numpy/core/src/multiarray/ctors.c trunk/numpy/core/tests/test_regression.py Log: BUG: core: fix #1436 by reverting r8144 r8144 "ENH: core: make PyArray_FromAny error message more clear" is a cosmetic fix -- a revised one could be committed later on in, but let's have this one bug fixed quick. Also, add a regression test. Modified: trunk/numpy/core/src/multiarray/ctors.c =================================================================== --- trunk/numpy/core/src/multiarray/ctors.c 2010-03-18 21:43:07 UTC (rev 8297) +++ trunk/numpy/core/src/multiarray/ctors.c 2010-03-21 22:30:39 UTC (rev 8298) @@ -1820,27 +1820,14 @@ * If object was explicitly requested, * then try nested list object array creation */ + PyErr_Clear(); if (isobject) { - PyErr_Clear(); Py_INCREF(newtype); r = ObjectArray_FromNestedList (op, newtype, flags & FORTRAN); seq = TRUE; Py_DECREF(newtype); } - else if ((newtype->type_num == PyArray_STRING || - newtype->type_num == PyArray_UNICODE) - && PySequence_Size(op) > 0) - { - /* It is not possible to set a sequence into - * a string/unicode 0-d array item -- bail out early - * so that the error message is more clear. - */ - seq = TRUE; - } - else { - PyErr_Clear(); - } } else { seq = TRUE; Modified: trunk/numpy/core/tests/test_regression.py =================================================================== --- trunk/numpy/core/tests/test_regression.py 2010-03-18 21:43:07 UTC (rev 8297) +++ trunk/numpy/core/tests/test_regression.py 2010-03-21 22:30:39 UTC (rev 8298) @@ -1281,5 +1281,9 @@ assert isinstance(path, (str, unicode)) assert path != '' + def test_0d_string_scalar(self): + # Bug #1436; the following should succeed + np.asarray('x', '>c') + if __name__ == "__main__": run_module_suite() From numpy-svn at scipy.org Sun Mar 21 21:45:40 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 21 Mar 2010 20:45:40 -0500 (CDT) Subject: [Numpy-svn] r8299 - in trunk/numpy/lib: . tests Message-ID: <20100322014540.58FAF39CB02@scipy.org> Author: pierregm Date: 2010-03-21 20:45:40 -0500 (Sun, 21 Mar 2010) New Revision: 8299 Modified: trunk/numpy/lib/function_base.py trunk/numpy/lib/tests/test_function_base.py Log: * Use putmask instead of fancy indexing in _nanop (bug #1421) Modified: trunk/numpy/lib/function_base.py =================================================================== --- trunk/numpy/lib/function_base.py 2010-03-21 22:30:39 UTC (rev 8298) +++ trunk/numpy/lib/function_base.py 2010-03-22 01:45:40 UTC (rev 8299) @@ -1282,7 +1282,10 @@ # We only need to take care of NaN's in floating point arrays if not np.issubdtype(y.dtype, int): - y[mask] = fill + # y[mask] = fill + # We can't use fancy indexing here as it'll mess w/ MaskedArrays + # Instead, let's fill the array directly... + np.putmask(y, mask, fill) res = op(y, axis=axis) mask_all_along_axis = mask.all(axis=axis) Modified: trunk/numpy/lib/tests/test_function_base.py =================================================================== --- trunk/numpy/lib/tests/test_function_base.py 2010-03-21 22:30:39 UTC (rev 8298) +++ trunk/numpy/lib/tests/test_function_base.py 2010-03-22 01:45:40 UTC (rev 8299) @@ -10,70 +10,70 @@ class TestAny(TestCase): def test_basic(self): - y1 = [0,0,1,0] - y2 = [0,0,0,0] - y3 = [1,0,1,0] + y1 = [0, 0, 1, 0] + y2 = [0, 0, 0, 0] + y3 = [1, 0, 1, 0] assert(any(y1)) assert(any(y3)) assert(not any(y2)) def test_nd(self): - y1 = [[0,0,0],[0,1,0],[1,1,0]] + y1 = [[0, 0, 0], [0, 1, 0], [1, 1, 0]] assert(any(y1)) - assert_array_equal(sometrue(y1,axis=0),[1,1,0]) - assert_array_equal(sometrue(y1,axis=1),[0,1,1]) + assert_array_equal(sometrue(y1, axis=0), [1, 1, 0]) + assert_array_equal(sometrue(y1, axis=1), [0, 1, 1]) class TestAll(TestCase): def test_basic(self): - y1 = [0,1,1,0] - y2 = [0,0,0,0] - y3 = [1,1,1,1] + y1 = [0, 1, 1, 0] + y2 = [0, 0, 0, 0] + y3 = [1, 1, 1, 1] assert(not all(y1)) assert(all(y3)) assert(not all(y2)) assert(all(~array(y2))) def test_nd(self): - y1 = [[0,0,1],[0,1,1],[1,1,1]] + y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]] assert(not all(y1)) - assert_array_equal(alltrue(y1,axis=0),[0,0,1]) - assert_array_equal(alltrue(y1,axis=1),[0,0,1]) + assert_array_equal(alltrue(y1, axis=0), [0, 0, 1]) + assert_array_equal(alltrue(y1, axis=1), [0, 0, 1]) class TestAverage(TestCase): def test_basic(self): - y1 = array([1,2,3]) - assert(average(y1,axis=0) == 2.) - y2 = array([1.,2.,3.]) - assert(average(y2,axis=0) == 2.) - y3 = [0.,0.,0.] - assert(average(y3,axis=0) == 0.) + y1 = array([1, 2, 3]) + assert(average(y1, axis=0) == 2.) + y2 = array([1., 2., 3.]) + assert(average(y2, axis=0) == 2.) + y3 = [0., 0., 0.] + assert(average(y3, axis=0) == 0.) - y4 = ones((4,4)) - y4[0,1] = 0 - y4[1,0] = 2 + y4 = ones((4, 4)) + y4[0, 1] = 0 + y4[1, 0] = 2 assert_almost_equal(y4.mean(0), average(y4, 0)) assert_almost_equal(y4.mean(1), average(y4, 1)) - y5 = rand(5,5) + y5 = rand(5, 5) assert_almost_equal(y5.mean(0), average(y5, 0)) assert_almost_equal(y5.mean(1), average(y5, 1)) - y6 = matrix(rand(5,5)) - assert_array_equal(y6.mean(0), average(y6,0)) + y6 = matrix(rand(5, 5)) + assert_array_equal(y6.mean(0), average(y6, 0)) def test_weights(self): y = arange(10) w = arange(10) - assert_almost_equal(average(y, weights=w), (arange(10)**2).sum()*1./arange(10).sum()) + assert_almost_equal(average(y, weights=w), (arange(10) ** 2).sum()*1. / arange(10).sum()) - y1 = array([[1,2,3],[4,5,6]]) - w0 = [1,2] - actual = average(y1,weights=w0,axis=0) - desired = array([3.,4.,5.]) + y1 = array([[1, 2, 3], [4, 5, 6]]) + w0 = [1, 2] + actual = average(y1, weights=w0, axis=0) + desired = array([3., 4., 5.]) assert_almost_equal(actual, desired) - w1 = [0,0,1] + w1 = [0, 0, 1] desired = array([3., 6.]) assert_almost_equal(average(y1, weights=w1, axis=1), desired) @@ -82,7 +82,7 @@ # 2D Case - w2 = [[0,0,1],[0,0,2]] + w2 = [[0, 0, 1], [0, 0, 2]] desired = array([3., 6.]) assert_array_equal(average(y1, weights=w2, axis=1), desired) @@ -90,345 +90,345 @@ def test_returned(self): - y = array([[1,2,3],[4,5,6]]) + y = array([[1, 2, 3], [4, 5, 6]]) # No weights avg, scl = average(y, returned=True) assert_equal(scl, 6.) avg, scl = average(y, 0, returned=True) - assert_array_equal(scl, array([2.,2.,2.])) + assert_array_equal(scl, array([2., 2., 2.])) avg, scl = average(y, 1, returned=True) - assert_array_equal(scl, array([3.,3.])) + assert_array_equal(scl, array([3., 3.])) # With weights - w0 = [1,2] + w0 = [1, 2] avg, scl = average(y, weights=w0, axis=0, returned=True) assert_array_equal(scl, array([3., 3., 3.])) - w1 = [1,2,3] + w1 = [1, 2, 3] avg, scl = average(y, weights=w1, axis=1, returned=True) assert_array_equal(scl, array([6., 6.])) - w2 = [[0,0,1],[1,2,3]] + w2 = [[0, 0, 1], [1, 2, 3]] avg, scl = average(y, weights=w2, axis=1, returned=True) - assert_array_equal(scl, array([1.,6.])) + assert_array_equal(scl, array([1., 6.])) class TestSelect(TestCase): - def _select(self,cond,values,default=0): + def _select(self, cond, values, default=0): output = [] for m in range(len(cond)): - output += [V[m] for V,C in zip(values,cond) if C[m]] or [default] + output += [V[m] for V, C in zip(values, cond) if C[m]] or [default] return output def test_basic(self): - choices = [array([1,2,3]), - array([4,5,6]), - array([7,8,9])] - conditions = [array([0,0,0]), - array([0,1,0]), - array([0,0,1])] - assert_array_equal(select(conditions,choices,default=15), - self._select(conditions,choices,default=15)) + choices = [array([1, 2, 3]), + array([4, 5, 6]), + array([7, 8, 9])] + conditions = [array([0, 0, 0]), + array([0, 1, 0]), + array([0, 0, 1])] + assert_array_equal(select(conditions, choices, default=15), + self._select(conditions, choices, default=15)) - assert_equal(len(choices),3) - assert_equal(len(conditions),3) + assert_equal(len(choices), 3) + assert_equal(len(conditions), 3) class TestInsert(TestCase): def test_basic(self): - a = [1,2,3] - assert_equal(insert(a,0,1), [1,1,2,3]) - assert_equal(insert(a,3,1), [1,2,3,1]) - assert_equal(insert(a,[1,1,1],[1,2,3]), [1,1,2,3,2,3]) + a = [1, 2, 3] + assert_equal(insert(a, 0, 1), [1, 1, 2, 3]) + assert_equal(insert(a, 3, 1), [1, 2, 3, 1]) + assert_equal(insert(a, [1, 1, 1], [1, 2, 3]), [1, 1, 2, 3, 2, 3]) class TestAmax(TestCase): def test_basic(self): - a = [3,4,5,10,-3,-5,6.0] - assert_equal(amax(a),10.0) - b = [[3,6.0, 9.0], - [4,10.0,5.0], - [8,3.0,2.0]] - assert_equal(amax(b,axis=0),[8.0,10.0,9.0]) - assert_equal(amax(b,axis=1),[9.0,10.0,8.0]) + a = [3, 4, 5, 10, -3, -5, 6.0] + assert_equal(amax(a), 10.0) + b = [[3, 6.0, 9.0], + [4, 10.0, 5.0], + [8, 3.0, 2.0]] + assert_equal(amax(b, axis=0), [8.0, 10.0, 9.0]) + assert_equal(amax(b, axis=1), [9.0, 10.0, 8.0]) class TestAmin(TestCase): def test_basic(self): - a = [3,4,5,10,-3,-5,6.0] - assert_equal(amin(a),-5.0) - b = [[3,6.0, 9.0], - [4,10.0,5.0], - [8,3.0,2.0]] - assert_equal(amin(b,axis=0),[3.0,3.0,2.0]) - assert_equal(amin(b,axis=1),[3.0,4.0,2.0]) + a = [3, 4, 5, 10, -3, -5, 6.0] + assert_equal(amin(a), -5.0) + b = [[3, 6.0, 9.0], + [4, 10.0, 5.0], + [8, 3.0, 2.0]] + assert_equal(amin(b, axis=0), [3.0, 3.0, 2.0]) + assert_equal(amin(b, axis=1), [3.0, 4.0, 2.0]) class TestPtp(TestCase): def test_basic(self): - a = [3,4,5,10,-3,-5,6.0] - assert_equal(ptp(a,axis=0),15.0) - b = [[3,6.0, 9.0], - [4,10.0,5.0], - [8,3.0,2.0]] - assert_equal(ptp(b,axis=0),[5.0,7.0,7.0]) - assert_equal(ptp(b,axis=-1),[6.0,6.0,6.0]) + a = [3, 4, 5, 10, -3, -5, 6.0] + assert_equal(ptp(a, axis=0), 15.0) + b = [[3, 6.0, 9.0], + [4, 10.0, 5.0], + [8, 3.0, 2.0]] + assert_equal(ptp(b, axis=0), [5.0, 7.0, 7.0]) + assert_equal(ptp(b, axis= -1), [6.0, 6.0, 6.0]) class TestCumsum(TestCase): def test_basic(self): - ba = [1,2,10,11,6,5,4] - ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]] - for ctype in [int8,uint8,int16,uint16,int32,uint32, - float32,float64,complex64,complex128]: - a = array(ba,ctype) - a2 = array(ba2,ctype) - assert_array_equal(cumsum(a,axis=0), array([1,3,13,24,30,35,39],ctype)) - assert_array_equal(cumsum(a2,axis=0), array([[1,2,3,4],[6,8,10,13], - [16,11,14,18]],ctype)) - assert_array_equal(cumsum(a2,axis=1), - array([[1,3,6,10], - [5,11,18,27], - [10,13,17,22]],ctype)) + ba = [1, 2, 10, 11, 6, 5, 4] + ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] + for ctype in [int8, uint8, int16, uint16, int32, uint32, + float32, float64, complex64, complex128]: + a = array(ba, ctype) + a2 = array(ba2, ctype) + assert_array_equal(cumsum(a, axis=0), array([1, 3, 13, 24, 30, 35, 39], ctype)) + assert_array_equal(cumsum(a2, axis=0), array([[1, 2, 3, 4], [6, 8, 10, 13], + [16, 11, 14, 18]], ctype)) + assert_array_equal(cumsum(a2, axis=1), + array([[1, 3, 6, 10], + [5, 11, 18, 27], + [10, 13, 17, 22]], ctype)) class TestProd(TestCase): def test_basic(self): - ba = [1,2,10,11,6,5,4] - ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]] - for ctype in [int16,uint16,int32,uint32, - float32,float64,complex64,complex128]: - a = array(ba,ctype) - a2 = array(ba2,ctype) + ba = [1, 2, 10, 11, 6, 5, 4] + ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] + for ctype in [int16, uint16, int32, uint32, + float32, float64, complex64, complex128]: + a = array(ba, ctype) + a2 = array(ba2, ctype) if ctype in ['1', 'b']: self.assertRaises(ArithmeticError, prod, a) self.assertRaises(ArithmeticError, prod, a2, 1) self.assertRaises(ArithmeticError, prod, a) else: - assert_equal(prod(a,axis=0),26400) - assert_array_equal(prod(a2,axis=0), - array([50,36,84,180],ctype)) - assert_array_equal(prod(a2,axis=-1),array([24, 1890, 600],ctype)) + assert_equal(prod(a, axis=0), 26400) + assert_array_equal(prod(a2, axis=0), + array([50, 36, 84, 180], ctype)) + assert_array_equal(prod(a2, axis= -1), array([24, 1890, 600], ctype)) class TestCumprod(TestCase): def test_basic(self): - ba = [1,2,10,11,6,5,4] - ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]] - for ctype in [int16,uint16,int32,uint32, - float32,float64,complex64,complex128]: - a = array(ba,ctype) - a2 = array(ba2,ctype) + ba = [1, 2, 10, 11, 6, 5, 4] + ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] + for ctype in [int16, uint16, int32, uint32, + float32, float64, complex64, complex128]: + a = array(ba, ctype) + a2 = array(ba2, ctype) if ctype in ['1', 'b']: self.assertRaises(ArithmeticError, cumprod, a) self.assertRaises(ArithmeticError, cumprod, a2, 1) self.assertRaises(ArithmeticError, cumprod, a) else: - assert_array_equal(cumprod(a,axis=-1), + assert_array_equal(cumprod(a, axis= -1), array([1, 2, 20, 220, - 1320, 6600, 26400],ctype)) - assert_array_equal(cumprod(a2,axis=0), - array([[ 1, 2, 3, 4], - [ 5, 12, 21, 36], - [50, 36, 84, 180]],ctype)) - assert_array_equal(cumprod(a2,axis=-1), - array([[ 1, 2, 6, 24], + 1320, 6600, 26400], ctype)) + assert_array_equal(cumprod(a2, axis=0), + array([[ 1, 2, 3, 4], + [ 5, 12, 21, 36], + [50, 36, 84, 180]], ctype)) + assert_array_equal(cumprod(a2, axis= -1), + array([[ 1, 2, 6, 24], [ 5, 30, 210, 1890], - [10, 30, 120, 600]],ctype)) + [10, 30, 120, 600]], ctype)) class TestDiff(TestCase): def test_basic(self): - x = [1,4,6,7,12] - out = array([3,2,1,5]) - out2 = array([-1,-1,4]) - out3 = array([0,5]) - assert_array_equal(diff(x),out) - assert_array_equal(diff(x,n=2),out2) - assert_array_equal(diff(x,n=3),out3) + x = [1, 4, 6, 7, 12] + out = array([3, 2, 1, 5]) + out2 = array([-1, -1, 4]) + out3 = array([0, 5]) + assert_array_equal(diff(x), out) + assert_array_equal(diff(x, n=2), out2) + assert_array_equal(diff(x, n=3), out3) def test_nd(self): - x = 20*rand(10,20,30) - out1 = x[:,:,1:] - x[:,:,:-1] - out2 = out1[:,:,1:] - out1[:,:,:-1] - out3 = x[1:,:,:] - x[:-1,:,:] - out4 = out3[1:,:,:] - out3[:-1,:,:] - assert_array_equal(diff(x),out1) - assert_array_equal(diff(x,n=2),out2) - assert_array_equal(diff(x,axis=0),out3) - assert_array_equal(diff(x,n=2,axis=0),out4) + x = 20 * rand(10, 20, 30) + out1 = x[:, :, 1:] - x[:, :, :-1] + out2 = out1[:, :, 1:] - out1[:, :, :-1] + out3 = x[1:, :, :] - x[:-1, :, :] + out4 = out3[1:, :, :] - out3[:-1, :, :] + assert_array_equal(diff(x), out1) + assert_array_equal(diff(x, n=2), out2) + assert_array_equal(diff(x, axis=0), out3) + assert_array_equal(diff(x, n=2, axis=0), out4) class TestGradient(TestCase): def test_basic(self): - x = array([[1,1],[3,4]]) - dx = [array([[2.,3.],[2.,3.]]), - array([[0.,0.],[1.,1.]])] + x = array([[1, 1], [3, 4]]) + dx = [array([[2., 3.], [2., 3.]]), + array([[0., 0.], [1., 1.]])] assert_array_equal(gradient(x), dx) def test_badargs(self): # for 2D array, gradient can take 0,1, or 2 extra args - x = array([[1,1],[3,4]]) - assert_raises(SyntaxError, gradient, x, array([1.,1.]), - array([1.,1.]), array([1.,1.])) + x = array([[1, 1], [3, 4]]) + assert_raises(SyntaxError, gradient, x, array([1., 1.]), + array([1., 1.]), array([1., 1.])) def test_masked(self): # Make sure that gradient supports subclasses like masked arrays - x = np.ma.array([[1,1],[3,4]]) + x = np.ma.array([[1, 1], [3, 4]]) assert_equal(type(gradient(x)[0]), type(x)) class TestAngle(TestCase): def test_basic(self): - x = [1+3j,sqrt(2)/2.0+1j*sqrt(2)/2,1,1j,-1,-1j,1-3j,-1+3j] + x = [1 + 3j, sqrt(2) / 2.0 + 1j * sqrt(2) / 2, 1, 1j, -1, -1j, 1 - 3j, -1 + 3j] y = angle(x) - yo = [arctan(3.0/1.0),arctan(1.0),0,pi/2,pi,-pi/2.0, - -arctan(3.0/1.0),pi-arctan(3.0/1.0)] - z = angle(x,deg=1) - zo = array(yo)*180/pi - assert_array_almost_equal(y,yo,11) - assert_array_almost_equal(z,zo,11) + yo = [arctan(3.0 / 1.0), arctan(1.0), 0, pi / 2, pi, -pi / 2.0, + - arctan(3.0 / 1.0), pi - arctan(3.0 / 1.0)] + z = angle(x, deg=1) + zo = array(yo) * 180 / pi + assert_array_almost_equal(y, yo, 11) + assert_array_almost_equal(z, zo, 11) class TestTrimZeros(TestCase): """ only testing for integer splits. """ def test_basic(self): - a= array([0,0,1,2,3,4,0]) + a = array([0, 0, 1, 2, 3, 4, 0]) res = trim_zeros(a) - assert_array_equal(res,array([1,2,3,4])) + assert_array_equal(res, array([1, 2, 3, 4])) def test_leading_skip(self): - a= array([0,0,1,0,2,3,4,0]) + a = array([0, 0, 1, 0, 2, 3, 4, 0]) res = trim_zeros(a) - assert_array_equal(res,array([1,0,2,3,4])) + assert_array_equal(res, array([1, 0, 2, 3, 4])) def test_trailing_skip(self): - a= array([0,0,1,0,2,3,0,4,0]) + a = array([0, 0, 1, 0, 2, 3, 0, 4, 0]) res = trim_zeros(a) - assert_array_equal(res,array([1,0,2,3,0,4])) + assert_array_equal(res, array([1, 0, 2, 3, 0, 4])) class TestExtins(TestCase): def test_basic(self): - a = array([1,3,2,1,2,3,3]) - b = extract(a>1,a) - assert_array_equal(b,[3,2,2,3,3]) + a = array([1, 3, 2, 1, 2, 3, 3]) + b = extract(a > 1, a) + assert_array_equal(b, [3, 2, 2, 3, 3]) def test_place(self): - a = array([1,4,3,2,5,8,7]) - place(a,[0,1,0,1,0,1,0],[2,4,6]) - assert_array_equal(a,[1,2,3,4,5,6,7]) + a = array([1, 4, 3, 2, 5, 8, 7]) + place(a, [0, 1, 0, 1, 0, 1, 0], [2, 4, 6]) + assert_array_equal(a, [1, 2, 3, 4, 5, 6, 7]) def test_both(self): a = rand(10) mask = a > 0.5 ac = a.copy() c = extract(mask, a) - place(a,mask,0) - place(a,mask,c) - assert_array_equal(a,ac) + place(a, mask, 0) + place(a, mask, c) + assert_array_equal(a, ac) class TestVectorize(TestCase): def test_simple(self): - def addsubtract(a,b): + def addsubtract(a, b): if a > b: return a - b else: return a + b f = vectorize(addsubtract) - r = f([0,3,6,9],[1,3,5,7]) - assert_array_equal(r,[1,6,1,2]) + r = f([0, 3, 6, 9], [1, 3, 5, 7]) + assert_array_equal(r, [1, 6, 1, 2]) def test_scalar(self): - def addsubtract(a,b): + def addsubtract(a, b): if a > b: return a - b else: return a + b f = vectorize(addsubtract) - r = f([0,3,6,9],5) - assert_array_equal(r,[5,8,1,4]) + r = f([0, 3, 6, 9], 5) + assert_array_equal(r, [5, 8, 1, 4]) def test_large(self): - x = linspace(-3,2,10000) + x = linspace(-3, 2, 10000) f = vectorize(lambda x: x) y = f(x) assert_array_equal(y, x) class TestDigitize(TestCase): def test_forward(self): - x = arange(-6,5) - bins = arange(-5,5) - assert_array_equal(digitize(x,bins),arange(11)) + x = arange(-6, 5) + bins = arange(-5, 5) + assert_array_equal(digitize(x, bins), arange(11)) def test_reverse(self): - x = arange(5,-6,-1) - bins = arange(5,-5,-1) - assert_array_equal(digitize(x,bins),arange(11)) + x = arange(5, -6, -1) + bins = arange(5, -5, -1) + assert_array_equal(digitize(x, bins), arange(11)) def test_random(self): x = rand(10) bin = linspace(x.min(), x.max(), 10) - assert all(digitize(x,bin) != 0) + assert all(digitize(x, bin) != 0) class TestUnwrap(TestCase): def test_simple(self): #check that unwrap removes jumps greather that 2*pi - assert_array_equal(unwrap([1,1+2*pi]),[1,1]) + assert_array_equal(unwrap([1, 1 + 2 * pi]), [1, 1]) #check that unwrap maintans continuity - assert(all(diff(unwrap(rand(10)*100))3, [4, 0]) + y = piecewise(x, x > 3, [4, 0]) assert y.ndim == 0 assert y == 0 @@ -869,9 +878,9 @@ y = np.bincount(x, w) assert_array_equal(y, np.array([0, 0.2, 0.5, 0, 0.5, 0.1])) -def compare_results(res,desired): +def compare_results(res, desired): for i in range(len(desired)): - assert_array_equal(res[i],desired[i]) + assert_array_equal(res[i], desired[i]) if __name__ == "__main__": run_module_suite() From numpy-svn at scipy.org Sun Mar 21 21:46:51 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 21 Mar 2010 20:46:51 -0500 (CDT) Subject: [Numpy-svn] r8300 - trunk/numpy/ma/tests Message-ID: <20100322014651.B5AAF39CB02@scipy.org> Author: pierregm Date: 2010-03-21 20:46:51 -0500 (Sun, 21 Mar 2010) New Revision: 8300 Modified: trunk/numpy/ma/tests/test_core.py Log: * added test_take_masked_indices Modified: trunk/numpy/ma/tests/test_core.py =================================================================== --- trunk/numpy/ma/tests/test_core.py 2010-03-22 01:45:40 UTC (rev 8299) +++ trunk/numpy/ma/tests/test_core.py 2010-03-22 01:46:51 UTC (rev 8300) @@ -2389,7 +2389,37 @@ assert_equal(take(x, [0, 2], axis=1), array([[10, 30], [40, 60]], mask=[[0, 1], [1, 0]])) + def test_take_masked_indices(self): + "Test take w/ masked indices" + a = np.array((40, 18, 37, 9, 22)) + indices = np.arange(3)[None, :] + np.arange(5)[:, None] + mindices = array(indices, mask=(indices >= len(a))) + # No mask + test = take(a, mindices, mode='clip') + ctrl = array([[40, 18, 37], + [18, 37, 9], + [37, 9, 22], + [ 9, 22, 22], + [22, 22, 22]]) + assert_equal(test, ctrl) + # Masked indices + test = take(a, mindices) + ctrl = array([[40, 18, 37], + [18, 37, 9], + [37, 9, 22], + [ 9, 22, 40], + [22, 40, 40]]) + ctrl[3, 2] = ctrl[4, 1] = ctrl[4, 2] = masked + assert_equal(test, ctrl) + assert_equal(test.mask, ctrl.mask) + # Masked input + masked indices + a = array((40, 18, 37, 9, 22), mask=(0, 1, 0, 0, 0)) + test = take(a, mindices) + ctrl[0, 1] = ctrl[1, 0] = masked + assert_equal(test, ctrl) + assert_equal(test.mask, ctrl.mask) + def test_tolist(self): "Tests to list" # ... on 1D From numpy-svn at scipy.org Wed Mar 24 14:18:38 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Wed, 24 Mar 2010 13:18:38 -0500 (CDT) Subject: [Numpy-svn] r8301 - trunk/numpy/core/src/private Message-ID: <20100324181838.BBA6639CAE7@scipy.org> Author: charris Date: 2010-03-24 13:18:38 -0500 (Wed, 24 Mar 2010) New Revision: 8301 Modified: trunk/numpy/core/src/private/npy_3kcompat.h Log: Fix wrong function name. These functions aren't used a present so this just fixes a warning. Modified: trunk/numpy/core/src/private/npy_3kcompat.h =================================================================== --- trunk/numpy/core/src/private/npy_3kcompat.h 2010-03-22 01:46:51 UTC (rev 8300) +++ trunk/numpy/core/src/private/npy_3kcompat.h 2010-03-24 18:18:38 UTC (rev 8301) @@ -234,7 +234,7 @@ static NPY_INLINE PyObject * NpyCapsule_FromVoidPtrAndDesc(void *ptr, void* context, void (*dtor)(PyObject *)) { - PyObject *ret = NpyCapsule_New(ptr, dtor); + PyObject *ret = NpyCapsule_FromVoidPtr(ptr, dtor); if (ret != NULL && PyCapsule_SetContext(ret, context) != 0) { PyErr_Clear(); Py_DECREF(ret); From numpy-svn at scipy.org Wed Mar 24 14:18:44 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Wed, 24 Mar 2010 13:18:44 -0500 (CDT) Subject: [Numpy-svn] r8302 - in trunk: numpy/lib tools Message-ID: <20100324181844.2015D39CAE7@scipy.org> Author: charris Date: 2010-03-24 13:18:43 -0500 (Wed, 24 Mar 2010) New Revision: 8302 Added: trunk/numpy/lib/npyio.py Removed: trunk/numpy/lib/io.py Modified: trunk/numpy/lib/__init__.py trunk/tools/py3tool.py Log: Rename numpy/lib/io.py to numpy/lib/npyio.py. The py3tool can probably be cleaned up a bit more with this change, but that is for later. Modified: trunk/numpy/lib/__init__.py =================================================================== --- trunk/numpy/lib/__init__.py 2010-03-24 18:18:38 UTC (rev 8301) +++ trunk/numpy/lib/__init__.py 2010-03-24 18:18:43 UTC (rev 8302) @@ -14,7 +14,7 @@ #import convertcode from utils import * from arraysetops import * -from io import * +from npyio import * from financial import * import math from arrayterator import * @@ -30,7 +30,7 @@ __all__ += polynomial.__all__ __all__ += utils.__all__ __all__ += arraysetops.__all__ -__all__ += io.__all__ +__all__ += npyio.__all__ __all__ += financial.__all__ from numpy.testing import Tester Deleted: trunk/numpy/lib/io.py =================================================================== --- trunk/numpy/lib/io.py 2010-03-24 18:18:38 UTC (rev 8301) +++ trunk/numpy/lib/io.py 2010-03-24 18:18:43 UTC (rev 8302) @@ -1,1603 +0,0 @@ -__all__ = ['savetxt', 'loadtxt', - 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', - 'load', 'loads', - 'save', 'savez', - 'packbits', 'unpackbits', - 'fromregex', - 'DataSource'] - -import numpy as np -import format -import sys -import os -import sys -import itertools -import warnings -from operator import itemgetter - -from cPickle import load as _cload, loads -from _datasource import DataSource -from _compiled_base import packbits, unpackbits - -from _iotools import LineSplitter, NameValidator, StringConverter, \ - ConverterError, ConverterLockError, ConversionWarning, \ - _is_string_like, has_nested_fields, flatten_dtype, \ - easy_dtype, _bytes_to_name - -from numpy.compat import asbytes, asstr, asbytes_nested, bytes - -if sys.version_info[0] >= 3: - import io - BytesIO = io.BytesIO -else: - from cStringIO import StringIO as BytesIO - -_file = open -_string_like = _is_string_like - -def seek_gzip_factory(f): - """Use this factory to produce the class so that we can do a lazy - import on gzip. - - """ - import gzip - - def seek(self, offset, whence=0): - # figure out new position (we can only seek forwards) - if whence == 1: - offset = self.offset + offset - - if whence not in [0, 1]: - raise IOError, "Illegal argument" - - if offset < self.offset: - # for negative seek, rewind and do positive seek - self.rewind() - count = offset - self.offset - for i in range(count // 1024): - self.read(1024) - self.read(count % 1024) - - def tell(self): - return self.offset - - if isinstance(f, str): - f = gzip.GzipFile(f) - - if sys.version_info[0] >= 3: - import types - f.seek = types.MethodType(seek, f) - f.tell = types.MethodType(tell, f) - else: - import new - f.seek = new.instancemethod(seek, f) - f.tell = new.instancemethod(tell, f) - - return f - -class BagObj(object): - """ - BagObj(obj) - - Convert attribute lookups to getitems on the object passed in. - - Parameters - ---------- - obj : class instance - Object on which attribute lookup is performed. - - Examples - -------- - >>> class BagDemo(object): - ... def __getitem__(self, key): - ... return key - ... - >>> demo_obj = BagDemo() - >>> bagobj = np.lib.io.BagObj(demo_obj) - >>> bagobj.some_item - 'some_item' - - """ - def __init__(self, obj): - self._obj = obj - def __getattribute__(self, key): - try: - return object.__getattribute__(self, '_obj')[key] - except KeyError: - raise AttributeError, key - -class NpzFile(object): - """ - NpzFile(fid) - - A dictionary-like object with lazy-loading of files in the zipped - archive provided on construction. - - `NpzFile` is used to load files in the NumPy ``.npz`` data archive - format. It assumes that files in the archive have a ".npy" extension, - other files are ignored. - - The arrays and file strings are lazily loaded on either - getitem access using ``obj['key']`` or attribute lookup using - ``obj.f.key``. A list of all files (without ".npy" extensions) can - be obtained with ``obj.files`` and the ZipFile object itself using - ``obj.zip``. - - Attributes - ---------- - files : list of str - List of all files in the archive with a ".npy" extension. - zip : ZipFile instance - The ZipFile object initialized with the zipped archive. - f : BagObj instance - An object on which attribute can be performed as an alternative - to getitem access on the `NpzFile` instance itself. - - Parameters - ---------- - fid : file or str - The zipped archive to open. This is either a file-like object - or a string containing the path to the archive. - - Examples - -------- - >>> from tempfile import TemporaryFile - >>> outfile = TemporaryFile() - >>> x = np.arange(10) - >>> y = np.sin(x) - >>> np.savez(outfile, x=x, y=y) - >>> outfile.seek(0) - - >>> npz = np.load(outfile) - >>> isinstance(npz, np.lib.io.NpzFile) - True - >>> npz.files - ['y', 'x'] - >>> npz['x'] # getitem access - array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) - >>> npz.f.x # attribute lookup - array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) - - """ - def __init__(self, fid): - # Import is postponed to here since zipfile depends on gzip, an optional - # component of the so-called standard library. - import zipfile - _zip = zipfile.ZipFile(fid) - self._files = _zip.namelist() - self.files = [] - for x in self._files: - if x.endswith('.npy'): - self.files.append(x[:-4]) - else: - self.files.append(x) - self.zip = _zip - self.f = BagObj(self) - - def __getitem__(self, key): - # FIXME: This seems like it will copy strings around - # more than is strictly necessary. The zipfile - # will read the string and then - # the format.read_array will copy the string - # to another place in memory. - # It would be better if the zipfile could read - # (or at least uncompress) the data - # directly into the array memory. - member = 0 - if key in self._files: - member = 1 - elif key in self.files: - member = 1 - key += '.npy' - if member: - bytes = self.zip.read(key) - if bytes.startswith(format.MAGIC_PREFIX): - value = BytesIO(bytes) - return format.read_array(value) - else: - return bytes - else: - raise KeyError, "%s is not a file in the archive" % key - - - def __iter__(self): - return iter(self.files) - - def items(self): - """ - Return a list of tuples, with each tuple (filename, array in file). - - """ - return [(f, self[f]) for f in self.files] - - def iteritems(self): - """Generator that returns tuples (filename, array in file).""" - for f in self.files: - yield (f, self[f]) - - def keys(self): - """Return files in the archive with a ".npy" extension.""" - return self.files - - def iterkeys(self): - """Return an iterator over the files in the archive.""" - return self.__iter__() - - def __contains__(self, key): - return self.files.__contains__(key) - - -def load(file, mmap_mode=None): - """ - Load a pickled, ``.npy``, or ``.npz`` binary file. - - Parameters - ---------- - file : file-like object or string - The file to read. It must support ``seek()`` and ``read()`` methods. - If the filename extension is ``.gz``, the file is first decompressed. - mmap_mode: {None, 'r+', 'r', 'w+', 'c'}, optional - If not None, then memory-map the file, using the given mode - (see `numpy.memmap`). The mode has no effect for pickled or - zipped files. - A memory-mapped array is stored on disk, and not directly loaded - into memory. However, it can be accessed and sliced like any - ndarray. Memory mapping is especially useful for accessing - small fragments of large files without reading the entire file - into memory. - - Returns - ------- - result : array, tuple, dict, etc. - Data stored in the file. - - Raises - ------ - IOError - If the input file does not exist or cannot be read. - - See Also - -------- - save, savez, loadtxt - memmap : Create a memory-map to an array stored in a file on disk. - - Notes - ----- - - If the file contains pickle data, then whatever is stored in the - pickle is returned. - - If the file is a ``.npy`` file, then an array is returned. - - If the file is a ``.npz`` file, then a dictionary-like object is - returned, containing ``{filename: array}`` key-value pairs, one for - each file in the archive. - - Examples - -------- - Store data to disk, and load it again: - - >>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) - >>> np.load('/tmp/123.npy') - array([[1, 2, 3], - [4, 5, 6]]) - - Mem-map the stored array, and then access the second row - directly from disk: - - >>> X = np.load('/tmp/123.npy', mmap_mode='r') - >>> X[1, :] - memmap([4, 5, 6]) - - """ - import gzip - - if isinstance(file, basestring): - fid = _file(file, "rb") - elif isinstance(file, gzip.GzipFile): - fid = seek_gzip_factory(file) - else: - fid = file - - # Code to distinguish from NumPy binary files and pickles. - _ZIP_PREFIX = asbytes('PK\x03\x04') - N = len(format.MAGIC_PREFIX) - magic = fid.read(N) - fid.seek(-N, 1) # back-up - if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) - return NpzFile(fid) - elif magic == format.MAGIC_PREFIX: # .npy file - if mmap_mode: - return format.open_memmap(file, mode=mmap_mode) - else: - return format.read_array(fid) - else: # Try a pickle - try: - return _cload(fid) - except: - raise IOError, \ - "Failed to interpret file %s as a pickle" % repr(file) - -def save(file, arr): - """ - Save an array to a binary file in NumPy ``.npy`` format. - - Parameters - ---------- - file : file or str - File or filename to which the data is saved. If file is a file-object, - then the filename is unchanged. If file is a string, a ``.npy`` - extension will be appended to the file name if it does not already - have one. - arr : array_like - Array data to be saved. - - See Also - -------- - savez : Save several arrays into a ``.npz`` compressed archive - savetxt, load - - Notes - ----- - For a description of the ``.npy`` format, see `format`. - - Examples - -------- - >>> from tempfile import TemporaryFile - >>> outfile = TemporaryFile() - - >>> x = np.arange(10) - >>> np.save(outfile, x) - - >>> outfile.seek(0) # Only needed here to simulate closing & reopening file - >>> np.load(outfile) - array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) - - """ - if isinstance(file, basestring): - if not file.endswith('.npy'): - file = file + '.npy' - fid = open(file, "wb") - else: - fid = file - - arr = np.asanyarray(arr) - format.write_array(fid, arr) - -def savez(file, *args, **kwds): - """ - Save several arrays into a single, compressed file in ``.npz`` format. - - If arguments are passed in with no keywords, the corresponding variable - names, in the .npz file, are 'arr_0', 'arr_1', etc. If keyword arguments - are given, the corresponding variable names, in the ``.npz`` file will - match the keyword names. - - Parameters - ---------- - file : str or file - Either the file name (string) or an open file (file-like object) - where the data will be saved. If file is a string, the ``.npz`` - extension will be appended to the file name if it is not already there. - \\*args : Arguments, optional - Arrays to save to the file. Since it is not possible for Python to - know the names of the arrays outside `savez`, the arrays will be saved - with names "arr_0", "arr_1", and so on. These arguments can be any - expression. - \\*\\*kwds : Keyword arguments, optional - Arrays to save to the file. Arrays will be saved in the file with the - keyword names. - - Returns - ------- - None - - See Also - -------- - save : Save a single array to a binary file in NumPy format. - savetxt : Save an array to a file as plain text. - - Notes - ----- - The ``.npz`` file format is a zipped archive of files named after the - variables they contain. Each file contains one variable in ``.npy`` - format. For a description of the ``.npy`` format, see `format`. - - When opening the saved ``.npz`` file with `load` a `NpzFile` object is - returned. This is a dictionary-like object which can be queried for - its list of arrays (with the ``.files`` attribute), and for the arrays - themselves. - - Examples - -------- - >>> from tempfile import TemporaryFile - >>> outfile = TemporaryFile() - >>> x = np.arange(10) - >>> y = np.sin(x) - - Using `savez` with \\*args, the arrays are saved with default names. - - >>> np.savez(outfile, x, y) - >>> outfile.seek(0) # Only needed here to simulate closing & reopening file - >>> npzfile = np.load(outfile) - >>> npzfile.files - ['arr_1', 'arr_0'] - >>> npzfile['arr_0'] - array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) - - Using `savez` with \\*\\*kwds, the arrays are saved with the keyword names. - - >>> outfile = TemporaryFile() - >>> np.savez(outfile, x=x, y=y) - >>> outfile.seek(0) - >>> npzfile = np.load(outfile) - >>> npzfile.files - ['y', 'x'] - >>> npzfile['x'] - array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) - - """ - - # Import is postponed to here since zipfile depends on gzip, an optional - # component of the so-called standard library. - import zipfile - # Import deferred for startup time improvement - import tempfile - - if isinstance(file, basestring): - if not file.endswith('.npz'): - file = file + '.npz' - - namedict = kwds - for i, val in enumerate(args): - key = 'arr_%d' % i - if key in namedict.keys(): - raise ValueError, "Cannot use un-named variables and keyword %s" % key - namedict[key] = val - - zip = zipfile.ZipFile(file, mode="w") - - # Stage arrays in a temporary file on disk, before writing to zip. - fd, tmpfile = tempfile.mkstemp(suffix='-numpy.npy') - os.close(fd) - try: - for key, val in namedict.iteritems(): - fname = key + '.npy' - fid = open(tmpfile, 'wb') - try: - format.write_array(fid, np.asanyarray(val)) - fid.close() - fid = None - zip.write(tmpfile, arcname=fname) - finally: - if fid: - fid.close() - finally: - os.remove(tmpfile) - - zip.close() - -# Adapted from matplotlib - -def _getconv(dtype): - typ = dtype.type - if issubclass(typ, np.bool_): - return lambda x: bool(int(x)) - if issubclass(typ, np.integer): - return lambda x: int(float(x)) - elif issubclass(typ, np.floating): - return float - elif issubclass(typ, np.complex): - return complex - elif issubclass(typ, np.bytes_): - return bytes - else: - return str - - - -def loadtxt(fname, dtype=float, comments='#', delimiter=None, - converters=None, skiprows=0, usecols=None, unpack=False): - """ - Load data from a text file. - - Each row in the text file must have the same number of values. - - Parameters - ---------- - fname : file or str - File or filename to read. If the filename extension is ``.gz`` or - ``.bz2``, the file is first decompressed. - dtype : dtype, optional - Data type of the resulting array. If this is a record data-type, - the resulting array will be 1-dimensional, and each row will be - interpreted as an element of the array. In this case, the number - of columns used must match the number of fields in the data-type. - comments : str, optional - The character used to indicate the start of a comment. - delimiter : str, optional - The string used to separate values. By default, this is any - whitespace. - converters : dict, optional - A dictionary mapping column number to a function that will convert - that column to a float. E.g., if column 0 is a date string: - ``converters = {0: datestr2num}``. Converters can also be used to - provide a default value for missing data: - ``converters = {3: lambda s: float(s or 0)}``. - skiprows : int, optional - Skip the first `skiprows` lines. - usecols : sequence, optional - Which columns to read, with 0 being the first. For example, - ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns. - unpack : bool, optional - If True, the returned array is transposed, so that arguments may be - unpacked using ``x, y, z = loadtxt(...)``. Default is False. - - Returns - ------- - out : ndarray - Data read from the text file. - - See Also - -------- - load, fromstring, fromregex - genfromtxt : Load data with missing values handled as specified. - scipy.io.loadmat : reads Matlab(R) data files - - Notes - ----- - This function aims to be a fast reader for simply formatted files. The - `genfromtxt` function provides more sophisticated handling of, e.g., - lines with missing values. - - Examples - -------- - >>> from StringIO import StringIO # StringIO behaves like a file object - >>> c = StringIO("0 1\\n2 3") - >>> np.loadtxt(c) - array([[ 0., 1.], - [ 2., 3.]]) - - >>> d = StringIO("M 21 72\\nF 35 58") - >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), - ... 'formats': ('S1', 'i4', 'f4')}) - array([('M', 21, 72.0), ('F', 35, 58.0)], - dtype=[('gender', '|S1'), ('age', '>> c = StringIO("1,0,2\\n3,0,4") - >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) - >>> x - array([ 1., 3.]) - >>> y - array([ 2., 4.]) - - """ - # Type conversions for Py3 convenience - comments = asbytes(comments) - if delimiter is not None: - delimiter = asbytes(delimiter) - - user_converters = converters - - if usecols is not None: - usecols = list(usecols) - - isstring = False - if _is_string_like(fname): - isstring = True - if fname.endswith('.gz'): - import gzip - fh = seek_gzip_factory(fname) - elif fname.endswith('.bz2'): - import bz2 - fh = bz2.BZ2File(fname) - else: - fh = file(fname) - elif hasattr(fname, 'readline'): - fh = fname - else: - raise ValueError('fname must be a string or file handle') - X = [] - - def flatten_dtype(dt): - """Unpack a structured data-type.""" - if dt.names is None: - # If the dtype is flattened, return. - # If the dtype has a shape, the dtype occurs - # in the list more than once. - return [dt.base] * int(np.prod(dt.shape)) - else: - types = [] - for field in dt.names: - tp, bytes = dt.fields[field] - flat_dt = flatten_dtype(tp) - types.extend(flat_dt) - return types - - def split_line(line): - """Chop off comments, strip, and split at delimiter.""" - line = line.split(comments)[0].strip() - if line: - return line.split(delimiter) - else: - return [] - - try: - # Make sure we're dealing with a proper dtype - dtype = np.dtype(dtype) - defconv = _getconv(dtype) - - # Skip the first `skiprows` lines - for i in xrange(skiprows): - fh.readline() - - # Read until we find a line with some values, and use - # it to estimate the number of columns, N. - first_vals = None - while not first_vals: - first_line = fh.readline() - if not first_line: # EOF reached - raise IOError('End-of-file reached before encountering data.') - first_vals = split_line(first_line) - N = len(usecols or first_vals) - - dtype_types = flatten_dtype(dtype) - if len(dtype_types) > 1: - # We're dealing with a structured array, each field of - # the dtype matches a column - converters = [_getconv(dt) for dt in dtype_types] - else: - # All fields have the same dtype - converters = [defconv for i in xrange(N)] - - # By preference, use the converters specified by the user - for i, conv in (user_converters or {}).iteritems(): - if usecols: - try: - i = usecols.index(i) - except ValueError: - # Unused converter specified - continue - converters[i] = conv - - # Parse each line, including the first - for i, line in enumerate(itertools.chain([first_line], fh)): - vals = split_line(line) - if len(vals) == 0: - continue - - if usecols: - vals = [vals[i] for i in usecols] - - # Convert each value according to its column and store - X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)])) - finally: - if isstring: - fh.close() - - if len(dtype_types) > 1: - # We're dealing with a structured array, with a dtype such as - # [('x', int), ('y', [('s', int), ('t', float)])] - # - # First, create the array using a flattened dtype: - # [('x', int), ('s', int), ('t', float)] - # - # Then, view the array using the specified dtype. - try: - X = np.array(X, dtype=np.dtype([('', t) for t in dtype_types])) - X = X.view(dtype) - except TypeError: - # In the case we have an object dtype - X = np.array(X, dtype=dtype) - else: - X = np.array(X, dtype) - - X = np.squeeze(X) - if unpack: - return X.T - else: - return X - - -def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'): - """ - Save an array to a text file. - - Parameters - ---------- - fname : filename or file handle - If the filename ends in ``.gz``, the file is automatically saved in - compressed gzip format. `loadtxt` understands gzipped files - transparently. - X : array_like - Data to be saved to a text file. - fmt : str or sequence of strs - A single format (%10.5f), a sequence of formats, or a - multi-format string, e.g. 'Iteration %d -- %10.5f', in which - case `delimiter` is ignored. - delimiter : str - Character separating columns. - newline : str - .. versionadded:: 2.0 - - Character separating lines. - - - See Also - -------- - save : Save an array to a binary file in NumPy ``.npy`` format - savez : Save several arrays into a ``.npz`` compressed archive - - Notes - ----- - Further explanation of the `fmt` parameter - (``%[flag]width[.precision]specifier``): - - flags: - ``-`` : left justify - - ``+`` : Forces to preceed result with + or -. - - ``0`` : Left pad the number with zeros instead of space (see width). - - width: - Minimum number of characters to be printed. The value is not truncated - if it has more characters. - - precision: - - For integer specifiers (eg. ``d,i,o,x``), the minimum number of - digits. - - For ``e, E`` and ``f`` specifiers, the number of digits to print - after the decimal point. - - For ``g`` and ``G``, the maximum number of significant digits. - - For ``s``, the maximum number of characters. - - specifiers: - ``c`` : character - - ``d`` or ``i`` : signed decimal integer - - ``e`` or ``E`` : scientific notation with ``e`` or ``E``. - - ``f`` : decimal floating point - - ``g,G`` : use the shorter of ``e,E`` or ``f`` - - ``o`` : signed octal - - ``s`` : string of characters - - ``u`` : unsigned decimal integer - - ``x,X`` : unsigned hexadecimal integer - - This explanation of ``fmt`` is not complete, for an exhaustive - specification see [1]_. - - References - ---------- - .. [1] `Format Specification Mini-Language - `_, Python Documentation. - - Examples - -------- - >>> x = y = z = np.arange(0.0,5.0,1.0) - >>> np.savetxt('test.out', x, delimiter=',') # X is an array - >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays - >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation - - """ - - # Py3 conversions first - if isinstance(fmt, bytes): - fmt = asstr(fmt) - delimiter = asstr(delimiter) - - if _is_string_like(fname): - if fname.endswith('.gz'): - import gzip - fh = gzip.open(fname, 'wb') - else: - if sys.version_info[0] >= 3: - fh = file(fname, 'wb') - else: - fh = file(fname, 'w') - elif hasattr(fname, 'seek'): - fh = fname - else: - raise ValueError('fname must be a string or file handle') - - X = np.asarray(X) - - # Handle 1-dimensional arrays - if X.ndim == 1: - # Common case -- 1d array of numbers - if X.dtype.names is None: - X = np.atleast_2d(X).T - ncol = 1 - - # Complex dtype -- each field indicates a separate column - else: - ncol = len(X.dtype.descr) - else: - ncol = X.shape[1] - - # `fmt` can be a string with multiple insertion points or a list of formats. - # E.g. '%10.5f\t%10d' or ('%10.5f', '$10d') - if type(fmt) in (list, tuple): - if len(fmt) != ncol: - raise AttributeError('fmt has wrong shape. %s' % str(fmt)) - format = asstr(delimiter).join(map(asstr, fmt)) - elif type(fmt) is str: - if fmt.count('%') == 1: - fmt = [fmt, ]*ncol - format = delimiter.join(fmt) - elif fmt.count('%') != ncol: - raise AttributeError('fmt has wrong number of %% formats. %s' - % fmt) - else: - format = fmt - - for row in X: - fh.write(asbytes(format % tuple(row) + newline)) - -import re -def fromregex(file, regexp, dtype): - """ - Construct an array from a text file, using regular expression parsing. - - The returned array is always a structured array, and is constructed from - all matches of the regular expression in the file. Groups in the regular - expression are converted to fields of the structured array. - - Parameters - ---------- - file : str or file - File name or file object to read. - regexp : str or regexp - Regular expression used to parse the file. - Groups in the regular expression correspond to fields in the dtype. - dtype : dtype or list of dtypes - Dtype for the structured array. - - Returns - ------- - output : ndarray - The output array, containing the part of the content of `file` that - was matched by `regexp`. `output` is always a structured array. - - Raises - ------ - TypeError - When `dtype` is not a valid dtype for a structured array. - - See Also - -------- - fromstring, loadtxt - - Notes - ----- - Dtypes for structured arrays can be specified in several forms, but all - forms specify at least the data type and field name. For details see - `doc.structured_arrays`. - - Examples - -------- - >>> f = open('test.dat', 'w') - >>> f.write("1312 foo\\n1534 bar\\n444 qux") - >>> f.close() - - >>> regexp = r"(\\d+)\\s+(...)" # match [digits, whitespace, anything] - >>> output = np.fromregex('test.dat', regexp, - ... [('num', np.int64), ('key', 'S3')]) - >>> output - array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')], - dtype=[('num', '>> output['num'] - array([1312, 1534, 444], dtype=int64) - - """ - if not hasattr(file, "read"): - file = open(file, 'rb') - if not hasattr(regexp, 'match'): - regexp = re.compile(asbytes(regexp)) - if not isinstance(dtype, np.dtype): - dtype = np.dtype(dtype) - - seq = regexp.findall(file.read()) - if seq and not isinstance(seq[0], tuple): - # Only one group is in the regexp. - # Create the new array as a single data-type and then - # re-interpret as a single-field structured array. - newdtype = np.dtype(dtype[dtype.names[0]]) - output = np.array(seq, dtype=newdtype) - output.dtype = dtype - else: - output = np.array(seq, dtype=dtype) - - return output - - - - -#####-------------------------------------------------------------------------- -#---- --- ASCII functions --- -#####-------------------------------------------------------------------------- - - - -def genfromtxt(fname, dtype=float, comments='#', delimiter=None, - skiprows=0, skip_header=0, skip_footer=0, converters=None, - missing='', missing_values=None, filling_values=None, - usecols=None, names=None, excludelist=None, deletechars=None, - autostrip=False, case_sensitive=True, defaultfmt="f%i", - unpack=None, usemask=False, loose=True, invalid_raise=True): - """ - Load data from a text file, with missing values handled as specified. - - Each line past the first `skiprows` lines is split at the `delimiter` - character, and characters following the `comments` character are discarded. - - Parameters - ---------- - fname : file or str - File or filename to read. If the filename extension is `.gz` or - `.bz2`, the file is first decompressed. - dtype : dtype, optional - Data type of the resulting array. - If None, the dtypes will be determined by the contents of each - column, individually. - comments : str, optional - The character used to indicate the start of a comment. - All the characters occurring on a line after a comment are discarded - delimiter : str, int, or sequence, optional - The string used to separate values. By default, any consecutive - whitespaces act as delimiter. An integer or sequence of integers - can also be provided as width(s) of each field. - skip_header : int, optional - The numbers of lines to skip at the beginning of the file. - skip_footer : int, optional - The numbers of lines to skip at the end of the file - converters : variable or None, optional - The set of functions that convert the data of a column to a value. - The converters can also be used to provide a default value - for missing data: ``converters = {3: lambda s: float(s or 0)}``. - missing_values : variable or None, optional - The set of strings corresponding to missing data. - filling_values : variable or None, optional - The set of values to be used as default when the data are missing. - usecols : sequence or None, optional - Which columns to read, with 0 being the first. For example, - ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns. - names : {None, True, str, sequence}, optional - If `names` is True, the field names are read from the first valid line - after the first `skiprows` lines. - If `names` is a sequence or a single-string of comma-separated names, - the names will be used to define the field names in a structured dtype. - If `names` is None, the names of the dtype fields will be used, if any. - excludelist : sequence, optional - A list of names to exclude. This list is appended to the default list - ['return','file','print']. Excluded names are appended an underscore: - for example, `file` would become `file_`. - deletechars : str, optional - A string combining invalid characters that must be deleted from the - names. - defaultfmt : str, optional - A format used to define default field names, such as "f%i" or "f_%02i". - autostrip : bool, optional - Whether to automatically strip white spaces from the variables. - case_sensitive : {True, False, 'upper', 'lower'}, optional - If True, field names are case sensitive. - If False or 'upper', field names are converted to upper case. - If 'lower', field names are converted to lower case. - unpack : bool, optional - If True, the returned array is transposed, so that arguments may be - unpacked using ``x, y, z = loadtxt(...)`` - usemask : bool, optional - If True, return a masked array. - If False, return a regular array. - invalid_raise : bool, optional - If True, an exception is raised if an inconsistency is detected in the - number of columns. - If False, a warning is emitted and the offending lines are skipped. - - Returns - ------- - out : ndarray - Data read from the text file. If `usemask` is True, this is a - masked array. - - See Also - -------- - numpy.loadtxt : equivalent function when no data is missing. - - Notes - ----- - * When spaces are used as delimiters, or when no delimiter has been given - as input, there should not be any missing data between two fields. - * When the variables are named (either by a flexible dtype or with `names`, - there must not be any header in the file (else a ValueError - exception is raised). - * Individual values are not stripped of spaces by default. - When using a custom converter, make sure the function does remove spaces. - - Examples - --------- - >>> from StringIO import StringIO - >>> import numpy as np - - Comma delimited file with mixed dtype - - >>> s = StringIO("1,1.3,abcde") - >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), - ... ('mystring','S5')], delimiter=",") - >>> data - array((1, 1.3, 'abcde'), - dtype=[('myint', '>> s.seek(0) # needed for StringIO example only - >>> data = np.genfromtxt(s, dtype=None, - ... names = ['myint','myfloat','mystring'], delimiter=",") - >>> data - array((1, 1.3, 'abcde'), - dtype=[('myint', '>> s.seek(0) - >>> data = np.genfromtxt(s, dtype="i8,f8,S5", - ... names=['myint','myfloat','mystring'], delimiter=",") - >>> data - array((1, 1.3, 'abcde'), - dtype=[('myint', '>> s = StringIO("11.3abcde") - >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], - ... delimiter=[1,3,5]) - >>> data - array((1, 1.3, 'abcde'), - dtype=[('intvar', ' nbcols): - descr = dtype.descr - dtype = np.dtype([descr[_] for _ in usecols]) - names = list(dtype.names) - # If `names` is not None, update the names - elif (names is not None) and (len(names) > nbcols): - names = [names[_] for _ in usecols] - - - # Process the missing values ............................... - # Rename missing_values for convenience - user_missing_values = missing_values or () - - # Define the list of missing_values (one column: one list) - missing_values = [list([asbytes('')]) for _ in range(nbcols)] - - # We have a dictionary: process it field by field - if isinstance(user_missing_values, dict): - # Loop on the items - for (key, val) in user_missing_values.items(): - # Is the key a string ? - if _is_string_like(key): - try: - # Transform it into an integer - key = names.index(key) - except ValueError: - # We couldn't find it: the name must have been dropped, then - continue - # Redefine the key as needed if it's a column number - if usecols: - try: - key = usecols.index(key) - except ValueError: - pass - # Transform the value as a list of string - if isinstance(val, (list, tuple)): - val = [str(_) for _ in val] - else: - val = [str(val), ] - # Add the value(s) to the current list of missing - if key is None: - # None acts as default - for miss in missing_values: - miss.extend(val) - else: - missing_values[key].extend(val) - # We have a sequence : each item matches a column - elif isinstance(user_missing_values, (list, tuple)): - for (value, entry) in zip(user_missing_values, missing_values): - value = str(value) - if value not in entry: - entry.append(value) - # We have a string : apply it to all entries - elif isinstance(user_missing_values, bytes): - user_value = user_missing_values.split(asbytes(",")) - for entry in missing_values: - entry.extend(user_value) - # We have something else: apply it to all entries - else: - for entry in missing_values: - entry.extend([str(user_missing_values)]) - - # Process the deprecated `missing` - if missing != asbytes(''): - warnings.warn("The use of `missing` is deprecated.\n"\ - "Please use `missing_values` instead.", - DeprecationWarning) - values = [str(_) for _ in missing.split(asbytes(","))] - for entry in missing_values: - entry.extend(values) - - # Process the filling_values ............................... - # Rename the input for convenience - user_filling_values = filling_values or [] - # Define the default - filling_values = [None] * nbcols - # We have a dictionary : update each entry individually - if isinstance(user_filling_values, dict): - for (key, val) in user_filling_values.items(): - if _is_string_like(key): - try: - # Transform it into an integer - key = names.index(key) - except ValueError: - # We couldn't find it: the name must have been dropped, then - continue - # Redefine the key if it's a column number and usecols is defined - if usecols: - try: - key = usecols.index(key) - except ValueError: - pass - # Add the value to the list - filling_values[key] = val - # We have a sequence : update on a one-to-one basis - elif isinstance(user_filling_values, (list, tuple)): - n = len(user_filling_values) - if (n <= nbcols): - filling_values[:n] = user_filling_values - else: - filling_values = user_filling_values[:nbcols] - # We have something else : use it for all entries - else: - filling_values = [user_filling_values] * nbcols - - # Initialize the converters ................................ - if dtype is None: - # Note: we can't use a [...]*nbcols, as we would have 3 times the same - # ... converter, instead of 3 different converters. - converters = [StringConverter(None, missing_values=miss, default=fill) - for (miss, fill) in zip(missing_values, filling_values)] - else: - dtype_flat = flatten_dtype(dtype, flatten_base=True) - # Initialize the converters - if len(dtype_flat) > 1: - # Flexible type : get a converter from each dtype - zipit = zip(dtype_flat, missing_values, filling_values) - converters = [StringConverter(dt, locked=True, - missing_values=miss, default=fill) - for (dt, miss, fill) in zipit] - else: - # Set to a default converter (but w/ different missing values) - zipit = zip(missing_values, filling_values) - converters = [StringConverter(dtype, locked=True, - missing_values=miss, default=fill) - for (miss, fill) in zipit] - # Update the converters to use the user-defined ones - uc_update = [] - for (i, conv) in user_converters.items(): - # If the converter is specified by column names, use the index instead - if _is_string_like(i): - try: - i = names.index(i) - except ValueError: - continue - elif usecols: - try: - i = usecols.index(i) - except ValueError: - # Unused converter specified - continue - converters[i].update(conv, locked=True, - default=filling_values[i], - missing_values=missing_values[i],) - uc_update.append((i, conv)) - # Make sure we have the corrected keys in user_converters... - user_converters.update(uc_update) - - miss_chars = [_.missing_values for _ in converters] - - - # Initialize the output lists ... - # ... rows - rows = [] - append_to_rows = rows.append - # ... masks - if usemask: - masks = [] - append_to_masks = masks.append - # ... invalid - invalid = [] - append_to_invalid = invalid.append - - # Parse each line - for (i, line) in enumerate(itertools.chain([first_line, ], fhd)): - values = split_line(line) - nbvalues = len(values) - # Skip an empty line - if nbvalues == 0: - continue - # Select only the columns we need - if usecols: - try: - values = [values[_] for _ in usecols] - except IndexError: - append_to_invalid((i, nbvalues)) - continue - elif nbvalues != nbcols: - append_to_invalid((i, nbvalues)) - continue - # Store the values - append_to_rows(tuple(values)) - if usemask: - append_to_masks(tuple([v.strip() in m - for (v, m) in zip(values, missing_values)])) - - # Strip the last skip_footer data - if skip_footer > 0: - rows = rows[:-skip_footer] - if usemask: - masks = masks[:-skip_footer] - - # Upgrade the converters (if needed) - if dtype is None: - for (i, converter) in enumerate(converters): - current_column = map(itemgetter(i), rows) - try: - converter.iterupgrade(current_column) - except ConverterLockError: - errmsg = "Converter #%i is locked and cannot be upgraded: " % i - current_column = itertools.imap(itemgetter(i), rows) - for (j, value) in enumerate(current_column): - try: - converter.upgrade(value) - except (ConverterError, ValueError): - errmsg += "(occurred line #%i for value '%s')" - errmsg %= (j + 1 + skip_header, value) - raise ConverterError(errmsg) - - # Check that we don't have invalid values - if len(invalid) > 0: - nbrows = len(rows) - # Construct the error message - template = " Line #%%i (got %%i columns instead of %i)" % nbcols - if skip_footer > 0: - nbrows -= skip_footer - errmsg = [template % (i + skip_header + 1, nb) - for (i, nb) in invalid if i < nbrows] - else: - errmsg = [template % (i + skip_header + 1, nb) - for (i, nb) in invalid] - if len(errmsg): - errmsg.insert(0, "Some errors were detected !") - errmsg = "\n".join(errmsg) - # Raise an exception ? - if invalid_raise: - raise ValueError(errmsg) - # Issue a warning ? - else: - warnings.warn(errmsg, ConversionWarning) - - # Convert each value according to the converter: - # We want to modify the list in place to avoid creating a new one... -# if loose: -# conversionfuncs = [conv._loose_call for conv in converters] -# else: -# conversionfuncs = [conv._strict_call for conv in converters] -# for (i, vals) in enumerate(rows): -# rows[i] = tuple([convert(val) -# for (convert, val) in zip(conversionfuncs, vals)]) - if loose: - rows = zip(*[map(converter._loose_call, map(itemgetter(i), rows)) - for (i, converter) in enumerate(converters)]) - else: - rows = zip(*[map(converter._strict_call, map(itemgetter(i), rows)) - for (i, converter) in enumerate(converters)]) - # Reset the dtype - data = rows - if dtype is None: - # Get the dtypes from the types of the converters - column_types = [conv.type for conv in converters] - # Find the columns with strings... - strcolidx = [i for (i, v) in enumerate(column_types) - if v in (type('S'), np.string_)] - # ... and take the largest number of chars. - for i in strcolidx: - column_types[i] = "|S%i" % max(len(row[i]) for row in data) - # - if names is None: - # If the dtype is uniform, don't define names, else use '' - base = set([c.type for c in converters if c._checked]) - if len(base) == 1: - (ddtype, mdtype) = (list(base)[0], np.bool) - else: - ddtype = [(defaultfmt % i, dt) - for (i, dt) in enumerate(column_types)] - if usemask: - mdtype = [(defaultfmt % i, np.bool) - for (i, dt) in enumerate(column_types)] - else: - ddtype = zip(names, column_types) - mdtype = zip(names, [np.bool] * len(column_types)) - output = np.array(data, dtype=ddtype) - if usemask: - outputmask = np.array(masks, dtype=mdtype) - else: - # Overwrite the initial dtype names if needed - if names and dtype.names: - dtype.names = names - # Case 1. We have a structured type - if len(dtype_flat) > 1: - # Nested dtype, eg [('a', int), ('b', [('b0', int), ('b1', 'f4')])] - # First, create the array using a flattened dtype: - # [('a', int), ('b1', int), ('b2', float)] - # Then, view the array using the specified dtype. - if 'O' in (_.char for _ in dtype_flat): - if has_nested_fields(dtype): - errmsg = "Nested fields involving objects "\ - "are not supported..." - raise NotImplementedError(errmsg) - else: - output = np.array(data, dtype=dtype) - else: - rows = np.array(data, dtype=[('', _) for _ in dtype_flat]) - output = rows.view(dtype) - # Now, process the rowmasks the same way - if usemask: - rowmasks = np.array(masks, - dtype=np.dtype([('', np.bool) - for t in dtype_flat])) - # Construct the new dtype - mdtype = make_mask_descr(dtype) - outputmask = rowmasks.view(mdtype) - # Case #2. We have a basic dtype - else: - # We used some user-defined converters - if user_converters: - ishomogeneous = True - descr = [] - for (i, ttype) in enumerate([conv.type for conv in converters]): - # Keep the dtype of the current converter - if i in user_converters: - ishomogeneous &= (ttype == dtype.type) - if ttype == np.string_: - ttype = "|S%i" % max(len(row[i]) for row in data) - descr.append(('', ttype)) - else: - descr.append(('', dtype)) - # So we changed the dtype ? - if not ishomogeneous: - # We have more than one field - if len(descr) > 1: - dtype = np.dtype(descr) - # We have only one field: drop the name if not needed. - else: - dtype = np.dtype(ttype) - # - output = np.array(data, dtype) - if usemask: - if dtype.names: - mdtype = [(_, np.bool) for _ in dtype.names] - else: - mdtype = np.bool - outputmask = np.array(masks, dtype=mdtype) - # Try to take care of the missing data we missed - names = output.dtype.names - if usemask and names: - for (name, conv) in zip(names or (), converters): - missing_values = [conv(_) for _ in conv.missing_values - if _ != asbytes('')] - for mval in missing_values: - outputmask[name] |= (output[name] == mval) - # Construct the final array - if usemask: - output = output.view(MaskedArray) - output._mask = outputmask - if unpack: - return output.squeeze().T - return output.squeeze() - - - -def ndfromtxt(fname, **kwargs): - """ - Load ASCII data stored in a file and return it as a single array. - - Complete description of all the optional input parameters is available in - the docstring of the `genfromtxt` function. - - See Also - -------- - numpy.genfromtxt : generic function. - - """ - kwargs['usemask'] = False - return genfromtxt(fname, **kwargs) - - -def mafromtxt(fname, **kwargs): - """ - Load ASCII data stored in a text file and return a masked array. - - For a complete description of all the input parameters, see `genfromtxt`. - - See Also - -------- - numpy.genfromtxt : generic function to load ASCII data. - - """ - kwargs['usemask'] = True - return genfromtxt(fname, **kwargs) - - -def recfromtxt(fname, **kwargs): - """ - Load ASCII data from a file and return it in a record array. - - If ``usemask=False`` a standard `recarray` is returned, - if ``usemask=True`` a MaskedRecords array is returned. - - Complete description of all the optional input parameters is available in - the docstring of the `genfromtxt` function. - - See Also - -------- - numpy.genfromtxt : generic function - - Notes - ----- - By default, `dtype` is None, which means that the data-type of the output - array will be determined from the data. - - """ - kwargs.update(dtype=kwargs.get('dtype', None)) - usemask = kwargs.get('usemask', False) - output = genfromtxt(fname, **kwargs) - if usemask: - from numpy.ma.mrecords import MaskedRecords - output = output.view(MaskedRecords) - else: - output = output.view(np.recarray) - return output - - -def recfromcsv(fname, **kwargs): - """ - Load ASCII data stored in a comma-separated file. - - The returned array is a record array (if ``usemask=False``, see - `recarray`) or a masked record array (if ``usemask=True``, - see `ma.mrecords.MaskedRecords`). - - For a complete description of all the input parameters, see `genfromtxt`. - - See Also - -------- - numpy.genfromtxt : generic function to load ASCII data. - - """ - case_sensitive = kwargs.get('case_sensitive', "lower") or "lower" - names = kwargs.get('names', True) - if names is None: - names = True - kwargs.update(dtype=kwargs.get('update', None), - delimiter=kwargs.get('delimiter', ",") or ",", - names=names, - case_sensitive=case_sensitive) - usemask = kwargs.get("usemask", False) - output = genfromtxt(fname, **kwargs) - if usemask: - from numpy.ma.mrecords import MaskedRecords - output = output.view(MaskedRecords) - else: - output = output.view(np.recarray) - return output Copied: trunk/numpy/lib/npyio.py (from rev 8301, trunk/numpy/lib/io.py) =================================================================== --- trunk/numpy/lib/npyio.py (rev 0) +++ trunk/numpy/lib/npyio.py 2010-03-24 18:18:43 UTC (rev 8302) @@ -0,0 +1,1603 @@ +__all__ = ['savetxt', 'loadtxt', + 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', + 'load', 'loads', + 'save', 'savez', + 'packbits', 'unpackbits', + 'fromregex', + 'DataSource'] + +import numpy as np +import format +import sys +import os +import sys +import itertools +import warnings +from operator import itemgetter + +from cPickle import load as _cload, loads +from _datasource import DataSource +from _compiled_base import packbits, unpackbits + +from _iotools import LineSplitter, NameValidator, StringConverter, \ + ConverterError, ConverterLockError, ConversionWarning, \ + _is_string_like, has_nested_fields, flatten_dtype, \ + easy_dtype, _bytes_to_name + +from numpy.compat import asbytes, asstr, asbytes_nested, bytes + +if sys.version_info[0] >= 3: + import io + BytesIO = io.BytesIO +else: + from cStringIO import StringIO as BytesIO + +_file = open +_string_like = _is_string_like + +def seek_gzip_factory(f): + """Use this factory to produce the class so that we can do a lazy + import on gzip. + + """ + import gzip + + def seek(self, offset, whence=0): + # figure out new position (we can only seek forwards) + if whence == 1: + offset = self.offset + offset + + if whence not in [0, 1]: + raise IOError, "Illegal argument" + + if offset < self.offset: + # for negative seek, rewind and do positive seek + self.rewind() + count = offset - self.offset + for i in range(count // 1024): + self.read(1024) + self.read(count % 1024) + + def tell(self): + return self.offset + + if isinstance(f, str): + f = gzip.GzipFile(f) + + if sys.version_info[0] >= 3: + import types + f.seek = types.MethodType(seek, f) + f.tell = types.MethodType(tell, f) + else: + import new + f.seek = new.instancemethod(seek, f) + f.tell = new.instancemethod(tell, f) + + return f + +class BagObj(object): + """ + BagObj(obj) + + Convert attribute lookups to getitems on the object passed in. + + Parameters + ---------- + obj : class instance + Object on which attribute lookup is performed. + + Examples + -------- + >>> class BagDemo(object): + ... def __getitem__(self, key): + ... return key + ... + >>> demo_obj = BagDemo() + >>> bagobj = np.lib.io.BagObj(demo_obj) + >>> bagobj.some_item + 'some_item' + + """ + def __init__(self, obj): + self._obj = obj + def __getattribute__(self, key): + try: + return object.__getattribute__(self, '_obj')[key] + except KeyError: + raise AttributeError, key + +class NpzFile(object): + """ + NpzFile(fid) + + A dictionary-like object with lazy-loading of files in the zipped + archive provided on construction. + + `NpzFile` is used to load files in the NumPy ``.npz`` data archive + format. It assumes that files in the archive have a ".npy" extension, + other files are ignored. + + The arrays and file strings are lazily loaded on either + getitem access using ``obj['key']`` or attribute lookup using + ``obj.f.key``. A list of all files (without ".npy" extensions) can + be obtained with ``obj.files`` and the ZipFile object itself using + ``obj.zip``. + + Attributes + ---------- + files : list of str + List of all files in the archive with a ".npy" extension. + zip : ZipFile instance + The ZipFile object initialized with the zipped archive. + f : BagObj instance + An object on which attribute can be performed as an alternative + to getitem access on the `NpzFile` instance itself. + + Parameters + ---------- + fid : file or str + The zipped archive to open. This is either a file-like object + or a string containing the path to the archive. + + Examples + -------- + >>> from tempfile import TemporaryFile + >>> outfile = TemporaryFile() + >>> x = np.arange(10) + >>> y = np.sin(x) + >>> np.savez(outfile, x=x, y=y) + >>> outfile.seek(0) + + >>> npz = np.load(outfile) + >>> isinstance(npz, np.lib.io.NpzFile) + True + >>> npz.files + ['y', 'x'] + >>> npz['x'] # getitem access + array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + >>> npz.f.x # attribute lookup + array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + + """ + def __init__(self, fid): + # Import is postponed to here since zipfile depends on gzip, an optional + # component of the so-called standard library. + import zipfile + _zip = zipfile.ZipFile(fid) + self._files = _zip.namelist() + self.files = [] + for x in self._files: + if x.endswith('.npy'): + self.files.append(x[:-4]) + else: + self.files.append(x) + self.zip = _zip + self.f = BagObj(self) + + def __getitem__(self, key): + # FIXME: This seems like it will copy strings around + # more than is strictly necessary. The zipfile + # will read the string and then + # the format.read_array will copy the string + # to another place in memory. + # It would be better if the zipfile could read + # (or at least uncompress) the data + # directly into the array memory. + member = 0 + if key in self._files: + member = 1 + elif key in self.files: + member = 1 + key += '.npy' + if member: + bytes = self.zip.read(key) + if bytes.startswith(format.MAGIC_PREFIX): + value = BytesIO(bytes) + return format.read_array(value) + else: + return bytes + else: + raise KeyError, "%s is not a file in the archive" % key + + + def __iter__(self): + return iter(self.files) + + def items(self): + """ + Return a list of tuples, with each tuple (filename, array in file). + + """ + return [(f, self[f]) for f in self.files] + + def iteritems(self): + """Generator that returns tuples (filename, array in file).""" + for f in self.files: + yield (f, self[f]) + + def keys(self): + """Return files in the archive with a ".npy" extension.""" + return self.files + + def iterkeys(self): + """Return an iterator over the files in the archive.""" + return self.__iter__() + + def __contains__(self, key): + return self.files.__contains__(key) + + +def load(file, mmap_mode=None): + """ + Load a pickled, ``.npy``, or ``.npz`` binary file. + + Parameters + ---------- + file : file-like object or string + The file to read. It must support ``seek()`` and ``read()`` methods. + If the filename extension is ``.gz``, the file is first decompressed. + mmap_mode: {None, 'r+', 'r', 'w+', 'c'}, optional + If not None, then memory-map the file, using the given mode + (see `numpy.memmap`). The mode has no effect for pickled or + zipped files. + A memory-mapped array is stored on disk, and not directly loaded + into memory. However, it can be accessed and sliced like any + ndarray. Memory mapping is especially useful for accessing + small fragments of large files without reading the entire file + into memory. + + Returns + ------- + result : array, tuple, dict, etc. + Data stored in the file. + + Raises + ------ + IOError + If the input file does not exist or cannot be read. + + See Also + -------- + save, savez, loadtxt + memmap : Create a memory-map to an array stored in a file on disk. + + Notes + ----- + - If the file contains pickle data, then whatever is stored in the + pickle is returned. + - If the file is a ``.npy`` file, then an array is returned. + - If the file is a ``.npz`` file, then a dictionary-like object is + returned, containing ``{filename: array}`` key-value pairs, one for + each file in the archive. + + Examples + -------- + Store data to disk, and load it again: + + >>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) + >>> np.load('/tmp/123.npy') + array([[1, 2, 3], + [4, 5, 6]]) + + Mem-map the stored array, and then access the second row + directly from disk: + + >>> X = np.load('/tmp/123.npy', mmap_mode='r') + >>> X[1, :] + memmap([4, 5, 6]) + + """ + import gzip + + if isinstance(file, basestring): + fid = _file(file, "rb") + elif isinstance(file, gzip.GzipFile): + fid = seek_gzip_factory(file) + else: + fid = file + + # Code to distinguish from NumPy binary files and pickles. + _ZIP_PREFIX = asbytes('PK\x03\x04') + N = len(format.MAGIC_PREFIX) + magic = fid.read(N) + fid.seek(-N, 1) # back-up + if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) + return NpzFile(fid) + elif magic == format.MAGIC_PREFIX: # .npy file + if mmap_mode: + return format.open_memmap(file, mode=mmap_mode) + else: + return format.read_array(fid) + else: # Try a pickle + try: + return _cload(fid) + except: + raise IOError, \ + "Failed to interpret file %s as a pickle" % repr(file) + +def save(file, arr): + """ + Save an array to a binary file in NumPy ``.npy`` format. + + Parameters + ---------- + file : file or str + File or filename to which the data is saved. If file is a file-object, + then the filename is unchanged. If file is a string, a ``.npy`` + extension will be appended to the file name if it does not already + have one. + arr : array_like + Array data to be saved. + + See Also + -------- + savez : Save several arrays into a ``.npz`` compressed archive + savetxt, load + + Notes + ----- + For a description of the ``.npy`` format, see `format`. + + Examples + -------- + >>> from tempfile import TemporaryFile + >>> outfile = TemporaryFile() + + >>> x = np.arange(10) + >>> np.save(outfile, x) + + >>> outfile.seek(0) # Only needed here to simulate closing & reopening file + >>> np.load(outfile) + array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + + """ + if isinstance(file, basestring): + if not file.endswith('.npy'): + file = file + '.npy' + fid = open(file, "wb") + else: + fid = file + + arr = np.asanyarray(arr) + format.write_array(fid, arr) + +def savez(file, *args, **kwds): + """ + Save several arrays into a single, compressed file in ``.npz`` format. + + If arguments are passed in with no keywords, the corresponding variable + names, in the .npz file, are 'arr_0', 'arr_1', etc. If keyword arguments + are given, the corresponding variable names, in the ``.npz`` file will + match the keyword names. + + Parameters + ---------- + file : str or file + Either the file name (string) or an open file (file-like object) + where the data will be saved. If file is a string, the ``.npz`` + extension will be appended to the file name if it is not already there. + \\*args : Arguments, optional + Arrays to save to the file. Since it is not possible for Python to + know the names of the arrays outside `savez`, the arrays will be saved + with names "arr_0", "arr_1", and so on. These arguments can be any + expression. + \\*\\*kwds : Keyword arguments, optional + Arrays to save to the file. Arrays will be saved in the file with the + keyword names. + + Returns + ------- + None + + See Also + -------- + save : Save a single array to a binary file in NumPy format. + savetxt : Save an array to a file as plain text. + + Notes + ----- + The ``.npz`` file format is a zipped archive of files named after the + variables they contain. Each file contains one variable in ``.npy`` + format. For a description of the ``.npy`` format, see `format`. + + When opening the saved ``.npz`` file with `load` a `NpzFile` object is + returned. This is a dictionary-like object which can be queried for + its list of arrays (with the ``.files`` attribute), and for the arrays + themselves. + + Examples + -------- + >>> from tempfile import TemporaryFile + >>> outfile = TemporaryFile() + >>> x = np.arange(10) + >>> y = np.sin(x) + + Using `savez` with \\*args, the arrays are saved with default names. + + >>> np.savez(outfile, x, y) + >>> outfile.seek(0) # Only needed here to simulate closing & reopening file + >>> npzfile = np.load(outfile) + >>> npzfile.files + ['arr_1', 'arr_0'] + >>> npzfile['arr_0'] + array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + + Using `savez` with \\*\\*kwds, the arrays are saved with the keyword names. + + >>> outfile = TemporaryFile() + >>> np.savez(outfile, x=x, y=y) + >>> outfile.seek(0) + >>> npzfile = np.load(outfile) + >>> npzfile.files + ['y', 'x'] + >>> npzfile['x'] + array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + + """ + + # Import is postponed to here since zipfile depends on gzip, an optional + # component of the so-called standard library. + import zipfile + # Import deferred for startup time improvement + import tempfile + + if isinstance(file, basestring): + if not file.endswith('.npz'): + file = file + '.npz' + + namedict = kwds + for i, val in enumerate(args): + key = 'arr_%d' % i + if key in namedict.keys(): + raise ValueError, "Cannot use un-named variables and keyword %s" % key + namedict[key] = val + + zip = zipfile.ZipFile(file, mode="w") + + # Stage arrays in a temporary file on disk, before writing to zip. + fd, tmpfile = tempfile.mkstemp(suffix='-numpy.npy') + os.close(fd) + try: + for key, val in namedict.iteritems(): + fname = key + '.npy' + fid = open(tmpfile, 'wb') + try: + format.write_array(fid, np.asanyarray(val)) + fid.close() + fid = None + zip.write(tmpfile, arcname=fname) + finally: + if fid: + fid.close() + finally: + os.remove(tmpfile) + + zip.close() + +# Adapted from matplotlib + +def _getconv(dtype): + typ = dtype.type + if issubclass(typ, np.bool_): + return lambda x: bool(int(x)) + if issubclass(typ, np.integer): + return lambda x: int(float(x)) + elif issubclass(typ, np.floating): + return float + elif issubclass(typ, np.complex): + return complex + elif issubclass(typ, np.bytes_): + return bytes + else: + return str + + + +def loadtxt(fname, dtype=float, comments='#', delimiter=None, + converters=None, skiprows=0, usecols=None, unpack=False): + """ + Load data from a text file. + + Each row in the text file must have the same number of values. + + Parameters + ---------- + fname : file or str + File or filename to read. If the filename extension is ``.gz`` or + ``.bz2``, the file is first decompressed. + dtype : dtype, optional + Data type of the resulting array. If this is a record data-type, + the resulting array will be 1-dimensional, and each row will be + interpreted as an element of the array. In this case, the number + of columns used must match the number of fields in the data-type. + comments : str, optional + The character used to indicate the start of a comment. + delimiter : str, optional + The string used to separate values. By default, this is any + whitespace. + converters : dict, optional + A dictionary mapping column number to a function that will convert + that column to a float. E.g., if column 0 is a date string: + ``converters = {0: datestr2num}``. Converters can also be used to + provide a default value for missing data: + ``converters = {3: lambda s: float(s or 0)}``. + skiprows : int, optional + Skip the first `skiprows` lines. + usecols : sequence, optional + Which columns to read, with 0 being the first. For example, + ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns. + unpack : bool, optional + If True, the returned array is transposed, so that arguments may be + unpacked using ``x, y, z = loadtxt(...)``. Default is False. + + Returns + ------- + out : ndarray + Data read from the text file. + + See Also + -------- + load, fromstring, fromregex + genfromtxt : Load data with missing values handled as specified. + scipy.io.loadmat : reads Matlab(R) data files + + Notes + ----- + This function aims to be a fast reader for simply formatted files. The + `genfromtxt` function provides more sophisticated handling of, e.g., + lines with missing values. + + Examples + -------- + >>> from StringIO import StringIO # StringIO behaves like a file object + >>> c = StringIO("0 1\\n2 3") + >>> np.loadtxt(c) + array([[ 0., 1.], + [ 2., 3.]]) + + >>> d = StringIO("M 21 72\\nF 35 58") + >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), + ... 'formats': ('S1', 'i4', 'f4')}) + array([('M', 21, 72.0), ('F', 35, 58.0)], + dtype=[('gender', '|S1'), ('age', '>> c = StringIO("1,0,2\\n3,0,4") + >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) + >>> x + array([ 1., 3.]) + >>> y + array([ 2., 4.]) + + """ + # Type conversions for Py3 convenience + comments = asbytes(comments) + if delimiter is not None: + delimiter = asbytes(delimiter) + + user_converters = converters + + if usecols is not None: + usecols = list(usecols) + + isstring = False + if _is_string_like(fname): + isstring = True + if fname.endswith('.gz'): + import gzip + fh = seek_gzip_factory(fname) + elif fname.endswith('.bz2'): + import bz2 + fh = bz2.BZ2File(fname) + else: + fh = file(fname) + elif hasattr(fname, 'readline'): + fh = fname + else: + raise ValueError('fname must be a string or file handle') + X = [] + + def flatten_dtype(dt): + """Unpack a structured data-type.""" + if dt.names is None: + # If the dtype is flattened, return. + # If the dtype has a shape, the dtype occurs + # in the list more than once. + return [dt.base] * int(np.prod(dt.shape)) + else: + types = [] + for field in dt.names: + tp, bytes = dt.fields[field] + flat_dt = flatten_dtype(tp) + types.extend(flat_dt) + return types + + def split_line(line): + """Chop off comments, strip, and split at delimiter.""" + line = line.split(comments)[0].strip() + if line: + return line.split(delimiter) + else: + return [] + + try: + # Make sure we're dealing with a proper dtype + dtype = np.dtype(dtype) + defconv = _getconv(dtype) + + # Skip the first `skiprows` lines + for i in xrange(skiprows): + fh.readline() + + # Read until we find a line with some values, and use + # it to estimate the number of columns, N. + first_vals = None + while not first_vals: + first_line = fh.readline() + if not first_line: # EOF reached + raise IOError('End-of-file reached before encountering data.') + first_vals = split_line(first_line) + N = len(usecols or first_vals) + + dtype_types = flatten_dtype(dtype) + if len(dtype_types) > 1: + # We're dealing with a structured array, each field of + # the dtype matches a column + converters = [_getconv(dt) for dt in dtype_types] + else: + # All fields have the same dtype + converters = [defconv for i in xrange(N)] + + # By preference, use the converters specified by the user + for i, conv in (user_converters or {}).iteritems(): + if usecols: + try: + i = usecols.index(i) + except ValueError: + # Unused converter specified + continue + converters[i] = conv + + # Parse each line, including the first + for i, line in enumerate(itertools.chain([first_line], fh)): + vals = split_line(line) + if len(vals) == 0: + continue + + if usecols: + vals = [vals[i] for i in usecols] + + # Convert each value according to its column and store + X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)])) + finally: + if isstring: + fh.close() + + if len(dtype_types) > 1: + # We're dealing with a structured array, with a dtype such as + # [('x', int), ('y', [('s', int), ('t', float)])] + # + # First, create the array using a flattened dtype: + # [('x', int), ('s', int), ('t', float)] + # + # Then, view the array using the specified dtype. + try: + X = np.array(X, dtype=np.dtype([('', t) for t in dtype_types])) + X = X.view(dtype) + except TypeError: + # In the case we have an object dtype + X = np.array(X, dtype=dtype) + else: + X = np.array(X, dtype) + + X = np.squeeze(X) + if unpack: + return X.T + else: + return X + + +def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'): + """ + Save an array to a text file. + + Parameters + ---------- + fname : filename or file handle + If the filename ends in ``.gz``, the file is automatically saved in + compressed gzip format. `loadtxt` understands gzipped files + transparently. + X : array_like + Data to be saved to a text file. + fmt : str or sequence of strs + A single format (%10.5f), a sequence of formats, or a + multi-format string, e.g. 'Iteration %d -- %10.5f', in which + case `delimiter` is ignored. + delimiter : str + Character separating columns. + newline : str + .. versionadded:: 2.0 + + Character separating lines. + + + See Also + -------- + save : Save an array to a binary file in NumPy ``.npy`` format + savez : Save several arrays into a ``.npz`` compressed archive + + Notes + ----- + Further explanation of the `fmt` parameter + (``%[flag]width[.precision]specifier``): + + flags: + ``-`` : left justify + + ``+`` : Forces to preceed result with + or -. + + ``0`` : Left pad the number with zeros instead of space (see width). + + width: + Minimum number of characters to be printed. The value is not truncated + if it has more characters. + + precision: + - For integer specifiers (eg. ``d,i,o,x``), the minimum number of + digits. + - For ``e, E`` and ``f`` specifiers, the number of digits to print + after the decimal point. + - For ``g`` and ``G``, the maximum number of significant digits. + - For ``s``, the maximum number of characters. + + specifiers: + ``c`` : character + + ``d`` or ``i`` : signed decimal integer + + ``e`` or ``E`` : scientific notation with ``e`` or ``E``. + + ``f`` : decimal floating point + + ``g,G`` : use the shorter of ``e,E`` or ``f`` + + ``o`` : signed octal + + ``s`` : string of characters + + ``u`` : unsigned decimal integer + + ``x,X`` : unsigned hexadecimal integer + + This explanation of ``fmt`` is not complete, for an exhaustive + specification see [1]_. + + References + ---------- + .. [1] `Format Specification Mini-Language + `_, Python Documentation. + + Examples + -------- + >>> x = y = z = np.arange(0.0,5.0,1.0) + >>> np.savetxt('test.out', x, delimiter=',') # X is an array + >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays + >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation + + """ + + # Py3 conversions first + if isinstance(fmt, bytes): + fmt = asstr(fmt) + delimiter = asstr(delimiter) + + if _is_string_like(fname): + if fname.endswith('.gz'): + import gzip + fh = gzip.open(fname, 'wb') + else: + if sys.version_info[0] >= 3: + fh = file(fname, 'wb') + else: + fh = file(fname, 'w') + elif hasattr(fname, 'seek'): + fh = fname + else: + raise ValueError('fname must be a string or file handle') + + X = np.asarray(X) + + # Handle 1-dimensional arrays + if X.ndim == 1: + # Common case -- 1d array of numbers + if X.dtype.names is None: + X = np.atleast_2d(X).T + ncol = 1 + + # Complex dtype -- each field indicates a separate column + else: + ncol = len(X.dtype.descr) + else: + ncol = X.shape[1] + + # `fmt` can be a string with multiple insertion points or a list of formats. + # E.g. '%10.5f\t%10d' or ('%10.5f', '$10d') + if type(fmt) in (list, tuple): + if len(fmt) != ncol: + raise AttributeError('fmt has wrong shape. %s' % str(fmt)) + format = asstr(delimiter).join(map(asstr, fmt)) + elif type(fmt) is str: + if fmt.count('%') == 1: + fmt = [fmt, ]*ncol + format = delimiter.join(fmt) + elif fmt.count('%') != ncol: + raise AttributeError('fmt has wrong number of %% formats. %s' + % fmt) + else: + format = fmt + + for row in X: + fh.write(asbytes(format % tuple(row) + newline)) + +import re +def fromregex(file, regexp, dtype): + """ + Construct an array from a text file, using regular expression parsing. + + The returned array is always a structured array, and is constructed from + all matches of the regular expression in the file. Groups in the regular + expression are converted to fields of the structured array. + + Parameters + ---------- + file : str or file + File name or file object to read. + regexp : str or regexp + Regular expression used to parse the file. + Groups in the regular expression correspond to fields in the dtype. + dtype : dtype or list of dtypes + Dtype for the structured array. + + Returns + ------- + output : ndarray + The output array, containing the part of the content of `file` that + was matched by `regexp`. `output` is always a structured array. + + Raises + ------ + TypeError + When `dtype` is not a valid dtype for a structured array. + + See Also + -------- + fromstring, loadtxt + + Notes + ----- + Dtypes for structured arrays can be specified in several forms, but all + forms specify at least the data type and field name. For details see + `doc.structured_arrays`. + + Examples + -------- + >>> f = open('test.dat', 'w') + >>> f.write("1312 foo\\n1534 bar\\n444 qux") + >>> f.close() + + >>> regexp = r"(\\d+)\\s+(...)" # match [digits, whitespace, anything] + >>> output = np.fromregex('test.dat', regexp, + ... [('num', np.int64), ('key', 'S3')]) + >>> output + array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')], + dtype=[('num', '>> output['num'] + array([1312, 1534, 444], dtype=int64) + + """ + if not hasattr(file, "read"): + file = open(file, 'rb') + if not hasattr(regexp, 'match'): + regexp = re.compile(asbytes(regexp)) + if not isinstance(dtype, np.dtype): + dtype = np.dtype(dtype) + + seq = regexp.findall(file.read()) + if seq and not isinstance(seq[0], tuple): + # Only one group is in the regexp. + # Create the new array as a single data-type and then + # re-interpret as a single-field structured array. + newdtype = np.dtype(dtype[dtype.names[0]]) + output = np.array(seq, dtype=newdtype) + output.dtype = dtype + else: + output = np.array(seq, dtype=dtype) + + return output + + + + +#####-------------------------------------------------------------------------- +#---- --- ASCII functions --- +#####-------------------------------------------------------------------------- + + + +def genfromtxt(fname, dtype=float, comments='#', delimiter=None, + skiprows=0, skip_header=0, skip_footer=0, converters=None, + missing='', missing_values=None, filling_values=None, + usecols=None, names=None, excludelist=None, deletechars=None, + autostrip=False, case_sensitive=True, defaultfmt="f%i", + unpack=None, usemask=False, loose=True, invalid_raise=True): + """ + Load data from a text file, with missing values handled as specified. + + Each line past the first `skiprows` lines is split at the `delimiter` + character, and characters following the `comments` character are discarded. + + Parameters + ---------- + fname : file or str + File or filename to read. If the filename extension is `.gz` or + `.bz2`, the file is first decompressed. + dtype : dtype, optional + Data type of the resulting array. + If None, the dtypes will be determined by the contents of each + column, individually. + comments : str, optional + The character used to indicate the start of a comment. + All the characters occurring on a line after a comment are discarded + delimiter : str, int, or sequence, optional + The string used to separate values. By default, any consecutive + whitespaces act as delimiter. An integer or sequence of integers + can also be provided as width(s) of each field. + skip_header : int, optional + The numbers of lines to skip at the beginning of the file. + skip_footer : int, optional + The numbers of lines to skip at the end of the file + converters : variable or None, optional + The set of functions that convert the data of a column to a value. + The converters can also be used to provide a default value + for missing data: ``converters = {3: lambda s: float(s or 0)}``. + missing_values : variable or None, optional + The set of strings corresponding to missing data. + filling_values : variable or None, optional + The set of values to be used as default when the data are missing. + usecols : sequence or None, optional + Which columns to read, with 0 being the first. For example, + ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns. + names : {None, True, str, sequence}, optional + If `names` is True, the field names are read from the first valid line + after the first `skiprows` lines. + If `names` is a sequence or a single-string of comma-separated names, + the names will be used to define the field names in a structured dtype. + If `names` is None, the names of the dtype fields will be used, if any. + excludelist : sequence, optional + A list of names to exclude. This list is appended to the default list + ['return','file','print']. Excluded names are appended an underscore: + for example, `file` would become `file_`. + deletechars : str, optional + A string combining invalid characters that must be deleted from the + names. + defaultfmt : str, optional + A format used to define default field names, such as "f%i" or "f_%02i". + autostrip : bool, optional + Whether to automatically strip white spaces from the variables. + case_sensitive : {True, False, 'upper', 'lower'}, optional + If True, field names are case sensitive. + If False or 'upper', field names are converted to upper case. + If 'lower', field names are converted to lower case. + unpack : bool, optional + If True, the returned array is transposed, so that arguments may be + unpacked using ``x, y, z = loadtxt(...)`` + usemask : bool, optional + If True, return a masked array. + If False, return a regular array. + invalid_raise : bool, optional + If True, an exception is raised if an inconsistency is detected in the + number of columns. + If False, a warning is emitted and the offending lines are skipped. + + Returns + ------- + out : ndarray + Data read from the text file. If `usemask` is True, this is a + masked array. + + See Also + -------- + numpy.loadtxt : equivalent function when no data is missing. + + Notes + ----- + * When spaces are used as delimiters, or when no delimiter has been given + as input, there should not be any missing data between two fields. + * When the variables are named (either by a flexible dtype or with `names`, + there must not be any header in the file (else a ValueError + exception is raised). + * Individual values are not stripped of spaces by default. + When using a custom converter, make sure the function does remove spaces. + + Examples + --------- + >>> from StringIO import StringIO + >>> import numpy as np + + Comma delimited file with mixed dtype + + >>> s = StringIO("1,1.3,abcde") + >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), + ... ('mystring','S5')], delimiter=",") + >>> data + array((1, 1.3, 'abcde'), + dtype=[('myint', '>> s.seek(0) # needed for StringIO example only + >>> data = np.genfromtxt(s, dtype=None, + ... names = ['myint','myfloat','mystring'], delimiter=",") + >>> data + array((1, 1.3, 'abcde'), + dtype=[('myint', '>> s.seek(0) + >>> data = np.genfromtxt(s, dtype="i8,f8,S5", + ... names=['myint','myfloat','mystring'], delimiter=",") + >>> data + array((1, 1.3, 'abcde'), + dtype=[('myint', '>> s = StringIO("11.3abcde") + >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], + ... delimiter=[1,3,5]) + >>> data + array((1, 1.3, 'abcde'), + dtype=[('intvar', ' nbcols): + descr = dtype.descr + dtype = np.dtype([descr[_] for _ in usecols]) + names = list(dtype.names) + # If `names` is not None, update the names + elif (names is not None) and (len(names) > nbcols): + names = [names[_] for _ in usecols] + + + # Process the missing values ............................... + # Rename missing_values for convenience + user_missing_values = missing_values or () + + # Define the list of missing_values (one column: one list) + missing_values = [list([asbytes('')]) for _ in range(nbcols)] + + # We have a dictionary: process it field by field + if isinstance(user_missing_values, dict): + # Loop on the items + for (key, val) in user_missing_values.items(): + # Is the key a string ? + if _is_string_like(key): + try: + # Transform it into an integer + key = names.index(key) + except ValueError: + # We couldn't find it: the name must have been dropped, then + continue + # Redefine the key as needed if it's a column number + if usecols: + try: + key = usecols.index(key) + except ValueError: + pass + # Transform the value as a list of string + if isinstance(val, (list, tuple)): + val = [str(_) for _ in val] + else: + val = [str(val), ] + # Add the value(s) to the current list of missing + if key is None: + # None acts as default + for miss in missing_values: + miss.extend(val) + else: + missing_values[key].extend(val) + # We have a sequence : each item matches a column + elif isinstance(user_missing_values, (list, tuple)): + for (value, entry) in zip(user_missing_values, missing_values): + value = str(value) + if value not in entry: + entry.append(value) + # We have a string : apply it to all entries + elif isinstance(user_missing_values, bytes): + user_value = user_missing_values.split(asbytes(",")) + for entry in missing_values: + entry.extend(user_value) + # We have something else: apply it to all entries + else: + for entry in missing_values: + entry.extend([str(user_missing_values)]) + + # Process the deprecated `missing` + if missing != asbytes(''): + warnings.warn("The use of `missing` is deprecated.\n"\ + "Please use `missing_values` instead.", + DeprecationWarning) + values = [str(_) for _ in missing.split(asbytes(","))] + for entry in missing_values: + entry.extend(values) + + # Process the filling_values ............................... + # Rename the input for convenience + user_filling_values = filling_values or [] + # Define the default + filling_values = [None] * nbcols + # We have a dictionary : update each entry individually + if isinstance(user_filling_values, dict): + for (key, val) in user_filling_values.items(): + if _is_string_like(key): + try: + # Transform it into an integer + key = names.index(key) + except ValueError: + # We couldn't find it: the name must have been dropped, then + continue + # Redefine the key if it's a column number and usecols is defined + if usecols: + try: + key = usecols.index(key) + except ValueError: + pass + # Add the value to the list + filling_values[key] = val + # We have a sequence : update on a one-to-one basis + elif isinstance(user_filling_values, (list, tuple)): + n = len(user_filling_values) + if (n <= nbcols): + filling_values[:n] = user_filling_values + else: + filling_values = user_filling_values[:nbcols] + # We have something else : use it for all entries + else: + filling_values = [user_filling_values] * nbcols + + # Initialize the converters ................................ + if dtype is None: + # Note: we can't use a [...]*nbcols, as we would have 3 times the same + # ... converter, instead of 3 different converters. + converters = [StringConverter(None, missing_values=miss, default=fill) + for (miss, fill) in zip(missing_values, filling_values)] + else: + dtype_flat = flatten_dtype(dtype, flatten_base=True) + # Initialize the converters + if len(dtype_flat) > 1: + # Flexible type : get a converter from each dtype + zipit = zip(dtype_flat, missing_values, filling_values) + converters = [StringConverter(dt, locked=True, + missing_values=miss, default=fill) + for (dt, miss, fill) in zipit] + else: + # Set to a default converter (but w/ different missing values) + zipit = zip(missing_values, filling_values) + converters = [StringConverter(dtype, locked=True, + missing_values=miss, default=fill) + for (miss, fill) in zipit] + # Update the converters to use the user-defined ones + uc_update = [] + for (i, conv) in user_converters.items(): + # If the converter is specified by column names, use the index instead + if _is_string_like(i): + try: + i = names.index(i) + except ValueError: + continue + elif usecols: + try: + i = usecols.index(i) + except ValueError: + # Unused converter specified + continue + converters[i].update(conv, locked=True, + default=filling_values[i], + missing_values=missing_values[i],) + uc_update.append((i, conv)) + # Make sure we have the corrected keys in user_converters... + user_converters.update(uc_update) + + miss_chars = [_.missing_values for _ in converters] + + + # Initialize the output lists ... + # ... rows + rows = [] + append_to_rows = rows.append + # ... masks + if usemask: + masks = [] + append_to_masks = masks.append + # ... invalid + invalid = [] + append_to_invalid = invalid.append + + # Parse each line + for (i, line) in enumerate(itertools.chain([first_line, ], fhd)): + values = split_line(line) + nbvalues = len(values) + # Skip an empty line + if nbvalues == 0: + continue + # Select only the columns we need + if usecols: + try: + values = [values[_] for _ in usecols] + except IndexError: + append_to_invalid((i, nbvalues)) + continue + elif nbvalues != nbcols: + append_to_invalid((i, nbvalues)) + continue + # Store the values + append_to_rows(tuple(values)) + if usemask: + append_to_masks(tuple([v.strip() in m + for (v, m) in zip(values, missing_values)])) + + # Strip the last skip_footer data + if skip_footer > 0: + rows = rows[:-skip_footer] + if usemask: + masks = masks[:-skip_footer] + + # Upgrade the converters (if needed) + if dtype is None: + for (i, converter) in enumerate(converters): + current_column = map(itemgetter(i), rows) + try: + converter.iterupgrade(current_column) + except ConverterLockError: + errmsg = "Converter #%i is locked and cannot be upgraded: " % i + current_column = itertools.imap(itemgetter(i), rows) + for (j, value) in enumerate(current_column): + try: + converter.upgrade(value) + except (ConverterError, ValueError): + errmsg += "(occurred line #%i for value '%s')" + errmsg %= (j + 1 + skip_header, value) + raise ConverterError(errmsg) + + # Check that we don't have invalid values + if len(invalid) > 0: + nbrows = len(rows) + # Construct the error message + template = " Line #%%i (got %%i columns instead of %i)" % nbcols + if skip_footer > 0: + nbrows -= skip_footer + errmsg = [template % (i + skip_header + 1, nb) + for (i, nb) in invalid if i < nbrows] + else: + errmsg = [template % (i + skip_header + 1, nb) + for (i, nb) in invalid] + if len(errmsg): + errmsg.insert(0, "Some errors were detected !") + errmsg = "\n".join(errmsg) + # Raise an exception ? + if invalid_raise: + raise ValueError(errmsg) + # Issue a warning ? + else: + warnings.warn(errmsg, ConversionWarning) + + # Convert each value according to the converter: + # We want to modify the list in place to avoid creating a new one... +# if loose: +# conversionfuncs = [conv._loose_call for conv in converters] +# else: +# conversionfuncs = [conv._strict_call for conv in converters] +# for (i, vals) in enumerate(rows): +# rows[i] = tuple([convert(val) +# for (convert, val) in zip(conversionfuncs, vals)]) + if loose: + rows = zip(*[map(converter._loose_call, map(itemgetter(i), rows)) + for (i, converter) in enumerate(converters)]) + else: + rows = zip(*[map(converter._strict_call, map(itemgetter(i), rows)) + for (i, converter) in enumerate(converters)]) + # Reset the dtype + data = rows + if dtype is None: + # Get the dtypes from the types of the converters + column_types = [conv.type for conv in converters] + # Find the columns with strings... + strcolidx = [i for (i, v) in enumerate(column_types) + if v in (type('S'), np.string_)] + # ... and take the largest number of chars. + for i in strcolidx: + column_types[i] = "|S%i" % max(len(row[i]) for row in data) + # + if names is None: + # If the dtype is uniform, don't define names, else use '' + base = set([c.type for c in converters if c._checked]) + if len(base) == 1: + (ddtype, mdtype) = (list(base)[0], np.bool) + else: + ddtype = [(defaultfmt % i, dt) + for (i, dt) in enumerate(column_types)] + if usemask: + mdtype = [(defaultfmt % i, np.bool) + for (i, dt) in enumerate(column_types)] + else: + ddtype = zip(names, column_types) + mdtype = zip(names, [np.bool] * len(column_types)) + output = np.array(data, dtype=ddtype) + if usemask: + outputmask = np.array(masks, dtype=mdtype) + else: + # Overwrite the initial dtype names if needed + if names and dtype.names: + dtype.names = names + # Case 1. We have a structured type + if len(dtype_flat) > 1: + # Nested dtype, eg [('a', int), ('b', [('b0', int), ('b1', 'f4')])] + # First, create the array using a flattened dtype: + # [('a', int), ('b1', int), ('b2', float)] + # Then, view the array using the specified dtype. + if 'O' in (_.char for _ in dtype_flat): + if has_nested_fields(dtype): + errmsg = "Nested fields involving objects "\ + "are not supported..." + raise NotImplementedError(errmsg) + else: + output = np.array(data, dtype=dtype) + else: + rows = np.array(data, dtype=[('', _) for _ in dtype_flat]) + output = rows.view(dtype) + # Now, process the rowmasks the same way + if usemask: + rowmasks = np.array(masks, + dtype=np.dtype([('', np.bool) + for t in dtype_flat])) + # Construct the new dtype + mdtype = make_mask_descr(dtype) + outputmask = rowmasks.view(mdtype) + # Case #2. We have a basic dtype + else: + # We used some user-defined converters + if user_converters: + ishomogeneous = True + descr = [] + for (i, ttype) in enumerate([conv.type for conv in converters]): + # Keep the dtype of the current converter + if i in user_converters: + ishomogeneous &= (ttype == dtype.type) + if ttype == np.string_: + ttype = "|S%i" % max(len(row[i]) for row in data) + descr.append(('', ttype)) + else: + descr.append(('', dtype)) + # So we changed the dtype ? + if not ishomogeneous: + # We have more than one field + if len(descr) > 1: + dtype = np.dtype(descr) + # We have only one field: drop the name if not needed. + else: + dtype = np.dtype(ttype) + # + output = np.array(data, dtype) + if usemask: + if dtype.names: + mdtype = [(_, np.bool) for _ in dtype.names] + else: + mdtype = np.bool + outputmask = np.array(masks, dtype=mdtype) + # Try to take care of the missing data we missed + names = output.dtype.names + if usemask and names: + for (name, conv) in zip(names or (), converters): + missing_values = [conv(_) for _ in conv.missing_values + if _ != asbytes('')] + for mval in missing_values: + outputmask[name] |= (output[name] == mval) + # Construct the final array + if usemask: + output = output.view(MaskedArray) + output._mask = outputmask + if unpack: + return output.squeeze().T + return output.squeeze() + + + +def ndfromtxt(fname, **kwargs): + """ + Load ASCII data stored in a file and return it as a single array. + + Complete description of all the optional input parameters is available in + the docstring of the `genfromtxt` function. + + See Also + -------- + numpy.genfromtxt : generic function. + + """ + kwargs['usemask'] = False + return genfromtxt(fname, **kwargs) + + +def mafromtxt(fname, **kwargs): + """ + Load ASCII data stored in a text file and return a masked array. + + For a complete description of all the input parameters, see `genfromtxt`. + + See Also + -------- + numpy.genfromtxt : generic function to load ASCII data. + + """ + kwargs['usemask'] = True + return genfromtxt(fname, **kwargs) + + +def recfromtxt(fname, **kwargs): + """ + Load ASCII data from a file and return it in a record array. + + If ``usemask=False`` a standard `recarray` is returned, + if ``usemask=True`` a MaskedRecords array is returned. + + Complete description of all the optional input parameters is available in + the docstring of the `genfromtxt` function. + + See Also + -------- + numpy.genfromtxt : generic function + + Notes + ----- + By default, `dtype` is None, which means that the data-type of the output + array will be determined from the data. + + """ + kwargs.update(dtype=kwargs.get('dtype', None)) + usemask = kwargs.get('usemask', False) + output = genfromtxt(fname, **kwargs) + if usemask: + from numpy.ma.mrecords import MaskedRecords + output = output.view(MaskedRecords) + else: + output = output.view(np.recarray) + return output + + +def recfromcsv(fname, **kwargs): + """ + Load ASCII data stored in a comma-separated file. + + The returned array is a record array (if ``usemask=False``, see + `recarray`) or a masked record array (if ``usemask=True``, + see `ma.mrecords.MaskedRecords`). + + For a complete description of all the input parameters, see `genfromtxt`. + + See Also + -------- + numpy.genfromtxt : generic function to load ASCII data. + + """ + case_sensitive = kwargs.get('case_sensitive', "lower") or "lower" + names = kwargs.get('names', True) + if names is None: + names = True + kwargs.update(dtype=kwargs.get('update', None), + delimiter=kwargs.get('delimiter', ",") or ",", + names=names, + case_sensitive=case_sensitive) + usemask = kwargs.get("usemask", False) + output = genfromtxt(fname, **kwargs) + if usemask: + from numpy.ma.mrecords import MaskedRecords + output = output.view(MaskedRecords) + else: + output = output.view(np.recarray) + return output Modified: trunk/tools/py3tool.py =================================================================== --- trunk/tools/py3tool.py 2010-03-24 18:18:38 UTC (rev 8301) +++ trunk/tools/py3tool.py 2010-03-24 18:18:43 UTC (rev 8302) @@ -141,7 +141,7 @@ os.path.join('core', 'arrayprint.py'), os.path.join('core', 'fromnumeric.py'), os.path.join('numpy', '__init__.py'), - os.path.join('lib', 'io.py'), + os.path.join('lib', 'npyio.py'), os.path.join('lib', 'function_base.py'), os.path.join('fft', 'fftpack.py'), os.path.join('random', '__init__.py'), @@ -166,7 +166,7 @@ f.write(text) f.close() - if filename.endswith(os.path.join('lib', 'io.py')): + if filename.endswith(os.path.join('lib', 'npyio.py')): f = open(filename, 'r') text = f.read() f.close() From numpy-svn at scipy.org Wed Mar 24 23:32:13 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Wed, 24 Mar 2010 22:32:13 -0500 (CDT) Subject: [Numpy-svn] r8303 - trunk/tools Message-ID: <20100325033213.1193C39CAE7@scipy.org> Author: charris Date: 2010-03-24 22:32:12 -0500 (Wed, 24 Mar 2010) New Revision: 8303 Modified: trunk/tools/py3tool.py Log: CLN: Remove some now unnecessary fixups to lib/npyio. Modified: trunk/tools/py3tool.py =================================================================== --- trunk/tools/py3tool.py 2010-03-24 18:18:43 UTC (rev 8302) +++ trunk/tools/py3tool.py 2010-03-25 03:32:12 UTC (rev 8303) @@ -166,15 +166,6 @@ f.write(text) f.close() - if filename.endswith(os.path.join('lib', 'npyio.py')): - f = open(filename, 'r') - text = f.read() - f.close() - text = text.replace('from . import io', 'import io') - f = open(filename, 'w') - f.write(text) - f.close() - def walk_sync(dir1, dir2, _seen=None): if _seen is None: seen = {} From numpy-svn at scipy.org Wed Mar 24 23:32:15 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Wed, 24 Mar 2010 22:32:15 -0500 (CDT) Subject: [Numpy-svn] r8304 - trunk/numpy/lib Message-ID: <20100325033215.87FA539CAEE@scipy.org> Author: charris Date: 2010-03-24 22:32:15 -0500 (Wed, 24 Mar 2010) New Revision: 8304 Modified: trunk/numpy/lib/npyio.py Log: CLN: Small cleanups. Fix docstring examples. Modified: trunk/numpy/lib/npyio.py =================================================================== --- trunk/numpy/lib/npyio.py 2010-03-25 03:32:12 UTC (rev 8303) +++ trunk/numpy/lib/npyio.py 2010-03-25 03:32:15 UTC (rev 8304) @@ -1,10 +1,6 @@ -__all__ = ['savetxt', 'loadtxt', - 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', - 'load', 'loads', - 'save', 'savez', - 'packbits', 'unpackbits', - 'fromregex', - 'DataSource'] +__all__ = ['savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', + 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez', + 'packbits', 'unpackbits', 'fromregex', 'DataSource'] import numpy as np import format @@ -27,8 +23,7 @@ from numpy.compat import asbytes, asstr, asbytes_nested, bytes if sys.version_info[0] >= 3: - import io - BytesIO = io.BytesIO + from io import BytesIO else: from cStringIO import StringIO as BytesIO @@ -93,7 +88,7 @@ ... return key ... >>> demo_obj = BagDemo() - >>> bagobj = np.lib.io.BagObj(demo_obj) + >>> bagobj = np.lib.npyio.BagObj(demo_obj) >>> bagobj.some_item 'some_item' @@ -149,7 +144,7 @@ >>> outfile.seek(0) >>> npz = np.load(outfile) - >>> isinstance(npz, np.lib.io.NpzFile) + >>> isinstance(npz, np.lib.npyio.NpzFile) True >>> npz.files ['y', 'x'] From numpy-svn at scipy.org Wed Mar 24 23:32:17 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Wed, 24 Mar 2010 22:32:17 -0500 (CDT) Subject: [Numpy-svn] r8305 - trunk/numpy/lib Message-ID: <20100325033217.E299A39CAEE@scipy.org> Author: charris Date: 2010-03-24 22:32:17 -0500 (Wed, 24 Mar 2010) New Revision: 8305 Modified: trunk/numpy/lib/utils.py Log: BUG: Update StringIO in _lookfor_generate_cache for Python3.x. Make small cleanup. Modified: trunk/numpy/lib/utils.py =================================================================== --- trunk/numpy/lib/utils.py 2010-03-25 03:32:15 UTC (rev 8304) +++ trunk/numpy/lib/utils.py 2010-03-25 03:32:17 UTC (rev 8305) @@ -6,11 +6,10 @@ from numpy.core.numerictypes import issubclass_, issubsctype, issubdtype from numpy.core import product, ndarray -__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', - 'issubdtype', 'deprecate', 'deprecate_with_doc', - 'get_numarray_include', 'get_include', - 'info', 'source', 'who', 'lookfor', - 'byte_bounds', 'may_share_memory', 'safe_eval'] +__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', 'issubdtype', + 'deprecate', 'deprecate_with_doc', 'get_numarray_include', + 'get_include', 'info', 'source', 'who', 'lookfor', 'byte_bounds', + 'may_share_memory', 'safe_eval'] def get_include(): """ @@ -848,7 +847,10 @@ global _lookfor_caches # Local import to speed up numpy's import time. import inspect - from StringIO import StringIO + if sys.version_info[0] >= 3: + from io import BytesIO + else: + from cStringIO import StringIO as BytesIO if module is None: module = "numpy" @@ -916,8 +918,8 @@ old_stdout = sys.stdout old_stderr = sys.stderr try: - sys.stdout = StringIO() - sys.stderr = StringIO() + sys.stdout = BytesIO() + sys.stderr = BytesIO() __import__("%s.%s" % (name, to_import)) finally: sys.stdout = old_stdout From numpy-svn at scipy.org Fri Mar 26 01:15:57 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Fri, 26 Mar 2010 00:15:57 -0500 (CDT) Subject: [Numpy-svn] r8306 - in trunk/numpy/lib: . tests Message-ID: <20100326051557.66F1439CAF4@scipy.org> Author: pierregm Date: 2010-03-26 00:15:57 -0500 (Fri, 26 Mar 2010) New Revision: 8306 Modified: trunk/numpy/lib/recfunctions.py trunk/numpy/lib/tests/test_recfunctions.py Log: * Fixed merge_arrays for arrays of size 1 (bug #1407) * merge_arrays now accepts sequences of lists/tuples as inputs Modified: trunk/numpy/lib/recfunctions.py =================================================================== --- trunk/numpy/lib/recfunctions.py 2010-03-25 03:32:17 UTC (rev 8305) +++ trunk/numpy/lib/recfunctions.py 2010-03-26 05:15:57 UTC (rev 8306) @@ -217,7 +217,7 @@ current = adtype[name] if current.names: if lastname: - parents[name] = [lastname,] + parents[name] = [lastname, ] else: parents[name] = [] parents.update(get_fieldstructure(current, name, parents)) @@ -227,7 +227,7 @@ # if (lastparent[-1] != lastname): lastparent.append(lastname) elif lastname: - lastparent = [lastname,] + lastparent = [lastname, ] parents[name] = lastparent or [] return parents or None @@ -274,7 +274,7 @@ Whether to """ # OK, that's a complete ripoff from Python2.6 itertools.izip_longest - def sentinel(counter = ([fill_value]*(len(seqarrays)-1)).pop): + def sentinel(counter=([fill_value] * (len(seqarrays) - 1)).pop): "Yields the fill_value or raises IndexError" yield counter() # @@ -324,8 +324,9 @@ return output + def merge_arrays(seqarrays, - fill_value=-1, flatten=False, usemask=True, asrecarray=False): + fill_value= -1, flatten=False, usemask=False, asrecarray=False): """ Merge arrays field by field. @@ -372,62 +373,92 @@ True for boolean values * XXX: I just obtained these values empirically """ + # Only one item in the input sequence ? if (len(seqarrays) == 1): - seqarrays = seqarrays[0] - if isinstance(seqarrays, ndarray): + seqarrays = np.asanyarray(seqarrays[0]) + # Do we have a single ndarary as input ? + if isinstance(seqarrays, (ndarray, np.void)): seqdtype = seqarrays.dtype if (not flatten) or \ (zip_descr((seqarrays,), flatten=True) == seqdtype.descr): + # Minimal processing needed: just make sure everythng's a-ok seqarrays = seqarrays.ravel() + # Make sure we have named fields if not seqdtype.names: - seqarrays = seqarrays.view([('', seqdtype)]) + seqdtype = [('', seqdtype)] + # Find what type of array we must return if usemask: if asrecarray: - return seqarrays.view(MaskedRecords) - return seqarrays.view(MaskedArray) + seqtype = MaskedRecords + else: + seqtype = MaskedArray elif asrecarray: - return seqarrays.view(recarray) - return seqarrays + seqtype = recarray + else: + seqtype = ndarray + return seqarrays.view(dtype=seqdtype, type=seqtype) else: seqarrays = (seqarrays,) - # Get the dtype + else: + # Make sure we have arrays in the input sequence + seqarrays = map(np.asanyarray, seqarrays) + # Find the sizes of the inputs and their maximum + sizes = tuple(a.size for a in seqarrays) + maxlength = max(sizes) + # Get the dtype of the output (flattening if needed) newdtype = zip_descr(seqarrays, flatten=flatten) - # Get the data and the fill_value from each array - seqdata = [ma.getdata(a.ravel()) for a in seqarrays] - seqmask = [ma.getmaskarray(a).ravel() for a in seqarrays] - fill_value = [_check_fill_value(fill_value, a.dtype) for a in seqdata] - # Make an iterator from each array, padding w/ fill_values - maxlength = max(len(a) for a in seqarrays) - for (i, (a, m, fval)) in enumerate(zip(seqdata, seqmask, fill_value)): - # Flatten the fill_values if there's only one field - if isinstance(fval, (ndarray, np.void)): - fmsk = ma.ones((1,), m.dtype)[0] - if len(fval.dtype) == 1: - fval = fval.item()[0] + # Initialize the sequences for data and mask + seqdata = [] + seqmask = [] + # If we expect some kind of MaskedArray, make a special loop. + if usemask: + for (a, n) in itertools.izip(seqarrays, sizes): + nbmissing = (maxlength - n) + # Get the data and mask + data = a.ravel().__array__() + mask = ma.getmaskarray(a).ravel() + # Get the filling value (if needed) + if nbmissing: + fval = _check_fill_value(fill_value, a.dtype) + if isinstance(fval, (ndarray, np.void)): + if len(fval.dtype) == 1: + fval = fval.item()[0] + fmsk = True + else: + fval = np.array(fval, dtype=a.dtype, ndmin=1) + fmsk = np.ones((1,), dtype=mask.dtype) + else: + fval = None fmsk = True - else: - # fval and fmsk should be np.void objects - fval = np.array([fval,], dtype=a.dtype)[0] -# fmsk = np.array([fmsk,], dtype=m.dtype)[0] - else: - fmsk = True - nbmissing = (maxlength-len(a)) - seqdata[i] = itertools.chain(a, [fval]*nbmissing) - seqmask[i] = itertools.chain(m, [fmsk]*nbmissing) - # - data = izip_records(seqdata, flatten=flatten) - data = tuple(data) - if usemask: - mask = izip_records(seqmask, fill_value=True, flatten=flatten) - mask = tuple(mask) - output = ma.array(np.fromiter(data, dtype=newdtype)) - output._mask[:] = list(mask) + # Store an iterator padding the input to the expected length + seqdata.append(itertools.chain(data, [fval] * nbmissing)) + seqmask.append(itertools.chain(mask, [fmsk] * nbmissing)) + # Create an iterator for the data + data = tuple(izip_records(seqdata, flatten=flatten)) + output = ma.array(np.fromiter(data, dtype=newdtype, count=maxlength), + mask=list(izip_records(seqmask, flatten=flatten))) if asrecarray: output = output.view(MaskedRecords) else: - output = np.fromiter(data, dtype=newdtype) + # Same as before, without the mask we don't need... + for (a, n) in itertools.izip(seqarrays, sizes): + nbmissing = (maxlength - n) + data = a.ravel().__array__() + if nbmissing: + fval = _check_fill_value(fill_value, a.dtype) + if isinstance(fval, (ndarray, np.void)): + if len(fval.dtype) == 1: + fval = fval.item()[0] + else: + fval = np.array(fval, dtype=a.dtype, ndmin=1) + else: + fval = None + seqdata.append(itertools.chain(data, [fval] * nbmissing)) + output = np.fromiter(tuple(izip_records(seqdata, flatten=flatten)), + dtype=newdtype, count=maxlength) if asrecarray: output = output.view(recarray) + # And we're done... return output @@ -467,7 +498,7 @@ dtype=[('a', ' '%s'" %\ + raise TypeError("Incompatible type '%s' <> '%s'" % \ (dict(newdescr)[name], descr[1])) # Only one field: use concatenate if len(newdescr) == 1: @@ -849,14 +880,14 @@ "'outer' or 'leftouter' (got '%s' instead)" % jointype) # If we have a single key, put it in a tuple if isinstance(key, basestring): - key = (key, ) + key = (key,) # Check the keys for name in key: if name not in r1.dtype.names: - raise ValueError('r1 does not have key field %s'%name) + raise ValueError('r1 does not have key field %s' % name) if name not in r2.dtype.names: - raise ValueError('r2 does not have key field %s'%name) + raise ValueError('r2 does not have key field %s' % name) # Make sure we work with ravelled arrays r1 = r1.ravel() @@ -915,7 +946,7 @@ else: current[0] += r1postfix desc[0] += r2postfix - ndtype.insert(nameidx+1, desc) + ndtype.insert(nameidx + 1, desc) #... we haven't: just add the description to the current list else: names.extend(desc[0]) @@ -934,7 +965,7 @@ current = output[f] current[:r1cmn] = selected[:r1cmn] if jointype in ('outer', 'leftouter'): - current[cmn:cmn+r1spc] = selected[r1cmn:] + current[cmn:cmn + r1spc] = selected[r1cmn:] for f in r2names: selected = s2[f] if f not in names: Modified: trunk/numpy/lib/tests/test_recfunctions.py =================================================================== --- trunk/numpy/lib/tests/test_recfunctions.py 2010-03-25 03:32:17 UTC (rev 8305) +++ trunk/numpy/lib/tests/test_recfunctions.py 2010-03-26 05:15:57 UTC (rev 8306) @@ -17,7 +17,7 @@ """ # def setUp(self): - x = np.array([1, 2,]) + x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array([('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) @@ -41,7 +41,7 @@ assert_equal(test, np.dtype([('', int), ('A', '|S3'), ('B', float)])) test = zip_descr((x, z), flatten=False) - assert_equal(test, + assert_equal(test, np.dtype([('', int), ('', [('A', '|S3'), ('B', float)])])) # Standard & nested dtype @@ -71,7 +71,7 @@ control = np.array([(1,), (4,)], dtype=[('a', int)]) assert_equal(test, control) # A nested sub-field - test = drop_fields(a, ['ba',]) + test = drop_fields(a, ['ba', ]) control = np.array([(1, (3.0,)), (4, (6.0,))], dtype=[('a', int), ('b', [('bb', int)])]) assert_equal(test, control) @@ -127,13 +127,13 @@ # One 1-nested field ndtype = np.dtype([('A', int), ('B', [('BA', float), ('BB', '|S1')])]) test = get_fieldstructure(ndtype) - assert_equal(test, {'A': [], 'B': [], 'BA':['B',], 'BB':['B']}) + assert_equal(test, {'A': [], 'B': [], 'BA':['B', ], 'BB':['B']}) # One 2-nested fields ndtype = np.dtype([('A', int), ('B', [('BA', int), ('BB', [('BBA', int), ('BBB', int)])])]) test = get_fieldstructure(ndtype) - control = {'A': [], 'B': [], 'BA': ['B'], 'BB': ['B'], + control = {'A': [], 'B': [], 'BA': ['B'], 'BB': ['B'], 'BBA': ['B', 'BB'], 'BBB': ['B', 'BB']} assert_equal(test, control) @@ -173,7 +173,7 @@ def test_find_duplicates_ignoremask(self): "Test the ignoremask option of find_duplicates" ndtype = [('a', int)] - a = ma.array([1, 1, 1, 2, 2, 3, 3], + a = ma.array([1, 1, 1, 2, 2, 3, 3], mask=[0, 0, 1, 0, 0, 0, 1]).view(ndtype) test = find_duplicates(a, ignoremask=True, return_index=True) control = [0, 1, 3, 4] @@ -218,7 +218,7 @@ Test merge_arrays """ def setUp(self): - x = np.array([1, 2,]) + x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array([('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) w = np.array([(1, (2, 3.0)), (4, (5, 6.0))], @@ -291,7 +291,7 @@ assert_equal(test, control) # test = merge_arrays((x, w), flatten=False) - controldtype = dtype=[('f0', int), + controldtype = dtype = [('f0', int), ('f1', [('a', int), ('b', [('ba', float), ('bb', int)])])] control = np.array([(1., (1, (2, 3.0))), (2, (4, (5, 6.0)))], @@ -325,6 +325,15 @@ test = merge_arrays((z, np.array([10, 20, 30]).view([('C', int)]))) control = np.array([('A', 1., 10), ('B', 2., 20), ('-1', -1, 20)], dtype=[('A', '|S3'), ('B', float), ('C', int)]) + # + def test_singlerecord(self): + (_, x, y, z) = self.data + test = merge_arrays((x[0], y[0], z[0]), usemask=False) + control = np.array([(1, 10, ('A', 1))], + dtype=[('f0', int), + ('f1', int), + ('f2', [('A', '|S3'), ('B', float)])]) + assert_equal(test, control) @@ -333,7 +342,7 @@ Test append_fields """ def setUp(self): - x = np.array([1, 2,]) + x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array([('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) w = np.array([(1, (2, 3.0)), (4, (5, 6.0))], @@ -387,7 +396,7 @@ Test stack_arrays """ def setUp(self): - x = np.array([1, 2,]) + x = np.array([1, 2, ]) y = np.array([10, 20, 30]) z = np.array([('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)]) w = np.array([(1, (2, 3.0)), (4, (5, 6.0))], @@ -436,7 +445,7 @@ # test = stack_arrays((z, x)) control = ma.array([('A', 1, -1), ('B', 2, -1), - (-1, -1, 1), (-1, -1, 2),], + (-1, -1, 1), (-1, -1, 2), ], mask=[(0, 0, 1), (0, 0, 1), (1, 1, 0), (1, 1, 0)], dtype=[('A', '|S3'), ('B', float), ('f2', int)]) @@ -446,7 +455,7 @@ test = stack_arrays((z, z, x)) control = ma.array([('A', 1, -1), ('B', 2, -1), ('A', 1, -1), ('B', 2, -1), - (-1, -1, 1), (-1, -1, 2),], + (-1, -1, 1), (-1, -1, 2), ], mask=[(0, 0, 1), (0, 0, 1), (0, 0, 1), (0, 0, 1), (1, 1, 0), (1, 1, 0)], @@ -555,26 +564,26 @@ ('c', int), ('d', int)]) # test = join_by(('a', 'b'), a, b, 'outer') - control = ma.array([( 0, 50, 100, -1), ( 1, 51, 101, -1), - ( 2, 52, 102, -1), ( 3, 53, 103, -1), - ( 4, 54, 104, -1), ( 5, 55, 105, -1), - ( 5, 65, -1, 100), ( 6, 56, 106, -1), - ( 6, 66, -1, 101), ( 7, 57, 107, -1), - ( 7, 67, -1, 102), ( 8, 58, 108, -1), - ( 8, 68, -1, 103), ( 9, 59, 109, -1), - ( 9, 69, -1, 104), (10, 70, -1, 105), - (11, 71, -1, 106), (12, 72, -1, 107), - (13, 73, -1, 108), (14, 74, -1, 109)], - mask=[( 0, 0, 0, 1), ( 0, 0, 0, 1), - ( 0, 0, 0, 1), ( 0, 0, 0, 1), - ( 0, 0, 0, 1), ( 0, 0, 0, 1), - ( 0, 0, 1, 0), ( 0, 0, 0, 1), - ( 0, 0, 1, 0), ( 0, 0, 0, 1), - ( 0, 0, 1, 0), ( 0, 0, 0, 1), - ( 0, 0, 1, 0), ( 0, 0, 0, 1), - ( 0, 0, 1, 0), ( 0, 0, 1, 0), - ( 0, 0, 1, 0), ( 0, 0, 1, 0), - ( 0, 0, 1, 0), ( 0, 0, 1, 0)], + control = ma.array([(0, 50, 100, -1), (1, 51, 101, -1), + (2, 52, 102, -1), (3, 53, 103, -1), + (4, 54, 104, -1), (5, 55, 105, -1), + (5, 65, -1, 100), (6, 56, 106, -1), + (6, 66, -1, 101), (7, 57, 107, -1), + (7, 67, -1, 102), (8, 58, 108, -1), + (8, 68, -1, 103), (9, 59, 109, -1), + (9, 69, -1, 104), (10, 70, -1, 105), + (11, 71, -1, 106), (12, 72, -1, 107), + (13, 73, -1, 108), (14, 74, -1, 109)], + mask=[(0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 0, 1), + (0, 0, 1, 0), (0, 0, 1, 0), + (0, 0, 1, 0), (0, 0, 1, 0), + (0, 0, 1, 0), (0, 0, 1, 0)], dtype=[('a', int), ('b', int), ('c', int), ('d', int)]) assert_equal(test, control) @@ -585,11 +594,11 @@ (4, 54, 104, -1), (5, 55, 105, -1), (6, 56, 106, -1), (7, 57, 107, -1), (8, 58, 108, -1), (9, 59, 109, -1)], - mask=[(0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1), - (0, 0, 0, 1), (0, 0, 0, 1)], + mask=[(0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1), + (0, 0, 0, 1), (0, 0, 0, 1)], dtype=[('a', int), ('b', int), ('c', int), ('d', int)]) From numpy-svn at scipy.org Fri Mar 26 23:56:18 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Fri, 26 Mar 2010 22:56:18 -0500 (CDT) Subject: [Numpy-svn] r8307 - trunk/numpy/lib Message-ID: <20100327035618.0B45939CAF1@scipy.org> Author: charris Date: 2010-03-26 22:56:17 -0500 (Fri, 26 Mar 2010) New Revision: 8307 Modified: trunk/numpy/lib/utils.py Log: BUG: Should be using StringIO, not BytesIO. The 2to3 script takes care of changing the module to import from to io. Modified: trunk/numpy/lib/utils.py =================================================================== --- trunk/numpy/lib/utils.py 2010-03-26 05:15:57 UTC (rev 8306) +++ trunk/numpy/lib/utils.py 2010-03-27 03:56:17 UTC (rev 8307) @@ -847,10 +847,7 @@ global _lookfor_caches # Local import to speed up numpy's import time. import inspect - if sys.version_info[0] >= 3: - from io import BytesIO - else: - from cStringIO import StringIO as BytesIO + from cStringIO import StringIO if module is None: module = "numpy" @@ -918,8 +915,8 @@ old_stdout = sys.stdout old_stderr = sys.stderr try: - sys.stdout = BytesIO() - sys.stderr = BytesIO() + sys.stdout = StringIO() + sys.stderr = StringIO() __import__("%s.%s" % (name, to_import)) finally: sys.stdout = old_stdout From numpy-svn at scipy.org Tue Mar 30 22:26:14 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Tue, 30 Mar 2010 21:26:14 -0500 (CDT) Subject: [Numpy-svn] r8308 - in trunk/numpy/lib: . tests Message-ID: <20100331022614.5E03A39CAE9@scipy.org> Author: cdavid Date: 2010-03-30 21:26:14 -0500 (Tue, 30 Mar 2010) New Revision: 8308 Modified: trunk/numpy/lib/function_base.py trunk/numpy/lib/tests/test_function_base.py Log: BUG: fix kaiser for M=1. Modified: trunk/numpy/lib/function_base.py =================================================================== --- trunk/numpy/lib/function_base.py 2010-03-27 03:56:17 UTC (rev 8307) +++ trunk/numpy/lib/function_base.py 2010-03-31 02:26:14 UTC (rev 8308) @@ -2608,6 +2608,8 @@ """ from numpy.dual import i0 + if M == 1: + return np.array([1.]) n = arange(0,M) alpha = (M-1)/2.0 return i0(beta * sqrt(1-((n-alpha)/alpha)**2.0))/i0(float(beta)) Modified: trunk/numpy/lib/tests/test_function_base.py =================================================================== --- trunk/numpy/lib/tests/test_function_base.py 2010-03-27 03:56:17 UTC (rev 8307) +++ trunk/numpy/lib/tests/test_function_base.py 2010-03-31 02:26:14 UTC (rev 8308) @@ -778,7 +778,7 @@ class TestKaiser(TestCase): def test_simple(self): assert_almost_equal(kaiser(0, 1.0), array([])) - assert isnan(kaiser(1, 1.0)) + assert isfinite(kaiser(1, 1.0)) assert_almost_equal(kaiser(2, 1.0), array([ 0.78984831, 0.78984831])) assert_almost_equal(kaiser(5, 1.0), array([ 0.78984831, 0.94503323, 1. , From numpy-svn at scipy.org Tue Mar 30 22:33:14 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Tue, 30 Mar 2010 21:33:14 -0500 (CDT) Subject: [Numpy-svn] r8309 - in branches/1.4.x/numpy/lib: . tests Message-ID: <20100331023314.0536539CAE9@scipy.org> Author: cdavid Date: 2010-03-30 21:33:13 -0500 (Tue, 30 Mar 2010) New Revision: 8309 Modified: branches/1.4.x/numpy/lib/function_base.py branches/1.4.x/numpy/lib/tests/test_function_base.py Log: BUG: fix kaiser for M=1. Modified: branches/1.4.x/numpy/lib/function_base.py =================================================================== --- branches/1.4.x/numpy/lib/function_base.py 2010-03-31 02:26:14 UTC (rev 8308) +++ branches/1.4.x/numpy/lib/function_base.py 2010-03-31 02:33:13 UTC (rev 8309) @@ -2612,6 +2612,8 @@ """ from numpy.dual import i0 + if M == 1: + return np.array([1.]) n = arange(0,M) alpha = (M-1)/2.0 return i0(beta * sqrt(1-((n-alpha)/alpha)**2.0))/i0(float(beta)) Modified: branches/1.4.x/numpy/lib/tests/test_function_base.py =================================================================== --- branches/1.4.x/numpy/lib/tests/test_function_base.py 2010-03-31 02:26:14 UTC (rev 8308) +++ branches/1.4.x/numpy/lib/tests/test_function_base.py 2010-03-31 02:33:13 UTC (rev 8309) @@ -772,15 +772,15 @@ class TestKaiser(TestCase): def test_simple(self): - assert_almost_equal(kaiser(0,1.0), array([])) - assert isnan(kaiser(1,1.0)) - assert_almost_equal(kaiser(2,1.0), array([ 0.78984831, 0.78984831])) - assert_almost_equal(kaiser(5,1.0), - array([ 0.78984831, 0.94503323, 1. , - 0.94503323, 0.78984831])) - assert_almost_equal(kaiser(5,1.56789), - array([ 0.58285404, 0.88409679, 1. , - 0.88409679, 0.58285404])) + assert_almost_equal(kaiser(0, 1.0), array([])) + assert isfinite(kaiser(1, 1.0)) + assert_almost_equal(kaiser(2, 1.0), array([ 0.78984831, 0.78984831])) + assert_almost_equal(kaiser(5, 1.0), + array([ 0.78984831, 0.94503323, 1. , + 0.94503323, 0.78984831])) + assert_almost_equal(kaiser(5, 1.56789), + array([ 0.58285404, 0.88409679, 1. , + 0.88409679, 0.58285404])) def test_int_beta(self): kaiser(3, 4) From numpy-svn at scipy.org Tue Mar 30 23:45:06 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Tue, 30 Mar 2010 22:45:06 -0500 (CDT) Subject: [Numpy-svn] r8310 - trunk/numpy/core/tests Message-ID: <20100331034506.D0CDE39CAE9@scipy.org> Author: cdavid Date: 2010-03-30 22:45:06 -0500 (Tue, 30 Mar 2010) New Revision: 8310 Modified: trunk/numpy/core/tests/test_regression.py trunk/numpy/core/tests/test_umath.py Log: BUG: fix divide by zero warnings in tests. Modified: trunk/numpy/core/tests/test_regression.py =================================================================== --- trunk/numpy/core/tests/test_regression.py 2010-03-31 02:33:13 UTC (rev 8309) +++ trunk/numpy/core/tests/test_regression.py 2010-03-31 03:45:06 UTC (rev 8310) @@ -1047,15 +1047,19 @@ def test_errobj_reference_leak(self, level=rlevel): """Ticket #955""" - z = int(0) - p = np.int32(-1) + old_err = np.seterr(all="ignore") + try: + z = int(0) + p = np.int32(-1) - gc.collect() - n_before = len(gc.get_objects()) - z**p # this shouldn't leak a reference to errobj - gc.collect() - n_after = len(gc.get_objects()) - assert n_before >= n_after, (n_before, n_after) + gc.collect() + n_before = len(gc.get_objects()) + z**p # this shouldn't leak a reference to errobj + gc.collect() + n_after = len(gc.get_objects()) + assert n_before >= n_after, (n_before, n_after) + finally: + np.seterr(**old_err) def test_void_scalar_with_titles(self, level=rlevel): """No ticket""" @@ -1253,8 +1257,12 @@ min = np.array([np.iinfo(t).min]) min /= -1 - for t in (np.int8, np.int16, np.int32, np.int64, np.int, np.long): - test_type(t) + old_err = np.seterr(divide="ignore") + try: + for t in (np.int8, np.int16, np.int32, np.int64, np.int, np.long): + test_type(t) + finally: + np.seterr(**old_err) def test_buffer_hashlib(self): try: Modified: trunk/numpy/core/tests/test_umath.py =================================================================== --- trunk/numpy/core/tests/test_umath.py 2010-03-31 02:33:13 UTC (rev 8309) +++ trunk/numpy/core/tests/test_umath.py 2010-03-31 03:45:06 UTC (rev 8310) @@ -863,8 +863,12 @@ def test_copysign(): assert np.copysign(1, -1) == -1 - assert 1 / np.copysign(0, -1) < 0 - assert 1 / np.copysign(0, 1) > 0 + old_err = np.seterr(divide="ignore") + try: + assert 1 / np.copysign(0, -1) < 0 + assert 1 / np.copysign(0, 1) > 0 + finally: + np.seterr(**old_err) assert np.signbit(np.copysign(np.nan, -1)) assert not np.signbit(np.copysign(np.nan, 1)) From numpy-svn at scipy.org Tue Mar 30 23:45:16 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Tue, 30 Mar 2010 22:45:16 -0500 (CDT) Subject: [Numpy-svn] r8311 - in trunk/numpy: core/tests lib/tests Message-ID: <20100331034516.3543339CAE9@scipy.org> Author: cdavid Date: 2010-03-30 22:45:16 -0500 (Tue, 30 Mar 2010) New Revision: 8311 Modified: trunk/numpy/core/tests/test_regression.py trunk/numpy/lib/tests/test_regression.py Log: BUG: move test from core to lib, mark it as deprected. Modified: trunk/numpy/core/tests/test_regression.py =================================================================== --- trunk/numpy/core/tests/test_regression.py 2010-03-31 03:45:06 UTC (rev 8310) +++ trunk/numpy/core/tests/test_regression.py 2010-03-31 03:45:16 UTC (rev 8311) @@ -1279,16 +1279,6 @@ # Just make sure this doesn't throw an exception numarray.handleError(0, "") - def test_include_dirs(self): - """As a sanity check, just test that get_include and - get_numarray_include include something reasonable. Somewhat - related to ticket #1405.""" - include_dirs = [np.get_include(), np.get_numarray_include(), - np.get_numpy_include()] - for path in include_dirs: - assert isinstance(path, (str, unicode)) - assert path != '' - def test_0d_string_scalar(self): # Bug #1436; the following should succeed np.asarray('x', '>c') Modified: trunk/numpy/lib/tests/test_regression.py =================================================================== --- trunk/numpy/lib/tests/test_regression.py 2010-03-31 03:45:06 UTC (rev 8310) +++ trunk/numpy/lib/tests/test_regression.py 2010-03-31 03:45:16 UTC (rev 8311) @@ -170,5 +170,16 @@ """Ticket #1387: empty array as input for bincount.""" assert_raises(ValueError, lambda : np.bincount(np.array([], dtype=np.intp))) + @dec.deprecated() + def test_include_dirs(self): + """As a sanity check, just test that get_include and + get_numarray_include include something reasonable. Somewhat + related to ticket #1405.""" + include_dirs = [np.get_include(), np.get_numarray_include(), + np.get_numpy_include()] + for path in include_dirs: + assert isinstance(path, (str, unicode)) + assert path != '' + if __name__ == "__main__": run_module_suite() From numpy-svn at scipy.org Tue Mar 30 23:45:25 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Tue, 30 Mar 2010 22:45:25 -0500 (CDT) Subject: [Numpy-svn] r8312 - in trunk/numpy/lib: . tests Message-ID: <20100331034525.6563239CAE9@scipy.org> Author: cdavid Date: 2010-03-30 22:45:25 -0500 (Tue, 30 Mar 2010) New Revision: 8312 Modified: trunk/numpy/lib/financial.py trunk/numpy/lib/tests/test_financial.py Log: BUG: fix div by zero handling in nper. Modified: trunk/numpy/lib/financial.py =================================================================== --- trunk/numpy/lib/financial.py 2010-03-31 03:45:16 UTC (rev 8311) +++ trunk/numpy/lib/financial.py 2010-03-31 03:45:25 UTC (rev 8312) @@ -255,16 +255,26 @@ """ when = _convert_when(when) rate, pmt, pv, fv, when = map(np.asarray, [rate, pmt, pv, fv, when]) + + use_zero_rate = False + old_err = np.seterr(divide="raise") try: - z = pmt*(1.0+rate*when)/rate - except ZeroDivisionError: - z = 0.0 - A = -(fv + pv)/(pmt+0.0) - B = np.log((-fv+z) / (pv+z))/np.log(1.0+rate) - miter = np.broadcast(rate, pmt, pv, fv, when) - zer = np.zeros(miter.shape) - return np.where(rate==zer, A+zer, B+zer) + 0.0 + try: + z = pmt*(1.0+rate*when)/rate + except FloatingPointError: + use_zero_rate = True + finally: + np.seterr(**old_err) + if use_zero_rate: + return (-fv + pv) / (pmt + 0.0) + else: + A = -(fv + pv)/(pmt+0.0) + B = np.log((-fv+z) / (pv+z))/np.log(1.0+rate) + miter = np.broadcast(rate, pmt, pv, fv, when) + zer = np.zeros(miter.shape) + return np.where(rate==zer, A+zer, B+zer) + 0.0 + def ipmt(rate, per, nper, pv, fv=0.0, when='end'): """ Not implemented. Compute the payment portion for loan interest. Modified: trunk/numpy/lib/tests/test_financial.py =================================================================== --- trunk/numpy/lib/tests/test_financial.py 2010-03-31 03:45:16 UTC (rev 8311) +++ trunk/numpy/lib/tests/test_financial.py 2010-03-31 03:45:25 UTC (rev 8312) @@ -27,7 +27,7 @@ assert_almost_equal(np.nper(0.075,-2000,0,100000.), 21.54, 2) - def test_nper(self): + def test_nper2(self): assert_almost_equal(np.nper(0.0,-2000,0,100000.), 50.0, 1) From numpy-svn at scipy.org Tue Mar 30 23:45:36 2010 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Tue, 30 Mar 2010 22:45:36 -0500 (CDT) Subject: [Numpy-svn] r8313 - in trunk/numpy/f2py: . tests Message-ID: <20100331034536.2622439CAE9@scipy.org> Author: cdavid Date: 2010-03-30 22:45:36 -0500 (Tue, 30 Mar 2010) New Revision: 8313 Modified: trunk/numpy/f2py/__init__.py trunk/numpy/f2py/tests/test_callback.py trunk/numpy/f2py/tests/test_mixed.py trunk/numpy/f2py/tests/test_return_character.py trunk/numpy/f2py/tests/test_return_complex.py trunk/numpy/f2py/tests/test_return_integer.py trunk/numpy/f2py/tests/test_return_logical.py trunk/numpy/f2py/tests/test_return_real.py Log: TST: mark slow f2py tests + add f2py test function. Modified: trunk/numpy/f2py/__init__.py =================================================================== --- trunk/numpy/f2py/__init__.py 2010-03-31 03:45:25 UTC (rev 8312) +++ trunk/numpy/f2py/__init__.py 2010-03-31 03:45:36 UTC (rev 8313) @@ -42,3 +42,7 @@ try: os.remove(fname) except OSError: pass return s + +from numpy.testing import Tester +test = Tester().test +bench = Tester().bench Modified: trunk/numpy/f2py/tests/test_callback.py =================================================================== --- trunk/numpy/f2py/tests/test_callback.py 2010-03-31 03:45:25 UTC (rev 8312) +++ trunk/numpy/f2py/tests/test_callback.py 2010-03-31 03:45:36 UTC (rev 8313) @@ -33,6 +33,7 @@ end """ + @dec.slow def test_all(self): for name in "t,t2".split(","): self.check_function(name) Modified: trunk/numpy/f2py/tests/test_mixed.py =================================================================== --- trunk/numpy/f2py/tests/test_mixed.py 2010-03-31 03:45:25 UTC (rev 8312) +++ trunk/numpy/f2py/tests/test_mixed.py 2010-03-31 03:45:36 UTC (rev 8313) @@ -14,6 +14,7 @@ _path('src', 'mixed', 'foo_fixed.f90'), _path('src', 'mixed', 'foo_free.f90')] + @dec.slow def test_all(self): assert self.module.bar11() == 11 assert self.module.foo_fixed.bar12() == 12 Modified: trunk/numpy/f2py/tests/test_return_character.py =================================================================== --- trunk/numpy/f2py/tests/test_return_character.py 2010-03-31 03:45:25 UTC (rev 8312) +++ trunk/numpy/f2py/tests/test_return_character.py 2010-03-31 03:45:36 UTC (rev 8313) @@ -72,6 +72,7 @@ end """ + @dec.slow def test_all(self): for name in "t0,t1,t5,s0,s1,s5,ss".split(","): self.check_function(getattr(self.module, name)) @@ -129,6 +130,7 @@ end module f90_return_char """ + @dec.slow def test_all(self): for name in "t0,t1,t5,ts,s0,s1,s5,ss".split(","): self.check_function(getattr(self.module.f90_return_char, name)) Modified: trunk/numpy/f2py/tests/test_return_complex.py =================================================================== --- trunk/numpy/f2py/tests/test_return_complex.py 2010-03-31 03:45:25 UTC (rev 8312) +++ trunk/numpy/f2py/tests/test_return_complex.py 2010-03-31 03:45:36 UTC (rev 8313) @@ -97,6 +97,7 @@ end """ + @dec.slow def test_all(self): for name in "t0,t8,t16,td,s0,s8,s16,sd".split(","): self.check_function(getattr(self.module, name)) @@ -155,6 +156,7 @@ end module f90_return_complex """ + @dec.slow def test_all(self): for name in "t0,t8,t16,td,s0,s8,s16,sd".split(","): self.check_function(getattr(self.module.f90_return_complex, name)) Modified: trunk/numpy/f2py/tests/test_return_integer.py =================================================================== --- trunk/numpy/f2py/tests/test_return_integer.py 2010-03-31 03:45:25 UTC (rev 8312) +++ trunk/numpy/f2py/tests/test_return_integer.py 2010-03-31 03:45:36 UTC (rev 8313) @@ -95,6 +95,7 @@ end """ + @dec.slow def test_all(self): for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): self.check_function(getattr(self.module, name)) @@ -164,6 +165,7 @@ end module f90_return_integer """ + @dec.slow def test_all(self): for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): self.check_function(getattr(self.module.f90_return_integer, name)) Modified: trunk/numpy/f2py/tests/test_return_logical.py =================================================================== --- trunk/numpy/f2py/tests/test_return_logical.py 2010-03-31 03:45:25 UTC (rev 8312) +++ trunk/numpy/f2py/tests/test_return_logical.py 2010-03-31 03:45:36 UTC (rev 8313) @@ -105,6 +105,7 @@ c end """ + @dec.slow def test_all(self): for name in "t0,t1,t2,t4,s0,s1,s2,s4".split(","): self.check_function(getattr(self.module, name)) @@ -173,6 +174,7 @@ end module f90_return_logical """ + @dec.slow def test_all(self): for name in "t0,t1,t2,t4,t8,s0,s1,s2,s4,s8".split(","): self.check_function(getattr(self.module.f90_return_logical, name)) Modified: trunk/numpy/f2py/tests/test_return_real.py =================================================================== --- trunk/numpy/f2py/tests/test_return_real.py 2010-03-31 03:45:25 UTC (rev 8312) +++ trunk/numpy/f2py/tests/test_return_real.py 2010-03-31 03:45:36 UTC (rev 8313) @@ -77,6 +77,7 @@ end python module c_ext_return_real """ + @dec.slow def test_all(self): for name in "t4,t8,s4,s8".split(","): self.check_function(getattr(self.module, name)) @@ -130,6 +131,7 @@ end """ + @dec.slow def test_all(self): for name in "t0,t4,t8,td,s0,s4,s8,sd".split(","): self.check_function(getattr(self.module, name)) @@ -187,6 +189,7 @@ end module f90_return_real """ + @dec.slow def test_all(self): for name in "t0,t4,t8,td,s0,s4,s8,sd".split(","): self.check_function(getattr(self.module.f90_return_real, name))