/usr/share/pyshared/pyfits/tests/testStructured.py is in python-pyfits 1:2.4.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | from __future__ import division # confidence high
import numpy
import pyfits
import os
from sys import stdout
import os.path
test_dir = os.path.dirname(__file__) + "/"
def compare_arrays(arr1in, arr2in, verbose=False):
"""
Compare the values field-by-field in two sets of numpy arrays or
recarrays.
"""
arr1 = arr1in.view(numpy.ndarray)
arr2 = arr2in.view(numpy.ndarray)
nfail = 0
for n2 in arr2.dtype.names:
n1 = n2
if n1 not in arr1.dtype.names:
n1 = n1.lower()
if n1 not in arr1.dtype.names:
n1 = n1.upper()
if n1 not in arr1.dtype.names:
raise ValueError('field name %s not found in array 1' % n2)
if verbose:
stdout.write(" testing field: '%s'\n" % n2)
stdout.write(' shape...........')
if arr2[n2].shape != arr1[n1].shape:
nfail += 1
if verbose:
stdout.write('shapes differ\n')
else:
if verbose:
stdout.write('OK\n')
stdout.write(' elements........')
w,=numpy.where(arr1[n1].ravel() != arr2[n2].ravel())
if w.size > 0:
nfail += 1
if verbose:
stdout.write('\n '+\
'%s elements in field %s differ\n' % (w.size,n2))
else:
if verbose:
stdout.write('OK\n')
if nfail == 0:
if verbose:
stdout.write('All tests passed\n')
return True
else:
if verbose:
stdout.write('%d differences found\n' % nfail)
return False
def get_test_data(verbose=False):
st = numpy.zeros(3, [('f1','i4'),('f2','S6'),('f3','>2f8')])
numpy.random.seed(35)
st['f1'] = [1,3,5]
st['f2'] = ['hello','world','byebye']
st['f3'] = numpy.random.random(st['f3'].shape)
print st.dtype.descr
print st
return st
def test(verbose=False):
fname=test_dir+'stddata.fits'
print 'Reading from ',fname
data1,h1 = pyfits.getdata(fname, ext=1, header=True)
data2,h2 = pyfits.getdata(fname, ext=2, header=True)
st = get_test_data()
outfile = 'test.fits'
print 'Writing to file data1:',outfile
pyfits.writeto(outfile, data1, clobber=True)
print 'Appending to file: data2',outfile
pyfits.append(outfile, data2)
print 'Appending to file: st',outfile
pyfits.append(outfile, st)
print st.dtype.descr
print st
assert st.dtype.isnative
assert numpy.all(st['f1'] == [1,3,5])
print 'Reading data back'
data1check, h1check = pyfits.getdata(outfile, ext=1, header=True)
data2check, h2check = pyfits.getdata(outfile, ext=2, header=True)
stcheck, sthcheck = pyfits.getdata(outfile, ext=3, header=True)
if verbose:
print acheck
print stcheck
if not compare_arrays(data1, data1check, verbose=True):
raise ValueError,'Fail'
if not compare_arrays(data2, data2check, verbose=True):
raise ValueError,'Fail'
print st, stcheck
if not compare_arrays(st, stcheck, verbose=True):
raise ValueError,'Fail'
# try reading with view
print 'Reading with ndarray view'
dataviewcheck, hviewcheck = pyfits.getdata(outfile, ext=2, header=True,
view=numpy.ndarray)
if not compare_arrays(data2, dataviewcheck, verbose=True):
raise ValueError,'Fail'
os.remove(outfile)
if __name__=='__main__':
test()
|