sisl.io.tableSile

class sisl.io.tableSile(filename, *args, **kwargs)

Bases: Sile

ASCII 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 Sile

read(*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.

base_file

File of the current Sile

file

File of the current Sile

__init__(filename, mode='r', *args, **kwargs)

Just to pass away the args and kwargs

base_directory(relative_to='.')

Retrieve the base directory of the file, relative to the path relative_to

property base_file

File of the current Sile

close()
dir_file(filename=None, filename_base='')

File of the current Sile

property file

File of the current Sile

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 **kwargs as arguments.

read_data(*args, **kwargs)[source]

Read tabular data from the file.

Parameters:
  • columns (list of int, optional) – only return the indices of the columns that are provided

  • delimiter (str, optional) – the delimiter used in the file, will automatically try to guess if not specified

  • ret_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 file

  • comment (list of str) – 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 **kwargs as arguments.

write_data(*args, **kwargs)[source]

Write tabular data to the file with optional header.

Parameters:
  • args (array_like or list of array_like) – the different columns in the tabular data. This may either be several 1D data, or 2D arrays. Internally the data is stacked via numpy.vstack and 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 to fmt * len(args). fmts has precedence over fmt.

  • newline (str, optional) – Defaults to \n.

  • delimiter (str, optional) – Defaults to \t.

  • comment (str or list of str, 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 (str or list of str, optional) – A header for the data (this will be prepended a comment if not present).

  • footer (str or list of str, 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