forked from brainstem-org/brainstem_python_api_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrainstem_api_tutorial.py
More file actions
70 lines (49 loc) · 2.58 KB
/
brainstem_api_tutorial.py
File metadata and controls
70 lines (49 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from brainstem_api_tools import BrainstemClient
# 0. Load the client. User email and password will be requested.
client = BrainstemClient()
# Loading sessions.
## load_model can be used to load any model:
## We just need to pass our settings and the name of the model.
output1 = client.load_model('session').json()
### We can fetch a single session entry from the loaded models.
session = output1["sessions"][0]
## We can also filter the models by providing a dictionary where
## the keys are the fields and the values the possible contents.
## In this example, it will just load sessions whose name is "yeah".
output1 = client.load_model('session', filters={'name': 'yeah'}).json()
## Loaded models can be sorted by different criteria applying to
## their fields. In this example, sessions will be sorted in
## descending ording according to their name.
output1 = client.load_model('session', sort=['-name']).json()
## In some cases models contain relations with other models, and
## they can be also loaded with the models if requested. In this
## example, all the projects, data acquisition, behaviors and
## manipulations related to each session will be included.
output1 = client.load_model('session', include=['projects', 'dataacquisition', 'behaviors', 'manipulations']).json()
### The list of related experiment data can be retrived from the
### returned dictionary.
dataacquisition = output1["sessions"][0]["dataacquisition"]
## All these options can be combined to suit the requirements
## of the users. For example, we can get only the session that
## contain the word "Rat" in their name, sorted in descending
## order by their name and including the related projects.
output1 = client.load_model('session', filters={'name.icontains': 'Rat'}, sort=["-name"], include=['projects']).json()
# Updating a session
## We can make changes to a model and update it in the database.
## In this case, we changed the description of one of the
## previously loaded sessions.
session = {}
session["description"] = 'new description'
output2 = client.save_model("session", id="0e39c1fd-f413-4142-95f7-f50185e81fa4", data=session).json()
# Creating a new session
## We can submit a new entry by defining a dictionary with the
## required fields.
session = {}
session["name"] = "New session"
session["projects"] = ['e7475834-7733-48cf-9e3b-f4f2d2d0305a']
session["description"] = 'description'
## Submitting session
output3 = client.save_model("session", data=session).json()
# Load public projects
## Request the public data by defining the portal to be public
output4 = client.load_model("project", portal="public").json()