Skip to content

Commit

Permalink
Fixed case where skeleton is 1 pixel or less
Browse files Browse the repository at this point in the history
  • Loading branch information
c-h-benedetti committed Jan 13, 2025
1 parent ad50c04 commit b68e2e1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/microglia_analyzer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.0"
__version__ = "1.0.1"

import re

Expand Down
20 changes: 13 additions & 7 deletions src/microglia_analyzer/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self):

self.sources_folder = None
self.active_worker = False
self.n_images = 0

# -------- UI: ----------------------------------

Expand Down Expand Up @@ -228,8 +229,8 @@ def measures_panel(self):
self.export_measures_button.clicked.connect(self.export_measures)
layout.addWidget(self.export_measures_button)

self.run_batch_button = QPushButton("☑️ Run batch")
self.run_batch_button.setFont(self.font)
self.run_batch_button = QPushButton(" Run batch")
# self.run_batch_button.setFont(self.font)
self.run_batch_button.clicked.connect(self.run_batch)
layout.addWidget(self.run_batch_button)

Expand Down Expand Up @@ -330,9 +331,10 @@ def export_measures(self):
self.thread.start()

def run_batch(self):
# self.total = len(self.get_all_tiff_files(self.sources_folder))
self.n_images = len(self.get_all_tiff_files(self.sources_folder))
self.pbr = progress()
self.pbr.set_description("Running on folder...")
self.run_batch_button.setText(f"▶ Run batch ({str(1).zfill(2)}/{self.n_images})")
self.set_active_ui(False)
self.thread = QThread()

Expand Down Expand Up @@ -360,6 +362,8 @@ def run_batch(self):
def end_batch(self):
self.pbr.close()
self.set_active_ui(True)
self.n_images = 0
self.run_batch_button.setText("▶ Run batch")
show_info("Batch completed.")

def write_measures(self):
Expand Down Expand Up @@ -452,10 +456,12 @@ def end_worker(self):

def update_pbr(self, text, current, total):
self.pbr.set_description(text)
if (total != self.total):
self.pbr.reset(total=total)
self.total = total
self.pbr.update(current)
# if (total != self.total):
# self.pbr.reset(total=total)
# self.total = total
if (self.n_images > 0):
self.run_batch_button.setText(f"▶ Run batch ({str(current+1).zfill(2)}/{self.n_images})")
# self.pbr.update(current)

def clear_attributes(self):
self.sources_folder = None
Expand Down
27 changes: 2 additions & 25 deletions src/microglia_analyzer/ma_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,21 +309,6 @@ def classification_postprocessing(self):
used.add(i)
self.classifications = clean_boxes

def _bind_classifications(self):
labeled = self.mask
regions = regionprops(labeled)
bindings = {int(l): (None, 0.0, None) for l in np.unique(labeled) if l != 0} # label: (class, IoU)
for region in regions:
seg_bbox = list(map(int, region.bbox))
bindings[region.label] = (0, 0.0, seg_bbox)
for box, cls in zip(self.classifications['boxes'], self.classifications['classes']):
x1, y1, x2, y2 = list(map(int, box))
detect_bbox = [y1, x1, y2, x2]
iou = calculate_iou(seg_bbox, detect_bbox)
if iou > bindings[region.label][1]: # iou > 0.2 and
bindings[region.label] = (cls, iou, seg_bbox)
self.bindings = bindings

def bind_classifications(self):
labeled = self.mask
regions = regionprops(labeled)
Expand Down Expand Up @@ -381,20 +366,12 @@ def analyze_as_graph(self):
if label == 0:
continue
mask = (self.mask == label).astype(np.uint8)
if np.sum(mask) < 3:
continue
results[label], skeleton = self.analyze_skeleton(mask)
skeletons = np.maximum(skeletons, skeleton)
self.graph_metrics = results
self.skeleton = skeletons

def sorted_by_class(self, bindings, common_labels):
sorted_bindings = {}
for label, (cls, iou, seg_bbox) in bindings.items():
if label not in common_labels:
continue
if cls not in sorted_bindings:
sorted_bindings[cls] = []
sorted_bindings[cls].append((label, iou, seg_bbox))
return sorted_bindings

def sort_labels_by_class(self, data, valid_labels):
filtered = {label: value for label, value in data.items() if label in valid_labels}
Expand Down

0 comments on commit b68e2e1

Please sign in to comment.