From 08b7d8dd85aaf36e640448b78bc5b486b570ad9e Mon Sep 17 00:00:00 2001 From: einarr Date: Thu, 11 Jan 2007 23:24:05 +0000 Subject: [PATCH] Added function to save fluents tab separated values (ftsv) files. --- fluents/dataset.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/fluents/dataset.py b/fluents/dataset.py index f9cbc96..4446dc8 100644 --- a/fluents/dataset.py +++ b/fluents/dataset.py @@ -383,6 +383,36 @@ class Selection(dict): self[axis] = labels +def write_ftsv(fd, ds): + # Write header information + if isinstance(ds, CategoryDataset): + type = 'category' + elif isinstance(ds, GraphDataset): + type = 'network' + elif isinstance(ds, Dataset): + type = 'dataset' + else: + raise Exception("Unknown object") + print >> fd, "# type: %s" % type + + for dim in ds.get_dim_name(): + print >> fd, "# dimension: %s" % dim, + for id in ds.get_identifiers(dim, None, True): + print >> fd, id, + print >> fd + + print >> fd, "# name: %s" % ds.get_name() + print >> fd + + # Write data + m = ds.asarray() + y, x = m.shape + for j in range(y): + for i in range(x): + print >> fd, "%s\t" % m[j, i], + print >> fd + + def read_ftsv(fd): split_re = re.compile('^#\s*(\w+)\s*:\s*(.+)') dimensions = []