diff options
Diffstat (limited to 'phstuff/diphawrapper.py')
-rw-r--r-- | phstuff/diphawrapper.py | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/phstuff/diphawrapper.py b/phstuff/diphawrapper.py index 2cead35..b2c9444 100644 --- a/phstuff/diphawrapper.py +++ b/phstuff/diphawrapper.py @@ -8,6 +8,7 @@ import tempfile import subprocess import phstuff.barcode as bc +import phstuff.simplicial as simpl """Handle marshalling data into and out of DIPHA's formats, and optionally manage running the DIPHA executable. @@ -156,7 +157,7 @@ def save_edge_list(fname, edge_list): for j in range(0, len(edge_list[i])): f.write(struct.pack('<d', factor*edge_list[i][j][1])) -def save_cubical(fname, complex): +def save_cubical(fname, array): """Save a (any-dimensional) array that will be interpreted as a cubical complex with top-dimensional cells entering the filtration at the scale given by the array values. @@ -168,7 +169,7 @@ def save_cubical(fname, complex): fname: Name of file to write. - complex: Any-dimensional array with floating-point values giving + array: Any-dimensional array with floating-point values giving the filtration values of the top-dimensional cells. """ @@ -176,14 +177,52 @@ def save_cubical(fname, complex): with open(fname, "wb") as f: f.write(struct.pack("<q", DIPHA_MAGIC)) f.write(struct.pack("<q", DIPHA_IMAGE_DATA)) - f.write(struct.pack("<q", np.product(complex.shape))) - f.write(struct.pack("<q", len(complex.shape))) + f.write(struct.pack("<q", np.product(array.shape))) + f.write(struct.pack("<q", len(array.shape))) - for n in complex.shape: + for n in array.shape: f.write(struct.pack("<q", n)) - for w in complex.flat: - f.write(struct.pack("<d", w)) + for w in array.flat: + f.write(struct.pack("<d", float(w))) + +def save_simplicial(fname, complex): + complex.order() + assert(complex.is_ordered()) + + with open(fname, "wb") as f: + f.write(struct.pack("<q", DIPHA_MAGIC)) + f.write(struct.pack("<q", DIPHA_WEIGHTED_BOUNDARY_MATRIX)) + f.write(struct.pack("<q", 0)) + f.write(struct.pack("<q", complex.size())) + f.write(struct.pack("<q", complex.top_dim())) + + # This can be made a whole lot more efficient. We're iterating + # in a stupid way. + + offsets = [] + total_nonzero = 0 + for node in complex: + d = node.dimension() + f.write(struct.pack("<q", d)) + offsets.append(total_nonzero) + if d != 0: + total_nonzero += d+1 + + for node in complex: + f.write(struct.pack("<d", node.w)) + + for off in offsets: + f.write(struct.pack("<q", off)) + + f.write(struct.pack("<q", total_nonzero)) + for node in complex: + for bnode in node.boundary_nodes(): + f.write(struct.pack("<q", bnode.i)) + + + + def load_barcode(fname, top_dim = None): @@ -368,10 +407,14 @@ class DiphaRunner: save_edge_list(self.ifile, edge_list) self.ready = True - def cubical(self, complex): + def cubical(self, array): """Initialize to run with a cubical complex. See `save_cubical`.""" - save_cubical(self.ifile, complex) + save_cubical(self.ifile, array) + self.ready = True + + def simplicial(self, complex): + save_simplicial(self.ifile, complex) self.ready = True def run(self): |