brainstem-python-api-tools 0.1.0__tar.gz → 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- brainstem_python_api_tools-2.0.0/PKG-INFO +179 -0
- brainstem_python_api_tools-2.0.0/README.md +163 -0
- {brainstem_python_api_tools-0.1.0 → brainstem_python_api_tools-2.0.0}/brainstem_api_tools/__init__.py +3 -1
- brainstem_python_api_tools-2.0.0/brainstem_api_tools/brainstem_api_client.py +895 -0
- brainstem_python_api_tools-2.0.0/brainstem_api_tools/cli.py +180 -0
- brainstem_python_api_tools-2.0.0/brainstem_python_api_tools.egg-info/PKG-INFO +179 -0
- {brainstem_python_api_tools-0.1.0 → brainstem_python_api_tools-2.0.0}/brainstem_python_api_tools.egg-info/SOURCES.txt +4 -2
- brainstem_python_api_tools-2.0.0/brainstem_python_api_tools.egg-info/entry_points.txt +2 -0
- brainstem_python_api_tools-2.0.0/brainstem_python_api_tools.egg-info/requires.txt +5 -0
- brainstem_python_api_tools-2.0.0/pyproject.toml +33 -0
- brainstem_python_api_tools-2.0.0/tests/test_client.py +729 -0
- brainstem_python_api_tools-0.1.0/PKG-INFO +0 -21
- brainstem_python_api_tools-0.1.0/README.md +0 -4
- brainstem_python_api_tools-0.1.0/brainstem_api_tools/brainstem_api_client.py +0 -247
- brainstem_python_api_tools-0.1.0/brainstem_python_api_tools.egg-info/PKG-INFO +0 -21
- brainstem_python_api_tools-0.1.0/brainstem_python_api_tools.egg-info/requires.txt +0 -2
- brainstem_python_api_tools-0.1.0/pyproject.toml +0 -3
- brainstem_python_api_tools-0.1.0/setup.py +0 -24
- {brainstem_python_api_tools-0.1.0 → brainstem_python_api_tools-2.0.0}/LICENSE +0 -0
- {brainstem_python_api_tools-0.1.0 → brainstem_python_api_tools-2.0.0}/brainstem_python_api_tools.egg-info/dependency_links.txt +0 -0
- {brainstem_python_api_tools-0.1.0 → brainstem_python_api_tools-2.0.0}/brainstem_python_api_tools.egg-info/top_level.txt +0 -0
- {brainstem_python_api_tools-0.1.0 → brainstem_python_api_tools-2.0.0}/setup.cfg +0 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: brainstem_python_api_tools
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: A Python toolset for interacting with the BrainSTEM API.
|
|
5
|
+
Author-email: BrainSTEM Team <petersen.peter@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/brainstem-org/brainstem_python_api_tools
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: requests>=2.28
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest; extra == "dev"
|
|
14
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# BrainSTEM Python API Tools
|
|
18
|
+
|
|
19
|
+
The `brainstem_python_api_tools` is a Python toolset for interacting with the [BrainSTEM](https://www.brainstem.org/) API, designed for researchers and developers working with neuroscience data.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
You can install the package using `pip`:
|
|
23
|
+
```bash
|
|
24
|
+
pip install brainstem_python_api_tools
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Authentication
|
|
28
|
+
|
|
29
|
+
Authentication uses a **browser-based device authorization flow** that supports two-factor authentication (2FA). No credentials are entered into the tool itself.
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from brainstem_api_tools import BrainstemClient
|
|
33
|
+
|
|
34
|
+
# First run: opens a browser window for secure login.
|
|
35
|
+
# The token is cached at ~/.config/brainstem/token and reused automatically.
|
|
36
|
+
client = BrainstemClient()
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
To skip the browser flow, pass a token directly:
|
|
40
|
+
```python
|
|
41
|
+
client = BrainstemClient(token="YOUR_TOKEN")
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
For headless environments (no browser available):
|
|
45
|
+
```python
|
|
46
|
+
client = BrainstemClient(headless=True) # prints a URL + code to enter manually
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
To connect to a different server (e.g. a local development instance):
|
|
50
|
+
```python
|
|
51
|
+
client = BrainstemClient(url="http://127.0.0.1:8000/")
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
> **Security note:** The cached token file is stored with owner-read-only permissions (`0600`). Treat it like a password and do not commit it to version control.
|
|
55
|
+
|
|
56
|
+
## Getting Started
|
|
57
|
+
To get started with the BrainSTEM API tools, please refer to the tutorial script provided:
|
|
58
|
+
|
|
59
|
+
- **Tutorial:** `brainstem_api_tutorial.py`
|
|
60
|
+
|
|
61
|
+
The tutorial demonstrates how to:
|
|
62
|
+
|
|
63
|
+
- **Authenticate:** Load the client and authenticate via your browser.
|
|
64
|
+
- **Load Data:** Load sessions and filter data using flexible options.
|
|
65
|
+
- **Paginate:** Retrieve large datasets using `limit` / `offset` or `load_all=True`.
|
|
66
|
+
- **Convenience Loaders:** Use `load_session()`, `load_subject()`, etc. for common queries.
|
|
67
|
+
- **Update Entries:** Modify existing records and update them in the database.
|
|
68
|
+
- **Create Entries:** Submit new data entries with required fields.
|
|
69
|
+
- **Delete Entries:** Remove records by ID.
|
|
70
|
+
- **Load Public Data:** Access public projects and data using the public portal.
|
|
71
|
+
|
|
72
|
+
## Example Usage
|
|
73
|
+
```python
|
|
74
|
+
from brainstem_api_tools import BrainstemClient
|
|
75
|
+
|
|
76
|
+
client = BrainstemClient()
|
|
77
|
+
|
|
78
|
+
# Load sessions sorted by name
|
|
79
|
+
response = client.load('session', sort=['name'])
|
|
80
|
+
print(response.json())
|
|
81
|
+
|
|
82
|
+
# Filter and include related data
|
|
83
|
+
response = client.load(
|
|
84
|
+
'session',
|
|
85
|
+
filters={'name.icontains': 'Rat'},
|
|
86
|
+
sort=['-name'],
|
|
87
|
+
include=['projects'],
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
# Paginate results (manual)
|
|
91
|
+
page2 = client.load('session', limit=20, offset=20).json()
|
|
92
|
+
|
|
93
|
+
# Auto-paginate — fetches every page and returns a merged dict
|
|
94
|
+
all_sessions = client.load('session', load_all=True)
|
|
95
|
+
|
|
96
|
+
# Convenience loaders — sensible defaults with named filter kwargs
|
|
97
|
+
# load_session embeds dataacquisition, behaviors, manipulations, epochs
|
|
98
|
+
sessions = client.load_session(name='Rat', load_all=True)
|
|
99
|
+
|
|
100
|
+
# load_subject embeds procedures and subjectlogs
|
|
101
|
+
subjects = client.load_subject(sex='M', projects='<project-uuid>', load_all=True)
|
|
102
|
+
|
|
103
|
+
# load_project embeds sessions, subjects, collections, cohorts
|
|
104
|
+
projects = client.load_project(name='MyProject')
|
|
105
|
+
|
|
106
|
+
# load_behavior / load_dataacquisition / load_manipulation scope by session UUID
|
|
107
|
+
behaviors = client.load_behavior(session='<session-uuid>', load_all=True)
|
|
108
|
+
|
|
109
|
+
# load_procedure scopes by subject UUID
|
|
110
|
+
procedures = client.load_procedure(subject='<subject-uuid>', load_all=True)
|
|
111
|
+
|
|
112
|
+
# Create a record
|
|
113
|
+
client.save('session', data={'name': 'New session', 'projects': ['<project-uuid>']})
|
|
114
|
+
|
|
115
|
+
# Update a record
|
|
116
|
+
client.save('session', id='<session-uuid>', data={'description': 'updated'})
|
|
117
|
+
|
|
118
|
+
# Delete a record
|
|
119
|
+
client.delete('session', id='<session-uuid>')
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Command-line Interface
|
|
123
|
+
|
|
124
|
+
After installation a `brainstem` command is available in your shell.
|
|
125
|
+
|
|
126
|
+
### Authentication
|
|
127
|
+
```bash
|
|
128
|
+
# Authenticate (opens browser) and cache token
|
|
129
|
+
brainstem login
|
|
130
|
+
|
|
131
|
+
# Headless — prints URL + code instead of opening browser
|
|
132
|
+
brainstem login --headless
|
|
133
|
+
|
|
134
|
+
# Connect to a local dev server
|
|
135
|
+
brainstem login --url http://127.0.0.1:8000/
|
|
136
|
+
|
|
137
|
+
# Remove cached token
|
|
138
|
+
brainstem logout
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Loading data
|
|
142
|
+
```bash
|
|
143
|
+
# Load all sessions (private portal)
|
|
144
|
+
brainstem load session
|
|
145
|
+
|
|
146
|
+
# Filter, sort and embed related data
|
|
147
|
+
brainstem load session --filters name.icontains=Rat --sort -name --include projects
|
|
148
|
+
|
|
149
|
+
# Load a single record by UUID
|
|
150
|
+
brainstem load session --id <uuid>
|
|
151
|
+
|
|
152
|
+
# Manual pagination
|
|
153
|
+
brainstem load session --limit 20 --offset 20
|
|
154
|
+
|
|
155
|
+
# Public portal
|
|
156
|
+
brainstem load project --portal public
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Creating and updating records
|
|
160
|
+
```bash
|
|
161
|
+
# Create a new session
|
|
162
|
+
brainstem save session --data '{"name":"New session","projects":["<uuid>"]}'
|
|
163
|
+
|
|
164
|
+
# Update an existing record
|
|
165
|
+
brainstem save session --id <uuid> --data '{"description":"updated"}'
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Deleting records
|
|
169
|
+
```bash
|
|
170
|
+
brainstem delete session --id <uuid>
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
All subcommands accept `--token`, `--headless`, and `--url` to override defaults.
|
|
174
|
+
|
|
175
|
+
## Contributing
|
|
176
|
+
Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
This project is licensed under the MIT License. See the `LICENSE` file for more details.
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# BrainSTEM Python API Tools
|
|
2
|
+
|
|
3
|
+
The `brainstem_python_api_tools` is a Python toolset for interacting with the [BrainSTEM](https://www.brainstem.org/) API, designed for researchers and developers working with neuroscience data.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
You can install the package using `pip`:
|
|
7
|
+
```bash
|
|
8
|
+
pip install brainstem_python_api_tools
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Authentication
|
|
12
|
+
|
|
13
|
+
Authentication uses a **browser-based device authorization flow** that supports two-factor authentication (2FA). No credentials are entered into the tool itself.
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from brainstem_api_tools import BrainstemClient
|
|
17
|
+
|
|
18
|
+
# First run: opens a browser window for secure login.
|
|
19
|
+
# The token is cached at ~/.config/brainstem/token and reused automatically.
|
|
20
|
+
client = BrainstemClient()
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
To skip the browser flow, pass a token directly:
|
|
24
|
+
```python
|
|
25
|
+
client = BrainstemClient(token="YOUR_TOKEN")
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
For headless environments (no browser available):
|
|
29
|
+
```python
|
|
30
|
+
client = BrainstemClient(headless=True) # prints a URL + code to enter manually
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
To connect to a different server (e.g. a local development instance):
|
|
34
|
+
```python
|
|
35
|
+
client = BrainstemClient(url="http://127.0.0.1:8000/")
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
> **Security note:** The cached token file is stored with owner-read-only permissions (`0600`). Treat it like a password and do not commit it to version control.
|
|
39
|
+
|
|
40
|
+
## Getting Started
|
|
41
|
+
To get started with the BrainSTEM API tools, please refer to the tutorial script provided:
|
|
42
|
+
|
|
43
|
+
- **Tutorial:** `brainstem_api_tutorial.py`
|
|
44
|
+
|
|
45
|
+
The tutorial demonstrates how to:
|
|
46
|
+
|
|
47
|
+
- **Authenticate:** Load the client and authenticate via your browser.
|
|
48
|
+
- **Load Data:** Load sessions and filter data using flexible options.
|
|
49
|
+
- **Paginate:** Retrieve large datasets using `limit` / `offset` or `load_all=True`.
|
|
50
|
+
- **Convenience Loaders:** Use `load_session()`, `load_subject()`, etc. for common queries.
|
|
51
|
+
- **Update Entries:** Modify existing records and update them in the database.
|
|
52
|
+
- **Create Entries:** Submit new data entries with required fields.
|
|
53
|
+
- **Delete Entries:** Remove records by ID.
|
|
54
|
+
- **Load Public Data:** Access public projects and data using the public portal.
|
|
55
|
+
|
|
56
|
+
## Example Usage
|
|
57
|
+
```python
|
|
58
|
+
from brainstem_api_tools import BrainstemClient
|
|
59
|
+
|
|
60
|
+
client = BrainstemClient()
|
|
61
|
+
|
|
62
|
+
# Load sessions sorted by name
|
|
63
|
+
response = client.load('session', sort=['name'])
|
|
64
|
+
print(response.json())
|
|
65
|
+
|
|
66
|
+
# Filter and include related data
|
|
67
|
+
response = client.load(
|
|
68
|
+
'session',
|
|
69
|
+
filters={'name.icontains': 'Rat'},
|
|
70
|
+
sort=['-name'],
|
|
71
|
+
include=['projects'],
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Paginate results (manual)
|
|
75
|
+
page2 = client.load('session', limit=20, offset=20).json()
|
|
76
|
+
|
|
77
|
+
# Auto-paginate — fetches every page and returns a merged dict
|
|
78
|
+
all_sessions = client.load('session', load_all=True)
|
|
79
|
+
|
|
80
|
+
# Convenience loaders — sensible defaults with named filter kwargs
|
|
81
|
+
# load_session embeds dataacquisition, behaviors, manipulations, epochs
|
|
82
|
+
sessions = client.load_session(name='Rat', load_all=True)
|
|
83
|
+
|
|
84
|
+
# load_subject embeds procedures and subjectlogs
|
|
85
|
+
subjects = client.load_subject(sex='M', projects='<project-uuid>', load_all=True)
|
|
86
|
+
|
|
87
|
+
# load_project embeds sessions, subjects, collections, cohorts
|
|
88
|
+
projects = client.load_project(name='MyProject')
|
|
89
|
+
|
|
90
|
+
# load_behavior / load_dataacquisition / load_manipulation scope by session UUID
|
|
91
|
+
behaviors = client.load_behavior(session='<session-uuid>', load_all=True)
|
|
92
|
+
|
|
93
|
+
# load_procedure scopes by subject UUID
|
|
94
|
+
procedures = client.load_procedure(subject='<subject-uuid>', load_all=True)
|
|
95
|
+
|
|
96
|
+
# Create a record
|
|
97
|
+
client.save('session', data={'name': 'New session', 'projects': ['<project-uuid>']})
|
|
98
|
+
|
|
99
|
+
# Update a record
|
|
100
|
+
client.save('session', id='<session-uuid>', data={'description': 'updated'})
|
|
101
|
+
|
|
102
|
+
# Delete a record
|
|
103
|
+
client.delete('session', id='<session-uuid>')
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Command-line Interface
|
|
107
|
+
|
|
108
|
+
After installation a `brainstem` command is available in your shell.
|
|
109
|
+
|
|
110
|
+
### Authentication
|
|
111
|
+
```bash
|
|
112
|
+
# Authenticate (opens browser) and cache token
|
|
113
|
+
brainstem login
|
|
114
|
+
|
|
115
|
+
# Headless — prints URL + code instead of opening browser
|
|
116
|
+
brainstem login --headless
|
|
117
|
+
|
|
118
|
+
# Connect to a local dev server
|
|
119
|
+
brainstem login --url http://127.0.0.1:8000/
|
|
120
|
+
|
|
121
|
+
# Remove cached token
|
|
122
|
+
brainstem logout
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Loading data
|
|
126
|
+
```bash
|
|
127
|
+
# Load all sessions (private portal)
|
|
128
|
+
brainstem load session
|
|
129
|
+
|
|
130
|
+
# Filter, sort and embed related data
|
|
131
|
+
brainstem load session --filters name.icontains=Rat --sort -name --include projects
|
|
132
|
+
|
|
133
|
+
# Load a single record by UUID
|
|
134
|
+
brainstem load session --id <uuid>
|
|
135
|
+
|
|
136
|
+
# Manual pagination
|
|
137
|
+
brainstem load session --limit 20 --offset 20
|
|
138
|
+
|
|
139
|
+
# Public portal
|
|
140
|
+
brainstem load project --portal public
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Creating and updating records
|
|
144
|
+
```bash
|
|
145
|
+
# Create a new session
|
|
146
|
+
brainstem save session --data '{"name":"New session","projects":["<uuid>"]}'
|
|
147
|
+
|
|
148
|
+
# Update an existing record
|
|
149
|
+
brainstem save session --id <uuid> --data '{"description":"updated"}'
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Deleting records
|
|
153
|
+
```bash
|
|
154
|
+
brainstem delete session --id <uuid>
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
All subcommands accept `--token`, `--headless`, and `--url` to override defaults.
|
|
158
|
+
|
|
159
|
+
## Contributing
|
|
160
|
+
Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
This project is licensed under the MIT License. See the `LICENSE` file for more details.
|