def setUp(self): self.testSheaf = ps.Sheaf([ ps.SheafCell(dimension=0, stalkDim=1, cofaces=[ ps.SheafCoface(index=1, restriction=np.array([1])), ps.SheafCoface(index=2, restriction=np.array([1])) ]), ps.SheafCell(dimension=1, stalkDim=1), ps.SheafCell(dimension=1, stalkDim=1) ]) self.asg = ps.Assignment([ ps.AssignmentCell(support=0, value=0), ps.AssignmentCell(support=1, value=0), ps.AssignmentCell(support=2, value=1) ]) self.testSheaf2 = ps.Sheaf([ ps.SheafCell(dimension=0, stalkDim=1, cofaces=[ ps.SheafCoface(index=1, restriction=np.array([1])), ps.SheafCoface(index=2, restriction=np.array([1])) ]), ps.SheafCell(dimension=1, stalkDim=1), ps.SheafCell(dimension=1, stalkDim=1), ps.SheafCell( dimension=0, stalkDim=1, cofaces=[ps.SheafCoface(index=1, restriction=np.array([1]))]) ]) self.asg2 = ps.Assignment([ ps.AssignmentCell(support=0, value=0), # A ps.AssignmentCell(support=1, value=0), # C ps.AssignmentCell(support=2, value=1), # B ps.AssignmentCell(support=3, value=0) ]) # D self.asg3 = ps.Assignment([ ps.AssignmentCell(support=0, value=0), # A ps.AssignmentCell(support=1, value=0), # C ps.AssignmentCell(support=2, value=1), # B ps.AssignmentCell(support=3, value=1) ]) # D
def setUp(self): self.testSheaf1 = ps.Sheaf([]) # Small sheaf self.testSheaf2 = ps.Sheaf([ ps.SheafCell(dimension=1, stalkDim=1, cofaces=[ ps.SheafCoface(index=2, orientation=np.array(1), restriction=np.array([0.5])) ]), ps.SheafCell(dimension=1, stalkDim=1, cofaces=[ ps.SheafCoface(index=2, orientation=np.array(-1), restriction=np.array([1])) ]), ps.SheafCell(dimension=2, stalkDim=1, cofaces=[]), ps.SheafCell(dimension=0, stalkDim=1, cofaces=[ ps.SheafCoface(index=0, orientation=np.array(1), restriction=np.array([2])), ps.SheafCoface(index=1, orientation=np.array(1), restriction=np.array([1])) ]) ]) self.asg2_1 = ps.Assignment([ ps.AssignmentCell(support=0, value=np.array(0.)), ps.AssignmentCell(support=1, value=np.array(1.)) ]) self.asg2_3 = ps.Assignment([ ps.AssignmentCell(support=0, value=np.array(0.)), ps.AssignmentCell(support=1, value=np.array(1.)), ps.AssignmentCell(support=2, value=np.array(0.5)), ps.AssignmentCell(support=3, value=np.array(1. / 3)) ])
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).mOptimizationCell = True graph.mPreventRedundantExtendedAssignments = True cell2_test_value = 81 cell5_test_value = 43046721 result_answer = 3 graph.GetCell(2).SetDataAssignment( ps.Assignment(TEST_TYPE, cell2_test_value)) graph.GetCell(5).SetDataAssignment( ps.Assignment(TEST_TYPE, cell5_test_value)) graph.ClearExtendedAssignments() graph.mNumpyNormType = None
compactClosure=True, stalkDim=1, metric=anglemetric, cofaces=[]), # V2 ps.SheafCell(dimension=2, compactClosure=True, stalkDim=1, cofaces=[]) ]) # V3 # Structure the data from Table 1 in the paper into various partially-filled Assignments, one for each Case listed input_data = [ ps.Assignment([ ps.AssignmentCell(support=0, value=np.array( [-70.649, 42.753, 11220, 495, 164, 0.928])), # X ps.AssignmentCell(support=1, value=np.array([-70.662, 42.829, 11178])), # U1 ps.AssignmentCell(support=2, value=np.array([-70.587, 42.741, 11346, 495, 164])), # U2 ps.AssignmentCell(support=3, value=np.array([77.1, 0.943])), # U3 ps.AssignmentCell(support=4, value=np.array([61.3, 0.890])), # U4 ps.AssignmentCell(support=5, value=np.array([-64.599, 44.243])) ]), # U5 ps.Assignment([ ps.AssignmentCell(support=0, value=np.array( [-70.668, 42.809, 11431, 495, 164, 1.05])), # X ps.AssignmentCell(support=1, value=np.array([-70.663, 42.752, 11299])), # U1 ps.AssignmentCell(support=2, value=np.array([-70.657, 42.773, 11346, 495, 164])), # U2
# "Topological Signal Processing", by Michael Robinson # # 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 numpy as np import pysheaf as ps sh1=ps.Sheaf([ps.SheafCell(dimension=0,cofaces=[ps.SheafCoface(index=2,orientation=-1,restriction=np.matrix(1))]), ps.SheafCell(dimension=0,cofaces=[ps.SheafCoface(index=2,orientation=1,restriction=np.matrix(1))]), ps.SheafCell(dimension=1,cofaces=[],stalkDim=1)]) sec1=ps.Assignment([ps.AssignmentCell(0,1),ps.AssignmentCell(1,1)]) sec2=ps.Assignment([ps.AssignmentCell(0,1)]) sec3=ps.Assignment([ps.AssignmentCell(0,1),ps.AssignmentCell(1,2)]) # Extending Assignment over two vertices to a common coface if sec1.extend(sh1,2) and sec2.extend(sh1,2) and not sec3.extend(sh1,2) and sec2.extend(sh1,1): print "Test 1 passed" else: print "Test 1 failed" sh2=ps.Sheaf([ps.SheafCell(dimension=0,cofaces=[ps.SheafCoface(index=1,orientation=-1,restriction=np.matrix(1))]), ps.SheafCell(dimension=1,cofaces=[],stalkDim=1), ps.SheafCell(dimension=0,cofaces=[ps.SheafCoface(index=1,orientation=1,restriction=np.matrix(-1)), ps.SheafCoface(index=3,orientation=-1,restriction=np.matrix(1))]), ps.SheafCell(dimension=1,cofaces=[],stalkDim=1)]) sec4=ps.Assignment([ps.AssignmentCell(0,1)])
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) assert(graph.number_of_edges() == 6), "Incorrect number of cofaces" print("number of nodes in graph",graph.number_of_nodes()) print("number of edges in graph",graph.number_of_edges()) known_test_values = [2,4,16,256,65536,16777216] graph.GetCell(0).SetDataAssignment(ps.Assignment(TEST_TYPE,2)) print("dataAssignment: ",graph.GetCell(0).mDataAssignment) graph.MaximallyExtendCell(0) assert(graph.GetCell(0).GetDataAssignment().mValue == known_test_values[0]), "Incorrect dataAssignment" assert(graph.GetCell(1).GetExtendedAssignmentValueList() == [ps.Assignment(TEST_TYPE,known_test_values[1])]), "Incorrect ExtendedAssignment" assert(graph.GetCell(2).GetExtendedAssignmentValueList() == [ps.Assignment(TEST_TYPE,known_test_values[2])]), "Incorrect ExtendedAssignment" assert(graph.GetCell(3).GetExtendedAssignmentValueList() == [ps.Assignment(TEST_TYPE,known_test_values[3])]), "Incorrect ExtendedAssignment" assert(graph.GetCell(4).GetExtendedAssignmentValueList() == [ps.Assignment(TEST_TYPE,known_test_values[3])]), "Incorrect ExtendedAssignment" assert(graph.GetCell(5).GetExtendedAssignmentValueList() == [ps.Assignment(TEST_TYPE,known_test_values[4]),ps.Assignment(TEST_TYPE,known_test_values[5])]), "Incorrect ExtendedAssignment" cell2_test_value = 6
if __name__ == '__main__': print("+-+-+-+-+-+-+-+-+-+-+-+-+") print("+-Linear Sheaf Example!-+") print("+-+-+-+-+-+-+-+-+-+-+-+-+") graph = ps.Sheaf() LINEAR_TYPE = "linear_morphism_type" graph.AddCell(0, ps.Cell(LINEAR_TYPE, CompareArrays, dataDimension=4)) graph.AddCell(1, ps.Cell(LINEAR_TYPE, CompareArrays, dataDimension=4)) graph.AddCell(2, ps.Cell(LINEAR_TYPE, CompareArrays, dataDimension=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]]))))
""" 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)))
"U3", "V3", ps.Coface(BEARING_DEGREES_AND_TIME_HOURS, TIME_HOURS, LinearMorphism(np.array([[0, 1]])))) sheaf.AddCoface( "U4", "V3", ps.Coface(BEARING_DEGREES_AND_TIME_HOURS, TIME_HOURS, LinearMorphism(np.array([[0, 1]])))) sheaf.AddCoface( "U4", "V2", ps.Coface(BEARING_DEGREES_AND_TIME_HOURS, BEARING_DEGREES, LinearMorphism(np.array([[1, 0]])))) sheaf.AddCoface("U5", "V1", ps.Coface(POSITION, BEARING_DEGREES, C)) sheaf.AddCoface("U5", "V2", ps.Coface(POSITION, BEARING_DEGREES, D)) sheaf.GetCell("X").SetDataAssignment( ps.Assignment(POSITION_3D_VELOCITY_TIME, np.array([-70.649, 42.753, 11220, 495, 164, 0.928]))) sheaf.GetCell("U1").SetDataAssignment( ps.Assignment(POSITION_3D, np.array([-70.662, 42.829, 11178]))) sheaf.GetCell("U2").SetDataAssignment( ps.Assignment(POSITION_3D_VELOCITY, np.array([-70.587, 42.741, 11346, 495, 164]))) sheaf.GetCell("U3").SetDataAssignment( ps.Assignment(BEARING_DEGREES_AND_TIME_HOURS, np.array([77.1, 0.943]))) sheaf.GetCell("U4").SetDataAssignment( ps.Assignment(BEARING_DEGREES_AND_TIME_HOURS, np.array([61.3, 0.890]))) sheaf.GetCell("U5").SetDataAssignment( ps.Assignment(POSITION, np.array([-64.599, 44.243]))) sheaf.GetCell("X").mOptimizationCell = True sheaf.GetCell("U1").mOptimizationCell = True sheaf.GetCell("U2").mOptimizationCell = True
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)