-
Notifications
You must be signed in to change notification settings - Fork 20
/
contact_reciprocal.py
338 lines (287 loc) · 11.2 KB
/
contact_reciprocal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -*- coding: utf-8 -*-
"""
Classes for searching and dealing with reciprocal contacts.
"""
from typing import Union
from flowmachine.core.mixins.graph_mixin import GraphMixin
from flowmachine.features.subscriber.contact_balance import ContactBalance
from flowmachine.features.utilities.events_tables_union import EventsTablesUnion
from flowmachine.features.subscriber.metaclasses import SubscriberFeature
from flowmachine.features.utilities.direction_enum import Direction
from flowmachine.utils import make_where, standardise_date
class ContactReciprocal(GraphMixin, SubscriberFeature):
"""
This class classifies a subscribers contact as reciprocal or not. In
addition to that, it calculates the number of incoming and outgoing events
between the subscriber and her/his counterpart as well as the proportion
that those events represent in total incoming and outgoing events.
A reciprocal contact is a contact who has initiated contact with the
subscriber and who also has been the counterpart of an initatiated contact
by the subscriber.
Parameters
----------
start, stop : str
iso-format start and stop datetimes
hours : 2-tuple of floats, default 'all'
Restrict the analysis to only a certain set
of hours within each day.
subscriber_subset : str, list, flowmachine.core.Query, flowmachine.core.Table, default None
If provided, string or list of string which are msisdn or imeis to limit
results to; or, a query or table which has a column with a name matching
subscriber_identifier (typically, msisdn), to limit results to.
exclude_self_calls : bool, default True
Set to false to *include* calls a subscriber made to themself
tables : str or list of strings, default 'all'
Can be a string of a single table (with the schema)
or a list of these. The keyword all is to select all
subscriber tables
Example
-------
>> s = ContactReciprocal('2016-01-01', '2016-01-08')
>> s.get_dataframe()
subscriber msisdn_counterpart events_in events_out proportion_in \
oQOm1YkDxljp3AVL 1jwYL3Nl1Y46lNeQ 0 21 0.0
E9nlO1dMyVAWr60X KXVqP6JyVDGzQa3b 17 0 1.0
alye4Z3yz5ENvnXY jWlyLwbGdvKV35Mm 0 19 0.0
dXogRAnyg1Q9lE3J qj8gk6qbN8R3DBdO 0 27 0.0
5jLW0EWeoyg6NQo3 W8eEBvV6P8Jv01XZ 20 0 1.0
... ... ... ... ...
proportion_out reciprocal
1.0 False
0.0 False
1.0 False
1.0 False
0.0 False
... ...
"""
def __init__(
self,
start,
stop,
*,
hours="all",
tables="all",
exclude_self_calls=True,
subscriber_subset=None,
):
self.tables = tables
self.start = standardise_date(start)
self.stop = standardise_date(stop)
self.hours = hours
self.exclude_self_calls = exclude_self_calls
self.tables = tables
self.contact_in_query = ContactBalance(
self.start,
self.stop,
hours=self.hours,
tables=self.tables,
subscriber_identifier="msisdn",
direction=Direction.IN,
exclude_self_calls=self.exclude_self_calls,
subscriber_subset=subscriber_subset,
)
self.contact_out_query = ContactBalance(
self.start,
self.stop,
hours=self.hours,
tables=self.tables,
subscriber_identifier="msisdn",
direction=Direction.OUT,
exclude_self_calls=self.exclude_self_calls,
subscriber_subset=subscriber_subset,
)
super().__init__()
@property
def column_names(self):
return [
"subscriber",
"msisdn_counterpart",
"events_in",
"events_out",
"proportion_in",
"proportion_out",
"reciprocal",
]
def _make_query(self):
sql = f"""
SELECT
COALESCE(I.subscriber, O.subscriber) AS subscriber,
COALESCE(I.msisdn_counterpart, O.msisdn_counterpart) AS msisdn_counterpart,
COALESCE(I.events_in, 0) AS events_in ,
COALESCE(O.events_out, 0) AS events_out,
COALESCE(I.proportion_in, 0) AS proportion_in,
COALESCE(O.proportion_out, 0) AS proportion_out,
CASE
WHEN I.events_in IS NULL OR O.events_out IS NULL THEN FALSE
ELSE TRUE
END AS reciprocal
FROM (
SELECT
subscriber,
msisdn_counterpart,
events AS events_in,
proportion AS proportion_in
FROM ({self.contact_in_query.get_query()}) C
) I
FULL OUTER JOIN (
SELECT
subscriber,
msisdn_counterpart,
events AS events_out,
proportion AS proportion_out
FROM ({self.contact_out_query.get_query()}) C
) O
ON
I.subscriber = O.subscriber AND
I.msisdn_counterpart = O.msisdn_counterpart
"""
return sql
class ProportionContactReciprocal(SubscriberFeature):
"""
This class calculates the proportion of reciprocal contacts a subscriber has.
A reciprocal contact is a contact who has initiated contact with the
subscriber and who also has been the counterpart of an initatiated contact
by the subscriber.
Parameters
----------
contact_reciprocal: flowmachine.features.ContactReciprocal
An instance of `ContactReciprocal` listing which contacts are reciprocal
and which are not.
Example
-------
>> s = ProportionContactReciprocal(ContactReciprocal('2016-01-01', '2016-01-08'))
>> s.get_dataframe()
subscriber value
9vXy462Ej8V1kpWl 0.0
Q4mwVxpBOo7X2lb9 0.0
5jLW0EWeoyg6NQo3 0.0
QEoRM9vlkV18N4ZY 0.0
a76Ajyb9dmEYNd8L 0.0
... ...
"""
def __init__(self, contact_reciprocal):
self.contact_reciprocal_query = contact_reciprocal
@property
def column_names(self):
return ["subscriber", "proportion"]
def _make_query(self):
return f"""
SELECT subscriber, AVG(reciprocal::int) AS proportion
FROM ({self.contact_reciprocal_query.get_query()}) R
GROUP BY subscriber
"""
class ProportionEventReciprocal(SubscriberFeature):
"""
This class calculates the proportion of events with a reciprocal contact
per subscriber. It is possible to fine-tune the period for which a
reciprocal contact must have happened.
A reciprocal contact is a contact who has initiated contact with the
subscriber and who also has been the counterpart of an initatiated contact
by the subscriber.
Parameters
----------
start, stop : str
iso-format start and stop datetimes
hours : 2-tuple of floats, default 'all'
Restrict the analysis to only a certain set
of hours within each day.
contact_reciprocal: flowmachine.features.ContactReciprocal
An instance of `ContactReciprocal` listing which contacts are reciprocal
and which are not.
subscriber_identifier : {'msisdn', 'imei'}, default 'msisdn'
Either msisdn, or imei, the column that identifies the subscriber.
subscriber_subset : str, list, flowmachine.core.Query, flowmachine.core.Table, default None
If provided, string or list of string which are msisdn or imeis to limit
results to; or, a query or table which has a column with a name matching
subscriber_identifier (typically, msisdn), to limit results to.
direction : {'in', 'out', 'both'} or Direction, default Direction.OUT
Whether to consider calls made, received, or both. Defaults to 'out'.
exclude_self_calls : bool, default True
Set to false to *include* calls a subscriber made to themself
tables : str or list of strings, default 'all'
Can be a string of a single table (with the schema)
or a list of these. The keyword all is to select all
subscriber tables
Example
-------
>> s = ProportionEventReciprocal('2016-01-01', '2016-01-08',
ContactReciprocal('2016-01-01', '2016-01-08'))
>> s.get_dataframe()
subscriber value
9vXy462Ej8V1kpWl 0.0
Q4mwVxpBOo7X2lb9 0.0
5jLW0EWeoyg6NQo3 0.0
QEoRM9vlkV18N4ZY 0.0
a76Ajyb9dmEYNd8L 0.0
... ...
"""
def __init__(
self,
start,
stop,
contact_reciprocal,
*,
direction: Union[str, Direction] = Direction.OUT,
subscriber_identifier="msisdn",
hours="all",
subscriber_subset=None,
tables="all",
exclude_self_calls=True,
):
self.start = start
self.stop = stop
self.subscriber_identifier = subscriber_identifier
self.hours = hours
self.exclude_self_calls = exclude_self_calls
self.direction = Direction(direction)
self.tables = tables
column_list = [
self.subscriber_identifier,
"msisdn",
"msisdn_counterpart",
*self.direction.required_columns,
]
self.unioned_query = EventsTablesUnion(
self.start,
self.stop,
tables=self.tables,
columns=column_list,
hours=hours,
subscriber_identifier=subscriber_identifier,
subscriber_subset=subscriber_subset,
)
self.contact_reciprocal_query = contact_reciprocal
super().__init__()
@property
def column_names(self):
return ["subscriber", "value"]
def _make_query(self):
filters = [self.direction.get_filter_clause()]
if self.exclude_self_calls:
filters.append("subscriber != msisdn_counterpart")
where_clause = make_where(filters)
on_clause = f"""
ON {'U.subscriber' if self.subscriber_identifier == 'msisdn' else 'U.msisdn'} = R.subscriber
AND U.msisdn_counterpart = R.msisdn_counterpart
"""
sql = f"""
SELECT subscriber, AVG(reciprocal::int) AS value
FROM (
SELECT U.subscriber, COALESCE(reciprocal, FALSE) AS reciprocal
FROM (
SELECT *
FROM ({self.unioned_query.get_query()}) U
{where_clause}
) U
LEFT JOIN (
SELECT subscriber, msisdn_counterpart, reciprocal
FROM ({self.contact_reciprocal_query.get_query()}) R
) R
{on_clause}
) R
GROUP BY subscriber
"""
return sql