def test_format_header(): r"""Test formatting header.""" kws_all = dict( field_names=['name', 'number', 'value', 'complex'], field_units=['n/a', 'g', 'cm', 'n/a']) res_all = dict( names="# name\tnumber\tvalue\tcomplex\n", units="# n/a\tg\tcm\tn/a\n") if platform._is_win: # pragma: windows kws_all['format_str'] = "%5s\t%l64d\t%g\t%g%+gj\n" res_all['format'] = "# " + kws_all['format_str'] else: kws_all['format_str'] = "%5s\t%ld\t%g\t%g%+gj\n" res_all['format'] = "# " + kws_all['format_str'] kws_all['dtype'] = serialize.cformat2nptype(kws_all['format_str'], names=kws_all['field_names']) for x in [kws_all, res_all]: for k, v in x.items(): if isinstance(v, str): x[k] = backwards.unicode2bytes(v) elif isinstance(v, list): x[k] = [backwards.unicode2bytes(iv) for iv in v] test_list = [(['format_str', 'field_names', 'field_units'], ['names', 'units', 'format']), (['field_names', 'field_units', 'dtype'], ['names', 'units', 'format']), (['field_units', 'dtype'], ['names', 'units', 'format']), (['field_names'], ['names']), (['field_units'], ['units'])] for kws_keys, res_keys in test_list: kws = {k: kws_all[k] for k in kws_keys} res = backwards.unicode2bytes('').join([res_all[k] for k in res_keys]) nt.assert_equal(serialize.format_header(**kws), res) nt.assert_raises(ValueError, serialize.format_header)
def test_cformat2nptype_structured(): r"""Test conversion from C format string to numpy dtype for structured data types.""" if platform._is_win: # pragma: debug fmts = ["%5s", "%l64d", "%lf", "%g%+gj"] else: fmts = ["%5s", "%ld", "%lf", "%g%+gj"] names0 = ['f0', 'f1', 'f2', 'f3'] names1 = ["name", "number", "value", "complex"] dtypes = ['S5', 'i8', 'f8', 'c16'] dtype0 = np.dtype([(n, f) for n, f in zip(names0, dtypes)]) dtype1 = np.dtype([(n, f) for n, f in zip(names1, dtypes)]) alist = ["\t".join(fmts) + "\n", ''.join(fmts), fmts] for a in alist: b0 = serialize.cformat2nptype(a) nt.assert_equal(b0, dtype0) b1 = serialize.cformat2nptype(a, names=names1) nt.assert_equal(b1, dtype1)
def test_array_to_table(): r"""Test conversion of arrays to ASCII table and back.""" flist = [backwards.unicode2bytes("%5s\t%ld\t%lf\t%g%+gj\n")] for use_astropy in [False, True]: for f in flist: dtype = serialize.cformat2nptype(f) arr0 = np.ones(5, dtype) arr0['f0'][0] = backwards.unicode2bytes('hello') tab = serialize.array_to_table(arr0, f, use_astropy=use_astropy) arr1 = serialize.table_to_array(tab, f, use_astropy=use_astropy) np.testing.assert_array_equal(arr1, arr0)
def test_format_message(): r"""Test formatting message from a list or arguments and back.""" fmt = backwards.unicode2bytes("%5s\t%ld\t%lf\t%g%+gj\n") dtype = serialize.cformat2nptype(fmt) x_arr = np.ones(1, dtype) x_tup = [x_arr[n][0] for n in x_arr.dtype.names] # x_tup[0] = backwards.bytes2unicode(x_tup[0]) flist = [fmt, "%ld"] alist = [tuple(x_tup), 0] for a, f in zip(alist, flist): msg = serialize.format_message(a, f) b = serialize.process_message(msg, f) if not isinstance(a, tuple): nt.assert_equal(b, (a, )) else: nt.assert_equal(b, a) # Errors nt.assert_raises(RuntimeError, serialize.format_message, (0, ), "%d %d") nt.assert_raises(TypeError, serialize.process_message, 0, "%d") nt.assert_raises(ValueError, serialize.process_message, backwards.unicode2bytes("hello"), "%d")
def test_cformat2nptype(): r"""Test conversion from C format string to numpy dtype.""" for a, b in map_cformat2nptype: if isinstance(a, str): a = [a] for _ia in a: if _ia.startswith(backwards.bytes2unicode(serialize._fmt_char)): ia = backwards.unicode2bytes(_ia) else: ia = serialize._fmt_char + backwards.unicode2bytes(_ia) nt.assert_equal(serialize.cformat2nptype(ia), np.dtype(b)) # .str) # nt.assert_equal(serialize.cformat2nptype(ia), np.dtype(b).str) nt.assert_raises(TypeError, serialize.cformat2nptype, 0) nt.assert_raises(ValueError, serialize.cformat2nptype, backwards.unicode2bytes('s')) nt.assert_raises(ValueError, serialize.cformat2nptype, backwards.unicode2bytes('%')) nt.assert_raises(ValueError, serialize.cformat2nptype, '%d\t%f', names=['one']) for a in unsupported_nptype: nt.assert_raises(ValueError, serialize.cformat2nptype, backwards.unicode2bytes('%' + a))
def dtype(self): r"""np.dtype: Data type of the table.""" if not hasattr(self, '_dtype'): self._dtype = serialize.cformat2nptype(self.format_str, names=self.column_names) return self._dtype
def numpy_dtype(self): r"""np.dtype: Data type associated with the format string.""" if self.format_str is None: return None return serialize.cformat2nptype(self.format_str, names=self.field_names)