2021-02-01 18:24:24 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pandas as pd
|
|
|
|
from numpy.random import default_rng
|
2021-02-02 16:20:46 +01:00
|
|
|
from pandas.testing import assert_series_equal
|
2021-02-01 18:24:24 +01:00
|
|
|
|
2021-08-18 15:23:30 +02:00
|
|
|
from features.communication import *
|
2021-02-01 18:24:24 +01:00
|
|
|
|
|
|
|
rng = default_rng()
|
|
|
|
|
|
|
|
|
|
|
|
class CallsFeatures(unittest.TestCase):
|
2021-02-02 16:20:46 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
2021-02-01 18:24:24 +01:00
|
|
|
call_rows = 10
|
2021-04-06 11:48:37 +02:00
|
|
|
callers = np.concatenate(
|
|
|
|
(
|
2021-02-02 16:20:46 +01:00
|
|
|
np.repeat("caller1", 2),
|
2021-02-01 18:24:24 +01:00
|
|
|
np.repeat("caller2", 3),
|
2021-02-02 16:20:46 +01:00
|
|
|
np.repeat("caller3", 4),
|
2021-04-06 11:48:37 +02:00
|
|
|
np.repeat("caller4", 1),
|
|
|
|
),
|
|
|
|
axis=None,
|
|
|
|
)
|
2021-02-01 18:24:24 +01:00
|
|
|
rng.shuffle(callers)
|
2021-05-07 15:48:17 +02:00
|
|
|
cls.comm = pd.DataFrame(
|
2021-04-06 11:48:37 +02:00
|
|
|
{
|
|
|
|
"id": np.linspace(0, call_rows - 1, num=call_rows, dtype="u4") + 100,
|
|
|
|
"_id": np.linspace(0, call_rows - 1, num=call_rows, dtype="u4"),
|
|
|
|
"timestamp": np.sort(
|
|
|
|
rng.integers(1612169903000, 1614556703000, size=call_rows)
|
|
|
|
),
|
|
|
|
"device_id": "device1",
|
|
|
|
"participant_id": 29,
|
2021-05-07 15:48:17 +02:00
|
|
|
"trace": callers,
|
2021-04-06 11:48:37 +02:00
|
|
|
}
|
|
|
|
)
|
2021-05-07 15:48:17 +02:00
|
|
|
cls.calls = cls.comm.assign(
|
|
|
|
call_type=rng.integers(1, 3, size=call_rows, endpoint=True),
|
|
|
|
call_duration=rng.integers(0, 600, size=call_rows),
|
|
|
|
)
|
|
|
|
cls.sms = cls.comm.assign(
|
|
|
|
message_type=rng.integers(1, 2, size=call_rows, endpoint=True)
|
|
|
|
)
|
2021-02-02 16:20:46 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def assertSeriesEqual(cls, a, b, msg=None, **optional):
|
|
|
|
try:
|
|
|
|
assert_series_equal(a, b, **optional)
|
|
|
|
except AssertionError as e:
|
|
|
|
raise cls.failureException(msg) from e
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.addTypeEqualityFunc(pd.DataFrame, self.assertSeriesEqual)
|
2021-02-01 18:24:24 +01:00
|
|
|
|
|
|
|
def test_get_calls_data(self):
|
|
|
|
calls_from_db = get_call_data(["nokia_0000003"])
|
|
|
|
self.assertIsNotNone(calls_from_db)
|
|
|
|
|
|
|
|
def test_enumeration(self):
|
2021-02-02 16:20:46 +01:00
|
|
|
self.calls["contact_id_manual"] = self.calls["trace"].astype("category")
|
2021-04-06 11:48:37 +02:00
|
|
|
self.calls["contact_id_manual"] = self.calls[
|
|
|
|
"contact_id_manual"
|
|
|
|
].cat.rename_categories(
|
|
|
|
{"caller1": 2, "caller2": 1, "caller3": 0, "caller4": 3}
|
2021-02-02 16:20:46 +01:00
|
|
|
)
|
|
|
|
# Enumerate callers manually by their frequency as set in setUpClass.
|
|
|
|
self.calls = enumerate_contacts(self.calls)
|
2021-04-06 11:48:37 +02:00
|
|
|
self.assertSeriesEqual(
|
2021-04-09 16:01:53 +02:00
|
|
|
self.calls["contact_id_manual"],
|
|
|
|
self.calls["contact_id"].astype("category"),
|
|
|
|
check_names=False,
|
|
|
|
check_category_order=False,
|
2021-04-06 11:48:37 +02:00
|
|
|
)
|
2021-04-06 17:12:36 +02:00
|
|
|
|
2021-05-07 15:48:17 +02:00
|
|
|
def test_count_comms_calls(self):
|
2021-04-06 17:12:36 +02:00
|
|
|
self.features = count_comms(self.calls)
|
|
|
|
self.assertIsInstance(self.features, pd.DataFrame)
|
2021-08-18 15:23:30 +02:00
|
|
|
self.assertCountEqual(self.features.columns.to_list(), FEATURES_CALLS)
|
2021-05-07 15:48:17 +02:00
|
|
|
|
|
|
|
def test_count_comms_sms(self):
|
|
|
|
self.features = count_comms(self.sms)
|
|
|
|
self.assertIsInstance(self.features, pd.DataFrame)
|
2021-08-18 15:28:46 +02:00
|
|
|
self.assertCountEqual(self.features.columns.to_list(), FEATURES_SMS)
|
2021-08-18 15:23:30 +02:00
|
|
|
|
|
|
|
def test_calls_sms_features(self):
|
|
|
|
self.features_call_sms = calls_sms_features(self.calls, self.sms)
|
|
|
|
self.assertIsInstance(self.features_call_sms, pd.DataFrame)
|
2021-08-18 15:35:42 +02:00
|
|
|
self.assertCountEqual(
|
2021-08-19 17:32:02 +02:00
|
|
|
self.features_call_sms.columns.to_list(), FEATURES_CALLS_SMS_ALL
|
2021-08-18 15:35:42 +02:00
|
|
|
)
|