def test_crop(self):
        subject_id = 'subjectA'
        crop_interval = Interval(start_time=50, end_time=101)
        data = [
            StageItem(epoch=Epoch(timestamp=10, index=2), stage=SleepStage.n1),
            StageItem(epoch=Epoch(timestamp=50, index=3), stage=SleepStage.n2),
            StageItem(epoch=Epoch(timestamp=100, index=4),
                      stage=SleepStage.n3),
            StageItem(epoch=Epoch(timestamp=170, index=5),
                      stage=SleepStage.rem)
        ]
        expected_data = [
            StageItem(epoch=Epoch(timestamp=50, index=3), stage=SleepStage.n2),
            StageItem(epoch=Epoch(timestamp=100, index=4),
                      stage=SleepStage.n3),
        ]

        input_raw_data_collection = PSGRawDataCollection(subject_id=subject_id,
                                                         data=data)

        returned_psg_raw_collection = PSGService.crop(
            input_raw_data_collection, crop_interval)

        self.assertEqual(subject_id, returned_psg_raw_collection.subject_id)
        TestHelper.assert_models_equal(self, expected_data[0],
                                       returned_psg_raw_collection.data[0])
        TestHelper.assert_models_equal(self, expected_data[1],
                                       returned_psg_raw_collection.data[1])
Exemple #2
0
    def get_intersecting_interval(collection_list):
        start_times = []
        end_times = []
        for collection in collection_list:
            interval = collection.get_interval()
            start_times.append(interval.start_time)
            end_times.append(interval.end_time)

        return Interval(start_time=max(start_times), end_time=min(end_times))
    def test_crop(self):
        subject_id = 'subjectA'
        data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
        motion_collection = MotionCollection(subject_id=subject_id, data=data)
        interval = Interval(start_time=4, end_time=9)
        cropped_data = np.array([[4, 5, 6], [7, 8, 9]])
        cropped_motion_collection = MotionCollection(subject_id=subject_id, data=cropped_data)

        returned_collection = MotionService.crop(motion_collection, interval)

        TestHelper.assert_models_equal(self, cropped_motion_collection, returned_collection)
    def test_constructor_and_get_functions(self):
        subject_id = "subject9000"
        data = [
            StageItem(Epoch(timestamp=2, index=2), stage=SleepStage.rem),
            StageItem(Epoch(timestamp=200, index=4), stage=SleepStage.n1)
        ]
        expected_interval = Interval(start_time=2, end_time=200)
        expected_np_array = np.array([[2, 5], [200, 1]])

        psg_raw_data_collection = PSGRawDataCollection(subject_id=subject_id,
                                                       data=data)

        self.assertListEqual(data, psg_raw_data_collection.data)
        self.assertEqual(subject_id, psg_raw_data_collection.subject_id)

        TestHelper.assert_models_equal(self, expected_interval,
                                       psg_raw_data_collection.get_interval())
        self.assertListEqual(expected_np_array.tolist(),
                             psg_raw_data_collection.get_np_array().tolist())
 def get_interval(self):
     return Interval(start_time=np.amin(self.data[:, 0]),
                     end_time=np.amax(self.data[:, 0]))
 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)
Exemple #7
0
 def test_get_interval(self):
     heart_rate_collection = HeartRateCollection(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, heart_rate_collection.get_interval().start_time)
     self.assertEqual(interval.end_time, heart_rate_collection.get_interval().end_time)
    def build(file_id):
        if Constants.VERBOSE:
            print('Building MESA subject ' + file_id + '...')

        raw_labeled_sleep = MesaPSGService.load_raw(file_id)
        heart_rate_collection = MesaHeartRateService.load_raw(file_id)
        activity_count_collection = MesaActigraphyService.load_raw(file_id)
        circadian_model = MesaTimeBasedService.load_circadian_model(file_id)

        if activity_count_collection.data[0][0] != -1 and circadian_model is not None:

            circadian_model = utils.remove_nans(circadian_model)

            interval = Interval(start_time=0, end_time=np.shape(raw_labeled_sleep)[0])

            activity_count_collection = ActivityCountService.crop(activity_count_collection, interval)
            heart_rate_collection = HeartRateService.crop(heart_rate_collection, interval)

            valid_epochs = []

            for timestamp in range(interval.start_time, interval.end_time, Epoch.DURATION):
                epoch = Epoch(timestamp=timestamp, index=len(valid_epochs))
                activity_count_indices = ActivityCountFeatureService.get_window(activity_count_collection.timestamps,
                                                                                epoch)
                heart_rate_indices = HeartRateFeatureService.get_window(heart_rate_collection.timestamps, epoch)

                if len(activity_count_indices) > 0 and 0 not in heart_rate_collection.values[heart_rate_indices]:
                    valid_epochs.append(epoch)
                else:
                    pass

            labeled_sleep = np.expand_dims(
                MesaPSGService.crop(psg_labels=raw_labeled_sleep, valid_epochs=valid_epochs),
                axis=1)

            feature_count = ActivityCountFeatureService.build_from_collection(activity_count_collection,
                                                                              valid_epochs)
            feature_hr = HeartRateFeatureService.build_from_collection(heart_rate_collection, valid_epochs)
            feature_time = np.expand_dims(TimeBasedFeatureService.build_time(valid_epochs), axis=1)
            feature_cosine = np.expand_dims(TimeBasedFeatureService.build_cosine(valid_epochs), axis=1)

            feature_circadian = TimeBasedFeatureService.build_circadian_model_from_raw(circadian_model,
                                                                                       valid_epochs)
            feature_dictionary = {FeatureType.count: feature_count,
                                  FeatureType.heart_rate: feature_hr,
                                  FeatureType.time: feature_time,
                                  FeatureType.circadian_model: feature_circadian,
                                  FeatureType.cosine: feature_cosine}

            subject = Subject(subject_id=file_id,
                              labeled_sleep=labeled_sleep,
                              feature_dictionary=feature_dictionary)

            # Uncomment to save files for all subjects
            # ax = plt.subplot(5, 1, 1)
            # ax.plot(range(len(feature_hr)), feature_hr)
            # ax = plt.subplot(5, 1, 2)
            # ax.plot(range(len(feature_count)), feature_count)
            # ax = plt.subplot(5, 1, 3)
            # ax.plot(range(len(feature_cosine)), feature_cosine)
            # ax = plt.subplot(5, 1, 4)
            # ax.plot(range(len(feature_circadian)), feature_circadian)
            # ax = plt.subplot(5, 1, 5)
            # ax.plot(range(len(labeled_sleep)), labeled_sleep)
            #
            # plt.savefig(str(Constants.FIGURE_FILE_PATH.joinpath(file_id + '.png')))
            # plt.close()

            return subject