Skip to content

Commit e4c820b

Browse files
anna-grimanna-grim
andauthored
doc: skeleton metrics and swc_loading (#157)
Co-authored-by: anna-grim <[email protected]>
1 parent 6d9dfd0 commit e4c820b

File tree

5 files changed

+395
-21
lines changed

5 files changed

+395
-21
lines changed

src/segmentation_skeleton_metrics/data_handling/graph_loading.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def load_groundtruth(self, swc_pointer, label_mask):
6565
label_handler=self.label_handler,
6666
label_mask=label_mask,
6767
use_anisotropy=False,
68+
verbose=self.verbose
6869
)
6970
return graph_loader.run(swc_pointer)
7071

@@ -99,6 +100,7 @@ def load_fragments(self, swc_pointer, gt_graphs):
99100
label_handler=self.label_handler,
100101
selected_ids=selected_ids,
101102
use_anisotropy=self.use_anisotropy,
103+
verbose=self.verbose
102104
)
103105
return graph_loader.run(swc_pointer)
104106

@@ -110,6 +112,7 @@ def get_all_node_labels(self, graphs):
110112
Parameters
111113
----------
112114
graphs : Dict[str, SkeletonGraph]
115+
Graph to be searched.
113116
114117
Returns
115118
-------
@@ -135,6 +138,7 @@ def __init__(
135138
label_mask=None,
136139
selected_ids=None,
137140
use_anisotropy=True,
141+
verbose=True
138142
):
139143
"""
140144
Instantiates a GraphLoader object.
@@ -156,12 +160,15 @@ def __init__(
156160
Indication of whether coordinates in SWC files should be converted
157161
from physical to image coordinates using the given anisotropy.
158162
Default is True.
163+
verbose : bool, optional
164+
Indication of whether to display a progress bar. Default is True.
159165
"""
160166
# Instance attributes
161167
self.anisotropy = anisotropy
162168
self.is_groundtruth = is_groundtruth
163169
self.label_handler = label_handler
164170
self.label_mask = label_mask
171+
self.verbose = verbose
165172

166173
# Reader
167174
anisotropy = anisotropy if use_anisotropy else (1.0, 1.0, 1.0)
@@ -210,17 +217,19 @@ def _build_graphs_from_swcs(self, swc_pointer):
210217
"""
211218
# Initializations
212219
swc_dicts = self.swc_reader.read(swc_pointer)
213-
pbar = tqdm(total=len(swc_dicts), desc="Build Graphs")
220+
if self.verbose:
221+
pbar = tqdm(total=len(swc_dicts), desc="Build Graphs")
214222

215223
# Main
216224
graph_dict = dict()
217225
if len(swc_dicts) > 10 ** 4:
218226
while len(swc_dicts) > 0:
219227
swc_dict = swc_dicts.pop()
220228
graph_dict.update(self.to_graph(swc_dict))
221-
pbar.update(1)
229+
if self.verbose:
230+
pbar.update(1)
222231
else:
223-
with ProcessPoolExecutor() as executor:
232+
with ProcessPoolExecutor(max_workers=1) as executor:
224233
# Assign processes
225234
processes = list()
226235
while len(swc_dicts) > 0:
@@ -230,7 +239,8 @@ def _build_graphs_from_swcs(self, swc_pointer):
230239
# Store results
231240
for process in as_completed(processes):
232241
graph_dict.update(process.result())
233-
pbar.update(1)
242+
if self.verbose:
243+
pbar.update(1)
234244
return graph_dict
235245

236246
def to_graph(self, swc_dict):

src/segmentation_skeleton_metrics/data_handling/swc_loading.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,20 @@ def read_from_gcs_zip(self, bucket_name, path):
429429
return swc_dicts
430430

431431
def read_from_s3(self, s3_path):
432+
"""
433+
Reads and parses SWC files from an S3 directory.
434+
435+
Parameters
436+
----------
437+
s3_path : str
438+
Path to a directory in an S3 bucket containing SWC files.
439+
440+
Returns
441+
-------
442+
Dequeue[dict]
443+
Dictionaries whose keys and values are the attribute
444+
names and values from an SWC file.
445+
"""
432446
# List filenames
433447
bucket_name, prefix = util.parse_cloud_path(s3_path)
434448
swc_paths = util.list_s3_paths(bucket_name, prefix, extension=".swc")

src/segmentation_skeleton_metrics/evaluate.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,22 @@ def init_results(self, gt_graphs):
161161
return results
162162

163163
def report_summary(self, results):
164-
pass
164+
# Averaged results
165+
filename = f"{self.results_filename}-overview.txt"
166+
path = os.path.join(self.output_dir, filename)
167+
util.update_txt(path, "Average Results...")
168+
for column in results.columns:
169+
if column != "SWC Run Length" and column != "SWC Name":
170+
avg = util.compute_weighted_avg(results, column)
171+
util.update_txt(path, f" {column}: {avg:.4f}")
172+
173+
# Total results
174+
n_splits = results["# Splits"].sum()
175+
util.update_txt(path, "\nTotal Results...")
176+
util.update_txt(path, f" # Splits: {n_splits}")
177+
if "# Merges" in results.columns:
178+
n_merges = results["# Merges"].sum()
179+
util.update_txt(path, f" # Merges: {n_merges}")
165180

166181
# --- Writers ---
167182
def save_fragments(self):

0 commit comments

Comments
 (0)