Skip to content

Testing #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/age_hist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np
import matplotlib.pyplot as plt

from .load_data_3d import load_targets
from .settings import DATA_DIRECTORY

def age_hist():
data = load_targets()
data = data['Y'].tolist()
plt.figure()
plt.hist(data, bins=100)
plt.savefig("plots/age_hist.pdf")
plt.close()
10 changes: 5 additions & 5 deletions app/cut_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ def cut_brain(sample, pos):
data = []
for d in sample:
if pos == "lt":
z_s,z_e = 60,len(d[0,0,:])
z_s,z_e = 60,d.shape[2]
y_s,y_e = 0,60
elif pos == "mt":
z_s,z_e = 60,len(d[0,0,:])
z_s,z_e = 60,d.shape[2]
y_s,y_e = 60,110
elif pos == "rt":
z_s,z_e = 50,len(d[0,0,:])
y_s,y_e = 110,len(d[0,:,0])
z_s,z_e = 50,d.shape[2]
y_s,y_e = 110,d.shape[1]
elif pos == "lb":
z_s,z_e = 0,60
y_s,y_e = 0,60
Expand All @@ -20,7 +20,7 @@ def cut_brain(sample, pos):
y_s,y_e = 60,110
elif pos == "rb":
z_s,z_e = 0,50
y_s,y_e = 110,len(d[0,:,0])
y_s,y_e = 110,d.shape[1]

area = np.array([[z_s,z_e],[y_s,y_e]])

Expand Down
40 changes: 40 additions & 0 deletions app/cut_brain_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np

def cut_brain(d, pos):
if pos == "lt":
z_s,z_e = 60,d.shape[2]
y_s,y_e = 0,60
elif pos == "mt":
z_s,z_e = 60,d.shape[2]
y_s,y_e = 60,110
elif pos == "rt":
z_s,z_e = 50,d.shape[2]
y_s,y_e = 110,d.shape[1]
elif pos == "lb":
z_s,z_e = 0,60
y_s,y_e = 0,60
elif pos == "mb":
z_s,z_e = 0,60
y_s,y_e = 60,110
elif pos == "rb":
z_s,z_e = 0,50
y_s,y_e = 110,d.shape[1]

area = np.array([[z_s,z_e],[y_s,y_e]])

z = range(area[0][0],area[0][1])
y = range(area[1][0],area[1][1])
x_len = d.shape[0]
y_len = len(y)
z_len = len(z)

d_new = np.zeros((x_len,y_len,z_len))
for k in range(0,x_len):
z_index = 0
for i in z:
y_index = 0
for j in y:
d_new[k,y_index,z_index] = d[k,j,i]
y_index += 1
z_index += 1
return d_new
49 changes: 21 additions & 28 deletions app/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .normalize import normalize

def feature_ratio_mean(inputs):
def feature_ratio_mean(inputs, norm=None):
inputs = get_flat_values(inputs)

fs = []
Expand All @@ -17,38 +17,28 @@ def feature_ratio_mean(inputs):

fs.append(ratio)

fs = normalize(fs)
return fs
if norm == None:
fs, minmax = normalize(fs)
return fs, minmax
else:
fs = normalize(fs, norm)
return fs

def feature_mean(inputs):
def feature_mean(inputs, norm=None):
inputs = get_flat_values(inputs)

fs = []
for i in inputs:
fs.append(np.mean(i))

fs = normalize(fs)
return fs
if norm == None:
fs, minmax = normalize(fs)
return fs, minmax
else:
fs = normalize(fs, norm)
return fs


def feature_ratio(inputs):
inputs = get_flat_values(inputs)

fs = []
for i in inputs:

low = i[i < 1100]
high = i[i >= 1100]

ratio = np.sum(high)/np.sum(low)

fs.append(ratio)

fs = normalize(fs)
return fs


def feature_max(inputs):
def feature_max(inputs, norm=None):
inputs = get_flat_values(inputs)

fs = []
Expand All @@ -58,9 +48,12 @@ def feature_max(inputs):
x_max = bin_edges[idx_max]

fs.append(x_max)

fs = normalize(fs)
return fs
if norm == None:
fs, minmax = normalize(fs)
return fs, minmax
else:
fs = normalize(fs, norm)
return fs


def get_flat_values(inputs):
Expand Down
75 changes: 75 additions & 0 deletions app/feature_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Description of this file."""

import numpy as np

from .normalize import normalize
from .get_number_of_files import get_number_of_files
from .load_deviations import load_deviation
from .cut_brain_test import cut_brain
from .load_data_3d import load_sample_input

def feature_ratio_mean(area="whole", training=True, norm=None):
fs = []
for i in range(1,get_number_of_files(training)+1):
data = load_data(i,training)
if not area == "whole":
data = cut_brain(data,area)
data = get_flat_values(data)
mean = np.mean(data)
low = data[data < mean]
high = data[data >= mean]

ratio = np.sum(high)/np.sum(low)

fs.append(ratio)

if norm == None:
fs, minmax = normalize(fs)
return fs, minmax
else:
fs = normalize(fs, norm)
return fs

def feature_mean(area, training=True, norm=None):
fs = []
for i in range(1,get_number_of_files(training)+1):
data = load_data(i,training)
if not area == "whole":
data = cut_brain(data,area)
data = get_flat_values(data)
fs.append(np.mean(data))

if norm == None:
fs, minmax = normalize(fs)
return fs, minmax
else:
fs = normalize(fs, norm)
return fs

def feature_max(area, training=True, norm=None):
fs = []
for i in range(1,get_number_of_files(training)+1):
data = load_data(i,training)
if not area == "whole":
data = cut_brain(data,area)
data = get_flat_values(data)
hist, bin_edges = np.histogram(data, bins='auto')
idx_max = hist.argmax()
x_max = bin_edges[idx_max]
fs.append(x_max)

if norm == None:
fs, minmax = normalize(fs)
return fs, minmax
else:
fs = normalize(fs, norm)
return fs

def get_flat_values(inputs):
vs = inputs.flatten()
return vs #[vs > 100]

def load_data(ID, training=True):
#return load_deviation(ID, training)
return load_sample_input(ID, training)

14 changes: 14 additions & 0 deletions app/get_number_of_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
from .settings import DATA_DIRECTORY

def get_number_of_files(training=True):
if training == True:
folder = "set_train"
else:
folder = "set_test"
DIR_IN = os.path.join(
DATA_DIRECTORY,
folder
)
nof = len([name for name in os.listdir(DIR_IN) if os.path.isfile(os.path.join(DIR_IN, name))])
return nof
9 changes: 6 additions & 3 deletions app/heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np

from .load_data import load_sample_input
from .load_deviations import load_deviation


__author__ = "lfievet"
Expand All @@ -20,8 +21,10 @@


def heatmap():
sample = 10
data = load_sample_input(sample)
sample = 20
#data = load_sample_input(sample)

data = load_deviation(sample)

# flat_data = data.get_data().flatten()
# flat_data = flat_data[flat_data > 100]
Expand All @@ -38,7 +41,7 @@ def heatmap():
# return

for i in range(60, 150):
heat_map = data.get_data()[:, :, i, 0]
heat_map = data[:][:][i] #.get_data()[:, :, i, 0]
# print(heat_map)
# print(heat_map.shape)
# heatmap.reshape((176, 208))
Expand Down
27 changes: 14 additions & 13 deletions app/load_data_3d.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
"""Loading training and testing data."""

import os
import scipy.io

import nibabel as ni
import pandas as pd

from .settings import DATA_DIRECTORY
from .get_number_of_files import get_number_of_files

def load_samples_inputs(training=True):
DIR = os.path.join(
DATA_DIRECTORY,
"set_train"
)
files = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])

nof = get_number_of_files(training)
inputs = []
for i in range(1, files+1):
inputs.append(load_sample_input(i))

for i in range(1, nof+1):
inputs.append(load_sample_input(i, training))
return inputs


def load_sample_input(id=1, training=True):
if training == True:
folder = "set_train"
files = "train"
else:
folder = "set_test"
files = "test"

file_path = os.path.join(
DATA_DIRECTORY,
"set_train",
"train_{}.nii".format(id)
folder,
"{}_{}.nii".format(files,id)
)
return ni.load(file_path).get_data()[:,:,:,0]

Expand Down
42 changes: 42 additions & 0 deletions app/load_deviations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import scipy.io
import numpy as np

from .settings import DATA_DIRECTORY
from .mean_brain import create_deviation_set
from .get_number_of_files import get_number_of_files

def load_deviations(training=True):
if training == True:
files = "train"
else:
files = "test"
DIR = os.path.join(
DATA_DIRECTORY,
"set_{}_deviation".format(files),
)
if not os.path.exists(DIR):
create_deviation_set()
data = []
for i in range(1,get_number_of_files(training)+1):
data.append(load_deviation(i,training))
return data

def load_deviation(ID,training=True):
if training == True:
files = "train"
else:
files = "test"
DIR = os.path.join(
DATA_DIRECTORY,
"set_{}_deviation".format(files),
)
if not os.path.exists(DIR):
create_deviation_set()
FILE = os.path.join(
DATA_DIRECTORY,
"set_{}_deviation".format(files),
"{}_{}.mat".format(files,ID)
)
return scipy.io.loadmat(FILE)['out']

Loading