Skip to content

Commit aa6d8ca

Browse files
authored
Merge pull request #160 from BIDMCDigitalPsychiatry/add-text-features
Added messages_usage text features
2 parents 1ebf930 + b082356 commit aa6d8ca

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

cortex/secondary/call_number.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def call_number(call_direction="all", **kwargs):
5656

5757
else:
5858
number = None
59-
log.info(""" %s was passed but is not an acceptable argument.
60-
Acceptable arguments include 'all','incoming', or 'outgoing'" """,
61-
call_direction)
59+
raise Exception(f"{call_direction} is not a proper argument. "
60+
+ "Must be incoming, outgoing, or all")
6261

6362
return {'timestamp': kwargs['start'], 'value': number}

cortex/secondary/text_degree.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
""" Module text_degree from raw feature messages_usage """
2+
import numpy as np
3+
4+
from ..feature_types import secondary_feature, log
5+
from ..raw.messages_usage import messages_usage
6+
7+
MS_IN_A_DAY = 86400000
8+
9+
10+
@secondary_feature(
11+
name='cortex.feature.text_degree',
12+
dependencies=[messages_usage]
13+
)
14+
def text_degree(**kwargs):
15+
"""The number of unique phone numbers a participant texted.
16+
17+
Args:
18+
**kwargs:
19+
id (string): The participant's LAMP id. Required.
20+
start (int): The initial UNIX timestamp (in ms) of the window for
21+
which the feature is being generated. Required.
22+
end (int): The last UNIX timestamp (in ms) of the window for
23+
which the feature is being generated. Required.
24+
25+
Returns:
26+
A dict consisting:
27+
timestamp (int): The beginning of the window (same as
28+
kwargs['start']).
29+
value (float): Number of unique phone numbers texted.
30+
"""
31+
_texts = messages_usage(**kwargs)['data']
32+
33+
if len(_texts) == 0:
34+
unique_contacts = None
35+
return {'timestamp': kwargs['start'], 'value': unique_contacts}
36+
else:
37+
unique_contacts = np.sum(text['totalUniqueContacts'] for text in _texts)
38+
39+
return {'timestamp': kwargs['start'], 'value': unique_contacts}
40+
41+

cortex/secondary/text_number.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
""" Module for text_number from raw feature messages_usage """
2+
import numpy as np
3+
from ..feature_types import secondary_feature, log
4+
from ..raw.messages_usage import messages_usage
5+
6+
MS_IN_A_DAY = 86400000
7+
@secondary_feature(
8+
name='cortex.feature.text_number',
9+
dependencies=[messages_usage]
10+
)
11+
def text_number(text_direction="all", **kwargs):
12+
"""Returns the number of texts made.
13+
14+
Args:
15+
text_direction (string): Returns all texts if "all",
16+
returns received texts if "incoming", returns
17+
sent texts if "outgoing".
18+
**kwargs:
19+
id (string): The participant's LAMP id. Required.
20+
start (int): The initial UNIX timestamp (in ms) of the window
21+
for which the feature is being generated. Required.
22+
end (int): The last UNIX timestamp (in ms) of the window
23+
for which the feature is being generated. Required.
24+
25+
Returns:
26+
A dict consisting:
27+
timestamp (int): The beginning of the window
28+
(same as kwargs['start']).
29+
value (float): The number of texts.
30+
"""
31+
_texts = messages_usage(**kwargs)['data']
32+
33+
incoming_text_count = np.sum(text['totalIncomingMessages'] for text in _texts)
34+
outgoing_text_count = np.sum(text['totalOutgoingMessages'] for text in _texts)
35+
36+
if len(_texts) == 0:
37+
number = None
38+
return {'timestamp': kwargs['start'], 'value': number}
39+
elif text_direction == "all":
40+
number = incoming_text_count + outgoing_text_count
41+
elif text_direction == "incoming":
42+
number = incoming_text_count
43+
elif text_direction == "outgoing":
44+
number = outgoing_text_count
45+
else:
46+
number = None
47+
raise Exception(f"{text_direction} is not a proper argument. "
48+
+ "Must be incoming, outgoing, or all")
49+
50+
return {'timestamp': kwargs['start'], 'value': number}

0 commit comments

Comments
 (0)