#NEWLINE# @ grouping ( interaction = 'callandtext' ) #NEWLINE# def percent_initiated_conversations( records ) : #NEWLINE# #INDENT# '\n    The percentage of conversations that have been initiated by the user.\n\n    Each call and each text conversation is weighted as a single interaction.  \n\n    See :ref:`Using bandicoot <conversations-label>` for a definition of conversations.\n    ' #NEWLINE# records = list ( records ) #NEWLINE# interactions = defaultdict ( list ) #NEWLINE# for r in records : #NEWLINE# #INDENT# interactions [ r . correspondent_id ] . append ( r ) #NEWLINE# #UNINDENT# def _percent_initiated( grouped ) : #NEWLINE# #INDENT# mapped = [ ( ( 1 if ( conv [ 0 ] . direction == 'out' ) else 0 ) , 1 ) for conv in _conversations ( grouped ) ] #NEWLINE# return mapped #NEWLINE# #UNINDENT# all_couples = [ sublist for i in interactions . values ( ) for sublist in _percent_initiated ( i ) ] #NEWLINE# if ( len ( interactions ) == 0 ) : #NEWLINE# #INDENT# ( init , total ) = ( 0 , 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( init , total ) = map ( sum , zip ( * all_couples ) ) #NEWLINE# #UNINDENT# return ( ( float ( init ) / total ) if ( total != 0 ) else 0 )