|
@@ -34,7 +34,7 @@ def find_optimal_index(slice_path, template_path, approx, temporary_directory, d
|
|
|
nib.save(nib.Nifti1Image(registered_data, np.eye(4)), registered_loc)
|
|
|
|
|
|
mutualinfo = tools.mutual_info_mask(template_data, registered_data)
|
|
|
- norm_factor = scipy.stats.norm.pdf(i-approx+dist, dist, dist*2) / scipy.stats.norm.pdf(dist, dist, dist*2)
|
|
|
+ norm_factor = scipy.stats.norm.pdf(i-approx+dist, dist, dist*10) / scipy.stats.norm.pdf(dist, dist, dist*10)
|
|
|
dice_coef = tools.dice_coef(template_data, registered_data)
|
|
|
weights.append((i, norm_factor * (0.7 * mutualinfo + 0.3 * dice_coef)))
|
|
|
|
|
@@ -47,7 +47,6 @@ def apply_transform(segmentation, fixed, index, out_dir, temporary_directory):
|
|
|
|
|
|
seg_slice_loc = out_dir / "segment_slice.nii"
|
|
|
tools.save_slice(str(segmentation), str(seg_slice_loc), index, np.eye(4), nib.load(str(fixed)).header)
|
|
|
- # post_transform_loc = out_dir / f'{seg_slice_loc.stem}_t.nii'
|
|
|
post_transform_loc = out_dir / f'Segmentation.nii'
|
|
|
|
|
|
template_slice_loc = str(temporary_directory / f't_slice{index}.nii')
|
|
@@ -68,14 +67,10 @@ def apply_transform(segmentation, fixed, index, out_dir, temporary_directory):
|
|
|
return post_transform_loc
|
|
|
|
|
|
def generate_segmentation(slice_path, segment_path, template_path, approx_index, dapi, output_directory):
|
|
|
-
|
|
|
+ tools.create_dir(output_directory)
|
|
|
+
|
|
|
if slice_path.endswith('.tiff') or slice_path.endswith('.tif'):
|
|
|
Image.MAX_IMAGE_PIXELS = None
|
|
|
- # tif_image = Image.open(slice_path)
|
|
|
- # ch_nm = (list(tif_image.tag_v2[270].split('='))[2])
|
|
|
- # if int(ch_nm[0]) < dapi:
|
|
|
- # print(f'DAPI channel out of range, max range is {ch_nm[0]} channels')
|
|
|
- # sys.exit()
|
|
|
tif_path = preprocess.tiff_to_nii(slice_path, output_directory)
|
|
|
slice_path = tif_path[0]
|
|
|
elif not slice_path.endswith(".nii"):
|
|
@@ -84,15 +79,17 @@ def generate_segmentation(slice_path, segment_path, template_path, approx_index,
|
|
|
|
|
|
out_subdir = Path(output_directory) / Path(slice_path).stem.replace(' ', '_').replace(',','_').replace('.','')
|
|
|
tools.create_dir(out_subdir)
|
|
|
- temporary_directory = Path(output_directory) / Path(out_subdir) / 'tmp'
|
|
|
+ temporary_directory = Path(out_subdir) / 'tmp'
|
|
|
tools.create_dir(temporary_directory)
|
|
|
channel_paths = preprocess.split_nii_channels(slice_path, out_subdir, True, dapi-1)
|
|
|
optimal_index = find_optimal_index(channel_paths[dapi-1], template_path, approx_index, temporary_directory)
|
|
|
seg_loc = apply_transform(segment_path, channel_paths[dapi-1], optimal_index, out_subdir, temporary_directory)
|
|
|
+
|
|
|
tools.remove_dir(temporary_directory)
|
|
|
os.remove(Path(out_subdir) / 'segment_slice.nii')
|
|
|
os.rename(Path(out_subdir) / 'TemplateSlice_Composite.h5', Path(out_subdir) / 'Transformation_Composite.h5')
|
|
|
os.rename(Path(out_subdir) / 'TemplateSlice_InverseComposite.h5', Path(out_subdir) / 'Transformation_InverseComposite.h5')
|
|
|
+
|
|
|
return seg_loc
|
|
|
|
|
|
if __name__ == "__main__":
|