Skip to content

Commit

Permalink
master
Browse files Browse the repository at this point in the history
  • Loading branch information
sagautam5 committed Aug 15, 2016
1 parent 108a4f8 commit de2e66e
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 79 deletions.
5 changes: 3 additions & 2 deletions Dementia.py
Expand Up @@ -5,7 +5,7 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk,GdkPixbuf
from DementiaDetector import on_fileOpen
from DementiaDetector import on_fileOpen,startjvm


#fileLoc = "/home/abinash/Project/_Project/Dementia/"
Expand Down Expand Up @@ -76,6 +76,7 @@ def on_save_clicked(self, widget):
class Main:
def __init__(self):
self.filepath = ''
startjvm()
builder1 = Gtk.Builder()
builder1.add_from_file(os.getcwd()+"/Resource/dementia.glade")
builder1.connect_signals(self)
Expand All @@ -89,7 +90,7 @@ def __init__(self):
def on_fileOpen_activate(self, widget):
print("open")
win = FileChooserWindow()

if (win.openFlag == True):
self.filePath = win.filepath
print ("calling functin on_fileOpen")
Expand Down
4 changes: 2 additions & 2 deletions DementiaDetector.py
Expand Up @@ -33,17 +33,17 @@ def getLabel(Labels):
else:
label = 0
return label
def startjvm():
startJVM(jpype.getDefaultJVMPath());

#Source = raw_input("Enter Image File Location:")
def on_fileOpen(Source):
#Source = '/home/abinash/Desktop/Untitled Folder/OAS1_0002_MR1_mpr_n4_anon_sbj_111.img'
startJVM(jpype.getDefaultJVMPath());
myPackage = JPackage('FeatureExtraction').org.classes
Dementia = myPackage.Dementia

String = Dementia.System("Test",Source)
Features = [float(x) for x in String.split()]

''' Best Approximated Value of Nearest Neighbours'''
K = 7

Expand Down
Binary file added DementiaDetector.pyc
Binary file not shown.
145 changes: 70 additions & 75 deletions DementiaDetector.py~
Expand Up @@ -34,78 +34,73 @@ def getLabel(Labels):
label = 0
return label

"""
Acessing java class Dementia from package Feature
Extraction to calculate Feature Vector
"""

startJVM(jpype.getDefaultJVMPath());
myPackage = JPackage('FeatureExtraction').org.classes #Location of Dementia.class
Dementia = myPackage.Dementia # Dementia.class file acess

Source = raw_input("Enter Test Image File Location:")
String = Dementia.System("Test",Source) #static function System(....,....) call
Features = [float(x) for x in String.split()] # we get feature vector as string so to get numeric
# spliting is required.

''' Best Approximated Value of Nearest Neighbours'''
K = 7

demented_file_no = 75
nondemented_file_no = 107

# List of Training Data of type [(_,_,_),_]
Train = []
# List of Test Data of type [(_,_,_),_]

predicted = []
actual_val = []
for i in range(0,demented_file_no):
filename = 'Resource/TrainData/Demented/Nearest/Image'+str(i)+'.txt'
Data = [float(x) for x in open(filename).read().split()]
data_sample = (tuple(Data),0)
Train.append(data_sample)

for j in range(0,nondemented_file_no):
filename = 'Resource/TrainData/NotDemented/Nearest/Image'+str(j)+'.txt'
Data = [float(x) for x in open(filename).read().split()]
data_sample = (tuple(Data),1)
Train.append(data_sample)

test_Feature = tuple(Features)
dist_vector = [1.0e+15]*K
labe_vector = [1]*K

'''KNN alorithm to test the new instances'''


for Feature,Label in Train:
dist = edistance(Feature,test_Feature)
for L in range(K):
if dist<dist_vector[L]:
dist_vector[L] = dist
labe_vector[L] = Label
break
#Assignment of majority label to label of test feature vector
newLabel = getLabel(labe_vector)
if newLabel==0:
print "KNN Model: Dementia Positive"
if newLabel==1:
print "KNN Model: Dementia Negative"

#Network Initialization...
nets = buildNetwork(3,2,2,2,bias = True,hiddenclass=TanhLayer)

#Assigning Weights to the Neural Network from xml file
nets = NetworkReader.readFrom('filename.xml')

#get output of the Neural Netwok
network_Result = nets.activate(Features)
#get final result from output
prediction = Normalize(network_Result)

if(prediction==[0,1]):
print "Neural Network: Dementia Positive"
if(prediction==[1,0]):
print "Neural Network: Dementia Negative"

#Source = raw_input("Enter Image File Location:")
def on_fileOpen(Source):
#Source = '/home/abinash/Desktop/Untitled Folder/OAS1_0002_MR1_mpr_n4_anon_sbj_111.img'
startJVM(jpype.getDefaultJVMPath());
myPackage = JPackage('FeatureExtraction').org.classes
Dementia = myPackage.Dementia

String = Dementia.System("Test",Source)
Features = [float(x) for x in String.split()]
#shutdownJVM();
''' Best Approximated Value of Nearest Neighbours'''
K = 7

demented_file_no = 75
nondemented_file_no = 107

# List of Training Data of type [(_,_,_),_]
Train = []
# List of Test Data of type [(_,_,_),_]

predicted = []
actual_val = []
for i in range(0,demented_file_no):
filename = 'Resource/TrainData/Demented/Nearest/Image'+str(i)+'.txt'
Data = [float(x) for x in open(filename).read().split()]
data_sample = (tuple(Data),0)
Train.append(data_sample)

for j in range(0,nondemented_file_no):
filename = 'Resource/TrainData/NotDemented/Nearest/Image'+str(j)+'.txt'
Data = [float(x) for x in open(filename).read().split()]
data_sample = (tuple(Data),1)
Train.append(data_sample)

test_Feature = tuple(Features)
dist_vector = [1.0e+15]*K
labe_vector = [1]*K

'''KNN alorithm to test the new instances'''


for Feature,Label in Train:
dist = edistance(Feature,test_Feature)
for L in range(K):
if dist<dist_vector[L]:
dist_vector[L] = dist
labe_vector[L] = Label
break
global newLabel
newLabel = getLabel(labe_vector)
if newLabel==0:
print ("KNN Model: Dementia Positive")
if newLabel==1:
print ("KNN Model: Dementia Negative")

nets = buildNetwork(3,2,2,2,bias = True,hiddenclass=TanhLayer)
nets = NetworkReader.readFrom('filename.xml')

network_Result = nets.activate(Features)
global predictionResult
prediction = Normalize(network_Result)

if(prediction==[0,1]):
predictionResult = 0
print ("Neural Network: Dementia Positive")
if(prediction==[1,0]):
predictionResult = 1
print ("Neural Network: Dementia Negative")

#on_fileOpen('/home/abinash/Desktop/Untitled Folder/OAS1_0002_MR1_mpr_n4_anon_sbj_111.img')
171 changes: 171 additions & 0 deletions Model.py~
@@ -0,0 +1,171 @@
__version__= '0.1'

__author__ = 'Sagar Gautam'
import math

import jpype

from jpype import *

from pybrain.tools.shortcuts import buildNetwork

from pybrain.tools.xml.networkreader import NetworkReader

from pybrain.structure import TanhLayer, SigmoidLayer

# Normalize result
def normalize(output):
if output[0] > output[1]:
return [1, 0]
else:
return [0, 1]

# Measure euclidean distance between feature vectors
def edit_distance(feature1, feature2):
total=0
for i in range(len(feature1)):
total = total+(feature1[i]-feature2[i])*(feature1[i]-feature2[i])
distance = math.sqrt(total)
return distance

# Assign majority class cabel to the label of new instance
def get_label(labels):
count1 = 0
for label in range(len(labels)):
if labels[label] == 1:
count1 = count1 + 1
count0 = len(labels)-count1

if count1 > count0:
new_label = 1
else:
new_label = 0
return new_label

class Model:
test_features = []
train = []
demented_file_no = 75
non_demented_file_no = 107

def __init__(self,Source):
self.Source = Source
if isJVMstarted():

startJVM(jpype.getDefaultJVMPath());
myPackage = JPackage('FeatureExtraction').org.classes
dementia = myPackage.Dementia
string = dementia.System("Test",Source)
test_features = [float(x) for x in string.split()]
shutdownJVM();

def KNN(self,K):
for i in range(0,demented_file_no):
filename = 'Resource/TrainData/Demented/Nearest/Image'+str(i)+'.txt'
data = [float(x) for x in open(filename).read().split()]
data_sample = (tuple(data), 0)
train.append(data_sample)

for j in range(0,non_demented_file_no):
filename = 'Resource/TrainData/NotDemented/Nearest/Image'+str(j)+'.txt'
data = [float(x) for x in open(filename).read().split()]
data_sample = (tuple(data), 1)
train.append(data_sample)

test_feature = tuple(test_features)
print test_feature
distance_vector = [1.0e+15]*K
label_vector = [1]*K

# KNN alorithm to test the new instances

for feature, label in train:
distance = edit_distance(feature, test_feature)
for value in range(K):
if distance < distance_vector[value]:
distance_vector[value] = distance
label_vector[value] = label
break

# Assign majority label to label of test feature vector
new_label = get_label(label_vector)
if new_label == 0:
print "KNN Model: Dementia Positive"
if new_label == 1:
print "KNN Model: Dementia Negative"

#def ANN(self):

# Acess java class Dementia from package featureextraction to calculate feature fector

startJVM(jpype.getDefaultJVMPath());
myPackage = JPackage('FeatureExtraction').org.classes # Location of Dementia.class
dementia = myPackage.Dementia # Dementia.class file acess
source = raw_input("Enter Test Image File Location:")
string = dementia.System("Test",source) # Static function System(....,....) call
features = [float(x) for x in string.split()] # We get feature vector as string so to get numeric
# Spliting is required

# Best Approximated Value of Nearest Neighbours
K = 7

demented_file_no = 75
non_demented_file_no = 107

# List of Training Data of type [(_,_,_),_]
train = []
# List of Test Data of type [(_,_,_),_]

for i in range(0,demented_file_no):
filename = 'Resource/TrainData/Demented/Nearest/Image'+str(i)+'.txt'
data = [float(x) for x in open(filename).read().split()]
data_sample = (tuple(data), 0)
train.append(data_sample)

for j in range(0,non_demented_file_no):
filename = 'Resource/TrainData/NotDemented/Nearest/Image'+str(j)+'.txt'
data = [float(x) for x in open(filename).read().split()]
data_sample = (tuple(data), 1)
train.append(data_sample)

test_feature = tuple(features)
distance_vector = [1.0e+15]*K
label_vector = [1]*K

# KNN alorithm to test the new instances

for feature, label in train:
distance = edit_distance(feature, test_feature)
for value in range(K):
if distance < distance_vector[value]:
distance_vector[value] = distance
label_vector[value] = label
break

# Assign majority label to label of test feature vector
new_label = get_label(label_vector)

if new_label == 0:
print "KNN Model: Dementia Positive"

if new_label == 1:
print "KNN Model: Dementia Negative"
shutdownJVM();
model = Model("Resource/Sample/Image5.img")
model.KNN()
# Network initialization
network = buildNetwork(3, 2, 2, 2, bias=True, hiddenclass=TanhLayer)

# Assign Weights to the neural network from xml file
network = NetworkReader.readFrom('filename.xml')

# Get output of the neural netwok
output = network.activate(features)
# Get final result from output
prediction = normalize(output)

if(prediction == [0, 1]):
print "Neural Network: Dementia Positive"
if(prediction == [1, 0]):
print "Neural Network: Dementia Negative"

0 comments on commit de2e66e

Please sign in to comment.