aboutsummaryrefslogtreecommitdiffstats
path: root/util.py
diff options
context:
space:
mode:
authorAndrea Bastoni <bastoni@sprg.uniroma2.it>2010-04-09 00:30:04 -0400
committerAndrea Bastoni <bastoni@sprg.uniroma2.it>2010-04-09 00:30:04 -0400
commitcf47870875e03488442ed39d96acfddfa2993f02 (patch)
tree02c1957b94dafc20cd19c3d2ed1d3c82a3111c43 /util.py
parent36b6614fa9ea3c2656aedb385c5c4154917aa618 (diff)
parent5a908690888395010b8a6615bc6ee3185920f2dc (diff)
Merge branch 'master' of cvs.cs.unc.edu:/cvs/proj/litmus/repo/simple-gnuplot-wrapper
Conflicts: gnuplot.py - Merge to add my "non clean" (ehm.. crappy) support to errorbars in plot_pm.py
Diffstat (limited to 'util.py')
-rw-r--r--util.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/util.py b/util.py
new file mode 100644
index 0000000..480ae5f
--- /dev/null
+++ b/util.py
@@ -0,0 +1,40 @@
1import numpy as np
2from tempfile import NamedTemporaryFile as Tmp
3
4def load_csv_file(fname, *args, **kargs):
5 f = open(fname)
6 data = np.genfromtxt(f, delimiter=",", comments="#",
7 *args, **kargs)
8 f.close() # don't leak file handles
9 return data
10
11def write_csv_file(fname, rows, header=None, width=None):
12 if fname is None:
13 f = Tmp()
14 else:
15 f = open(fname, 'w')
16 if width:
17 fmt = "%%%ds" % width
18 else:
19 fmt = "%s"
20
21 if header:
22 f.write('#')
23 f.write(", ".join([fmt % str(x) for x in header]))
24 f.write('\n')
25 for row in rows:
26 f.write(' ')
27 f.write(", ".join([fmt % str(x) for x in row]))
28 f.write('\n')
29 if fname is None:
30 # keep it open
31 f.flush()
32 return f
33 else:
34 f.close()
35
36def select(keep, rows):
37 ok_rows = np.zeros(len(rows), dtype=bool)
38 for i, row in enumerate(rows):
39 ok_rows[i] = keep(row)
40 return rows[ok_rows]