Skip to main content

A Python package for interacting with the SNAPI API, YawnLabs, and various other health device APIs.

Project description

Yawn-API (yapi)

PyPI version

A Python package for interacting with the SNAPI API, YawnLabs, and various other health device APIs.

Installation

Install via Github using pip install:

pip install yawn-api

Usage

Primarily interfaces with the SNAPI API, but also has some functionality for directly accessing device-specific APIs. By default data is rethieved in JSON format, but can be returned as a response object by setting verbose=True.

from yapi import YapiClient

# Verbose mode will print more information to the console and is useful for debugging. 

# NOTE: Will also return responses as response objects, rather than json, 
# so subsequent functionality may be impacted.

# yp = YapiClient(verbose=True) 
yp = YapiClient()

sleeps = yp.withings.sleep.get('participant1')
print(sleeps)

Utilities

YAPI has a number of helper functions for working with the data returned by the API. For example, the below gets participants information for the associated study and then requests sleep summary information based on the participant IDs.\ The function epoch.backup_study_epoch_data is then called to retrieve epoch data for each participant directly from Withings and save them to individual .csv files.\ Finally, epoch.combine_epoch_data is called to combine the individual .csv files into a single DataFrame.

from yapi import YapiClient

yp = YapiClient()
study = 'SAMOSA'

study_participants = yp.participants.get_all(study_name=study)
participant_ids = [participant['lab_id'] for participant in study_participants]

sleeps = yp.withings.sleep.get(participant_ids, as_df=True)

path = '/path/for/epoch/data'

epoch.backup_study_epoch_data(
    study=study,    # Required - the name of the study.
    folder=path,    # Optional - if False, will create a directory f'{study}_EpochData' in the current working directory. 
                    # If True, will open a file dialog to select the folder. Otherwise, specify the path to the folder.
    verbose=True,   # Optional - print more information to the console.
    update=True     # Optional - update the epoch data if it already exists, 
                    # otherwise will skip any existing files in the folder.
)

# Inclusion of the sleep_df argument will combine the timezone information from the sleep data.
# This is not necessary, but can be useful with interpretation. If unsure, it is recommended to include it.
epoch_data = epoch.combine_epoch_data(
    study=study,        # Required - the name of the study.
    input_folder=path,  # Required - the folder to save the epoch data to.
    output_folder=path, # Optional - the folder to save the epoch data to, defaults to the input folder.
    sleep_df=sleeps,    # Optional - the sleep data to include timezone information from. 
                        # If not included, will still combine the epoch data, but will not include timezone information.
    save=True           # Optional - save the combined epoch data to a '{study}_epochCombined.csv' file in the output folder.
)

print(epoch_data.head())

RedCap

YAPI also has a number of helper functions for working with the RedCap API.

For example, the below will get all records from a RedCap project, filter out to only includethose that have consented, and then create participant entries in the YawnLabs database for each of them.

from yapi.redcap import RedcapClient

token = 'redcap_token'  # Required - the API token for the RedCap project.
yrc = RedcapClient(token=token) 

r = yrc.records.get()
consented = [record for record in r if record['checkconsent'] == '1']

for participant in consented:
    yp.participants.create({
        'lab_id': participant['lab_id'],
        'study_name': 'MIMOSA'
    })

Alternatively, the below will get a file from a RedCap project and save it to the current working directory.

from yapi.redcap import RedcapClient

yrc = RedcapClient(token=s)
response = ypr.files.get(
    record_id='SAMOSA036',      # Required - the record ID in the RedCap project.
    field='cpap_upload',        # Required - the field name in the RedCap project.
    event='recruitment_arm_1'   # Required for longitudinal projects, only.
)

ypr.files.save(
    response,
    filename='test',# The extension can be automatically inferred from the 
                    # response object, or specified manually 
                    # (e.g., filename='test.pdf').
    filepath='/data'# Optional - the path to save the file to. 
                    # Defaults to the current working directory.
)

Inquisit

YAPI also has some helper functions for working with the Inquisit API. For now, this is primarily for collating Inquisit .iqdat files into a single DataFrame, but more will be added as time allows.

from yapi.inquisit import InquisitClient

yiq = InquisitClient()

path = '/path/to/iqdat/files'
savepath = '/path/to/save/combined/file'
pvt_summary, pvt_raw = yiq.collate(
    path=path,          # Required - the path to the folder containing the .iqdat files.
    test='PVT',         # Optional - the name of the test to collate. 
                        # If not specified, will attempt to collate all tests in the folder.
                        # NOTE: this must match (or be in) the name of the test in the .iqdat files (case-insensitive).
                        # For example, 'PVT' will match 'PVT', 'pvt', 'PVT_1', 'PVT_2', etc.
    savepath=savepath   # Optional - the path to save the combined file to. 
                        # If not specified, will not save the file.
)

Below is a more complex example that collates PVT and Digit Span data from two different folders and combines them into a single DataFrame. The identifier argument is used to specify the column/value tuple to use as an identifier for the data. This is useful for combining data from different timepoints, for example.

from yapi.inquisit import InquisitClient
yiq = InquisitClient(verbose=True)

savepath = "Cognitive Data/Combined Data/"

pvt1_path = "Cognitive Data/PVT"
pvt2_path = "Cognitive Data/Follow Up/PVT"
ds1_path = "Cognitive Data/Digit Span"
ds2_path = "Cognitive Data/Follow Up/Digital Span"

pvt1_summary, pvt1_raw = yiq.collate(pvt1_path, test = 'PVT', identifier=('timepoint', 'baseline'))
pvt2_summary, pvt2_raw = yiq.collate(pvt2_path, test = 'PVT', identifier=('timepoint', 'followup'))
pvt_summary = pd.concat([pvt1_summary, pvt2_summary], ignore_index=True).reset_index(drop=True)
pvt_raw = pd.concat([pvt1_raw, pvt2_raw], ignore_index=True).reset_index(drop=True)
pvt_summary.to_csv(f"{savepath}PVT_combinedSummary.csv", index=False)
pvt_raw.to_csv(f"{savepath}PVT_combinedRaw.csv", index=False)

ds1_summary, ds1_raw = yiq.collate(ds1_path, test = 'Digit Span', identifier=('timepoint', 'baseline'))
ds2_summary, ds2_raw = yiq.collate(ds2_path, test = 'Digit Span', identifier=('timepoint', 'followup'))
ds_summary = pd.concat([ds1_summary, ds2_summary], ignore_index=True).reset_index(drop=True)
ds_raw = pd.concat([ds1_raw, ds2_raw], ignore_index=True).reset_index(drop=True)
ds_summary.to_csv(f"{savepath}DigitSpan_combinedSummary.csv", index=False)
ds_raw.to_csv(f"{savepath}DigitSpan_combinedRaw.csv", index=False)

Sleep Report Scraper

YAPI also has a helper function for scraping sleep study reports. Currently, it is set up to look for .slp folders, .rtf files, and .dat files in order to find any .rft file with the word 'report' in it.

This is a very basic implementation and will need to be adjusted for different file structures, but should be a good starting point.

def sleep_studies():
    from yapi.sleep_studies import SleepStudies
    yss = SleepStudies()
    
    path = 'path/to/sleep/studies'
    savepath="savefolder/sleep_studies.csv"
    
    df = yss.report.collate(path)   # Optional - savepath. Left blank here
                                    # due to further processing below.
    
    for i, row in df.iterrows():
        # Filepath saved in the dataframe is the full path to the file
        # Below is an example of using this to extract the filename and 
        # participant ID.
        df.at[i, 'file'] = row['filepath'].split('\\')[-1]
        df.at[i, 'participant'] = row['filepath'].split('sleep\\')[-1].split('\\')[0]

    df.to_csv(savepath, index=False)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yawn_api-0.1.3.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yawn_api-0.1.3-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file yawn_api-0.1.3.tar.gz.

File metadata

  • Download URL: yawn_api-0.1.3.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for yawn_api-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4c65aefc0e0f9fb2369eb99fff8d248cb31b212ac56d391f44a02a1dd4848c5e
MD5 952ee6367dde899b1e75e0e738ccbe67
BLAKE2b-256 a2e8036607c8e0e03fa840a10f82b9b2f47daad32fd3a3067ebde2c32b15fc78

See more details on using hashes here.

File details

Details for the file yawn_api-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: yawn_api-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for yawn_api-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 92903bb3858dfbfe4439602129e16d01f3228c5de3e5c9c8a798e1411f02bb7d
MD5 2c5a658b548e39968c32683f74c6d338
BLAKE2b-256 a6562eed2bbeca7d65194770b6382914b3ab142e1b9ad0dca9a6bbfd132dae52

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page