Python astropy.table.Table.read() Examples
The following are 30
code examples of astropy.table.Table.read().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
astropy.table.Table
, or try the search function
.
Example #1
Source File: test_pandas.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_read_fixed_width_format(): """Test reading with pandas read_fwf() """ tbl = """\ a b c 1 2.0 a 2 3.0 b""" buf = StringIO() buf.write(tbl) # Explicitly provide converters to avoid casting 'a' to int32. # See https://github.com/astropy/astropy/issues/8682 t = Table.read(tbl, format='ascii', guess=False, converters={'a': [ascii.convert_numpy(np.int64)]}) buf.seek(0) t2 = Table.read(buf, format='pandas.fwf') assert t.colnames == t2.colnames assert np.all(t == t2)
Example #2
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_metadata_very_large(tmpdir): """Test that very large datasets work, now!""" test_file = str(tmpdir.join('test.hdf5')) t1 = Table() t1['a'] = Column(data=[1, 2, 3], unit="s") t1['a'].meta['a0'] = "A0" t1['a'].meta['a1'] = {"a1": [0, 1]} t1['a'].format = '7.3f' t1['a'].description = 'A column' t1.meta['b'] = 1 t1.meta['c'] = {"c0": [0, 1]} t1.meta["meta_big"] = "0" * (2 ** 16 + 1) t1.meta["meta_biggerstill"] = "0" * (2 ** 18) t1.write(test_file, path='the_table', serialize_meta=True, overwrite=True) t2 = Table.read(test_file, path='the_table') assert t1['a'].unit == t2['a'].unit assert t1['a'].format == t2['a'].format assert t1['a'].description == t2['a'].description assert t1['a'].meta == t2['a'].meta assert t1.meta == t2.meta
Example #3
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_read_h5py_objects(tmpdir): # Regression test - ensure that Datasets are recognized automatically test_file = str(tmpdir.join('test.hdf5')) import h5py with h5py.File(test_file, 'w') as output_file: t1 = Table() t1.add_column(Column(name='a', data=[1, 2, 3])) t1.write(output_file, path='the_table') f = h5py.File(test_file, mode='r') t2 = Table.read(f, path='the_table') assert np.all(t2['a'] == [1, 2, 3]) t3 = Table.read(f['/'], path='the_table') assert np.all(t3['a'] == [1, 2, 3]) t4 = Table.read(f['the_table']) assert np.all(t4['a'] == [1, 2, 3]) f.close() # don't raise an error in 'test --open-files'
Example #4
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_preserve_serialized_old_meta_format(tmpdir): """Test the old meta format Only for some files created prior to v4.0, in compatibility mode. """ test_file = get_pkg_data_filename('data/old_meta_example.hdf5') t1 = Table() t1['a'] = Column(data=[1, 2, 3], unit="s") t1['a'].meta['a0'] = "A0" t1['a'].meta['a1'] = {"a1": [0, 1]} t1['a'].format = '7.3f' t1['a'].description = 'A column' t1.meta['b'] = 1 t1.meta['c'] = {"c0": [0, 1]} t2 = Table.read(test_file, path='the_table') assert t1['a'].unit == t2['a'].unit assert t1['a'].format == t2['a'].format assert t1['a'].description == t2['a'].description assert t1['a'].meta == t2['a'].meta assert t1.meta == t2.meta
Example #5
Source File: averages.py From dust_extinction with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, **kwargs): # get the tabulated information data_path = pkg_resources.resource_filename("dust_extinction", "data/") a = Table.read( data_path + "fritz11_galcenter.dat", format="ascii.commented_header" ) self.obsdata_x = 1.0 / a["wave"].data # ext is total extinction to GalCenter # A(K) = 2.42 # A(K)/A(V) = 0.112 (F19, R(V) = 3.1) self.obsdata_axav = 0.112 * a["ext"].data / 2.42 self.obsdata_axav_unc = 0.112 * a["unc"].data / 2.42 # accuracy of the observed data based on published table self.obsdata_tolerance = 1e-6 super().__init__(**kwargs)
Example #6
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_preserve_serialized(tmpdir): test_file = str(tmpdir.join('test.hdf5')) t1 = Table() t1['a'] = Column(data=[1, 2, 3], unit="s") t1['a'].meta['a0'] = "A0" t1['a'].meta['a1'] = {"a1": [0, 1]} t1['a'].format = '7.3f' t1['a'].description = 'A column' t1.meta['b'] = 1 t1.meta['c'] = {"c0": [0, 1]} t1.write(test_file, path='the_table', serialize_meta=True, overwrite=True) t2 = Table.read(test_file, path='the_table') assert t1['a'].unit == t2['a'].unit assert t1['a'].format == t2['a'].format assert t1['a'].description == t2['a'].description assert t1['a'].meta == t2['a'].meta assert t1.meta == t2.meta
Example #7
Source File: averages.py From dust_extinction with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, **kwargs): # get the tabulated information data_path = pkg_resources.resource_filename("dust_extinction", "data/") a = Table.read( data_path + "CT06_pixiedust.dat", format="ascii.commented_header" ) self.obsdata_x = 1.0 / a["wave"].data # ext is A(lambda)/A(K) # A(K)/A(V) = 0.112 (F19, R(V) = 3.1) self.obsdata_axav = 0.112 * a["local"].data # accuracy of the observed data based on published table self.obsdata_tolerance = 1e-6 super().__init__(**kwargs)
Example #8
Source File: averages.py From dust_extinction with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, **kwargs): # get the tabulated information data_path = pkg_resources.resource_filename("dust_extinction", "data/") a = Table.read( data_path + "CT06_pixiedust.dat", format="ascii.commented_header" ) self.obsdata_x = 1.0 / a["wave"].data # ext is A(lambda)/A(K) # A(K)/A(V) = 0.112 (F19, R(V) = 3.1) self.obsdata_axav = 0.112 * a["galcen"].data # accuracy of the observed data based on published table self.obsdata_tolerance = 1e-6 super().__init__(**kwargs)
Example #9
Source File: test_io.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_table_io(tmpdir): tmpfile = str(tmpdir.join('table.asdf')) table = make_table() table.write(tmpfile) # Simple sanity check using ASDF directly with asdf.open(tmpfile) as af: assert 'data' in af.keys() assert isinstance(af['data'], Table) assert all(af['data'] == table) # Now test using the table reader new_t = Table.read(tmpfile) assert all(new_t == table)
Example #10
Source File: ffi.py From eleanor with MIT License | 6 votes |
def check_pointing(sector, camera, chip, path=None): """ Checks to see if a pointing model exists locally already. """ # Tries to create a pointing model directory if path == None: pm_dir = '.' else: pm_dir = path searches = [ 's{0:04d}-{1}-{2}_tess_v2_pm.txt'.format(sector, camera, chip), 'pointingModel_{0:04d}_{1}-{2}.txt'.format(sector, camera, chip), ] # Checks a directory of pointing models, if it exists # Returns the pointing model if it's in the pointing model directory for search in searches: if not os.path.isdir(pm_dir): continue pm_downloaded = os.listdir(pm_dir) pm = [i for i in pm_downloaded if search in i] if len(pm) > 0: return Table.read(os.path.join(pm_dir, pm[0]), format="ascii.basic") warnings.warn("couldn't find pointing model")
Example #11
Source File: vacs.py From marvin with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_table(self, ext=None): ''' Create an Astropy table for a data extension Parameters: ext (int|str): The HDU extension name or number Returns: An Astropy table for the given extension ''' if not ext: log.info('No HDU extension specified. Defaulting to ext=1') ext = 1 # check if extension is an image if self.data[ext].is_image: log.info('Ext={0} is not a table extension. Cannot read.'.format(ext)) return return Table.read(self._path, ext, format='fits')
Example #12
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_preserve_meta(tmpdir): test_file = str(tmpdir.join('test.hdf5')) t1 = Table() t1.add_column(Column(name='a', data=[1, 2, 3])) t1.meta['a'] = 1 t1.meta['b'] = 'hello' t1.meta['c'] = 3.14159 t1.meta['d'] = True t1.meta['e'] = np.array([1, 2, 3]) t1.write(test_file, path='the_table') t2 = Table.read(test_file, path='the_table') for key in t1.meta: assert np.all(t1.meta[key] == t2.meta[key])
Example #13
Source File: test_pandas.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_read_write_format(fmt): """ Test round-trip through pandas write/read for supported formats. :param fmt: format name, e.g. csv, html, json :return: """ # Skip the reading tests if fmt == 'html' and not HAS_HTML_DEPS: pytest.skip('Missing lxml or bs4 + html5lib for HTML read/write test') pandas_fmt = 'pandas.' + fmt # Explicitly provide dtype to avoid casting 'a' to int32. # See https://github.com/astropy/astropy/issues/8682 t = Table([[1, 2, 3], [1.0, 2.5, 5.0], ['a', 'b', 'c']], dtype=(np.int64, np.float64, np.str)) buf = StringIO() t.write(buf, format=pandas_fmt) buf.seek(0) t2 = Table.read(buf, format=pandas_fmt) assert t.colnames == t2.colnames assert np.all(t == t2)
Example #14
Source File: test_pandas.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_write_with_mixins(): """Writing a table with mixins just drops them via to_pandas() This also tests passing a kwarg to pandas read and write. """ sc = SkyCoord([1, 2], [3, 4], unit='deg') q = [5, 6] * u.m qt = QTable([[1, 2], q, sc], names=['i', 'q', 'sc']) buf = StringIO() qt.write(buf, format='pandas.csv', sep=' ') exp = ['i q sc.ra sc.dec', '1 5.0 1.0 3.0', '2 6.0 2.0 4.0'] assert buf.getvalue().splitlines() == exp # Read it back buf.seek(0) qt2 = Table.read(buf, format='pandas.csv', sep=' ') # Explicitly provide converters to avoid casting 'i' to int32. # See https://github.com/astropy/astropy/issues/8682 exp_t = ascii.read(exp, converters={'i': [ascii.convert_numpy(np.int64)]}) assert qt2.colnames == exp_t.colnames assert np.all(qt2 == exp_t)
Example #15
Source File: test_connect.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_simple_meta(self, tmpdir): filename = str(tmpdir.join('test_simple.fits')) t1 = Table(self.data) t1.meta['A'] = 1 t1.meta['B'] = 2.3 t1.meta['C'] = 'spam' t1.meta['comments'] = ['this', 'is', 'a', 'long', 'comment'] t1.meta['HISTORY'] = ['first', 'second', 'third'] t1.write(filename, overwrite=True) t2 = Table.read(filename) assert equal_data(t1, t2) for key in t1.meta: if isinstance(t1.meta, list): for i in range(len(t1.meta[key])): assert t1.meta[key][i] == t2.meta[key][i] else: assert t1.meta[key] == t2.meta[key]
Example #16
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_preserve_all_dtypes(tmpdir): test_file = str(tmpdir.join('test.hdf5')) t1 = Table() for dtype in ALL_DTYPES: values = _default_values(dtype) t1.add_column(Column(name=str(dtype), data=np.array(values, dtype=dtype))) t1.write(test_file, path='the_table') t2 = Table.read(test_file, path='the_table') for dtype in ALL_DTYPES: values = _default_values(dtype) assert np.all(t2[str(dtype)] == values) assert t2[str(dtype)].dtype == dtype
Example #17
Source File: grismconf.py From grizli with MIT License | 6 votes |
def load_grism_config(conf_file): """Load parameters from an aXe configuration file Parameters ---------- conf_file : str Filename of the configuration file Returns ------- conf : `~grizli.grismconf.aXeConf` Configuration file object. Runs `conf.get_beams()` to read the sensitivity curves. """ conf = aXeConf(conf_file) conf.get_beams() return conf
Example #18
Source File: test_mask.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_serialize_ecsv_masked(tmpdir): tm = Time([1, 2, 3], format='cxcsec') tm[1] = np.ma.masked # Serializing in the default way for ECSV fails to round-trip # because it writes out a "nan" instead of "". But for jd1/jd2 # this works OK. tm.info.serialize_method['ecsv'] = 'jd1_jd2' fn = str(tmpdir.join('tempfile.ecsv')) t = Table([tm]) t.write(fn) t2 = Table.read(fn) assert t2['col0'].masked assert np.all(t2['col0'].mask == [False, True, False]) # Serializing floats to ASCII loses some precision so use allclose # and 1e-7 seconds tolerance. assert np.allclose(t2['col0'].value, t['col0'].value, rtol=0, atol=1e-7)
Example #19
Source File: grismconf.py From grizli with MIT License | 6 votes |
def __init__(self, conf_file='WFC3.IR.G141.V2.5.conf'): """Read an aXe-compatible configuration file Parameters ---------- conf_file: str Filename of the configuration file to read """ if conf_file is not None: self.conf = self.read_conf_file(conf_file) self.conf_file = conf_file self.count_beam_orders() # Global XOFF/YOFF offsets if 'XOFF' in self.conf.keys(): self.xoff = np.float(conf['XOFF']) else: self.xoff = 0. if 'YOFF' in self.conf.keys(): self.yoff = np.float(conf['YOFF']) else: self.yoff = 0.
Example #20
Source File: table_test.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_read_through_table_interface(tmpdir): from astropy.table import Table with get_pkg_data_fileobj('data/regression.xml', encoding='binary') as fd: t = Table.read(fd, format='votable', table_id='main_table') assert len(t) == 5 # Issue 8354 assert t['float'].format is None fn = os.path.join(str(tmpdir), "table_interface.xml") # W39: Bit values can not be masked with pytest.warns(W39): t.write(fn, table_id='FOO', format='votable') with open(fn, 'rb') as fd: t2 = Table.read(fd, format='votable', table_id='FOO') assert len(t2) == 5
Example #21
Source File: test_connect.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_masked(self, tmpdir): filename = str(tmpdir.join('test_masked.fits')) t1 = Table(self.data, masked=True) t1.mask['a'] = [1, 0, 1, 0] t1.mask['b'] = [1, 0, 0, 1] t1.mask['c'] = [0, 1, 1, 0] t1.write(filename, overwrite=True) t2 = Table.read(filename) assert t2.masked assert equal_data(t1, t2) assert np.all(t1['a'].mask == t2['a'].mask) # Disabled for now, as there is no obvious way to handle masking of # non-integer columns in FITS # TODO: Re-enable these tests if some workaround for this can be found # assert np.all(t1['b'].mask == t2['b'].mask) # assert np.all(t1['c'].mask == t2['c'].mask)
Example #22
Source File: test_connect.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_masked_nan(self, tmpdir): filename = str(tmpdir.join('test_masked_nan.fits')) data = np.array(list(zip([5.2, 8.4, 3.9, 6.3], [2.3, 4.5, 6.7, 8.9])), dtype=[('a', np.float64), ('b', np.float32)]) t1 = Table(data, masked=True) t1.mask['a'] = [1, 0, 1, 0] t1.mask['b'] = [1, 0, 0, 1] t1.write(filename, overwrite=True) t2 = Table.read(filename) np.testing.assert_array_almost_equal(t2['a'], [np.nan, 8.4, np.nan, 6.3]) np.testing.assert_array_almost_equal(t2['b'], [np.nan, 4.5, 6.7, np.nan]) # assert t2.masked # t2.masked = false currently, as the only way to determine whether a table is masked # while reading is to check whether col.null is present. For float columns, col.null # is not initialized
Example #23
Source File: conftest.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def T1(request): T = Table.read([' a b c d', ' 2 c 7.0 0', ' 2 b 5.0 1', ' 2 b 6.0 2', ' 2 a 4.0 3', ' 0 a 0.0 4', ' 1 b 3.0 5', ' 1 a 2.0 6', ' 1 a 1.0 7', ], format='ascii') T.meta.update({'ta': 1}) T['c'].meta.update({'a': 1}) T['c'].description = 'column c' if request.param: T.add_index('a') return T
Example #24
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_write_memory(tmpdir): with h5py.File('test', 'w', driver='core', backing_store=False) as output_file: t1 = Table() t1.add_column(Column(name='a', data=[1, 2, 3])) t1.write(output_file, path='the_table') t2 = Table.read(output_file, path='the_table') assert np.all(t2['a'] == [1, 2, 3])
Example #25
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_fileobj(tmpdir): test_file = str(tmpdir.join('test.hdf5')) t1 = Table() t1.add_column(Column(name='a', data=[1, 2, 3])) t1.write(test_file, path='the_table') import h5py with h5py.File(test_file, 'r') as input_file: t2 = Table.read(input_file, path='the_table') assert np.all(t2['a'] == [1, 2, 3])
Example #26
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_write_existing_overwrite(tmpdir): test_file = str(tmpdir.join('test.hdf5')) h5py.File(test_file, 'w').close() # create empty file t1 = Table() t1.add_column(Column(name='a', data=[1, 2, 3])) t1.write(test_file, path='the_table', overwrite=True) t2 = Table.read(test_file, path='the_table') assert np.all(t2['a'] == [1, 2, 3])
Example #27
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_wrong_fileobj(): class FakeFile: def read(self): pass f = FakeFile() with pytest.raises(TypeError) as exc: t1 = Table.read(f, format='hdf5') assert exc.value.args[0] == 'h5py can only open regular files'
Example #28
Source File: test_connect.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_memmap(self, tmpdir): filename = str(tmpdir.join('test_simple.fts')) t1 = Table(self.data) t1.write(filename, overwrite=True) t2 = Table.read(filename, memmap=False) t3 = Table.read(filename, memmap=True) assert equal_data(t2, t3) # To avoid issues with --open-files, we need to remove references to # data that uses memory mapping and force the garbage collection del t1, t2, t3 gc.collect()
Example #29
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_write_existing_append(tmpdir): test_file = str(tmpdir.join('test.hdf5')) h5py.File(test_file, 'w').close() # create empty file t1 = Table() t1.add_column(Column(name='a', data=[1, 2, 3])) t1.write(test_file, path='the_table_1', append=True) t1.write(test_file, path='the_table_2', append=True) t2 = Table.read(test_file, path='the_table_1') assert np.all(t2['a'] == [1, 2, 3]) t3 = Table.read(test_file, path='the_table_2') assert np.all(t3['a'] == [1, 2, 3])
Example #30
Source File: test_hdf5.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_read_write_simple(tmpdir): test_file = str(tmpdir.join('test.hdf5')) t1 = Table() t1.add_column(Column(name='a', data=[1, 2, 3])) t1.write(test_file, path='the_table') t2 = Table.read(test_file, path='the_table') assert np.all(t2['a'] == [1, 2, 3])