In [ ]:
"""
Copyright (c) 2016 by SAS Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0
   
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

"""

In [ ]:
from swat import *
from pprint import pprint
from matplotlib import pyplot as plt
import pandas as pd
import os, sys
import numpy as np
from scipy import misc
from PIL import Image
from skimage import feature
import re
import csv

# CAS setup
cashost='rdcgrd001'
casport=xxxxx
casauth='C:/Users/xxxxx/_authinfo'

# Start CAS session
sess = CAS(cashost, casport, authinfo=casauth, caslib="casuser")

In [ ]:
path = 
#destPath = 
dirs = os.listdir( path )
nblocks   = 5
npixels   = 16
data      = []
numPoints = 9
radius    = 3

In [ ]:
for dir in dirs:
    print dir
    i = 1
    # read in .pgm images and save as .jpg
    dirname = path+"\\"+dir
    print dirname
    for file in os.listdir(dirname):
        imageFile = dirname + "\\" + file
        #destdirname = destPath+"\\"+dir
        try:
            os.stat(destdirname)
        except:
            os.mkdir(destdirname)
        print imageFile
        image = misc.imread(imageFile)
        #print image.shape
        # LBP transformation 
        lbp = feature.local_binary_pattern(image, numPoints,radius, method="uniform")
        #lbp8 = (((lbp - lbp.min()) / (lbp.max() - lbp.min())) * 250).astype(np.uint8)
        #result = Image.fromarray(lbp8)
        #outFile = destdirname + "\\" + str(i) + ".jpg"
        #result.save(outFile)

        #downsize images to 90x90, divide each image to 5x5 blocks
        slbp = misc.imresize(lbp, (90,90) )
        features  = []
        for j in range(nblocks):
            for k in range(nblocks):
                block = slbp[j*npixels:(j+1)*npixels,k*npixels:(k+1)*npixels]
                #outImgFile = destdirname + "\\" + "block" + str(i)+str(j) +str(k)+ ".jpg"
                #misc.imsave(outImgFile, block)
                
                block = block.reshape(1,block.shape[0]*block.shape[1])
                features.append(block)
                #print np.array(features).shape
        i +=1
        features = np.array(features).reshape(1,block.shape[0]*block.shape[1]*nblocks*nblocks)
        personID = int(re.search(r'\d+', dir).group())
        #print personID
        features = np.append(features,personID)
        #print features
        data.append(features)

In [ ]:
print np.array(data).shape
np.savetxt("LBP.csv", data, delimiter=",", fmt="%i")

In [ ]:
varList = []
for i in range(np.array(data).shape[1]-1):
    temp = "Feature" + str(i+1)
    varList = np.append(varList,temp)
varList = np.append(varList,'ID')
#print varList

In [ ]:
with open('LBP_full.csv', 'wb') as outcsv:
    writer = csv.writer(outcsv)    
    writer.writerow(varList)
    
    with open('LBP.csv', 'rb') as incsv:
        reader = csv.reader(incsv)
        writer.writerows(row  for row in reader)

In [ ]:
out = sess.upload("LBP_full.csv")

In [ ]:
#data=sess.CASTable('ORL_FULL1')
#pprint(data.fetch(to=20))

In [ ]:
allVars = []
for j in range(nblocks*nblocks):
    varList = []
    for i in range(j*npixels*npixels,(j+1)*npixels*npixels):
        temp = "Feature" + str(i+1)
        varList.append(temp)
    allVars.append(varList)
#print allVars

In [ ]:
sess.loadactionset(actionset="sampling")
sess.stratified(display={"names":"STRAFreq"},
                output={"casOut":{"name":"partLBP", "replace":True}, "copyVars":"ALL"},
                samppct=10,  partind=True, seed=654321,
                table={"name":"LBP_FULL", "groupBy":{"ID"}})

In [ ]:
#stra_out=sess.CASTable('partLBP')
#pprint(stra_out.fetch(to=20))

In [ ]:
sess.loadactionset(actionset="neuralNet")
    
def NNET(k,hiddenUnits,seed):
    NNETModel = "_NNET_MODEL_"+ str(k)
    NNETScore = "_NNET_SCORE_"+ str(k)

    sess.anntrain(acts={"TANH"},
              arch="MLP",randDist='UNIFORM', scaleInit=2, 
              casOut={"name":NNETModel, "replace":True}, 
              errorFunc="ENTROPY",
              hiddens={hiddenUnits},
              inputs = allVars[k],
              nloOpts={"algorithm":"lbfgs","optmlOpt":{"maxIters":250,"maxTime":510.0,"regL1":0.0001,"regL2":0.0001},
                       "printOpt":{"statusMsg":True}},
              seed=seed,
              std="MIDRANGE",
              target="ID",targetAct="SOFTMAX",nominals={'ID'},
              validtable={"name":"partLBP","where":"_partIND_ = 1" },
              table={"name":"partLBP","where":"_partIND_ = 0" })
    
    sess.annscore(#assess=True,
              assessOneRow=True,
              casOut={"name":NNETScore, "replace":True}, 
              model={"name":NNETModel},
              target="ID",
              encodeName = True,
              copyVars={"ID"},
              table={"name":"partLBP","where":"_partIND_ = 1" })
    

for i in range(25):
    print ("block: "+str(i))
    NNET(i,50,23451)

In [ ]:
Str="data _nn_scored_merged;merge "
for i in range(25):
    temp = " _NNET_SCORE_"+str(i) +"(rename=(I_ID=I_ID"+str(i)+"))"
    Str = Str+temp
print (Str)
Str = Str + "; by ID; keep ID " 
for i in range(25):
    temp = " I_ID"+str(i)
    Str = Str+temp
print (Str)
Str = Str+"; run;"

In [ ]:
sess.dataStep.runCode( 
    code=Str
)

In [ ]:
scores=sess.CASTable("_nn_scored_merged")
pred = scores.fetch(to=40)['Fetch']
Freqs = np.zeros((40,40))
ID = []
for i in range(40):
    index = int(pred.iloc[i].iloc[0])
    ID = np.append(ID,index)
    for j in range(1,26):
        index = int(pred.iloc[i].iloc[j].strip())-1
        Freqs[i][index] +=1
        
#print Freqs
pred_ID = []
for i in range(40):
    tmpID = np.where(Freqs[i,]==max(Freqs[i,]))
    tmpID = min(tmpID[0]+1)
    pred_ID = np.append(pred_ID,tmpID)

print ID,pred_ID

In [ ]:
def calMisclas(arr1,arr2):
    n1 = len(arr1)
    n2 = len(arr2)
    if (n1==n2):
        nMisclas = 0
        for i in range(n1):
            if (arr2[i]!=arr1[i]):  
                nMisclas +=1
        ratio = nMisclas/float(n1)
    return ratio

In [ ]:
misRate = calMisclas(ID,p_ID)*100
print misRate