/usr/lib/python3/dist-packages/pyfits/tests/test_groups.py is in python3-pyfits 1:3.4-1.
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | from __future__ import with_statement
import os
import time
import numpy as np
from nose.tools import assert_raises
import pyfits as fits
from . import PyfitsTestCase
from .test_table import comparerecords
class TestGroupsFunctions(PyfitsTestCase):
def test_open(self):
with fits.open(self.data('random_groups.fits')) as hdul:
assert isinstance(hdul[0], fits.GroupsHDU)
naxes = (3, 1, 128, 1, 1)
parameters = ['UU', 'VV', 'WW', 'BASELINE', 'DATE']
assert (hdul.info(output=False) ==
[(0, 'PRIMARY', 'GroupsHDU', 147, naxes, 'float32',
'3 Groups 5 Parameters')])
ghdu = hdul[0]
assert ghdu.parnames == parameters
assert list(ghdu.data.dtype.names) == parameters + ['DATA']
assert isinstance(ghdu.data, fits.GroupData)
# The data should be equal to the number of groups
assert ghdu.header['GCOUNT'] == len(ghdu.data)
assert ghdu.data.data.shape == (len(ghdu.data),) + naxes[::-1]
assert ghdu.data.parnames == parameters
assert isinstance(ghdu.data[0], fits.Group)
assert len(ghdu.data[0]) == len(parameters) + 1
assert ghdu.data[0].data.shape == naxes[::-1]
assert ghdu.data[0].parnames == parameters
def test_open_groups_in_update_mode(self):
"""
Test that opening a file containing a groups HDU in update mode and
then immediately closing it does not result in any unnecessary file
modifications.
Similar to
test_image.TestImageFunctions.test_open_scaled_in_update_mode().
"""
# Copy the original file before making any possible changes to it
self.copy_file('random_groups.fits')
mtime = os.stat(self.temp('random_groups.fits')).st_mtime
time.sleep(1)
fits.open(self.temp('random_groups.fits'), mode='update',
memmap=False).close()
# Ensure that no changes were made to the file merely by immediately
# opening and closing it.
assert mtime == os.stat(self.temp('random_groups.fits')).st_mtime
def test_random_groups_data_update(self):
"""
Regression test for https://github.com/astropy/astropy/issues/3730 and
for https://github.com/spacetelescope/PyFITS/issues/102
"""
self.copy_file('random_groups.fits')
with fits.open(self.temp('random_groups.fits'), mode='update') as h:
h[0].data['UU'] = 0.42
with fits.open(self.temp('random_groups.fits'), mode='update') as h:
assert np.all(h[0].data['UU'] == 0.42)
def test_parnames_round_trip(self):
"""
Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/130
Ensures that opening a random groups file in update mode or writing it
to a new file does not cause any change to the parameter names.
"""
# Because this test tries to update the random_groups.fits file, let's
# make a copy of it first (so that the file doesn't actually get
# modified in the off chance that the test fails
self.copy_file('random_groups.fits')
parameters = ['UU', 'VV', 'WW', 'BASELINE', 'DATE']
with fits.open(self.temp('random_groups.fits'), mode='update') as h:
assert h[0].parnames == parameters
h.flush()
# Open again just in read-only mode to ensure the parnames didn't
# change
with fits.open(self.temp('random_groups.fits')) as h:
assert h[0].parnames == parameters
h.writeto(self.temp('test.fits'))
with fits.open(self.temp('test.fits')) as h:
assert h[0].parnames == parameters
def test_groupdata_slice(self):
"""
A simple test to ensure that slicing GroupData returns a new, smaller
GroupData object, as is the case with a normal FITS_rec. This is a
regression test for an as-of-yet unreported issue where slicing
GroupData returned a single Group record.
"""
with fits.open(self.data('random_groups.fits')) as hdul:
s = hdul[0].data[1:]
assert isinstance(s, fits.GroupData)
assert len(s) == 2
assert hdul[0].data.parnames == s.parnames
def test_group_slice(self):
"""
Tests basic slicing a single group record.
"""
# A very basic slice test
with fits.open(self.data('random_groups.fits')) as hdul:
g = hdul[0].data[0]
s = g[2:4]
assert len(s) == 2
assert s[0] == g[2]
assert s[-1] == g[-3]
s = g[::-1]
assert len(s) == 6
assert (s[0] == g[-1]).all()
assert s[-1] == g[0]
s = g[::2]
assert len(s) == 3
assert s[0] == g[0]
assert s[1] == g[2]
assert s[2] == g[4]
def test_create_groupdata(self):
"""
Basic test for creating GroupData from scratch.
"""
imdata = np.arange(100.0)
imdata.shape = (10, 1, 1, 2, 5)
pdata1 = np.arange(10, dtype=np.float32) + 0.1
pdata2 = 42.0
x = fits.hdu.groups.GroupData(imdata, parnames=['abc', 'xyz'],
pardata=[pdata1, pdata2], bitpix=-32)
assert x.parnames == ['abc', 'xyz']
assert (x.par('abc') == pdata1).all()
assert (x.par('xyz') == ([pdata2] * len(x))).all()
assert (x.data == imdata).all()
# Test putting the data into a GroupsHDU and round-tripping it
ghdu = fits.GroupsHDU(data=x)
ghdu.writeto(self.temp('test.fits'))
with fits.open(self.temp('test.fits')) as h:
hdr = h[0].header
assert hdr['GCOUNT'] == 10
assert hdr['PCOUNT'] == 2
assert hdr['NAXIS'] == 5
assert hdr['NAXIS1'] == 0
assert hdr['NAXIS2'] == 5
assert hdr['NAXIS3'] == 2
assert hdr['NAXIS4'] == 1
assert hdr['NAXIS5'] == 1
assert h[0].data.parnames == ['abc', 'xyz']
assert comparerecords(h[0].data, x)
def test_duplicate_parameter(self):
"""
Tests support for multiple parameters of the same name, and ensures
that the data in duplicate parameters are returned as a single summed
value.
"""
imdata = np.arange(100.0)
imdata.shape = (10, 1, 1, 2, 5)
pdata1 = np.arange(10, dtype=np.float32) + 1
pdata2 = 42.0
x = fits.hdu.groups.GroupData(imdata, parnames=['abc', 'xyz', 'abc'],
pardata=[pdata1, pdata2, pdata1],
bitpix=-32)
assert x.parnames == ['abc', 'xyz', 'abc']
assert (x.par('abc') == pdata1 * 2).all()
assert x[0].par('abc') == 2
# Test setting a parameter
x[0].setpar(0, 2)
assert x[0].par('abc') == 3
assert_raises(ValueError, x[0].setpar, 'abc', 2)
x[0].setpar('abc', (2, 3))
assert x[0].par('abc') == 5
assert x.par('abc')[0] == 5
assert (x.par('abc')[1:] == pdata1[1:] * 2).all()
# Test round-trip
ghdu = fits.GroupsHDU(data=x)
ghdu.writeto(self.temp('test.fits'))
with fits.open(self.temp('test.fits')) as h:
hdr = h[0].header
assert hdr['PCOUNT'] == 3
assert hdr['PTYPE1'] == 'abc'
assert hdr['PTYPE2'] == 'xyz'
assert hdr['PTYPE3'] == 'abc'
assert x.parnames == ['abc', 'xyz', 'abc']
assert x.dtype.names == ('abc', 'xyz', '_abc', 'DATA')
assert x.par('abc')[0] == 5
assert (x.par('abc')[1:] == pdata1[1:] * 2).all()
|