Commit 3b1b237b authored by Andrei Roibu's avatar Andrei Roibu
Browse files

updated correlation evaluation

parent f7e0e7b2
......@@ -24,22 +24,26 @@ import nibabel as nib
import matplotlib.pyplot as plt
import pandas as pd
from fsl.data.image import Image
import itertools
log = logging.getLogger(__name__)
def evaluate_correlation(trained_model_path,
data_directory,
mapping_data_file,
target_data_file,
data_list,
prediction_output_path,
brain_mask_path,
mean_mask_path,
mean_reduction,
scaling_factors,
device=0,
mode='evaluate',
exit_on_error=False):
data_directory,
mapping_data_file,
target_data_file,
data_list,
prediction_output_path,
brain_mask_path,
rsfmri_mean_mask_path,
dmri_mean_mask_path,
mean_reduction,
scaling_factors,
regression_factors,
device=0,
mode='evaluate',
exit_on_error=False):
"""Model Evaluator
This function generates correlation map between the output and target rsfMRI maps
......@@ -52,9 +56,11 @@ def evaluate_correlation(trained_model_path,
data_list (str): Path to a .txt file containing the input files for consideration
prediction_output_path (str): Output prediction path
brain_mask_path (str): Path to the MNI brain mask file
mean_mask_path (str): Path to the dualreg subject mean mask
rsfmri_mean_mask_path (str): Path to the dualreg subject mean mask
dmri_mean_mask_path (str): Path to the dualreg subject mean mask
mean_reduction (bool): Flag indicating if the targets should be de-meaned using the mean_mask_path
scaling_factors (str): Path to the scaling factor file
regression_factors (str): Path to the linear regression weights file
device (str/int): Device type used for training (int - GPU id, str- CPU)
mode (str): Current run mode or phase
exit_on_error (bool): Flag that triggers the raising of an exception
......@@ -111,17 +117,27 @@ def evaluate_correlation(trained_model_path,
try:
print("Correlating Volume {}/{}".format(volume_index+1, len(file_paths)))
subject = volumes_to_be_used[volume_index]
predicted_complete_volume, predicted_volume, header, xform = _generate_volume_map(
file_path, model, device, cuda_available, brain_mask_path, mean_mask_path, scaling_factors, mean_reduction)
file_path, subject, model,
device, cuda_available, brain_mask_path,
dmri_mean_mask_path, rsfmri_mean_mask_path, scaling_factors,
regression_factors, mean_reduction)
for target_index, target_path in enumerate(target_paths):
target, target_demeaned = data_utils.load_and_preprocess_targets(target_path, mean_mask_path)
target, target_demeaned = data_utils.load_and_preprocess_targets(
target_path, rsfmri_mean_mask_path)
r_predicted_complete = _pearson_correlation(predicted_complete_volume, target)
r_predicted_demeaned = _pearson_correlation(predicted_volume, target_demeaned)
r_predicted_complete = _pearson_correlation(
predicted_complete_volume, target)
r_predicted_demeaned = _pearson_correlation(
predicted_volume, target_demeaned)
correlation_matrix_complete[volume_index, target_index] = r_predicted_complete
correlation_matrix_demeaned[volume_index, target_index] = r_predicted_demeaned
correlation_matrix_complete[volume_index,
target_index] = r_predicted_complete
correlation_matrix_demeaned[volume_index,
target_index] = r_predicted_demeaned
log.info("Correlated: " + volumes_to_be_used[volume_index] + " " + str(
volume_index + 1) + " out of " + str(len(volumes_to_be_used)))
......@@ -142,8 +158,10 @@ def evaluate_correlation(trained_model_path,
# HERE WE NEED TO CALL THE PRINT FUNCTION TWICE
_generate_correlation_matrix(correlation_matrix_complete, title='Complete Volume Correlation', prediction_output_path=prediction_output_path, normalize=False)
_generate_correlation_matrix(correlation_matrix_demeaned, title='Particularity Volume Correlation', prediction_output_path=prediction_output_path, normalize=False)
_generate_correlation_matrix(correlation_matrix_complete, title='Complete Volume Correlation',
prediction_output_path=prediction_output_path, normalize=False)
_generate_correlation_matrix(correlation_matrix_demeaned, title='Particularity Volume Correlation',
prediction_output_path=prediction_output_path, normalize=False)
def evaluate_mapping(trained_model_path,
......@@ -230,9 +248,9 @@ def evaluate_mapping(trained_model_path,
subject = volumes_to_be_used[volume_index]
predicted_complete_volume, predicted_volume, header, xform = _generate_volume_map(
file_path, subject, model,
device, cuda_available, brain_mask_path,
dmri_mean_mask_path, rsfmri_mean_mask_path, scaling_factors,
file_path, subject, model,
device, cuda_available, brain_mask_path,
dmri_mean_mask_path, rsfmri_mean_mask_path, scaling_factors,
regression_factors, mean_reduction)
# Generate New Header Affine
......@@ -284,17 +302,17 @@ def evaluate_mapping(trained_model_path,
log.info("rsfMRI Generation Complete")
def _generate_volume_map(file_path,
subject,
model,
device,
cuda_available,
brain_mask_path,
dmri_mean_mask_path,
rsfmri_mean_mask_path,
scaling_factors,
regression_factors,
mean_reduction=False):
def _generate_volume_map(file_path,
subject,
model,
device,
cuda_available,
brain_mask_path,
dmri_mean_mask_path,
rsfmri_mean_mask_path,
scaling_factors,
regression_factors,
mean_reduction=False):
"""rsfMRI Volume Generator
This function uses the trained model to generate a new volume
......@@ -320,12 +338,13 @@ def _generate_volume_map(file_path,
volume, header, xform = data_utils.load_and_preprocess_evaluation(
file_path)
volume = _regress_input(volume, subject, dmri_mean_mask_path, regression_factors)
volume = _regress_input(
volume, subject, dmri_mean_mask_path, regression_factors)
print('volume range:', np.min(volume), np.max(volume))
volume = _scale_input(volume, scaling_factors)
if len(volume.shape) == 5:
volume = volume
else:
......@@ -339,7 +358,7 @@ def _generate_volume_map(file_path,
output = model(volume)
output = (output.cpu().numpy()).astype('float32')
output = np.squeeze(output)
print('output range:', np.min(output), np.max(output))
output = _rescale_output(output, scaling_factors)
......@@ -351,20 +370,25 @@ def _generate_volume_map(file_path,
if mean_reduction == True:
mean_mask = Image(rsfmri_mean_mask_path).data[:, :, :, 0]
weight = pd.read_pickle(regression_factors).loc[subject]['w_rsfMRI']
predicted_complete_volume = np.add(output, np.multiply(weight, mean_mask))
predicted_complete_volume = np.add(
output, np.multiply(weight, mean_mask))
print('predicted_complete_volume', np.min(predicted_complete_volume), np.max(predicted_complete_volume))
print('predicted_complete_volume', np.min(
predicted_complete_volume), np.max(predicted_complete_volume))
predicted_complete_volume = np.multiply(predicted_complete_volume, MNI152_T1_2mm_brain_mask)
predicted_complete_volume = np.multiply(
predicted_complete_volume, MNI152_T1_2mm_brain_mask)
print('predicted_complete_volume masked:', np.min(predicted_complete_volume), np.max(predicted_complete_volume))
print('predicted_complete_volume masked:', np.min(
predicted_complete_volume), np.max(predicted_complete_volume))
else:
predicted_complete_volume = None
predicted_volume = np.multiply(output, MNI152_T1_2mm_brain_mask)
print('predicted_volume masked:', np.min(predicted_volume), np.max(predicted_volume))
print('predicted_volume masked:', np.min(
predicted_volume), np.max(predicted_volume))
return predicted_complete_volume, predicted_volume, header, xform
......@@ -395,7 +419,8 @@ def _scale_input(volume, scaling_factors):
# Normalization to [0, 1]
# scaled_volume = np.divide(np.subtract(volume, min_value), np.subtract(max_value, min_value))
# Scaling between [-1, 1]
scaled_volume = np.add(-1.0, np.multiply(2.0, np.divide(np.subtract(volume, min_value), np.subtract(max_value, min_value))))
scaled_volume = np.add(-1.0, np.multiply(2.0, np.divide(
np.subtract(volume, min_value), np.subtract(max_value, min_value))))
return scaled_volume
......@@ -424,6 +449,7 @@ def _regress_input(volume, subject, dmri_mean_mask_path, regression_factors):
return regressed_volume
def _rescale_output(volume, scaling_factors):
"""Output Rescaling
......@@ -446,7 +472,8 @@ def _rescale_output(volume, scaling_factors):
# Normalization to [0, 1]
# rescaled_volume = np.add(np.multiply(volume, np.subtract(max_value, min_value)), min_value)
# Scaling between [-1, 1]
rescaled_volume = np.add(np.multiply(np.divide(np.add(volume, 1), 2), np.subtract(max_value, min_value)), min_value)
rescaled_volume = np.add(np.multiply(
np.divide(np.add(volume, 1), 2), np.subtract(max_value, min_value)), min_value)
return rescaled_volume
......@@ -464,8 +491,9 @@ def _pearson_correlation(volume, target):
r (np.float32): The Pearson Correlation Coefficient
"""
r = np.sum(np.multiply(np.subtract(volume, volume.mean()), np.subtract(target, target.mean()))) / np.sqrt(np.multiply(np.sum(np.power(np.subtract(volume, volume.mean()),2)), np.sum(np.power(np.subtract(target, target.mean()),2))))
r = np.sum(np.multiply(np.subtract(volume, volume.mean()), np.subtract(target, target.mean()))) / np.sqrt(np.multiply(
np.sum(np.power(np.subtract(volume, volume.mean()), 2)), np.sum(np.power(np.subtract(target, target.mean()), 2))))
return r
......@@ -484,7 +512,8 @@ def _generate_correlation_matrix(correlation_matrix, title, prediction_output_pa
# THIS FUNCTION NEEDS TO BE VERIFIED!
if normalize:
correlation_matrix = correlation_matrix.astype('float') / correlation_matrix.sum(axis=1)[:, np.newaxis]
correlation_matrix = correlation_matrix.astype(
'float') / correlation_matrix.sum(axis=1)[:, np.newaxis]
plt.imshow(correlation_matrix, interpolation='nearest', cmap=plt.cm.Blues)
plt.title(title)
......@@ -494,10 +523,11 @@ def _generate_correlation_matrix(correlation_matrix, title, prediction_output_pa
plt.yticks(tick_marks, correlation_matrix)
threshold = correlation_matrix.max() / 2.0
for i, j in itertools.product(range(correlation_matrix.shape[0]), range(correlation_matrix.shape[1])):
plt.text(j, i, format(correlation_matrix[i,j], '.4f'), horizontalalignment='center', color="white" if correlation_matrix[i,j] > threshold else "black")
plt.text(j, i, format(correlation_matrix[i, j], '.4f'), horizontalalignment='center',
color="white" if correlation_matrix[i, j] > threshold else "black")
plt.tight_layout()
plt.ylabel('Predicted')
plt.xlabel('Targets')
plt.savefig(prediction_output_path + '/' + title+ '.png')
plt.savefig(prediction_output_path + '/' + title + '.png')
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