|
| 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