def results(self): r"""list: Results that should be found in the output files.""" assert(os.path.isfile(self.input_file)) assert(os.path.isfile(self.input_table)) assert(os.path.isfile(self.output_array)) with open(self.input_file, 'r') as fd: icont = fd.read() with open(self.input_table, 'rb') as fd: iATT = serialize.table_to_array(fd.read(), comment='#') with open(self.input_array, 'rb') as fd: iATA = serialize.table_to_array(fd.read(), comment='#') return [icont, (self.check_table, iATT), (self.check_table, iATA)]
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 read_header(self): r"""Read header lines from the file and update serializer info.""" if self.header_was_read: return header_lines = [] header_size = 0 self.fd.seek(0) for line in self.fd: sline = backwards.unicode2bytes( line.replace(self.platform_newline, self.newline)) if not sline.startswith(self.comment): break header_size += len(line) header_lines.append(sline) # Parse header & set serializer attributes header = serialize.parse_header(header_lines) for k in ['format_str', 'field_names', 'field_units']: if header.get(k, False): setattr(self.serializer, k, header[k]) # Try to determine format from array without header str_fmt = backwards.unicode2bytes('%s') if (((self.serializer.format_str is None) or (str_fmt in self.serializer.format_str))): with open(self.address, self.open_mode) as fd: fd.seek(header_size) all_contents = fd.read() if len(all_contents) == 0: # pragma: debug return # In case the file has not been written arr = serialize.table_to_array(all_contents, names=self.serializer.field_names, comment=self.comment, delimiter=self.delimiter) self.serializer.field_names = arr.dtype.names if self.serializer.format_str is None: self.serializer.format_str = serialize.table2format( arr.dtype, delimiter=self.delimiter, comment=backwards.unicode2bytes(''), newline=self.newline) while str_fmt in self.serializer.format_str: ifld = self.serializer.field_formats.index(str_fmt) max_len = len( max(arr[self.serializer.field_names[ifld]], key=len)) new_str_fmt = backwards.unicode2bytes('%' + str(max_len) + 's') self.serializer.format_str = self.serializer.format_str.replace( str_fmt, new_str_fmt, 1) self.delimiter = self.serializer.table_info['delimiter'] # Seek to just after the header self.fd.seek(header_size) self.header_was_read = True
def func_deserialize(self, msg): r"""Deserialize a message. Args: msg: Message to be deserialized. Returns: obj: Deserialized message. """ if self.format_str is None: raise RuntimeError("Format string is not defined.") if (len(msg) == 0): out = tuple() elif self.as_array: out = serialize.table_to_array(msg, self.format_str, use_astropy=self.use_astropy, names=self.field_names) else: out = super(AsciiTableSerialize, self).func_deserialize(msg) return out
def read_array(self, names=None): r"""Read the table in as an array. Args: names (list, optional): List of column names to label columns. If not provided, existing names are used if they exist. Defaults to None. Returns: np.ndarray: Array of table contents. Raises: ValueError: If names are provided, but not the same number as there are columns. """ if names is None: names = self.column_names if (names is not None) and (len(names) != self.ncols): raise ValueError( "The number of names does not match the number of columns") if hasattr(self, '_arr'): return self._arr openned = False if not self.is_open: self.open() openned = True msg = self.fd.read() arr = serialize.table_to_array(msg, fmt_str=self.format_str, use_astropy=self.use_astropy, delimiter=self.column, comment=self.comment, names=names) if openned: self.close() return arr
def check_table(self, fname, iAT): r"""Assert that contents of input/output ascii tables are identical.""" with open(fname, 'rb') as fd: oAT = serialize.table_to_array(fd.read(), comment='#') np.testing.assert_equal(oAT, iAT)