<s> import os <EOL> import bandicoot as bc <EOL> from collections import Counter <EOL> def export_antennas ( user , directory ) : <EOL> with open ( os . path . join ( directory , '<STR_LIT>' ) , '<STR_LIT:wb>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> for id , ( lat , lon ) in user . antennas . items ( ) : <EOL> interactions = sum ( [ <NUM_LIT:1> for r in user . records if r . position . antenna == id ] ) <EOL> f . write ( "<STR_LIT>" . format ( id , lat , lon , interactions ) ) <EOL> def export_transitions ( user , directory ) : <EOL> antennas_id = [ r . position . antenna for r in user . records ] <EOL> transitions = Counter ( [ ( min ( i , j ) , max ( i , j ) ) for ( i , j ) in zip ( antennas_id , antennas_id [ <NUM_LIT:1> : ] ) ] ) <EOL> with open ( os . path . join ( directory , '<STR_LIT>' ) , '<STR_LIT:wb>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> for ( source , target ) , count in transitions . items ( ) : <EOL> if source != target : <EOL> f . write ( "<STR_LIT>" . format ( source , target , count ) ) <EOL> def export_timeline ( user , directory ) : <EOL> with open ( os . path . join ( directory , '<STR_LIT>' ) , '<STR_LIT:wb>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> for r in user . records : <EOL> time = r . datetime . strftime ( '<STR_LIT>' ) <EOL> interaction = ( '<STR_LIT>' if r . direction == '<STR_LIT>' else '<STR_LIT>' ) + r . interaction <EOL> call_duration = r . call_duration or '<STR_LIT>' <EOL> f . write ( "<STR_LIT>" . format ( time , interaction , call_duration ) ) <EOL> def export_network ( user , directory ) : <EOL> m_texts = bc . network . matrix_directed_weighted ( user , '<STR_LIT:text>' ) <EOL> m_calls = bc . network . matrix_directed_weighted ( user , '<STR_LIT>' ) <EOL> nb_users = len ( m_texts ) <EOL> links = [ ] <EOL> for i in range ( nb_users ) : <EOL> for j in range ( nb_users ) : <EOL> source_calls = m_calls [ j ] [ i ] or <NUM_LIT:0> <EOL> target_calls = m_calls [ i ] [ j ] or <NUM_LIT:0> <EOL> source_texts = m_texts [ j ] [ i ] or <NUM_LIT:0> <EOL> target_texts = m_texts [ i ] [ j ] or <NUM_LIT:0> <EOL> if i < j and source_calls + target_calls + source_texts + target_texts > <NUM_LIT:0> : <EOL> links . append ( [ i , j , ( source_calls , target_calls , source_texts , target_texts ) ] ) <EOL> names = [ user . name ] + [ i for i in user . network . keys ( ) if i != user . name ] <EOL> nodes = set ( l [ <NUM_LIT:0> ] for l in links ) | set ( l [ <NUM_LIT:1> ] for l in links ) <EOL> with open ( os . path . join ( directory , '<STR_LIT>' ) , '<STR_LIT:wb>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> for n in nodes : <EOL> f . write ( "<STR_LIT>" . format ( names [ n ] , int ( None in m_texts [ n ] or None in m_calls [ n ] ) ) ) <EOL> compress_ids = dict ( zip ( nodes , range ( len ( nodes ) ) ) ) <EOL> with open ( os . path . join ( directory , '<STR_LIT>' ) , '<STR_LIT:wb>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> for i , j , data in links : <EOL> f . write ( "<STR_LIT>" . format ( * ( compress_ids [ i ] , compress_ids [ j ] ) + data ) ) </s>
