def create_client_and_authenticate(ctx): """Create a client and authenticate.""" host = ctx.config['server_url'] port = ctx.config['port'] api_path = ctx.config['api_path'] info(f"Connecting to server at '{host}:{port}{api_path}'") username = q.text("Username:"******"Password:"******"Could not authenticate with server!") debug(e) exit(1) return client
def get_official_client(username, password): """ Get official vantage6 client. :param username: :param password: :return: """ client = Client(_HOST, _PORT) client.authenticate(username, password) client.setup_encryption(None) return client
# print the results per node for result in results: node_id = result.get("node") print("-" * 80) print(f"Results from node = {node_id}") print(result.get("result")) # central server configuration host = "http://192.168.37.1" port = 5000 collaboration_id = 3 # 1. authenticate to the central server client = Client(host=host, port=port, path="/api") client.setup_encryption(None) client.authenticate("root", "admin") # 2. connect to websocket interface bearer_token = client.token socket = SocketIO(host, port=port, headers={"Authorization": f"Bearer {bearer_token}"}) # subscribe to the websocket channel. taskNamespace = socket.define(TasksNamespace, "/tasks") taskNamespace.emit("join_room", f"collaboration_{collaboration_id}") # input for the dsummary Docker image (algorithm) input_ = {
A more advanced example shows how to obtain the results using websockets The researcher has to execute the following steps: 1) Authenticate to the central-server 2) Prepare the input for the algorithm 3) Post a new task to a collaboration on the central server 4) Wait for all results to finish (polling) 5) Obtain the results 6) Optionally do some central processing """ import time from vantage6.client import Client # 1. authenticate to the central server client = Client(host="http://192.168.37.1", port=5000, path="/api") client.setup_encryption(None) client.authenticate("root", "admin") # 2. Prepare input for the dsummary Docker image (algorithm) input_ = { "master": "true", "method": "master", "args": [], "kwargs": { #"functions": ["min", "max"], "columns": [{ "variable": "age", "table": "records", "functions": ["min", "max"] }, {
For simplicity this example also uses polling to obtain the results. A more advanced example shows how to obtain the results using websockets The researcher has to execute the following steps: 1) Authenticate to the central-server 2) Prepare the input for the algorithm 3) Post a new task to a collaboration on the central server 4) Wait for central container to finish (polling) 5) Obtain the results """ import time from vantage6.client import Client # 1. authenticate to the central server client = Client(host="http://192.168.37.1", port=5000, path="/api") client.setup_encryption(None) client.authenticate("root", "admin") # 2. Prepare input for the dsummary Docker image (algorithm) input_ = { "master": "true", "method": "master", "args": [], "kwargs": { #"functions": ["min", "max"], "columns": [{ "variable": "age", "table": "records", "functions": ["min", "max"] }, {
def setup_client() -> Client: client = Client(HOST, PORT) client.authenticate(FAKE_USERNAME, FAKE_PASSWORD) client.setup_encryption(None) return client
from vantage6.client import Client from pathlib import Path import time print("Attempt login to Vantage6 API") client = Client("http://localhost", 5000, "/api") client.authenticate("johan", "1234") client.setup_encryption(None) input_ = {"master": "true", "method": "master", "args": [], "kwargs": {}} print("Requesting to execute summary algorithm") task = client.post_task(name="testing", image="docker.io/username/imagename", collaboration_id=1, input_=input_, organization_ids=[1]) print("Wait and fetch results") res = client.result.get(id_=task.get("results")[0]['id']) attempts = 1 while ((res["result"] == None) and attempts < 7): print("waiting...") time.sleep(5) res = client.result.get(id_=task.get("results")[0]['id']) attempts += 1 print(res)
def master(client: Client, data, incidence, population, gender, ageclass, prefacture, standard_population, organization_ids=None): """Master algoritm. The master algorithm is the chair of the Round Robin, which makes sure everyone waits for their turn to identify themselfs. """ # get all organizations (ids) that are within the collaboration # FlaskIO knows the collaboration to which the container belongs # as this is encoded in the JWT (Bearer token) if not organization_ids: organizations = client.get_organizations_in_my_collaboration() organization_ids = [organization.get("id") for organization in organizations] # all participating paties compute crude rate and incidence population info("Dispatching preliminairy_results") task_preliminairy_results = client.create_new_task( input_={ "method": "preliminairy_results", "kwargs": { "incidence": incidence, "population": population, "gender": gender, "ageclass": ageclass, "prefacture": prefacture } }, organization_ids=organization_ids ) # while we wait, lets compute the relative population dataframe info('Calculating relative population') rel_pop = relative_population(standard_population, population, ageclass) # Collect results preliminairy_results = \ wait_and_collect_results(client, task_preliminairy_results.get('id')) crude_rate = [res["crude_rate"] for res in preliminairy_results] total_local_pop = \ [res["total_local_population"] for res in preliminairy_results] total_local_incidence = \ [res["total_local_incidence"] for res in preliminairy_results] incidence_population_results = \ [res["incidence_population"] for res in preliminairy_results] info("Combining crude rates") crude_rate_combined = \ combined_crude_rate(total_local_incidence, total_local_pop) info("Calculating people at risk") people_at_risk_results = people_at_risk(incidence_population_results) info("Dispatching Adjusted Rate") task_adjusted_rate = client.create_new_task( input_={ "method": "adjusted_rate", "kwargs": { "incidence": incidence, "gender": gender, "ageclass": ageclass, "rel_pop": rel_pop, "people_at_risk": people_at_risk_results, "population": population, "prefacture": prefacture } }, organization_ids=organization_ids ) adjusted_rate_results = \ wait_and_collect_results(client, task_adjusted_rate.get('id')) adjusted_rate_glob = \ [res["adj_rate_glob"] for res in adjusted_rate_results] adjusted_rate_local = \ [res["adj_rate_local"] for res in adjusted_rate_results] info("Combining adjusted rates") adjusted_rate_combined = combined_adjusted_rate(adjusted_rate_glob) info("Master algorithm complete") return { "local_crude_rate": crude_rate, "combined_crude_rate": crude_rate_combined, "local_adjusted_rate": adjusted_rate_local, "combined_adjusted_rate": adjusted_rate_combined }
# algorithm needs to be executed on the machine of the researcher. # For simplicity this example also uses polling to obtain the results. # A more advanced example shows how to obtain the results using websockets # The researcher has to execute the following steps: # 1) Authenticate to the central-server # 2) Prepare the input for the algorithm # 3) Post a new task to a collaboration on the central server # 4) Wait for central container to finish (polling) # 5) Obtain the results # """ # import time # from vantage6.client import Client # # 1. authenticate to the central server client = Client(host="127.0.0.1", port=5000, path="/api") client.authenticate("root", "admin") client.setup_encryption(None) # # # 2. Prepare input for the dsummary Docker image (algorithm) input_ = { "method": "master", "args": [], "kwargs": { "decimal": ",", "seperator": ";", "columns": { "patient_id": "Int64", "age": "Int64", "weight": "float64",