A Python interface for the Daisi Platform
Project description
Simples steps for using the PyDaisi SDK
Preliminary tasks
Install with PIP:
pip install pydaisi
(Optional) Set your personal access token:
Create your personal access token
Set it in the environment:
export DAISI_ACCESS_TOKEN=a1b2c3d4e5f67890abcdef124567890
or in a .env
file:
DAISI_ACCESS_TOKEN=a1b2c3d4e5f67890abcdef124567890
Using PyDaisi
Normal calls
You can call the Daisi function, it will run until complete, and the result will be available in the value
attribute when it has returned.
from pydaisi import Daisi
# instantiate a Daisi object
daisi = Daisi("my-pebble-tutorial")
# call a Daisi function. You can also use positional parameters: daisi.median("London")
temp = daisi.median(city="London")
print(f"Median temperature in London was: {temp.value}")
print(f"Mean temperature in Amsterdam was: {daisi.mean('Amsterdam')}.value")
Async calls
You can also use Python asyncio functions to create and dispatch many calls asynchronously.
from pydaisi import Daisi
import asyncio
async def callmany():
calls = []
# sets up a call, but does not execute
with Daisi("my-pebble-tutorial") as daisi:
calls.append(daisi.defer.mean("Paris").fetch_result())
calls.append(daisi.defer.mean("London").fetch_result())
calls.append(daisi.defer.mean("Amsterdam").fetch_result())
await asyncio.gather(calls)
Parallel Execution
You may also use helper functions to execute many calls from your synchronous code
from pydaisi import Daisi
with Daisi('my_daisi_name') as my_daisi:
calls = []
calls.append(my_daisi.endpoint1_(**kwargs))
calls.append(my_daisi.endpoint2_(**kwargs))
calls.append(my_daisi.endpoint3_(**kwargs))
print(Daisi.run_parallel(*calls))
Bulk Execution
You can pass a list of arguments all at once, to avoid the overhead of multiple requests to the API:
from pydaisi import Daisi
with Daisi("Add Two Numbers", base_url="https://dev3.daisi.io") as my_daisi:
dbe = my_daisi._bulk_run(arguments = [{"firstNumber": 5, "secondNumber": x} for x in range(10)])
print(dbe._bulk_fetch_result())
Execution Status
A Daisi's status can be accessed with the status
property:
from pydaisi import Daisi
with Daisi("Add Two Numbers", base_url="https://dev3.daisi.io") as my_daisi:
de = my_daisi.compute(firstNumber=5, secondNumber=6)
print(de.status)
Execution Logs
A Daisi's logs can be accessed with the logs
property:
from pydaisi import Daisi
with Daisi("Live Logging", base_url="https://dev3.daisi.io") as my_daisi:
de = my_daisi.live_log_test(firstNumber=5, secondNumber=6, delay=3)
print(de.logs)
Remote Results
You need not fetch the full data of a Daisi Execution in order to chain it to the computation of another daisi! Consider this example:
from pydaisi import Daisi
# Connect to the Serialization Daisi
d3 = Daisi("Daisi Serialize", base_url="https://dev3.daisi.io")
# Import numpy and define the MapStack class that we will use as an example of custom serialization
import numpy as np
class MapStack:
def __init__(self, nx, ny):
self.nx = nx
self.ny = ny
self.nb_layers = None
self.maps = []
def add_layer(self, map):
if len(map.shape) == 2 and map.shape[0] == self.ny and map.shape[1] == self.nx:
self.maps.append(map)
self.nb_layers = len(self.maps)
return "Map sucessfully added."
else:
return "Could not add map. Incompatible dimensions."
# Initialize a new MapStack object with 10 layers
nx = 200
ny = 200
ms = MapStack(nx, ny)
for i in range(10):
ms.add_layer(np.random.rand(nx, ny))
# Compute the daisi, adding a new layer
d3_execution = d3.compute(map_stack=ms, map=np.random.rand(nx, ny))
d3_execution.value_id
# Compute the daisi, adding a another new layer
d3_execution2 = d3.compute(map_stack=d3_execution, map=np.random.rand(nx, ny))
d3_execution2.value
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file pydaisi-0.3.0rc5-py3-none-any.whl
.
File metadata
- Download URL: pydaisi-0.3.0rc5-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 082ad580254621aac2366229172e8b990ca73d5b2597b02b7826b6ca4ffecc71 |
|
MD5 | 46b2f8be4e9ead2600c0fcc29d8d5d5f |
|
BLAKE2b-256 | 0eddc019ea47df85281c9387707d3abb2fbc831637fcb18a9c37262075aa0fd2 |