<s> from __main__ import vtk , qt , ctk , slicer <EOL> import string <EOL> import numpy <EOL> import math <EOL> import operator <EOL> import collections <EOL> import FeatureExtractionLib <EOL> class TextureGLRL : <EOL> def __init__ ( self , grayLevels , numGrayLevels , parameterMatrix , parameterMatrixCoordinates , parameterValues , <EOL> allKeys ) : <EOL> self . textureFeaturesGLRL = collections . OrderedDict ( ) <EOL> self . textureFeaturesGLRLTiming = collections . OrderedDict ( ) <EOL> self . textureFeaturesGLRL [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ <EOL> "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ <EOL> "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ <EOL> "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . textureFeaturesGLRL [ <EOL> "<STR_LIT>" ] = "<STR_LIT>" <EOL> self . grayLevels = grayLevels <EOL> self . parameterMatrix = parameterMatrix <EOL> self . parameterMatrixCoordinates = parameterMatrixCoordinates <EOL> self . parameterValues = parameterValues <EOL> self . numGrayLevels = numGrayLevels <EOL> self . keys = set ( allKeys ) . intersection ( self . textureFeaturesGLRL . keys ( ) ) <EOL> def CalculateCoefficients ( self ) : <EOL> self . angles = <NUM_LIT> <EOL> self . Ng = self . numGrayLevels <EOL> self . Nr = numpy . max ( self . parameterMatrix . shape ) <EOL> self . Np = self . parameterValues . size <EOL> self . eps = numpy . spacing ( <NUM_LIT:1> ) <EOL> self . P_glrl = numpy . zeros ( <EOL> ( self . Ng , self . Nr , self . angles ) ) <EOL> self . P_glrl = self . calculate_glrl ( self . grayLevels , self . Ng , self . parameterMatrix , <EOL> self . parameterMatrixCoordinates , self . angles , self . P_glrl ) <EOL> self . sumP_glrl = numpy . sum ( numpy . sum ( self . P_glrl , <NUM_LIT:0> ) , <NUM_LIT:0> ) + self . eps <EOL> self . ivector = numpy . arange ( self . Ng ) + <NUM_LIT:1> <EOL> self . jvector = numpy . arange ( self . Nr ) + <NUM_LIT:1> <EOL> def shortRunEmphasis ( self , P_glrl , jvector , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> sre = numpy . sum ( numpy . sum ( ( P_glrl / ( ( jvector ** <NUM_LIT:2> ) [ None , : , None ] ) ) , <NUM_LIT:0> ) , <NUM_LIT:0> ) / ( sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> sre = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( sre . mean ( ) ) <EOL> else : <EOL> return sre <EOL> def longRunEmphasis ( self , P_glrl , jvector , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> lre = numpy . sum ( numpy . sum ( ( P_glrl * ( ( jvector ** <NUM_LIT:2> ) [ None , : , None ] ) ) , <NUM_LIT:0> ) , <NUM_LIT:0> ) / ( sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> lre = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( lre . mean ( ) ) <EOL> else : <EOL> return lre <EOL> def grayLevelNonUniformity ( self , P_glrl , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> gln = numpy . sum ( ( numpy . sum ( P_glrl , <NUM_LIT:1> ) ** <NUM_LIT:2> ) , <NUM_LIT:0> ) / ( sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> gln = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( gln . mean ( ) ) <EOL> else : <EOL> return gln <EOL> def runLengthNonUniformity ( self , P_glrl , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> rln = numpy . sum ( ( numpy . sum ( P_glrl , <NUM_LIT:0> ) ** <NUM_LIT:2> ) , <NUM_LIT:0> ) / ( sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> rln = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( rln . mean ( ) ) <EOL> else : <EOL> return rln <EOL> def runPercentage ( self , P_glrl , Np , meanFlag = True ) : <EOL> try : <EOL> rp = numpy . sum ( numpy . sum ( ( P_glrl / ( Np ) ) , <NUM_LIT:0> ) , <NUM_LIT:0> ) <EOL> except ZeroDivisionError : <EOL> rp = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( rp . mean ( ) ) <EOL> else : <EOL> return rp <EOL> def lowGrayLevelRunEmphasis ( self , P_glrl , ivector , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> lglre = numpy . sum ( numpy . sum ( ( P_glrl / ( ( ivector ** <NUM_LIT:2> ) [ : , None , None ] ) ) , <NUM_LIT:0> ) , <NUM_LIT:0> ) / ( sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> lglre = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( lglre . mean ( ) ) <EOL> else : <EOL> return lglre <EOL> def highGrayLevelRunEmphasis ( self , P_glrl , ivector , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> hglre = numpy . sum ( numpy . sum ( ( P_glrl * ( ( ivector ** <NUM_LIT:2> ) [ : , None , None ] ) ) , <NUM_LIT:0> ) , <NUM_LIT:0> ) / ( sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> hglre = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( hglre . mean ( ) ) <EOL> else : <EOL> return hglre <EOL> def shortRunLowGrayLevelEmphasis ( self , P_glrl , ivector , jvector , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> srlgle = numpy . sum ( numpy . sum ( ( P_glrl / ( ( jvector ** <NUM_LIT:2> ) [ None , : , None ] * ( ivector ** <NUM_LIT:2> ) [ : , None , None ] ) ) , <NUM_LIT:0> ) , <EOL> <NUM_LIT:0> ) / ( sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> srlgle = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( srlgle . mean ( ) ) <EOL> else : <EOL> return srlgle <EOL> def shortRunHighGrayLevelEmphasis ( self , P_glrl , ivector , jvector , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> srhgle = numpy . sum ( <EOL> numpy . sum ( ( ( P_glrl * ( ivector ** <NUM_LIT:2> ) [ : , None , None ] ) / ( ( jvector ** <NUM_LIT:2> ) [ None , : , None ] ) ) , <NUM_LIT:0> ) , <NUM_LIT:0> ) / ( <EOL> sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> srhgle = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( srhgle . mean ( ) ) <EOL> else : <EOL> return srhgle <EOL> def longRunLowGrayLevelEmphasis ( self , P_glrl , ivector , jvector , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> lrlgle = numpy . sum ( <EOL> numpy . sum ( ( ( P_glrl * ( jvector ** <NUM_LIT:2> ) [ None , : , None ] ) / ( ( ivector ** <NUM_LIT:2> ) [ : , None , None ] ) ) , <NUM_LIT:0> ) , <NUM_LIT:0> ) / ( <EOL> sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> lrlgle = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( lrlgle . mean ( ) ) <EOL> else : <EOL> return lrlgle <EOL> def longRunHighGrayLevelEmphasis ( self , P_glrl , ivector , jvector , sumP_glrl , meanFlag = True ) : <EOL> try : <EOL> lrhgle = numpy . sum ( numpy . sum ( ( P_glrl * ( ivector ** <NUM_LIT:2> ) [ : , None , None ] * ( jvector ** <NUM_LIT:2> ) [ None , : , None ] ) , <NUM_LIT:0> ) , <EOL> <NUM_LIT:0> ) / ( sumP_glrl [ None , None , : ] ) <EOL> except ZeroDivisionError : <EOL> lrhgle = <NUM_LIT:0> <EOL> if meanFlag : <EOL> return ( lrhgle . mean ( ) ) <EOL> else : <EOL> return lrhgle <EOL> def calculate_glrl ( self , grayLevels , numGrayLevels , matrix , matrixCoordinates , angles , P_out ) : <EOL> padVal = <NUM_LIT:0> <EOL> matrixDiagonals = list ( ) <EOL> aDiags = reduce ( lambda x , y : x + y , [ a . tolist ( ) for a in numpy . transpose ( matrix , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , aDiags ) ) <EOL> bDiags = reduce ( lambda x , y : x + y , [ a . tolist ( ) for a in numpy . transpose ( matrix , ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ) ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , bDiags ) ) <EOL> cDiags = reduce ( lambda x , y : x + y , [ a . tolist ( ) for a in numpy . transpose ( matrix , ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , cDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:1> ] <EOL> dDiags = reduce ( lambda x , y : x + y , [ matrix . diagonal ( a , <NUM_LIT:0> , <NUM_LIT:1> ) . tolist ( ) for a in xrange ( lowBound , highBound ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , dDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:2> ] <EOL> eDiags = reduce ( lambda x , y : x + y , [ matrix . diagonal ( a , <NUM_LIT:0> , <NUM_LIT:2> ) . tolist ( ) for a in xrange ( lowBound , highBound ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , eDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:2> ] <EOL> fDiags = reduce ( lambda x , y : x + y , [ matrix . diagonal ( a , <NUM_LIT:1> , <NUM_LIT:2> ) . tolist ( ) for a in xrange ( lowBound , highBound ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , fDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:1> ] <EOL> gDiags = reduce ( lambda x , y : x + y , <EOL> [ matrix [ : , : : - <NUM_LIT:1> , : ] . diagonal ( a , <NUM_LIT:0> , <NUM_LIT:1> ) . tolist ( ) for a in xrange ( lowBound , highBound ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , gDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:2> ] <EOL> hDiags = reduce ( lambda x , y : x + y , <EOL> [ matrix [ : , : , : : - <NUM_LIT:1> ] . diagonal ( a , <NUM_LIT:0> , <NUM_LIT:2> ) . tolist ( ) for a in xrange ( lowBound , highBound ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , hDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:2> ] <EOL> iDiags = reduce ( lambda x , y : x + y , <EOL> [ matrix [ : , : , : : - <NUM_LIT:1> ] . diagonal ( a , <NUM_LIT:1> , <NUM_LIT:2> ) . tolist ( ) for a in xrange ( lowBound , highBound ) ] ) <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , iDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:1> ] <EOL> jDiags = [ numpy . diagonal ( h , x , <NUM_LIT:0> , <NUM_LIT:1> ) . tolist ( ) for h in <EOL> [ matrix . diagonal ( a , <NUM_LIT:0> , <NUM_LIT:1> ) for a in xrange ( lowBound , highBound ) ] for x in <EOL> xrange ( - h . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> , h . shape [ <NUM_LIT:1> ] ) ] <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , jDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:1> ] <EOL> kDiags = [ numpy . diagonal ( h , x , <NUM_LIT:0> , <NUM_LIT:1> ) . tolist ( ) for h in <EOL> [ matrix [ : , : : - <NUM_LIT:1> , : ] . diagonal ( a , <NUM_LIT:0> , <NUM_LIT:1> ) for a in xrange ( lowBound , highBound ) ] for x in <EOL> xrange ( - h . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> , h . shape [ <NUM_LIT:1> ] ) ] <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , kDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:1> ] <EOL> lDiags = [ numpy . diagonal ( h , x , <NUM_LIT:0> , <NUM_LIT:1> ) . tolist ( ) for h in <EOL> [ matrix [ : , : , : : - <NUM_LIT:1> ] . diagonal ( a , <NUM_LIT:0> , <NUM_LIT:1> ) for a in xrange ( lowBound , highBound ) ] for x in <EOL> xrange ( - h . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> , h . shape [ <NUM_LIT:1> ] ) ] <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , lDiags ) ) <EOL> lowBound = - matrix . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> highBound = matrix . shape [ <NUM_LIT:1> ] <EOL> mDiags = [ numpy . diagonal ( h , x , <NUM_LIT:0> , <NUM_LIT:1> ) . tolist ( ) for h in <EOL> [ matrix [ : , : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] . diagonal ( a , <NUM_LIT:0> , <NUM_LIT:1> ) for a in xrange ( lowBound , highBound ) ] for x in <EOL> xrange ( - h . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> , h . shape [ <NUM_LIT:1> ] ) ] <EOL> matrixDiagonals . append ( filter ( lambda x : numpy . nonzero ( x ) [ <NUM_LIT:0> ] . size > <NUM_LIT:1> , mDiags ) ) <EOL> for angle in xrange ( <NUM_LIT:0> , len ( matrixDiagonals ) ) : <EOL> P = P_out [ : , : , angle ] <EOL> for diagonal in matrixDiagonals [ angle ] : <EOL> diagonal = numpy . array ( diagonal , dtype = '<STR_LIT:int>' ) <EOL> pos , = numpy . where ( <EOL> numpy . diff ( diagonal ) != <NUM_LIT:0> ) <EOL> pos = numpy . concatenate ( ( [ <NUM_LIT:0> ] , pos + <NUM_LIT:1> , [ len ( diagonal ) ] ) ) <EOL> rle = zip ( [ n for n in diagonal [ pos [ : - <NUM_LIT:1> ] ] if n != padVal ] , pos [ <NUM_LIT:1> : ] - pos [ : - <NUM_LIT:1> ] ) <EOL> rle = [ [ numpy . where ( grayLevels == x ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , y - <NUM_LIT:1> ] for x , y in <EOL> rle ] <EOL> P [ zip ( * rle ) ] += <NUM_LIT:1> <EOL> return ( P_out ) <EOL> def EvaluateFeatures ( self , printTiming = False , checkStopProcessFunction = None ) : <EOL> if not self . keys : <EOL> if not printTiming : <EOL> return self . textureFeaturesGLRL <EOL> else : <EOL> return self . textureFeaturesGLRL , self . textureFeaturesGLRLTiming <EOL> if not printTiming : <EOL> self . CalculateCoefficients ( ) <EOL> else : <EOL> import time <EOL> t1 = time . time ( ) <EOL> self . CalculateCoefficients ( ) <EOL> print ( "<STR_LIT>" . format ( time . time ( ) - t1 ) ) <EOL> if not printTiming : <EOL> for key in self . keys : <EOL> self . textureFeaturesGLRL [ key ] = eval ( self . textureFeaturesGLRL [ key ] ) <EOL> if checkStopProcessFunction is not None : <EOL> checkStopProcessFunction ( ) <EOL> return self . textureFeaturesGLRL <EOL> else : <EOL> for key in self . keys : <EOL> t1 = time . time ( ) <EOL> self . textureFeaturesGLRL [ key ] = eval ( self . textureFeaturesGLRL [ key ] ) <EOL> self . textureFeaturesGLRLTiming [ key ] = time . time ( ) - t1 <EOL> if checkStopProcessFunction is not None : <EOL> checkStopProcessFunction ( ) <EOL> return self . textureFeaturesGLRL , self . textureFeaturesGLRLTiming </s>
