|
265 | 265 | "cell_type": "markdown", |
266 | 266 | "metadata": {}, |
267 | 267 | "source": [ |
268 | | - "# 6. Including Related Models\n", |
269 | | - "\n", |
270 | | - "Brainstem's data models have relationships between them (projects contain sessions, sessions involve subjects, etc.). The include parameter lets you fetch related models in a single request, reducing the number of API calls needed. For example, you can retrieve a project along with all its sessions and subjects in one operation" |
271 | | - ] |
272 | | - }, |
273 | | - { |
274 | | - "cell_type": "code", |
275 | | - "execution_count": null, |
276 | | - "metadata": {}, |
277 | | - "outputs": [], |
278 | | - "source": [ |
279 | | - "# Find the Allen Institute: Visual Coding – Neuropixels project\n", |
280 | | - "allen_projects = client.load_model(\n", |
281 | | - " \"project\", \n", |
282 | | - " portal=\"public\", \n", |
283 | | - " filters={'name.iexact': 'Allen Institute: Visual Coding – Neuropixels'}\n", |
284 | | - ").json()\n", |
285 | | - "\n", |
286 | | - "# Get project ID\n", |
287 | | - "project_id = allen_projects['projects'][0]['id']\n", |
288 | | - "\n", |
289 | | - "# Get detailed project information with sessions and subjects included\n", |
290 | | - "project_with_data = client.load_model(\n", |
291 | | - " \"project\",\n", |
292 | | - " portal=\"public\",\n", |
293 | | - " id=project_id,\n", |
294 | | - " include=[\"sessions\", \"subjects\"] # Include both sessions and subjects\n", |
295 | | - ").json()\n", |
296 | | - "\n", |
297 | | - "# Access project information\n", |
298 | | - "project = project_with_data['project']\n", |
299 | | - "print(f\"Project name: {project['name']}\")\n", |
300 | | - "print(f\"Description: {project['description'][:100]}...\")\n", |
301 | | - "print(f\"Public: {project['is_public']}\")\n", |
302 | | - "\n", |
303 | | - "# Print session and subject counts. We have this data from a single API call\n", |
304 | | - "print(f\"\\nThis project has {len(project['sessions'])} sessions\")\n", |
305 | | - "print(f\"This project has {len(project['subjects'])} subjects\")\n", |
306 | | - "\n", |
307 | | - "# List first 3 subjects directly from the include\n", |
308 | | - "print(\"\\nFirst 3 subjects in the project:\")\n", |
309 | | - "for i, subject_id in enumerate(project['subjects'][:3]):\n", |
310 | | - " print(f\"{i+1}. Subject ID: {subject_id}\")\n", |
311 | | - "\n", |
312 | | - "# List first 3 sessions directly from the include\n", |
313 | | - "print(\"\\nFirst 3 sessions in the project:\")\n", |
314 | | - "for i, session_id in enumerate(project['sessions'][:3]):\n", |
315 | | - " print(f\"{i+1}. Session ID: {session_id}\")\n", |
316 | | - "\n", |
317 | | - "# Get details for a specific session with included dataacquisition\n", |
318 | | - "first_session_id = project['sessions'][0]\n", |
319 | | - "session_with_data = client.load_model(\n", |
320 | | - " \"session\",\n", |
321 | | - " portal=\"public\",\n", |
322 | | - " id=first_session_id,\n", |
323 | | - " include=[\"dataacquisition\"] # Include data acquisition methods\n", |
324 | | - ").json()\n", |
325 | | - "\n", |
326 | | - "# Access the session data\n", |
327 | | - "session = session_with_data['session']\n", |
328 | | - "print(f\"\\nSession details:\")\n", |
329 | | - "print(f\"Name: {session['name']}\")\n", |
330 | | - "print(f\"Description: {session['description']}\")\n", |
331 | | - "\n", |
332 | | - "# Now actually use the included dataacquisition data\n", |
333 | | - "if 'dataacquisition' in session:\n", |
334 | | - " data_acq_ids = session['dataacquisition']\n", |
335 | | - " print(f\"\\nThis session has {len(data_acq_ids)} data acquisition method(s) (included in response)\")\n", |
336 | | - " \n", |
337 | | - " # Demonstrate using the included dataacquisition IDs\n", |
338 | | - " if data_acq_ids:\n", |
339 | | - " print(\"\\nData acquisition IDs from session include:\")\n", |
340 | | - " for i, acq_id in enumerate(data_acq_ids):\n", |
341 | | - " print(f\"{i+1}. {acq_id}\")" |
342 | | - ] |
343 | | - }, |
344 | | - { |
345 | | - "cell_type": "markdown", |
346 | | - "metadata": {}, |
347 | | - "source": [ |
348 | | - "# 7. Sorting\n", |
| 268 | + "# 6. Sorting\n", |
349 | 269 | "\n", |
350 | 270 | "You can control the order of returned results using the sort parameter. Sort by any field in ascending order (e.g., alphabetically by name) or use a minus sign prefix for descending order." |
351 | 271 | ] |
|
383 | 303 | "cell_type": "markdown", |
384 | 304 | "metadata": {}, |
385 | 305 | "source": [ |
386 | | - "# 8. Updating data\n", |
| 306 | + "# 7. Updating data\n", |
387 | 307 | "The API supports creating new records and updating existing ones. When creating a record, provide the required fields for that model type. For updates, fetch the existing record, modify its attributes, and save it back. Both operations require appropriate permissions." |
388 | 308 | ] |
389 | 309 | }, |
|
447 | 367 | "cell_type": "markdown", |
448 | 368 | "metadata": {}, |
449 | 369 | "source": [ |
450 | | - "# 9. Deleting data\n", |
| 370 | + "# 8. Deleting data\n", |
451 | 371 | "You can remove records from the database using their IDs. Deletion is permanent, so use this operation with caution. The API will return a success status code (204) when deletion is successful. Like other write operations, deletion requires appropriate permissions." |
452 | 372 | ] |
453 | 373 | }, |
|
0 commit comments