def test_maximum_common_subgraph_for_data_pair(self):
        """Load two segmented data frames and track them"""

        mesh_sequence = mesh.read_sequence_from_data(
            path.join(dirname(__file__), 'data', 'first_few_frames'))

        first_mesh = mesh_sequence[0]

        second_mesh = mesh_sequence[1]

        print 'I am testing...'
        subgraph_finder = tracking.LocalisedSubgraphFinder(
            first_mesh, second_mesh)
        print 'Can I find a subgraph?'
        subgraph_finder.find_maximum_common_subgraph()
        print len(subgraph_finder.largest_mappings)
        post_processor = tracking.PostProcessor(
            first_mesh, second_mesh, subgraph_finder.largest_mappings)
        post_processor.index_global_ids_from_largest_mappings()

        #         tracked_ids = tracking.find_maximum_common_subgraph( first_mesh, second_mesh )

        first_mesh.plot(path.join(dirname(__file__), 'output',
                                  'first_mesh_tracked.pdf'),
                        color_by_global_id=True,
                        total_number_of_global_ids=len(first_mesh.elements))

        second_mesh.plot(path.join(dirname(__file__), 'output',
                                   'second_mesh_tracked.pdf'),
                         color_by_global_id=True,
                         total_number_of_global_ids=len(first_mesh.elements))

        first_mesh.save(
            path.join(dirname(__file__), 'output', 'first_mesh_tracked.mesh'))
        second_mesh.save(
            path.join(dirname(__file__), 'output', 'second_mesh_tracked.mesh'))

        post_processor.tidy_current_mapping()

        first_mesh.plot(
            path.join(dirname(__file__), 'output',
                      'first_mesh_tracked_cleaned.pdf'),
            color_by_global_id=True,
            total_number_of_global_ids=first_mesh.get_num_elements())

        second_mesh.plot(
            path.join(dirname(__file__), 'output',
                      'second_mesh_tracked_cleaned.pdf'),
            color_by_global_id=True,
            total_number_of_global_ids=first_mesh.get_num_elements())

        first_mesh.save(
            path.join(dirname(__file__), 'output',
                      'first_mesh_tracked_cleaned.mesh'))
        second_mesh.save(
            path.join(dirname(__file__), 'output',
                      'second_mesh_tracked_cleaned.mesh'))
    def test_post_processing_for_second_data_pair(self):
        """Load two segmented data frames and track them"""

        first_mesh = mesh.load(
            path.join(dirname(__file__), 'output',
                      'second_mesh_tracked_with_third_mesh_cleaned.mesh'))
        second_mesh = mesh.load(
            path.join(dirname(__file__), 'output',
                      'third_mesh_tracked_cleaned.mesh'))

        largest_mapping = {}

        for element in first_mesh.elements:
            if element.global_id != None:
                second_frame_id = second_mesh.get_element_with_global_id(
                    element.global_id).id_in_frame
                largest_mapping[element.id_in_frame] = second_frame_id

        largest_mappings = [largest_mapping]

        post_processor = tracking.PostProcessor(first_mesh, second_mesh,
                                                largest_mappings)
        post_processor.index_global_ids_from_largest_mappings()

        post_processor.post_process_with_data()

        first_mesh.plot(
            path.join(dirname(__file__), 'output',
                      'second_mesh_tracked_with_third_mesh_processed.pdf'),
            color_by_global_id=True,
            total_number_of_global_ids=first_mesh.get_num_elements())

        second_mesh.plot(
            path.join(dirname(__file__), 'output',
                      'third_mesh_tracked_processed.pdf'),
            color_by_global_id=True,
            total_number_of_global_ids=first_mesh.get_num_elements())

        first_mesh.save(
            path.join(
                dirname(__file__), 'output',
                'second_mesh_tracked_with_third_mesh_tracked_processed.mesh'))
        second_mesh.save(
            path.join(dirname(__file__), 'output',
                      'third_mesh_tracked_processed.mesh'))
def make_division_II_plot(figuresize):
    """We load a mesh where two maximum common subgraphs will be identified, and track the division event"""

    mesh_one = mesh.load('standard_ambiguous_division_one.mesh')
    mesh_two = copy.deepcopy(mesh_one)

    mesh_one.assign_frame_ids_in_order()
    mesh_two.assign_frame_ids_randomly()

    # build ground truth for testing the mapping
    ground_truth = {}
    for element_index, element in enumerate(mesh_one.elements):
        ground_truth[
            element.id_in_frame] = mesh_two.elements[element_index].id_in_frame

    # pick the element closest to the centre
    most_central_element = mesh_two.find_most_central_element()

    mesh_two.divide_element_with_frame_id_in_direction(
        most_central_element.id_in_frame, [1.0, 1.0])

    subgraph_finder = tracking.ReducedBacktrackingSubgraphFinder(
        mesh_one, mesh_two)
    subgraph_finder.find_maximum_common_subgraph()

    post_processor = tracking.PostProcessor(
        mesh_one, mesh_two, [subgraph_finder.largest_mappings[1]])
    post_processor.index_global_ids_from_largest_mappings()

    tracked_ids = post_processor.mapped_ids

    tracked_ids_all = tracked_ids

    plotname = 'division_II'

    make_difference_plot(mesh_one, mesh_two, tracked_ids, tracked_ids_all,
                         plotname, figuresize)
    def test_post_processing_for_first_data_pair(self):
        """Load two segmented data frames and track them"""

        first_mesh = mesh.load(
            path.join(dirname(__file__), 'output',
                      'first_mesh_tracked_cleaned.mesh'))
        second_mesh = mesh.load(
            path.join(dirname(__file__), 'output',
                      'second_mesh_tracked_cleaned.mesh'))

        largest_mapping = {}

        for element in first_mesh.elements:
            if element.global_id != None:
                second_frame_id = second_mesh.get_element_with_global_id(
                    element.global_id).id_in_frame
                largest_mapping[element.id_in_frame] = second_frame_id

        largest_mappings = [largest_mapping]

        post_processor = tracking.PostProcessor(first_mesh, second_mesh,
                                                largest_mappings)
        post_processor.index_global_ids_from_largest_mappings()

        post_processor.post_process_with_data()

        first_mesh.plot(
            path.join(dirname(__file__), 'output',
                      'first_mesh_tracked_processed.pdf'),
            color_by_global_id=True,
            total_number_of_global_ids=first_mesh.get_num_elements())

        second_mesh.plot(
            path.join(dirname(__file__), 'output',
                      'second_mesh_tracked_processed.pdf'),
            color_by_global_id=True,
            total_number_of_global_ids=first_mesh.get_num_elements())

        first_mesh.save(
            path.join(dirname(__file__), 'output',
                      'first_mesh_tracked_processed.mesh'))
        second_mesh.save(
            path.join(dirname(__file__), 'output',
                      'second_mesh_tracked_processed.mesh'))
        global_ids = []
        for element in second_mesh.elements:
            if element.global_id != None:
                if element.global_id in global_ids:
                    print 'found double global id'
                else:
                    global_ids.append(element.global_id)

        global_ids_one = []
        for element in first_mesh.elements:
            if element.global_id != None:
                if element.global_id in global_ids_one:
                    print 'found double global id'
                else:
                    global_ids_one.append(element.global_id)

        self.assertEqual(len(global_ids_one), len(global_ids))