def test_properties(self):
        subject_id = "subjectA"
        activity_count_collection = ActivityCountCollection(subject_id=subject_id,
                                                            data=np.array([[1, 2, 3], [4, 5, 6]]))

        self.assertEqual(subject_id, activity_count_collection.subject_id)
        self.assertEqual(np.array([1, 4]).tolist(), activity_count_collection.timestamps.tolist())
        self.assertEqual(np.array([[2, 3], [5, 6]]).tolist(), activity_count_collection.values.tolist())
    def crop(activity_count_collection, interval):
        subject_id = activity_count_collection.subject_id
        timestamps = activity_count_collection.timestamps
        valid_indices = ((timestamps >= interval.start_time)
                         & (timestamps < interval.end_time)).nonzero()[0]

        cropped_data = activity_count_collection.data[valid_indices, :]
        return ActivityCountCollection(subject_id=subject_id,
                                       data=cropped_data)
Example #3
0
    def test_interpolate(self):
        subject_id = "subjectA"
        data = np.array([[1, 0], [10, 9]])
        activity_count_collection = ActivityCountCollection(
            subject_id=subject_id, data=data)

        interpolated_timestamps, interpolated_counts = ActivityCountFeatureService.interpolate(
            activity_count_collection)

        self.assertListEqual([0, 1, 2, 3, 4, 5, 6, 7, 8],
                             interpolated_counts.tolist())
        self.assertListEqual([1, 2, 3, 4, 5, 6, 7, 8, 9],
                             interpolated_timestamps.tolist())
 def test_load_cropped(self, mock_cropped_file_path, mock_load):
     subject_id = 'subjectA'
     mock_cropped_file_path.return_value = path = 'path/to/file'
     data = np.array([[1, 2, 3], [4, 5, 6]])
     expected_activity_count_collection = ActivityCountCollection(
         subject_id=subject_id, data=data)
     mock_load.return_value = data
     returned_activity_count_collection = ActivityCountService.load_cropped(
         subject_id)
     mock_cropped_file_path.assert_called_once_with(subject_id)
     mock_load.assert_called_once_with(path)
     TestHelper.assert_models_equal(self,
                                    expected_activity_count_collection,
                                    returned_activity_count_collection)
Example #5
0
    def load_raw(file_id):
        line_align = -1  # Find alignment line between PSG and actigraphy
        project_root = str(utils.get_project_root())

        with open(project_root +
                  '/data/mesa/overlap/mesa-actigraphy-psg-overlap.csv'
                  ) as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            next(csv_file)
            for row in csv_reader:
                if int(row[0]) == int(file_id):
                    line_align = int(row[1])

        activity = []
        elapsed_time_counter = 0

        if line_align == -1:  # If there was no alignment found
            return ActivityCountCollection(subject_id=file_id,
                                           data=np.array([[-1], [-1]]))

        with open(project_root + '/data/mesa/actigraphy/mesa-sleep-' +
                  file_id + '.csv') as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            next(csv_file)
            for row in csv_reader:
                if int(row[1]) >= line_align:
                    if row[4] == '':
                        activity.append([elapsed_time_counter, np.nan])
                    else:
                        activity.append([elapsed_time_counter, float(row[4])])
                    elapsed_time_counter = elapsed_time_counter + 30

        data = np.array(activity)
        data = utils.remove_nans(data)

        return ActivityCountCollection(subject_id=file_id, data=data)
Example #6
0
    def test_build_feature_array(self, mock_load_cropped, mock_get_feature):
        subject_id = "subjectA"
        data = np.array([[1, 10], [10, 220], [20, 0], [40, 500], [70, 200],
                         [90, 0], [100, 0], [120, 4]])
        activity_count_collection = ActivityCountCollection(
            subject_id=subject_id, data=data)
        mock_load_cropped.return_value = activity_count_collection
        expected_features = [np.array([0.1]), np.array([0.2])]
        mock_get_feature.side_effect = expected_features
        expected_feature_array = np.array(expected_features)

        valid_epochs = [
            Epoch(timestamp=4, index=1),
            Epoch(timestamp=50, index=2)
        ]

        returned_feature_array = ActivityCountFeatureService.build(
            subject_id, valid_epochs)

        self.assertEqual(expected_feature_array.tolist(),
                         returned_feature_array.tolist())
    def test_build(self, mock_psg_service_load_raw, mock_heart_rate_service,
                   mock_actigraphy_service, mock_time_service):
        file_id = 'subjectA'

        returned_sleep_data = []
        for i in range(30):
            returned_sleep_data.append(0)
        for i in range(90):
            returned_sleep_data.append(1)
        for i in range(90):
            returned_sleep_data.append(3)
        for i in range(30):
            returned_sleep_data.append(2)

        heart_rate_data = np.array([[0, 20], [4, 60], [25, 95], [35, 333],
                                    [55, 25], [100, 233], [190, 24]])

        actigraphy_data = np.array([[0, 1], [15, 2], [30, 3], [45, 4], [60, 5],
                                    [75, 6], [90, 7]])

        circadian_data = np.array([[0, 0.3], [5, 0.4], [10, 0.1], [15, 0.6],
                                   [20, 0.7], [25, 0.2], [30, 0.1], [35, 0.3],
                                   [40, 0.4], [45, 0.1], [50, 0.6], [55, 0.7],
                                   [60, 0.2], [65, 0.1]])

        mock_psg_service_load_raw.return_value = np.array(returned_sleep_data)
        mock_heart_rate_service.load_raw.return_value = HeartRateCollection(
            subject_id=file_id, data=heart_rate_data)
        mock_actigraphy_service.load_raw.return_value = ActivityCountCollection(
            subject_id=file_id, data=actigraphy_data)
        mock_time_service.load_circadian_model.return_value = circadian_data

        subject = MesaSubjectBuilder.build(file_id)

        self.assertEqual([[0], [1], [1], [1], [3], [3], [3], [2]],
                         subject.labeled_sleep.tolist())
 def test_get_interval(self):
     activity_count_collection = ActivityCountCollection(subject_id="subjectA",
                                                         data=np.array([[1, 2, 3], [4, 5, 6]]))
     interval = Interval(start_time=1, end_time=4)
     self.assertEqual(interval.start_time, activity_count_collection.get_interval().start_time)
     self.assertEqual(interval.end_time, activity_count_collection.get_interval().end_time)
 def load_cropped(subject_id):
     activity_counts_path = ActivityCountService.get_cropped_file_path(
         subject_id)
     counts_array = ActivityCountService.load(activity_counts_path)
     return ActivityCountCollection(subject_id=subject_id,
                                    data=counts_array)