Skip to content

Commit 5b46b24

Browse files
anna-grimanna-grim
andauthored
Refactor general updates (#147)
* refactor: improved metric defaults * refactor: improved NaN metric handling --------- Co-authored-by: anna-grim <[email protected]>
1 parent e51df70 commit 5b46b24

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

src/segmentation_skeleton_metrics/skeleton_metric.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ def run(self):
301301
path = f"{self.output_dir}/{prefix}results.csv"
302302
if self.fragment_graphs is None:
303303
self.metrics = self.metrics.drop("# Merges", axis=1)
304+
self.metrics = self.metrics.drop("Merge Rate", axis=1)
304305
self.metrics.to_csv(path, index=False)
305306

306307
# Report results
@@ -337,28 +338,39 @@ def detect_splits(self):
337338

338339
# Store results
339340
for process in as_completed(pending.keys()):
341+
# Update graph
340342
key = pending.pop(process)
341-
self.graphs[key], n_split_edges = process.result()
342-
n_before = self.graphs[key].graph["n_initial_edges"]
343-
n_after = self.graphs[key].number_of_edges()
344-
345-
n_missing_edges = n_before - n_after
346-
n_splits = gutil.count_splits(self.graphs[key])
347-
p_omit = 100 * (n_missing_edges + n_split_edges) / n_before
348-
p_split = 100 * n_split_edges / n_before
349-
rl = np.sum(self.graphs[key].run_lengths())
350-
gt_rl = self.graphs[key].run_length
351-
split_rate = rl / n_splits if n_splits > 0 else np.nan
352-
353-
self.metrics.at[key, "# Splits"] = n_splits
354-
self.metrics.at[key, "Split Rate"] = split_rate
355-
self.metrics.loc[key, "% Split Edges"] = round(p_split, 2)
356-
self.metrics.at[key, "% Omit Edges"] = round(p_omit, 2)
357-
self.metrics.loc[key, "SWC Run Length"] = round(gt_rl, 2)
343+
graph, n_split_edges = process.result()
344+
self.graphs[key] = graph
358345

346+
# Compute metrics
347+
self.compute_split_metrics(key, n_split_edges)
359348
if self.verbose:
360349
pbar.update(1)
361350

351+
def compute_split_metrics(self, key, n_split_edges):
352+
# Edge counts
353+
n_edges_before = self.graphs[key].graph["n_initial_edges"]
354+
n_edges_after = self.graphs[key].number_of_edges()
355+
n_edge_missing = n_edges_before - n_edges_after
356+
357+
# Split statistics
358+
n_splits = gutil.count_splits(self.graphs[key])
359+
p_omit = 100 * (n_edge_missing + n_split_edges) / n_edges_before
360+
p_split = 100 * n_split_edges / n_edges_before
361+
362+
# Run lengths
363+
rl = np.sum(self.graphs[key].run_lengths())
364+
gt_rl = self.graphs[key].run_length
365+
split_rate = rl / n_splits if n_splits > 0 else np.nan
366+
367+
# Update metrics
368+
self.metrics.at[key, "# Splits"] = n_splits
369+
self.metrics.at[key, "Split Rate"] = split_rate
370+
self.metrics.at[key, "% Split Edges"] = round(p_split, 2)
371+
self.metrics.at[key, "% Omit Edges"] = round(p_omit, 2)
372+
self.metrics.at[key, "SWC Run Length"] = round(gt_rl, 2)
373+
362374
# -- Merge Detection --
363375
def detect_merges(self):
364376
"""
@@ -724,4 +736,3 @@ def to_local_voxels(self, key, i, offset):
724736
voxel = np.array(self.graphs[key].voxels[i])
725737
offset = np.array(offset)
726738
return tuple(voxel - offset)
727-

src/segmentation_skeleton_metrics/utils/swc_util.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ def read(self, swc_pointer):
6767
----------
6868
swc_pointer : str or List[str]
6969
Object that points to SWC files to be read, must be one of:
70-
- swc_path (str): Path to single SWC file.
71-
- swc_dir_path (str): Path to local directory containing SWC files.
72-
- swc_zip_path (str): Path to a local ZIP archive containing SWC files.
73-
- swc_zip_dir_path (str): Path to a local directory of ZIPs with SWC files.
74-
- swc_s3_dir_path (str): Path to S3 directory containing SWC files.
75-
- swc_gcs_dir_path (str): Path to GCS directory containing SWC files.
76-
- swc_gcs_zip_dir_path (str): Path to GCS directory containing ZIP archives of SWC files.
77-
- swc_path_list (List[str]): List of paths to local SWC files.
70+
- file_path (str): Path to single SWC file
71+
- dir_path (str): Path to local directory with SWC files
72+
- zip_path (str): Path to local ZIP with SWC files
73+
- zip_dir_path (str): Path to local directory of ZIPs with SWC files
74+
- s3_dir_path (str): Path to S3 directory with SWC files
75+
- gcs_dir_path (str): Path to GCS directory with SWC files
76+
- gcs_zip_dir_path (str): Path to GCS directory with ZIPs of SWC files
77+
- path_list (List[str]): List of paths to local SWC files
7878
7979
Returns
8080
-------
8181
Deque[dict]
82-
List of dictionaries whose keys and values are the attribute names
83-
and values from the SWC files. Each dictionary contains the
84-
following items:
82+
Dictionaries whose keys and values are the attribute names and
83+
values from the SWC files. Each dictionary contains the following:
84+
items:
8585
- "id": unique identifier of each node in an SWC file.
8686
- "pid": parent ID of each node.
8787
- "radius": radius value corresponding to each node.

0 commit comments

Comments
 (0)