eng
competition

Text Practice Mode

Python Practice (Code)

created Friday December 20, 09:47 by ArqamKhan


0


Rating

359 words
3 completed
00:00
def transIndex(S,SlicedData,modelPath,rootdir,outdir,SliceSize=64,SlidingWindow=64,frameNo=10,depthDirFromRoot=0):
     
    [model, _, _] = LOADModel(modelPath)
    mergedirs=[dirs for dirs,_,_ in os.walk(rootdir) if len(dirs.split("\\"))==(len(rootdir.split("\\"))+depthDirFromRoot)]
     
    for dir in mergedirs:
        print(f"\n--------------Inside {dir}-----------------\n")
        savedir=outdir+'\\'+os.path.basename(Path(dir))
        os.makedirs(savedir,exist_ok=True)
        temp=np.array(list(map(lambda x: np.array(x[1:],dtype="int") if dir in x[0].decode()[4:] else None,SlicedData.F)))
        temp=np.array(list(filter(lambda x: x is not None, temp)))
        Bothslices=np.array([],dtype="int64")
        Startslices=np.array([],dtype="int64")
        Endslices=np.array([],dtype="int64")
        Allslices=np.array([],dtype="int64")
        i=1
         
        for file in temp:
            start=int((file[0]-SliceSize)/SlidingWindow)+1
            end=int((file[1]-SliceSize)/SlidingWindow)+1
            Y=np.array(SlicedData.Y[file[0]:file[1]]).reshape(-1)
            n=int((Y.shape[0]-SliceSize)/SlidingWindow)+1
            Y=np.reshape(Y, (n, SliceSize))
            Y = np.array([[np.zeros((1),dtype="int16"), np.where(np.diff(i) != 0)[0] + 1,start+ind] if len(np.unique(i, return_counts=True)[1]) == 1 else [np.ones((1),dtype="int16"), np.where(np.diff(i) != 0)[0] + 1,start+ind] for ind,i in enumerate(Y)])
            bothslices = np.array([ i[2] for i in Y if (i[1] == frameNo).any() and (SliceSize - i[1] == frameNo).any()],dtype="int64")
            endslices= np.array([ i[2] for i in Y if (SliceSize - i[1] == frameNo).any()],dtype="int64")
            startslices= np.array([ i[2] for i in Y if (i[1] == frameNo).any()],dtype="int64")
            Bothslices=np.append(Bothslices,bothslices)
            Startslices=np.append(Startslices,startslices)
            Endslices=np.append(Endslices,endslices)
            Allslices=np.append(Allslices,Y[:,2].astype("int64"))
            if i%150==0:
                print(f"----------------{i} of {temp.shape[0]} files processed-------------")
            i+=1
         
        print(f"\n------------Started Prediction for all transition slices {len(Allslices)}-------------\n")
        X=SlicedData.Xs[Allslices]
        Y_pred=model.predict(X[:,:,1:],batch_size=32,verbose=0)
        Y_pred=np.argmax(Y_pred,axis=1)
        Y=np.argmax(SlicedData.Ys[Allslices],axis=1)
        confusionAll=metrics.confusion_matrix(Y,Y_pred,labels=[0,1])
        _,recallAll,_,_=metrics.precision_recall_fscore_support(Y,Y_pred,labels=[0,1])
 
        if Startslices.size!=0:
            print(f"\n------------Started Prediction for beginning transition slices {len(Startslices)}-------------\n")
            X=SlicedData.Xs[Startslices]
            Y_pred=model.predict(X[:,:,1:],batch_size=32,verbose=0)
            Y_pred=np.argmax(Y_pred,axis=1)
            Y=np.argmax(SlicedData.Ys[Startslices],axis=1)
            confusionStart=metrics.confusion_matrix(Y,Y_pred,labels=[0,1])
            _,recallStart,_,_=metrics.precision_recall_fscore_support(Y,Y_pred,labels=[0,1])
            errorS10=(confusionStart[1,0]/confusionAll[1,0])*100
            errorS01=(confusionStart[0,1]/confusionAll[0,1])*100
         
        if Endslices.size!=0:
            print(f"\n------------Started Prediction for ending transition slices {len(Endslices)}-------------\n")
            X=SlicedData.Xs[Endslices]
            Y_pred=model.predict(X[:,:,1:],batch_size=32,verbose=0)
            Y_pred=np.argmax(Y_pred,axis=1)
            Y=np.argmax(SlicedData.Ys[Endslices],axis=1)
            confusionEnd=metrics.confusion_matrix(Y,Y_pred,labels=[0,1])
            _,recallEnd,_,_=metrics.precision_recall_fscore_support(Y,Y_pred,labels=[0,1])
            errorE10=(confusionEnd[1,0]/confusionAll[1,0]  )*100
            errorE01=(confusionEnd[0,1]/confusionAll[0,1]  )*100
         
        if Bothslices.size!=0:
            print(f"\n------------Started Prediction for transition slices at both start & end {len(Bothslices)}-------------\n")
            X=SlicedData.Xs[Bothslices]
            Y_pred=model.predict(X[:,:,1:],batch_size=32,verbose=0)
            Y_pred=np.argmax(Y_pred,axis=1)
            Y=np.argmax(SlicedData.Ys[Bothslices],axis=1)
            confusionBoth=metrics.confusion_matrix(Y,Y_pred,labels=[0,1])
            _,recallBoth,_,_=metrics.precision_recall_fscore_support(Y,Y_pred,labels=[0,1])         
            errorB10=(confusionBoth[1,0]/confusionAll[1,0] )*100
            errorB01=(confusionBoth[0,1]/confusionAll[0,1] )*100
         
        with open(f"{savedir}\\Confusion_Matrix_transitionAt_{frameNo}.csv", 'w') as f:
            np.savetxt(f, [f"{dir}"], fmt="%s", delimiter=",")
             
 
            if Startslices.size!=0:
                np.savetxt(f, [f"Error transit/no-transit pos={frameNo}th frame  "], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{confusionStart[1,0]}/{confusionAll[1,0]}"], fmt="%s", delimiter=",")
                np.savetxt(f, [errorS10], fmt="%s", delimiter=",")
                np.savetxt(f, [f"Error no-transit/transit pos={frameNo}th frame  "], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{confusionStart[0,1]}/{confusionAll[0,1]}"], fmt="%s", delimiter=",")
                np.savetxt(f, [errorS01], fmt="%s", delimiter=",")
                np.savetxt(f, [f"Total slices having transition at pos={frameNo}th frame"], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{Startslices.size}"], fmt="%s", delimiter=",")
             
            if Endslices.size!=0:
                np.savetxt(f, [f"Error transit/no-transit pos={SliceSize-frameNo}th frame "], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{confusionEnd[1,0]}/{confusionAll[1,0]}"], fmt="%s", delimiter=",")
                np.savetxt(f, [errorE10], fmt="%s", delimiter=",")
                np.savetxt(f, [f"Error no-transit/transit pos={SliceSize-frameNo}th frame  "], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{confusionEnd[0,1]}/{confusionAll[0,1]}"], fmt="%s", delimiter=",")
                np.savetxt(f, [errorE01], fmt="%s", delimiter=",")
                np.savetxt(f, [f"Total slices having transition at pos={SliceSize-frameNo}th frame"], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{Endslices.size}"], fmt="%s", delimiter=",")
 
            if Bothslices.size!=0:
                np.savetxt(f, ["Error transit/no-transit pos=both  "], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{confusionBoth[1,0]}/{confusionAll[1,0]}"], fmt="%s", delimiter=",")
                np.savetxt(f, [errorB10], fmt="%s", delimiter=",")
                np.savetxt(f, ["Error no-transit/transit pos=both  "], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{confusionBoth[0,1]}/{confusionAll[0,1]}"], fmt="%s", delimiter=",")
                np.savetxt(f, [errorB01], fmt="%s", delimiter=",")
                np.savetxt(f, [f"Total slices having transition at both start/end positions "], fmt="%s", delimiter=",")
                np.savetxt(f, [f"{Bothslices.size}"], fmt="%s", delimiter=",")
 
    print("\n------------Done--------------\n")
    return

saving score / loading statistics ...