# Path: tensorlog/comline.py
# def parseCommandLine(argv,extraArgConsumer=None,extraArgSpec=[],extraArgUsage=[]):
#     def usage():
#     def status(msg): logging.info('%s time %.3f sec mem %.3f Gb' % (msg,time.time()-startTime,util.memusage()))
# def isUncachefromSrc(s): return s.find("|")>=0
# def getCacheSrcPair(s): return s.split("|")
# def makeCacheSrcPair(s1,s2): return "%s|%s" % (s1,s2)
# def parseDatasetSpec(spec,db):
# def parseDBSpec(spec):
# def parseProgSpec(spec,db,proppr=False):
# 
# Path: tensorlog/declare.py
# def asMode(spec):
#     def __init__(self,goal):
#     def args(self):
#     def arg(self,i):
#     def getArity(self):
#     def getFunctor(self):
#     def __str__(self):
#     def __repr__(self):
#     def __hash__(self):
#     def __eq__(self,other):
#     def __init__(self,goal,strict=True):
#     def isInput(self,i):
#     def isOutput(self,i):
#     def isConst(self,i):
#     def __str__(self):
#     def __init__(self,goal):
#     def getType(self,i):
#     def typeSet(self):
# class AbstractDeclaration(object):
# class ModeDeclaration(AbstractDeclaration):
# class TypeDeclaration(AbstractDeclaration):
# 
# Path: tensorlog/interp.py
#   DEBUGGER_AVAILABLE = True
#   DEBUGGER_AVAILABLE = False
#       X = self.db.onehot(sym)
# class Interp(object):
#     def __init__(self,prog,trainData=None,testData=None):
#     def help(self):
#     def set(self,depth=None,echo=None,normalize=None,maxTraceMsg=-1,trace=None):
#     def list(self,str=None):
#     def _listRules(self,functor,arity):
#     def _listAllRules(self):
#     def _listFacts(self,functor,arity):
#     def _listAllFacts(self):
#     def _listFunction(self,modeSpec):
#     def eval(self,modeSpec,sym,inputType=None,outputType=None):
#     def debug(self,modeSpec,sym):
#     def helpConfig(self):
#     def debugDset(self,modeSpec,test=False):
# 
# Path: tensorlog/mutil.py
# NONETYPE=type(None) 
#         D = SS.csr_matrix((mat.data,mat.indices-loIndex,mat.indptr),shape=newShape,dtype='float32').todense()
#     D1 = SS.csr_matrix((m1.data,m1.indices-loIndex,m1.indptr),shape=newShape1,dtype='float32').todense()
#     D2 = SS.csr_matrix((m2.data,m2.indices-loIndex,m2.indptr),shape=newShape2,dtype='float32').todense()
# def summary(mat):
# def pprintSummary(mat):
# def checkCSR(mat,context='unknown'):
# def checkNoNANs(mat,context='unknown'):
# def maxValue(mat):
# def densify(mat,maxExpandFactor=-1,maxExpandIntercept=-1):
# def denseSize(m,loIndex,hiIndex):
# def sparseSize(m,loIndex,hiIndex):
# def codensify(m1,m2,maxExpandFactor=-1,maxExpandIntercept=-1):
# def undensify(denseMat, info):
# def mean(mat):
# def rowsum(mat):
# def mapData(dataFun,mat):
# def stack(mats):
# def numRows(m):
# def numCols(m):
# def nzCols(m,i):
# def repeat(row,n):
# def alterMatrixRows(mat,alterationFun):
# def softmax(db,mat):
#         def softMaxAlteration(data,lo,hi,unused):
# def denseSoftmax(m):
# def broadcastAndComponentwiseMultiply(m1,m2):
# def multiplyByBroadcastRowVec(m,v):
# def broadcastAndWeightByRowSum(m1,m2):
# def shuffleRows(m,shuffledRowNums=None):
# def selectRows(m,lo,hi):


import sys
import time
from tensorlog import comline
from tensorlog import declare
from tensorlog import interp
from tensorlog import mutil

def setExptParams(n):
    factFile = 'smoker-%d.cfacts' % n
    queryFile = 'query-entities-%d.txt' % n
    optdict,args = comline.parseCommandLine(['--prog','smokers.ppr','--proppr','--db',factFile])
    ti = interp.Interp(optdict['prog'])
    ti.prog.setRuleWeights()
    ti.prog.maxDepth = 99
    rows = []
    for line in open(queryFile):
        sym = line.strip()
        rows.append(ti.db.onehot(sym))
        if len(rows)==BATCHSIZE:
            break
    X = mutil.stack(rows)
    print(len(rows),'queries')
    return ti,X,rows

def runMain(n,minibatch):

    (ti,X,rows) = setExptParams(n)
    nQueries = X.shape[0]
    start0 = time.time()
    for modeString in ["t_stress/io", "t_influences/io","t_cancer_spont/io", "t_cancer_smoke/io"]:
        print('eval',modeString, end=' ')
        start = time.time()
        if minibatch:
            ti.prog.eval(declare.asMode(modeString), [X])
        else:
            for Xi in rows:

# Path: tensorlog/comline.py
# def parseCommandLine(argv,extraArgConsumer=None,extraArgSpec=[],extraArgUsage=[]):
#     def usage():
#     def status(msg): logging.info('%s time %.3f sec mem %.3f Gb' % (msg,time.time()-startTime,util.memusage()))
# def isUncachefromSrc(s): return s.find("|")>=0
# def getCacheSrcPair(s): return s.split("|")
# def makeCacheSrcPair(s1,s2): return "%s|%s" % (s1,s2)
# def parseDatasetSpec(spec,db):
# def parseDBSpec(spec):
# def parseProgSpec(spec,db,proppr=False):
# 
# Path: tensorlog/declare.py
# def asMode(spec):
#     def __init__(self,goal):
#     def args(self):
#     def arg(self,i):
#     def getArity(self):
#     def getFunctor(self):
#     def __str__(self):
#     def __repr__(self):
#     def __hash__(self):
#     def __eq__(self,other):
#     def __init__(self,goal,strict=True):
#     def isInput(self,i):
#     def isOutput(self,i):
#     def isConst(self,i):
#     def __str__(self):
#     def __init__(self,goal):
#     def getType(self,i):
#     def typeSet(self):
# class AbstractDeclaration(object):
# class ModeDeclaration(AbstractDeclaration):
# class TypeDeclaration(AbstractDeclaration):
# 
# Path: tensorlog/interp.py
#   DEBUGGER_AVAILABLE = True
#   DEBUGGER_AVAILABLE = False
#       X = self.db.onehot(sym)
# class Interp(object):
#     def __init__(self,prog,trainData=None,testData=None):
#     def help(self):
#     def set(self,depth=None,echo=None,normalize=None,maxTraceMsg=-1,trace=None):
#     def list(self,str=None):
#     def _listRules(self,functor,arity):
#     def _listAllRules(self):
#     def _listFacts(self,functor,arity):
#     def _listAllFacts(self):
#     def _listFunction(self,modeSpec):
#     def eval(self,modeSpec,sym,inputType=None,outputType=None):
#     def debug(self,modeSpec,sym):
#     def helpConfig(self):
#     def debugDset(self,modeSpec,test=False):
# 
# Path: tensorlog/mutil.py
# NONETYPE=type(None) 
#         D = SS.csr_matrix((mat.data,mat.indices-loIndex,mat.indptr),shape=newShape,dtype='float32').todense()
#     D1 = SS.csr_matrix((m1.data,m1.indices-loIndex,m1.indptr),shape=newShape1,dtype='float32').todense()
#     D2 = SS.csr_matrix((m2.data,m2.indices-loIndex,m2.indptr),shape=newShape2,dtype='float32').todense()
# def summary(mat):
# def pprintSummary(mat):
# def checkCSR(mat,context='unknown'):
# def checkNoNANs(mat,context='unknown'):
# def maxValue(mat):
# def densify(mat,maxExpandFactor=-1,maxExpandIntercept=-1):
# def denseSize(m,loIndex,hiIndex):
# def sparseSize(m,loIndex,hiIndex):
# def codensify(m1,m2,maxExpandFactor=-1,maxExpandIntercept=-1):
# def undensify(denseMat, info):
# def mean(mat):
# def rowsum(mat):
# def mapData(dataFun,mat):
# def stack(mats):
# def numRows(m):
# def numCols(m):
# def nzCols(m,i):
# def repeat(row,n):
# def alterMatrixRows(mat,alterationFun):
# def softmax(db,mat):
#         def softMaxAlteration(data,lo,hi,unused):
# def denseSoftmax(m):
# def broadcastAndComponentwiseMultiply(m1,m2):
# def multiplyByBroadcastRowVec(m,v):
# def broadcastAndWeightByRowSum(m1,m2):
# def shuffleRows(m,shuffledRowNums=None):
# def selectRows(m,lo,hi):


# Path: datasets/smokers/scaleup/expt.py
import sys
import time
from tensorlog import comline
from tensorlog import declare
from tensorlog import interp
from tensorlog import mutil


def setExptParams(n):
    factFile = 'smoker-%d.cfacts' % n
    queryFile = 'query-entities-%d.txt' % n
    optdict,args = comline.parseCommandLine(['--prog','smokers.ppr','--proppr','--db',factFile])
    ti = interp.Interp(optdict['prog'])
    ti.prog.setRuleWeights()
    ti.prog.maxDepth = 99
    rows = []
    for line in open(queryFile):
        sym = line.strip()
        rows.append(ti.db.onehot(sym))
        if len(rows)==BATCHSIZE:
            break
    X = mutil.stack(rows)
    print(len(rows),'queries')
    return ti,X,rows

def runMain(n,minibatch):

    (ti,X,rows) = setExptParams(n)
    nQueries = X.shape[0]
    start0 = time.time()
    for modeString in ["t_stress/io", "t_influences/io","t_cancer_spont/io", "t_cancer_smoke/io"]:
        print('eval',modeString, end=' ')
        start = time.time()
        if minibatch:
            ti.prog.eval(declare.asMode(modeString), [X])
        else:
            for Xi in rows:

                ti.prog.eval(declare.asMode(modeString), [Xi])