def test_track_multiple_t1_swaps_special_example_7(self):

        sys.setrecursionlimit(40000)
 
        first_mesh = mesh.load(path.join(dirname(__file__), 'data', 'multiple_t1_example_7_before.mesh'))
        second_mesh = mesh.load(path.join(dirname(__file__), 'data', 'multiple_t1_example_7_after.mesh'))
         
        # build ground truth for testing the mapping
        ground_truth = {}
        for element_index, element in enumerate(first_mesh.elements):
            ground_truth[element.id_in_frame] = second_mesh.elements[element_index].id_in_frame
         
        first_mesh.plot(path.join(dirname(__file__),'output','multiple_t1_example_7_before.pdf'))
        second_mesh.plot(path.join(dirname(__file__),'output','multiple_t1_example_7_after.pdf'))
 
        tracking.track(first_mesh, second_mesh)
#         tracking.find_maximum_common_subgraph(first_mesh, second_mesh)
     
        first_mesh.plot(path.join(dirname(__file__),'output','multiple_t1_example_7_before.pdf'), 
                        color_by_global_id = True, total_number_of_global_ids = first_mesh.get_num_elements())
        second_mesh.plot(path.join(dirname(__file__),'output','multiple_t1_example_7_after.pdf'),
                        color_by_global_id = True, total_number_of_global_ids = first_mesh.get_num_elements())
         
        print 'evaluate tracking'
        tracking_success, number_tracked_cells = tracking.evaluate_tracking(first_mesh, 
                                                                   second_mesh, 
                                                                   ground_truth)
Beispiel #2
0
def test_success(t1_percentage, output = False):
    """Perform tracking test on a mesh where t1_number of t1 swaps are randomly performed
    
    Parameters
    ----------
    
    t1_number : int
        How many T1 swaps should be performed
        
    output : bool
        whether the meshes should be written to a file. Defaults to False.
        
    Returns
    -------
    
    success_boolean : bool
       True if tracking successful
       
    average_number_tracked_cells : float
       average number of cells that were tracked correctly
    """
    
    print 'making meshes'
    first_mesh = mesh.creation.generate_random_tesselation(20,20)
    second_mesh = copy.deepcopy(first_mesh)
    
    first_mesh.assign_frame_ids_in_order()
    second_mesh.assign_frame_ids_randomly()

    # build ground truth for testing the mapping
    print 'making ground truth'
    ground_truth = {}
    for element_index, element in enumerate(first_mesh.elements):
        ground_truth[element.id_in_frame] = second_mesh.elements[element_index].id_in_frame

    total_number_edges = len(first_mesh.collect_elements_of_inner_edges())
    edges_to_swap = int( 0.01 * t1_percentage * total_number_edges)
    print 'applying t1 swaps'
    apply_t1_swaps_to_mesh( second_mesh, edges_to_swap )
    
    if output:
        first_mesh.save(path.join(dirname(__file__),'output','multiple_t1_before.mesh'))
        second_mesh.save(path.join(dirname(__file__),'output','multiple_t1_after.mesh'))
    
    try:
        tracking.track(first_mesh, second_mesh)
    except:
        return 0, 0, 0

    if output:
        first_mesh.plot(path.join(dirname(__file__),'output','multiple_t1_before.pdf'), 
                        color_by_global_id = True, total_number_of_global_ids = first_mesh.get_num_elements())
        second_mesh.plot(path.join(dirname(__file__),'output','multiple_t1_after.pdf'),
                        color_by_global_id = True, total_number_of_global_ids = first_mesh.get_num_elements())
    
    print 'evaluate tracking'
    tracking_success, number_tracked_cells, number_incorrectly_tracked_cells = tracking.evaluate_tracking(first_mesh, 
                                                                                                          second_mesh, 
                                                                                                          ground_truth)
    
    percentage_correctly_tracked_cells = float(number_tracked_cells)/first_mesh.get_num_elements()
    percentage_incorrectly_tracked_cells = float(number_incorrectly_tracked_cells)/first_mesh.get_num_elements()

    return int(tracking_success), percentage_correctly_tracked_cells, percentage_incorrectly_tracked_cells
def test_success(t1_percentage, output=False):
    """Perform tracking test on a mesh where t1_number of t1 swaps are randomly performed
    
    Parameters
    ----------
    
    t1_number : int
        How many T1 swaps should be performed
        
    output : bool
        whether the meshes should be written to a file. Defaults to False.
        
    Returns
    -------
    
    success_boolean : bool
       True if tracking successful
       
    average_number_tracked_cells : float
       average number of cells that were tracked correctly
    """

    print 'making meshes'
    first_mesh = mesh.creation.generate_random_tesselation(20, 20)
    second_mesh = copy.deepcopy(first_mesh)

    first_mesh.assign_frame_ids_in_order()
    second_mesh.assign_frame_ids_randomly()

    # build ground truth for testing the mapping
    print 'making ground truth'
    ground_truth = {}
    for element_index, element in enumerate(first_mesh.elements):
        ground_truth[element.id_in_frame] = second_mesh.elements[
            element_index].id_in_frame

    total_number_edges = len(first_mesh.collect_elements_of_inner_edges())
    edges_to_swap = int(0.01 * t1_percentage * total_number_edges)
    print 'applying t1 swaps'
    apply_t1_swaps_to_mesh(second_mesh, edges_to_swap)

    counter = 0
    for cell in ground_truth:
        this_cell = first_mesh.get_element_with_frame_id(cell)
        image_cell = second_mesh.get_element_with_frame_id(ground_truth[cell])
        if np.linalg.norm(this_cell.calculate_centroid() -
                          image_cell.calculate_centroid()) > 0.0001:
            counter += 1

    print 'tissue with t1 percentage'
    print t1_percentage
    print 'has this rearranged_cell percentage'
    print float(counter) / first_mesh.get_num_elements()

    #     if output:
    #         first_mesh.save(path.join(dirname(__file__),'output','multiple_t1_before.mesh'))
    #         second_mesh.save(path.join(dirname(__file__),'output','multiple_t1_after.mesh'))
    #
    #     try:
    #         tracking.track(first_mesh, second_mesh)
    #     except:
    #         return 0, 0, 0

    if output:
        first_mesh.plot(
            path.join(dirname(__file__), 'output', 'multiple_t1_before.pdf'),
            color_by_global_id=True,
            total_number_of_global_ids=first_mesh.get_num_elements())
        second_mesh.plot(
            path.join(dirname(__file__), 'output', 'multiple_t1_after.pdf'),
            color_by_global_id=True,
            total_number_of_global_ids=first_mesh.get_num_elements())

    print 'evaluate tracking'
    tracking_success, number_tracked_cells, number_incorrectly_tracked_cells = tracking.evaluate_tracking(
        first_mesh, second_mesh, ground_truth)

    percentage_correctly_tracked_cells = float(
        number_tracked_cells) / first_mesh.get_num_elements()
    percentage_incorrectly_tracked_cells = float(
        number_incorrectly_tracked_cells) / first_mesh.get_num_elements()

    return int(
        tracking_success
    ), percentage_correctly_tracked_cells, percentage_incorrectly_tracked_cells