from typing import Counter


def count_neuron_frequency(neurons):
    """
    Calculate neuron frequency for given labels sorted by id number and frequency (ensure the same frequency still keep sort by number )
    Counter usage example: https://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-an-unordered-list
    :param neurons: labels type
    :return: (unique_ids, frequency)
    """
    ids = [int(i) for v in neurons.values() for i in v.keys()]
    counter = [(k, v) for k, v in sorted(Counter(ids).items(), key = lambda item: item[0])]  # sorted by id number
    counter = [(k, v) for k, v in sorted(counter, key = lambda item: item[1], reverse = True)]
    unique_ids, frequency = [v[0] for v in counter], [v[1] for v in counter]
    return unique_ids, frequency