Commit 26a7f66b authored by Andrei Roibu's avatar Andrei Roibu
Browse files

updated imports, added shrinkage

parent 10ce17b4
......@@ -20,6 +20,7 @@ import numpy as np
import torch
import logging
import utils.data_utils as data_utils
from utils.common_utils import create_folder
import matplotlib.pyplot as plt
import pandas as pd
from fsl.data.image import Image
......@@ -96,7 +97,7 @@ def evaluate_correlation(trained_model_path,
# Create the prediction path folder if this is not available
data_utils.create_folder(prediction_output_path)
create_folder(prediction_output_path)
# Initiate the evaluation
......@@ -231,7 +232,7 @@ def evaluate_mapping(trained_model_path,
# Create the prediction path folder if this is not available
data_utils.create_folder(prediction_output_path)
create_folder(prediction_output_path)
# Initiate the evaluation
......@@ -340,12 +341,14 @@ def _generate_volume_map(file_path,
volume, header, xform = data_utils.load_and_preprocess_evaluation(file_path)
shrinkage_flag = True
if mean_regression == True:
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)
volume = _scale_input(volume, scaling_factors, shrinkage_flag=shrinkage_flag)
if len(volume.shape) == 5:
volume = volume
......@@ -363,7 +366,7 @@ def _generate_volume_map(file_path,
print('output range:', np.min(output), np.max(output))
output = _rescale_output(output, scaling_factors)
output = _rescale_output(output, scaling_factors, shrinkage_flag=shrinkage_flag)
print('output rescaled:', np.min(output), np.max(output))
......@@ -397,7 +400,7 @@ def _generate_volume_map(file_path,
return predicted_complete_volume, predicted_volume, header, xform
def _scale_input(volume, scaling_factors):
def _scale_input(volume, scaling_factors, shrinkage_flag=False):
"""Input Scaling
This function reads the scaling factors from the saved file and then scales the data.
......@@ -405,6 +408,7 @@ def _scale_input(volume, scaling_factors):
Args:
volume (np.array): Unscalled volume
scaling_factors (str): Path to the scaling factor file
shrinkage_flag (bool): Flag signaling if the file is to be shrunk
Returns:
scaled_volume (np.array): Scaled volume
......@@ -418,6 +422,19 @@ def _scale_input(volume, scaling_factors):
# Andrei Scaling
# min_value, max_value, _, _ = [-0.0539, 0.0969, -12.094, 14.6319]
# min_value, max_value, _, _ = [0.0, 0.0969, 0.0, 14.6319]
hard_shrinkage_flag = True
if shrinkage_flag == True:
lambd = 0.003 # Hard coded, equivalent to tht 1p and 99p values across the whole population in UKBB
if hard_shrinkage_flag == True:
volume = _hard_shrinkage(volume, lambd)
elif hard_shrinkage_flag == False:
volume = _soft_shrinkage(volume, lambd)
min_value += lambd
max_value -= lambd
# Eliminating outliers
volume[volume > max_value] = max_value
......@@ -427,7 +444,6 @@ def _scale_input(volume, scaling_factors):
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))))
return scaled_volume
......@@ -456,7 +472,7 @@ def _regress_input(volume, subject, dmri_mean_mask_path, regression_factors):
return regressed_volume
def _rescale_output(volume, scaling_factors):
def _rescale_output(volume, scaling_factors, shrinkage_flag=False):
"""Output Rescaling
This function reads the scaling factors from the saved file and then scales the data.
......@@ -464,6 +480,7 @@ def _rescale_output(volume, scaling_factors):
Args:
volume (np.array): Unscalled volume
scaling_factors (str): Path to the scaling factor file
shrinkage_flag (bool): Flag signaling if the file is to be shrunk
Returns:
rescaled_volume (np.array): Rescaled volume
......@@ -473,10 +490,21 @@ def _rescale_output(volume, scaling_factors):
# _, _, min_value, max_value = pickle.load(input_file)
# Steve Scaling
_, _, min_value, max_value = [0.0, 0.2, 0.0, 10.0]
# _, _, min_value, max_value = [0.0, 0.2, 0.0, 10.0]
# Andrei Scaling
# _, _, min_value, max_value = [-0.0539, 0.0969, -12.094, 14.6319]
_, _, min_value, max_value = [0.0, 0.0969, 0.0, 14.6319]
hard_shrinkage_flag = True
if shrinkage_flag == True:
lambd = 3.0
if hard_shrinkage_flag == True:
pass
elif hard_shrinkage_flag == False:
min_value += lambd
max_value -= lambd
# Normalization to [0, 1]
rescaled_volume = np.add(np.multiply(volume, np.subtract(max_value, min_value)), min_value)
......@@ -486,6 +514,50 @@ def _rescale_output(volume, scaling_factors):
return rescaled_volume
def _hard_shrinkage(volume, lambd):
""" Hard Shrinkage
This function performs a hard shrinkage on the volumes.
volume = { x , x > lambd | x < -lambd
0 , x e [-lambd, lambd]
}
Args:
volume (np.array): Unshrunken volume
lambd (float): Threshold parameter
Returns:
volume (np.array) : Hard shrunk volume
"""
volume[np.where(np.logical_and(volume>-lambd, volume<lambd))] = 0
return volume
def _soft_shrinkage(volume, lambd):
""" Soft Shrinkage
This function performs a soft shrinkage on the volumes.
volume = { x + lambd , x < -lambd
0 , x e [-lambd, lambd]
x - lambd , x > lambd
}
Args:
volume (np.array): Unshrunken volume
lambd (float): Threshold parameter
Returns:
volume (np.array) : Soft shrunk volume
"""
volume[np.where(np.logical_and(volume>=-lambd, volume<=lambd))] = 0.0
volume[volume < -lambd] = volume[volume < -lambd] + lambd
volume[volume > lambd] = volume[volume > lambd] - lambd
return volume
def _pearson_correlation(volume, target):
"""Calculate Pearson Correlation Coefficient
......
Supports Markdown
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