From da4b7d22647b48ca1c0420540ee7c3eada2281cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=2E=20Brandenburg?= Date: Fri, 26 Mar 2010 14:55:28 -0400 Subject: Add numpy/scipy based statistics and helpers. --- util.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 util.py (limited to 'util.py') diff --git a/util.py b/util.py new file mode 100644 index 0000000..fdedaa4 --- /dev/null +++ b/util.py @@ -0,0 +1,11 @@ +import numpy as np + +def load_csv_file(fname, *args, **kargs): + return np.genfromtxt(open(fname), delimiter=",", comments="#", + *args, **kargs) + +def select(keep, rows): + ok_rows = np.zeros(len(rows), dtype=bool) + for i, row in enumerate(rows): + ok_rows[i] = keep(row) + return rows[ok_rows] -- cgit v1.2.2 From ebb62de88cf64ba87bcb332625cc9d75a88c7257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=2E=20Brandenburg?= Date: Sat, 27 Mar 2010 11:46:13 -0400 Subject: Avoid leaking file handles. --- util.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'util.py') diff --git a/util.py b/util.py index fdedaa4..b092858 100644 --- a/util.py +++ b/util.py @@ -1,8 +1,11 @@ import numpy as np def load_csv_file(fname, *args, **kargs): - return np.genfromtxt(open(fname), delimiter=",", comments="#", + f = open(fname) + data = np.genfromtxt(f, delimiter=",", comments="#", *args, **kargs) + f.close() # don't leak file handles + return data def select(keep, rows): ok_rows = np.zeros(len(rows), dtype=bool) -- cgit v1.2.2 From 78d011ded95ac9dec18e68699a8ebf4fb3dc8797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=2E=20Brandenburg?= Date: Sat, 27 Mar 2010 20:09:34 -0400 Subject: Add CSV file writing helper. --- util.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'util.py') diff --git a/util.py b/util.py index b092858..480ae5f 100644 --- a/util.py +++ b/util.py @@ -1,4 +1,5 @@ import numpy as np +from tempfile import NamedTemporaryFile as Tmp def load_csv_file(fname, *args, **kargs): f = open(fname) @@ -7,6 +8,31 @@ def load_csv_file(fname, *args, **kargs): f.close() # don't leak file handles return data +def write_csv_file(fname, rows, header=None, width=None): + if fname is None: + f = Tmp() + else: + f = open(fname, 'w') + if width: + fmt = "%%%ds" % width + else: + fmt = "%s" + + if header: + f.write('#') + f.write(", ".join([fmt % str(x) for x in header])) + f.write('\n') + for row in rows: + f.write(' ') + f.write(", ".join([fmt % str(x) for x in row])) + f.write('\n') + if fname is None: + # keep it open + f.flush() + return f + else: + f.close() + def select(keep, rows): ok_rows = np.zeros(len(rows), dtype=bool) for i, row in enumerate(rows): -- cgit v1.2.2