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)

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 git+https://github.com/jackmanners/yawn-api.git

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. Will also return responses as response objects, rather than json.

# 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.

yp = YapiClient()
study = 'SAMOSA'

study_participants = yp.withings.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.

token = 'redcap_token'  # Required - the API token for the RedCap project.
yrc = RedcapClient(verbose=False, 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

ypr = RedcapClient(verbose=True, 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').
)

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.0.tar.gz (13.0 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.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yawn_api-0.1.0.tar.gz
  • Upload date:
  • Size: 13.0 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.0.tar.gz
Algorithm Hash digest
SHA256 4c95d2a91233ebe22d30dd580e5e35934a1648dc32187294dba68fd0763aa2c7
MD5 cca2b76d806f768d13f7b9a3e8fe44eb
BLAKE2b-256 f2a63792d06b7d86886e2ffab20b65903012a77a922ca5ef11a0e6b7655c1b00

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yawn_api-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0b4f49c03b0146fc4582bf9b679ca9a76a104423cbebb0c81420ecbc50b2eae1
MD5 c0701acdfefe7352f12338af78fd9a16
BLAKE2b-256 650b328d646985a7a8d594ed71955e9d70aae423e8b3480fb1107b78ec934452

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