Skip to content

Commit

Permalink
Merge 781b813 into 92f1013
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuboudreau committed Jan 7, 2022
2 parents 92f1013 + 781b813 commit 74cbdab
Show file tree
Hide file tree
Showing 26 changed files with 489 additions and 1,625 deletions.
46 changes: 46 additions & 0 deletions AxonDeepSeg/apply_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from pathlib import Path

# AxonDeepSeg imports
import AxonDeepSeg.ads_utils as ads
from AxonDeepSeg.visualization.merge_masks import merge_masks
from config import axon_suffix, myelin_suffix, axonmyelin_suffix

from ivadomed import inference as imed_inference

def axon_segmentation(
path_acquisitions_folders,
acquisitions_filenames,
path_model_folder,
overlap_value=[48,48],
acquired_resolution=None,
verbosity_level = 0
):
'''
Segment images using IVADOMED.
:param path_acquisitions_folders: the directory containing the images to segment.
:param acquisitions_filenames: filenames of the images to segment.
:param path_model_folder: path to the folder of the IVADOMED-trained model.
:param overlap_value: the number of pixels to be used for overlap when doing prediction. Higher value means less
border effects but more time to perform the segmentation.
:param acquired_resolution: isotropic pixel size of the acquired images.
:param verbosity_level: Level of verbosity. The higher, the more information is given about the segmentation
process.
:return: Nothing.
'''

path_model=path_model_folder
input_filenames = acquisitions_filenames
options = {"pixel_size": [acquired_resolution, acquired_resolution], "overlap_2D":overlap_value, "binarize_maxpooling": True}

# IVADOMED automated segmentation
nii_lst, target_lst = imed_inference.segment_volume(str(path_model), input_filenames, options=options)

imed_inference.pred_to_png(nii_lst, target_lst, str(Path(input_filenames[0]).parent / Path(input_filenames[0]).stem))
if verbosity_level >= 1:
print(Path(path_acquisitions_folders) / (Path(input_filenames[0]).stem + str(axonmyelin_suffix)))

merge_masks(
Path(path_acquisitions_folders) / (Path(input_filenames[0]).stem + str(axon_suffix)),
Path(path_acquisitions_folders) / (Path(input_filenames[0]).stem + str(myelin_suffix)),
Path(path_acquisitions_folders) / (Path(input_filenames[0]).stem + str(axonmyelin_suffix))
)
56 changes: 30 additions & 26 deletions AxonDeepSeg/download_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@


def download_model(destination = None):
sem_release_version = 'r20211209v2'
tem_release_version = 'r20211111v3'
bf_release_version = 'r20211210'

if destination is None:
sem_destination = Path("AxonDeepSeg/models/default_SEM_model")
tem_destination = Path("AxonDeepSeg/models/default_TEM_model")
model_seg_pns_bf_destination = Path("AxonDeepSeg/models/model_seg_pns_bf")
sem_destination = Path("AxonDeepSeg/models/model_seg_rat_axon-myelin_sem")
tem_destination = Path("AxonDeepSeg/models/model_seg_mouse_axon-myelin_tem")
bf_destination = Path("AxonDeepSeg/models/model_seg_rat_axon-myelin_bf")
else:
destination = convert_path(destination)
sem_destination = destination / "default_SEM_model"
tem_destination = destination / "default_TEM_model"
model_seg_pns_bf_destination = destination / "model_seg_pns_bf"
sem_destination = destination / "model_seg_rat_axon-myelin_sem"
tem_destination = destination / "model_seg_mouse_axon-myelin_tem"
bf_destination = destination / "model_seg_rat_axon-myelin_bf"

url_TEM_model = "https://github.com/axondeepseg/default-TEM-model/archive/refs/tags/r20210615.zip"
url_SEM_model = "https://github.com/axondeepseg/default-SEM-model/archive/refs/tags/r20210615.zip"
url_model_seg_pns_bf = "https://github.com/axondeepseg/model-seg-pns-bf/archive/refs/tags/r20210615.zip"
url_sem_destination = "https://github.com/axondeepseg/default-SEM-model/archive/refs/tags/" + sem_release_version + ".zip"
url_tem_destination = "https://github.com/axondeepseg/default-TEM-model/archive/refs/tags/" + tem_release_version + ".zip"
url_bf_destination = "https://github.com/axondeepseg/default-BF-model/archive/refs/tags/" + bf_release_version + ".zip"

files_before = list(Path.cwd().iterdir())

if (
not download_data(url_TEM_model) and not download_data(url_SEM_model) and not download_data(url_model_seg_pns_bf)
not download_data(url_sem_destination) and not download_data(url_tem_destination) and not download_data(url_bf_destination)
) == 1:
print("Data downloaded and unzipped succesfully.")
else:
Expand All @@ -32,29 +35,30 @@ def download_model(destination = None):
files_after = list(Path.cwd().iterdir())

# retrieving unknown model folder names
model_folders = list(set(files_after)-set(files_before))
folder_name_TEM_model = ''.join([str(x) for x in model_folders if 'TEM' in str(x)])
folder_name_SEM_model = ''.join([str(x) for x in model_folders if 'SEM' in str(x)])
folder_name_OM_model = ''.join([str(x) for x in model_folders if 'pns-bf' in str(x)])
folder_name_SEM_model = Path("default-SEM-model-" + sem_release_version)
folder_name_TEM_model = Path("default-TEM-model-" + tem_release_version)
folder_name_BF_model = Path("default-BF-model-" + bf_release_version)

if sem_destination.exists():
print('SEM model folder already existed - deleting old one.')
print('SEM model folder already existed - deleting old one')
shutil.rmtree(str(sem_destination))

if tem_destination.exists():
print('TEM model folder already existed - deleting old one.')
shutil.rmtree(str(tem_destination))
if model_seg_pns_bf_destination.exists():
print('Bright Field Optical Microscopy model folder already existed - deleting old one')
shutil.rmtree(str(model_seg_pns_bf_destination))
print('TEM model folder already existed - deleting old one')
shutil.rmtree(str(tem_destination))

if bf_destination.exists():
print('OM model folder already existed - deleting old one')
shutil.rmtree(str(bf_destination))

shutil.move(Path(folder_name_SEM_model).joinpath("default_SEM_model"), str(sem_destination))
shutil.move(Path(folder_name_TEM_model).joinpath("default_TEM_model"), str(tem_destination))
shutil.move(Path(folder_name_OM_model).joinpath("model_seg_pns_bf"), str(model_seg_pns_bf_destination))
shutil.move(folder_name_SEM_model.joinpath("model_seg_rat_axon-myelin_sem"), str(sem_destination))
shutil.move(folder_name_TEM_model.joinpath("model_seg_mouse_axon-myelin_tem"), str(tem_destination))
shutil.move(folder_name_BF_model.joinpath("model_seg_rat_axon-myelin_bf"), str(bf_destination))

# remove temporary folders
shutil.rmtree(folder_name_TEM_model)
shutil.rmtree(folder_name_SEM_model)
shutil.rmtree(folder_name_OM_model)
shutil.rmtree(folder_name_TEM_model)
shutil.rmtree(folder_name_BF_model)

def main(argv=None):
download_model()
2 changes: 1 addition & 1 deletion AxonDeepSeg/download_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def download_tests(destination=None):
destination = convert_path(destination)
test_files_destination = destination / "__test_files__"

url_tests = "https://github.com/axondeepseg/data-testing/archive/refs/tags/r20210906b.zip"
url_tests = "https://github.com/axondeepseg/data-testing/archive/refs/tags/r20211210.zip"
files_before = list(Path.cwd().iterdir())

if (
Expand Down
15 changes: 3 additions & 12 deletions AxonDeepSeg/integrity_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# AxonDeepSeg imports
from AxonDeepSeg.testing.segmentation_scoring import pw_dice
from AxonDeepSeg.apply_model import axon_segmentation
import AxonDeepSeg.ads_utils as ads
import AxonDeepSeg.ads_utils
from config import axonmyelin_suffix
Expand All @@ -24,24 +25,14 @@ def integrity_test():
# input parameters

path = Path('folder_name') / 'file_name'
model_name = 'default_SEM_model'
model_name = 'model_seg_rat_axon-myelin_sem'
path_model = dir_path / 'models' / model_name
path_testing = path_model / 'data_test'
path_configfile = path_model / 'config_network.json'
image = Path("image.png")

# Read the configuration file
print('Reading test configuration file.')
if not path_model.exists():
path_model.mkdir(parents=True)

with open(path_configfile, 'r') as fd:
config_network = json.loads(fd.read())


# Launch the axon and myelin segmentation on test image sample provided in the installation
print('Computing the segmentation of axon and myelin on test image.')
prediction = axon_segmentation([path_testing], [str(image)], path_model, config_network, prediction_proba_activate=True, verbosity_level=4)
axon_segmentation(path_testing, [str(path_testing / image)], path_model, overlap_value=[48,48], acquired_resolution=0.13)

# Read the ground truth mask and the obtained segmentation mask
mask = ads.imread(path_testing / 'mask.png')
Expand Down
23 changes: 15 additions & 8 deletions AxonDeepSeg/morphometrics/compute_morphometrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import math
import numpy as np
from scipy import ndimage as ndi
from skimage import measure, morphology
from skimage import measure
from skimage.segmentation import watershed

# Graphs and plots imports
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
Expand Down Expand Up @@ -99,7 +100,7 @@ def get_axon_morphometrics(im_axon, path_folder=None, im_myelin=None, pixel_size
im_centroid[ind_centroid[0][i], ind_centroid[1][i]] = i + 1

# Watershed segmentation of axonmyelin using distance map
im_axonmyelin_label = morphology.watershed(-distance, im_centroid, mask=im_axonmyelin)
im_axonmyelin_label = watershed(-distance, im_centroid, mask=im_axonmyelin)
# Measure properties of each axonmyelin object
axonmyelin_objects = measure.regionprops(im_axonmyelin_label)

Expand Down Expand Up @@ -135,7 +136,13 @@ def get_axon_morphometrics(im_axon, path_folder=None, im_myelin=None, pixel_size
'axon_perimeter': axon_perimeter,
'solidity': solidity,
'eccentricity': eccentricity,
'orientation': orientation}
'orientation': orientation,
'gratio': 0,
'myelin_thickness': 0,
'myelin_area': 0,
'axonmyelin_area': 0,
'axonmyelin_perimeter': 0
}

# Deal with myelin
if im_myelin is not None:
Expand Down Expand Up @@ -164,11 +171,11 @@ def get_axon_morphometrics(im_axon, path_folder=None, im_myelin=None, pixel_size
stats['axonmyelin_perimeter'] = axonmyelin_perimeter
except ZeroDivisionError:
print(f"ZeroDivisionError caught on invalid object #{idx}.")
stats['gratio'] = float('NaN')
stats['myelin_thickness'] = float('NaN')
stats['myelin_area'] = float('NaN')
stats['axonmyelin_area'] = float('NaN')
stats['axonmyelin_perimeter'] = float('NaN')
stats['gratio'] = np.nan
stats['myelin_thickness'] = np.nan
stats['myelin_area'] = np.nan
stats['axonmyelin_area'] = np.nan
stats['axonmyelin_perimeter'] = np.nan

else:
print(
Expand Down
8 changes: 4 additions & 4 deletions AxonDeepSeg/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ def remove_intersection(mask_1, mask_2, priority=1, return_overlap=False):
if priority not in [1, 2]:
raise Exception("Parameter priority can only be 1 or 2")

array_1 = mask_1.astype(np.bool)
array_2 = mask_2.astype(np.bool)
array_1 = mask_1.astype(bool)
array_2 = mask_2.astype(bool)
intersection = (array_1 & array_2).astype(np.uint8)

if priority is 1:
if priority == 1:
mask_2 = mask_2 - intersection
if priority is 2:
if priority == 2:
mask_1 = mask_1 - intersection

if return_overlap is True:
Expand Down
Loading

0 comments on commit 74cbdab

Please sign in to comment.