def generateKMeansSeries(activationsPath, labelIndexMappingPath, targetFolder):
    if targetFolder.endswith('/') == False:
        targetFolder += '/'

    activations = utility.arrayFromFile(activationsPath)
    labels = utility.getLabelStrings(labelIndexMappingPath)
    neurons = activations.shape[1] - len(labels)

    # test per label k-means
    k = PER_LABEL_START
    while k <= PER_LABEL_END:
        runCounter = 0
        while runCounter < RUNS_PER_TYPE:

            logger.info("Calculating per label KMeans with k = " + str(k) +
                        ".")
            currentTarget = targetFolder + "perLabel_" + str(
                k) + "/run_" + str(runCounter) + "/"
            if not os.path.exists(os.path.dirname(currentTarget)):
                os.makedirs(os.path.dirname(currentTarget))

            c, i = kMeans_per_label.findKMeansPerLabel(activations, k,
                                                       len(labels),
                                                       currentTarget,
                                                       labelIndexMappingPath)
            plot_cluster.plotClusters(kMeans_core.cleanUp(c), i, neurons,
                                      currentTarget, labels[:len(labels)])
            runCounter += 1
        k += 1

    # test mixed k-means
    k = MIXED_START
    while k <= MIXED_END:
        runCounter = 0
        while runCounter < RUNS_PER_TYPE:

            logger.info("Calculating mixed KMeans with k = " + str(k) + ".")
            currentTarget = targetFolder + "mixed_" + str(k) + "/run_" + str(
                runCounter) + "/"
            if not os.path.exists(os.path.dirname(currentTarget)):
                os.makedirs(os.path.dirname(currentTarget))

            [c, i] = kMeans_mixed.findKMeans(activations, k, len(labels),
                                             currentTarget)
            plot_cluster.plotClusters(kMeans_core.cleanUp(c), i,
                                      activations.shape[1] - len(labels),
                                      currentTarget, labels[:len(labels)])
            runCounter += 1
        k += MIXED_STEP
def generateKMeansSeries(activationsPath, labelIndexMappingPath, targetFolder):
    if targetFolder.endswith("/") == False:
        targetFolder += "/"

    activations = utility.arrayFromFile(activationsPath)
    labels = utility.getLabelStrings(labelIndexMappingPath)
    neurons = activations.shape[1] - len(labels)

    # test per label k-means
    k = PER_LABEL_START
    while k <= PER_LABEL_END:
        runCounter = 0
        while runCounter < RUNS_PER_TYPE:

            logger.info("Calculating per label KMeans with k = " + str(k) + ".")
            currentTarget = targetFolder + "perLabel_" + str(k) + "/run_" + str(runCounter) + "/"
            if not os.path.exists(os.path.dirname(currentTarget)):
                os.makedirs(os.path.dirname(currentTarget))

            c, i = kMeans_per_label.findKMeansPerLabel(
                activations, k, len(labels), currentTarget, labelIndexMappingPath
            )
            plot_cluster.plotClusters(kMeans_core.cleanUp(c), i, neurons, currentTarget, labels[: len(labels)])
            runCounter += 1
        k += 1

    # test mixed k-means
    k = MIXED_START
    while k <= MIXED_END:
        runCounter = 0
        while runCounter < RUNS_PER_TYPE:

            logger.info("Calculating mixed KMeans with k = " + str(k) + ".")
            currentTarget = targetFolder + "mixed_" + str(k) + "/run_" + str(runCounter) + "/"
            if not os.path.exists(os.path.dirname(currentTarget)):
                os.makedirs(os.path.dirname(currentTarget))

            [c, i] = kMeans_mixed.findKMeans(activations, k, len(labels), currentTarget)
            plot_cluster.plotClusters(
                kMeans_core.cleanUp(c), i, activations.shape[1] - len(labels), currentTarget, labels[: len(labels)]
            )
            runCounter += 1
        k += MIXED_STEP
    with open(sys.argv[2], "r") as result:
        for line in result.readlines():

            split = line.strip().split(' ')
            imagePaths.append(split[0])
    # for rootPath, subdirs, files in os.walk(sys.argv[2]):
    #    for f in files:
    #       if f.endswith('.jpg'):
    #          imagePaths.append(rootPath + f)

    clusters = None
    if len(sys.argv) == 5:
        clusters = pickle.load(sys.argv[4])

    activations = utility.arrayFromFile(sys.argv[1])

    if clusters == None:
        [clusters, iterations] = kMeans.findKMeans(activations, K, 0,
                                                   targetPath)

    for clusterIndex, cluster in enumerate(clusters):
        for index in cluster['memberIndices']:
            currentTarget = targetPath + str(clusterIndex) + "/"

            if not os.path.exists(os.path.dirname(currentTarget)):
                os.makedirs(os.path.dirname(currentTarget))

            copyfile(imagePaths[index],
                     currentTarget + os.path.basename(imagePaths[index]))