def GetMapping(self, item1, item2): index1, index2 = item1.magnitude, item2.magnitude diff_string = NumericMapping.DifferenceString(index1, index2) if diff_string: return NumericMapping(name=diff_string, category=Number()) else: return None
def FindMapping(self, item1, item2, *, controller, seqsee_ltm): """Find mapping between two instances of Number.""" magnitude_1, magnitude_2 = item1.magnitude, item2.magnitude diff_string = NumericMapping.DifferenceString(magnitude_1, magnitude_2) if diff_string: return NumericMapping(name=diff_string, category=Number()) else: return None
def test_sanity(self): myltm = LTMGraph(filename=self.filename) m1 = NumericMapping(name='succ', category=Prime()) m2 = NumericMapping(name='succ', category=Prime()) m3 = NumericMapping(name='pred', category=Prime()) m4 = NumericMapping(name='succ', category=Number()) self.assertEqual(m1, m2) self.assertNotEqual(m1, m3) self.assertNotEqual(m1, m4) self.assertNotEqual(m3, m4) # Also test this with parametrized categories. for content in (m1, m2, m3, m4): myltm.GetNode(content=content) myltm.DumpToFile() # Let's clear the memos for NumericMapping. NumericMapping.__memo__.clear() myltm2 = LTMGraph(filename=self.filename) self.assertEqual(5, len(myltm2.nodes)) m1_like_node = NumericMapping(name='succ', category=Prime()) self.assertNotEqual(m1_like_node, m1) self.assertNotEqual(m1_like_node.GetLTMStorableContent(), m1.GetLTMStorableContent()) myltm2.GetNode(content=m1_like_node) self.assertEqual(5, len(myltm2.nodes))
def GetMapping(self, item1, item2): binding1 = item1.DescribeAs(self) if not binding1: return None binding2 = item2.DescribeAs(self) if not binding2: return None index1, index2 = (binding1.GetBindingsForAttribute('index'), binding2.GetBindingsForAttribute('index')) diff_string = NumericMapping.DifferenceString(index1, index2) if diff_string: return NumericMapping(name=diff_string, category=self) else: return None
def FindMapping(self, item1, item2, *, controller, seqsee_ltm): """Find mapping between two instances of the same precomputed list.""" binding1 = item1.DescribeAs(self) if not binding1: return None binding2 = item2.DescribeAs(self) if not binding2: return None index1, index2 = (binding1.GetBindingsForAttribute('index'), binding2.GetBindingsForAttribute('index')) diff_string = NumericMapping.DifferenceString(index1, index2) if diff_string: return NumericMapping(name=diff_string, category=self) else: return None
def Test_123_123(self): controller = MockSeqseeController((1, 2, 3, 1, 2, 3)) workspace = controller.workspace numeric_succesor_mapping = NumericMapping(name='succ', category=Number()) ascending_group = MappingBasedCategory( mapping=numeric_succesor_mapping) group1 = self.HelperCreateAndInsertGroup(workspace, (0, 1, 2), {numeric_succesor_mapping}) group2 = self.HelperCreateAndInsertGroup(workspace, (3, 4, 5), {numeric_succesor_mapping}) group1.object.DescribeAs(ascending_group) group2.object.DescribeAs(ascending_group) self.AssertFringeContains(controller, group1, {numeric_succesor_mapping: 0.9}) from farg.apps.seqsee.subspaces.get_mapping import CF_FindAnchoredSimilarity self.AssertFringeOverlap( controller, group1, group2, 0.4, expected_similarity_affordances=(CodeletPresenceSpec( CF_FindAnchoredSimilarity, { 'left': group1, 'right': group2 }), )) controller.stream.FocusOn(group2) controller.Step() from farg.apps.seqsee.codelet_families.all import CF_FocusOn self.AssertCodeletPresent(CodeletPresenceSpec(CF_FocusOn), controller.coderack._codelets)
def test_plonk_into_place(self): ws = Workspace() ws.InsertElements((7, 8, 7, 8, 9)) # Plonk an element... returns existing element. elt = SAnchored(SElement(8), (), 3, 3) self.assertEqual(ws.elements[3], ws._PlonkIntoPlace(elt)) # Plonk a group, one item of which is an existing element, one novel. The plonked group # has the existing element as a subgroup. elt0 = SAnchored(SElement(7), (), 0, 0) elt1 = SAnchored(SElement(8), (), 1, 1) elt2 = SAnchored(SElement(7), (), 2, 2) elt3 = SAnchored(SElement(8), (), 3, 3) elt4 = SAnchored(SElement(9), (), 4, 4) numeric_successor = NumericMapping(name="succ", category=Number()) numeric_sameness = NumericMapping(name="same", category=Number()) successor_mapping_based_cat = MappingBasedCategory( mapping=numeric_successor) next_ascending = StructuralMapping( category=MappingBasedCategory(mapping=numeric_successor), bindings_mapping=frozenset((('length', numeric_successor), ('start', numeric_sameness)))) gp1 = SAnchored.Create( (elt0, elt1), underlying_mapping_set={numeric_successor}) gp2 = SAnchored.Create( (elt2, elt3, elt4), underlying_mapping_set={numeric_successor}) gp3 = SAnchored.Create( (gp1, gp2), underlying_mapping_set={next_ascending}) self.assertTrue(gp1.object.IsKnownAsInstanceOf( successor_mapping_based_cat)) plonked = ws._PlonkIntoPlace(gp3) self.assertEqual(((7, 8), (7, 8, 9)), plonked.Structure()) existing_groups = list(ws.GetGroupsWithSpan(Exactly(0), Exactly(1))) self.assertEqual(existing_groups[0], plonked.items[0]) self.assertTrue(plonked.items[0].object.IsKnownAsInstanceOf( successor_mapping_based_cat)) self.assertTrue(numeric_successor in plonked.items[ 0].object.underlying_mapping_set) self.assertTrue( next_ascending in plonked.object.underlying_mapping_set)
def test_mapping_based(self): Size2 = SizeNCategory(size=2) numeric_sameness = NumericMapping(name="same", category=Number()) numeric_successor = NumericMapping(name="succ", category=Number()) mapping_second_succ = StructuralMapping( category=Size2, bindings_mapping=frozenset((('pos_1', numeric_sameness), ('pos_2', numeric_successor)))) mapping_first_succ = StructuralMapping( category=Size2, bindings_mapping=frozenset((('pos_2', numeric_sameness), ('pos_1', numeric_successor)))) SecondSucc = MappingBasedCategory(mapping=mapping_second_succ) SecondSuccp = MappingBasedCategory(mapping=mapping_second_succ) FirstSucc = MappingBasedCategory(mapping=mapping_first_succ) self.assertEqual(SecondSucc, SecondSuccp) self.assertNotEqual(SecondSucc, FirstSucc) self.assertTrue(SecondSucc.IsInstance( SObject.Create([(3, 4), (3, 5)]))) self.assertTrue(SecondSucc.IsInstance(SObject.Create([3, 5]))) self.assertTrue(SecondSucc.IsInstance( SObject.Create([(3, 4), (3, 5), (3, 6)]))) self.assertFalse(SecondSucc.IsInstance(SObject.Create([3, 4, 6]))) self.assertFalse(SecondSucc.IsInstance( SObject.Create([(3, 4), (3, 6), (3, 7)]))) group = SObject.Create([(3, 5), (3, 6)]) binding = SecondSucc.IsInstance(group) self.assertFalse(group.IsKnownAsInstanceOf(SecondSucc)) self.assertTrue(binding) self.assertEqual( (3, 5), binding.GetBindingsForAttribute('start').Structure()) self.assertEqual( 2, binding.GetBindingsForAttribute('length').magnitude) binding2 = group.DescribeAs(SecondSucc) self.assertTrue(group.IsKnownAsInstanceOf(SecondSucc)) self.assertTrue(binding2) self.assertEqual( (3, 5), binding.GetBindingsForAttribute('start').Structure()) self.assertEqual( 2, binding.GetBindingsForAttribute('length').magnitude) self.assertNotEqual(binding, binding2) # Same (stored) binding returned. binding3 = group.DescribeAs(SecondSucc) self.assertEqual(binding3, binding2) element5 = SObject.Create([(13, 5), (13, 6)]) element6 = SObject.Create([(14, 6), (14, 7), (14, 8)]) controller = MockSeqseeController() mapping = SecondSucc.FindMapping(element5, element6, controller=controller, seqsee_ltm=controller.ltm) self.assertTrue(isinstance(mapping, StructuralMapping)) self.assertEqual(SecondSucc, mapping.category) self.assertEqual(((15, 7), (15, 8), (15, 9), (15, 10)), mapping.Apply(element6).Structure())