import collections


def threshold_clusters(clustering, file_names, threshold):
    """
    Method to split clustering into those above and below some threshold.
    Only clusters larger than (or equal to) the threshold is allowed on the "above" list.

    Parameters
    ----------
    clustering  :   iterable object
                    1D list/numpy array with assigned cluster labels
    file_names  :   iterable object
                    1D list/numpy array with the filenames of the images associated with each label in "clustering"
    threshold   :   int
                    Threshold of size of clusters. Determines what clusters will be in each of the returned lists.

    Returns
    -------
    above   :   list
                2D lists with labels and associated filenames, for clusters above/equal to threshold
    below   :   list
                2D lists with labels and associated filenames, for clusters below threshold
    cluster_sizes   :   Counter-object
                        Contains the sizes of each type of cluster
    """

    cluster_sizes = collections.Counter(clustering)

    z_files = zip(clustering, file_names)
    above = [[cluster, filename.strip()] for cluster, filename in z_files if cluster_sizes[cluster] >= threshold]

    z_files = zip(clustering, file_names)
    below = [[cluster, filename.strip()] for cluster, filename in z_files if cluster_sizes[cluster] < threshold]

    return above, below, cluster_sizes