def useHier(topX, regX, R, hierIters, hierInner, regIters, regInner,
            tensorInfo):
    topY1, top1stats, top1mstats = CP_APR.cp_apr(topX,
                                                 R,
                                                 maxiters=hierIters,
                                                 maxinner=hierInner)
    # reduce them to probability and then just sort them
    topY1.normalize_sort(1)
    topY1 = pmdTools.zeroSmallFactors(topY1, 1e-4)
    ### Use the factors to populate the factors
    Udiag = np.zeros((len(tensorInfo['diag']), R))
    Umed = np.zeros((len(tensorInfo['med']), R))
    ### Patient factors stays the same
    for idx, diag in enumerate(tensorInfo['diag']):
        topDiagIdx = tensorInfo['diagHier'][diag]
        diagCount = tensorInfo['diagHierCount'][topDiagIdx]
        Udiag[idx, :] = topY1.U[1][topDiagIdx, :] / diagCount
    for idx, med in enumerate(tensorInfo['med']):
        topMedIdx = tensorInfo['medHier'][med]
        medCount = tensorInfo['medHierCount'][topMedIdx]
        Umed[idx, :] = topY1.U[2][topMedIdx, :] / medCount
    Mtop = ktensor.ktensor(np.ones(R), [topY1.U[0].copy(), Udiag, Umed])
    Y1, ystats, mstats = CP_APR.cp_apr(X1,
                                       R,
                                       Minit=Mtop,
                                       maxiters=regIters,
                                       maxinner=regInner)
    return Y1, topY1, top1stats, top1mstats, ystats, mstats
def useHier(topX, regX, R, hierIters, hierInner, regIters, regInner, tensorInfo):
    topY1, top1stats, top1mstats = CP_APR.cp_apr(topX, R, maxiters=hierIters, maxinner=hierInner)
    # reduce them to probability and then just sort them
    topY1.normalize_sort(1)
    topY1 = pmdTools.zeroSmallFactors(topY1, 1e-4)
    ### Use the factors to populate the factors
    Udiag = np.zeros((len(tensorInfo['diag']), R))
    Umed = np.zeros((len(tensorInfo['med']), R))
    ### Patient factors stays the same
    for idx, diag in enumerate(tensorInfo['diag']):
        topDiagIdx = tensorInfo['diagHier'][diag]
        diagCount = tensorInfo['diagHierCount'][topDiagIdx]
        Udiag[idx,:] = topY1.U[1][topDiagIdx,:] / diagCount
    for idx, med in enumerate(tensorInfo['med']):
        topMedIdx = tensorInfo['medHier'][med]
        medCount = tensorInfo['medHierCount'][topMedIdx]        
        Umed[idx,:] = topY1.U[2][topMedIdx,:] / medCount
    Mtop = ktensor.ktensor(np.ones(R), [topY1.U[0].copy(), Udiag, Umed])
    Y1, ystats, mstats = CP_APR.cp_apr(X1, R, Minit=Mtop, maxiters=regIters, maxinner=regInner)
    return Y1, topY1, top1stats, top1mstats, ystats, mstats
outfile = 'results/unique-hier-db-{0}-{1}.csv'
rawTopFile = 'results/unique-hier-top-raw-{0}-{1}.dat'
rawRegFile = 'results/unique-hier-reg-raw-{0}-{1}.dat'
yaxis = [np.array(tensorInfo['pat']), np.array(tensorInfo['diag']), np.array(tensorInfo['med'])]

modelOut = file("results/uniq-hier-{0}.sql".format(exptID), "w")
for i in range(totalIter):
    # set the seed for repeatability
    np.random.seed(seedArray[i])
    yreg, ytop, ytopstats, ytopMstats, yregstats, yregMstats = useHier(topX1, X1, R, hierIters, innerIters, regIters, innerIters, tensorInfo)
    yreg.writeRawFile(rawRegFile.format(exptID, i))
    ytop.writeRawFile(rawTopFile.format(exptID, i))
    
    yreg.normalize_sort(1)
    yreg = pmdTools.zeroSmallFactors(yreg, 1e-4)
    yfile = outfile.format(exptID, i)
    __writeDBFile(yreg, yfile, yaxis, exptID, i)
    
    mstats = yregMstats
    modelOut.write("insert into joyceho.tensor_uniq_models values({0},{1},{2},\'{3}\',{4},{5},{6},{7},{8});\n".format(exptID, i, labelID, expt_desc, hierIters+regIters, innerIters, mstats['LS'], mstats['LL'], mstats['KKT']))
    modelOut.write("load client from /home/joyceho/workspace/tensor/{0} of del modified by coldel| insert into joyceho.tensor_uniq_results;\n".format(yfile))

    yTopFactor.append(ytop)
    yRegFactor.append(yreg)
    yTopStats.append(ytopstats)
    yRegStats.append(yregstats)
    mTopStats.append(ytopMstats)
    mRegStats.append(yregMstats)

## Calculate all the scores
    np.array(tensorInfo['pat']),
    np.array(tensorInfo['diag']),
    np.array(tensorInfo['med'])
]

modelOut = file("results/uniq-hier-{0}.sql".format(exptID), "w")
for i in range(totalIter):
    # set the seed for repeatability
    np.random.seed(seedArray[i])
    yreg, ytop, ytopstats, ytopMstats, yregstats, yregMstats = useHier(
        topX1, X1, R, hierIters, innerIters, regIters, innerIters, tensorInfo)
    yreg.writeRawFile(rawRegFile.format(exptID, i))
    ytop.writeRawFile(rawTopFile.format(exptID, i))

    yreg.normalize_sort(1)
    yreg = pmdTools.zeroSmallFactors(yreg, 1e-4)
    yfile = outfile.format(exptID, i)
    __writeDBFile(yreg, yfile, yaxis, exptID, i)

    mstats = yregMstats
    modelOut.write(
        "insert into joyceho.tensor_uniq_models values({0},{1},{2},\'{3}\',{4},{5},{6},{7},{8});\n"
        .format(exptID, i, labelID, expt_desc, hierIters + regIters,
                innerIters, mstats['LS'], mstats['LL'], mstats['KKT']))
    modelOut.write(
        "load client from /home/joyceho/workspace/tensor/{0} of del modified by coldel| insert into joyceho.tensor_uniq_results;\n"
        .format(yfile))

    yTopFactor.append(ytop)
    yRegFactor.append(yreg)
    yTopStats.append(ytopstats)