|
| 1 | +from bandicoot.helper.group import grouping |
| 2 | +import bandicoot as bc |
| 3 | + |
| 4 | + |
| 5 | +# Loading a User |
| 6 | +U = bc.read_csv('ego', 'data/', 'data/antennas.csv') |
| 7 | + |
| 8 | + |
| 9 | +####################### |
| 10 | +# Export visulization # |
| 11 | +####################### |
| 12 | + |
| 13 | +bc.visualization.export(U, 'my-viz-path') |
| 14 | + |
| 15 | + |
| 16 | +######################################### |
| 17 | +# Run individual and spatial indicators # |
| 18 | +######################################### |
| 19 | + |
| 20 | +bc.individual.percent_initiated_conversations(U) |
| 21 | +bc.spatial.number_of_antennas(U) |
| 22 | +bc.spatial.radius_of_gyration(U) |
| 23 | + |
| 24 | + |
| 25 | +###################################### |
| 26 | +# Group indicators by weeks or month # |
| 27 | +###################################### |
| 28 | + |
| 29 | +# The groupby keyword controls the aggregation: |
| 30 | +# - groupby='week' to divide by week (by default), |
| 31 | +# - groupby='month' to divide by month, |
| 32 | +# - groupby=None to aggregate all values. |
| 33 | + |
| 34 | +bc.individual.active_days(U, groupby='week') |
| 35 | +bc.individual.active_days(U, groupby='month') |
| 36 | +bc.individual.active_days(U, groupby=None) # No grouping |
| 37 | + |
| 38 | + |
| 39 | +################################ |
| 40 | +# Returning extended summaries # |
| 41 | +################################ |
| 42 | + |
| 43 | +# The summary keyword can take three values: |
| 44 | +# - summary='default' to return mean and standard deviation, |
| 45 | +# - summary='extended' for the second type of indicators, to return mean, std, |
| 46 | +# median, skewness and std of the distribution, |
| 47 | +# - summary=None to return the full distribution. |
| 48 | + |
| 49 | +bc.individual.call_duration(U) |
| 50 | +bc.individual.call_duration(U, summary='extended') |
| 51 | +bc.individual.call_duration(U, summary=None) |
| 52 | + |
| 53 | +############################ |
| 54 | +# Splitting days and weeks # |
| 55 | +############################ |
| 56 | + |
| 57 | +# split_week divide records by 'all week', 'weekday', and 'weekend'. |
| 58 | +# split_day divide records by 'all day', 'day', and 'night'. |
| 59 | + |
| 60 | +bc.individual.active_days(U, split_week=True, split_day=True) |
| 61 | + |
| 62 | +######################## |
| 63 | +# Exporting indicators # |
| 64 | +######################## |
| 65 | + |
| 66 | +# The function bc.utils.all computes automatically all indicators for a single |
| 67 | +# user. You can use the same keywords to group by week/month/all time range, or |
| 68 | +# return extended statistics. |
| 69 | + |
| 70 | +features = bc.utils.all(U, groupby=None) |
| 71 | + |
| 72 | +bc.to_csv(features, 'demo_export_user.csv') |
| 73 | +bc.to_json(features, 'demo_export_user.json') |
| 74 | + |
| 75 | +####################### |
| 76 | +# Extending bandicoot # |
| 77 | +####################### |
| 78 | + |
| 79 | +# You can easily develop your indicator using the @grouping decorator. You only |
| 80 | +# need to write a function taking as input a list of records and returning an |
| 81 | +# integer or a list of integers (for a distribution). The @grouping decorator |
| 82 | +# wraps the function and call it for each group of weeks. |
| 83 | + |
| 84 | + |
| 85 | +@grouping(interaction='call') |
| 86 | +def shortest_call(records): |
| 87 | + in_durations = (r.call_duration for r in records) |
| 88 | + return min(in_durations) |
| 89 | + |
| 90 | +shortest_call(U) |
| 91 | +shortest_call(U, split_day=True) |
0 commit comments