def test_duplicate_count(self): from libcellml import Annotator, Parser annotator = Annotator() parser = Parser() model = parser.parseModel(file_contents("annotator/lots_of_duplicate_ids.cellml")) annotator.setModel(model) self.assertEqual(8, annotator.itemCount("duplicateId1")) self.assertEqual(7, annotator.itemCount("duplicateId3"))
print('The id strings used in the model are:') ids = annotator.ids() for id in ids: print(' - "{}"'.format(id)) # end 4.a # The hex strings printed are those which have been automatically generated by the assignId # function we can also see the 'marco' and 'polo' ids as expected. # 4.b # Use the duplicateIds function to return a vector of those ids which have been duplicated in # the model, and print them to the terminal. print('Duplicated id strings are:') duplicated_ids = annotator.duplicateIds() for id in duplicated_ids: print(' - "{}" occurs {} times'.format(id, annotator.itemCount(id))) # end 4 print('----------------------------------------------------------') print(' STEP 5: See who else is lurking around the corner ') print('----------------------------------------------------------') # The final step is to make sure that imported items can have their annotations # tracked back to their sources too. # 5.a # Retrieve an item with id of 'whoAmIAndWhereDidIComeFrom' and print its item type # to the terminal. who_am_i = annotator.item('whoAmIAndWhereDidIComeFrom') print('The type of item with ID "whoAmIAndWhereDidIComeFrom" is {}'.format(
len(duplicatedIds))) for i in duplicatedIds: print(' - ' + i) print() # Retrieve all items with the given id string. This returns a std.vector # of AnyItems which will need to be cast into libcellml items before they # can be used. Note that duplicated ids are not valid CellML, and need # to be fixed before the model can be used. allItemsWithDuplicateId1 = annotator.items('duplicateId1') # A new id string which is automatically generated and unique can be # assigned to these items. print( 'Before assigning automatic ids there are {} items with an id of "duplicateId1".' .format(annotator.itemCount('duplicateId1'))) for item in allItemsWithDuplicateId1: annotator.assignId(item) # Now there are no more items with the duplicated id 'duplicateId1' # remaining in the model. allItemsWithDuplicateId1 = annotator.items('duplicateId1') print( 'After assigning automatic ids there are {} items with an id of "duplicateId1".' .format(annotator.itemCount('duplicateId1'))) # It's straightforward to use a double loop to automatically assign new and unique ids to # any duplicated ids in the model. duplicatedIds = annotator.duplicateIds() for i in duplicatedIds: itemsWithThisId = annotator.items(i)