sisl.io.tableSile
- class sisl.io.tableSile
Bases:
SileASCII tabular formatted data
A generic table data which will easily accommodate the most common write-outs of data
Examples
>>> a = np.arange(6).reshape(3, 2) >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(a) >>> print(''.join(open('test.dat').readlines())) 0.00000e+00 2.00000e+00 4.00000e+00 1.00000e+00 3.00000e+00 5.00000e+00
>>> a = np.arange(6).reshape(3, 2) >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(a, comment='Hello') >>> print(''.join(open('test.dat').readlines())) # Hello 0.00000e+00 2.00000e+00 4.00000e+00 1.00000e+00 3.00000e+00 5.00000e+00
>>> a = np.arange(6).reshape(3, 2) >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(a, header=['x', 'y']) >>> print(''.join(open('test.dat').readlines())) #x y 0.00000e+00 2.00000e+00 4.00000e+00 1.00000e+00 3.00000e+00 5.00000e+00
>>> a = np.arange(6).reshape(3, 2) >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(a.T) >>> print(''.join(open('test.dat').readlines())) 0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00
>>> x = np.arange(4) >>> y = np.arange(8).reshape(2, 4) + 4 >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(x, y) >>> print(''.join(open('test.dat').readlines())) 0.00000e+00 4.00000e+00 8.00000e+00 1.00000e+00 5.00000e+00 9.00000e+00 2.00000e+00 6.00000e+00 1.00000e+01 3.00000e+00 7.00000e+00 1.10000e+01
Methods
base_directory([relative_to])Retrieve the base directory of the file, relative to the path relative_to
close()dir_file([filename, filename_base])File of the current
Sileread(*args, **kwargs)Generic read method which should be overloaded in child-classes
read_data(*args, **kwargs)Read tabular data from the file.
write(*args, **kwargs)Generic write method which should be overloaded in child-classes
write_data(*args, **kwargs)Write tabular data to the file with optional header.
Attributes
File of the current
SileFile of the current
Sile- base_directory(relative_to='.')
Retrieve the base directory of the file, relative to the path relative_to
- close()
- read(*args, **kwargs)
Generic read method which should be overloaded in child-classes
- Parameters:
kwargs – keyword arguments will try and search for the attribute
read_<>and call it with the remaining**kwargsas arguments.
- read_data(*args, **kwargs)[source]
Read tabular data from the file.
- Parameters:
columns (
listofint, optional) – only return the indices of the columns that are provideddelimiter (
str, optional) – the delimiter used in the file, will automatically try to guess if not specifiedret_comment (
bool, optional) – also return the comments at the top of the file (if queried)ret_header (
bool, optional) – also return the header information (if queried)comment (
str, optional) – lines starting with this are discarded as comments
- Returns:
data (
numpy.ndarray) – the contained data in the filecomment (
listofstr) – comment lines in the file (only if ret_comment is true)header (
str) – header line defining data columns (only if ret_header is true)
- write(*args, **kwargs)
Generic write method which should be overloaded in child-classes
- Parameters:
**kwargs – keyword arguments will try and search for the attribute write_ and call it with the remaining
**kwargsas arguments.
- write_data(*args, **kwargs)[source]
Write tabular data to the file with optional header.
- Parameters:
args (
ndarrayorlistofndarray) – the different columns in the tabular data. This may either be several 1D data, or 2D arrays. Internally the data is stacked vianumpy.vstackand for any dimension higher than 2 it gets separated by two newline characters (like gnuplot acceptable data).fmt (
str, optional) – The formatting string, defaults to.5e.fmts (
str, optional) – The formatting string (for all columns), defaults tofmt * len(args). fmts has precedence over fmt.newline (
str, optional) – Defaults to\n.delimiter (
str, optional) – Defaults to\t.comment (
strorlistofstr, optional) – A pre-header text at the top of the file. This comment is automatically prepended a#at the start of new lines, for lists each item corresponds to a newline which is automatically appended.header (
strorlistofstr, optional) – A header for the data (this will be prepended a comment if not present).footer (
strorlistofstr, optional) – A footer written at the end of the file.
Examples
>>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(range(2), range(1, 3), comment='A comment', header=['index', 'value']) >>> print(''.join(open('test.dat').readlines())) # A comment #index value 0.00000e+00 1.00000e+00 1.00000e+00 2.00000e+00