Skip to content

Commit 78a4b11

Browse files
authored
Merge pull request #141 from BIDMCDigitalPsychiatry/lucy_new_feature
Lucy new feature
2 parents 210b72f + 696769e commit 78a4b11

File tree

7 files changed

+97
-76
lines changed

7 files changed

+97
-76
lines changed

cortex/raw/nearby_device.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
""" Module for raw feature nearby_device """
2+
from cortex.feature_types import raw_feature
3+
4+
@raw_feature(
5+
name="lamp.nearby_device",
6+
dependencies=["lamp.nearby_device"]
7+
)
8+
def nearby_device(_limit=10000,
9+
cache=False,
10+
recursive=False,
11+
**kwargs):
12+
""" Get all nearby_device data bounded by the time interval.
13+
14+
Args:
15+
_limit (int): The maximum number of sensor events to query for in a single request
16+
cache (bool): Indicates whether to save raw data locally in cache dir
17+
recursive (bool): if True, continue requesting data until all data is
18+
returned; else just one request
19+
20+
Returns:
21+
timestamp (int): The UTC timestamp for the Bluetooth event.
22+
bt_rssi (int): The rssi for the Bluetooth event.
23+
bt_name (str): The name of the Bluetooth device.
24+
bt_address (str): Address of Bluetooth event.
25+
26+
Example:
27+
[{'timestamp': 1617990580971,
28+
'bt_rssi': -97,
29+
'bt_name': '[TV] Samsung 7 Series (65)',
30+
'bt_address': '873C0B2B-6B43-6DBE-8ECC-369D59767ADF'},
31+
{'timestamp': 1617990285117,
32+
'bt_rssi': -100,
33+
'bt_name': '[TV] Samsung Q900 Series (65)',
34+
'bt_address': '049AD0D2-9CB1-E132-6347-0BDFCED3E8B8'},]
35+
"""
36+
return

cortex/raw/wifi.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

cortex/secondary/bluetooth_device_count.py

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
""" Module to compute nearby device count from raw feature nearby_device """
2+
import numpy as np
3+
import pandas as pd
4+
5+
from cortex.feature_types import secondary_feature
6+
from cortex.raw.nearby_device import nearby_device
7+
8+
MS_IN_A_DAY = 86400000
9+
@secondary_feature(
10+
name='cortex.feature.nearby_device_count',
11+
dependencies=[nearby_device]
12+
)
13+
def nearby_device_count(**kwargs):
14+
"""Number of unique nearby devices.
15+
16+
Args:
17+
**kwargs:
18+
id (string): The participant's LAMP id. Required.
19+
start (int): The initial UNIX timestamp (in ms) of the window for which the feature
20+
is being generated. Required.
21+
end (int): The last UNIX timestamp (in ms) of the window for which the feature
22+
is being generated. Required.
23+
resolution (int): The resolution (in ms) over which to comptute features.
24+
25+
Returns:
26+
A dict consisting:
27+
timestamp (int): The beginning of the window (same as kwargs['start']).
28+
value (float): Number of unique bluetooth devices that were nearby.
29+
"""
30+
_nearby_device = pd.DataFrame(nearby_device(**kwargs)['data'])
31+
_nearby_device_count = None
32+
if len(_nearby_device) > 0:
33+
bluetooth_devices = _nearby_device[_nearby_device['type'] == 'bluetooth']
34+
_nearby_device_count = len(np.unique(bluetooth_devices['address'], return_counts=False))
35+
36+
return {'timestamp': kwargs['start'], 'value': _nearby_device_count}

run_pylint.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ echo "${green} jewels_a.py ${reset}"
5959
pylint cortex/raw/jewels_a.py
6060
echo "${green} jewels_b.py ${reset}"
6161
pylint cortex/raw/jewels_b.py
62+
echo "${green} nearby_device.py ${reset}"
63+
pylint cortex/raw/nearby_device.py
6264
echo "${green} pop_the_bubbles.py ${reset}"
6365
pylint cortex/raw/pop_the_bubbles.py
6466
echo "${green} screen_state.py ${reset}"
@@ -75,8 +77,6 @@ echo "${green} survey.py ${reset}"
7577
pylint cortex/raw/survey.py
7678
echo "${green} telephony.py ${reset}"
7779
pylint cortex/raw/telephony.py
78-
echo "${green} wifi.py ${reset}"
79-
pylint cortex/raw/wifi.py
8080

8181
echo "${red}----------------------------------${reset}"
8282
echo "${red} Running pylint for primary features ${reset}"
@@ -95,8 +95,6 @@ pylint cortex/primary/trips.py
9595

9696
echo "${red}----------------------------------${reset}"
9797
echo "${red} Running pylint for secondary features ${reset}"
98-
echo "${green} bluetooth_device_count.py ${reset}"
99-
pylint cortex/secondary/bluetooth_device_count.py
10098
echo "${green} call_degree.py ${reset}"
10199
pylint cortex/secondary/call_degree.py
102100
echo "${green} call_duration.py ${reset}"
@@ -115,6 +113,8 @@ echo "${green} hometime.py ${reset}"
115113
pylint cortex/secondary/hometime.py
116114
echo "${green} inactive_duration.py ${reset}"
117115
pylint cortex/secondary/inactive_duration.py
116+
echo "${green} nearby_device_count.py ${reset}"
117+
pylint cortex/secondary/nearby_device_count.py
118118
echo "${green} screen_duration.py ${reset}"
119119
pylint cortex/secondary/screen_duration.py
120120
echo "${green} step_count.py ${reset}"

run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ find tests -name '*tests.py' -print0 |
1818
echo "${green} Running tests for ${line} ${reset}"
1919
coverage run -m unittest "$line"
2020
echo "${cyan} Coverage Report for ${line}"
21-
coverage report -m cortex/secondary/bluetooth_device_count.py
21+
coverage report -m cortex/secondary/nearby_device_count.py
2222
echo "${cyan} Coverage Report for ${line}"
2323
coverage report -m cortex/secondary/data_quality.py
2424
echo "${cyan} Coverage Report for ${line}"

tests/secondary_feature_tests.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class TestSecondary(unittest.TestCase):
2727
TEST_START_TIME_JERK = 1584137124130
2828
TEST_START_TIME_STEPS = 1651161331270 - 5 * MS_IN_DAY
2929
TEST_PARTICIPANT_CALLS = "U7955172051"
30+
TEST_PARTICIPANT_NEARBY_DEVICES = "U1753020007"
31+
NEARBY_TEST_START = 1646485947205
32+
NEARBY_TEST_END = 1647003793838
3033
CALLS_TEST_START = 1654781897000 + 10 * MS_IN_DAY
3134
CALLS_TEST_END = 1654781897001 + 11 * MS_IN_DAY
3235

@@ -35,16 +38,25 @@ def setUp(self):
3538
logger = logging.getLogger()
3639
logger.setLevel(logging.CRITICAL)
3740

38-
# 0. bluetooth_device_count
41+
# 0. nearby_device_count
3942
def test_device_count_no_data(self):
4043
# Test if the participant has no data
41-
ret0 = secondary.bluetooth_device_count.bluetooth_device_count(id=self.EMPTY_PARTICIPANT,
44+
ret0 = secondary.nearby_device_count.nearby_device_count(id=self.EMPTY_PARTICIPANT,
4245
start=self.TEST_END_TIME - 3 * self.MS_IN_DAY,
4346
end=self.TEST_END_TIME,
4447
resolution=self.MS_IN_DAY)
4548
for x in ret0['data']:
4649
self.assertEqual(x['value'], None)
4750

51+
def test_device_count_data(self):
52+
# Test that nearby device count works
53+
ret0 = secondary.nearby_device_count.nearby_device_count(id=self.TEST_PARTICIPANT_NEARBY_DEVICES,
54+
start=self.NEARBY_TEST_START,
55+
end=self.NEARBY_TEST_END,
56+
resolution=self.MS_IN_DAY)
57+
self.assertEqual(ret0['data'][0]['value'], 3)
58+
self.assertEqual(ret0['data'][1]['value'], None)
59+
4860
# 1. data_quality
4961
def test_data_quality_no_data(self):
5062
# Test if the participant has no data
@@ -151,8 +163,8 @@ def test_call_duration(self):
151163
local_ret = cortex.secondary.call_duration.call_duration(
152164
incoming=option,
153165
id=self.TEST_PARTICIPANT_CALLS,
154-
start=self.CALLS_TEST_START - self.MS_IN_DAY,
155-
end=self.CALLS_TEST_END - self.MS_IN_DAY,
166+
start=self.CALLS_TEST_START,
167+
end=self.CALLS_TEST_END,
156168
resolution=self.MS_IN_DAY,
157169
feature="telephony")['data'][0]['value']
158170
rets_incoming.append(local_ret)
@@ -163,7 +175,7 @@ def test_call_duration(self):
163175
self.assertEqual(rets[3], None)
164176
self.assertEqual(ret_none, None)
165177
self.assertEqual(rets_incoming[0], 34)
166-
self.assertEqual(rets_incoming[0], 24)
178+
self.assertEqual(rets_incoming[1], 24)
167179

168180
def test_call_number(self):
169181
# Test that call number works
@@ -198,8 +210,8 @@ def test_call_number(self):
198210
local_ret = cortex.secondary.call_number.call_number(
199211
incoming=option,
200212
id=self.TEST_PARTICIPANT_CALLS,
201-
start=self.CALLS_TEST_START - self.MS_IN_DAY,
202-
end=self.CALLS_TEST_END - self.MS_IN_DAY,
213+
start=self.CALLS_TEST_START,
214+
end=self.CALLS_TEST_END,
203215
resolution=self.MS_IN_DAY,
204216
feature="telephony")['data'][0]['value']
205217
rets_incoming.append(local_ret)
@@ -210,7 +222,7 @@ def test_call_number(self):
210222
self.assertEqual(rets[3], None)
211223
self.assertEqual(ret_none, None)
212224
self.assertEqual(rets_incoming[0], 1)
213-
self.assertEqual(rets_incoming[0], 1)
225+
self.assertEqual(rets_incoming[1], 1)
214226

215227
if __name__ == '__main__':
216228
unittest.main()

0 commit comments

Comments
 (0)