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 = []