#!/usr/bin/env python # coding: utf-8 # # Start guide for the devicely package # To install devicely locally just run `pip install devicely` # # Use devicely # In[43]: import os import devicely write_dir = 'New' if not os.path.isdir(write_dir): os.makedirs(write_dir) # ## Empatica E4 # # The Empatice E4 wristband can be used to obtain data from inter-beat intervals, electrodermal activity, heart rate, temperature and blood volume pulse. The wristband uses [this directory structure](https://github.com/hpi-dhc/devicely-example/tree/main/Empatica) for its measurement data. # ### Read the data # # Create an EmpaticaReader object: # In[44]: empatica_reader = devicely.EmpaticaReader('Empatica') # Access the sampling frequencies and starting times for all signals: # In[45]: empatica_reader.start_times # In[46]: empatica_reader.sample_freqs # Access the individual dataframes via the attributes ACC, BVP, EDA, HR, TEMP, IBI and tags: # In[47]: empatica_reader.HR.head() # Access a joined dataframe of all signals: # In[48]: empatica_reader.data.head() # The dataframe contains nan values because the individual signals have different sampling frequencies. # # # ### Timeshift the data # # Apply a timeshift: # In[49]: empatica_reader.timeshift() empatica_reader.start_times # By providing no parameter to `timeshift` the data is shifted by a random time interval between one month and two years to the past. You can also provide a `pandas.Timedelta` object to shift the data by that timedelta or a `pandas.Timestamp` object to shift the data such that this timestamp is the earliest entry. # ### Write the data # In[50]: empatica_write_path = os.path.join(write_dir, 'Empatica') empatica_reader.write(empatica_write_path) os.listdir(empatica_write_path) # ## Spacelabs Blood Pressure Monitor # # Spacelabs uses [a single file](https://github.com/hpi-dhc/devicely-example/tree/main/Spacelabs) to output its metadata as well as the actual signals. # ### Read the data # # Create a `SpacelabsReader` object: # In[51]: spacelabs_reader = devicely.SpacelabsReader(os.path.join('Spacelabs', 'spacelabs.abp')) # Acess the metadata: # In[52]: spacelabs_reader.subject # In[53]: spacelabs_reader.metadata # Access the signal dataframe: # In[54]: spacelabs_reader.data.head() # ### Timeshift the data # # Apply a timeshift: # In[55]: spacelabs_reader.timeshift() spacelabs_reader.data.head() # By providing no parameter to `timeshift` the data is shifted by a random time interval between one month and two years to the past. You can also provide a `pandas.Timedelta` object to shift the data by that timedelta or a `pandas.Timestamp` object to shift the data such that this timestamp is the earliest entry. # ### Remove metadata # In[56]: spacelabs_reader.deidentify('001') spacelabs_reader.metadata # ### Write the data # In[57]: spacelabs_write_path = os.path.join(write_dir, 'Spacelabs') if not os.path.isdir(spacelabs_write_path): os.makedirs(spacelabs_write_path) spacelabs_reader.write(os.path.join(spacelabs_write_path, 'spacelabs.abp')) os.listdir(spacelabs_write_path) # ## Bittium Faros # # The Faros device outpus data in [EDF files](https://www.edfplus.info/specs/edf.html). These are specifically made for health sensor data and not human-readable. # ### Read the data # In[58]: faros_reader = devicely.FarosReader(os.path.join('Faros', 'faros.EDF')) # Access metadata: # In[59]: faros_reader.start_time # In[60]: faros_reader.sample_freqs # In[61]: faros_reader.units # You can access the individual signals via the `ECG`, `ACC`, `HRV` and `Marker` attributes: # In[62]: faros_reader.ACC.head() # Join the dataframes: # In[63]: faros_reader.join_dataframes() faros_reader.data.head() # ### Timeshift the data # # Apply a timeshift: # In[64]: faros_reader.timeshift() faros_reader.data.head() # By providing no parameter to `timeshift` the data is shifted by a random time interval between one month and two years to the past. You can also provide a `pandas.Timedelta` object to shift the data by that timedelta or a `pandas.Timestamp` object to shift the data such that this timestamp is the earliest entry. # ### Write the data # # You can write back the data in the original EDF format or to a directory of individual signal files. Writing to a directory is the preferred method. You can find out why this is the case in our [module reference](https://hpi-dhc.github.io/devicely/moduleref.html#module-devicely.faros). # In[65]: faros_write_path = os.path.join(write_dir, 'Faros') faros_reader.write(faros_write_path) os.listdir(faros_write_path) # You can also create a FarosReader from a written directory: # In[66]: new_faros_reader = devicely.FarosReader(faros_write_path) new_faros_reader.ECG.head() # You can also save files as EDF if the necessary metadata are still present. # In[67]: faros_write_new_path = os.path.join(write_dir, 'Faros', 'faros.edf') faros_reader.write(faros_write_new_path, file_format='edf') os.listdir(faros_write_path) # ## Biovotion Everion # # The Everion device outputs data in [multiple csv files](https://github.com/hpi-dhc/devicely-example/tree/main/Everion). Each csv file has a `tag` column which specifies the type of measurement. You can see the different tags and what they mean by looking at `EverionReader.SIGNAL_TAGS`, `EverionReader.SENSOR_TAGS` and `EverionReader.FEATURE_TAGS`. # In[68]: devicely.EverionReader.FEATURE_TAGS # ### Read the data # In[69]: everion_reader = devicely.EverionReader('Everion') # If you would like to specify which tags to keep, you can specify this when initializing the reader. # # Access the individual dataframes via aggregates, analytics_events, attributes_dailys, everion_events, features, sensors and signals attributes: # In[70]: everion_reader.signals.head() # Access a joined dataframe of all signals: # In[71]: everion_reader.data.head() # ### Timeshift the data # # Apply a timeshift: # In[72]: everion_reader.timeshift() everion_reader.data.head() # By providing no parameter to `timeshift` the data is shifted by a random time interval between one month and two years to the past. You can also provide a `pandas.Timedelta` object to shift the data by that timedelta or a `pandas.Timestamp` object to shift the data such that this timestamp is the earliest entry. # ### Write the data # # Write the data to a directory while keeping the same format as the original. If you used only a subset of tags when initializing the reader, only these tags will be written. # In[73]: everion_write_path = os.path.join(write_dir, 'Everion') everion_reader.write(everion_write_path) os.listdir(everion_write_path) # ## Shimmer # # Shimmer uses a [single CSV file](https://github.com/hpi-dhc/devicely-example/tree/main/Shimmer), indexed by time of measurement. # ### Read the data # In[74]: shimmer_reader = devicely.ShimmerPlusReader(os.path.join('Shimmer', 'shimmer.csv')) shimmer_reader.data.head() # ### Timeshift the data # # Apply a timeshift: # In[75]: shimmer_reader.timeshift() shimmer_reader.data.head() # By providing no parameter to `timeshift` the data is shifted by a random time interval between one month and two years to the past. You can also provide a `pandas.Timedelta` object to shift the data by that timedelta or a `pandas.Timestamp` object to shift the data such that this timestamp is the earliest entry. # ### Write the data # In[76]: shimmer_write_path = os.path.join(write_dir, 'Shimmer') if not os.path.isdir(shimmer_write_path): os.makedirs(shimmer_write_path) shimmer_reader.write(os.path.join(shimmer_write_path, 'shimmer_write.csv')) os.listdir(shimmer_write_path) # ## Muse # # The devicely.MuseReader can be used for reading data generated by the Muse S headband. # ### Read the data # In[77]: muse_reader = devicely.MuseReader(os.path.join('Muse', 'data.csv')) muse_reader.data.head() # ### Timeshift the data # # Apply a random timeshift: # In[78]: muse_reader.timeshift() muse_reader.data.head() # ### Write the data # In[79]: muse_write_path = os.path.join(write_dir, 'muse_write_data.csv') if os.path.isfile(muse_write_path): os.remove(muse_write_path) muse_reader.write(muse_write_path) os.listdir(write_dir) # ## Tags # # You can use the TimeStampReader to read data created by the Android app TimeStamp. Researchers use this app to mark important times during experiments. The format simple, as can be seen in this [example file](https://github.com/hpi-dhc/devicely-example/tree/main/Tags). # ### Read the data # In[80]: timestamp_reader = devicely.TimeStampReader(os.path.join('Tags', 'tags.csv')) timestamp_reader.data.head() # ### Timeshif the data # # Apply a timeshift: # In[81]: timestamp_reader.timeshift() timestamp_reader.data.head() # By providing no parameter to `timeshift` the data is shifted by a random time interval between one month and two years to the past. You can also provide a `pandas.Timedelta` object to shift the data by that timedelta or a `pandas.Timestamp` object to shift the data such that this timestamp is the earliest entry. # ### Write the data # In[82]: tags_write_path = os.path.join(write_dir, 'Tags') if not os.path.isdir(tags_write_path): os.makedirs(tags_write_path) timestamp_reader.write(os.path.join(tags_write_path, 'tags_write.csv')) os.listdir(tags_write_path)