Skip to content

Commit a5a6ce8

Browse files
committed
Improved readability
1 parent d6dbca4 commit a5a6ce8

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

flamingo_tools/segmentation/ihc_synapse_postprocessing.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def find_overlapping_masks(
5151

5252

5353
def replace_masks(
54-
data_base: np.ndarray,
55-
data_ref: np.ndarray,
54+
arr_base: np.ndarray,
55+
arr_ref: np.ndarray,
5656
label_id_base: int,
5757
edit_labels: List[dict],
5858
) -> np.ndarray:
@@ -68,24 +68,24 @@ def replace_masks(
6868
Base array with updated content.
6969
"""
7070
print(f"Replacing {len(edit_labels)} instances")
71-
data_base[data_base == label_id_base] = 0
71+
arr_base[arr_base == label_id_base] = 0
7272
for edit_dic in edit_labels:
7373
# bool array for new mask
74-
data_ref_id = data_ref.copy()
74+
data_ref_id = arr_ref.copy()
7575
data_ref_id[data_ref_id != edit_dic["ref_id"]] = 0
76-
arr_ref = data_ref_id.astype(bool)
76+
bool_ref = data_ref_id.astype(bool)
7777

78-
data_base[arr_ref] = edit_dic["new_label"]
79-
return data_base
78+
arr_base[bool_ref] = edit_dic["new_label"]
79+
return arr_base
8080

8181

8282
def postprocess_ihc_synapse_crop(
83-
data_base_: np.ndarray,
84-
data_ref_: np.ndarray,
83+
data_base: np.typing.ArrayLike,
84+
data_ref: np.typing.ArrayLike,
8585
table_base: pd.DataFrame,
8686
synapse_limit: int = 25,
8787
min_overlap: float = 0.5,
88-
) -> np.ndarray:
88+
) -> np.typing.ArrayLike:
8989
"""Postprocess IHC segmentation based on number of synapse per IHC count.
9090
Segmentations from a base segmentation are analysed and replaced with
9191
instances from a reference segmentation, if suitable instances overlap with
@@ -107,25 +107,24 @@ def postprocess_ihc_synapse_crop(
107107
running_label_id = int(table_base["label_id"].max() + 1)
108108
min_overlap = 0.5
109109
edit_labels = []
110-
data_base = data_base_.copy()
111110

112111
seg_ids_base = np.unique(data_base)[1:]
113112
for seg_id_base in seg_ids_base:
114113
if seg_id_base in list(table_edit["label_id"]):
115114

116115
edit_labels, running_label_id = find_overlapping_masks(
117-
data_base.copy(), data_ref_.copy(), seg_id_base,
116+
data_base.copy(), data_ref.copy(), seg_id_base,
118117
running_label_id, min_overlap=min_overlap,
119118
)
120119

121120
if len(edit_labels) > 1:
122-
data_base = replace_masks(data_base, data_ref_, seg_id_base, edit_labels)
121+
data_base = replace_masks(data_base, data_ref, seg_id_base, edit_labels)
123122
return data_base
124123

125124

126125
def postprocess_ihc_synapse(
127-
data_base_: np.ndarray,
128-
data_ref_: np.ndarray,
126+
data_base: np.typing.ArrayLike,
127+
data_ref: np.typing.ArrayLike,
129128
table_base: pd.DataFrame,
130129
synapse_limit: int = 25,
131130
min_overlap: float = 0.5,
@@ -138,8 +137,8 @@ def postprocess_ihc_synapse(
138137
the base segmentation.
139138
140139
Args:
141-
data_base_: Base array.
142-
data_ref_: Reference array.
140+
data_base: Base array.
141+
data_ref: Reference array.
143142
table_base: Segmentation table of base segmentation with synapse per IHC counts.
144143
synapse_limit: Limit of synapses per IHC to consider replacement of base segmentation.
145144
min_overlap: Minimal fraction of overlap between ref and base isntances to consider replacement.
@@ -162,17 +161,17 @@ def postprocess_ihc_synapse(
162161
coords_min = [int(round(c / resolution)) for c in coords_min]
163162
roi = tuple(slice(cmin - roi_pad, cmax + roi_pad) for cmax, cmin in zip(coords_max, coords_min))
164163

165-
data_base = data_base_[roi]
166-
data_ref = data_ref_[roi]
164+
roi_base = data_base[roi]
165+
roi_ref = data_ref[roi]
167166
label_id_base = row["label_id"]
168167

169168
edit_labels, running_label_id = find_overlapping_masks(
170-
data_base.copy(), data_ref.copy(), label_id_base,
169+
roi_base.copy(), roi_ref.copy(), label_id_base,
171170
running_label_id, min_overlap=min_overlap,
172171
)
173172

174173
if len(edit_labels) > 1:
175-
data_base = replace_masks(data_base, data_ref, label_id_base, edit_labels)
176-
data_base_[roi] = data_base
174+
roi_base = replace_masks(roi_base, roi_ref, label_id_base, edit_labels)
175+
data_base[roi] = roi_base
177176

178-
return data_base_
177+
return data_base

scripts/prediction/postprocess_ihc_synapse.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ def main():
4343
args = parser.parse_args()
4444

4545
if args.base_key is None:
46-
data_base_ = read_image_data(args.base_path, args.base_key)
46+
data_base = read_image_data(args.base_path, args.base_key)
4747
else:
48-
data_base_ = open_file(args.base_path, "a")[args.base_key]
49-
data_ref_ = read_image_data(args.ref_path, args.ref_key)
48+
data_base = open_file(args.base_path, "a")[args.base_key]
49+
data_ref = read_image_data(args.ref_path, args.ref_key)
5050

5151
with open(args.base_table, "r") as f:
5252
table_base = pd.read_csv(f, sep="\t")
5353

5454
if args.crop:
5555
output_ = ihc_synapse_postprocessing.postprocess_ihc_synapse_crop(
56-
data_base_, data_ref_, table_base=table_base, synapse_limit=25, min_overlap=0.5,
56+
data_base, data_ref, table_base=table_base, synapse_limit=25, min_overlap=0.5,
5757
)
5858
else:
5959
output_ = ihc_synapse_postprocessing.postprocess_ihc_synapse(
60-
data_base_, data_ref_, table_base=table_base, synapse_limit=25, min_overlap=0.5,
60+
data_base, data_ref, table_base=table_base, synapse_limit=25, min_overlap=0.5,
6161
resolution=0.38, roi_buffer=40,
6262
)
6363

0 commit comments

Comments
 (0)