brainstem-python-api-tools 2.0.0__tar.gz → 2.1.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.
Files changed (19) hide show
  1. brainstem_python_api_tools-2.1.0/PKG-INFO +330 -0
  2. brainstem_python_api_tools-2.1.0/README.md +314 -0
  3. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/brainstem_api_tools/__init__.py +2 -2
  4. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/brainstem_api_tools/brainstem_api_client.py +489 -144
  5. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/brainstem_api_tools/cli.py +90 -15
  6. brainstem_python_api_tools-2.1.0/brainstem_python_api_tools.egg-info/PKG-INFO +330 -0
  7. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/pyproject.toml +1 -1
  8. brainstem_python_api_tools-2.1.0/tests/test_client.py +1369 -0
  9. brainstem_python_api_tools-2.0.0/PKG-INFO +0 -179
  10. brainstem_python_api_tools-2.0.0/README.md +0 -163
  11. brainstem_python_api_tools-2.0.0/brainstem_python_api_tools.egg-info/PKG-INFO +0 -179
  12. brainstem_python_api_tools-2.0.0/tests/test_client.py +0 -729
  13. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/LICENSE +0 -0
  14. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/brainstem_python_api_tools.egg-info/SOURCES.txt +0 -0
  15. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/brainstem_python_api_tools.egg-info/dependency_links.txt +0 -0
  16. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/brainstem_python_api_tools.egg-info/entry_points.txt +0 -0
  17. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/brainstem_python_api_tools.egg-info/requires.txt +0 -0
  18. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/brainstem_python_api_tools.egg-info/top_level.txt +0 -0
  19. {brainstem_python_api_tools-2.0.0 → brainstem_python_api_tools-2.1.0}/setup.cfg +0 -0
@@ -0,0 +1,330 @@
1
+ Metadata-Version: 2.4
2
+ Name: brainstem_python_api_tools
3
+ Version: 2.1.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 read public data without logging in (no token is sent; only `portal='public'` works):
50
+ ```python
51
+ client = BrainstemClient(anonymous=True)
52
+ client.load("project", portal="public", load_all=True)
53
+ ```
54
+
55
+ To connect to a different server (e.g. a local development instance):
56
+ ```python
57
+ client = BrainstemClient(url="http://127.0.0.1:8000/")
58
+ ```
59
+
60
+ The client and the CLI also read two environment variables when no `token` / `url` is given:
61
+
62
+ - `BRAINSTEM_API_TOKEN`: API token, used before the cached token.
63
+ - `BRAINSTEM_API_URL`: server base URL (default `https://www.brainstem.org/`).
64
+
65
+ > **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.
66
+
67
+ ## Getting Started
68
+ To get started with the BrainSTEM API tools, please refer to the tutorial script provided:
69
+
70
+ - **Tutorial:** `brainstem_api_tutorial.py`
71
+
72
+ The tutorial demonstrates how to:
73
+
74
+ - **Authenticate:** Load the client and authenticate via your browser.
75
+ - **Load Data:** Load sessions and filter data using flexible options.
76
+ - **Paginate:** Retrieve large datasets page by page (`limit` / `offset`) or all at once (`load_all=True`).
77
+ - **Convenience Loaders:** Use `load_session()`, `load_subject()`, etc. for common queries.
78
+ - **Update Entries:** Modify existing records and update them in the database.
79
+ - **Create Entries:** Submit new data entries with required fields.
80
+ - **Delete Entries:** Remove records by ID.
81
+ - **Load Public Data:** Access public projects and data using the public portal.
82
+
83
+ ## Example Usage
84
+ ```python
85
+ from brainstem_api_tools import BrainstemClient
86
+
87
+ client = BrainstemClient()
88
+
89
+ # Load sessions sorted by name
90
+ response = client.load('session', sort=['name'])
91
+ print(response.json())
92
+
93
+ # Filter and include related data
94
+ response = client.load(
95
+ 'session',
96
+ filters={'name.icontains': 'Rat'},
97
+ sort=['-name'],
98
+ include=['projects'],
99
+ )
100
+
101
+ # Paginate results (manual). The API pages with page/per_page: limit is sent
102
+ # as per_page (1 to 500, default 100) and offset is converted to a page number,
103
+ # so offset must be a multiple of limit. Here: page 2 of 20 records.
104
+ page2 = client.load('session', limit=20, offset=20).json()
105
+ print(page2['meta']) # {'page': 2, 'per_page': 20, 'total_results': ..., 'total_pages': ...}
106
+
107
+ # Auto-paginate — fetches every page and returns a merged dict. Records of
108
+ # sideloaded relations (include=[...]) are merged per key as well.
109
+ all_sessions = client.load('session', load_all=True)
110
+
111
+ # Convenience loaders — sensible defaults with named filter kwargs. Their
112
+ # filters send the bare field (filter{projects}=<uuid>), which every server accepts.
113
+ # load_session sideloads dataacquisition, behaviors, manipulations, epochs
114
+ sessions = client.load_session(name='Rat', load_all=True)
115
+
116
+ # load_subject sideloads procedures and subjectlogs
117
+ subjects = client.load_subject(sex='M', projects='<project-uuid>', load_all=True)
118
+
119
+ # load_project sideloads sessions, subjects, collections, cohorts
120
+ projects = client.load_project(name='MyProject')
121
+
122
+ # load_behavior / load_dataacquisition / load_manipulation scope by session UUID
123
+ behaviors = client.load_behavior(session='<session-uuid>', load_all=True)
124
+
125
+ # load_procedure / load_subjectlog scope by subject UUID; load_procedurelog by
126
+ # procedure, load_equipment by setup and load_strain by species UUID.
127
+ procedures = client.load_procedure(subject='<subject-uuid>', load_all=True)
128
+
129
+ # Create a record
130
+ client.save('session', data={'name': 'New session', 'projects': ['<project-uuid>']})
131
+
132
+ # Update a record
133
+ client.save('session', id='<session-uuid>', data={'description': 'updated'})
134
+
135
+ # Sending a list field replaces the whole list. Lists read with include (and
136
+ # the convenience loaders' default includes) hold only records you may read,
137
+ # so read the record without include before changing one:
138
+ session = client.load('session', id='<session-uuid>').json()['session']
139
+ client.save('session', id='<session-uuid>',
140
+ data={'projects': session['projects'] + ['<project-uuid>']})
141
+
142
+ # Delete a record
143
+ client.delete('session', id='<session-uuid>')
144
+ ```
145
+
146
+ ### Specimens, interfaces and materials
147
+
148
+ Specimen sets and specimens (`stem`), experimental interfaces, material usages and specimen procedures/logs (`modules`), and anatomical regions (`taxonomies`) load like any other model:
149
+
150
+ ```python
151
+ specimen_sets = client.load('specimenset', filters={'projects': '<project-uuid>'}, load_all=True)
152
+ interfaces = client.load('experimentalinterface', load_all=True)
153
+ ```
154
+
155
+ ### Species, strains and brain regions
156
+
157
+ ```python
158
+ # Search species and strains by common name as well as scientific name
159
+ mice = client.load_species(popular_name='mouse').json()
160
+ strains = client.load_strain(popular_name='black 6').json()
161
+
162
+ # Brain regions are anatomical regions; find one by acronym, then list its subregions
163
+ ca1 = client.load_anatomicalregion(acronym='CA1').json()['anatomical_regions'][0]
164
+ subregions = client.load_anatomicalregion_descendants(ca1['id']).json()['anatomical_regions']
165
+ for region in subregions:
166
+ print(region['depth'], region['acronym'], region['name'])
167
+ ```
168
+
169
+ `brainregion` and `load_brainregion()` are deprecated names for `anatomicalregion` and `load_anatomicalregion()`. They still work, with a `FutureWarning`, and load anatomical regions.
170
+
171
+ ### Known limitations
172
+
173
+ - Specimen-era models (specimen sets, specimens, experimental interfaces, material usages, anatomical regions, ...), `popular_name` and region descendants need a server with the specimen release. Older servers answer 404 for those models and 400 for a `popular_name` filter.
174
+ - `offset` must be a multiple of the page size (`limit`, default 100), and a page holds at most 500 records. Use `load_all=True` to get everything.
175
+ - Lists read with `include` show only the related records you may read; see the note on `save` above.
176
+
177
+ ## Exporting projects and sessions
178
+
179
+ `export_project()` returns a whole project as one JSON-serializable dict. It is the same file as the JSON download on the BrainSTEM website (**Project → Download → JSON**). Use it for backups, NWB conversion, external integrations or analysis pipelines. You need read access to the project.
180
+
181
+ ```python
182
+ import json
183
+
184
+ export = client.export_project('<project-uuid>')
185
+
186
+ print(export['export_info']['format_version'], export['export_info']['counts'])
187
+ for record in export['records']['stem.session']:
188
+ print(record['pk'], record['fields']['name'])
189
+
190
+ with open('project_backup.json', 'w') as f:
191
+ json.dump(export, f)
192
+ ```
193
+
194
+ ### Session exports
195
+
196
+ `export_session()` exports one session and everything it touches: the content of one NWB file. You need read access to one of the session's projects.
197
+
198
+ ```python
199
+ export = client.export_session('<session-uuid>')
200
+ ```
201
+
202
+ The file has the same format as a project export, with `export_info['scope'] == 'session'`. It contains:
203
+
204
+ - the session with its data acquisitions, manipulations, behaviors and epochs;
205
+ - the subjects and specimen sets linked to the session, with their full procedure and log history;
206
+ - the session's projects you can read (`export_info['project_ids']`; a session can belong to several);
207
+ - the lab resources, catalog entries and users these records reference.
208
+
209
+ A subject's procedure history is complete, including procedures after the session, so filter by date where that matters.
210
+
211
+ ### Large exports
212
+
213
+ Large exports can take longer than the server's 30-second request limit. For those, pass `background=True`: the server builds the export as a job, the client waits for it and downloads the result, and you also get an email when it is ready.
214
+
215
+ ```python
216
+ export = client.export_project('<project-uuid>', background=True)
217
+ export = client.export_session('<session-uuid>', background=True)
218
+ ```
219
+
220
+ ### Format
221
+
222
+ Both exports have this structure:
223
+
224
+ ```text
225
+ {
226
+ "export_info": {
227
+ "format_version": "2.1",
228
+ "exported_at": "<ISO 8601 UTC timestamp>",
229
+ "scope": "project", # or "session"
230
+ "project_id": "<project uuid>", # null for a session export
231
+ "project_ids": ["<project uuid>", ...],
232
+ "session_id": null, # the session's uuid for a session export
233
+ "lab_resources": "<which lab resources are included>",
234
+ "restricted": {"personal_attributes.datastorage": [{"pk": "<uuid>", "name": "Lab server"}], ...},
235
+ "record_count": 2344,
236
+ "counts": {"stem.project": 1, "stem.subject": 21, ...}
237
+ },
238
+ "records": {
239
+ "stem.project": [{"model": "stem.project", "pk": "<uuid>", "fields": {...}}],
240
+ "stem.subject": [...],
241
+ "stem.session": [...],
242
+ ...
243
+ }
244
+ }
245
+ ```
246
+
247
+ - `records` is keyed by model label (`app_label.model_name`). Every label is always present, with an empty list when there are no such records.
248
+ - Links to other records are primary keys. Many-to-many links are sorted lists of primary keys. Referenced catalog entries (species, brain regions, suppliers, ...) and users (names only) are included in the file.
249
+ - Lab resources (setups, data storages, equipment, protocols, ...) are included when you can read them and the exported records use them. Two people exporting the same project can therefore get different lab resources. Referenced lab resources you cannot read are listed by name in `export_info['restricted']`.
250
+ - A minor format version (2.1, 2.2, ...) only adds labels and keys, so ignore any you don't recognise.
251
+
252
+ Problems raise `ExportError` with a readable message, for example when the project or session doesn't exist or you can't read it, a background job fails, or the server doesn't support exports yet.
253
+
254
+ ## Agent reference
255
+
256
+ The server describes its own API and data model (resources, fields, relationships, JSON schemas, guides) at
257
+ `/reference/`. It is written for AI agents, public, and needs no token:
258
+
259
+ ```python
260
+ print(client.agent_reference()) # index of all pages
261
+ print(client.agent_reference("resources/session.md")) # one resource
262
+ print(client.agent_reference("guides/query-planning.md")) # a guide
263
+ ```
264
+
265
+ ```bash
266
+ brainstem reference rules.md
267
+ ```
268
+
269
+ ## Command-line Interface
270
+
271
+ After installation a `brainstem` command is available in your shell.
272
+
273
+ ### Authentication
274
+ ```bash
275
+ # Authenticate (opens browser) and cache token
276
+ brainstem login
277
+
278
+ # Headless — prints URL + code instead of opening browser
279
+ brainstem login --headless
280
+
281
+ # Connect to a local dev server
282
+ brainstem login --url http://127.0.0.1:8000/
283
+
284
+ # Remove cached token
285
+ brainstem logout
286
+ ```
287
+
288
+ ### Loading data
289
+ ```bash
290
+ # Load all sessions (private portal)
291
+ brainstem load session
292
+
293
+ # Filter, sort and embed related data
294
+ brainstem load session --filters name.icontains=Rat --sort -name --include projects
295
+
296
+ # Load a single record by UUID
297
+ brainstem load session --id <uuid>
298
+
299
+ # Manual pagination (page 2, 20 records per page)
300
+ brainstem load session --per-page 20 --page 2
301
+ brainstem load session --limit 20 --offset 20 # equivalent
302
+
303
+ # Fetch every page and print one merged JSON object
304
+ brainstem load session --all
305
+
306
+ # Public portal
307
+ brainstem load project --portal public
308
+ ```
309
+
310
+ ### Creating and updating records
311
+ ```bash
312
+ # Create a new session
313
+ brainstem save session --data '{"name":"New session","projects":["<uuid>"]}'
314
+
315
+ # Update an existing record
316
+ brainstem save session --id <uuid> --data '{"description":"updated"}'
317
+ ```
318
+
319
+ ### Deleting records
320
+ ```bash
321
+ brainstem delete session --id <uuid>
322
+ ```
323
+
324
+ All subcommands accept `--token`, `--headless`, and `--url` to override defaults (`--token` and `--url` default to `$BRAINSTEM_API_TOKEN` and `$BRAINSTEM_API_URL`). `brainstem login` never prints the token; it reports where it is cached.
325
+
326
+ ## Contributing
327
+ Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
328
+
329
+ ## License
330
+ This project is licensed under the MIT License. See the `LICENSE` file for more details.
@@ -0,0 +1,314 @@
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 read public data without logging in (no token is sent; only `portal='public'` works):
34
+ ```python
35
+ client = BrainstemClient(anonymous=True)
36
+ client.load("project", portal="public", load_all=True)
37
+ ```
38
+
39
+ To connect to a different server (e.g. a local development instance):
40
+ ```python
41
+ client = BrainstemClient(url="http://127.0.0.1:8000/")
42
+ ```
43
+
44
+ The client and the CLI also read two environment variables when no `token` / `url` is given:
45
+
46
+ - `BRAINSTEM_API_TOKEN`: API token, used before the cached token.
47
+ - `BRAINSTEM_API_URL`: server base URL (default `https://www.brainstem.org/`).
48
+
49
+ > **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.
50
+
51
+ ## Getting Started
52
+ To get started with the BrainSTEM API tools, please refer to the tutorial script provided:
53
+
54
+ - **Tutorial:** `brainstem_api_tutorial.py`
55
+
56
+ The tutorial demonstrates how to:
57
+
58
+ - **Authenticate:** Load the client and authenticate via your browser.
59
+ - **Load Data:** Load sessions and filter data using flexible options.
60
+ - **Paginate:** Retrieve large datasets page by page (`limit` / `offset`) or all at once (`load_all=True`).
61
+ - **Convenience Loaders:** Use `load_session()`, `load_subject()`, etc. for common queries.
62
+ - **Update Entries:** Modify existing records and update them in the database.
63
+ - **Create Entries:** Submit new data entries with required fields.
64
+ - **Delete Entries:** Remove records by ID.
65
+ - **Load Public Data:** Access public projects and data using the public portal.
66
+
67
+ ## Example Usage
68
+ ```python
69
+ from brainstem_api_tools import BrainstemClient
70
+
71
+ client = BrainstemClient()
72
+
73
+ # Load sessions sorted by name
74
+ response = client.load('session', sort=['name'])
75
+ print(response.json())
76
+
77
+ # Filter and include related data
78
+ response = client.load(
79
+ 'session',
80
+ filters={'name.icontains': 'Rat'},
81
+ sort=['-name'],
82
+ include=['projects'],
83
+ )
84
+
85
+ # Paginate results (manual). The API pages with page/per_page: limit is sent
86
+ # as per_page (1 to 500, default 100) and offset is converted to a page number,
87
+ # so offset must be a multiple of limit. Here: page 2 of 20 records.
88
+ page2 = client.load('session', limit=20, offset=20).json()
89
+ print(page2['meta']) # {'page': 2, 'per_page': 20, 'total_results': ..., 'total_pages': ...}
90
+
91
+ # Auto-paginate — fetches every page and returns a merged dict. Records of
92
+ # sideloaded relations (include=[...]) are merged per key as well.
93
+ all_sessions = client.load('session', load_all=True)
94
+
95
+ # Convenience loaders — sensible defaults with named filter kwargs. Their
96
+ # filters send the bare field (filter{projects}=<uuid>), which every server accepts.
97
+ # load_session sideloads dataacquisition, behaviors, manipulations, epochs
98
+ sessions = client.load_session(name='Rat', load_all=True)
99
+
100
+ # load_subject sideloads procedures and subjectlogs
101
+ subjects = client.load_subject(sex='M', projects='<project-uuid>', load_all=True)
102
+
103
+ # load_project sideloads 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 / load_subjectlog scope by subject UUID; load_procedurelog by
110
+ # procedure, load_equipment by setup and load_strain by species UUID.
111
+ procedures = client.load_procedure(subject='<subject-uuid>', load_all=True)
112
+
113
+ # Create a record
114
+ client.save('session', data={'name': 'New session', 'projects': ['<project-uuid>']})
115
+
116
+ # Update a record
117
+ client.save('session', id='<session-uuid>', data={'description': 'updated'})
118
+
119
+ # Sending a list field replaces the whole list. Lists read with include (and
120
+ # the convenience loaders' default includes) hold only records you may read,
121
+ # so read the record without include before changing one:
122
+ session = client.load('session', id='<session-uuid>').json()['session']
123
+ client.save('session', id='<session-uuid>',
124
+ data={'projects': session['projects'] + ['<project-uuid>']})
125
+
126
+ # Delete a record
127
+ client.delete('session', id='<session-uuid>')
128
+ ```
129
+
130
+ ### Specimens, interfaces and materials
131
+
132
+ Specimen sets and specimens (`stem`), experimental interfaces, material usages and specimen procedures/logs (`modules`), and anatomical regions (`taxonomies`) load like any other model:
133
+
134
+ ```python
135
+ specimen_sets = client.load('specimenset', filters={'projects': '<project-uuid>'}, load_all=True)
136
+ interfaces = client.load('experimentalinterface', load_all=True)
137
+ ```
138
+
139
+ ### Species, strains and brain regions
140
+
141
+ ```python
142
+ # Search species and strains by common name as well as scientific name
143
+ mice = client.load_species(popular_name='mouse').json()
144
+ strains = client.load_strain(popular_name='black 6').json()
145
+
146
+ # Brain regions are anatomical regions; find one by acronym, then list its subregions
147
+ ca1 = client.load_anatomicalregion(acronym='CA1').json()['anatomical_regions'][0]
148
+ subregions = client.load_anatomicalregion_descendants(ca1['id']).json()['anatomical_regions']
149
+ for region in subregions:
150
+ print(region['depth'], region['acronym'], region['name'])
151
+ ```
152
+
153
+ `brainregion` and `load_brainregion()` are deprecated names for `anatomicalregion` and `load_anatomicalregion()`. They still work, with a `FutureWarning`, and load anatomical regions.
154
+
155
+ ### Known limitations
156
+
157
+ - Specimen-era models (specimen sets, specimens, experimental interfaces, material usages, anatomical regions, ...), `popular_name` and region descendants need a server with the specimen release. Older servers answer 404 for those models and 400 for a `popular_name` filter.
158
+ - `offset` must be a multiple of the page size (`limit`, default 100), and a page holds at most 500 records. Use `load_all=True` to get everything.
159
+ - Lists read with `include` show only the related records you may read; see the note on `save` above.
160
+
161
+ ## Exporting projects and sessions
162
+
163
+ `export_project()` returns a whole project as one JSON-serializable dict. It is the same file as the JSON download on the BrainSTEM website (**Project → Download → JSON**). Use it for backups, NWB conversion, external integrations or analysis pipelines. You need read access to the project.
164
+
165
+ ```python
166
+ import json
167
+
168
+ export = client.export_project('<project-uuid>')
169
+
170
+ print(export['export_info']['format_version'], export['export_info']['counts'])
171
+ for record in export['records']['stem.session']:
172
+ print(record['pk'], record['fields']['name'])
173
+
174
+ with open('project_backup.json', 'w') as f:
175
+ json.dump(export, f)
176
+ ```
177
+
178
+ ### Session exports
179
+
180
+ `export_session()` exports one session and everything it touches: the content of one NWB file. You need read access to one of the session's projects.
181
+
182
+ ```python
183
+ export = client.export_session('<session-uuid>')
184
+ ```
185
+
186
+ The file has the same format as a project export, with `export_info['scope'] == 'session'`. It contains:
187
+
188
+ - the session with its data acquisitions, manipulations, behaviors and epochs;
189
+ - the subjects and specimen sets linked to the session, with their full procedure and log history;
190
+ - the session's projects you can read (`export_info['project_ids']`; a session can belong to several);
191
+ - the lab resources, catalog entries and users these records reference.
192
+
193
+ A subject's procedure history is complete, including procedures after the session, so filter by date where that matters.
194
+
195
+ ### Large exports
196
+
197
+ Large exports can take longer than the server's 30-second request limit. For those, pass `background=True`: the server builds the export as a job, the client waits for it and downloads the result, and you also get an email when it is ready.
198
+
199
+ ```python
200
+ export = client.export_project('<project-uuid>', background=True)
201
+ export = client.export_session('<session-uuid>', background=True)
202
+ ```
203
+
204
+ ### Format
205
+
206
+ Both exports have this structure:
207
+
208
+ ```text
209
+ {
210
+ "export_info": {
211
+ "format_version": "2.1",
212
+ "exported_at": "<ISO 8601 UTC timestamp>",
213
+ "scope": "project", # or "session"
214
+ "project_id": "<project uuid>", # null for a session export
215
+ "project_ids": ["<project uuid>", ...],
216
+ "session_id": null, # the session's uuid for a session export
217
+ "lab_resources": "<which lab resources are included>",
218
+ "restricted": {"personal_attributes.datastorage": [{"pk": "<uuid>", "name": "Lab server"}], ...},
219
+ "record_count": 2344,
220
+ "counts": {"stem.project": 1, "stem.subject": 21, ...}
221
+ },
222
+ "records": {
223
+ "stem.project": [{"model": "stem.project", "pk": "<uuid>", "fields": {...}}],
224
+ "stem.subject": [...],
225
+ "stem.session": [...],
226
+ ...
227
+ }
228
+ }
229
+ ```
230
+
231
+ - `records` is keyed by model label (`app_label.model_name`). Every label is always present, with an empty list when there are no such records.
232
+ - Links to other records are primary keys. Many-to-many links are sorted lists of primary keys. Referenced catalog entries (species, brain regions, suppliers, ...) and users (names only) are included in the file.
233
+ - Lab resources (setups, data storages, equipment, protocols, ...) are included when you can read them and the exported records use them. Two people exporting the same project can therefore get different lab resources. Referenced lab resources you cannot read are listed by name in `export_info['restricted']`.
234
+ - A minor format version (2.1, 2.2, ...) only adds labels and keys, so ignore any you don't recognise.
235
+
236
+ Problems raise `ExportError` with a readable message, for example when the project or session doesn't exist or you can't read it, a background job fails, or the server doesn't support exports yet.
237
+
238
+ ## Agent reference
239
+
240
+ The server describes its own API and data model (resources, fields, relationships, JSON schemas, guides) at
241
+ `/reference/`. It is written for AI agents, public, and needs no token:
242
+
243
+ ```python
244
+ print(client.agent_reference()) # index of all pages
245
+ print(client.agent_reference("resources/session.md")) # one resource
246
+ print(client.agent_reference("guides/query-planning.md")) # a guide
247
+ ```
248
+
249
+ ```bash
250
+ brainstem reference rules.md
251
+ ```
252
+
253
+ ## Command-line Interface
254
+
255
+ After installation a `brainstem` command is available in your shell.
256
+
257
+ ### Authentication
258
+ ```bash
259
+ # Authenticate (opens browser) and cache token
260
+ brainstem login
261
+
262
+ # Headless — prints URL + code instead of opening browser
263
+ brainstem login --headless
264
+
265
+ # Connect to a local dev server
266
+ brainstem login --url http://127.0.0.1:8000/
267
+
268
+ # Remove cached token
269
+ brainstem logout
270
+ ```
271
+
272
+ ### Loading data
273
+ ```bash
274
+ # Load all sessions (private portal)
275
+ brainstem load session
276
+
277
+ # Filter, sort and embed related data
278
+ brainstem load session --filters name.icontains=Rat --sort -name --include projects
279
+
280
+ # Load a single record by UUID
281
+ brainstem load session --id <uuid>
282
+
283
+ # Manual pagination (page 2, 20 records per page)
284
+ brainstem load session --per-page 20 --page 2
285
+ brainstem load session --limit 20 --offset 20 # equivalent
286
+
287
+ # Fetch every page and print one merged JSON object
288
+ brainstem load session --all
289
+
290
+ # Public portal
291
+ brainstem load project --portal public
292
+ ```
293
+
294
+ ### Creating and updating records
295
+ ```bash
296
+ # Create a new session
297
+ brainstem save session --data '{"name":"New session","projects":["<uuid>"]}'
298
+
299
+ # Update an existing record
300
+ brainstem save session --id <uuid> --data '{"description":"updated"}'
301
+ ```
302
+
303
+ ### Deleting records
304
+ ```bash
305
+ brainstem delete session --id <uuid>
306
+ ```
307
+
308
+ All subcommands accept `--token`, `--headless`, and `--url` to override defaults (`--token` and `--url` default to `$BRAINSTEM_API_TOKEN` and `$BRAINSTEM_API_URL`). `brainstem login` never prints the token; it reports where it is cached.
309
+
310
+ ## Contributing
311
+ Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
312
+
313
+ ## License
314
+ This project is licensed under the MIT License. See the `LICENSE` file for more details.
@@ -1,3 +1,3 @@
1
- from .brainstem_api_client import BrainstemClient, ModelType, PortalType, AuthenticationError
1
+ from .brainstem_api_client import BrainstemClient, ModelType, PortalType, AuthenticationError, ExportError
2
2
 
3
- __version__ = "2.0.0"
3
+ __version__ = "2.1.0"