Skip to content

Commit

Permalink
Alpha-Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
platonic-realm committed Sep 13, 2022
1 parent 5a31c54 commit 173339b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
18 changes: 13 additions & 5 deletions src/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ def __init__(self, _configs):
self.project_name = _configs['project_name']
self.batch_size = _configs['batch_size']
self.embedding_dimensionality = _configs['dimensionality']
self.base_directory = _configs['base_dir']
self.source_directory = _configs['source_dir']
self.output_segmentation_directory = _configs['result_segmentation_dir']
self.output_npy_directory = _configs['npy_dir']
self.output_morphometry_directory = _configs['result_morphometry_dir']
self.resource_alloc_value = _configs['resource_allocation']
self.is_stacked = _configs['is_stacked']
Expand All @@ -76,8 +78,8 @@ def __init__(self, _configs):
self.SHARED_MEMORY_SIZE = 20
self.MIN_PIXELS = 10
self.BICO_DIR = 'res/bico/'
self.TEMP_DIR = self.source_directory + '/temp/'
self.LOG_DIR = self.source_directory + '/log/'
self.TEMP_DIR = self.base_directory + '/temp/'
self.LOG_DIR = self.base_directory + '/log/'
self.CC_SCALE = 4

self.no_of_tile_processes, self.no_of_cluster_processes = \
Expand All @@ -99,6 +101,9 @@ def __init__(self, _configs):
if not os.path.exists(self.output_segmentation_directory):
os.mkdir(self.output_segmentation_directory)

if not os.path.exists(self.output_npy_directory):
os.mkdir(self.output_npy_directory)

if not os.path.exists(self.LOG_DIR):
os.mkdir(self.LOG_DIR)

Expand All @@ -112,7 +117,8 @@ def __init__(self, _configs):

logging.info(f"Creating the dataset from images.")
# Creating the dataset
self.dataset = PredictionDataset(_source_directory=self.source_directory,
self.dataset = PredictionDataset(_configs=self.configs,
_source_directory=self.source_directory,
_target_resolution=self.TARGET_RESOLUTION,
_sample_dimension=self.SAMPLE_SIZE,
_steps=self.DATASET_STEPS)
Expand Down Expand Up @@ -283,7 +289,7 @@ def exec(self):

if self.proceed[0]:
self.configs['is_segmentation_finished'] = True
config_file_path = os.path.join(self.source_directory, "conf.json")
config_file_path = os.path.join(self.base_directory, "conf.json")
with open(config_file_path, 'w+') as file:
file.write(json.dumps(self.configs))

Expand Down Expand Up @@ -381,9 +387,11 @@ def collector_procedure(self, _event_finished):
filepath = self.dataset.image_files[image_id]
mask_inst = mask_img[0]
mask_sem = mask_img[1]

npy_out_dir, _ = mkdirs(self.output_npy_directory, filepath)
sub_out_dir, fn_short = mkdirs(self.output_segmentation_directory, filepath)

np.save(os.path.join(sub_out_dir, "%s_pred.npy" % fn_short[:-4]), mask_img)
np.save(os.path.join(npy_out_dir, "%s_pred.npy" % fn_short[:-4]), mask_img)

result_file_name = os.path.join(sub_out_dir, "%s_pred.png" % fn_short[:-4])
collector_logger.debug(f"Saving the result to: {result_file_name}")
Expand Down
8 changes: 5 additions & 3 deletions src/morph.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ def __init__(self, _configs):
self.configs = _configs
self.project_id = _configs['project_id']
self.project_name = _configs['project_name']
self.base_directory = _configs['base_dir']
self.source_directory = _configs['source_dir']
self.npy_directory = _configs['npy_dir']
self.output_segmentation_directory = _configs['result_segmentation_dir']
self.output_morphometry_directory = _configs['result_morphometry_dir']

self.LOG_DIR = self.source_directory + '/log/'
self.LOG_DIR = self.base_directory + '/log/'
self.morphometry_logger = self.get_logger("morphometry")

if not os.path.exists(self.output_morphometry_directory):
Expand All @@ -51,14 +53,14 @@ def exec(self):
self.morphometry_logger.info("Morphometry process started")

self.foot_processes_parameter_table(self.source_directory,
self.output_segmentation_directory,
self.npy_directory,
self.output_morphometry_directory)

self.combine_FP_SD(self.output_morphometry_directory)
self.morphometry_logger.info("Morphometry process finished")

self.configs['is_morphometry_finished'] = True
config_file_path = os.path.join(self.source_directory, "conf.json")
config_file_path = os.path.join(self.base_directory, "conf.json")
with open(config_file_path, 'w+') as file:
file.write(json.dumps(self.configs))

Expand Down
22 changes: 16 additions & 6 deletions src/nn/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@


class PredictionDataset(Dataset):
def __init__(self, _source_directory, _target_resolution, _sample_dimension, _steps):
def __init__(self, _configs, _source_directory, _target_resolution, _sample_dimension, _steps):

self.configs = _configs
self.source_directory = _source_directory

self.dimension = _sample_dimension
Expand Down Expand Up @@ -55,12 +56,21 @@ def n_imgs(self):

def read_file(self, fn):
img = tifffile.imread(os.path.join(self.source_directory, fn))
if len(img.shape) > 3:
img = np.max(img, axis=1)
if (len(img.shape) == 3) and (img.shape[0] > 2):
img = np.max(img, axis=0)

# Choosing the channel and max projecting if needed

if self.configs['is_stacked']:
if len(img.shape) > 3:
if img.shape[0] == 2:
img = np.max(img, axis=1)
else:
img = np.max(img, axis=0)
if len(img.shape) == 3:
img = np.max(img, axis=0)

if len(img.shape) > 2:
img = img[0]
img = img[self.configs['target_channel']]

img = Image.fromarray(img)
w, h = img.size
res = get_resolution(os.path.join(self.source_directory, fn), w)
Expand Down
14 changes: 10 additions & 4 deletions src/ui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def check_project_status(self):
percentage = 99
self.progress_dialog.setValue(percentage)
elif self.morphometry is not None:
percentage = (self.morphometry.no_of_processed_images[0] + 1) / self.morphometry.no_of_images[0] * 100
percentage = (self.morphometry.no_of_processed_images[0]) / self.morphometry.no_of_images[0] * 100
if percentage > 99:
percentage = 99
self.progress_dialog.setValue(percentage)
Expand Down Expand Up @@ -387,7 +387,7 @@ def add_project_click(self):
return

# Copying the selected directory and images to the project
destination_directory = f"{os.getcwd()}/{PROJECT_DIR}/{selected_path.name}"
destination_directory = f"{os.getcwd()}/{PROJECT_DIR}/{selected_path.name}/"

progressDialog = create_progress_dialog(f'Creating project "{selected_path.name}"', 'Please wait', self)
progressDialog.show()
Expand All @@ -407,19 +407,25 @@ def add_project_click(self):
return

os.mkdir(destination_directory)

images_directory = os.path.join(destination_directory, 'images/')
os.mkdir(images_directory)

for tiff_image in tiff_files:
# Prevents UI freeze
QApplication.processEvents()

full_name = os.path.join(selected_directory, tiff_image)
if os.path.isfile(full_name):
shutil.copy(full_name, destination_directory)
shutil.copy(full_name, images_directory)

# Saving the configuration file into the project directory (Do not confuse with 'Projects' dir)
project_configuration = {
"project_id": f"{uuid.uuid4()}",
"project_name": f"{selected_path.name}",
"source_dir": f"./{PROJECT_DIR}/{selected_path.name}/",
"base_dir": f"./{PROJECT_DIR}/{selected_path.name}/",
"source_dir": f"./{PROJECT_DIR}/{selected_path.name}/images/",
"npy_dir": f"./{PROJECT_DIR}/{selected_path.name}/npy/",
"result_segmentation_dir": f"./{PROJECT_DIR}/{selected_path.name}/segmentation/",
"result_morphometry_dir": f"./{PROJECT_DIR}/{selected_path.name}/morphometry/",
"resource_allocation": 3,
Expand Down
2 changes: 1 addition & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def analyze_tiff_files(_path):
# Based on the reviewed data samples, if number of channels is 2, meaning "tiff_image.shape[1][1] == 2"
# Only the first channel is usable, and the images are not stacked.
# If no of channels is bigger than 2, the tiff image contains multiple stacked images
if tiff_rank == 4 and tiff_image.shape[1] > 2:
if tiff_rank == 4 and (tiff_image.shape[1] > 2 or tiff_image.shape[0] > 2):
is_stacked = True
elif tiff_rank == 3 and tiff_image.shape[0] > 2:
is_stacked = True
Expand Down

0 comments on commit 173339b

Please sign in to comment.