diff options
| author | Andrea Bastoni <bastoni@sprg.uniroma2.it> | 2010-04-09 00:30:04 -0400 |
|---|---|---|
| committer | Andrea Bastoni <bastoni@sprg.uniroma2.it> | 2010-04-09 00:30:04 -0400 |
| commit | cf47870875e03488442ed39d96acfddfa2993f02 (patch) | |
| tree | 02c1957b94dafc20cd19c3d2ed1d3c82a3111c43 /util.py | |
| parent | 36b6614fa9ea3c2656aedb385c5c4154917aa618 (diff) | |
| parent | 5a908690888395010b8a6615bc6ee3185920f2dc (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.py | 40 |
1 files changed, 40 insertions, 0 deletions
| @@ -0,0 +1,40 @@ | |||
| 1 | import numpy as np | ||
| 2 | from tempfile import NamedTemporaryFile as Tmp | ||
| 3 | |||
| 4 | def 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 | |||
| 11 | def 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 | |||
| 36 | def 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] | ||
