Mentions légales du service

Skip to content
Snippets Groups Projects
nutrimorph.py 43 KiB
Newer Older

        # Add a node "soma" and link it to the root nodes
        soma_node = f"S-{int(soma.centroid[0])}-{int(soma.centroid[1])}-{int(soma.centroid[2])}"
        soma.skl_graph.add_node(soma_node, soma=True, soma_nfo=soma)

        for node in soma.graph_roots.values():
            soma.skl_graph.add_edge(node, soma_node, root=True)

        # nx_.draw_networkx(soma.skl_graph)
        # pl_.show(block=True)
        # pl_.close()

        print(": Done")

    elapsed_time = tm_.gmtime(tm_.time() - start_time)
    print(f"\nElapsed Time={tm_.strftime('%Hh %Mm %Ss', elapsed_time)}")

    # --- Extract features
    print('\n--- Features Extraction\n')

    # Parameters
    if hist_bins_borders_length is None:
        number_of_bins_length = int(number_of_bins_length)
        bins_length = np_.linspace(hist_min_length, hist_min_length + hist_step_length * number_of_bins_length,
                                   num=number_of_bins_length)
        bins_length[-1] = np_.inf
    else:
        bins_length = np_.array(hist_bins_borders_length)
        bins_length[-1] = np_.inf

    if hist_bins_borders_curvature is None:
        number_of_bins_curvature = int(number_of_bins_curvature)
        bins_curvature = np_.linspace(hist_min_curvature,
                                      hist_min_curvature + hist_step_curvature * number_of_bins_curvature,
                                      num=number_of_bins_curvature)
        bins_curvature[-1] = np_.inf
    else:
        bins_curvature = np_.array(hist_bins_borders_curvature)
        bins_curvature[-1] = np_.inf

    # Pandas dataframe creation with all the measured features
    features_df = ge_.ExtractFeaturesInDF(name_file, somas, size_voxel_in_micron, bins_length, bins_curvature,
                                          ext_scales)

    # Save the pandas df into .csv
    features_df.to_csv(f"{name_dir}\\{name_file}.csv")

    #
    elapsed_time = tm_.gmtime(tm_.time() - start_time)
    print(f"\nElapsed Time={tm_.strftime('%Hh %Mm %Ss', elapsed_time)}")
    print(f"DONE: {tm_.strftime('%a, %b %d %Y @ %H:%M:%S')}\n")

    return features_df


if __name__ == '__main__':

    # --- Extract cell graphs and features from microscope images using NutriMorph function.
    #
    # Differentiate between path to a tiff file or to a repository
    if pathlib.Path(data_path).is_file():
        # Perform NutriMorph algorithm on the file entered in parameters
        features_df = NutriMorph(data_path)

    elif pathlib.Path(data_path).is_dir():
        # Keep the directory to the repository
        name_dir = os_.path.dirname(data_path)
        # Initialize the future concatenated features
        concatenated_features_df = pd_.DataFrame()
        # Find all the tiff files in the parent and child repositories
        for path in pathlib.Path(data_path).glob("**/*.tif"):
            if path.is_file():
                name_file = os_.path.basename(path)
                try:
                    # Perform NutriMorph algorithm
                    features_df = NutriMorph(path)
                    # Concatenate all the dataframes
                    concatenated_features_df = concatenated_features_df.append(features_df)
                except:
                    print(f"WARNING: Error in the running of NutriMorph on {name_file}")
        # Save to .csv in the parent repository
        concatenated_features_df.to_csv(f"{data_path}_features.csv")

    # --- TODO Clustering with this df and module cell_clustering.py