Commit 243c678e authored by Fernando Perez-Garcia's avatar Fernando Perez-Garcia

Add some test scripts

parent c54ba638
Pipeline #1078 failed with stages
in 3 seconds
from pathlib import Path
import numpy as np
import nibabel as nib
from vesseg import nifti
from vesseg.network.evaluation.metrics import (
get_confusion_matrix_elements_from_arrays,
dice,
precision,
recall,
)
def lncc(x, y):
EPSILON = 1e-6
cc = x * y
den = x.std() * y.std() + EPSILON
normalized = cc / den
return normalized.mean()
tmp_dir = Path('/tmp/lncc')
label_path = tmp_dir / 'pre.nii.gz'
prediction_path = tmp_dir / 'intra_on_pre.nii.gz'
label_nii = nifti.load(label_path)
label_array = label_nii.get_data()
prediction_array = nifti.load(prediction_path).get_data()
input_shape = np.array(label_array.shape)
input_origin = label_nii.affine[:3, 3]
patch_length = 11 # around 12 mm
patch_shape = np.array(3 * [patch_length]) # use cubic patch for now
patch_radius = patch_shape // 2
first_voxel = patch_radius
output_origin = nib.affines.apply_affine(label_nii.affine, first_voxel)
output_shape = input_shape - (patch_shape - 1)
output_shape = output_shape.astype(np.uint16)
last_voxel = first_voxel + output_shape # - 1 # not included so -1 not necessary
lncc_path = tmp_dir / f'lncc_{patch_radius}.nii.gz'
lncc_array = np.empty(output_shape)
lncc_affine = np.copy(label_nii.affine)
lncc_affine[:3, 3] = output_origin
oi, oj, ok = output_shape
for i in range(oi):
print(f'{i}/{oi}')
for j in range(oj):
for k in range(ok):
output_voxel = np.array([i, j, k])
input_voxel = first_voxel + output_voxel
input_voxel_ini = input_voxel - patch_radius
input_voxel_fin = input_voxel + patch_radius + 1
i_ini, j_ini, k_ini = input_voxel_ini
i_fin, j_fin, k_fin = input_voxel_fin
label_patch = label_array[i_ini:i_fin, j_ini:j_fin, k_ini:k_fin]
pred_patch = prediction_array[i_ini:i_fin, j_ini:j_fin, k_ini:k_fin]
lncc_value = -lncc(label_patch, pred_patch)
lncc_array[i, j, k] = lncc_value
nifti.save(lncc_array, lncc_affine, lncc_path)
from pathlib import Path
import numpy as np
import nibabel as nib
import vesseg
from vesseg import nifti
from vesseg.network.evaluation.metrics import (
get_confusion_matrix_elements_from_arrays,
dice,
precision,
recall,
)
tmp_dir = Path('/tmp/patches')
label_path = tmp_dir / 'bres_dsa_ica_right_boneless_vesselness_seg.nii.gz'
prediction_path = tmp_dir / 'bres_dsa_ica_right_inferred_bin.nii.gz'
label_nii = nifti.load(label_path)
label_array = label_nii.get_data()
prediction_array = nifti.load(prediction_path).get_data()
input_shape = np.array(label_array.shape)
input_origin = label_nii.affine[:3, 3]
patch_length = 11 # around 5 mm
patch_shape = np.array(3 * [patch_length]) # use cubic patch for now
patch_radius = patch_shape // 2
first_voxel = patch_radius
output_origin = nib.affines.apply_affine(label_nii.affine, first_voxel)
output_shape = input_shape - (patch_shape - 1)
output_shape = output_shape.astype(np.uint16)
last_voxel = first_voxel + output_shape # - 1 # not included so -1 not necessary
dice_path = tmp_dir / f'dice_{patch_radius}.nii.gz'
dice_array = np.empty(output_shape)
dice_affine = np.copy(label_nii.affine)
dice_affine[:3, 3] = output_origin
precision_path = tmp_dir / f'precision_{patch_radius}.nii.gz'
precision_array = np.empty(output_shape)
precision_affine = np.copy(label_nii.affine)
precision_affine[:3, 3] = output_origin
recall_path = tmp_dir / f'recall_{patch_radius}.nii.gz'
recall_array = np.empty(output_shape)
recall_affine = np.copy(label_nii.affine)
recall_affine[:3, 3] = output_origin
oi, oj, ok = output_shape
for i in range(oi):
print(f'{i}/{oi}')
for j in range(oj):
for k in range(ok):
output_voxel = np.array([i, j, k])
input_voxel = first_voxel + output_voxel
input_voxel_ini = input_voxel - patch_radius
input_voxel_fin = input_voxel + patch_radius + 1
i_ini, j_ini, k_ini = input_voxel_ini
i_fin, j_fin, k_fin = input_voxel_fin
label_patch = label_array[i_ini:i_fin, j_ini:j_fin, k_ini:k_fin]
pred_patch = prediction_array[i_ini:i_fin, j_ini:j_fin, k_ini:k_fin]
confusion = get_confusion_matrix_elements_from_arrays(
label_patch, pred_patch)
dice_value = dice(confusion)
dice_array[i, j, k] = dice_value
precision_value = precision(confusion)
precision_array[i, j, k] = precision_value
recall_value = recall(confusion)
recall_array[i, j, k] = recall_value
nifti.save(dice_array, dice_affine, dice_path)
nifti.save(precision_array, precision_affine, precision_path)
nifti.save(recall_array, recall_affine, recall_path)
from pathlib import Path
import numpy as np
import nibabel as nib
import vesseg
from vesseg import nifti
from vesseg.network.evaluation.metrics import (
get_confusion_matrix_elements_from_arrays,
dice,
precision,
recall,
)
tmp_dir = Path('/tmp/patches')
label_path = tmp_dir / 'bres_dsa_ica_right_boneless_vesselness_seg.nii.gz'
prediction_path = tmp_dir / 'bres_dsa_ica_right_inferred_bin.nii.gz'
label_nii = nifti.load(label_path)
affine = label_nii.affine
label_array = label_nii.get_data()
prediction_array = nifti.load(prediction_path).get_data()
# 2D version
label_array = label_array[134:135, ...]
prediction_array = prediction_array[134:135, ...]
input_shape = np.array(label_array.shape)
input_origin = nib.affines.apply_affine(affine, [134, 0, 0])
new_affine = np.copy(affine)
new_affine[:3, 3] = input_origin
print(input_shape)
nifti.save(label_array, new_affine, tmp_dir / 'ground_truth_2D.nii.gz')
nifti.save(prediction_array, new_affine, tmp_dir / 'prediction_2D.nii.gz')
# patch_length = 25
# patch_shape = np.array(3 * [patch_length]) # use cubic patch for now
# patch_radius = patch_shape // 2
# first_voxel = patch_radius
# output_origin = nib.affines.apply_affine(label_nii.affine, first_voxel)
# output_shape = input_shape - (patch_shape - 1)
# output_shape = output_shape.astype(np.uint16)
# last_voxel = first_voxel + output_shape # - 1 # not included so -1 not necessary
# dice_path = tmp_dir / 'dice.nii.gz'
# dice_array = np.empty(output_shape)
# dice_affine = np.copy(label_nii.affine)
# dice_affine[:3, 3] = output_origin
# precision_path = tmp_dir / 'precision.nii.gz'
# precision_array = np.empty(output_shape)
# precision_affine = np.copy(label_nii.affine)
# precision_affine[:3, 3] = output_origin
# recall_path = tmp_dir / 'recall.nii.gz'
# recall_array = np.empty(output_shape)
# recall_affine = np.copy(label_nii.affine)
# recall_affine[:3, 3] = output_origin
# oi, oj, ok = output_shape
# for i in range(oi):
# print(f'{i}/{oi}')
# for j in range(oj):
# for k in range(ok):
# output_voxel = np.array([i, j, k])
# input_voxel = first_voxel + output_voxel
# input_voxel_ini = input_voxel - patch_radius
# input_voxel_fin = input_voxel + patch_radius + 1
# i_ini, i_fin, j_ini = input_voxel_ini
# j_fin, k_ini, k_fin = input_voxel_fin
# label_patch = label_array[i_ini:i_fin, j_ini:j_fin, k_ini:k_fin]
# pred_patch = prediction_array[i_ini:i_fin, j_ini:j_fin, k_ini:k_fin]
# confusion = get_confusion_matrix_elements_from_arrays(
# label_patch, pred_patch)
# dice_value = dice(confusion)
# dice_array[i, j, k] = dice_value
# precision_value = precision(confusion)
# precision_array[i, j, k] = precision_value
# recall_value = recall(confusion)
# recall_array[i, j, k] = recall_value
# print(recall_value)
# nifti.save(dice_array, dice_affine, dice_path)
# nifti.save(precision_array, precision_affine, precision_path)
# nifti.save(recall_array, recall_affine, recall_path)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment