def create_lookup(stream, derived_fields, index_field):
    """
    Build a lookup table from an indexed field to a set of derived fields.
    Ensures that there is a unique relation, raises assertion if violated
    :param stream: stream of records
    :param derived_fields: list of derived fields assumed to be a function of the
        indexed field only
    :param index_field: the index field which determined the others
    :return: lookup table from index to dict of derived fields
    """
    lookup = {}
    for row in stream:
        index = row[index_field]
        if index not in lookup:
            # put info in lookup
            lookup[index] = {}
            for derived_field in derived_fields:
                value = row[derived_field]
                lookup[index][derived_field] = value
        else:
            # ensure there is a single unique derived field
            for derived_field in derived_fields:
                value = row[derived_field]
                assert lookup[index][derived_field] == value

    return lookup