File size: 43,064 Bytes
fd4ffa6 |
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 |
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import gc
import json
import os
import shutil
import time
import warnings
import zipfile
import imageio.v3 as imageio
import numpy as np
from PIL import Image
from pycocotools.coco import COCO
from sklearn.model_selection import KFold
# from skimage.io import imsave
# from skimage.measure import label
# import imageio
def min_label_precision(label):
lm = label.max()
if lm <= 255:
label = label.astype(np.uint8)
elif lm <= 65535:
label = label.astype(np.uint16)
else:
label = label.astype(np.uint32)
return label
def guess_convert_to_uint16(img, margin=30):
"""
Guess a multiplier that makes all pixels integers.
The input img (each channel) is already in the range 0..1, they must have been converted from uint16 integers as image / scale,
where scale was the unknown max intensity.
We could guess the scale by looking at unique values: 1/np.min(np.diff(np.unique(im)).
the hypothesis is that it will be more accurate recovery of the original image,
instead of doing a simple (img*65535).astype(np.uint16)
"""
for i in range(img.shape[0]):
im = img[i]
if im.any():
start = time.time()
imsmall = im[::4, ::4] # subsample
# imsmall = im
scale = int(np.round(1 / np.min(np.diff(np.unique(imsmall))))) # guessing scale
test = [
(np.sum((imsmall * k) % 1)) for k in range(scale - margin, scale + margin)
] # finetune, guess a multiplier that makes all pixels integers
sid = np.argmin(test) # fine tune scale
if scale < 16000 or scale > 16400:
warnings.warn("scale not in expected range")
print(
"guessing scale",
scale,
test[margin],
"fine tuning scale",
scale - margin + sid,
"dif",
test[sid],
"time",
time.time() - start,
)
scale = 16384
else:
scale = scale - margin + sid
# all the recovered scale values seems to be up to 16384,
# we can stretch to 65535(for better visualization, most tiff viewers expect that range)
scale = min(65535, scale * 4)
img[i] = im * scale
img = img.astype(np.uint16)
return img
def concatenate_masks(mask_dir):
labeled_mask = None
i = 0
for filename in sorted(os.listdir(mask_dir)):
if filename.endswith(".png"):
mask = imageio.imread(os.path.join(mask_dir, filename))
if labeled_mask is None:
labeled_mask = np.zeros(shape=mask.shape, dtype=np.uint16)
labeled_mask[mask > 0] = i
i = i + 1
if i <= 255:
labeled_mask = labeled_mask.astype(np.uint8)
return labeled_mask
# def concatenate_masks(mask_dir):
# masks = []
# for filename in sorted(os.listdir(mask_dir)):
# if filename.endswith('.png'):
# mask = imageio.imread(os.path.join(mask_dir, filename))
# masks.append(mask)
# concatenated_mask = np.any(masks, axis=0).astype(np.uint8)
# labeled_mask = label(concatenated_mask)
# return labeled_mask
# def normalize_image(image):
# # Convert to float and normalize each channel
# image = image.astype(np.float32)
# for i in range(3):
# channel = image[..., i]
# channel_min = np.min(channel)
# channel_max = np.max(channel)
# if channel_max - channel_min != 0:
# image[..., i] = (channel - channel_min) / (channel_max - channel_min)
# return image
def get_filenames_exclude_masks(dir1, target_string):
filenames = []
# Combine lists of files from both directories
files = os.listdir(dir1)
# Filter files that contain the target string but exclude 'masks'
filenames = [f for f in files if target_string in f and "masks" not in f]
return filenames
def remove_overlaps(masks, medians, overlap_threshold=0.75):
"""replace overlapping mask pixels with mask id of closest mask
if mask fully within another mask, remove it
masks = Nmasks x Ly x Lx
"""
cellpix = masks.sum(axis=0)
igood = np.ones(masks.shape[0], "bool")
for i in masks.sum(axis=(1, 2)).argsort():
npix = float(masks[i].sum())
noverlap = float(masks[i][cellpix > 1].sum())
if noverlap / npix >= overlap_threshold:
igood[i] = False
cellpix[masks[i] > 0] -= 1
# print(cellpix.min())
print(f"removing {(~igood).sum()} masks")
masks = masks[igood]
medians = medians[igood]
cellpix = masks.sum(axis=0)
overlaps = np.array(np.nonzero(cellpix > 1.0)).T
dists = ((overlaps[:, :, np.newaxis] - medians.T) ** 2).sum(axis=1)
tocell = np.argmin(dists, axis=1)
masks[:, overlaps[:, 0], overlaps[:, 1]] = 0
masks[tocell, overlaps[:, 0], overlaps[:, 1]] = 1
# labels should be 1 to mask.shape[0]
masks = masks.astype(int) * np.arange(1, masks.shape[0] + 1, 1, int)[:, np.newaxis, np.newaxis]
masks = masks.sum(axis=0)
gc.collect()
return masks
def livecell_json_files(dataset_dir, json_f_path):
"""
This function takes in the directory of livecell extracted dataset as input and
creates 7 json lists with 5 folds. Separate testing set is recorded in the json list.
Please note that there are some hard-coded directory names as per the original dataset.
At the time of creation, the livecell zipfile had 'images' and 'LIVECell_dataset_2021' directories
"""
# "A172", "BT474", "Huh7", "MCF7", "SHSY5Y", "SkBr3", "SKOV3"
# TODO "BV2" is being skipped
cell_type_list = ["A172", "BT474", "Huh7", "MCF7", "SHSY5Y", "SkBr3", "SKOV3"]
for each_cell_tp in cell_type_list:
for split in ["train", "val", "test"]:
print(f"Working on split: {split}")
if split == "test":
img_path = os.path.join(dataset_dir, "images", "livecell_test_images", each_cell_tp)
msk_path = os.path.join(dataset_dir, "images", "livecell_test_images", each_cell_tp + "_masks")
else:
img_path = os.path.join(dataset_dir, "images", "livecell_train_val_images", each_cell_tp)
msk_path = os.path.join(dataset_dir, "images", "livecell_train_val_images", each_cell_tp + "_masks")
if not os.path.exists(msk_path):
os.makedirs(msk_path)
# annotation path
path = os.path.join(
dataset_dir,
"livecell-dataset.s3.eu-central-1.amazonaws.com",
"LIVECell_dataset_2021",
"annotations",
"LIVECell_single_cells",
each_cell_tp.lower(),
split + ".json",
)
annotation = COCO(path)
# Convert COCO format segmentation to binary mask
images = annotation.loadImgs(annotation.getImgIds())
height = []
width = []
for index, im in enumerate(images):
print("Status: {}/{}, Process image: {}".format(index, len(images), im["file_name"]))
if (
im["file_name"] == "BV2_Phase_C4_2_03d00h00m_1.tif"
or im["file_name"] == "BV2_Phase_C4_2_03d00h00m_3.tif"
):
print("Skipping the file: BV2_Phase_C4_2_03d00h00m_1.tif, as it is troublesome")
continue
# load image
img = Image.open(os.path.join(img_path, im["file_name"])).convert("L")
height.append(img.size[0])
width.append(img.size[1])
# arr = np.asarray(img) #? not used
# msk = np.zeros(arr.shape)
# load and display instance annotations
annids = annotation.getAnnIds(imgIds=im["id"], iscrowd=None)
anns = annotation.loadAnns(annids)
idx = 1
medians = []
masks = []
k = 0
for ann in anns:
# convert segmentation to binary mask
mask = annotation.annToMask(ann)
masks.append(mask)
ypix, xpix = mask.nonzero()
medians.append(np.array([ypix.mean().astype(np.float32), xpix.mean().astype(np.float32)]))
k += 1
# add instance mask to image mask
# msk = np.add(msk, mask*idx)
# idx += 1
masks = np.array(masks).astype(np.int8)
medians = np.array(medians)
masks = remove_overlaps(masks, medians, overlap_threshold=0.75)
gc.collect()
# ## Create new name for the image and also for the mask and save them as .tif format
# masks_int32 = masks.astype(np.int32)
# mask_pil = Image.fromarray(masks_int32, 'I')
t_filename = im["file_name"]
# cell_type = t_filename.split('_')[0] #? not used
new_mask_name = t_filename[:-4] + "_masks.tif"
# mask_pil.save(os.path.join(msk_path, new_mask_name))
imageio.imwrite(os.path.join(msk_path, new_mask_name), min_label_precision(masks))
gc.collect()
print(f"In total {len(images)} images")
# The directory containing your files
# cell_type = 'BV2'
json_save_path = os.path.join(json_f_path, f"lc_{each_cell_tp}.json")
directory = os.path.join(dataset_dir, "images", "livecell_train_val_images", each_cell_tp)
mask_directory = os.path.join(dataset_dir, "images", "livecell_train_val_images", each_cell_tp + "_masks")
test_directory = os.path.join(dataset_dir, "images", "livecell_test_images", each_cell_tp)
mask_test_directory = os.path.join(dataset_dir, "images", "livecell_test_images", each_cell_tp + "_masks")
# List to hold all image-mask pairs
data_pairs = []
test_data_pairs = []
all_data = {}
# Scan the directory for image files and create pairs
for filename in os.listdir(directory):
if filename.endswith(".tif"):
# Construct the corresponding mask filename
mask_filename = filename.replace(".tif", "_masks.tif")
# Check if the corresponding mask file exists
if os.path.exists(os.path.join(mask_directory, mask_filename)):
# Add the pair to the list
data_pairs.append(
{
"image": os.path.join(
"livecell_dataset", "images", "livecell_train_val_images", each_cell_tp, filename
),
"label": os.path.join(
"livecell_dataset",
"images",
"livecell_train_val_images",
f"{each_cell_tp}_masks",
mask_filename,
),
}
)
# Convert data_pairs to a numpy array for easy indexing by KFold
data_pairs_array = np.array(data_pairs)
# Initialize KFold
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# Assign fold numbers
for fold, (_train_index, val_index) in enumerate(kf.split(data_pairs_array)):
for idx in val_index:
data_pairs_array[idx]["fold"] = fold
# Convert the array back to a list and sort by fold
sorted_data_pairs = sorted(data_pairs_array.tolist(), key=lambda x: x["fold"])
print(sorted_data_pairs)
# Scan the directory for image files and create pairs
for filename in os.listdir(test_directory):
if filename.endswith(".tif"):
# Construct the corresponding mask filename
mask_filename = filename.replace(".tif", "_masks.tif")
# Check if the corresponding mask file exists
if os.path.exists(os.path.join(mask_test_directory, mask_filename)):
# Add the pair to the list
test_data_pairs.append(
{
"image": os.path.join(
"livecell_dataset", "images", "livecell_test_images", each_cell_tp, filename
),
"label": os.path.join(
"livecell_dataset",
"images",
"livecell_test_images",
f"{each_cell_tp}_masks",
mask_filename,
),
}
)
all_data["training"] = sorted_data_pairs
all_data["testing"] = test_data_pairs
with open(json_save_path, "w") as j_file:
json.dump(all_data, j_file, indent=4)
j_file.close()
def tissuenet_json_files(dataset_dir, json_f_path):
"""
This function takes in the directory of TissueNet extracted dataset as input and
creates 13 json lists with 5 folds each. Separate testing set is recorded in the json list per subset.
Please note that there are some hard-coded directory names as per the original dataset.
At the time of creation, the tissuenet 1.0 zipfile had 'train', 'val' and 'test' directories that
images with paired labels.
"""
for folder in ["train", "val", "test"]:
if not os.path.exists(os.path.join(dataset_dir, "tissuenet_1.0", folder)):
os.mkdir(os.path.join(dataset_dir, "tissuenet_1.0", folder))
for folder in ["train", "val", "test"]:
print(f"Working on {folder} directory of tissuenet")
f_name = f"tissuenet_1.0/tissuenet_v1.0_{folder}.npz"
dat = np.load(os.path.join(dataset_dir, f_name))
data = dat["X"]
labels = dat["y"]
tissues = dat["tissue_list"]
platforms = dat["platform_list"]
tlabels = np.unique(tissues)
plabels = np.unique(platforms)
tp = 0
for t in tlabels:
for p in plabels:
ix = ((tissues == t) * (platforms == p)).nonzero()[0]
tp += 1
if len(ix) > 0:
print(f"Working on {t} {p}")
for k, i in enumerate(ix):
print(f"Status: {k}/{len(ix)} {tp}/{len(tlabels) * len(plabels)} {t} {p}")
img = data[i].transpose(2, 0, 1)
label = labels[i][:, :, 0]
img = guess_convert_to_uint16(img) # guess inverse scale and convert to uint16
label = min_label_precision(label)
if folder == "train":
img = img.reshape(2, 2, 256, 2, 256).transpose(0, 1, 3, 2, 4).reshape(2, 4, 256, 256)
label = label.reshape(2, 256, 2, 256).transpose(0, 2, 1, 3).reshape(4, 256, 256)
zero_channel = np.zeros((1, img.shape[1], img.shape[2], img.shape[3]), dtype=img.dtype)
# Concatenate the zero channel with the original array along the first dimension
new_array = np.concatenate([img, zero_channel], axis=0)
# reshaped_array = np.transpose(new_array, (1, 2, 3, 0))
for j in range(4):
img_name = f"{folder}/{t}_{p}_{k}_{j}.tif"
mask_name = f"{folder}/{t}_{p}_{k}_{j}_masks.tif"
imageio.imwrite(os.path.join(dataset_dir, "tissuenet_1.0", img_name), new_array[:, j])
imageio.imwrite(os.path.join(dataset_dir, "tissuenet_1.0", mask_name), label[j])
else:
zero_channel = np.zeros((1, img.shape[1], img.shape[2]), dtype=img.dtype)
new_array = np.concatenate([img, zero_channel], axis=0)
# reshaped_array = np.transpose(new_array, (1, 2, 0))
img_name = f"{folder}/{t}_{p}_{k}.tif"
mask_name = f"{folder}/{t}_{p}_{k}_masks.tif"
imageio.imwrite(os.path.join(dataset_dir, "tissuenet_1.0", img_name), new_array)
imageio.imwrite(os.path.join(dataset_dir, "tissuenet_1.0", mask_name), label)
t_p_combos = [
["breast", "imc"],
["breast", "mibi"],
["breast", "vectra"],
["gi", "codex"],
["gi", "mibi"],
["gi", "mxif"],
["immune", "cycif"],
["immune", "mibi"],
["immune", "vectra"],
["lung", "cycif"],
["lung", "mibi"],
["pancreas", "codex"],
["pancreas", "vectra"],
["skin", "mibi"],
]
for each_t_p in t_p_combos:
json_f_name = "tn_" + each_t_p[0] + "_" + each_t_p[1] + ".json"
json_f_subset_path = os.path.join(json_f_path, json_f_name)
tp_match = each_t_p[0] + "_" + each_t_p[1]
train_filenames = get_filenames_exclude_masks(os.path.join(dataset_dir, "tissuenet_1.0", "train"), tp_match)
val_filenames = get_filenames_exclude_masks(os.path.join(dataset_dir, "tissuenet_1.0", "val"), tp_match)
test_filenames = get_filenames_exclude_masks(os.path.join(dataset_dir, "tissuenet_1.0", "test"), tp_match)
train_data_list = []
test_data_list = []
for each_tf in train_filenames:
t_dict = {
"image": os.path.join("tissuenet_dataset", "tissuenet_1.0", "train", each_tf),
"label": os.path.join("tissuenet_dataset", "tissuenet_1.0", "train", each_tf[:-4] + "_masks.tif"),
}
train_data_list.append(t_dict)
for each_vf in val_filenames:
t_dict = {
"image": os.path.join("tissuenet_dataset", "tissuenet_1.0", "val", each_vf),
"label": os.path.join("tissuenet_dataset", "tissuenet_1.0", "val", each_vf[:-4] + "_masks.tif"),
}
train_data_list.append(t_dict)
for each_tf in test_filenames:
t_dict = {
"image": os.path.join("tissuenet_dataset", "tissuenet_1.0", "test", each_tf),
"label": os.path.join("tissuenet_dataset", "tissuenet_1.0", "test", each_tf[:-4] + "_masks.tif"),
}
test_data_list.append(t_dict)
# print(train_data_list)
# print(test_data_list)
# Convert data_pairs to a numpy array for easy indexing by KFold
data_pairs_array = np.array(train_data_list)
# Initialize KFold
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# Assign fold numbers
for fold, (_train_index, val_index) in enumerate(kf.split(data_pairs_array)):
for idx in val_index:
data_pairs_array[idx]["fold"] = fold
# Convert the array back to a list and sort by fold
sorted_data_pairs = sorted(data_pairs_array.tolist(), key=lambda x: x["fold"])
print(sorted_data_pairs)
all_data = {}
all_data["training"] = sorted_data_pairs
all_data["testing"] = test_data_list
with open(json_f_subset_path, "w") as j_file:
json.dump(all_data, j_file, indent=4)
j_file.close()
def omnipose_json_file(dataset_dir, json_path):
"""
This function takes in the directory of extracted Omnipose dataset as input
and creates a json list with 5 folds. Please note that only 'bact_phase' and 'bact_fluor' were
used for creating datasets as they have bacteria the other directiories are worms. Each directory
has 'train_sorted' and 'test_sorted'.Separate testing set is recorded in the json list.
Please note that there are some hard-coded directory names as per the original dataset.
"""
# Define the folders
op_list = ["bact_fluor", "bact_phase"]
for each_op in op_list:
print(f"Working on {each_op} ...")
images_folder = os.path.join(dataset_dir, each_op, "train_sorted")
test_images_folder = os.path.join(dataset_dir, each_op, "test_sorted")
json_f_path = os.path.join(json_path, f"op_{each_op}.json")
# Initialize the list for training data
training_data = []
# Loop through each image file to find its corresponding label file
sub_dirs = os.listdir(images_folder)
# Likely Omnipose dataset was created using a Mac and hence the spare filename
sub_dirs.remove(".DS_Store")
for each_sub in sub_dirs:
# List files in the images folder
image_files = os.listdir(os.path.join(images_folder, each_sub))
for image_file in image_files:
# Extract the name without the extension
base_name = os.path.splitext(image_file)[0]
# Construct the label file name by adding '_label' before the extension
label_file = base_name + "_masks.tif" # + os.path.splitext(image_file)[1]
flows_file = base_name + "_flows.tif"
# Check if the corresponding label file exists in the labels folder
if label_file in os.listdir(os.path.join(images_folder, each_sub)):
# Add the file names to the training data list
training_data.append(
{
"image": os.path.join("omnipose_dataset", each_op, "train_sorted", each_sub, image_file),
"label": os.path.join("omnipose_dataset", each_op, "train_sorted", each_sub, label_file),
"flows": os.path.join("omnipose_dataset", each_op, "train_sorted", each_sub, flows_file),
}
)
# Convert data_pairs to a numpy array for easy indexing by KFold
data_pairs_array = np.array(training_data)
# Initialize KFold
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# Assign fold numbers
for fold, (_train_index, val_index) in enumerate(kf.split(data_pairs_array)):
for idx in val_index:
data_pairs_array[idx]["fold"] = fold
# Convert the array back to a list and sort by fold
sorted_data_pairs = sorted(data_pairs_array.tolist(), key=lambda x: x["fold"])
# Initialize the list for testing data
testing_data = []
test_sub_dirs = os.listdir(test_images_folder)
# Likely Omnipose dataset was created using a Mac and hence the spare filename
test_sub_dirs.remove(".DS_Store")
# Loop through each image file to find its corresponding label file
for each_test_sub in test_sub_dirs:
# List files in the images folder
test_image_files = os.listdir(os.path.join(test_images_folder, each_test_sub))
for image_file in test_image_files:
# Extract the name without the extension
base_name = os.path.splitext(image_file)[0]
# Construct the label file name by adding '_label' before the extension
label_file = base_name + "_masks.tif" # + os.path.splitext(image_file)[1]
# Check if the corresponding label file exists in the labels folder
if label_file in os.listdir(os.path.join(test_images_folder, each_test_sub)):
# Add the file names to the training data list
testing_data.append(
{
"image": os.path.join(
"omnipose_dataset", each_op, "test_sorted", each_test_sub, image_file
),
"label": os.path.join(
"omnipose_dataset", each_op, "test_sorted", each_test_sub, label_file
),
}
)
all_data = {}
all_data["training"] = sorted_data_pairs
all_data["testing"] = testing_data
# Save the training data list to a JSON file
with open(json_f_path, "w") as json_file:
json.dump(all_data, json_file, indent=4)
def nips_json_file(dataset_dir, json_f_path):
"""
This function takes in the directory of extracted NIPS cell segmentation challenge as input
and creates a json list with 5 folds. Separate testing set is recorded in the json list.
Please note that there are some hard-coded directory names as per the original dataset.
At the time of creation, the NIPS zipfile had 'Training-labeled' and 'Testing' directories that
both contained 'images' and 'labels' directories
"""
# The directory containing your files
json_save_path = os.path.normpath(json_f_path)
directory = os.path.join(dataset_dir, "Training-labeled")
test_directory = os.path.join(dataset_dir, "Testing", "Public")
# List to hold all image-mask pairs
data_pairs = []
test_data_pairs = []
all_data = {}
# Scan the directory for image files and create pairs
for filename in os.listdir(os.path.join(directory, "images")):
if os.path.exists(os.path.join(directory, "images", filename)):
# Extract the name without the extension
base_name = os.path.splitext(filename)[0]
# Construct the label file name by adding '_label' before the extension
label_file = base_name + "_label.tiff" # + os.path.splitext(image_file)[1]
# Check if the corresponding label file exists in the labels folder
if label_file in os.listdir(os.path.join(directory, "labels")):
# Add the file names to the training data list
data_pairs.append(
{
"image": os.path.join("nips_dataset", "Training-labeled", "images", filename),
"label": os.path.join("nips_dataset", "Training-labeled", "labels", label_file),
}
)
# Convert data_pairs to a numpy array for easy indexing by KFold
data_pairs_array = np.array(data_pairs)
# Initialize KFold
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# Assign fold numbers
for fold, (_train_index, val_index) in enumerate(kf.split(data_pairs_array)):
for idx in val_index:
data_pairs_array[idx]["fold"] = fold
# Convert the array back to a list and sort by fold
sorted_data_pairs = sorted(data_pairs_array.tolist(), key=lambda x: x["fold"])
print(sorted_data_pairs)
# Scan the directory for image files and create pairs
for filename in os.listdir(os.path.join(test_directory, "images")):
if os.path.exists(os.path.join(test_directory, "images", filename)):
# Extract the name without the extension
base_name = os.path.splitext(filename)[0]
# Construct the label file name by adding '_label' before the extension
label_file = base_name + "_label.tiff" # + os.path.splitext(image_file)[1]
# Check if the corresponding label file exists in the labels folder
if label_file in os.listdir(os.path.join(test_directory, "labels")):
# Add the file names to the training data list
test_data_pairs.append(
{
"image": os.path.join("nips_dataset", "Testing", "Public", "images", filename),
"label": os.path.join("nips_dataset", "Testing", "Public", "labels", label_file),
}
)
all_data["training"] = sorted_data_pairs
all_data["testing"] = test_data_pairs
with open(json_save_path, "w") as j_file:
json.dump(all_data, j_file, indent=4)
j_file.close()
def kaggle_json_file(dataset_dir, json_f_path):
"""
This function takes in the directory of kaggle nuclei extracted dataset as input and
creates a json list with 5 folds.
Please note that there are some hard-coded directory names as per the original dataset.
The function creates an instance processed dataset and then a 5 fold json file based on
the instance processed dataset
"""
data_dir = os.path.join(dataset_dir, "stage1_train")
saving_path = os.path.join(dataset_dir, "instance_processed_data")
if not os.path.exists(saving_path):
os.mkdir(saving_path)
# Process the images and create instance masks first
for idx, subdir in enumerate(os.listdir(data_dir)):
subdir_path = os.path.join(data_dir, subdir)
if os.path.isdir(subdir_path):
images_dir = os.path.join(subdir_path, "images")
masks_dir = os.path.join(subdir_path, "masks")
if os.path.isdir(images_dir) and os.path.isdir(masks_dir):
image_file = os.path.join(images_dir, os.listdir(images_dir)[0])
filename_prefix = f"kg_bowl_{idx}_"
mask_data = concatenate_masks(masks_dir)
# ## Apply channel-wise normalization and use only the first three channels
# image_data = imageio.imread(image_file)
# normalized_image = normalize_image(image_data[..., :3])
# imageio.imwrite(os.path.join(saving_path, f"{filename_prefix}img.tiff"), normalized_image)
shutil.copyfile(image_file, os.path.join(saving_path, f"{filename_prefix}img.png"))
imageio.imwrite(os.path.join(saving_path, f"{filename_prefix}img_masks.tiff"), mask_data)
directory = saving_path
# List to hold all image-mask pairs
data_pairs = []
all_data = {}
# Scan the directory for image files and create pairs
for filename in os.listdir(directory):
if filename.endswith("_img.png"):
# Construct the corresponding mask filename
mask_filename = filename.replace("_img.png", "_img_masks.tiff")
# Check if the corresponding mask file exists
if os.path.exists(os.path.join(directory, mask_filename)):
# Add the pair to the list
data_pairs.append(
{
"image": os.path.join("kaggle_dataset", "instance_processed_data", filename),
"label": os.path.join("kaggle_dataset", "instance_processed_data", mask_filename),
}
)
# Convert data_pairs to a numpy array for easy indexing by KFold
data_pairs_array = np.array(data_pairs)
# Initialize KFold
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# Assign fold numbers
for fold, (_train_index, val_index) in enumerate(kf.split(data_pairs_array)):
for idx in val_index:
data_pairs_array[idx]["fold"] = fold
# Convert the array back to a list and sort by fold
sorted_data_pairs = sorted(data_pairs_array.tolist(), key=lambda x: x["fold"])
print(sorted_data_pairs)
all_data["training"] = sorted_data_pairs
with open(json_f_path, "w") as j_file:
json.dump(all_data, j_file, indent=4)
j_file.close()
def deepbacs_json_file(dataset_dir, json_f_path):
"""
This function takes in the directory of deepbacs extracted dataset as input and
creates a json list with 5 folds. Separate testing set is recorded in the json list.
Please note that there are some hard-coded directory names as per the original dataset.
At the time of creation, the deepbacs zipfile had 'training' and 'test' directories that
both contained 'source' and 'target' directories
"""
# The directory containing your files
json_save_path = os.path.normpath(json_f_path)
directory = os.path.join(dataset_dir, "training")
test_directory = os.path.join(dataset_dir, "test")
# List to hold all image-mask pairs
data_pairs = []
test_data_pairs = []
all_data = {}
# Scan the directory for image files and create pairs
for filename in os.listdir(os.path.join(directory, "source")):
if os.path.exists(os.path.join(directory, "source", filename)):
# Construct the corresponding mask filename
mask_filename = filename
# Check if the corresponding mask file exists
if os.path.exists(os.path.join(directory, "target", mask_filename)):
# Add the pair to the list
data_pairs.append(
{
"image": os.path.join("deepbacs_dataset", "training", "source", filename),
"label": os.path.join("deepbacs_dataset", "training", "target", mask_filename),
}
)
# Convert data_pairs to a numpy array for easy indexing by KFold
data_pairs_array = np.array(data_pairs)
# Initialize KFold
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# Assign fold numbers
for fold, (_train_index, val_index) in enumerate(kf.split(data_pairs_array)):
for idx in val_index:
data_pairs_array[idx]["fold"] = fold
# Convert the array back to a list and sort by fold
sorted_data_pairs = sorted(data_pairs_array.tolist(), key=lambda x: x["fold"])
print(sorted_data_pairs)
# Scan the directory for image files and create pairs
for filename in os.listdir(os.path.join(test_directory, "source")):
if os.path.exists(os.path.join(test_directory, "source", filename)):
# Construct the corresponding mask filename
mask_filename = filename
# Check if the corresponding mask file exists
if os.path.exists(os.path.join(test_directory, "target", mask_filename)):
# Add the pair to the list
test_data_pairs.append(
{
"image": os.path.join("deepbacs_dataset", "test", "source", filename),
"label": os.path.join("deepbacs_dataset", "test", "target", filename),
}
)
all_data["training"] = sorted_data_pairs
all_data["testing"] = test_data_pairs
with open(json_save_path, "w") as j_file:
json.dump(all_data, j_file, indent=4)
j_file.close()
def cellpose_json_file(dataset_dir, json_f_path):
"""
This function takes in the directory of cellpose extracted dataset as input and
creates a json list with 5 folds. Separate testing set is recorded in the json list.
Please note that there are some hard-coded directory names as per the original dataset.
At the time of creation, the cellpose dataset had 'train.zip' and 'test.zip' that
extracted as 'train' and 'test' directories
"""
# The directory containing your files
json_save_path = os.path.normpath(json_f_path)
directory = os.path.join(dataset_dir, "train")
test_directory = os.path.join(dataset_dir, "test")
# List to hold all image-mask pairs
data_pairs = []
test_data_pairs = []
all_data = {}
# Scan the directory for image files and create pairs
for filename in os.listdir(directory):
if filename.endswith("_img.png"):
# Construct the corresponding mask filename
mask_filename = filename.replace("_img.png", "_masks.png")
# Check if the corresponding mask file exists
if os.path.exists(os.path.normpath(os.path.join(directory, mask_filename))):
# Add the pair to the list
data_pairs.append(
{
"image": os.path.join("cellpose_dataset", "train", filename),
"label": os.path.join("cellpose_dataset", "train", mask_filename),
}
)
# Convert data_pairs to a numpy array for easy indexing by KFold
data_pairs_array = np.array(data_pairs)
# Initialize KFold
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# Assign fold numbers
for fold, (_train_index, val_index) in enumerate(kf.split(data_pairs_array)):
for idx in val_index:
data_pairs_array[idx]["fold"] = fold
# Convert the array back to a list and sort by fold
sorted_data_pairs = sorted(data_pairs_array.tolist(), key=lambda x: x["fold"])
print(sorted_data_pairs)
# Scan the directory for image files and create pairs
for filename in os.listdir(test_directory):
if filename.endswith("_img.png"):
# Construct the corresponding mask filename
mask_filename = filename.replace("_img.png", "_masks.png")
# Check if the corresponding mask file exists
if os.path.exists(os.path.join(directory, mask_filename)):
# Add the pair to the list
test_data_pairs.append(
{
"image": os.path.join("cellpose_dataset", "test", filename),
"label": os.path.join("cellpose_dataset", "test", mask_filename),
}
)
all_data["training"] = sorted_data_pairs
all_data["testing"] = test_data_pairs
with open(json_save_path, "w") as j_file:
json.dump(all_data, j_file, indent=4)
j_file.close()
def extract_zip(zip_path, extract_to):
# Ensure the target directory exists
print(f"Extracting from: {zip_path}")
print(f"Extracting to: {extract_to}")
if not os.path.exists(extract_to):
os.makedirs(extract_to)
# Extract all contents of the zip file to the specified directory
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(extract_to)
def main():
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument("--dir", type=str, help="Directory of datasets to generate json", default="/set/the/path")
args = parser.parse_args()
data_root_path = os.path.normpath(args.dir)
if not os.path.exists(os.path.join(data_root_path, "json_files")):
os.mkdir(os.path.join(data_root_path, "json_files"))
dataset_dict = {
"cellpose_dataset": ["train.zip", "test.zip"],
"deepbacs_dataset": ["deepbacs.zip"],
"kaggle_dataset": ["data-science-bowl-2018.zip"],
"nips_dataset": ["nips_train.zip", "nips_test.zip"],
"omnipose_dataset": ["datasets.zip"],
"tissuenet_dataset": ["tissuenet_v1.0.zip"],
"livecell_dataset": [
"livecell-dataset.s3.eu-central-1.amazonaws.com/LIVECell_dataset_2021/images_per_celltype.zip"
],
}
for key, value in dataset_dict.items():
dataset_path = os.path.join(data_root_path, key)
for each_zipped in value:
in_path = os.path.join(dataset_path, each_zipped)
try:
if os.path.exists(in_path):
print(f"File exists at: {in_path}")
except Exception:
print(f"File: {in_path} was not found")
out_path = os.path.join(dataset_path)
extract_zip(in_path, out_path)
print(
"If we reached here, that means all zip files got extracted ... Working on pre-processing and generating json files"
)
# Looping over all datasets again, Cellpose & Deepbacs have a similar directory structure
for key, _value in dataset_dict.items():
if key == "cellpose_dataset":
print("Creating Cellpose Dataset Json file ...")
dataset_path = os.path.join(data_root_path, key)
json_path = os.path.join(data_root_path, "json_files", "cellpose.json")
cellpose_json_file(dataset_dir=dataset_path, json_f_path=json_path)
elif key == "nips_dataset":
print("Creating NIPS Dataset Json file ...")
dataset_path = os.path.join(data_root_path, key)
json_path = os.path.join(data_root_path, "json_files", "nips.json")
nips_json_file(dataset_dir=dataset_path, json_f_path=json_path)
elif key == "omnipose_dataset":
print("Creating Omnipose Dataset Json files ...")
dataset_path = os.path.join(data_root_path, key)
json_path = os.path.join(data_root_path, "json_files")
omnipose_json_file(dataset_dir=dataset_path, json_path=json_path)
elif key == "kaggle_dataset":
print("Needs additional extraction")
train_zip_path = os.path.join(data_root_path, key, "stage1_train.zip")
zip_out_path = os.path.join(data_root_path, key, "stage1_train")
extract_zip(train_zip_path, zip_out_path)
print("Creating Kaggle Dataset Json files ...")
dataset_path = os.path.join(data_root_path, key)
json_f_path = os.path.join(data_root_path, "json_files", "kaggle.json")
kaggle_json_file(dataset_dir=dataset_path, json_f_path=json_f_path)
elif key == "livecell_dataset":
print("Creating LiveCell Dataset Json files ... Please note that 7 files will be created from livecell")
dataset_path = os.path.join(data_root_path, key)
json_base_name = os.path.join(data_root_path, "json_files")
livecell_json_files(dataset_dir=dataset_path, json_f_path=json_base_name)
elif key == "deepbacs_dataset":
print("Creating Deepbacs Dataset Json file ...")
dataset_path = os.path.join(data_root_path, key)
json_path = os.path.join(data_root_path, "json_files", "deepbacs.json")
deepbacs_json_file(dataset_dir=dataset_path, json_f_path=json_path)
elif key == "tissuenet_dataset":
print("Creating TissueNet Dataset Json files ... Please note that 13 files will be created from tissuenet")
dataset_path = os.path.join(data_root_path, key)
json_base_name = os.path.join(data_root_path, "json_files")
tissuenet_json_files(dataset_dir=dataset_path, json_f_path=json_base_name)
return None
if __name__ == "__main__":
main()
|