def load_experiment(data_path, diary_path, start_hour): # Configure an Experiment exp = Experiment() # Iterates over a set of files in a directory. # Unfortunately, we have to do it manually with RawProcessing because we are modifying the annotations print("Running %s" % data_path) for file in glob(data_path): print("FILE", file) pp = RawProcessing(file, cols_for_activity=["stdMET_highIC_Branch"], is_act_count=False, col_for_datetime="REALTIME", strftime="%d-%b-%Y %H:%M:%S", col_for_pid="id", col_for_hr="mean_hr", device_location="dw") pp.data["hyp_act_x"] = pp.data[ "hyp_act_x"] - 1.0 # adjust for the BBVA dataset w = Wearable(pp) # Creates a wearable from a pp object exp.add_wearable(w) # Set frequency for every wearable in the collection exp.set_freq_in_secs(60) # Changing the hour the experiment starts from midnight (0) to 3pm (15) exp.change_start_hour_for_experiment_day(start_hour) diary = Diary().from_file(diary_path) exp.add_diary(diary) return exp
def load_experiment(data_path, diary_path, start_hour): # Configure an Experiment exp = Experiment() # Iterates over a set of files in a directory. # Unfortunately, we have to do it manually with RawProcessing because we are modifying the annotations for file in glob(data_path): pp = RawProcessing( file, # HR information col_for_hr="HR", # Activity information cols_for_activity=["Axis1", "Axis2", "Axis3"], is_act_count=False, # Datetime information col_for_datetime="time", strftime="%Y-%b-%d %H:%M:%S", # Participant information col_for_pid="pid") w = Wearable(pp) # Creates a wearable from a pp object exp.add_wearable(w) # Set frequency for every wearable in the collection # exp.set_freq_in_secs(5) # Changing the hour the experiment starts from midnight (0) to 3pm (15) exp.change_start_hour_for_experiment_day(start_hour) diary = Diary().from_file(diary_path) exp.add_diary(diary) return exp
def setup_experiment(file_path, diary_path, start_hour): # Configure an Experiment exp = Experiment() # Iterates over a set of files in a directory. # Unfortunately, we have to do it manually with RawProcessing because we are modifying the annotations for file in glob(file_path): pp = RawProcessing(file, device_location="dw", # HR information col_for_hr="mean_hr", # Activity information cols_for_activity=["activity"], is_act_count=True, # Datetime information col_for_datetime="linetime", strftime="%Y-%m-%d %H:%M:%S", # Participant information col_for_pid="mesaid") w = Wearable(pp) # Creates a wearable from a pp object # Invert the two_stages flag. Now True means sleeping and False means awake w.data["hyp_annotation"] = (w.data["stages"] > 0) exp.add_wearable(w) exp.set_freq_in_secs(30) w.change_start_hour_for_experiment_day(start_hour) diary = Diary().from_file(diary_path) exp.add_diary(diary) return exp
def load_experiment(data_path, start_hour): # Configure an Experiment exp = Experiment() # Iterates over a set of files in a directory. # Unfortunately, we have to do it manually with RawProcessing because we are modifying the annotations for file in glob(data_path): pp = RawProcessing(file, # HR information col_for_hr="hr", # Activity information cols_for_activity=["x", "y", "z"], is_act_count=True, # Datetime information col_for_datetime="faketime", strftime="%Y-%m-%d %H:%M:%S", # Participant information col_for_pid="pid") w = Wearable(pp) # Creates a wearable from a pp object w.data["hyp_annotation"] = (w.data["label"] > 0) exp.add_wearable(w) # Set frequency for every wearable in the collection exp.set_freq_in_secs(15) # Changing the hour the experiment starts from midnight (0) to 3pm (15) exp.change_start_hour_for_experiment_day(start_hour) return exp
def configure_experiment(self, datapath: str, device_location: str, cols_for_activity, col_for_mets: str = None, is_emno: bool = False, is_act_count: bool = False, col_for_datetime: str = "time", start_of_week: int = -1, strftime: str = None, col_for_pid: str = None, pid: int = -1, additional_data: object = None, col_for_hr: str = None): # TODO: Missing a check to see if datapath exists. if os.path.isdir(datapath): if not datapath.endswith("*"): datapath = os.path.join(datapath, "*") else: if '*' not in datapath: datapath = datapath + "*" for file in glob(datapath): pp = RawProcessing(file, device_location=device_location, # activitiy information cols_for_activity=cols_for_activity, is_act_count=is_act_count, is_emno=is_emno, # Datatime information col_for_datetime=col_for_datetime, col_for_mets=col_for_mets, start_of_week=start_of_week, strftime=strftime, # Participant information col_for_pid=col_for_pid, pid=pid, additional_data=additional_data, # HR information col_for_hr=col_for_hr, ) w = Wearable(pp) self.wearables[w.get_pid()] = w
def setUp(self): # Loads some file pp1 = RawProcessing() pp1.load_file("../data/examples_mesa/mesa-sample-day5-invalid5hours.csv", # activitiy information cols_for_activity=["activity"], is_act_count=True, # Datatime information col_for_datatime="linetime", device_location="dw", start_of_week="dayofweek", # Participant information col_for_pid="mesaid") self.w_5day_invalid5hours = Wearable(pp1)
def setup_experiment(preprocessor, file_path, start_hour): # Configure an Experiment exp = Experiment() # Iterates over a set of files in a directory. # Unfortunately, we have to do it manually with RawProcessing because we are modifying the annotations for file in tqdm(glob(file_path)): pp = preprocessor(file) w = Wearable(pp) # Creates a wearable from a pp object # Invert the two_stages flag. Now True means sleeping and False means awake w.data["interval_sleep"] = w.data["interval"].isin(["REST-S", "REST"]) w.data["sleep"] = ~w.data["wake"].astype(bool) exp.add_wearable(w) exp.set_freq_in_secs(30) w.change_start_hour_for_experiment_day(start_hour) return exp
def configure_experiment(self, datapath: str, device_location: str, cols_for_activity, col_for_mets: str = None, is_emno: bool = False, is_act_count: bool = False, col_for_datetime: str = "time", start_of_week: int = -1, strftime: str = None, col_for_pid: str = None, pid: int = -1, additional_data: object = None, col_for_hr: str = None): """ Parameters ---------- datapath : str DESCRIPTION. Path to the dat folder device_location : str, optional DESCRIPTION. The default is None. Where this device was located (options are: "bw", "hip", "dw", "ndw", "chest", "hp_ch", "hp_bw", "all") # Configuration for activity cols_for_activity : list / str DESCRIPTION. Which columns record activity col_for_mets : object, optional DESCRIPTION. Column that records METs. is_emno : bool, optional DESCRIPTION. True if the cols_for_activity are already computed as the ENMO (Euclidean Norm Minus One) is_act_count : bool, optional DESCRIPTION. The default is False. True us cols_for_activity are already computed as counts # Datetime parameters col_for_datetime : str, optional DESCRIPTION. The default is "time". Name of timestamp column. start_of_week : int, optional DESCRIPTION. The default is -1. Integer that represents the day at the start of the week strftime : str, optional DESCRIPTION. The default is None. Format to parse col_for_datetime # PID parameters col_for_pid : str, optional DESCRIPTION. The default is None. Participant ID columns pid : int, optional DESCRIPTION. The default is -1. # HR parameters col_for_hr : str, optional DESCRIPTION. The default is None. Column with heart rate data # Any additional data? additional_data : object, optional DESCRIPTION. The default is None. Returns ------- None. """ # TODO: Missing a check to see if datapath exists. if os.path.isdir(datapath): if not datapath.endswith("*"): datapath = os.path.join(datapath, "*") else: if '*' not in datapath: datapath = datapath + "*" for file in glob(datapath): pp = RawProcessing( file, device_location=device_location, # activitiy information cols_for_activity=cols_for_activity, is_act_count=is_act_count, is_emno=is_emno, # Datatime information col_for_datetime=col_for_datetime, col_for_mets=col_for_mets, start_of_week=start_of_week, strftime=strftime, # Participant information col_for_pid=col_for_pid, pid=pid, additional_data=additional_data, # HR information col_for_hr=col_for_hr, ) w = Wearable(pp) self.wearables[w.get_pid()] = w
# Title: Dont forget to add a title! from hypnospy import Wearable from hypnospy.data import MESAPreProcessing from hypnospy.analysis import Viewer, NonWearingDetector, SleepWakeAnalysis #from tf.keras.preprocessing import timeseries_dataset_from_array import pandas as pd import numpy as np import matplotlib.pyplot as plt # MESAPreProcessing is a specialized class to preprocess csv files from Philips Actiwatch Spectrum devices used in the MESA Sleep experiment # MESA Sleep dataset can be found here: https://sleepdata.org/datasets/mesa/ preprocessed = MESAPreProcessing("../data/examples_mesa/mesa-sample.csv") # Wearable is the main object in HypnosPy. w = Wearable(preprocessed) # In HypnosPy, we have the concept of ``experiment day'' which by default starts at midnight (00 hours). # We can easily change it to any other time we wish. For example, lets run this script with experiment days # that start at 3pm (15h) w.change_start_hour_for_experiment_day(15) # Sleep Wake Analysis module sw = SleepWakeAnalysis(w) sw.run_sleep_algorithm( "ScrippsClinic", inplace=True) # runs alg and creates new col named 'ScrippsClinic' sw.run_sleep_algorithm( "Cole-Kripke", inplace=True) # runs alg and creates new col named 'Cole-Kripke' sw.run_sleep_algorithm(
import matplotlib.pyplot as plt pp = RawProcessing( "./data/shiftworker/Shiftworker.csv", # HR information col_for_hr="mean_hr", # Activity information cols_for_activity=["ACC"], is_act_count=True, # Datetime information col_for_datetime="real_time", strftime="%d/%m/%Y %H:%M", # Participant information col_for_pid="id") w = Wearable(pp) #Check out data columns #print(w.data.head(10)) #Define parameters fo HR-based sleep algorithm hr_quantile = 0.4 hr_min_window_length = 60 hr_merge_blocks = 180 hr_volarity = 5 #Time to consider as start and end of each experiment day - if equal the sleep labelling occurs #over the entire 24 hours start_hour = 18 end_hour = 18
from hypnospy import Experiment if __name__ == "__main__": # Configure an Experiment exp = Experiment() file_path = "./data/small_collection_hchs/*" # Iterates over a set of files in a directory. # Unfortunately, we have to do it manually with RawProcessing because we are modifying the annotations for file in glob(file_path): pp = ActiwatchSleepData(file, col_for_datetime="time", col_for_pid="pid") w = Wearable(pp) # Creates a wearable from a pp object exp.add_wearable(w) tsp = TimeSeriesProcessing(exp) tsp.fill_no_activity(-0.0001) tsp.detect_non_wear(strategy="choi") tsp.check_consecutive_days(5) print("Valid days:", tsp.get_valid_days()) print("Invalid days:", tsp.get_invalid_days()) tsp.detect_sleep_boundaries(strategy="annotation", annotation_hour_to_start_search=18) tsp.invalidate_day_if_no_sleep() print("Valid days:", tsp.get_valid_days())