Giter VIP home page Giter VIP logo

dfx-api-snippets-python's Introduction

DFX API Python Snippets

This repository contains snippets of Python code you may find useful to connect to the NuraLogix DeepAffex API

Getting started

  • Create and activate your python3.6 environment and run pip install .

  • Get payload files(payload, metadata, properties) from the DFX SDK and save them in a directory

  • In a shell, run:

    python measure.py "studyID received from Nuralogix" \
                      "Token from registration/login process"
                      "base REST url to the DFX API" \
                      "base Websocket url to the DFX API" \
                      "your directory above"

Let's take a look at measure.py

First we import what we need:

import asyncio  # For async operations
import argparse # For parsing arguments

# The followings are the libraries we made in the dfxsnippets directory
# Refer to each .md files of them for detailed description
from dfxsnippets.createMeasurement import createMeasurement
from dfxsnippets.subscribeResults import subscribeResults
from dfxsnippets.addData import addData

Then, we parse the command line to set up the studyId, token, restUrl, websocketUrl, and input directory to the payload files.

parser = argparse.ArgumentParser()

parser.add_argument("studyID", help="StudyID")
parser.add_argument("token", help="user or device token")
parser.add_argument("restUrl", help="DFX API REST url")
parser.add_argument("wsUrl", help="DFX API Websocket url")
parser.add_argument("payloadDir", help="Directory of payload files")

args = parser.parse_args()

studyID = args.studyID
token = args.token
rest_url = args.restUrl
ws_url = args.wsUrl
input_directory = args.payloadDir

Then, we create the eventloop which manages all the async tasks:

loop = asyncio.get_event_loop()

Then, we create a Measurement and get it's measurementID

createmeasurementObj = createMeasurement(studyID, token, rest_url)
measurementID = createmeasurementObj.create()

Then, we create an addData object which prepares the data need to be sent in the input_directory

adddataObj = addData(measurementID, token, rest_url, input_directory)

Then, we create a subscribeResults object which prepares the subscribeResults request

subscriberesultsObj = subscribeResults(measurementID, token, ws_url, adddataObj.num_chunks)

Then, we add the subscribeResults.subscribe() method to an async task list

tasks = []
t = loop.create_task(subscriberesultsObj.subscribe())
tasks.append(t)

Then, we start the event loop by asking it to run until it finishes all the chunks, sending requests in addData.sendAsync() asynchronously:

loop.run_until_complete(adddataObj.sendAsync())

In this process, whenever the sendAsyncs are awaiting for I/O operation to finish, the event loop will try to switch context to the next async task that is not awaiting, which is the subscribeResults.subscribe() in our case.

At this point, once the event loop has finished all the chunks in addData.sendAsync(), we ask the eventloop to keep running until the subscribeResults.subscribe() finishes, which will return when all chunks results are received, in the task list to finish.

wait_tasks = asyncio.wait(tasks)
loop.run_until_complete(wait_tasks)
loop.close()

dfx-api-snippets-python's People

Contributors

yuch3ng avatar sambhare avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.