summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGard Spreemann <gspreemann@gmail.com>2017-10-16 17:38:17 +0200
committerGard Spreemann <gspreemann@gmail.com>2017-10-16 17:38:17 +0200
commitca01dde4621cfb136f93096dc65a05d7fd0bbc2a (patch)
tree7157531dbd2bfc7427841e25c2bc5b09b60dd44e
parente82fef95f573075f5a00ae8278542005f2a26bc0 (diff)
Saving barcodes in DIPHA's format.
-rw-r--r--phstuff/diphawrapper.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/phstuff/diphawrapper.py b/phstuff/diphawrapper.py
index 1b633b5..517a929 100644
--- a/phstuff/diphawrapper.py
+++ b/phstuff/diphawrapper.py
@@ -303,6 +303,35 @@ def save_text_barcode(fname, barcode):
else:
f.write("%d %g inf\n" %(dim, interval.birth))
+def save_barcode(fname, barcode):
+ """Saves a barcode in DIPHA's format.
+
+ Parameters:
+ -----------
+
+ fname: File name to save to.
+
+ barcode: The barcode to save, a dictionary keyed on degree, each
+ entry being a list of `Interval`s.
+
+ """
+
+ with open(fname, "wb") as f:
+ f.write(struct.pack("<q", DIPHA_MAGIC))
+ f.write(struct.pack("<q", DIPHA_DIPHA_PERSISTENCE_DIAGRAM))
+
+ n = 0
+ for (dim, intervals) in barcode.items():
+ n += len(intervals)
+ f.write(struct.pack("<q", n))
+
+ for (dim, intervals) in barcode.items():
+ for interval in intervals:
+ if interval.is_finite():
+ f.write(struct.pack("<qdd", dim, interval.birth, interval.death))
+ else:
+ f.write(struct.pack("<qdd", -dim - 1, interval.birth, float("inf")))
+
class DiphaRunner: