huangziwei / TracePy

A script for processing .trace file from ImageJ / Simple Neurite Tracer.
2 stars 2 forks source link

get_df_paths2 stack a wrong connect_to_at point to the original path #2

Closed huangziwei closed 7 years ago

huangziwei commented 7 years ago
def get_df_paths2(df_trace, soma_info):

    all_paths = get_all_paths(df_trace)
    df_paths = pd.DataFrame(list(all_paths.items()), columns=['path_id', 'path'])
    # df_paths['path2'] = ''

    connect_to_all    = []
    connect_to_at_all = []

    for i, key in enumerate(all_paths.keys()):

        if connected_with_soma(all_paths, key, soma_info, threshold=5):

            connect_to    = -1
            connect_to_at = []
        else:
            connect_to, connect_to_at = get_connect_to(all_paths, key)

        connect_to_all.append(connect_to)
        connect_to_at_all.append(connect_to_at)

    df_paths['connect_to'] = connect_to_all
    df_paths['connect_to_at'] = connect_to_at_all

    df_paths.sort_values(['path_id'], ascending=[True], inplace=True)
    df_paths.index = df_paths.path_id.as_matrix()

    connected_by_all    = []
    connected_by_at_all = []

    for i, key in enumerate(df_paths.path_id):

        connected_by    = df_paths[df_paths.connect_to == key].path_id.tolist()
        connected_by_at = df_paths[df_paths.connect_to == key].connect_to_at.tolist()

        connected_by_all.append(connected_by)
        connected_by_at_all.append(connected_by_at)

    df_paths['connected_by'] = connected_by_all
    df_paths['connected_by_at'] = connected_by_at_all

    path2 = {}
    for path_id in df_paths.path_id:

        connect_to = df_paths.loc[path_id].connect_to
        path = df_paths.loc[path_id].path
        connect_to_at = df_paths.loc[path_id].connect_to_at

        if connect_to == -1:
            path2[path_id] = path

        else:
            if (connect_to_at == path).all(1).any():
                path2[path_id] = path
            else:
                path = np.vstack([connect_to_at, path]) # the bug is here
                path2[path_id] = path

    df_paths['path'] = path2.values()

    return df_paths