Mentions légales du service

Skip to content
Snippets Groups Projects
nutrimorph.py 45.8 KiB
Newer Older
        soma.graph_roots = ge_.FindGraphsRootWithNodes(soma)

        # 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)

NADAL Morgane's avatar
NADAL Morgane committed
        if save_images is not None:
NADAL Morgane's avatar
NADAL Morgane committed
            nx_.draw_networkx(soma.skl_graph)
NADAL Morgane's avatar
NADAL Morgane committed
            pl_.savefig(f"{save_images}\\graph_{name_file}_soma{soma.uid}.png")
NADAL Morgane's avatar
NADAL Morgane committed
            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
NADAL Morgane's avatar
NADAL Morgane committed
        print("WARNING: Will not perform features analysis on a single image.\n For features analysis, "
              "give a directory path.\n")
        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:
NADAL Morgane's avatar
NADAL Morgane committed
                    ## TODO /!\ Still errors in the graph = some extensions are tangent to each other.
                    #       Verify Dilatation and Erosion!
                    ## TODO + Error in best fitting ellipsoid : JTJ is singular.
                    print(f"WARNING: Error in the running of NutriMorph on {name_file}")
        # Save to .csv in the parent repository
NADAL Morgane's avatar
NADAL Morgane committed
        concatenated_features_df.to_csv(f"{data_path}\\features.csv")

            # --- TODO Clustering with this df and module features_analysis.py
NADAL Morgane's avatar
NADAL Morgane committed
        # if statistical_analysis:
NADAL Morgane's avatar
NADAL Morgane committed
            ## -- K-means with all the features (2 conditions)

            ## Between the two conditions, regardless the duration of experiment (2 conditions, all durations)
            # kmeans = fa_.KmeansOnDF(concatenated_features_df, nb_clusters=(2,), elbow=True, intracluster_var=True)

            ## Between the two conditions, for each duration (2 conditions, 3 durations)
            # groupby_duration = concatenated_features_df.groupby("Duration")
            # for duration, values in groupby_duration:
                # kmeans = fa_.KmeansOnDF(values, nb_clusters=(2,), elbow=True, intracluster_var=True)

            ## -- PCA with all the features

            ## Between the two conditions, regardless the duration of experiment (2 conditions, all durations)
            # TODO pca = fa_.PCAOnDF(concatenated_features_df)

            ## Between the two conditions, for each duration (2 conditions, 3 durations)
            # for duration, values in groupby_duration:
                # pca = fa_.PCAOnDF(values)

            ## -- Select Discriminant features by statistical analysis
            # TODO filtered_df = SelectFeatures(concatenated_features_df)

            ## -- K-means with selected features

            ## Between the two conditions, regardless the duration of experiment (2 conditions, all durations)
            # filtered_kmeans = fa_.KmeansOnDF(filtered_df, nb_clusters=(2,), elbow=True, intracluster_var=True)

            ## Between the two conditions, for each duration (2 conditions, 3 durations)
            # filtered_groupby_duration = filtered_df.groupby("Duration")
            # for duration, values in filtered_groupby_duration:
                # filtered_kmeans = fa_.KmeansOnDF(values, nb_clusters=(2,), elbow=True, intracluster_var=True)

            ## -- PCA with selected features

            ## Between the two conditions, regardless the duration of experiment (2 conditions, all durations)
            # TODO pca = fa_.PCAOnDF(filtered_df)

            ## Between the two conditions, for each duration (2 conditions, 3 durations)
            # for duration, values in filtered_groupby_duration:
                # pca = fa_.PCAOnDF(values)