Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# Create the graphs with SKLGraph module (available on Eric Debreuve Gitlab)
for soma in somas:
print(f" Soma {soma.uid}", end="")
# Create SKLGraph skeletonized map
ext_map = skl_map_t.FromShapeMap(ext_nfo['lmp_soma'] == soma.uid,
store_widths=True,
skeletonize=False,
do_post_thinning=False)
# Create the graph from the SKLGaph skeletonized map
soma.skl_graph = skl_graph_t.FromSkeleton(ext_map, end_point, size_voxel=size_voxel_in_micron)
# --- Find the root of the {ext+conn} graphs.
# Roots are the nodes of degree 1 that are to be linked to the soma
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)
if save_images is not None:
nx_.draw_networkx(soma.skl_graph)
pl_.savefig(f"{save_images}\\graph_{name_file}_soma{soma.uid}.png")
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
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")
features_df.to_csv(f"{save_images}\\{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
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():
# Perform NutriMorph algorithm
features_df = NutriMorph(path)
# Concatenate all the dataframes
concatenated_features_df = concatenated_features_df.append(features_df)
# If some rows (ie. somas0) have NaN, drop them
# -- due to best fitting ellipsoid algo (JTJ is singular due to convex hull being flat)
concatenated_features_df.dropna()
## TODO /!\ Still errors in the graph = some extensions are tangent to each other.
# Verify Dilatation and Erosion!
# Save to .csv in the parent repository
concatenated_features_df.to_csv(f"{data_path}\\features.csv")
# --- TODO Clustering with this df and module features_analysis.py
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
## -- 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)
## -- PCA with all the features with the cluster as label
## 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)