Skip to content

Commit

Permalink
Update 'graphSOPHYSM' functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
niccolo99mandelli committed Oct 8, 2023
1 parent 74d6cae commit d0cc556
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
44 changes: 40 additions & 4 deletions src/graphManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ _colon(I::CartesianIndex{N}, J::CartesianIndex{N}) where N =
CartesianIndices(map((i,j) -> i:j, Tuple(I), Tuple(J)))

"""
region_adjacency_graph_JHistint(s::SegmentedImage, weight_fn::Function)
region_adjacency_graph_JHistint(s::SegmentedImage, weight_fn::Function,
min_threshold::Float32, max_threshold::Float32)
Constructs a region adjacency graph (RAG) from the `SegmentedImage`. It returns
the RAGalong with a Dict(label=>vertex) map and a dataframe containing the
Expand All @@ -59,7 +60,8 @@ including their identifiers, positions, colors, and areas.
weight_fn(label1, label2): Returns a real number corresponding to the weight of
the edge between label1 and label2.
"""
function region_adjacency_graph_JHistint(s::SegmentedImage, weight_fn::Function)
function region_adjacency_graph_JHistint(s::SegmentedImage, weight_fn::Function,
min_threshold::Float32, max_threshold::Float32)

function neighbor_regions!(df_cartesian_indices::AbstractArray,
G::SimpleWeightedGraph,
Expand Down Expand Up @@ -145,19 +147,53 @@ function region_adjacency_graph_JHistint(s::SegmentedImage, weight_fn::Function)
df_cartesian_indices_filtered = CartesianIndex[]
df_color_indices_filtered = []
df_area_filtered = Int[]

df_label_filtered_extra = Int[]
df_cartesian_indices_filtered_extra = CartesianIndex[]
df_color_indices_filtered_extra = []
df_area_filtered_extra = Int[]

df_label_total = Int[]
df_cartesian_indices_total = CartesianIndex[]
df_color_indices_total = []
df_area_total = Int[]
for i in 1:length(s.segment_labels)
if(s.segment_pixel_count[i] > 3000)
if(s.segment_pixel_count[i] > max_threshold)
push!(df_label_filtered, s.segment_labels[i])
push!(df_cartesian_indices_filtered, df_cartesian_indices[i])
push!(df_color_indices_filtered, df_color_indices[i])
push!(df_area_filtered, s.segment_pixel_count[i])
end
if(s.segment_pixel_count[i] <= max_threshold &&
s.segment_pixel_count[i] > min_threshold)
push!(df_label_filtered_extra, s.segment_labels[i])
push!(df_cartesian_indices_filtered_extra, df_cartesian_indices[i])
push!(df_color_indices_filtered_extra, df_color_indices[i])
push!(df_area_filtered_extra, s.segment_pixel_count[i])
end
if(s.segment_pixel_count[i] > min_threshold)
push!(df_label_total, s.segment_labels[i])
push!(df_cartesian_indices_total, df_cartesian_indices[i])
push!(df_color_indices_total, df_color_indices[i])
push!(df_area_total, s.segment_pixel_count[i])
end
end
df_label.label = df_label_filtered
df_label.position_label = df_cartesian_indices_filtered
df_label.color_label = df_color_indices_filtered
df_label.area = df_area_filtered
G, vert_map, df_label

df_noisy_label.label = df_label_filtered_extra
df_noisy_label.position_label = df_cartesian_indices_filtered_extra
df_noisy_label.color_label = df_color_indices_filtered_extra
df_noisy_label.area = df_area_filtered_extra

df_total_label.label = df_label_total
df_total_label.position_label = df_cartesian_indices_total
df_total_label.color_label = df_color_indices_total
df_total_label.area = df_area_total

G, vert_map, df_label, df_noisy_label, df_total_label
end


Expand Down
2 changes: 1 addition & 1 deletion src/noiseManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function compute_centroid_total_cells(s::SegmentedImage,
end
end
# manual threshold for cells dimension in pixel (delete noise)
if(s.segment_pixel_count[s.image_indexmap[p]] > minima_threshold)
if(s.segment_pixel_count[s.image_indexmap[p]] > min_threshold)
centroid = div(s.segment_pixel_count[s.image_indexmap[p]], 2)
if(!(df_cells_indices[centroid] in df_centroids))
push!(df_label_list, s.image_indexmap[p])
Expand Down
9 changes: 7 additions & 2 deletions src/segmentationManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,14 @@ function apply_segmentation_SOPHYSM_graph(filepath_input::AbstractString,
# build dataframe
weight_fn(i,j) = euclidean(segment_pixel_count(segments,i), segment_pixel_count(segments,j))
df_label = DataFrame()
G, vert_map, df_label = region_adjacency_graph_JHistint(segments, weight_fn)
df_noisy_labels = DataFrame()
df_total_labels = DataFrame()
G, vert_map, df_label, df_noisy_labels, df_total_labels = region_adjacency_graph_JHistint(segments, weight_fn, min_threshold, max_threshold)
nvertices = length(vert_map)
df_label = compute_centroid_cells(segments, df_label, max_threshold)
# define centroids
df_total_labels = compute_centroid_total_cells(segments, df_total_labels, min_threshold)
df_label = filter_dataframe_cells(df_total_labels, max_threshold)
# define matrix
matrix = weighted_graph_to_adjacency_matrix(G, nvertices)
filepath_matrix = replace(filepath_output, ".tif" => ".txt")
save_adjacency_matrix(matrix, filepath_matrix)
Expand Down

0 comments on commit d0cc556

Please sign in to comment.