|
| 1 | +import argparse |
| 2 | +import os |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | +import tifffile |
| 7 | +import zarr |
| 8 | + |
| 9 | +from flamingo_tools.s3_utils import get_s3_path, BUCKET_NAME, SERVICE_ENDPOINT |
| 10 | +# from skimage.segmentation import relabel_sequential |
| 11 | + |
| 12 | + |
| 13 | +def filter_marker_instances(cochlea, segmentation, seg_name, group=None): |
| 14 | + """Filter segmentation with marker labels. |
| 15 | + Positive segmentation instances are set to 1, negative to 2. |
| 16 | + """ |
| 17 | + internal_path = os.path.join(cochlea, "tables", seg_name, "default.tsv") |
| 18 | + tsv_path, fs = get_s3_path(internal_path, bucket_name=BUCKET_NAME, service_endpoint=SERVICE_ENDPOINT) |
| 19 | + with fs.open(tsv_path, "r") as f: |
| 20 | + table_seg = pd.read_csv(f, sep="\t") |
| 21 | + |
| 22 | + label_ids_positive = list(table_seg.loc[table_seg["marker_labels"] == 1, "label_id"]) |
| 23 | + label_ids_negative = list(table_seg.loc[table_seg["marker_labels"] == 2, "label_id"]) |
| 24 | + |
| 25 | + if group is None: |
| 26 | + label_ids_marker = label_ids_positive + label_ids_negative |
| 27 | + filter_mask = ~np.isin(segmentation, label_ids_marker) |
| 28 | + segmentation[filter_mask] = 0 |
| 29 | + |
| 30 | + filter_mask = np.isin(segmentation, label_ids_positive) |
| 31 | + segmentation[filter_mask] = 1 |
| 32 | + filter_mask = np.isin(segmentation, label_ids_negative) |
| 33 | + segmentation[filter_mask] = 2 |
| 34 | + elif group == "positive": |
| 35 | + filter_mask = ~np.isin(segmentation, label_ids_positive) |
| 36 | + segmentation[filter_mask] = 0 |
| 37 | + filter_mask = np.isin(segmentation, label_ids_positive) |
| 38 | + segmentation[filter_mask] = 1 |
| 39 | + elif group == "negative": |
| 40 | + filter_mask = ~np.isin(segmentation, label_ids_negative) |
| 41 | + segmentation[filter_mask] = 0 |
| 42 | + filter_mask = np.isin(segmentation, label_ids_negative) |
| 43 | + segmentation[filter_mask] = 2 |
| 44 | + else: |
| 45 | + raise ValueError("Choose either 'positive' or 'negative' as group value.") |
| 46 | + |
| 47 | + segmentation = segmentation.astype("uint16") |
| 48 | + return segmentation |
| 49 | + |
| 50 | + |
| 51 | +def export_lower_resolution(args): |
| 52 | + |
| 53 | + # iterate through exporting lower resolutions |
| 54 | + for scale in args.scale: |
| 55 | + output_folder = os.path.join(args.output_folder, args.cochlea, f"scale{scale}") |
| 56 | + os.makedirs(output_folder, exist_ok=True) |
| 57 | + |
| 58 | + for group in ["positive", "negative"]: |
| 59 | + |
| 60 | + input_key = f"s{scale}" |
| 61 | + for channel in args.channels: |
| 62 | + |
| 63 | + out_path = os.path.join(output_folder, f"{channel}_marker_{group}.tif") |
| 64 | + if os.path.exists(out_path): |
| 65 | + continue |
| 66 | + |
| 67 | + print("Exporting channel", channel) |
| 68 | + internal_path = os.path.join(args.cochlea, "images", "ome-zarr", f"{channel}.ome.zarr") |
| 69 | + s3_store, fs = get_s3_path(internal_path, bucket_name=BUCKET_NAME, service_endpoint=SERVICE_ENDPOINT) |
| 70 | + with zarr.open(s3_store, mode="r") as f: |
| 71 | + data = f[input_key][:] |
| 72 | + print("Data shape", data.shape) |
| 73 | + |
| 74 | + print(f"Filtering {group} marker instances.") |
| 75 | + data = filter_marker_instances(args.cochlea, data, channel, group=group) |
| 76 | + tifffile.imwrite(out_path, data, bigtiff=True, compression="zlib") |
| 77 | + |
| 78 | + |
| 79 | +def main(): |
| 80 | + parser = argparse.ArgumentParser() |
| 81 | + parser.add_argument("--cochlea", "-c", required=True) |
| 82 | + parser.add_argument("--scale", "-s", nargs="+", type=int, required=True) |
| 83 | + parser.add_argument("--output_folder", "-o", required=True) |
| 84 | + parser.add_argument("--channels", nargs="+", type=str, default=["PV", "VGlut3", "CTBP2"]) |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + export_lower_resolution(args) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments