summaryrefslogtreecommitdiff
path: root/python/phat.py
diff options
context:
space:
mode:
authorBryn Keller <xoltar@xoltar.org>2016-03-07 13:51:48 -0800
committerBryn Keller <xoltar@xoltar.org>2016-03-07 13:51:48 -0800
commitd7677b1f3f5ceee9cfdb5275ef00cbf79a714122 (patch)
treefb6e9356e5093b0e077979faacf9de6f215152c2 /python/phat.py
parent41a3fef94446ec6ce115c65d92890ae9b888690c (diff)
Python interface to PHAT
Diffstat (limited to 'python/phat.py')
-rw-r--r--python/phat.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/python/phat.py b/python/phat.py
new file mode 100644
index 0000000..7cc0ae2
--- /dev/null
+++ b/python/phat.py
@@ -0,0 +1,88 @@
+import _phat
+import enum
+
+from _phat import persistence_pairs
+
+__all__ = ['boundary_matrix',
+ 'persistence_pairs',
+ 'compute_persistence_pairs',
+ 'compute_persistence_pairs_dualized']
+
+
+"""Bindings for the Persistent Homology Algorithm Toolbox
+
+
+Please see https://bitbucket.org/phat-code/phat for more information.
+"""
+
+
+class representations(enum.Enum):
+ """Available representations for internal storage of columns in
+ a `boundary_matrix`
+ """
+ bit_tree_pivot_column = 1
+ sparse_pivot_column = 2
+ full_pivot_column = 3
+ vector_vector = 4
+ vector_heap = 5
+ vector_set = 6
+ vector_list = 7
+
+class reductions(enum.Enum):
+ "Available reduction algorithms"
+ twist_reduction = 1
+ chunk_reduction = 2
+ standard_reduction = 3
+ row_reduction = 4
+ spectral_sequence_reduction = 5
+
+def __short_name(name):
+ return "".join([n[0] for n in name.split("_")])
+
+def convert(source, to_representation):
+ """Internal - function to convert from one `boundary_matrix` implementation to another"""
+ class_name = source.__class__.__name__
+ source_rep_short_name = class_name[len('boundary_matrix_'):]
+ to_rep_short_name = __short_name(to_representation.name)
+ function = getattr(_phat, "convert_%s_to_%s" % (source_rep_short_name, to_rep_short_name))
+ return function(source)
+
+def boundary_matrix(representation = representations.bit_tree_pivot_column, source = None):
+ """Returns an instance of a `boundary_matrix` class.
+ The boundary matrix will use the specified implementation for storing its column data.
+ If the `source` parameter is specified, it will be assumed to be another boundary matrix,
+ whose data should be copied into the new matrix.
+ """
+ if source:
+ return convert(source, representation)
+ else:
+ class_name = representation.name
+ short_name = __short_name(class_name)
+ function = getattr(_phat, "boundary_matrix_" + short_name)
+ return function()
+
+def compute_persistence_pairs(matrix,
+ reduction = reductions.twist_reduction):
+ """Computes persistence pairs (birth, death) for the given boundary matrix."""
+ class_name = matrix.__class__.__name__
+ representation_short_name = class_name[len('boundary_matrix_'):]
+ algo_name = reduction.name
+ algo_short_name = __short_name(algo_name)
+ function = getattr(_phat, "compute_persistence_pairs_" + representation_short_name + "_" + algo_short_name)
+ return function(matrix)
+
+def compute_persistence_pairs_dualized(matrix,
+ reduction = reductions.twist_reduction):
+ """Computes persistence pairs (birth, death) from the dualized form of the given boundary matrix."""
+ class_name = matrix.__class__.__name__
+ representation_short_name = class_name[len('boundary_matrix_'):]
+ algo_name = reduction.name
+ algo_short_name = __short_name(algo_name)
+ function = getattr(_phat, "compute_persistence_pairs_dualized_" + representation_short_name + "_" + algo_short_name)
+ return function(matrix)
+
+
+
+
+
+