Skip to content

Commit

Permalink
Merge pull request #6 from VemundFredriksen/tumor_insertion_improvement
Browse files Browse the repository at this point in the history
Tumor insertion improvement
  • Loading branch information
sosevle authored Nov 10, 2023
2 parents a759560 + c7de1c6 commit c632e15
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion assets/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
2. Download the MSD Lung Tumor dataset from [here](https://drive.google.com/drive/folders/1HqEgzS8BV2c7xYNrZdEAnrHk7osJJ--2).
3. Extract the zip file into `/assets/`.
4. Run `synthlung format --dataset msd` to adjust dataset format
5. Run `synthlung seed --dataset` to extract tumor seeds from the dataset
5. Run `synthlung seed --dataset msd` to extract tumor seeds from the dataset
6. Run `synthlung host --dataset msd` to extract lung masks from the images
10 changes: 5 additions & 5 deletions synthlung/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def format_msd():

def generate_randomized_tumors():
tumor_inserter = InsertTumorPipeline()
json_file_path = "./assets/source/msd/dataset.json"
json_file_path = "./assets/source/dataset.json"
with open(json_file_path, 'r') as json_file:
image_dict = json.load(json_file)

json_seed_path = "./assets/seeds/msd/dataset.json"
json_seed_path = "./assets/seeds/dataset.json"
with open(json_seed_path, 'r') as json_file:
seeds_dict = json.load(json_file)

Expand All @@ -36,12 +36,12 @@ def generate_randomized_tumors():
def mask_hosts():
lung_masker = LMInferer()
host_masker = LungMaskPipeline(lung_masker)
json_file_path = "./assets/source/msd/dataset.json"
json_file_path = "./assets/source/dataset.json"
with open(json_file_path, 'r') as json_file:
image_dict = json.load(json_file)

#host_masker(image_dict)
json_generator = HostJsonGenerator('./assets/hosts/msd/')
host_masker(image_dict)
json_generator = HostJsonGenerator('./assets/hosts/')
json_generator.generate_json()

def main():
Expand Down
2 changes: 1 addition & 1 deletion synthlung/utils/lung_segmentation_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, lungmask_inferer: LMInferer) -> None:
self.compose = Compose([
LoadImaged(keys=['image'], image_only = False),
MaskLungs(lungmask_inferer=self.inferer),
SaveImaged(keys=['mask'], output_dir='./assets/hosts/msd/', output_postfix='', separate_folder=False)
SaveImaged(keys=['mask'], output_dir='./assets/hosts/', output_postfix='', separate_folder=False)
])

def __call__(self, image_dict) -> Any:
Expand Down
67 changes: 36 additions & 31 deletions synthlung/utils/tumor_insertion_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,61 @@
import random
import json
import os
import requests
import secrets

seed = 3

class InsertTumor(object):
def __call__(self, sample) -> None:
image, label, seed_image, seed_label = sample['image'], sample['label'], sample['seed_image'], sample['seed_label']
image, label, image_mask, seed_image, seed_label = sample['image'], sample['label'], sample["image_mask"], sample['seed_image'], sample['seed_label']

lungmask = self.__generate_random_lungmask__(image)
offset_randomizer = np.random.default_rng(3)
offset_randomizer = np.random.default_rng(seed)

while (True): #Temporary offset calculation
offset_location = (offset_randomizer.standard_normal() * (sample['image_meta_dict']['spatial_shape'] - seed_image.shape)).astype(int)
if (lungmask[offset_location[0], offset_location[1], offset_location[2]] == 1):
while (True):
offset_location = (offset_randomizer.random(size=3) * sample['image_meta_dict']['spatial_shape']).astype(int)
if (image_mask[offset_location[0], offset_location[1], offset_location[2]] > 0):
break

offset_location = offset_location - np.array(seed_image.shape) // 2

image[offset_location[0] : offset_location[0] + seed_image.shape[0], offset_location[1] : offset_location[1] + seed_image.shape[1], offset_location[2] : offset_location[2] + seed_image.shape[2]][seed_label == 1] = seed_image[seed_label == 1]
label[offset_location[0] : offset_location[0] + seed_image.shape[0], offset_location[1] : offset_location[1] + seed_image.shape[1], offset_location[2] : offset_location[2] + seed_image.shape[2]][seed_label == 1] = 1

self.__remove_filename__(image)
self.__remove_filename__(label)
sample["image_meta_dict"]["filename_or_obj"] = sample["randomized_image"]
sample["label_meta_dict"]["filename_or_obj"] = sample["randomized_label"]

return sample

def __generate_random_lungmask__(self, image): #Temporary method
shape_dim_0 = image.shape[0]
shape_dim_1 = image.shape[1]
shape_dim_2 = image.shape[2]
dim0min = shape_dim_0 // 4
dim0max = 3 * dim0min
dim1min = shape_dim_1 // 4
dim1max = 3 * dim1min
dim2min = shape_dim_2 // 4
dim2max = 3 * dim2min
arra = np.where(image > 0, 0, 0)
arra[dim0min : dim0max, dim1min : dim1max, dim2min : dim2max] = 1
return arra

def __remove_filename__(self, image):
image.meta['filename_or_obj'] = ""

class InsertTumorPipeline(object):
def __init__(self) -> None:
self.randomized_dict = []
self.time = f"{datetime.datetime.now()}".replace(" ", "-").replace(":", ".")
#self.time = f"{datetime.datetime.now()}".replace(" ", "-").replace(":", ".")
self.time = "00-00"
self.dir_name = f"./assets/artificial_tumors/{self.time}/"
self.compose = Compose([
LoadImaged(keys=['image', 'label', 'seed_image', 'seed_label']),
LoadImaged(keys=['image', 'label', 'image_mask', 'seed_image', 'seed_label']),
InsertTumor(),
SaveImaged(keys=['image'], output_dir=f"{self.dir_name}randomzied_images/", output_postfix="image"),
SaveImaged(keys=['label'], output_dir=f"{self.dir_name}randomzied_images/", output_postfix="label")
SaveImaged(keys=['image'], output_dir=f"{self.dir_name}randomzied_images/", output_postfix="", separate_folder=False),
SaveImaged(keys=['label'], output_dir=f"{self.dir_name}randomzied_images/", output_postfix="", separate_folder=False)
])

def __call__(self, image_dict, seeds_dict) -> None:
self.__generate_randomized_dict__(image_dict, seeds_dict)
self.compose(self.randomized_dict)

def __generate_randomized_dict__(self, image_dict, seeds_dict):
for n, i in enumerate(range(1)):
for n, i in enumerate(range(10)):
image = random.choice(image_dict)
seed = random.choice(seeds_dict)

image["image_mask"] = image["label"].replace("/source/", "/hosts/").replace("source_", "host_")

output_name = {"randomized_image" : f"{self.dir_name}randomzied_images/image_{n}.nii.gz",
"randomized_label" : f"{self.dir_name}randomzied_images/label_{n}.nii.gz"}

self.randomized_dict.append({**image, **seed, **output_name})

self.__log__()

def __log__(self):
Expand All @@ -79,3 +70,17 @@ def __log__(self):

with open(log_name + "dataset.json", 'w') as json_file:
json.dump(self.randomized_dict, json_file, indent=4)

if (True):
return

for elem in self.randomized_dict:
jsonData = {"id" : str(secrets.token_hex(12))}
jsonData = {**jsonData, **elem}
jsonData = json.dumps(jsonData)
url = "http://localhost:5167/api/Log"
header = {"Content-Type" : "application/json"}

print(jsonData)

requests.post(url, data=jsonData, headers=header, verify=False)

0 comments on commit c632e15

Please sign in to comment.