import itertools


def relative_bics(saveresult):
    """
    This convenience function calculates all the relative BIC comparisons
    from an AFINO save result.

    Parameters
    ----------
    saveresult : dict
        An AFINO save result dictionary. Can be restored from a previous save file

    Returns
    -------
    dbic_values : dict
        A dictionary of relative BIC value comparisons, i.e. BICa - BICb for every a,b pair
    
    """
    
    bics = []
    ids = []   
    dbic_values = {}

    for key in saveresult:
        bics.append(saveresult[key]['BIC'])
        ids.append(int(saveresult[key]['ID']))

    # extract all the ID and BIC pairs (includes reverse pairings)
    combo_list = list(itertools.permutations(ids))
    combo_bic_list = list(itertools.permutations(bics))

    # for each ID/BIC pair a,b, put BICa - BICb in a dictionary
    for i, combo in enumerate(combo_list):
        dbic_values['dBIC_' + str(combo[0]) + '_minus_' + str(combo[1])] = combo_bic_list[i][0] - combo_bic_list[i][1]
    
    return dbic_values