Repository navigation
Expand file tree
/
Copy pathexport_block_catalog_distributions.py
More file actions
executable file
·214 lines (182 loc) · 7.26 KB
/
Copy pathexport_block_catalog_distributions.py
File metadata and controls
executable file
·214 lines (182 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
"""Export labeled block catalogs and per-sample metric distributions."""
from __future__ import annotations
import argparse
import csv
import math
from pathlib import Path
from typing import Dict, List, Sequence
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
METRICS = [
("omit_length_um", "Omit Length (um)"),
("correct_length_um", "Recall Length (um)"),
("split_length_um", "Split Length (um)"),
("merge_count", "Merge Count"),
("self_merge_count", "Self-Merge Count"),
("avg_intensity_mean", "Average Intensity"),
]
ALL_COMBINED_LABEL = "all-carveouts-combined"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--source-csv", required=True, type=Path)
parser.add_argument("--output-dir", required=True, type=Path)
parser.add_argument("--voxel-size-xy", type=float, default=0.748)
return parser.parse_args()
def read_csv_rows(path: Path) -> List[dict]:
with path.open("r", newline="") as f:
return list(csv.DictReader(f))
def write_csv(path: Path, rows: Sequence[dict], fieldnames: Sequence[str]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames, extrasaction="ignore")
writer.writeheader()
for row in rows:
writer.writerow(row)
def safe_float(value: str) -> float:
return float(value) if value not in ("", None) else 0.0
def safe_int(value: str) -> int:
return int(value) if value not in ("", None) else 0
def add_derived_metrics(row: dict, voxel_size_xy: float) -> dict:
out = row.copy()
counts = {
label: safe_int(row[f"{label}_point_count"])
for label in ("omit", "correct", "split")
}
means = {
label: safe_float(row[f"{label}_intensity_mean"])
for label in ("omit", "correct", "split")
}
stds = {
label: safe_float(row[f"{label}_intensity_std"])
for label in ("omit", "correct", "split")
}
total_points = sum(counts.values())
if total_points > 0:
mean = sum(counts[label] * means[label] for label in counts) / total_points
second = sum(counts[label] * (stds[label] ** 2 + means[label] ** 2) for label in counts) / total_points
out["avg_intensity_mean"] = mean
out["avg_intensity_std"] = math.sqrt(max(second - mean * mean, 0.0))
else:
out["avg_intensity_mean"] = 0.0
out["avg_intensity_std"] = 0.0
out["total_length_um"] = sum(safe_float(row[f"{label}_length_um"]) for label in ("omit", "correct", "split"))
out["merge_total"] = safe_int(row["merge_count"]) + safe_int(row["self_merge_count"])
out["cube_center_x_phys_um"] = safe_float(row["cube_center_x_vox"]) * voxel_size_xy
out["cube_center_y_phys_um"] = safe_float(row["cube_center_y_vox"]) * voxel_size_xy
out["cube_center_z_phys_um"] = safe_float(row["cube_center_z_vox"])
out["center_volume_coordinate_vox"] = (
f"[{int(round(safe_float(row['cube_center_x_vox'])))}"
f",{int(round(safe_float(row['cube_center_y_vox'])))}"
f",{int(round(safe_float(row['cube_center_z_vox'])))}]"
)
return out
def build_catalog(rows: Sequence[dict]) -> List[dict]:
grouped: Dict[str, List[dict]] = {}
for row in rows:
grouped.setdefault(row["carveout"], []).append(row)
catalog_rows: List[dict] = []
for carveout, group in sorted(grouped.items()):
group_sorted = sorted(
group,
key=lambda r: (
safe_int(r["cube_z"]),
safe_int(r["cube_y"]),
safe_int(r["cube_x"]),
),
)
width = max(3, len(str(len(group_sorted) - 1)))
for idx, row in enumerate(group_sorted):
out = row.copy()
out["block_label"] = f"{idx:0{width}d}"
catalog_rows.append(out)
return catalog_rows
def plot_distributions(rows: Sequence[dict], output_dir: Path) -> None:
grouped: Dict[str, List[dict]] = {}
for row in rows:
grouped.setdefault(row["carveout"], []).append(row)
grouped[ALL_COMBINED_LABEL] = list(rows)
dist_dir = output_dir / "distributions"
dist_dir.mkdir(parents=True, exist_ok=True)
summary_rows = []
for carveout, group in sorted(grouped.items()):
fig, axes = plt.subplots(2, 3, figsize=(15, 9), facecolor="white")
axes = axes.ravel()
for ax, (metric, title) in zip(axes, METRICS):
vals = np.asarray([safe_float(row[metric]) for row in group], dtype=float)
ax.hist(vals, bins=30, color="#4f81bd", edgecolor="white", linewidth=0.6)
ax.axvline(float(np.mean(vals)), color="#c0504d", linewidth=1.3, label="mean")
ax.axvline(float(np.median(vals)), color="#9bbb59", linewidth=1.3, label="median")
ax.set_title(title)
ax.set_xlabel(metric)
ax.set_ylabel("block count")
ax.legend(loc="upper right", fontsize=8)
summary_rows.append(
{
"carveout": carveout,
"metric": metric,
"count": len(vals),
"mean": float(np.mean(vals)),
"median": float(np.median(vals)),
"std": float(np.std(vals)),
"min": float(np.min(vals)),
"max": float(np.max(vals)),
}
)
title = (
"all cells / blocks combined metric distributions"
if carveout == ALL_COMBINED_LABEL
else f"{carveout} block metric distributions"
)
fig.suptitle(title, fontsize=14)
plt.tight_layout()
plt.savefig(dist_dir / f"{carveout}_block_metric_distributions.png", dpi=220, bbox_inches="tight")
plt.close(fig)
write_csv(
output_dir / "distribution_summary.csv",
summary_rows,
["carveout", "metric", "count", "mean", "median", "std", "min", "max"],
)
def main() -> None:
args = parse_args()
args.output_dir.mkdir(parents=True, exist_ok=True)
source_rows = [add_derived_metrics(row, args.voxel_size_xy) for row in read_csv_rows(args.source_csv)]
catalog_rows = build_catalog(source_rows)
fieldnames = [
"carveout",
"block_label",
"center_volume_coordinate_vox",
"cube_z",
"cube_y",
"cube_x",
"cube_origin_z_vox",
"cube_origin_y_vox",
"cube_origin_x_vox",
"cube_center_z_phys_um",
"cube_center_y_phys_um",
"cube_center_x_phys_um",
"omit_length_um",
"correct_length_um",
"split_length_um",
"total_length_um",
"merge_count",
"self_merge_count",
"merge_total",
"omit_point_count",
"correct_point_count",
"split_point_count",
"omit_intensity_mean",
"omit_intensity_std",
"correct_intensity_mean",
"correct_intensity_std",
"split_intensity_mean",
"split_intensity_std",
"avg_intensity_mean",
"avg_intensity_std",
]
write_csv(args.output_dir / "block_catalog.csv", catalog_rows, fieldnames)
plot_distributions(catalog_rows, args.output_dir)
if __name__ == "__main__":
main()