clustering.py
def createFolds(cluster_groups, numfolds, target_lines, randomize):
    '''split target clusters into numfolds folds with balanced num poses per fold
       If randomize, will balance less well.
    '''
    folds = [[] for _ in range(numfolds)]
    fold_numposes = [0]*numfolds
    group_numposes = [0]*len(cluster_groups)
    foldmap = {}
    for i, group in enumerate(cluster_groups):
        #count num poses per group
        for target in group:            
            group_numposes[i] += len(target_lines[target])
    for _ in cluster_groups:
        #iteratively assign group with most poses to fold with fewest poses
        maxgroup = group_numposes.index(np.max(group_numposes))
        if randomize:
            space = np.max(fold_numposes) - np.array(fold_numposes)
            tot = np.sum(space)
            if tot == 0:
                minfold = np.random.choice(numfolds)
            else: #weighted selection, prefer spots with more free space
                choice = np.random.choice(tot)
                tot = 0
                for i in range(len(space)):
                    tot += space[i]
                    if choice < tot:
                        minfold = i
                        break
        else:
            minfold = fold_numposes.index(np.min(fold_numposes))
        folds[minfold].extend(cluster_groups[maxgroup])
        fold_numposes[minfold] += group_numposes[maxgroup]
        group_numposes[maxgroup] = -1
        for t in cluster_groups[maxgroup]:
            foldmap[t] = minfold
    print('Poses per fold: {}'.format(fold_numposes))
    for f in folds:
        f.sort()
    return folds, foldmap
split target clusters into numfolds folds with balanced num poses per fold
       If randomize, will balance less well.
    
split target clusters into numfolds folds with balanced num poses per fold
If randomize, will balance less well.
split target clusters into numfolds folds with balanced num poses per fold If randomize will balance less well .
def createFolds(cluster_groups, numfolds, target_lines, randomize):
    folds = [[] for _ in range(numfolds)]
    fold_numposes = [0]*numfolds
    group_numposes = [0]*len(cluster_groups)
    foldmap = {}
    for i, group in enumerate(cluster_groups):
        for target in group:            
            group_numposes[i] += len(target_lines[target])
    for _ in cluster_groups:
        maxgroup = group_numposes.index(np.max(group_numposes))
        if randomize:
            space = np.max(fold_numposes) - np.array(fold_numposes)
            tot = np.sum(space)
            if tot == 0:
                minfold = np.random.choice(numfolds)
            else: 
                choice = np.random.choice(tot)
                tot = 0
                for i in range(len(space)):
                    tot += space[i]
                    if choice < tot:
                        minfold = i
                        break
        else:
            minfold = fold_numposes.index(np.min(fold_numposes))
        folds[minfold].extend(cluster_groups[maxgroup])
        fold_numposes[minfold] += group_numposes[maxgroup]
        group_numposes[maxgroup] = -1
        for t in cluster_groups[maxgroup]:
            foldmap[t] = minfold
    print('Poses per fold: {}'.format(fold_numposes))
    for f in folds:
        f.sort()
    return folds, foldmap
def createFolds ( cluster_groups , numfolds , target_lines , randomize ) : folds = [ [ ] for _ in range ( numfolds ) ] fold_numposes = [ 0 ] * numfolds group_numposes = [ 0 ] * len ( cluster_groups ) foldmap = { } for i , group in enumerate ( cluster_groups ) : for target in group : group_numposes [ i ] += len ( target_lines [ target ] ) for _ in cluster_groups : maxgroup = group_numposes . index ( np . max ( group_numposes ) ) if randomize : space = np . max ( fold_numposes ) - np . array ( fold_numposes ) tot = np . sum ( space ) if tot == 0 : minfold = np . random . choice ( numfolds ) else : choice = np . random . choice ( tot ) tot = 0 for i in range ( len ( space ) ) : tot += space [ i ] if choice < tot : minfold = i break else : minfold = fold_numposes . index ( np . min ( fold_numposes ) ) folds [ minfold ] . extend ( cluster_groups [ maxgroup ] ) fold_numposes [ minfold ] += group_numposes [ maxgroup ] group_numposes [ maxgroup ] = - 1 for t in cluster_groups [ maxgroup ] : foldmap [ t ] = minfold print ( 'Poses per fold: {}' . format ( fold_numposes ) ) for f in folds : f . sort ( ) return folds , foldmap
split target clusters into numfolds folds with balanced num poses per fold
If randomize, will balance less well.
split target clusters into numfolds folds with balanced num poses per fold If randomize will balance less well .
'''split target clusters into numfolds folds with balanced num poses per fold
       If randomize, will balance less well.
    ''' #count num poses per group #iteratively assign group with most poses to fold with fewest poses #weighted selection, prefer spots with more free space
cluster_groups
numfolds
target_lines
randomize