@grouping(interaction='callandtext')
def response_rate_text(records):
    records = list(records)

    interactions = defaultdict(list)
    for r in records:
        interactions[r.correspondent_id].append(r)

    def _response_rate(grouped):
        received, responded = 0, 0
        conversations = _conversations(grouped)

        for conv in conversations:
            if len(conv) != 0:
                first = conv[0]
                if first.direction == 'in' and first.interaction == 'text':
                    received += 1
                    if any((i.direction == 'out' for i in conv)):
                        responded += 1

        return responded, received

    # Group all records by their correspondent, and compute the response rate
    # for each
    all_couples = map(_response_rate, interactions.values())
    responded, received = map(sum, zip(*all_couples))

    return float(responded) / received if received != 0 else 0
The response rate of the user (between 0 and 1).
dataset/ETHPy150Open yvesalexandre/bandicoot/bandicoot/individual.py/response_rate_text