Skip to main content

Run computations

Specify DCR and computation to run

import decentriq_platform as dq
import decentriq_platform.legacy.sql as dqsql
import decentriq_platform.legacy.container as dqc
from decentriq_platform.legacy.container.proto import MountPoint

user_email = "test_user@company.com"
api_token = "@@ YOUR TOKEN HERE @@"

client = dq.create_client(user_email, api_token)
enclave_specs = "" #dq.enclave_specifications.versions([...])

auth, _ = client.create_auth_using_decentriq_pki(enclave_specs)
session = client.create_session(auth, enclave_specs)

builder = dq.legacy.DataRoomBuilder("My DCR", enclave_specs=enclave_specs)

data_node_builder = dqsql.TabularDataNodeBuilder(
"salary_data",
schema=[
("name", dqsql.PrimitiveType.STRING, False),
("salary", dqsql.PrimitiveType.FLOAT64, False)
]
)
data_node_builder.add_to_builder(
builder,
authentication=client.decentriq_pki_authentication,
users=[user_email]
)

query_node = dqsql.SqlCompute(
name="salary_sum",
sql_statement=f"""
SELECT SUM(salary)
FROM salary_data
""",
dependencies=[ ("salary_data", data_node_builder.output_node_id) ]
)

DCR_SQL_NODE_ID = builder.add_compute_node(query_node)

my_script_content = b"""
with open("/input/lowercase.txt", "r") as input_file:
input_data = input_file.read()
with open("/output/uppercase.txt", "w") as output_file:
output_file.write(input_data.upper())
"""
script_node = dq.StaticContent("script_node", my_script_content)
script_node_id = builder.add_compute_node(script_node)
data_node_id = builder.add_data_node("input_data_node", is_required=True)

uppercase_text_node = dqc.StaticContainerCompute(
name="uppercase_text_node",
command=["python", "/input/my_script.py"],
mount_points=[
MountPoint(path="my_script.py", dependency=script_node_id),
MountPoint(path="lowercase.txt", dependency=data_node_id)
],
output_path="/output",
enclave_type="decentriq.python-ml-worker-32-64"
)
DCR_PYTHON_NODE_ID = builder.add_compute_node(uppercase_text_node)

builder.add_user_permission(
email=user_email,
authentication_method=client.decentriq_pki_authentication,
permissions=[
dq.Permissions.update_data_room_status(),
dq.Permissions.leaf_crud(data_node_id),
dq.Permissions.execute_compute(DCR_PYTHON_NODE_ID),
dq.Permissions.retrieve_compute_result(DCR_PYTHON_NODE_ID),
dq.Permissions.execute_compute(DCR_SQL_NODE_ID),
dq.Permissions.retrieve_compute_result(DCR_SQL_NODE_ID),
]
)

dcr = builder.build()
DCR_ID = session.publish_data_room(dcr)

encryption_key = dq.Key()

data = dqsql.read_input_csv_string(
"""\
Name,Salary
Alice,10.0
Bob,5.0
John,14.0
""",
has_header=True,
delimiter=","
)
dataset_id = dqsql.upload_and_publish_tabular_dataset(
data, encryption_key, DCR_ID,
table="salary_data",
session=session, description="salary", validate=True
)

import io
data = io.BytesIO(b"hello world")
dataset_id = client.upload_dataset(data, encryption_key, "myfile")
session.publish_dataset(DCR_ID, dataset_id, data_node_id, encryption_key)

Run and retrieve SQL computation results

import decentriq_platform.legacy.sql as dqsql

job_id = session.run_computation(DCR_ID, DCR_SQL_NODE_ID)
results = session.get_computation_result(job_id)
csv = dqsql.read_sql_query_result_as_string(results)

print(f"Computation result:\n\n{csv}")

Run and retrieve script computation results

import decentriq_platform.legacy.container as dqc

raw_result = session.run_computation_and_get_results(DCR_ID, DCR_PYTHON_NODE_ID)
zip_result = dqc.read_result_as_zipfile(raw_result)
text = zip_result.read("uppercase.txt").decode()

print(f"Computation result:\n\n{text}")