def load_dms(removed_nodes: List[int],
             save_info: sl.MemoryAccess,
             num_iterations: int,
             use_specific_iter: int = None):
    if num_iterations == 1:
        assert (use_specific_iter is not None)
        if save_info.has_distance_matrix(removed_nodes=removed_nodes,
                                         iteration=use_specific_iter):
            yield save_info.load_distance_matrix(removed_nodes=removed_nodes,
                                                 iteration=use_specific_iter)
        else:
            emb = save_info.load_embedding(removed_nodes=removed_nodes,
                                           iteration=use_specific_iter)
            dm = cdm.calc_distances(model=emb,
                                    save_info=save_info,
                                    removed_nodes=removed_nodes,
                                    iteration=use_specific_iter)
            yield dm
    else:
        for i in range(num_iterations):
            if save_info.has_distance_matrix(removed_nodes=removed_nodes,
                                             iteration=i):
                yield save_info.load_distance_matrix(
                    removed_nodes=removed_nodes, iteration=i)
            else:
                emb = save_info.load_embedding(removed_nodes=removed_nodes,
                                               iteration=i)
                dm = cdm.calc_distances(model=emb,
                                        save_info=save_info,
                                        removed_nodes=removed_nodes,
                                        iteration=i)
                yield dm
Exemple #2
0
def __calc_dm(graph: gc.Graph, removed_nodes: [int],
              save_info: sl.MemoryAccess, i: int) -> (int, pd.DataFrame):
    if save_info.has_distance_matrix(removed_nodes=removed_nodes, iteration=i):
        # print("Distance matrix for removed nodes", removed_nodes, "and iteration", i, "is already trained")
        return i, save_info.load_distance_matrix(removed_nodes=removed_nodes,
                                                 iteration=i)
    else:
        # print(f'Calculate distance matrix for removed nodes {removed_nodes} iteration {i}')
        # model = embedding_function(graph=graph, save_info=save_info, removed_nodes=removed_nodes, iteration=i)
        # thows error if embedding does not exist

        model = save_info.load_embedding(removed_nodes=removed_nodes,
                                         iteration=i)
        dm = cd.calc_distances(model=model,
                               graph=graph,
                               save_info=save_info,
                               removed_nodes=removed_nodes,
                               iteration=i,
                               save=False)

        save_info.save_distance_matrix(removed_nodes=removed_nodes,
                                       iteration=i,
                                       dm=dm)

        return i, dm
Exemple #3
0
def calc_distances(model,
                   save_info: sl.MemoryAccess,
                   removed_nodes: [int],
                   iteration: int,
                   graph: gc.Graph = None,
                   save: bool = True,
                   check_for_existing: bool = True):
    if check_for_existing and save_info.has_distance_matrix(
            removed_nodes, iteration):
        return save_info.load_distance_matrix(removed_nodes, iteration)

    dm = calc_distances_based_on_gensim_fast(model=model)
    """
    if type(model) == pd.DataFrame:
        dm = __calc_distances_based_on_df(embedding=model)
    else:
        dm = calc_distances_based_on_gensim_fast(model=model)
        # dm = __calc_distances_based_on_gensim(model=model, node_names=node_names)
    """

    if save:
        save_info.save_distance_matrix(removed_nodes, iteration, dm)

    return dm