Exemplo n.º 1
0
 def build(subject_id, valid_epochs):
     psg_array = PSGService.load_cropped_array(subject_id)
     labels = []
     for epoch in valid_epochs:
         value = np.interp(epoch.timestamp, psg_array[:, 0], psg_array[:, 1])
         labels.append(value)
     return np.array(labels)
    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])
Exemplo n.º 3
0
    def crop_all(subject_id):
        # psg_raw_collection = PSGService.read_raw(subject_id)  # Used to extract PSG details from the reports
        psg_raw_collection = PSGService.read_precleaned(
            subject_id)  # Loads already extracted PSG data
        motion_collection = MotionService.load_raw(subject_id)
        heart_rate_collection = HeartRateService.load_raw(subject_id)

        valid_interval = RawDataProcessor.get_intersecting_interval(
            [psg_raw_collection, motion_collection, heart_rate_collection])

        psg_raw_collection = PSGService.crop(psg_raw_collection,
                                             valid_interval)
        motion_collection = MotionService.crop(motion_collection,
                                               valid_interval)
        heart_rate_collection = HeartRateService.crop(heart_rate_collection,
                                                      valid_interval)

        PSGService.write(psg_raw_collection)
        MotionService.write(motion_collection)
        HeartRateService.write(heart_rate_collection)
        ActivityCountService.build_activity_counts_without_matlab(
            subject_id, motion_collection.data
        )  # Builds activity counts with python, not MATLAB
    def test_write(self, mock_np):
        subject_id = 'subjectA'
        mock_np.array.return_value = returned_array = np.array([[1, 2], [3,
                                                                         4]])
        list_data = [[10, 1], [50, 2], [100, 3], [170, 5]]
        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)
        ]
        psg_raw_data_collection = PSGRawDataCollection(subject_id=subject_id,
                                                       data=data)
        psg_output_path = Constants.CROPPED_FILE_PATH.joinpath(
            "subjectA_cleaned_psg.out")

        PSGService.write(psg_raw_data_collection)

        mock_np.array.assert_called_once_with(list_data)
        mock_np.savetxt.assert_called_once_with(psg_output_path,
                                                returned_array,
                                                fmt='%f')
Exemplo n.º 5
0
    def get_valid_epochs(subject_id):

        psg_collection = PSGService.load_cropped(subject_id)
        motion_collection = MotionService.load_cropped(subject_id)
        heart_rate_collection = HeartRateService.load_cropped(subject_id)

        start_time = psg_collection.data[0].epoch.timestamp
        motion_epoch_dictionary = RawDataProcessor.get_valid_epoch_dictionary(
            motion_collection.timestamps, start_time)
        hr_epoch_dictionary = RawDataProcessor.get_valid_epoch_dictionary(
            heart_rate_collection.timestamps, start_time)

        valid_epochs = []
        for stage_item in psg_collection.data:
            epoch = stage_item.epoch

            if epoch.timestamp in motion_epoch_dictionary and epoch.timestamp in hr_epoch_dictionary \
                    and stage_item.stage != SleepStage.unscored:
                valid_epochs.append(epoch)

        return valid_epochs
    def test_load_cropped_array(self, mock_pd):
        subject_id = 'subject100'
        cropped_psg_path = Constants.CROPPED_FILE_PATH.joinpath(
            subject_id + "_cleaned_psg.out")
        mock_pd.read_csv.return_value.values = np.array([[1, 2], [4, 5],
                                                         [10, 1]])

        psg_collection = PSGService.load_cropped(subject_id)

        mock_pd.read_csv.assert_called_once_with(str(cropped_psg_path),
                                                 delimiter=' ')
        TestHelper.assert_models_equal(
            self,
            StageItem(epoch=Epoch(timestamp=1, index=0), stage=SleepStage.n2),
            psg_collection.data[0])
        TestHelper.assert_models_equal(
            self,
            StageItem(epoch=Epoch(timestamp=4, index=1), stage=SleepStage.rem),
            psg_collection.data[1])
        TestHelper.assert_models_equal(
            self,
            StageItem(epoch=Epoch(timestamp=10, index=2), stage=SleepStage.n1),
            psg_collection.data[2])