Here are the examples of the python api numpy.string_ taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
91 Examples
5
Example 1
Project: f2py Source File: test_py_support.py
def check_numpy_scalar_argument_return_string_1(self):
f = PyCFunction('foo')
f += Variable('a1', numpy.string_, 'in, out')
f += Variable('a2', numpy.string0, 'in, out')
foo = f.build()
args = ('hey', [1,2])
results = (numpy.string_('hey'), numpy.string_('[1, 2]'))
assert_equal(foo(*args), results)
f = PyCFunction('foo')
f += Variable('a1', 'npy_str', 'in, out')
f += Variable('a2', 'npy_string', 'in, out')
foo = f.build()
assert_equal(foo(*args), results)
3
Example 2
def to_hdf5(self, group):
"""Write distribution to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
"""
group.attrs['type'] = np.string_('maxwell')
group.attrs['u'] = self.u
self.theta.to_hdf5(group, 'theta')
3
Example 3
def test_lower(self):
tgt = asbytes_nested([[' abc ', ''],
['12345', 'mixedcase'],
['123 \t 345 \0 ', 'upper']])
assert_(issubclass(self.A.lower().dtype.type, np.string_))
assert_array_equal(self.A.lower(), tgt)
tgt = [[sixu(' \u03c3 '), sixu('')],
[sixu('12345'), sixu('mixedcase')],
[sixu('123 \t 345 \0 '), sixu('upper')]]
assert_(issubclass(self.B.lower().dtype.type, np.unicode_))
assert_array_equal(self.B.lower(), tgt)
3
Example 4
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
Function: test_swap_case
Function: test_swap_case
def test_swapcase(self):
tgt = asbytes_nested([[' ABC ', ''],
['12345', 'mIXEDcASE'],
['123 \t 345 \0 ', 'upper']])
assert_(issubclass(self.A.swapcase().dtype.type, np.string_))
assert_array_equal(self.A.swapcase(), tgt)
tgt = [[sixu(' \u03c3 '), sixu('')],
[sixu('12345'), sixu('mIXEDcASE')],
[sixu('123 \t 345 \0 '), sixu('upper')]]
assert_(issubclass(self.B.swapcase().dtype.type, np.unicode_))
assert_array_equal(self.B.swapcase(), tgt)
3
Example 5
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
def test_invalid_function_args(self):
def fail():
_vec_string(['a'], np.string_, 'strip', (1,))
self.assertRaises(TypeError, fail)
3
Example 6
def write(self, handle, name):
self._check_all_set()
g = handle.create_group(name)
g.attrs['type'] = np.string_('plane_parallel'.encode('utf-8'))
g.attrs['x'] = self.position[0]
g.attrs['y'] = self.position[1]
g.attrs['z'] = self.position[2]
g.attrs['r'] = self.radius
g.attrs['theta'] = self.direction[0]
g.attrs['phi'] = self.direction[1]
Source.write(self, g)
3
Example 7
def test_rjust(self):
assert_(issubclass(self.A.rjust(10).dtype.type, np.string_))
C = self.A.rjust([10, 20])
assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
C = self.A.rjust(20, asbytes('#'))
assert_(np.all(C.startswith(asbytes('#'))))
assert_array_equal(C.endswith(asbytes('#')),
[[False, True], [False, False], [False, False]])
C = np.char.rjust(asbytes('FOO'), [[10, 20], [15, 8]])
tgt = asbytes_nested([[' FOO', ' FOO'],
[' FOO', ' FOO']])
assert_(issubclass(C.dtype.type, np.string_))
assert_array_equal(C, tgt)
3
Example 8
def run(self):
while not self.stopped():
print('.')
try:
# publish data to tick
# function: .u.upd
# table: ask
self.q.sync('.u.upd', numpy.string_('ask'), self.get_ask_data())
time.sleep(1)
except QException as e:
print(e)
except:
self.stop()
3
Example 9
def write(self, handle, name):
self._check_all_set()
g = handle.create_group(name)
g.attrs['type'] = np.string_('point'.encode('utf-8'))
g.attrs['x'] = self.position[0]
g.attrs['y'] = self.position[1]
g.attrs['z'] = self.position[2]
Source.write(self, g)
3
Example 10
def to_hdf5(self, group):
"""Write distribution to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
"""
group.attrs['type'] = np.string_('watt')
group.attrs['u'] = self.u
self.a.to_hdf5(group, 'a')
self.b.to_hdf5(group, 'b')
3
Example 11
def to_hdf5(self, group):
"""Write distribution to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
"""
group.attrs['type'] = np.string_('discrete_photon')
group.attrs['primary_flag'] = self.primary_flag
group.attrs['energy'] = self.energy
group.attrs['atomic_weight_ratio'] = self.atomic_weight_ratio
3
Example 12
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
def test_non_string_array(self):
def fail():
_vec_string(1, np.string_, 'strip')
self.assertRaises(TypeError, fail)
3
Example 13
def write(self, handle, name):
self._check_all_set()
g = handle.create_group(name)
g.attrs['type'] = np.string_('extern_box'.encode('utf-8'))
g.attrs['xmin'] = self.bounds[0][0]
g.attrs['xmax'] = self.bounds[0][1]
g.attrs['ymin'] = self.bounds[1][0]
g.attrs['ymax'] = self.bounds[1][1]
g.attrs['zmin'] = self.bounds[2][0]
g.attrs['zmax'] = self.bounds[2][1]
Source.write(self, g)
3
Example 14
def test_center(self):
assert_(issubclass(self.A.center(10).dtype.type, np.string_))
C = self.A.center([10, 20])
assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
C = self.A.center(20, asbytes('#'))
assert_(np.all(C.startswith(asbytes('#'))))
assert_(np.all(C.endswith(asbytes('#'))))
C = np.char.center(asbytes('FOO'), [[10, 20], [15, 8]])
tgt = asbytes_nested([[' FOO ', ' FOO '],
[' FOO ', ' FOO ']])
assert_(issubclass(C.dtype.type, np.string_))
assert_array_equal(C, tgt)
3
Example 15
Project: sherpa Source File: instrument.py
def _set_origin(self, vals):
par = vals
if type(vals) in (str,numpy.string_):
raise PSFErr('notstr')
elif type(vals) not in (list, tuple, numpy.ndarray):
par = [vals]
self._origin = tuple(par)
if par is None:
self._origin = None
3
Example 16
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
Function: test_partition
Function: test_partition
def test_partition(self):
P = self.A.partition(asbytes_nested(['3', 'M']))
tgt = asbytes_nested([[(' abc ', '', ''), ('', '', '')],
[('12', '3', '45'), ('', 'M', 'ixedCase')],
[('12', '3', ' \t 345 \0 '), ('UPPER', '', '')]])
assert_(issubclass(P.dtype.type, np.string_))
assert_array_equal(P, tgt)
3
Example 17
Project: hyperspy Source File: test_model_as_dictionary.py
def remove_empty_numpy_strings(dic):
for k, v in dic.items():
if isinstance(v, dict):
remove_empty_numpy_strings(v)
elif isinstance(v, list):
for vv in v:
if isinstance(vv, dict):
remove_empty_numpy_strings(vv)
elif isinstance(vv, np.string_) and len(vv) == 0:
vv = ''
elif isinstance(v, np.string_) and len(v) == 0:
del dic[k]
dic[k] = ''
3
Example 18
def test_rstrip(self):
assert_(issubclass(self.A.rstrip().dtype.type, np.string_))
tgt = asbytes_nested([[' abc', ''],
['12345', 'MixedCase'],
['123 \t 345', 'UPPER']])
assert_array_equal(self.A.rstrip(), tgt)
tgt = asbytes_nested([[' abc ', ''],
['1234', 'MixedCase'],
['123 \t 345 \x00', 'UPP']
])
assert_array_equal(self.A.rstrip(asbytes_nested(['5', 'ER'])), tgt)
tgt = [[sixu(' \u03a3'), ''],
['12345', 'MixedCase'],
['123 \t 345', 'UPPER']]
assert_(issubclass(self.B.rstrip().dtype.type, np.unicode_))
assert_array_equal(self.B.rstrip(), tgt)
3
Example 19
Project: pycollada Source File: test_source.py
def test_name_source_saving(self):
namesource = collada.source.NameSource("mynamesource",
numpy.array(['Name1', 'Name2'], dtype=numpy.string_),
('JOINT',))
self.assertEqual(namesource.id, "mynamesource")
self.assertEqual(len(namesource), 2)
self.assertTupleEqual(namesource.components, ('JOINT',))
self.assertIsNotNone(str(namesource))
namesource.id = "yournamesource"
namesource.components = ('WEIGHT', 'WHATEVER')
namesource.data = numpy.array(['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6'], dtype=numpy.string_)
namesource.save()
loaded_namesource = collada.source.Source.load(self.dummy, {}, fromstring(tostring(namesource.xmlnode)))
self.assertTrue(isinstance(loaded_namesource, collada.source.NameSource))
self.assertEqual(loaded_namesource.id, "yournamesource")
self.assertEqual(len(loaded_namesource), 3)
self.assertTupleEqual(loaded_namesource.components, ('WEIGHT', 'WHATEVER'))
3
Example 20
def test_upper(self):
tgt = asbytes_nested([[' ABC ', ''],
['12345', 'MIXEDCASE'],
['123 \t 345 \0 ', 'UPPER']])
assert_(issubclass(self.A.upper().dtype.type, np.string_))
assert_array_equal(self.A.upper(), tgt)
tgt = [[sixu(' \u03a3 '), sixu('')],
[sixu('12345'), sixu('MIXEDCASE')],
[sixu('123 \t 345 \0 '), sixu('UPPER')]]
assert_(issubclass(self.B.upper().dtype.type, np.unicode_))
assert_array_equal(self.B.upper(), tgt)
3
Example 21
def write(self, handle, name):
self._check_all_set()
g = handle.create_group(name)
g.attrs['type'] = np.string_('spot'.encode('utf-8'))
g.attrs['longitude'] = self.longitude
g.attrs['latitude'] = self.latitude
g.attrs['radius'] = self.radius
Source.write(self, g)
3
Example 22
def to_hdf5(self, group):
"""Write distribution to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
"""
group.attrs['type'] = np.string_('madland-nix')
group.attrs['efl'] = self.efl
group.attrs['efh'] = self.efh
self.tm.to_hdf5(group)
3
Example 23
Project: xarray Source File: test_variable.py
def test_index_0d_numpy_string(self):
# regression test to verify our work around for indexing 0d strings
v = Variable([], np.string_('asdf'))
self.assertVariableIdentical(v[()], v)
v = Variable([], np.unicode_(u'asdf'))
self.assertVariableIdentical(v[()], v)
3
Example 24
def write(self, handle, name):
self._check_all_set()
g = handle.create_group(name)
g.attrs['type'] = np.string_('point_collection'.encode('utf-8'))
g.create_dataset('position', data=self.position, compression=True)
Source.write(self, g)
3
Example 25
def to_hdf5(self, group):
"""Write distribution to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
"""
group.attrs['type'] = np.string_('level')
group.attrs['threshold'] = self.threshold
group.attrs['mass_ratio'] = self.mass_ratio
3
Example 26
def to_hdf5(self, group, name='xy'):
"""Write polynomial function to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
name : str
Name of the dataset to create
"""
dataset = group.create_dataset(name, data=self.coef)
dataset.attrs['type'] = np.string_(type(self).__name__)
3
Example 27
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
Function: test_non_existent_method
Function: test_non_existent_method
def test_non_existent_method(self):
def fail():
_vec_string('a', np.string_, 'bogus')
self.assertRaises(AttributeError, fail)
3
Example 28
def write(self, handle, name):
self._check_all_set()
g = handle.create_group(name)
g.attrs['type'] = np.string_('extern_sph'.encode('utf-8'))
g.attrs['x'] = self.position[0]
g.attrs['y'] = self.position[1]
g.attrs['z'] = self.position[2]
g.attrs['r'] = self.radius
Source.write(self, g)
3
Example 29
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
def test_invalid_args_tuple(self):
def fail():
_vec_string(['a'], np.string_, 'strip', 1)
self.assertRaises(TypeError, fail)
3
Example 30
Project: sherpa Source File: instrument.py
def _set_size(self, vals):
par = vals
if type(vals) in (str,numpy.string_):
raise PSFErr('notstr')
elif type(vals) not in (list, tuple, numpy.ndarray):
par = [vals]
self._size = tuple(par)
if par is None:
self._size = None
3
Example 31
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
Function: test_capitalize
Function: test_capitalize
def test_capitalize(self):
tgt = asbytes_nested([[' abc ', ''],
['12345', 'Mixedcase'],
['123 \t 345 \0 ', 'Upper']])
assert_(issubclass(self.A.capitalize().dtype.type, np.string_))
assert_array_equal(self.A.capitalize(), tgt)
tgt = [[sixu(' \u03c3 '), ''],
['12345', 'Mixedcase'],
['123 \t 345 \0 ', 'Upper']]
assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_))
assert_array_equal(self.B.capitalize(), tgt)
3
Example 32
def write(self, handle, name, grid, compression=True, map_dtype=float):
self._check_all_set()
g = handle.create_group(name)
g.attrs['type'] = np.string_('map'.encode('utf-8'))
grid.write_single_array(g, "Luminosity map", self.map,
compression=compression,
physics_dtype=map_dtype)
Source.write(self, g)
3
Example 33
def test_ljust(self):
assert_(issubclass(self.A.ljust(10).dtype.type, np.string_))
C = self.A.ljust([10, 20])
assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
C = self.A.ljust(20, asbytes('#'))
assert_array_equal(C.startswith(asbytes('#')), [
[False, True], [False, False], [False, False]])
assert_(np.all(C.endswith(asbytes('#'))))
C = np.char.ljust(asbytes('FOO'), [[10, 20], [15, 8]])
tgt = asbytes_nested([['FOO ', 'FOO '],
['FOO ', 'FOO ']])
assert_(issubclass(C.dtype.type, np.string_))
assert_array_equal(C, tgt)
3
Example 34
Project: pycollada Source File: test_source.py
def test_idref_source_saving(self):
idrefsource = collada.source.IDRefSource("myidrefsource",
numpy.array(['Ref1', 'Ref2'], dtype=numpy.string_),
('MORPH_TARGET',))
self.assertEqual(idrefsource.id, "myidrefsource")
self.assertEqual(len(idrefsource), 2)
self.assertTupleEqual(idrefsource.components, ('MORPH_TARGET',))
self.assertIsNotNone(str(idrefsource))
idrefsource.id = "youridrefsource"
idrefsource.components = ('JOINT_TARGET', 'WHATEVER_TARGET')
idrefsource.data = numpy.array(['Ref5', 'Ref6', 'Ref7', 'Ref8', 'Ref9', 'Ref10'], dtype=numpy.string_)
idrefsource.save()
loaded_idrefsource = collada.source.Source.load(self.dummy, {}, fromstring(tostring(idrefsource.xmlnode)))
self.assertTrue(isinstance(loaded_idrefsource, collada.source.IDRefSource))
self.assertEqual(loaded_idrefsource.id, "youridrefsource")
self.assertEqual(len(loaded_idrefsource), 3)
self.assertTupleEqual(loaded_idrefsource.components, ('JOINT_TARGET', 'WHATEVER_TARGET'))
3
Example 35
def test_lstrip(self):
tgt = asbytes_nested([['abc ', ''],
['12345', 'MixedCase'],
['123 \t 345 \0 ', 'UPPER']])
assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
assert_array_equal(self.A.lstrip(), tgt)
tgt = asbytes_nested([[' abc', ''],
['2345', 'ixedCase'],
['23 \t 345 \x00', 'UPPER']])
assert_array_equal(self.A.lstrip(asbytes_nested(['1', 'M'])), tgt)
tgt = [[sixu('\u03a3 '), ''],
['12345', 'MixedCase'],
['123 \t 345 \0 ', 'UPPER']]
assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
assert_array_equal(self.B.lstrip(), tgt)
3
Example 36
def ensure_unicode(stuff, encoding='utf8', encoding2='latin-1'):
if not isinstance(stuff, (bytes, np.string_)):
return stuff
else:
string = stuff
try:
string = string.decode(encoding)
except:
string = string.decode(encoding2, errors='ignore')
return string
3
Example 37
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
Function: test_replace
Function: test_replace
def test_replace(self):
R = self.A.replace(asbytes_nested(['3', 'a']),
asbytes_nested(['##########', '@']))
tgt = asbytes_nested([[' abc ', ''],
['12##########45', 'MixedC@se'],
['12########## \t ##########45 \x00', 'UPPER']])
assert_(issubclass(R.dtype.type, np.string_))
assert_array_equal(R, tgt)
if sys.version_info[0] < 3:
# NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
R = self.A.replace(asbytes('a'), sixu('\u03a3'))
tgt = [[sixu(' \u03a3bc '), ''],
['12345', sixu('MixedC\u03a3se')],
['123 \t 345 \x00', 'UPPER']]
assert_(issubclass(R.dtype.type, np.unicode_))
assert_array_equal(R, tgt)
3
Example 38
Project: deep_recommend_system Source File: tensor_util.py
def GetNumpyAppendFn(dtype):
# numpy dtype for strings are variable length. We can not compare
# dtype with a single constant (np.string does not exist) to decide
# dtype is a "string" type. We need to compare the dtype.type to be
# sure it's a string type.
if dtype.type == np.string_ or dtype.type == np.unicode_:
if _FAST_TENSOR_UTIL_AVAILABLE:
return fast_tensor_util.AppendObjectArrayToTensorProto
else:
return SlowAppendObjectArrayToTensorProto
return GetFromNumpyDTypeDict(_NP_TO_APPEND_FN, dtype)
3
Example 39
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_defchararray.py
Function: test_rpartition
Function: test_rpartition
def test_rpartition(self):
P = self.A.rpartition(asbytes_nested(['3', 'M']))
tgt = asbytes_nested([[('', '', ' abc '), ('', '', '')],
[('12', '3', '45'), ('', 'M', 'ixedCase')],
[('123 \t ', '3', '45 \0 '), ('', '', 'UPPER')]])
assert_(issubclass(P.dtype.type, np.string_))
assert_array_equal(P, tgt)
3
Example 40
Project: sima Source File: test_imaging.py
def test_export_averages_hdf5(self):
time_avg_path = os.path.join(self.filepath, 'time_avg.h5')
self.ds.export_averages(time_avg_path, fmt='HDF5', scale_values=False)
h5_time_avg = h5py.File(time_avg_path, 'r')['time_average']
assert_equal(self.ds.time_averages.astype('uint16'), h5_time_avg)
assert_equal(np.string_(self.ds.channel_names),
np.string_(h5_time_avg.attrs['channel_names']))
dim_labels = [dim.label for dim in h5_time_avg.dims]
assert_equal(['z', 'y', 'x', 'c'], dim_labels)
3
Example 41
def test_strip(self):
tgt = asbytes_nested([['abc', ''],
['12345', 'MixedCase'],
['123 \t 345', 'UPPER']])
assert_(issubclass(self.A.strip().dtype.type, np.string_))
assert_array_equal(self.A.strip(), tgt)
tgt = asbytes_nested([[' abc ', ''],
['234', 'ixedCas'],
['23 \t 345 \x00', 'UPP']])
assert_array_equal(self.A.strip(asbytes_nested(['15', 'EReM'])), tgt)
tgt = [[sixu('\u03a3'), ''],
['12345', 'MixedCase'],
['123 \t 345', 'UPPER']]
assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
assert_array_equal(self.B.strip(), tgt)
3
Example 42
def __setitem__(self, key, value):
if type(key) == int:
if key > len(self.keys) - 1:
raise Exception("Element %i does not exist" % key)
else:
self.values[key] = value
elif type(key) in [str, np.string_, unicode]:
if key in self.keys:
index = self.keys.index(key)
self.values[index] = value
else:
self.keys.append(key)
self.values.append(value)
else:
raise Exception("Wrong type for key: %s" % type(key))
3
Example 43
def test_title(self):
tgt = asbytes_nested([[' Abc ', ''],
['12345', 'Mixedcase'],
['123 \t 345 \0 ', 'Upper']])
assert_(issubclass(self.A.title().dtype.type, np.string_))
assert_array_equal(self.A.title(), tgt)
tgt = [[sixu(' \u03a3 '), sixu('')],
[sixu('12345'), sixu('Mixedcase')],
[sixu('123 \t 345 \0 '), sixu('Upper')]]
assert_(issubclass(self.B.title().dtype.type, np.unicode_))
assert_array_equal(self.B.title(), tgt)
3
Example 44
def to_hdf5(self, group):
"""Write distribution to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
"""
group.attrs['type'] = np.string_('evaporation')
group.attrs['u'] = self.u
self.theta.to_hdf5(group, 'theta')
3
Example 45
def to_hdf5(self, group):
"""Write distribution to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
"""
group.attrs['type'] = np.string_('nbody')
group.attrs['total_mass'] = self.total_mass
group.attrs['n_particles'] = self.n_particles
group.attrs['atomic_weight_ratio'] = self.atomic_weight_ratio
group.attrs['q_value'] = self.q_value
3
Example 46
def __getitem__(self, key):
if type(key) == int:
return self.values[key]
elif type(key) in [str, np.string_]:
index = self.keys.index(key)
return self.values[index]
else:
raise Exception("Wrong type for key: %s" % type(key))
3
Example 47
Project: sherpa Source File: instrument.py
def _set_center(self, vals):
par = vals
if type(vals) in (str,numpy.string_):
raise PSFErr('nostr')
elif type(vals) not in (list, tuple, numpy.ndarray):
par = [vals]
self._center = tuple(par)
if par is None:
self._center = None
3
Example 48
def write(self, handle, name):
self._check_all_set()
g = handle.create_group(name)
g.attrs['type'] = np.string_('sphere'.encode('utf-8'))
g.attrs['x'] = self.position[0]
g.attrs['y'] = self.position[1]
g.attrs['z'] = self.position[2]
g.attrs['r'] = self.radius
g.attrs['limb'] = np.string_(bool2str(self.limb))
Source.write(self, g)
for i, spot in enumerate(self._spots):
spot.write(g, 'Spot %i' % i)
3
Example 49
def to_hdf5(self, group):
"""Write distribution to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
"""
group.attrs['type'] = np.string_('uncorrelated')
if self.angle is not None:
angle_group = group.create_group('angle')
self.angle.to_hdf5(angle_group)
if self.energy is not None:
energy_group = group.create_group('energy')
self.energy.to_hdf5(energy_group)
0
Example 50
@auto_download_to_file
@auto_fileobj_to_file
def read(self, filename, hdu=None, memmap=False, verbose=True):
'''
Read a table from a FITS file
Required Arguments:
*filename*: [ string ]
The FITS file to read the table from
Optional Keyword Arguments:
*hdu*: [ integer ]
The HDU to read from the FITS file (this is only required
if there are more than one table in the FITS file)
*memmap*: [ bool ]
Whether PyFITS should use memory mapping
'''
self.reset()
# If no hdu is requested, check that there is only one table
if not hdu:
tables = _list_tables(filename)
if len(tables) == 0:
raise Exception("No tables in file")
elif len(tables) == 1:
hdu = tables.keys()[0]
else:
raise TableException(tables, 'hdu')
hdulist = fits.open(filename, memmap=memmap)
hdu = hdulist[hdu]
table = hdu.data
header = hdu.header
columns = hdu.columns
# Construct dtype for table
dtype = []
for i in range(len(hdu.data.dtype)):
name = hdu.data.dtype.names[i]
type = hdu.data.dtype[name]
if type.subdtype:
type, shape = type.subdtype
else:
shape = ()
# Get actual FITS format and zero-point
format, bzero = hdu.columns[i].format, hdu.columns[i].bzero
# Remove numbers from format, to find just type
format = format.strip("1234567890.")
if type.type is np.string_ and format in ['I', 'F', 'E', 'D']:
if format == 'I':
type = np.int64
elif format in ['F', 'E']:
type = np.float32
elif format == 'D':
type = np.float64
if format == 'X' and type.type == np.uint8:
type = np.bool
if len(shape) == 1:
shape = (shape[0] * 8,)
if format == 'L':
type = np.bool
if bzero and format in ['B', 'I', 'J']:
if format == 'B' and bzero == -128:
dtype.append((name, np.int8, shape))
elif format == 'I' and bzero == - np.iinfo(np.int16).min:
dtype.append((name, np.uint16, shape))
elif format == 'J' and bzero == - np.iinfo(np.int32).min:
dtype.append((name, np.uint32, shape))
else:
dtype.append((name, type, shape))
else:
dtype.append((name, type, shape))
dtype = np.dtype(dtype)
if self._masked:
self._setup_table(len(hdu.data), dtype, units=columns.units)
else:
self._setup_table(len(hdu.data), dtype, units=columns.units, \
nulls=columns.nulls)
# Populate the table
for i, name in enumerate(columns.names):
format, bzero = hdu.columns[i].format[-1], hdu.columns[i].bzero
if bzero and format in ['B', 'I', 'J']:
data = np.rec.recarray.field(hdu.data, i)
if format == 'B' and bzero == -128:
data = (data.astype(np.int16) + bzero).astype(np.int8)
elif format == 'I' and bzero == - np.iinfo(np.int16).min:
data = (data.astype(np.int32) + bzero).astype(np.uint16)
elif format == 'J' and bzero == - np.iinfo(np.int32).min:
data = (data.astype(np.int64) + bzero).astype(np.uint32)
else:
data = table.field(name)
else:
data = table.field(name)
self.data[name][:] = data[:]
if self._masked:
if columns.nulls[i] == 'NAN.0':
null = np.nan
elif columns.nulls[i] == 'INF.0':
null = np.inf
else:
null = columns.nulls[i]
self.data[name].mask = smart_mask(data, null)
self.data[name].set_fill_value(null)
for key in header.keys():
if not key[:4] in ['TFOR', 'TDIS', 'TDIM', 'TTYP', 'TUNI'] and \
not key in standard_keys:
self.add_keyword(key, header[key])
try:
header['COMMENT']
except KeyError:
pass
else:
# PyFITS used to define header['COMMENT'] as the last comment read in
# (which was a string), but now defines it as a _HeaderCommentaryCards
# object
if isinstance(header['COMMENT'], basestring):
for comment in header.get_comment():
if isinstance(comment, fits.Card):
self.add_comment(comment.value)
else:
self.add_comment(comment)
else:
for comment in header['COMMENT']:
if isinstance(comment, fits.Card):
self.add_comment(comment.value)
else:
self.add_comment(comment)
if hdu.name:
self.table_name = str(hdu.name)
hdulist.close()
return