def get_total_class_count(classes, idx2classes, old_total_class_count=None, removed_entry_dict=None):
    """
    Function which calculates the counts for each of the classes - i.e. given the idx2classes dictionary, how many times
    does each of the classes (defined by the classes list) appear?
    
    
    This can be calculate in one of two ways:

    (1) Directly from the idx2classes dictionary with the classes list. This is computationally a little expensive
        in that each entry in idx2classes dictionary, but gives a ground truthe.

    (2) Using an old total_class_count dictionary and a removed_entry_dict which reflects a single entry that is to
        be removed. To expand a bit on this, if an old_total_class_count dictionary is provided, it should be a dictionary
        of the format {'class name':class_count,...}. Then, a removed_entry_dict should be of the same format, an the
        returned total_class_count dictionary subtracts the counts for the removed_entry_dict from the old_total_class_count
        dictionary. 

    The first option is a de novo calculation, whereas the second one is an almost instantaneous correction, although we 
    do allocate a new dictionary so the return variable is a different instantiation than the old_total_class_count.
 
    Parameters
    -------------
    classes : list
        List of valid classes. All classes in the idx2classes dictionary should be represented in the 
        classes list, and, similarly, every dictionary in the idx2classes should map between each of 
        the classes and a count (even if the count is 0).
        
    idx2classes : dict
        Dictionary that maps index (starting at 0) to a second dictionary, which itself maps the 
        class name to the number of instances that class pops up.

    old_total_class_count : dict
        If provided, this should be a single dictionary that maps class to total count. 
        Default = None.

    removed_entry_dict : dict
        If provided, this should be a single dictionary that maps class to a count. The counts here
        are subtracted from the old_total_class_count


    Returns
    ------------
    dict
        Dict which maps between class identity and total counts for that class

    Raises
    ------------
    Exception
    
            
    """

    # if both removed entry dict and old_total_class_count are provdied
    if removed_entry_dict is not None and old_total_class_count is not None:

        # update the old_total_class_count and return - note we do allocate a 
        # new dictionary in case this function ever is used in a scenario where
        # pass by reference would be a problem        
        new_dict = {}
        for c in old_total_class_count:
            new_dict[c] = old_total_class_count[c] - removed_entry_dict[c]
            
        return new_dict
    

    # if only one of these two is allocate through an exception
    if removed_entry_dict is not None or old_total_class_count is not None:
        raise Exception('If removed_entry_dict is provided then old_total_class_count must also be provided (and vice versa)')
    

    # If we get here we're calculating the total_class_count from scratch
    total_class_count = {}

    # initialize
    for c in classes:
        total_class_count[c] = 0
        
    # for each entry
    for idx in idx2classes:
        for c in classes:
            total_class_count[c] = total_class_count[c] + idx2classes[idx][c]
                        
    return total_class_count