def test_write(self, mock_cropped_file_path, mock_np): subject_id = 'subjectA' data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) heart_rate_collection = HeartRateCollection(subject_id=subject_id, data=data) mock_cropped_file_path.return_value = file_path = 'path/to/file' HeartRateService.write(heart_rate_collection) mock_np.savetxt.assert_called_once_with(file_path, heart_rate_collection.data, fmt='%f')
def test_get_cropped_file_path(self): subject_id = 'subject1' file_path = HeartRateService.get_cropped_file_path(subject_id) self.assertEqual( Constants.CROPPED_FILE_PATH.joinpath("subject1_cleaned_hr.out"), file_path)
def test_get_raw_file_path(self): subject_id = 'subject1' heart_rate_dir = utils.get_project_root().joinpath('data/heart_rate/') file_path = HeartRateService.get_raw_file_path(subject_id) self.assertEqual(heart_rate_dir.joinpath("subject1_heartrate.txt"), file_path)
def test_load(self, mock_pd): heart_rate_file_path = 'path/to/file' mock_pd.read_csv.return_value.values = expected_data = np.array( [1, 2, 3, 4, 5]) returned_data = HeartRateService.load(heart_rate_file_path) mock_pd.read_csv.assert_called_once_with(heart_rate_file_path, delimiter=' ') self.assertEqual(expected_data.tolist(), returned_data.tolist())
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_load_cropped(self, mock_get_cropped_file_path, mock_load): subject_id = 'subjectB' mock_get_cropped_file_path.return_value = returned_cropped_path = 'path/to/file' mock_load.return_value = data = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) expected_heart_rate_collection = HeartRateCollection( subject_id=subject_id, data=data) returned_heart_rate_collection = HeartRateService.load_cropped( subject_id) TestHelper.assert_models_equal(self, expected_heart_rate_collection, returned_heart_rate_collection) mock_get_cropped_file_path.assert_called_once_with(subject_id) mock_load.assert_called_once_with(returned_cropped_path)
def test_crop(self): subject_id = 'subjectA' data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) heart_rate_collection = HeartRateCollection(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_heart_rate_collection = HeartRateCollection( subject_id=subject_id, data=cropped_data) returned_collection = HeartRateService.crop(heart_rate_collection, interval) TestHelper.assert_models_equal(self, cropped_heart_rate_collection, returned_collection)
def test_load_raw(self, mock_get_raw_file_path, mock_load, mock_remove_repeats): subject_id = 'subjectA' mock_get_raw_file_path.return_value = returned_raw_path = 'path/to/file' mock_load.return_value = data = np.array([[1, 2, 3], [4, 5, 6]]) mock_remove_repeats.return_value = data expected_heart_rate_collection = HeartRateCollection( subject_id=subject_id, data=data) returned_heart_rate_collection = HeartRateService.load_raw(subject_id) TestHelper.assert_models_equal(self, expected_heart_rate_collection, returned_heart_rate_collection) mock_get_raw_file_path.assert_called_once_with(subject_id) mock_load.assert_called_once_with(returned_raw_path, ',') mock_remove_repeats.assert_called_once_with(data)
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 build(subject_id, valid_epochs): heart_rate_collection = HeartRateService.load_cropped(subject_id) return HeartRateFeatureService.build_from_collection( heart_rate_collection, valid_epochs)
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