def BuildConstantSheaf(G, dataDimension=1): """Construct a constant sheaf on a graph G with a given dataDimension""" shf = ps.Sheaf() # Add cells for each node in the graph for node in G.nodes(): shf.AddCell(node, ps.Cell('vector', dataDimension=dataDimension)) # Add cofaces for each edge in the graph for edge in G.edges(): shf.AddCoface( edge[0], edge[1], ps.Coface('vector', 'vector', dataTools.LinearMorphism(np.eye(dataDimension)))) return shf # BuildConstantSheaf
def setUp(self): self.poset = ps.Poset([ ps.Cell(0, True, [ps.Coface(1, 1), ps.Coface(2, 1)]), ps.Cell(1, True, [ps.Coface(3, 1), ps.Coface(4, 1)]), ps.Cell(1, True, [ps.Coface(3, 1)]), ps.Cell(2, True, [ps.Coface(5, 1)]), ps.Cell(2, True, []), ps.Cell(3, True, []) ])
def setUp(self): ### complex is: {A,C,E,K,AC,AE,AK,ACK} self.cellA = ps.Cell( 0, name='A', id=0, cofaces=[ps.Coface(4, -1), ps.Coface(5, -1), ps.Coface(6, -1)]) self.cellC = ps.Cell( 0, name='C', id=1, cofaces=[ps.Coface(4, 1), ps.Coface(8, -1), ps.Coface(9, -1)]) self.cellE = ps.Cell(0, name='E', id=2, cofaces=[ps.Coface(5, 1), ps.Coface(8, 1)]) self.cellK = ps.Cell(0, name='K', id=3, cofaces=[ps.Coface(6, 1), ps.Coface(9, 1)]) self.cellAC = ps.Cell(1, name='AC', id=4, cofaces=[ps.Coface(7, 1)]) self.cellAE = ps.Cell(1, name='AE', id=5) self.cellAK = ps.Cell(1, name='AK', id=6, cofaces=[ps.Coface(7, -1)]) self.cellACK = ps.Cell(2, name='ACK', id=7) self.cellCE = ps.Cell(1, name='CE', id=8) self.cellCK = ps.Cell(1, name='CK', id=9, cofaces=[ps.Coface(7, 1)]) self.cmplx = ps.CellComplex([ self.cellA, self.cellC, self.cellE, self.cellK, self.cellAC, self.cellAE, self.cellAK, self.cellACK, self.cellCE, self.cellCK ]) return
def setUp(self): ## asc = [1,2,3,4,5] ####### list of cell indices self.coface0 = ps.Coface() self.coface1 = ps.Coface(2, 1) self.coface2 = ps.Coface(3, -1) self.cell1 = ps.Cell(0, cofaces=[self.coface1, self.coface2])
fs3 = fs.star([7]) print 'Flow sheaf degree 0 Sheaf Betti number ' + str(fs3.cobetti(0)) print 'Dimension of local sections over a single edge ' + str( fs.localSectional([0])[0].cobetti(0)) print 'Dimension of local sections over two edges ' + str( fs.localSectional([0, 1])[0].cobetti(0)) print 'Dimension of local sections over three edges ' + str( fs.localSectional([0, 1, 2])[0].cobetti(0)) print 'Dimension of local sections over three edges and a common vertex ' + str( fs.localSectional([0, 1, 2, 6])[0].cobetti(0)) print 'Induced map on local sections from 3 edges to 2 edges ' + str( fs.localRestriction([0, 1, 2], [0, 1])) print 'Induced map on local sections from 3 edges and common vertex to 3 edges ' + str( fs.localRestriction([0, 1, 2, 6], [0, 1, 2])) pos = ps.Poset([ ps.Cell(dimension=0, cofaces=[ ps.Coface(index=1, orientation=1), ps.Coface(index=2, orientation=1) ]), ps.Cell(dimension=1, cofaces=[ps.Coface(index=3, orientation=1)]), ps.Cell(dimension=1, cofaces=[ps.Coface(index=3, orientation=1)]), ps.Cell(dimension=2, cofaces=[]) ]) dg = pos.hasseDiagram()
assert (graph.number_of_nodes() == 6), "Incorrect number of cells" """ Test Structure 0 | 1 | 2 / \ 4 3 \ / 5 """ coface_square = ps.Coface(TEST_TYPE, TEST_TYPE, Pow2) graph.AddCoface(0, 1, coface_square) coface_square = ps.Coface(TEST_TYPE, TEST_TYPE, Pow2) graph.AddCoface(1, 2, coface_square) coface_square = ps.Coface(TEST_TYPE, TEST_TYPE, Pow2) graph.AddCoface(2, 3, coface_square) coface_square = ps.Coface(TEST_TYPE, TEST_TYPE, Pow2) graph.AddCoface(2, 4, coface_square) coface_square = ps.Coface(TEST_TYPE, TEST_TYPE, Pow2) graph.AddCoface(3, 5, coface_square) coface_square = ps.Coface(TEST_TYPE, TEST_TYPE, Pow3) graph.AddCoface(4, 5, coface_square) known_test_values = [2, 4, 16, 256, 65536, 16777216] graph.GetCell(0).SetDataAssignment(ps.Assignment(TEST_TYPE, 2))
graph.GetCell(0).SetDataAssignment( ps.Assignment(LINEAR_TYPE, np.array([5, 6, 7, 8]))) graph.GetCell(0).SetBounds([(0, None), (0, None), (0, None), (0, None)]) graph.GetCell(0).mOptimizationCell = True graph.GetCell(1).SetDataAssignment( ps.Assignment(LINEAR_TYPE, np.array([15, 16, 17, 18]))) graph.GetCell(2).SetDataAssignment( ps.Assignment(LINEAR_TYPE, np.array([30, 32]))) graph.AddCoface( 0, 1, ps.Coface( LINEAR_TYPE, LINEAR_TYPE, dataTools.LinearMorphism( np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])))) graph.AddCoface( 1, 2, ps.Coface( LINEAR_TYPE, LINEAR_TYPE, dataTools.LinearMorphism(np.array([[2, 0, 0, 0], [0, 2, 0, 0]])))) graph.MaximallyExtendCell(0) print("consistency radius before gobal fuse assignment:", graph.ComputeConsistencyRadius()) graph.ClearExtendedAssignments() print(graph.FuseAssignment()) print("consistency radius after global fuse assignment:", graph.ComputeConsistencyRadius())
print '0-Persistent degree 1 Betti number ' + str(PerSheaf0.betti(1)) PerSheaf1=ps.PersistenceSheaf([ColLoopSheaf,LineSheaf,LoopSheaf],[(0,1,MorColLine),(0,2,MorColLoop)],1) print '1-Persistent degree 0 Betti number ' + str(PerSheaf1.betti(0)) print '1-Persistent degree 1 Betti number ' + str(PerSheaf1.betti(1)) fs=ps.FlowSheaf(ps.DirectedGraph([(None,1),(None,1),(1,2),(1,None),(None,2),(2,None)])) print 'Flow sheaf degree 0 Betti number ' + str(fs.betti(0)) fs2=fs.star([6]) print 'Flow sheaf degree 0 Betti number ' + str(fs2.betti(0)) fs3=fs.star([7]) print 'Flow sheaf degree 0 Betti number ' + str(fs3.betti(0)) print 'Dimension of local sections over a single edge ' + str(fs.localSectional([0])[0].betti(0)) print 'Dimension of local sections over two edges ' + str(fs.localSectional([0,1])[0].betti(0)) print 'Dimension of local sections over three edges ' + str(fs.localSectional([0,1,2])[0].betti(0)) print 'Dimension of local sections over three edges and a common vertex ' + str(fs.localSectional([0,1,2,6])[0].betti(0)) print 'Induced map on local sections from 3 edges to 2 edges ' + str(fs.localRestriction([0,1,2],[0,1])) print 'Induced map on local sections from 3 edges and common vertex to 3 edges ' + str(fs.localRestriction([0,1,2,6],[0,1,2])) pos=ps.Poset([ps.Cell(dimension=0,cofaces=[ps.Coface(index=1,orientation=1),ps.Coface(index=2,orientation=1)]), ps.Cell(dimension=1,cofaces=[ps.Coface(index=3,orientation=1)]), ps.Cell(dimension=1,cofaces=[ps.Coface(index=3,orientation=1)]), ps.Cell(dimension=2,cofaces=[])]) dg=pos.hasseDiagram()
# Unit test for poset chain methods # # Copyright (c) 2013-2014, Michael Robinson # Distribution of unaltered copies permitted for noncommercial use only # All other uses require express permission of the author # This software comes with no warrantees express or implied import pysheaf as ps import numpy as np poset = ps.Poset([ ps.Cell(0, True, [ps.Coface(1, 1), ps.Coface(2, 1)]), ps.Cell(1, True, [ps.Coface(3, 1), ps.Coface(4, 1)]), ps.Cell(1, True, [ps.Coface(3, 1)]), ps.Cell(2, True, [ps.Coface(5, 1)]), ps.Cell(2, True, []), ps.Cell(3, True, []) ]) print poset.maximalChains(0) dg = ps.DirectedGraph([(0, 1), (1, 2), (1, 2), (0, 2)]) print dg.maxFlow(4, 6)
# Test generation of the meet matrix for a poset # # Copyright (c) 2013-2014, Michael Robinson # Distribution of unaltered copies permitted for noncommercial use only # All other uses require express permission of the author # This software comes with no warrantees express or implied import pysheaf as ps poset = ps.Poset([ ps.Cell(0, cofaces=[ps.Coface(2, 1)]), ps.Cell(0, cofaces=[ps.Coface(2, 1)]), ps.Cell(1, []) ]) poset.meetMatrix() poset2 = ps.Poset([ ps.Cell(0, cofaces=[ps.Coface(1, 1), ps.Coface(2, 1)]), ps.Cell(1, []), ps.Cell(1, cofaces=[ps.Coface(3, 1), ps.Coface(4, 1)]), ps.Cell(2, []), ps.Cell(2, []) ]) poset2.meetMatrix()
# -*- coding: utf-8 -*- """ Created on Wed Oct 31 16:16:22 2018 @author: mrobinson """ import pysheaf as ps shf=ps.Sheaf() shf.AddCell('A',ps.Cell('Scalar')) shf.AddCell('B',ps.Cell('Scalar')) shf.AddCell('C',ps.Cell('Scalar')) shf.AddCoface('A','C',ps.Coface('Scalar','Scalar',lambda x: x)) shf.AddCoface('B','C',ps.Coface('Scalar','Scalar',lambda x: x)) shf.GetCell('A').SetDataAssignment(ps.Assignment('Scalar',0)) shf.GetCell('B').SetDataAssignment(ps.Assignment('Scalar',1)) shf.GetCell('C').SetDataAssignment(ps.Assignment('Scalar',0.25)) shf.mPreventRedundantExtendedAssignments=False shf.MaximallyExtendCell('A') shf.MaximallyExtendCell('B') shf.MaximallyExtendCell('C') print('Consistency radius : {}'.format(shf.ComputeConsistencyRadius())) for thres in [0., 0.2, 0.3, 0.8]: print('Consistent stars at {} : {}'.format(thres,shf.ConsistentStarCollection(thres)))
Search and rescue Structure X / / \ \ U2 U3 U5 U4 / /\/ \/ / / /\ /\ U1 V1 V3 V2 """ sheaf.AddCoface( "X", "U2", ps.Coface( POSITION_3D_VELOCITY_TIME, POSITION_3D_VELOCITY, LinearMorphism( np.array([[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0]])))) sheaf.AddCoface( "X", "U3", ps.Coface(POSITION_3D_VELOCITY_TIME, BEARING_DEGREES_AND_TIME_HOURS, A)) sheaf.AddCoface( "X", "U4", ps.Coface(POSITION_3D_VELOCITY_TIME, BEARING_DEGREES_AND_TIME_HOURS, B)) sheaf.AddCoface("X", "U5", ps.Coface(POSITION_3D_VELOCITY_TIME, POSITION, E)) sheaf.AddCoface( "U2", "U1", ps.Coface(
if __name__ == '__main__': ###### generate the cells, their cofaces, and the induced cellComplex exCells = [] for cell in example: temp = ps.Cell(len(cell) - 1, name=cell) exCells.append(temp) for idx, cell in enumerate(exCells): ## cell represents the coface for l in range(1, len(cell.name)): temp = list(it.combinations(cell.name, l)) for cbn in temp: face = ("").join(cbn) ort = ps.simplexOrientation(face, cell.name) id = exidx[face] # print face, "id: ",id, "ort: ", ort # print "line 46 ",cells[id].cofaceList(); exCells[id].cofaces.append(ps.Coface(exidx[cell.name], ort)) # print id,cells[id].name,cells[id].cofaceList() # print cells[id].name, cells[id].cofaceList() exCmplx = ps.CellComplex(exCells) ######## print skeletons of example: # for dm in range(len(example[-1])): # print dm," skeleton: ", # for idx in exCmplx.skeleton(dm): # print cells[idx].name, # print # ######## print cofaces of each cell: # for cell in exCmplx.cells:
def __init__(self,rx_types=None,rxs=None): """Construct a foxhunting sheaf with a single receptionAssignment from a list of Receiver instances. This sheaf is suitable for asynchronous data. rx_types is a list of strings, either 'bearing' or 'rssi', telling the capabilities of each receiver. Note that although this constructor expects to unpack Receiver instances as defined in the generate_data module, it will happily consume anything supporting that API""" ps.Sheaf.__init__(self) if (rx_types is None) or (rxs is None): return self.rx_types=rx_types self.rx_names=[] self.rx_cellidx=[] # Use Euclidean distance for aggregating consistency radius throughout self.mNumpyNormType=2 self.mPreventRedundantExtendedAssignments=False # Usually, you want this behavior! It's redundant because this is also the PySheaf default ## Sheaf construction begins here! # It is perhaps a little confusing, but cells are indexed by integers. I do this because I want to the construction to scale cleanly with more or fewer receivers. (I could do this differently, but it'll do!) # Each cell is added according the the following pattern: # First, create the cell with its appropriate stalk "dimension". In PySheaf "dimension" means merely the number of free parameters it takes to specify the data. PySheaf will happily work with stalks of non-numeric data, but numpy arrays are all we need here. # Second, cells with virtual observations are marked with mOptimizationCell = True. True observations (made by sensors) are marked with mOptimizationCell = False. This instructs PySheaf that it is not allowed to alter an assignment supported on a true observations # Third, the stalks are given an assignment. If it's a true observation, this is used. Virtual observations are given random initial value. This will certainly be overwritten by an optimization step, later, but most optimizers want an initial guess. # Cofaces (= restrictions) are created along with the cells. Although the resulting sheaf is a sheaf of Sets, wherever the restrictions happen to be linear, we use LinearMorphisms. This has a dual purpose: (1) it's just plain easier to read and (2) if you happen to want a subsheaf that happens to be all LinearMorphisms, then it will automatically be interpreted sheaf of vector spaces. (Yay duck typing!) self.AddCell(0, # 0: Fox location and power level ps.Cell('foxloc_power', dataDimension=3)) self.GetCell(0).mOptimizationCell = True self.GetCell(0).SetDataAssignment(ps.Assignment('foxloc_power', np.random.randn((3)))) self.AddCell(1, # 1: Fox location only ps.Cell('foxloc', dataDimension=2)) self.GetCell(1).mOptimizationCell = True self.GetCell(1).SetDataAssignment(ps.Assignment('foxloc', np.random.randn(2))) self.AddCoface(0,1, ps.Coface('foxloc_power','foxloc',LinearMorphism(np.array([[1,0,0], [0,1,0]])))) idx=2 # Current cell index for rx_type,rx in zip(rx_types,rxs): if rx_type == 'bearing': for i,r in enumerate(rx.reception_reports): self.AddCell(idx, ps.Cell('foxloc_rxloc', dataDimension=4)) self.GetCell(idx).mOptimizationCell = True self.GetCell(idx).SetDataAssignment(ps.Assignment('foxloc_rxloc', np.array([np.random.randn(1)[0], np.random.randn(1)[0], r.location[0], r.location[1]]))) self.AddCell(idx+1, ps.Cell('bearing_rxloc', dataDimension=3, compareAssignmentsMethod=bearing_metric)) self.GetCell(idx+1).SetDataAssignment(ps.Assignment('bearing_rxloc', np.array([r.location[0], r.location[1], r.bearing]))) self.GetCell(idx+1).mOptimizationCell=False self.AddCoface(idx,idx+1, ps.Coface('foxloc_rxloc','bearing_rxloc', SetMorphism(bearing_restrict))) self.AddCoface(idx,1, ps.Coface('foxloc_rxloc','foxloc', LinearMorphism(np.array([[1,0,0,0], [0,1,0,0]])))) idx += 2 elif rx_type == 'rssi': for i,r in enumerate(rx.reception_reports): self.AddCell(idx, ps.Cell('foxloc_power_rxloc', dataDimension=5)) self.GetCell(idx).mOptimizationCell = True self.GetCell(idx).SetDataAssignment(ps.Assignment('foxloc_power_rxloc', np.array([np.random.randn(1)[0], np.random.randn(1)[0], r.location[0], r.location[1], r.rssi]))) self.AddCell(idx+1, ps.Cell('rssi_rxloc', dataDimension=3)) self.GetCell(idx+1).SetDataAssignment(ps.Assignment('rssi_rxloc', np.array([r.location[0], r.location[1], r.rssi]))) self.GetCell(idx+1).mOptimizationCell=False self.AddCoface(idx,idx+1, ps.Coface('foxloc_power_rxloc','rssi_rxloc', SetMorphism(freespace_restrict))) self.AddCoface(idx,0, ps.Coface('foxloc_power_rxloc','foxloc_power', LinearMorphism(np.array([[1,0,0,0,0], [0,1,0,0,0], [0,0,0,0,1]])))) idx += 2 else: raise(NotImplementedError)