<s> """<STR_LIT>""" <EOL> import os . path <EOL> from types import StringTypes <EOL> from fnmatch import fnmatchcase <EOL> from copy import deepcopy <EOL> import subprocess <EOL> try : <EOL> import numpy <EOL> except ImportError : <EOL> pass <EOL> _LEAF = - <NUM_LIT:1> <EOL> class DCPTDict ( dict ) : <EOL> def flat ( self ) : <EOL> for key in sorted ( self . keys ( ) ) : <EOL> for i in self [ key ] : <EOL> yield i <EOL> class ProbTable ( object ) : <EOL> def __init__ ( self , scard , pcard ) : <EOL> super ( ProbTable , self ) . __init__ ( ) <EOL> if not issequence ( scard ) : <EOL> scard = [ scard ] <EOL> if not issequence ( pcard ) : <EOL> pcard = [ pcard ] <EOL> self . _nvars = len ( scard ) <EOL> self . _npars = len ( pcard ) <EOL> self . _cvars = tuple ( scard ) <EOL> self . _cpars = tuple ( pcard ) <EOL> self . _table = self . _initTable ( scard , pcard ) <EOL> def __eq__ ( self , other ) : <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if not hasattr ( other , attr ) : <EOL> return NotImplementedError ( ) <EOL> return ( self . _cvars == other . _cvars ) and ( self . _cpars == other . _cpars ) and self . _eqTables ( other ) <EOL> def _initTable ( self , scard , pcard ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def _eqTables ( self , other ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def getTable ( self ) : <EOL> return self . _table <EOL> table = property ( getTable ) <EOL> def _convertSlices ( self , item ) : <EOL> var = [ ] <EOL> cond = [ ] <EOL> cur = var <EOL> if not issequence ( item ) : <EOL> item = [ item ] <EOL> for i in item : <EOL> if isinstance ( i , slice ) : <EOL> if cur is cond : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if i . step is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if i . start is not None : <EOL> var . append ( i . start ) <EOL> if i . stop is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> cond . append ( i . stop ) <EOL> cur = cond <EOL> else : <EOL> cur . append ( i ) <EOL> return tuple ( var ) , tuple ( cond ) <EOL> def _getMassFunction ( self , parents ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def _createMassFunction ( self , parents ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def _delMassFunction ( self , parents ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __getitem__ ( self , item ) : <EOL> var , cond = self . _convertSlices ( item ) <EOL> mf = self . _getMassFunction ( cond ) <EOL> if len ( var ) > <NUM_LIT:1> : <EOL> return mf [ var ] <EOL> elif len ( var ) == <NUM_LIT:1> : <EOL> return mf [ var [ <NUM_LIT:0> ] ] <EOL> else : <EOL> return mf <EOL> def __setitem__ ( self , item , value ) : <EOL> var , cond = self . _convertSlices ( item ) <EOL> mf = self . _createMassFunction ( cond ) <EOL> if len ( var ) > <NUM_LIT:1> : <EOL> mf [ var ] = value <EOL> elif len ( var ) == <NUM_LIT:1> : <EOL> mf [ var [ <NUM_LIT:0> ] ] = value <EOL> elif len ( mf ) == len ( value ) : <EOL> mf [ : ] = value [ : ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( len ( mf ) , len ( value ) ) ) <EOL> def __delitem__ ( self , item ) : <EOL> var , cond = self . _convertSlices ( item ) <EOL> if len ( var ) > <NUM_LIT:0> : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> self . _delMassFunction ( cond ) <EOL> def getParentCards ( self ) : <EOL> return self . _cpars <EOL> parentCards = property ( getParentCards ) <EOL> def getSelfCards ( self ) : <EOL> return self . _cvars <EOL> selfCards = property ( getSelfCards ) <EOL> class DenseTable ( ProbTable ) : <EOL> def _initTable ( self , scard , pcard ) : <EOL> if len ( scard ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> ret = DCPTDict ( ) <EOL> for p in self . possibleParents : <EOL> ret [ p ] = [ <NUM_LIT:0.> ] * scard [ <NUM_LIT:0> ] <EOL> return ret <EOL> def _getMassFunction ( self , parents ) : <EOL> if len ( parents ) != self . _npars : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return self . _table [ parents ] <EOL> def _eqTables ( self , other ) : <EOL> return self . _table == other . _table <EOL> _createMassFunction = _getMassFunction <EOL> def changeParentCards ( self , newParentCards ) : <EOL> """<STR_LIT>""" <EOL> if not issequence ( newParentCards ) : <EOL> newParentCards = [ newParentCards ] <EOL> if <NUM_LIT:0> in newParentCards : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> toBeDeleted = [ range ( newParentCards [ p ] , self . parentCards [ p ] ) for p in range ( min ( len ( newParentCards ) , len ( self . parentCards ) ) ) ] <EOL> toBeDeleted += [ range ( self . parentCards [ p ] ) for p in range ( len ( newParentCards ) , len ( self . parentCards ) ) ] <EOL> if len ( newParentCards ) < len ( self . parentCards ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> parCards = list ( self . _cpars ) <EOL> for i , p in enumerate ( toBeDeleted ) : <EOL> parRanges = [ range ( j ) for j in parCards ] <EOL> parRanges [ i ] = p <EOL> for pmKey in cartezian ( * parRanges ) : <EOL> del self . table [ pmKey ] <EOL> if parCards [ i ] > newParentCards [ i ] : <EOL> parCards [ i ] = newParentCards [ i ] <EOL> toBeAdded = [ range ( self . parentCards [ p ] , newParentCards [ p ] ) for p in range ( min ( len ( newParentCards ) , len ( self . parentCards ) ) ) ] <EOL> toBeAdded += [ range ( newParentCards [ p ] ) for p in range ( len ( self . parentCards ) , len ( newParentCards ) ) ] <EOL> for i , p in enumerate ( toBeAdded ) : <EOL> parRanges = [ range ( j ) for j in parCards ] <EOL> parRanges [ i ] = p <EOL> for pmKey in cartezian ( * parRanges ) : <EOL> self . table [ pmKey ] = [ <NUM_LIT:0.> ] * self . selfCards [ <NUM_LIT:0> ] <EOL> if parCards [ i ] < newParentCards [ i ] : <EOL> parCards [ i ] = newParentCards [ i ] <EOL> self . _cpars = tuple ( parCards ) <EOL> def _delMassFunction ( self , parents ) : <EOL> if len ( parents ) != self . _npars : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> del self . _table [ parents ] <EOL> def getPossibleParents ( self ) : <EOL> keys = [ range ( i ) for i in self . parentCards ] <EOL> if keys : <EOL> return cartezian ( * keys ) <EOL> else : <EOL> return [ ( ) ] <EOL> possibleParents = property ( getPossibleParents ) <EOL> class _Object ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , name , * args ) : <EOL> super ( _Object , self ) . __init__ ( * args ) <EOL> self . name = name <EOL> self . parent = parent <EOL> self . parent [ self . __class__ ] = self <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . name ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def writeToFile ( self , stream ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def setName ( self , name ) : <EOL> self . _name = name <EOL> def getName ( self ) : <EOL> return self . _name <EOL> name = property ( getName , setName ) <EOL> """<STR_LIT>""" <EOL> def setParent ( self , parent ) : <EOL> self . _parent = parent <EOL> def getParent ( self ) : <EOL> return self . _parent <EOL> parent = property ( getParent , setParent ) <EOL> class NameCollection ( _Object , list ) : <EOL> def __init__ ( self , parent , name ) : <EOL> _Object . __init__ ( self , parent , name ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> name = stream . readWord ( ) <EOL> coll = cls ( parent , name ) <EOL> n_obj = stream . readInt ( ) <EOL> for i in range ( n_obj ) : <EOL> coll . append ( stream . readWord ( ) ) <EOL> return coll <EOL> def writeToFile ( self , stream ) : <EOL> stream . writelnWord ( self . name ) <EOL> stream . writelnInt ( len ( self ) ) <EOL> for i in self : <EOL> stream . writelnWord ( i ) <EOL> def __eq__ ( self , other ) : <EOL> return list ( self ) == list ( other ) <EOL> class TreeLeaf ( object ) : <EOL> def __init__ ( self , expression , ) : <EOL> if isinstance ( expression , int ) : <EOL> self . eval = False <EOL> self . expression = expression <EOL> self . sexpression = str ( expression ) <EOL> elif expression [ <NUM_LIT:0> ] == '<STR_LIT:{>' and expression [ - <NUM_LIT:1> ] == '<STR_LIT:}>' : <EOL> self . sexpression = expression <EOL> expression = expression [ <NUM_LIT:1> : - <NUM_LIT:1> ] . replace ( '<STR_LIT:!>' , '<STR_LIT>' ) <EOL> expression = expression . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> expression = '<STR_LIT>' % expression . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . eval = True <EOL> self . expression = compile ( expression , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> self . sexpression = expression <EOL> self . eval = False <EOL> self . expression = int ( expression ) <EOL> def __call__ ( self , parents ) : <EOL> if self . eval : <EOL> ns = { } <EOL> for i , value in enumerate ( parents ) : <EOL> ns [ '<STR_LIT>' % i ] = value <EOL> return eval ( self . expression , ns ) <EOL> else : <EOL> return self . expression <EOL> def __eq__ ( self , other ) : <EOL> if not hasattr ( other , '<STR_LIT>' ) : <EOL> return NotImplemented <EOL> return self . expression == other . expression <EOL> @ classmethod <EOL> def readFromFile ( cls , stream ) : <EOL> total = stream . readWord ( ) <EOL> if total [ <NUM_LIT:0> ] == '<STR_LIT:{>' : <EOL> while total [ - <NUM_LIT:1> ] != '<STR_LIT:}>' : <EOL> total += stream . readWord ( ) <EOL> leaf = cls ( total ) <EOL> return leaf <EOL> def writeToFile ( self , stream , indent = '<STR_LIT>' ) : <EOL> stream . writeWord ( self . sexpression ) <EOL> class TreeBranch ( object ) : <EOL> def __init__ ( self , parent_id , default = <NUM_LIT:0> , comment = None ) : <EOL> super ( TreeBranch , self ) . __init__ ( ) <EOL> self . parentId = parent_id <EOL> self . _questions = { } <EOL> self . default = default <EOL> self . comment = comment <EOL> def __eq__ ( self , other ) : <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:default>' ) : <EOL> if not hasattr ( other , attr ) : <EOL> return NotImplemented <EOL> return ( self . parentId == other . parentId ) and ( self . _questions == other . _questions ) and ( self . default == other . default ) <EOL> def isLeaf ( self ) : <EOL> return self . parentId == _LEAF <EOL> def vanish ( self ) : <EOL> while True : <EOL> if self . isLeaf ( ) : <EOL> return <EOL> if len ( self . _questions ) == <NUM_LIT:0> : <EOL> self . parentId = self . default . parentId <EOL> self . _questions = self . default . _questions <EOL> self . default = self . default . default <EOL> else : <EOL> break <EOL> for branch in self . _questions . values ( ) : <EOL> branch . vanish ( ) <EOL> self . default . vanish ( ) <EOL> def __contains__ ( self , item ) : <EOL> return item in self . _questions <EOL> def numQuestions ( self ) : <EOL> return len ( self . _questions ) <EOL> def __getitem__ ( self , item ) : <EOL> if isinstance ( item , ( long , int ) ) : <EOL> if item in self . _questions : <EOL> return self . _questions [ item ] <EOL> else : <EOL> return self . default <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __setitem__ ( self , item , value ) : <EOL> if isinstance ( item , ( long , int ) ) : <EOL> self . _questions [ item ] = value <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def append ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _questions [ self . numQuestions ( ) ] = value <EOL> def __delitem__ ( self , item ) : <EOL> if isinstance ( item , ( long , int ) ) : <EOL> try : <EOL> del self . _questions [ item ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" % item ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def readFromFile ( cls , stream ) : <EOL> parent_id = stream . readInt ( ) <EOL> branch = cls ( parent_id ) <EOL> if parent_id == _LEAF : <EOL> branch . default = TreeLeaf . readFromFile ( stream ) <EOL> branch . comment = stream . comment <EOL> return branch <EOL> else : <EOL> n_quest = stream . readInt ( ) <EOL> questions = [ ] <EOL> while len ( questions ) < n_quest - <NUM_LIT:1> : <EOL> w = stream . readWord ( ) <EOL> if w == '<STR_LIT>' : <EOL> questions . extend ( range ( questions [ - <NUM_LIT:1> ] + <NUM_LIT:1> , stream . readInt ( ) + <NUM_LIT:1> ) ) <EOL> else : <EOL> questions . append ( int ( w ) ) <EOL> question = stream . readWord ( ) <EOL> if question != '<STR_LIT:default>' : <EOL> raise ValueError ( '<STR_LIT>' % question ) <EOL> branch . comment = stream . comment <EOL> for q in questions : <EOL> answer = cls . readFromFile ( stream ) <EOL> branch [ q ] = answer <EOL> branch . default = cls . readFromFile ( stream ) <EOL> return branch <EOL> def writeToFile ( self , stream , indent = '<STR_LIT>' ) : <EOL> if indent : <EOL> stream . writeWord ( indent ) <EOL> stream . writeInt ( self . parentId ) <EOL> if self . isLeaf ( ) : <EOL> self . default . writeToFile ( stream ) <EOL> if self . comment : <EOL> stream . writelnWord ( '<STR_LIT>' + self . comment ) <EOL> else : <EOL> stream . writeNewLine ( ) <EOL> else : <EOL> stream . writeInt ( len ( self . _questions ) + <NUM_LIT:1> ) <EOL> l = sorted ( self . _questions ) <EOL> if l == range ( len ( l ) ) : <EOL> stream . writeWord ( self . makeRange ( len ( l ) ) ) <EOL> else : <EOL> for q in l : <EOL> stream . writeWord ( q ) <EOL> stream . writeWord ( '<STR_LIT:default>' ) <EOL> if self . comment : <EOL> stream . writelnWord ( '<STR_LIT>' + self . comment ) <EOL> else : <EOL> stream . writeNewLine ( ) <EOL> for i in l : <EOL> self . _questions [ i ] . writeToFile ( stream , indent + '<STR_LIT:U+0020>' ) <EOL> self . default . writeToFile ( stream , indent + '<STR_LIT:U+0020>' ) <EOL> def makeRange ( self , N ) : <EOL> if N < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif N == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> elif N == <NUM_LIT:1> : <EOL> return '<STR_LIT:0>' <EOL> else : <EOL> return '<STR_LIT>' + str ( N - <NUM_LIT:1> ) <EOL> class DT ( _Object ) : <EOL> NullTree = TreeBranch ( _LEAF , TreeLeaf ( <NUM_LIT:0> ) ) <EOL> def __init__ ( self , parent , name , parentCount , tree ) : <EOL> self . _tree = tree <EOL> self . _parentCount = parentCount <EOL> super ( DT , self ) . __init__ ( parent , name ) <EOL> def __eq__ ( self , other ) : <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if not hasattr ( other , attr ) : <EOL> return NotImplemented <EOL> return ( self . _parentCount == other . _parentCount ) and ( self . _tree == other . _tree ) <EOL> def getTree ( self ) : <EOL> return self . _tree <EOL> tree = property ( getTree ) <EOL> def getParentCount ( self ) : <EOL> return self . _parentCount <EOL> parentCount = property ( getParentCount ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream , readDTS = True ) : <EOL> name = stream . readWord ( ) <EOL> w = stream . readWord ( ) <EOL> try : <EOL> parentCount = int ( w ) <EOL> per_utterance = False <EOL> except ValueError : <EOL> per_utterance = True <EOL> if not per_utterance : <EOL> tree = TreeBranch . readFromFile ( stream ) <EOL> return cls ( parent , name , parentCount , tree ) <EOL> else : <EOL> return DTs ( parent , w , readDTS ) <EOL> def writeToFile ( self , stream ) : <EOL> stream . writelnWord ( self . name ) <EOL> stream . writelnInt ( self . parentCount ) <EOL> self . tree . writeToFile ( stream ) <EOL> def __getitem__ ( self , item ) : <EOL> return self . answer ( item ) <EOL> def answer ( self , values ) : <EOL> if len ( values ) != self . parentCount : <EOL> raise ValueError ( '<STR_LIT>' % self . parentCount ) <EOL> tree = self . tree <EOL> while True : <EOL> if tree . isLeaf ( ) : <EOL> return tree . default ( values ) <EOL> else : <EOL> tree = tree [ values [ tree . parentId ] ] <EOL> def __setitem__ ( self , item , value ) : <EOL> self . store ( item , value ) <EOL> def store ( self , parents , value ) : <EOL> if len ( parents ) != self . parentCount : <EOL> raise ValueError ( '<STR_LIT>' % self . parentCount ) <EOL> p_indexes = range ( len ( parents ) ) <EOL> if not isinstance ( value , TreeLeaf ) : <EOL> value = TreeLeaf ( value ) <EOL> tree = self . tree <EOL> while True : <EOL> if tree . isLeaf ( ) : <EOL> if p_indexes : <EOL> tree . default = deepcopy ( tree ) <EOL> tree . parentId = p_indexes . pop ( <NUM_LIT:0> ) <EOL> continue <EOL> else : <EOL> tree . default = value <EOL> break <EOL> else : <EOL> p_id = tree . parentId <EOL> p_val = parents [ p_id ] <EOL> if p_val in tree : <EOL> tree = tree [ p_val ] <EOL> if p_id in p_indexes : <EOL> p_indexes . remove ( p_id ) <EOL> else : <EOL> if p_indexes : <EOL> new_p_id = p_indexes . pop ( <NUM_LIT:0> ) <EOL> new_default = deepcopy ( tree . default ) <EOL> new_tree = TreeBranch ( new_p_id , new_default ) <EOL> tree [ p_val ] = new_tree <EOL> tree = new_tree <EOL> else : <EOL> tree [ p_val ] = TreeBranch ( _LEAF , value ) <EOL> break <EOL> def __delitem__ ( self , item ) : <EOL> self . delete ( item ) <EOL> def delete ( self , values ) : <EOL> if len ( values ) != self . parentCount : <EOL> raise ValueError ( '<STR_LIT>' % self . parentCount ) <EOL> tree = self . tree <EOL> old_tree = None <EOL> while True : <EOL> if tree . isLeaf ( ) : <EOL> if old_tree is not None : <EOL> val = values [ old_tree . parentId ] <EOL> del old_tree [ val ] <EOL> old_tree . vanish ( ) <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> val = values [ tree . parentId ] <EOL> if val in tree : <EOL> tree , old_tree = tree [ val ] , tree <EOL> else : <EOL> tree = tree [ val ] <EOL> old_tree = None <EOL> class DTs ( _Object ) : <EOL> def __init__ ( self , parent , name , readDTS = True ) : <EOL> gmtk_name = os . path . basename ( name ) . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> super ( DTs , self ) . __init__ ( parent , gmtk_name ) <EOL> self . _trees = [ ] <EOL> self . _readDTS = readDTS <EOL> self . setDtsFilename ( name ) <EOL> def __eq__ ( self , other ) : <EOL> if not hasattr ( other , '<STR_LIT>' ) : <EOL> return NotImplemented <EOL> return ( self . _trees == other . _trees ) <EOL> def getDtsFilename ( self ) : <EOL> return self . _dtsFilename <EOL> def setDtsFilename ( self , name ) : <EOL> self . _dtsFilename = name <EOL> if self . _readDTS : <EOL> self . readTrees ( ) <EOL> dtsFilename = property ( getDtsFilename , setDtsFilename ) <EOL> def writeToFile ( self , stream ) : <EOL> stream . writeInt ( len ( self . _trees ) ) <EOL> stream . writelnWord ( '<STR_LIT>' ) <EOL> for i in range ( len ( self . _trees ) ) : <EOL> stream . writelnInt ( i ) <EOL> self . _trees [ i ] . writeToFile ( stream ) <EOL> stream . writeNewLine ( ) <EOL> def getTrees ( self ) : <EOL> return self . _trees <EOL> trees = property ( getTrees ) <EOL> def discardTrees ( self ) : <EOL> trees = self . trees <EOL> parent = self . parent <EOL> while trees : <EOL> t = trees . pop ( ) <EOL> del parent [ DT , t . name ] <EOL> def readTrees ( self ) : <EOL> self . discardTrees ( ) <EOL> trees = self . trees <EOL> io = self . parent . preprocessFile ( self . dtsFilename ) <EOL> nobj = io . readInt ( ) <EOL> for i in range ( nobj ) : <EOL> ri = io . readInt ( ) <EOL> if i != ri : <EOL> raise ValueError ( '<STR_LIT>' % ( ri , i ) ) <EOL> trees . append ( DT . readFromFile ( self . parent , io ) ) <EOL> io . close ( ) <EOL> class _PMF ( _Object ) : <EOL> def __init__ ( self , parent , name , cardinality ) : <EOL> super ( _PMF , self ) . __init__ ( parent , name ) <EOL> self . _initTable ( cardinality ) <EOL> def _initTable ( self , cardinality ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def getCardinality ( self ) : <EOL> return len ( self ) <EOL> cardinality = property ( getCardinality ) <EOL> class DPMF ( _PMF , list ) : <EOL> def _initTable ( self , cardinality ) : <EOL> self [ : ] = [ <NUM_LIT:0> ] * cardinality <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> name = stream . readWord ( ) <EOL> cardinality = stream . readInt ( ) <EOL> dpmf = cls ( parent , name , cardinality ) <EOL> for i in range ( cardinality ) : <EOL> dpmf [ i ] = stream . readFloat ( ) <EOL> return dpmf <EOL> def writeToFile ( self , stream ) : <EOL> stream . writeWord ( self . name ) <EOL> stream . writeInt ( self . cardinality ) <EOL> for i in self : <EOL> stream . writeFloat ( i ) <EOL> def copy ( self , parent , name ) : <EOL> '''<STR_LIT>''' <EOL> other = self . __class__ ( parent , name , self . cardinality ) <EOL> other [ : ] = self [ : ] <EOL> return other <EOL> class MEAN ( _Object , list ) : <EOL> def __init__ ( self , parent , name , dimensionality ) : <EOL> _Object . __init__ ( self , parent , name ) <EOL> self . _initTable ( dimensionality ) <EOL> def _initTable ( self , dimensionality ) : <EOL> self [ : ] = [ <NUM_LIT:0> ] * dimensionality <EOL> def getDimensionality ( self ) : <EOL> return len ( self ) <EOL> dimensionality = property ( getDimensionality ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> name = stream . readWord ( ) <EOL> dimensionality = stream . readInt ( ) <EOL> vec = cls ( parent , name , dimensionality ) <EOL> for i in range ( dimensionality ) : <EOL> vec [ i ] = stream . readFloat ( ) <EOL> return vec <EOL> def writeToFile ( self , stream ) : <EOL> stream . writeWord ( self . name ) <EOL> stream . writeInt ( self . dimensionality ) <EOL> for i in self : <EOL> stream . writeFloat ( i ) <EOL> def copy ( self , parent , name ) : <EOL> '''<STR_LIT>''' <EOL> other = self . __class__ ( parent , name , self . dimensionality ) <EOL> other [ : ] = self [ : ] <EOL> return other <EOL> class COVAR ( MEAN ) : <EOL> pass <EOL> class GC ( _Object ) : <EOL> def __init__ ( self , parent , name , dimensionality , meanName , varName ) : <EOL> super ( GC , self ) . __init__ ( parent , name ) <EOL> self . dimensionality = dimensionality <EOL> self . meanName = meanName <EOL> self . varName = varName <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> dimensionality = stream . readInt ( ) <EOL> typ = stream . readInt ( ) <EOL> if typ != <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' % typ ) <EOL> name = stream . readWord ( ) <EOL> meanName = stream . readWord ( ) <EOL> varName = stream . readWord ( ) <EOL> gc = cls ( parent , name , dimensionality , meanName , varName ) <EOL> return gc <EOL> def writeToFile ( self , stream ) : <EOL> stream . writeInt ( self . dimensionality ) <EOL> stream . writeInt ( <NUM_LIT:0> ) <EOL> stream . writeWord ( self . name ) <EOL> stream . writeWord ( self . meanName ) <EOL> stream . writelnWord ( self . varName ) <EOL> class MG ( _Object ) : <EOL> def __init__ ( self , parent , name , dimensionality , weightsDpmfName , gcNames ) : <EOL> super ( MG , self ) . __init__ ( parent , name ) <EOL> self . dimensionality = dimensionality <EOL> self . weightsDpmfName = weightsDpmfName <EOL> self . gcNames = gcNames <EOL> def copy ( self , parent , name ) : <EOL> """<STR_LIT>""" <EOL> return MG ( parent , name , self . dimensionality , self . weightsDpmfName , self . gcNames ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> dimensionality = stream . readInt ( ) <EOL> name = stream . readWord ( ) <EOL> numComponents = stream . readInt ( ) <EOL> weightsDpmfName = stream . readWord ( ) <EOL> gcNames = [ ] <EOL> for i in range ( numComponents ) : <EOL> gcNames . append ( stream . readWord ( ) ) <EOL> mg = cls ( parent , name , dimensionality , weightsDpmfName , gcNames ) <EOL> return mg <EOL> def writeToFile ( self , stream ) : <EOL> stream . writeInt ( self . dimensionality ) <EOL> stream . writeWord ( self . name ) <EOL> stream . writeInt ( len ( self . gcNames ) ) <EOL> stream . writelnWord ( self . weightsDpmfName ) <EOL> for i in range ( len ( self . gcNames ) ) : <EOL> stream . writeWord ( self . gcNames [ i ] ) <EOL> class DLINK_MAT ( _Object ) : <EOL> def __init__ ( self , parent , name ) : <EOL> raise NotImplementedError ( ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> raise NotImplementedError ( ) <EOL> class WEIGHT_MAT ( _Object ) : <EOL> def __init__ ( self , parent , name , cardinality ) : <EOL> raise NotImplementedError ( ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> raise NotImplementedError ( ) <EOL> class GSMG ( _Object ) : <EOL> def __init__ ( self , parent , name , cardinality ) : <EOL> raise NotImplementedError ( ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> raise NotImplementedError ( ) <EOL> class LSMG ( _Object ) : <EOL> def __init__ ( self , parent , name , cardinality ) : <EOL> raise NotImplementedError ( ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> raise NotImplementedError ( ) <EOL> class MSMG ( _Object ) : <EOL> def __init__ ( self , parent , name , cardinality ) : <EOL> raise NotImplementedError ( ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> raise NotImplementedError ( ) <EOL> class SPMF ( _PMF ) : <EOL> def __init__ ( self , parent , name , cardinality , dpmfName ) : <EOL> super ( SPMF , self ) . __init__ ( parent , name , cardinality ) <EOL> self . _dpmfName = dpmfName <EOL> self . _ptrs = { } <EOL> def __eq__ ( self , other ) : <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if not hasattr ( other , attr ) : <EOL> return NotImplemented <EOL> return ( self . _dpmfName == other . _dpmfName ) and ( self . _ptrs == other . _ptrs ) <EOL> def _initTable ( self , cardinality ) : <EOL> self . _cardinality = cardinality <EOL> def getDpmf ( self ) : <EOL> return self . parent [ DPMF , self . dpmfName ] <EOL> dpmf = property ( getDpmf ) <EOL> def getDpmfName ( self ) : <EOL> return self . _dpmfName <EOL> dpmfName = property ( getDpmfName ) <EOL> def getPtrs ( self ) : <EOL> return self . _ptrs <EOL> ptrs = property ( getPtrs ) <EOL> def __len__ ( self ) : <EOL> return self . _cardinality <EOL> def __getitem__ ( self , item ) : <EOL> dpmf = self . dpmf <EOL> ptrs = self . _ptrs <EOL> l = len ( self ) <EOL> if isinstance ( item , ( int , long ) ) : <EOL> if item < <NUM_LIT:0> : <EOL> item += l <EOL> if not ( <NUM_LIT:0> <= item < l ) : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> if item in ptrs : <EOL> return dpmf [ ptrs [ item ] ] <EOL> else : <EOL> return <NUM_LIT:0.0> <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __setitem__ ( self , item , value ) : <EOL> dpmf = self . dpmf <EOL> ptrs = self . _ptrs <EOL> l = len ( self ) <EOL> if isinstance ( item , ( int , long ) ) : <EOL> if item < <NUM_LIT:0> : <EOL> item += l <EOL> if not ( <NUM_LIT:0> <= item < l ) : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> if item in ptrs : <EOL> dpmf [ ptrs [ item ] ] = value <EOL> else : <EOL> new_index = len ( dpmf ) <EOL> ptrs [ item ] = new_index <EOL> dpmf . append ( value ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __delitem__ ( self , item ) : <EOL> dpmf = self . dpmf <EOL> ptrs = self . _ptrs <EOL> l = len ( self ) <EOL> if isinstance ( item , ( int , long ) ) : <EOL> if item < <NUM_LIT:0> : <EOL> item += l <EOL> if not ( <NUM_LIT:0> <= item < l ) : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> if item in ptrs : <EOL> ref = ptrs [ item ] <EOL> del ptrs [ item ] <EOL> del dpmf [ ref ] <EOL> for key , value in ptrs . items ( ) : <EOL> if value > ref : <EOL> ptrs [ key ] = value - <NUM_LIT:1> <EOL> else : <EOL> pass <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> name = stream . readWord ( ) <EOL> cardinality = stream . readInt ( ) <EOL> ptrs = { } <EOL> length = stream . readInt ( ) <EOL> for i in range ( length ) : <EOL> ptr = stream . readInt ( ) <EOL> ptrs [ ptr ] = i <EOL> dpmfName = stream . readWord ( ) <EOL> spmf = cls ( parent , name , cardinality , dpmfName ) <EOL> spmf . ptrs . update ( ptrs ) <EOL> return spmf <EOL> def writeToFile ( self , stream ) : <EOL> stream . writelnWord ( self . name ) <EOL> stream . writelnInt ( self . cardinality ) <EOL> t = [ y [ <NUM_LIT:0> ] for y in sorted ( self . _ptrs . items ( ) , key = lambda x : x [ <NUM_LIT:1> ] ) ] <EOL> stream . writelnInt ( len ( t ) ) <EOL> for n in t : <EOL> stream . writeInt ( n ) <EOL> stream . writeNewLine ( ) <EOL> stream . writelnWord ( self . dpmfName ) <EOL> class _CPT ( _Object , ProbTable ) : <EOL> def __init__ ( self , parent , name , parent_cards , self_card ) : <EOL> super ( _CPT , self ) . __init__ ( parent , name , [ self_card ] , parent_cards ) <EOL> def getSelfCard ( self ) : <EOL> cards = self . selfCards <EOL> assert len ( cards ) == <NUM_LIT:1> <EOL> return cards [ <NUM_LIT:0> ] <EOL> selfCard = property ( getSelfCard ) <EOL> def getTableAsNumpyArray ( self ) : <EOL> mat = numpy . zeros ( self . parentCards + self . selfCards ) <EOL> for par_state in self . possibleParents : <EOL> mat [ par_state ] = self . _table [ par_state ] <EOL> return mat <EOL> array = property ( getTableAsNumpyArray ) <EOL> class DCPT ( _CPT , DenseTable ) : <EOL> def setTableFromNumpyArray ( self , nparray ) : <EOL> for parval in self . getPossibleParents ( ) : <EOL> self . _table [ parval ] = list ( nparray [ parval ] ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> name = stream . readWord ( ) <EOL> n_parents = stream . readInt ( ) <EOL> parent_cards = [ ] <EOL> total = <NUM_LIT:1> <EOL> for i in range ( n_parents ) : <EOL> card = stream . readInt ( ) <EOL> parent_cards . append ( card ) <EOL> total *= card <EOL> self_card = stream . readInt ( ) <EOL> total *= self_card <EOL> dcpt = cls ( parent , name , parent_cards , self_card ) <EOL> t = dcpt . table <EOL> for key in sorted ( t . keys ( ) ) : <EOL> for i in range ( self_card ) : <EOL> t [ key ] [ i ] = stream . readFloat ( ) <EOL> return dcpt <EOL> def writeToFile ( self , stream ) : <EOL> stream . writelnWord ( self . name ) <EOL> stream . writeInt ( len ( self . parentCards ) ) <EOL> for c in self . parentCards : <EOL> stream . writeInt ( c ) <EOL> stream . writeNewLine ( ) <EOL> self_card = self . selfCard <EOL> stream . writelnInt ( self_card ) <EOL> for i , val in enumerate ( self . table . flat ( ) ) : <EOL> if i > <NUM_LIT:0> and i % self_card == <NUM_LIT:0> : <EOL> stream . writeNewLine ( ) <EOL> stream . writeFloat ( val ) <EOL> else : <EOL> stream . writeNewLine ( ) <EOL> class SCPT ( _CPT ) : <EOL> def __init__ ( self , parent , name , parent_cards , self_card , dt_name , coll_name ) : <EOL> super ( SCPT , self ) . __init__ ( parent , name , parent_cards , self_card ) <EOL> self . _dtName = dt_name <EOL> self . _collName = coll_name <EOL> def __eq__ ( self , other ) : <EOL> return ( super ( SCPT , self ) . __eq__ ( other ) ) and ( self . _dtName == other . _dtName ) and ( self . _collName == other . _collName ) <EOL> def _initTable ( self , scard , pcard ) : <EOL> return None <EOL> def _eqTables ( self , other ) : <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if not hasattr ( other , attr ) : <EOL> return NotImplemented <EOL> return ( self . _dtName == other . _dtName ) and ( self . _collName == other . _collName ) <EOL> def getDtName ( self ) : <EOL> return self . _dtName <EOL> dtName = property ( getDtName ) <EOL> def getDt ( self ) : <EOL> return self . parent [ DT , self . dtName ] <EOL> dt = property ( getDt ) <EOL> def getCollName ( self ) : <EOL> return self . _collName <EOL> collName = property ( getCollName ) <EOL> def getColl ( self ) : <EOL> return self . parent [ NameCollection , self . collName ] <EOL> coll = property ( getColl ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> name = stream . readWord ( ) <EOL> n_parents = stream . readInt ( ) <EOL> parent_cards = [ ] <EOL> for i in range ( n_parents ) : <EOL> card = stream . readInt ( ) <EOL> parent_cards . append ( card ) <EOL> self_card = stream . readInt ( ) <EOL> dtName = stream . readWord ( ) <EOL> collName = stream . readWord ( ) <EOL> scpt = cls ( parent , name , parent_cards , self_card , dtName , collName ) <EOL> return scpt <EOL> def writeToFile ( self , stream ) : <EOL> stream . writelnWord ( self . name ) <EOL> stream . writeInt ( len ( self . parentCards ) ) <EOL> for c in self . parentCards : <EOL> stream . writeInt ( c ) <EOL> stream . writeNewLine ( ) <EOL> stream . writelnInt ( self . selfCard ) <EOL> stream . writelnWord ( self . dtName ) <EOL> stream . writelnWord ( self . collName ) <EOL> @ classmethod <EOL> def create ( cls , parent , name , parent_cards , self_card ) : <EOL> collection = NameCollection ( parent , name ) <EOL> collection . append ( name + '<STR_LIT>' ) <EOL> null_dpmf = DPMF ( parent , name + '<STR_LIT>' , self_card ) <EOL> null_spmf = SPMF ( parent , name + '<STR_LIT>' , self_card , name + '<STR_LIT>' ) <EOL> dt = DT ( parent , name , len ( parent_cards ) , DT . NullTree ) <EOL> return cls ( parent , name , parent_cards , self_card , name , name ) <EOL> def _getMassFunction ( self , parents ) : <EOL> index = self . dt [ parents ] <EOL> spmf = self . parent [ SPMF , self . coll [ index ] ] <EOL> return spmf <EOL> def newMassFunction ( self ) : <EOL> """<STR_LIT>""" <EOL> index = len ( self . coll ) <EOL> new_name = '<STR_LIT>' % ( self . name , index ) <EOL> dpmf = DPMF ( self . parent , new_name , <NUM_LIT:0> ) <EOL> spmf = SPMF ( self . parent , new_name , self . selfCard , dpmf . name ) <EOL> self . coll . append ( new_name ) <EOL> return index , spmf <EOL> def _createMassFunction ( self , parents ) : <EOL> tree_value = self . dt [ parents ] <EOL> if tree_value != <NUM_LIT:0> : <EOL> return self . _getMassFunction ( parents ) <EOL> else : <EOL> index , spmf = self . newMassFunction ( ) <EOL> self . dt [ parents ] = index <EOL> return spmf <EOL> def _delMassFunction ( self , parents ) : <EOL> del self . dt [ parents ] <EOL> class DetCPT ( _CPT ) : <EOL> def __init__ ( self , parent , name , parent_cards , self_card , dt_name ) : <EOL> super ( DetCPT , self ) . __init__ ( parent , name , parent_cards , self_card ) <EOL> self . _dtName = dt_name <EOL> def _eqTables ( self , other ) : <EOL> if not hasattr ( other , '<STR_LIT>' ) : <EOL> return NotImplemented <EOL> return ( self . _dtName == other . _dtName ) <EOL> def getDtName ( self ) : <EOL> return self . _dtName <EOL> dtName = property ( getDtName ) <EOL> def getDt ( self ) : <EOL> return self . parent [ DT , self . dtName ] <EOL> dt = property ( getDt ) <EOL> def _initTable ( self , scard , pcard ) : <EOL> return None <EOL> def _getMassFunction ( self , parents ) : <EOL> i = self . dt [ parents ] <EOL> ret = [ <NUM_LIT:0.> ] * self . selfCard <EOL> if not ( <NUM_LIT:0> <= i < self . selfCard ) : <EOL> raise IndexError ( "<STR_LIT>" % ( self . dtName , i , self . selfCard - <NUM_LIT:1> ) ) <EOL> ret [ i ] = <NUM_LIT:1.0> <EOL> return ret <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , stream ) : <EOL> name = stream . readWord ( ) <EOL> n_parents = stream . readInt ( ) <EOL> parent_cards = [ ] <EOL> for i in range ( n_parents ) : <EOL> card = stream . readInt ( ) <EOL> parent_cards . append ( card ) <EOL> self_card = stream . readInt ( ) <EOL> dtName = stream . readWord ( ) <EOL> detcpt = cls ( parent , name , parent_cards , self_card , dtName ) <EOL> return detcpt <EOL> def writeToFile ( self , stream ) : <EOL> stream . writelnWord ( self . name ) <EOL> stream . writeInt ( len ( self . parentCards ) ) <EOL> for c in self . parentCards : <EOL> stream . writeInt ( c ) <EOL> stream . writeNewLine ( ) <EOL> stream . writelnInt ( self . selfCard ) <EOL> stream . writelnWord ( self . dtName ) <EOL> class FeatureDefinition ( set ) : <EOL> def __init__ ( self , name , allowedValues ) : <EOL> self . name = name <EOL> self . update ( allowedValues ) <EOL> @ classmethod <EOL> def readFromFile ( cls , io ) : <EOL> name = io . readWord ( ) <EOL> empty , firstVal = io . readWord ( ) . split ( '<STR_LIT:(>' ) <EOL> if not empty == '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" % io . line ) <EOL> self . add ( firstVal ) <EOL> endSeen = False <EOL> while not endSeen : <EOL> val = io . readWord ( ) <EOL> try : <EOL> lastVal , leftovers = val . split ( '<STR_LIT:)>' ) <EOL> except ValueError : <EOL> self . add ( val ) <EOL> else : <EOL> endSeen = True <EOL> if lastVal : <EOL> self . add ( lastVal ) <EOL> if leftovers : <EOL> raise ValueError ( "<STR_LIT>" % io . line ) <EOL> return cls ( name , featureValues ) <EOL> def writeToFile ( self , io ) : <EOL> io . writeWord ( self . name ) <EOL> io . writeWord ( '<STR_LIT:(>' ) <EOL> for v in self : <EOL> io . writeWord ( v ) <EOL> io . writeWord ( '<STR_LIT:)>' ) <EOL> class FeatureValues ( object ) : <EOL> def __init__ ( self , name , featureValues ) : <EOL> self . featureValues = featureValues <EOL> self . name = name <EOL> @ classmethod <EOL> def readFromFile ( cls , io ) : <EOL> name = io . readWord ( ) <EOL> featureValues = io . file . readLine ( ) . split ( ) <EOL> return cls ( name , featureValues ) <EOL> def writeToFile ( self , io ) : <EOL> io . writeWord ( self . name ) <EOL> for v in self . featureValues : <EOL> io . writeWord ( v ) <EOL> class Question ( set ) : <EOL> def __init__ ( self , name , feature , values = set ( ) ) : <EOL> self . name = name <EOL> self . feature = feature <EOL> self . update ( values ) <EOL> @ classmethod <EOL> def readFromFile ( cls , io ) : <EOL> name = io . readWord ( ) <EOL> feature = io . readWord ( ) <EOL> q = Question ( name , feature ) <EOL> n = io . readInt ( ) <EOL> for i in range ( n ) : <EOL> q . add ( io . readWord ( ) ) <EOL> return q <EOL> def writeToFile ( self , io ) : <EOL> io . writeWord ( self . name ) <EOL> io . writeWord ( self . feature ) <EOL> io . writeInt ( len ( self ) ) <EOL> for v in sorted ( self ) : <EOL> io . writeWord ( v ) <EOL> class NamedObjectCollection ( dict ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , obj_type ) : <EOL> self . obj_type = obj_type <EOL> def readFromIO ( self , io ) : <EOL> nobj = io . readInt ( ) <EOL> for i in range ( nobj ) : <EOL> ri = io . readInt ( ) <EOL> if i != ri : <EOL> raise ValueError ( '<STR_LIT>' % ( ri , i ) ) <EOL> obj = self . obj_type . readFromFile ( io ) <EOL> self [ obj . name ] = obj <EOL> def writeToFile ( self , io ) : <EOL> items = sorted ( self . items ( ) ) <EOL> ostr = NumberedObjectOutStream ( io , self . obj_type ) <EOL> for ( name , obj ) in items : <EOL> ostr . writeObject ( obj ) <EOL> ostr . finalize ( ) <EOL> class ObjectOutStream ( object ) : <EOL> def __init__ ( self , io ) : <EOL> self . _io = io <EOL> self . objCount = <NUM_LIT:0> <EOL> self . _countBuf = self . _io . file . tell ( ) <EOL> self . _io . writeWord ( '<STR_LIT:U+0020>' * <NUM_LIT:20> ) <EOL> def finalize ( self ) : <EOL> curPos = self . _io . file . tell ( ) <EOL> self . _io . file . seek ( self . _countBuf ) <EOL> self . _io . writeInt ( self . objCount ) <EOL> self . _io . file . seek ( curPos ) <EOL> def writeComment ( self , s ) : <EOL> self . _io . writelnWord ( s ) <EOL> class UnnumberedOutStream ( ObjectOutStream ) : <EOL> def __init__ ( self , io ) : <EOL> super ( UnnumberedOutStream , self ) . __init__ ( io ) <EOL> self . _io . writelnWord ( '<STR_LIT>' ) <EOL> self . _io . writeNewLine ( ) <EOL> def writeObject ( self , obj ) : <EOL> obj . writeToFile ( self . _io ) <EOL> self . objCount += <NUM_LIT:1> <EOL> class NumberedObjectOutStream ( ObjectOutStream ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , io , obj_type ) : <EOL> try : <EOL> obj_type = obj_type . __name__ <EOL> except AttributeError : <EOL> pass <EOL> super ( NumberedObjectOutStream , self ) . __init__ ( io ) <EOL> self . _io . writelnWord ( '<STR_LIT>' + "<STR_LIT>" % obj_type ) <EOL> self . _io . writeNewLine ( ) <EOL> def writelnString ( self , s ) : <EOL> self . _io . writeInt ( self . objCount ) <EOL> self . _io . writelnWord ( s ) <EOL> self . objCount += <NUM_LIT:1> <EOL> def writeObject ( self , obj ) : <EOL> self . _io . writelnInt ( self . objCount ) <EOL> obj . writeToFile ( self . _io ) <EOL> self . _io . writeNewLine ( ) <EOL> self . _io . writeNewLine ( ) <EOL> self . objCount += <NUM_LIT:1> <EOL> class NamedObjectList ( list ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , obj_type ) : <EOL> self . obj_type = obj_type <EOL> def readFromIO ( self , io ) : <EOL> """<STR_LIT>""" <EOL> nobj = io . readInt ( ) <EOL> for i in range ( nobj ) : <EOL> ri = io . readInt ( ) <EOL> if i != ri : <EOL> raise ValueError ( '<STR_LIT>' % ( ri , i ) ) <EOL> obj = self . obj_type . readFromFile ( io ) <EOL> self [ i ] = obj <EOL> def writeToFile ( self , io ) : <EOL> ostr = NumberedObjectOutStream ( io , self . obj_type ) <EOL> for obj in self : <EOL> ostr . writeObject ( obj ) <EOL> ostr . finalize ( ) <EOL> class FeatureValuesOutStream ( NumberedObjectOutStream ) : <EOL> def __init__ ( self , io , name , featureDefsName ) : <EOL> io . writelnWord ( name + "<STR_LIT>" ) <EOL> io . writelnWord ( featureDefsName + "<STR_LIT>" ) <EOL> super ( FeatureValuesOutStream , self ) . __init__ ( io , FeatureValues ) <EOL> class FeatureValuesCollection ( NamedObjectCollection , _Object ) : <EOL> "<STR_LIT>" <EOL> obj_type = FeatureValues <EOL> def __init__ ( self , parent , name = None , featureDefsName = None , featureValuesList = [ ] ) : <EOL> self . featureDefsName = featureDefsName <EOL> self . update ( featureValuesList ) <EOL> NamedObjectCollection . __init__ ( self , self . obj_type ) <EOL> _Object . __init__ ( self , parent , name ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , io ) : <EOL> coll = cls ( parent ) <EOL> coll . readFromIO ( io ) <EOL> return coll <EOL> def readFromIO ( self , io ) : <EOL> self . name = io . readWord ( ) <EOL> self . featureDefsName = io . readWord ( ) <EOL> super ( FeatureValuesCollection , self ) . readFromIO ( io ) <EOL> def getFeatureDefs ( self ) : <EOL> return self . parent [ FeatureDefinitionList , self . featureDefsName ] <EOL> featureDefs = property ( getFeatureDefs ) <EOL> def writeToFile ( self , io ) : <EOL> ostr = FeatureValuesOutStream ( io , self . name , self . featureDefsName ) <EOL> for obj in self : <EOL> ostr . writeObject ( obj ) <EOL> ostr . finalize ( ) <EOL> class QuestionCollection ( NamedObjectCollection , _Object ) : <EOL> "<STR_LIT>" <EOL> obj_type = Question <EOL> def __init__ ( self , parent , name = None , featureDefsName = None , featureValuesName = None , questions = { } ) : <EOL> self . featureDefsName = featureDefsName <EOL> self . featureValuesName = featureValuesName <EOL> self . update ( questions ) <EOL> NamedObjectCollection . __init__ ( self , self . obj_type ) <EOL> _Object . __init__ ( self , parent , name ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , io ) : <EOL> coll = cls ( parent ) <EOL> coll . readFromIO ( io ) <EOL> return coll <EOL> def readFromIO ( self , io ) : <EOL> self . name = io . readWord ( ) <EOL> self . featureDefsName = io . readWord ( ) <EOL> self . featureValuesName = io . readWord ( ) <EOL> super ( FeatureValuesCollection , self ) . readFromIO ( io ) <EOL> def getFeatureDefs ( self ) : <EOL> return self . parent [ FeatureDefinitionList , self . featureDefsName ] <EOL> featureDefs = property ( getFeatureDefs ) <EOL> def writeToFile ( self , io ) : <EOL> io . writelnWord ( self . name + "<STR_LIT>" ) <EOL> io . writelnWord ( self . featureDefsName + "<STR_LIT>" ) <EOL> io . writelnWord ( self . featureValuesName + "<STR_LIT>" ) <EOL> super ( QuestionCollection , self ) . writeToFile ( io ) <EOL> def validate ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( ) <EOL> class FeatureDefinitionList ( NamedObjectList , _Object ) : <EOL> "<STR_LIT>" <EOL> obj_type = FeatureDefinition <EOL> def __init__ ( self , parent , name = None , featureDefList = [ ] ) : <EOL> NamedObjectList . __init__ ( self , self . obj_type ) <EOL> _Object . __init__ ( self , parent , name ) <EOL> self . extend ( featureDefList ) <EOL> @ classmethod <EOL> def readFromFile ( cls , parent , io ) : <EOL> coll = cls ( parent ) <EOL> coll . readFromIO ( io ) <EOL> return coll <EOL> def readFromIO ( self , io ) : <EOL> self . name = io . readWord ( ) <EOL> super ( FeatureDefinitionList , self ) . readFromFile ( io ) <EOL> def writeToFile ( self , io ) : <EOL> io . writelnWord ( self . name + "<STR_LIT>" ) <EOL> super ( FeatureDefinitionList , self ) . writeToFile ( io ) <EOL> class Workspace ( object ) : <EOL> """<STR_LIT>""" <EOL> knownObjects = { <EOL> '<STR_LIT>' : NameCollection , <EOL> '<STR_LIT>' : DPMF , <EOL> '<STR_LIT>' : MEAN , <EOL> '<STR_LIT>' : COVAR , <EOL> '<STR_LIT>' : GC , <EOL> '<STR_LIT>' : MG , <EOL> '<STR_LIT>' : SPMF , <EOL> '<STR_LIT>' : DCPT , <EOL> '<STR_LIT>' : SCPT , <EOL> '<STR_LIT>' : DetCPT , <EOL> '<STR_LIT>' : DT , <EOL> '<STR_LIT>' : DTs , <EOL> '<STR_LIT>' : DLINK_MAT , <EOL> '<STR_LIT>' : WEIGHT_MAT , <EOL> '<STR_LIT>' : GSMG , <EOL> '<STR_LIT>' : LSMG , <EOL> '<STR_LIT>' : MSMG , <EOL> '<STR_LIT>' : FeatureValuesCollection , <EOL> '<STR_LIT>' : FeatureDefinitionList , <EOL> '<STR_LIT>' : QuestionCollection , <EOL> } <EOL> def __init__ ( self , cppOptions = None , readDTS = True ) : <EOL> super ( Workspace , self ) . __init__ ( ) <EOL> self . _objects = dict ( [ ( obj_type , { } ) for obj_type in self . knownTypes ] ) <EOL> self . _cpp = Preprocessor ( cppOptions = cppOptions ) <EOL> self . _readDTS = readDTS <EOL> def __str__ ( self ) : <EOL> print "<STR_LIT>" % len ( self . objects ) <EOL> for obj_type in self . objects : <EOL> print "<STR_LIT>" % ( obj_type , len ( self [ obj_type ] ) ) <EOL> for o in sorted ( self [ obj_type ] . items ( ) ) : <EOL> print o <EOL> def getKnownTypes ( self ) : <EOL> return self . knownObjects . values ( ) <EOL> knownTypes = property ( getKnownTypes ) <EOL> def getObjects ( self ) : <EOL> return self . _objects <EOL> objects = property ( getObjects ) <EOL> def preprocessFile ( self , fn ) : <EOL> return WorkspaceIO ( self . _cpp . openFile ( fn ) ) <EOL> def readMasterFile ( self , mstr ) : <EOL> IN_FILE = '<STR_LIT>' <EOL> INLINE = '<STR_LIT>' <EOL> ASCII = '<STR_LIT:ascii>' <EOL> mstr_io = self . preprocessFile ( mstr ) <EOL> file_ios = { } <EOL> while True : <EOL> try : <EOL> command = mstr_io . readWord ( ) <EOL> except IOError : <EOL> break <EOL> if not command . endswith ( IN_FILE ) : <EOL> raise ValueError ( '<STR_LIT>' % command ) <EOL> type_name = command [ : - len ( IN_FILE ) ] <EOL> obj_type = self . knownObjects [ type_name ] <EOL> fn = mstr_io . readWord ( ) <EOL> if fn == INLINE : <EOL> self . readFromIO ( obj_type , mstr_io ) <EOL> else : <EOL> format = mstr_io . readWord ( ) <EOL> if format != ASCII : <EOL> raise ValueError ( '<STR_LIT>' % ( fn , format ) ) <EOL> if fn in file_ios : <EOL> file_io = file_ios [ fn ] <EOL> else : <EOL> file_io = self . preprocessFile ( fn ) <EOL> file_ios [ fn ] = file_io <EOL> self . readFromIO ( obj_type , mstr_io ) <EOL> def writeMasterFile ( self , mstr ) : <EOL> OUT_FILE = '<STR_LIT>' <EOL> ASCII = '<STR_LIT:ascii>' <EOL> mstr_io = self . preprocessFile ( mstr ) <EOL> file_ios = { } <EOL> while True : <EOL> try : <EOL> command = mstr_io . readWord ( ) <EOL> except IOError : <EOL> break <EOL> if not command . endswith ( OUT_FILE ) : <EOL> raise ValueError ( '<STR_LIT>' % command ) <EOL> type_name = command [ : - len ( OUT_FILE ) ] <EOL> obj_type = self . knownObjects [ type_name ] <EOL> fn = mstr_io . readWord ( ) <EOL> format = mstr_io . readWord ( ) <EOL> if format != ASCII : <EOL> raise ValueError ( '<STR_LIT>' % ( fn , format ) ) <EOL> if fn in file_ios : <EOL> file_io = file_ios [ fn ] <EOL> else : <EOL> fw = file ( fn , '<STR_LIT:w>' ) <EOL> file_io = WorkspaceIO ( fw ) <EOL> file_ios [ fn ] = file_io <EOL> self . writeToIO ( obj_type , file_io ) <EOL> for f in file_ios . values ( ) : <EOL> f . close ( ) <EOL> def readTrainableParamsFile ( self , trainableFile ) : <EOL> io = self . preprocessFile ( trainableFile ) <EOL> for typ in [ DPMF , SPMF , MEAN , COVAR , DLINK_MAT , WEIGHT_MAT , DCPT , GC , MG , GSMG , LSMG , MSMG , ] : <EOL> self . readFromIO ( typ , io ) <EOL> io . close ( ) <EOL> def writeTrainableParamsFile ( self , trainableFile ) : <EOL> io = WorkspaceIO ( file ( trainableFile , '<STR_LIT:w>' ) ) <EOL> self . writeTrainableParamsIO ( io ) <EOL> io . close ( ) <EOL> def writeTrainableParamsIO ( self , io ) : <EOL> for typ in [ DPMF , SPMF , MEAN , COVAR , DLINK_MAT , WEIGHT_MAT , DCPT , GC , MG , GSMG , LSMG , MSMG , ] : <EOL> self . writeToIO ( typ , io ) <EOL> def readFromFile ( self , obj_type , filename ) : <EOL> f = self . preprocessFile ( filename ) <EOL> try : <EOL> self . readFromIO ( obj_type , f ) <EOL> finally : <EOL> f . close ( ) <EOL> def writeToFile ( self , obj_type , filename ) : <EOL> f = WorkspaceIO . withFile ( filename , '<STR_LIT:w>' ) <EOL> try : <EOL> self . writeToIO ( obj_type , f ) <EOL> finally : <EOL> f . close ( ) <EOL> def readFromIO ( self , obj_type , io ) : <EOL> nobj = io . readInt ( ) <EOL> for i in range ( nobj ) : <EOL> ri = io . readInt ( ) <EOL> if i != ri : <EOL> raise ValueError ( '<STR_LIT>' % ( ri , i ) ) <EOL> if obj_type == DT : <EOL> obj = obj_type . readFromFile ( self , io , readDTS = self . _readDTS ) <EOL> else : <EOL> obj = obj_type . readFromFile ( self , io ) <EOL> def writeToIO ( self , obj_type , io ) : <EOL> items = sorted ( self [ obj_type ] . items ( ) ) <EOL> io . writelnWord ( '<STR_LIT>' + "<STR_LIT>" % obj_type . __name__ ) <EOL> io . writeInt ( len ( items ) ) <EOL> io . writeNewLine ( ) <EOL> io . writeNewLine ( ) <EOL> for i , ( name , obj ) in enumerate ( items ) : <EOL> io . writelnInt ( i ) <EOL> obj . writeToFile ( io ) <EOL> io . writeNewLine ( ) <EOL> def __contains__ ( self , ( obj_type , name ) ) : <EOL> return name in self . _objects [ obj_type ] <EOL> def __getitem__ ( self , item ) : <EOL> if not issequence ( item ) : <EOL> item = [ item ] <EOL> if len ( item ) == <NUM_LIT:1> : <EOL> return self . _objects [ item [ <NUM_LIT:0> ] ] <EOL> elif len ( item ) == <NUM_LIT:2> : <EOL> return self . _objects [ item [ <NUM_LIT:0> ] ] [ item [ <NUM_LIT:1> ] ] <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' % item ) <EOL> def __setitem__ ( self , obj_type , value ) : <EOL> name = value . name <EOL> if ( obj_type , name ) in self : <EOL> raise ValueError ( '<STR_LIT>' % ( obj_type . __name__ , name ) ) <EOL> self . _objects [ obj_type ] [ name ] = value <EOL> def __delitem__ ( self , ( obj_type , name ) ) : <EOL> obj = self . _objects [ obj_type ] [ name ] <EOL> del self . _objects [ obj_type ] [ name ] <EOL> obj . parent = None <EOL> def getObjLike ( self , obj_type , mask ) : <EOL> objs = self . objects [ obj_type ] <EOL> ret = [ ] <EOL> for name , obj in objs . iteritems ( ) : <EOL> if fnmatchcase ( name , mask ) : <EOL> ret . append ( obj ) <EOL> return ret <EOL> def delObjLike ( self , obj_type , mask ) : <EOL> objs = self . objects [ obj_type ] <EOL> to_del = [ ] <EOL> for name , obj in objs . iteritems ( ) : <EOL> if fnmatchcase ( name , mask ) : <EOL> to_del . append ( name ) <EOL> for name in to_del : <EOL> del self [ obj_type , name ] <EOL> class Preprocessor ( object ) : <EOL> def __init__ ( self , cppOptions = None ) : <EOL> super ( Preprocessor , self ) . __init__ ( ) <EOL> if cppOptions is None : <EOL> cppOptions = [ ] <EOL> self . cppOptions = cppOptions <EOL> def createProcess ( self , fn ) : <EOL> p = subprocess . Popen ( [ '<STR_LIT>' ] + self . cppOptions + [ '<STR_LIT>' , fn ] , stdout = subprocess . PIPE ) <EOL> return p <EOL> def openFile ( self , fn ) : <EOL> p = self . createProcess ( fn ) <EOL> return p . stdout <EOL> class WorkspaceIO ( object ) : <EOL> def __init__ ( self , fobj ) : <EOL> super ( WorkspaceIO , self ) . __init__ ( ) <EOL> self . _file = fobj <EOL> self . _line = <NUM_LIT:0> <EOL> self . _stack = [ ] <EOL> self . _ws = False <EOL> self . _wordsInFile = self . wordsGen ( ) <EOL> '''<STR_LIT>''' <EOL> self . comment = '<STR_LIT>' <EOL> @ classmethod <EOL> def withFile ( cls , * args , ** kwargs ) : <EOL> f = file ( * args , ** kwargs ) <EOL> return cls ( f ) <EOL> def getFile ( self ) : <EOL> return self . _file <EOL> file = property ( getFile ) <EOL> @ property <EOL> def name ( self ) : <EOL> return self . _file . name <EOL> def readWord ( self ) : <EOL> return self . _wordsInFile . next ( ) <EOL> def getLine ( self ) : <EOL> return self . _line <EOL> line = property ( getLine ) <EOL> def wordsGen ( self ) : <EOL> for line in self . _file : <EOL> self . _line += <NUM_LIT:1> <EOL> l = line . split ( '<STR_LIT:%>' , <NUM_LIT:2> ) <EOL> line = l [ <NUM_LIT:0> ] <EOL> self . comment = ( len ( l ) > <NUM_LIT:1> and l [ <NUM_LIT:1> ] or '<STR_LIT>' ) . strip ( ) <EOL> for w in line . split ( ) : <EOL> yield w <EOL> raise IOError ( '<STR_LIT>' ) <EOL> def readInt ( self ) : <EOL> return int ( self . readWord ( ) ) <EOL> def readFloat ( self ) : <EOL> return float ( self . readWord ( ) ) <EOL> def writeWord ( self , w ) : <EOL> if self . _ws : <EOL> self . _file . write ( '<STR_LIT:U+0020>' ) <EOL> self . _file . write ( '<STR_LIT:%s>' % w ) <EOL> self . _ws = True <EOL> def writelnWord ( self , w ) : <EOL> self . writeWord ( w ) <EOL> self . writeNewLine ( ) <EOL> def writeInt ( self , i ) : <EOL> if self . _ws : <EOL> self . _file . write ( '<STR_LIT:U+0020>' ) <EOL> self . _file . write ( '<STR_LIT>' % i ) <EOL> self . _ws = True <EOL> def writelnInt ( self , w ) : <EOL> self . writeInt ( w ) <EOL> self . writeNewLine ( ) <EOL> def writeFloat ( self , f ) : <EOL> if self . _ws : <EOL> self . _file . write ( '<STR_LIT:U+0020>' ) <EOL> self . _file . write ( '<STR_LIT>' % f ) <EOL> self . _ws = True <EOL> def writelnFloat ( self , w ) : <EOL> self . writeFloat ( w ) <EOL> self . writeNewLine ( ) <EOL> def writeNewLine ( self ) : <EOL> self . _file . write ( '<STR_LIT:\n>' ) <EOL> self . _ws = False <EOL> def close ( self ) : <EOL> self . _file . close ( ) <EOL> def cartezian ( * vectors ) : <EOL> """<STR_LIT>""" <EOL> ret = ret_old = [ ( v , ) for v in vectors [ <NUM_LIT:0> ] ] <EOL> for vec in vectors [ <NUM_LIT:1> : ] : <EOL> ret = [ ] <EOL> for v in vec : <EOL> for r in ret_old : <EOL> ret . append ( r + ( v , ) ) <EOL> ret_old = ret <EOL> return ret <EOL> def issequence ( obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , StringTypes ) : <EOL> return False <EOL> else : <EOL> try : <EOL> len ( obj ) <EOL> return True <EOL> except TypeError : <EOL> return False <EOL> def createWorkspaceFromNumpyArrays ( np_arrays ) : <EOL> """<STR_LIT>""" <EOL> w = Workspace ( ) <EOL> for name , array in np_arrays . items ( ) : <EOL> elem = DCPT ( w , name , array . shape [ : - <NUM_LIT:1> ] , array . shape [ - <NUM_LIT:1> ] ) <EOL> elem . setTableFromNumpyArray ( array ) <EOL> return w </s>
