/usr/share/pyshared/Scientific/IO/ArrayIO.py is in python-scientific 2.8-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | # Array I/O to text files
#
# Written by Konrad Hinsen <hinsen@cnrs-orleans.fr>
# last revision: 2006-6-23
#
"""
Basic support for I/O of one- and two-dimensional numerical arrays to and from plain text files
The text file format is very simple and used by many other programs as
well:
- each line corresponds to one row of the array
- the numbers within a line are separated by white space
- lines starting with # are ignored (comment lines)
An array containing only one line or one column is returned as a
one-dimensional array on reading. One-dimensional arrays are written
as one item per line.
Numbers in files to be read must conform to Python/C syntax. For
reading files containing Fortran-style double-precision numbers
(exponent prefixed by D), use the module L{Scientific.IO.FortranFormat}.
"""
from Scientific.IO.TextFile import TextFile
from Scientific import N; Numeric = N
import string
def readArray(filename):
"""
Read array data from a file
This function works for arbitrary data types (every array element can be
given by an arbitrary Python expression), but at the price of being
slow. For large arrays, use L{readFloatArray} or L{readIntegerArray}
if possible.
@param filename: the name of the file to read
@type filename: C{str}
@returns: an array containing the data from the file
@rtype: C{Numeric.array}
"""
data = []
for line in TextFile(filename):
if len(line) == 0 and len(data) > 0:
break
if line[0] != '#':
data.append(map(eval, string.split(line)))
a = Numeric.array(data)
if a.shape[0] == 1 or a.shape[1] == 1:
a = Numeric.ravel(a)
return a
def readFloatArray(filename):
"""
Read array data from a file into an array of floats
@param filename: the name of the file to read
@type filename: C{str}
@returns: an array containing the data from the file
@rtype: C{Numeric.array} of C{float}
"""
data = []
for line in TextFile(filename):
if line[0] != '#':
data.append(map(string.atof, string.split(line)))
a = Numeric.array(data)
if a.shape[0] == 1 or a.shape[1] == 1:
a = Numeric.ravel(a)
return a
def readIntegerArray(filename):
"""
Read array data from a file into an array of integers
@param filename: the name of the file to read
@type filename: C{str}
@returns: an array containing the data from the file
@rtype: C{Numeric.array} of C{int}
"""
data = []
for line in TextFile(filename):
if line[0] != '#':
data.append(map(string.atoi, string.split(line)))
a = Numeric.array(data)
if a.shape[0] == 1 or a.shape[1] == 1:
a = Numeric.ravel(a)
return a
def writeArray(array, filename, mode='w'):
"""
Write a text representation of an array to a file.
@param array: the array to be written
@type array: C{Numeric.array}
@param filename: the name of the output file
@type filename: C{str}
@param mode: the file access mode, 'w' (new file) or 'a' (append)
@type mode: C{str}
"""
file = TextFile(filename, mode)
if len(array.shape) == 1:
array = array[:, Numeric.NewAxis]
for line in array:
for element in line:
file.write(`element` + ' ')
file.write('\n')
file.close()
#
# Write several data sets (one point per line) to a text file,
# with a separator line between data sets. This is sufficient
# to make input files for most plotting programs.
#
def writeDataSets(datasets, filename, separator = ''):
"""
Write multiple datasets to a text file.
@param datasets: a sequence of datasets describing a curve to be
plotted. Each dataset is either a 1d-array
(list of values) or a 2d-array of shape N x 2
(list of (x, y) pairs). Nested lists can be used
instead of arrays.
@param filename: the name of the output file
@type filename: C{str}
@param separator: the contents of the line that is written between
two datasets
@type separator: C{str}
"""
file = TextFile(filename, 'w')
nsets = len(datasets)
for i in range(nsets):
d = Numeric.array(list(datasets[i]))
if len(d.shape) == 1:
d = d[:, Numeric.NewAxis]
for point in d:
for number in point:
file.write(`number` + ' ')
file.write('\n')
if (i < nsets-1):
file.write(separator + '\n')
file.close()
|