def test_load_time(self, mock_pd):
        mock_pd.read_csv.return_value = mock_return = MagicMock()
        mock_return.values = expected_return = np.array([1, 2, 3, 4, 5])
        actual_returned_value = TimeBasedFeatureService.load_time("subjectA")

        self.assertListEqual(expected_return.tolist(),
                             actual_returned_value.tolist())
        mock_pd.read_csv.assert_called_once_with(
            str(TimeBasedFeatureService.get_path_for_time("subjectA")))
    def test_write_cosine(self, mock_np):
        feature_to_write = np.array([1, 2, 3, 4])
        subject_id = "subjectA"
        TimeBasedFeatureService.write_cosine(subject_id, feature_to_write)

        mock_np.savetxt.assert_called_once_with(
            TimeBasedFeatureService.get_path_for_cosine(subject_id),
            feature_to_write,
            fmt='%f')
    def build_from_time(subject_id, valid_epochs):

        circadian_feature = TimeBasedFeatureService.build_circadian_model(subject_id, valid_epochs)
        cosine_feature = TimeBasedFeatureService.build_cosine(valid_epochs)
        time_feature = TimeBasedFeatureService.build_time(valid_epochs)

        TimeBasedFeatureService.write_circadian_model(subject_id, circadian_feature)
        TimeBasedFeatureService.write_cosine(subject_id, cosine_feature)
        TimeBasedFeatureService.write_time(subject_id, time_feature)
    def test_get_cosine_path(self):
        expected_path = Constants.FEATURE_FILE_PATH.joinpath(
            "subjectA" + '_cosine_feature.out')

        self.assertEqual(
            expected_path,
            TimeBasedFeatureService.get_path_for_cosine("subjectA"))
Beispiel #5
0
    def build(subject_id):
        feature_count = ActivityCountFeatureService.load(subject_id)
        feature_hr = HeartRateFeatureService.load(subject_id)
        feature_time = TimeBasedFeatureService.load_time(subject_id)
        #feature_circadian = TimeBasedFeatureService.load_circadian_model(subject_id)
        feature_cosine = TimeBasedFeatureService.load_cosine(subject_id)
        labeled_sleep = PSGLabelService.load(subject_id)

        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=subject_id,
                          labeled_sleep=labeled_sleep,
                          feature_dictionary=feature_dictionary)

        # Uncomment to save plots of every subject's data:
        ax = plt.subplot(5, 1, 1)
        ax.plot(range(len(feature_hr)), feature_hr, label="Heart-Rate")
        ax.legend()
        ax = plt.subplot(5, 1, 2)
        ax.plot(range(len(feature_count)), feature_count, label="Motion")
        ax.legend()
        ax = plt.subplot(5, 1, 3)
        ax.plot(range(len(feature_cosine)),
                feature_cosine,
                label="Clock proxy")
        ax.legend()
        # ax = plt.subplot(5, 1, 4)
        # ax.plot(range(len(feature_circadian)), feature_circadian)
        ax = plt.subplot(5, 1, 4)
        ax.plot(range(len(labeled_sleep)), labeled_sleep, label="Sleep Stages")
        ax.legend()
        #
        plt.savefig(
            str(Constants.FIGURE_FILE_PATH.joinpath(subject_id + '_info.png')))
        plt.close()

        return subject
    def build_from_time(subject_id, valid_epochs):

        if Constants.INCLUDE_CIRCADIAN:
            circadian_feature = TimeBasedFeatureService.build_circadian_model(
                subject_id, valid_epochs)
            TimeBasedFeatureService.write_circadian_model(
                subject_id, circadian_feature)

        cosine_feature = TimeBasedFeatureService.build_cosine(valid_epochs)
        time_feature = TimeBasedFeatureService.build_time(valid_epochs)

        TimeBasedFeatureService.write_cosine(subject_id, cosine_feature)
        TimeBasedFeatureService.write_time(subject_id, time_feature)
    def build(subject_id):
        feature_count = ActivityCountFeatureService.load(subject_id)
        feature_hr = HeartRateFeatureService.load(subject_id)
        feature_time = TimeBasedFeatureService.load_time(subject_id)
        if Constants.INCLUDE_CIRCADIAN:
            feature_circadian = TimeBasedFeatureService.load_circadian_model(
                subject_id)
        else:
            feature_circadian = None
        feature_cosine = TimeBasedFeatureService.load_cosine(subject_id)
        labeled_sleep = PSGLabelService.load(subject_id)

        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=subject_id,
                          labeled_sleep=labeled_sleep,
                          feature_dictionary=feature_dictionary)

        # Uncomment to save plots of every subject's data:
        # 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(subject_id + '_applewatch.png')))
        # plt.close()
        return subject
    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