Skip to content

Commit 379efe0

Browse files
committed
Export lower resolution for marker
1 parent 4cf71ff commit 379efe0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

scripts/export_lower_resolution.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,31 @@ def filter_cochlea(
9696
dilation_iterations=dilation_iterations)
9797

9898

99+
def filter_marker_instances(cochlea, segmentation, seg_name):
100+
"""Filter segmentation with marker labels.
101+
Positive segmentation instances are set to 1, negative to 2.
102+
"""
103+
internal_path = os.path.join(cochlea, "tables", seg_name, "default.tsv")
104+
tsv_path, fs = get_s3_path(internal_path, bucket_name=BUCKET_NAME, service_endpoint=SERVICE_ENDPOINT)
105+
with fs.open(tsv_path, "r") as f:
106+
table_seg = pd.read_csv(f, sep="\t")
107+
108+
label_ids_positive = list(table_seg.loc[table_seg["marker_labels"] == 1, "label_id"])
109+
label_ids_negative = list(table_seg.loc[table_seg["marker_labels"] == 2, "label_id"])
110+
111+
label_ids_marker = label_ids_positive + label_ids_negative
112+
filter_mask = ~np.isin(segmentation, label_ids_marker)
113+
segmentation[filter_mask] = 0
114+
115+
filter_mask = np.isin(segmentation, label_ids_positive)
116+
segmentation[filter_mask] = 1
117+
filter_mask = np.isin(segmentation, label_ids_negative)
118+
segmentation[filter_mask] = 2
119+
120+
segmentation = segmentation.astype("uint16")
121+
return segmentation
122+
123+
99124
def upscale_volume(
100125
target_data: np.ndarray,
101126
downscaled_volume: np.ndarray,
@@ -146,6 +171,9 @@ def export_lower_resolution(args):
146171
input_key = f"s{scale}"
147172
for channel in args.channels:
148173
out_path = os.path.join(output_folder, f"{channel}.tif")
174+
if args.filter_marker_labels:
175+
out_path = os.path.join(output_folder, f"{channel}_marker.tif")
176+
149177
if os.path.exists(out_path):
150178
continue
151179

@@ -156,7 +184,11 @@ def export_lower_resolution(args):
156184
data = f[input_key][:]
157185
print("Data shape", data.shape)
158186
if args.filter_by_components is not None:
187+
print(f"Filtering channel {channel} by components {args.filter_by_components}.")
159188
data = filter_component(fs, data, args.cochlea, channel, args.filter_by_components)
189+
if args.filter_marker_labels:
190+
print(f"Filtering marker instances for channel {channel}.")
191+
data = filter_marker_instances(args.cochlea, data, channel)
160192
if args.filter_cochlea_channels is not None:
161193
us_factor = ds_factor // (2 ** scale)
162194
upscaled_filter = upscale_volume(data, filter_volume, upscale_factor=us_factor)
@@ -181,6 +213,7 @@ def main():
181213
parser.add_argument("--filter_ihc_components", nargs="+", type=int, default=[1])
182214
parser.add_argument("--binarize", action="store_true")
183215
parser.add_argument("--filter_cochlea_channels", nargs="+", type=str, default=None)
216+
parser.add_argument("--filter_marker_labels", action="store_true")
184217
parser.add_argument("--filter_dilation_iterations", type=int, default=8)
185218
args = parser.parse_args()
186219

0 commit comments

Comments
 (0)