diff --git a/connection.py b/connection.py index 0f8794ce565774125bac894d1bba3221015af229..ee0d89b04ba3ff8774b9c6a569a0f82e53ae5247 100644 --- a/connection.py +++ b/connection.py @@ -18,6 +18,8 @@ def CandidateConnections( ) -> list: # candidate_conn_nfo = [] # conn=connection + + extensions = filter(lambda ext: ext.is_unconnected, extensions) for soma, extension in it_.product(somas, extensions): new_candidates = extension.EndPointsForSoma(soma.uid, influence_map) candidate_conn_nfo.extend( diff --git a/extension.py b/extension.py index 8633e4b77c7965cc4aa96656321cf7a11c960610..66db159d454364dfc89bc56ab55dd176859e8153 100644 --- a/extension.py +++ b/extension.py @@ -51,6 +51,11 @@ class extension_t(glial_cmp_t): return instance + @property + def is_unconnected(self) -> bool: + # + return self.soma_uid is None + @property def end_points_as_array(self) -> array_t: # diff --git a/main.py b/main.py index b289d5df07796a99af78b638cc9f95553a7d7b98..435f5ac1155d0c418667e2d2e265e966c7de7acb 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import connection as cn_ import extension as xt_ import feedback as fb_ +from main_prm import * import soma as sm_ import time as tm_ @@ -10,33 +11,14 @@ import numpy as np_ import skimage.color as cl_ import skimage.io as io_ import skimage.measure as ms_ -import skimage.morphology as mp_ - - -print(f"STARTED: {tm_.strftime('%a, %b %d %Y @ %H:%M:%S')}") -start_time = tm_.time() soma_t = sm_.soma_t extension_t = xt_.extension_t -# --- Parameters -data_path = "./DIO_6H_6_1.70bis_2.2_3.tif" -run = ("soma", "extension", "som-ext", "ext-ext") -with_plot = False - -soma_low_c = 0.15 -soma_high_c = 0.7126 -ext_low_c = 0.2 # 0.02 # 0.2 # ext_low_c = 9.0e-4 -ext_high_c = 0.6 # 0.04 # 0.6 # high_ext = 8.0e-3 - -soma_selem_c = mp_.disk(2) - -max_straight_sq_dist_c = 30 ** 2 -max_weighted_length_c = 20.0 - -min_area_c = 1000 +print(f"STARTED: {tm_.strftime('%a, %b %d %Y @ %H:%M:%S')}") +start_time = tm_.time() # --- Images @@ -50,79 +32,73 @@ costs = 1.0 / (image + 1.0) # --- Initialization -n_somas = 0 som_nfo = {} # som=soma, nfo=info -somas = None # Tuple of soma objects - -n_extensions = 0 ext_nfo = {} # ext=extension, nfo=info -extensions = None # Tuple of extension objects - axes = {} +# n_somas = 0 +# n_extensions = 0 +# somas = None # Tuple of soma objects +# extensions = None # Tuple of extension objects + # --- Somas -if "soma" in run: - print("\n--- Soma Detection") +print("\n--- Soma Detection") - som_nfo["map"] = soma_t.Map(image_for_soma, soma_low_c, soma_high_c, soma_selem_c) - som_nfo["map"] = soma_t.FilteredMap(som_nfo["map"], min_area_c) - som_nfo["lmp"], n_somas = ms_.label(som_nfo["map"], return_num=True) - som_nfo["dist_to_closest"], som_nfo["influence_map"] = soma_t.InfluenceMaps( - som_nfo["lmp"] - ) +som_nfo["map"] = soma_t.Map(image_for_soma, soma_low_c, soma_high_c, soma_selem_c) +som_nfo["map"] = soma_t.FilteredMap(som_nfo["map"], min_area_c) +som_nfo["lmp"], n_somas = ms_.label(som_nfo["map"], return_num=True) +som_nfo["dist_to_closest"], som_nfo["influence_map"] = soma_t.InfluenceMaps( + som_nfo["lmp"] +) - somas = tuple( - soma_t().FromMap(som_nfo["lmp"], uid) for uid in range(1, n_somas + 1) - ) +somas = tuple( + soma_t().FromMap(som_nfo["lmp"], uid) for uid in range(1, n_somas + 1) +) - print(f" n = {n_somas}") - if with_plot: - fb_.PlotSomas(somas, som_nfo, axes) +print(f" n = {n_somas}") +if with_plot: + fb_.PlotSomas(somas, som_nfo, axes) # -- Extentions -if "extension" in run: - print("\n--- Extension Detection") - - enhanced_ext, ext_scales = extension_t.EnhancedForDetection(image_for_ext) - ext_nfo["coarse_map"] = extension_t.CoarseMap(enhanced_ext, ext_low_c, ext_high_c) - ext_nfo["coarse_map"] = extension_t.FilteredCoarseMap(ext_nfo["coarse_map"]) - ext_nfo["map"] = extension_t.FineMapFromCoarseMap(ext_nfo["coarse_map"]) - ext_nfo["map"][som_nfo["map"] > 0] = 0 - ext_nfo["lmp"], n_extensions = ms_.label(ext_nfo["map"], return_num=True) - - extensions = tuple( - extension_t().FromMap(ext_nfo["lmp"], ext_scales, uid) - for uid in range(1, n_extensions + 1) - ) +print("\n--- Extension Detection") - print(f" n = {n_extensions}") - if with_plot: - fb_.PlotExtensions(extensions, ext_nfo, img_shape) +enhanced_ext, ext_scales = extension_t.EnhancedForDetection(image_for_ext) +ext_nfo["coarse_map"] = extension_t.CoarseMap(enhanced_ext, ext_low_c, ext_high_c) +ext_nfo["coarse_map"] = extension_t.FilteredCoarseMap(ext_nfo["coarse_map"]) +ext_nfo["map"] = extension_t.FineMapFromCoarseMap(ext_nfo["coarse_map"]) +ext_nfo["map"][som_nfo["map"] > 0] = 0 +ext_nfo["lmp"], n_extensions = ms_.label(ext_nfo["map"], return_num=True) +extensions = tuple( + extension_t().FromMap(ext_nfo["lmp"], ext_scales, uid) + for uid in range(1, n_extensions + 1) +) -# -- Soma-Extention -if "som-ext" in run: - print("\n--- Soma <-> Extension") - - # TODO: Ideally, the extension part should be dilated - # but in ext-ext connections, there must not be dilation around the current ext - # (current ext plays the role of a soma in soma-ext step) - costs[np_.logical_or(som_nfo["map"] > 0, ext_nfo["map"] > 0)] = np_.inf - - candidate_conn_nfo = cn_.CandidateConnections( - somas, - som_nfo["influence_map"], - som_nfo["dist_to_closest"], - extensions, - max_straight_sq_dist_c, - ) +print(f" n = {n_extensions}") +if with_plot: + fb_.PlotExtensions(extensions, ext_nfo, img_shape) - for ep_idx, soma, extension, end_point in candidate_conn_nfo: - if extension.soma_uid is not None: - continue +# -- Soma-Extention +print("\n--- Soma <-> Extension") + +# TODO: Ideally, the extension part should be dilated +# but in ext-ext connections, there must not be dilation around the current ext +# (current ext plays the role of a soma in soma-ext step) +costs[np_.logical_or(som_nfo["map"] > 0, ext_nfo["map"] > 0)] = np_.inf + +candidate_conn_nfo = cn_.CandidateConnections( + somas, + som_nfo["influence_map"], + som_nfo["dist_to_closest"], + extensions, + max_straight_sq_dist_c, +) + +for ep_idx, soma, extension, end_point in candidate_conn_nfo: + if extension.is_unconnected: print(f" Soma.{soma.uid} <-?-> Ext.{extension.uid}({ep_idx})", end="") path, length = cn_.ShortestPathTo( end_point, @@ -136,41 +112,35 @@ if "som-ext" in run: else: print("") - # for soma in somas: - # soma.Extend(extensions, som_nfo["dist_to_closest"], costs) +# for soma in somas: +# soma.Extend(extensions, som_nfo["dist_to_closest"], costs) - fb_.PrintConnectedExtensions(extensions) - if with_plot: - fb_.PlotSomasWithExtensions(somas, som_nfo, "all") +fb_.PrintConnectedExtensions(extensions) +if with_plot: + fb_.PlotSomasWithExtensions(somas, som_nfo, "all") # -- Extention-Extention -if "ext-ext" in run: - print("\n--- Extension <-> Extension") - - should_look_for_connections = True - while should_look_for_connections: - som_nfo["soma_w_ext_lmp"] = soma_t.SomasWithExtensionsLMap(somas) - som_nfo["dist_to_closest"], som_nfo["influence_map"] = soma_t.InfluenceMaps( - som_nfo["soma_w_ext_lmp"] - ) - unconnected_extensions = list( - filter(lambda ext: ext.soma_uid is None, extensions) - ) +print("\n--- Extension <-> Extension") - candidate_conn_nfo = cn_.CandidateConnections( - somas, - som_nfo["influence_map"], - som_nfo["dist_to_closest"], - unconnected_extensions, - max_straight_sq_dist_c, - ) +should_look_for_connections = True +while should_look_for_connections: + som_nfo["soma_w_ext_lmp"] = soma_t.SomasWithExtensionsLMap(somas) + som_nfo["dist_to_closest"], som_nfo["influence_map"] = soma_t.InfluenceMaps( + som_nfo["soma_w_ext_lmp"] + ) - should_look_for_connections = False - for ep_idx, soma, extension, end_point in candidate_conn_nfo: - if extension.soma_uid is not None: - continue + candidate_conn_nfo = cn_.CandidateConnections( + somas, + som_nfo["influence_map"], + som_nfo["dist_to_closest"], + extensions, + max_straight_sq_dist_c, + ) + should_look_for_connections = False + for ep_idx, soma, extension, end_point in candidate_conn_nfo: + if extension.is_unconnected: print(f" Soma.{soma.uid} <-?-> Ext.{extension.uid}({ep_idx})", end="") path, length = cn_.ShortestPathTo( end_point, @@ -186,9 +156,9 @@ if "ext-ext" in run: else: print("") - fb_.PrintConnectedExtensions(extensions) - if with_plot: - fb_.PlotSomasWithExtensions(somas, som_nfo, "with_ext_of_ext") +fb_.PrintConnectedExtensions(extensions) +if with_plot: + fb_.PlotSomasWithExtensions(somas, som_nfo, "with_ext_of_ext") # -- Summary diff --git a/main_prm.py b/main_prm.py new file mode 100644 index 0000000000000000000000000000000000000000..80c03a06f02f42dfbb0ed8102faed514cf4772a1 --- /dev/null +++ b/main_prm.py @@ -0,0 +1,17 @@ +import skimage.morphology as mp_ + + +data_path = "./DIO_6H_6_1.70bis_2.2_3.tif" +with_plot = False + +soma_low_c = 0.15 +soma_high_c = 0.7126 +ext_low_c = 0.2 # 0.02 # 0.2 # ext_low_c = 9.0e-4 +ext_high_c = 0.6 # 0.04 # 0.6 # high_ext = 8.0e-3 + +soma_selem_c = mp_.disk(2) + +max_straight_sq_dist_c = 30 ** 2 +max_weighted_length_c = 20.0 + +min_area_c = 1000 diff --git a/soma.py b/soma.py index af24ece1c9edb1ab6981a085e5adf1a7a0c25002..7b81978602aefb8c957720c1682d33b477c4bb08 100644 --- a/soma.py +++ b/soma.py @@ -158,6 +158,7 @@ class soma_t(glial_cmp_t): return ( f"Soma.{self.uid}, " + f"area={self.sites[0].__len__()}, " f"contour points={self.contour_points.__len__()}, " f"extensions={n_extensions}" ) @@ -227,7 +228,7 @@ class soma_t(glial_cmp_t): return dist_map, np_.array(map_[tuple(idx_map)]) @staticmethod - def SomasWithExtensionsLMap(somas: Sequence[soma_t]) -> array_t: + def SomasLMap(somas: Sequence[soma_t], with_extensions: bool = True) -> array_t: # shape = somas[0].img_shape result = np_.zeros(shape, dtype=np_.int64) @@ -235,16 +236,18 @@ class soma_t(glial_cmp_t): for soma in somas: result[soma.sites] = soma.uid - for connection_path in filter( - lambda path: path is not None, soma.connection_path.values() - ): - result[connection_path] = soma.uid - for extension in soma.Extensions(): + if with_extensions: for connection_path in filter( - lambda path: path is not None, extension.connection_path.values() + lambda path: path is not None, soma.connection_path.values() ): result[connection_path] = soma.uid - result[extension.sites] = soma.uid + for extension in soma.Extensions(): + for connection_path in filter( + lambda path: path is not None, + extension.connection_path.values(), + ): + result[connection_path] = soma.uid + result[extension.sites] = soma.uid return result