Skip to content

Instantly share code, notes, and snippets.

@rly
Last active December 14, 2023 06:10
Show Gist options
  • Save rly/43f67f86c4bb3145efc785c01074bf1d to your computer and use it in GitHub Desktop.
Save rly/43f67f86c4bb3145efc785c01074bf1d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <style>\n",
" .container-fields {\n",
" font-family: \"Open Sans\", Arial, sans-serif;\n",
" }\n",
" .container-fields .field-value {\n",
" color: #00788E;\n",
" }\n",
" .container-fields details > summary {\n",
" cursor: pointer;\n",
" display: list-item;\n",
" }\n",
" .container-fields details > summary:hover {\n",
" color: #0A6EAA;\n",
" }\n",
" </style>\n",
" \n",
" <script>\n",
" function copyToClipboard(text) {\n",
" navigator.clipboard.writeText(text).then(function() {\n",
" console.log('Copied to clipboard: ' + text);\n",
" }, function(err) {\n",
" console.error('Could not copy text: ', err);\n",
" });\n",
" }\n",
"\n",
" document.addEventListener('DOMContentLoaded', function() {\n",
" let fieldKeys = document.querySelectorAll('.container-fields .field-key');\n",
" fieldKeys.forEach(function(fieldKey) {\n",
" fieldKey.addEventListener('click', function() {\n",
" let accessCode = fieldKey.getAttribute('title').replace('Access code: ', '');\n",
" copyToClipboard(accessCode);\n",
" });\n",
" });\n",
" });\n",
" </script>\n",
" <div class='container-wrap'><div class='container-header'><div class='xr-obj-type'><h3>ImageSegmentation</h3></div></div><details><summary style=\"display: list-item; margin-left: 0px;\" class=\"container-fields field-key\" title=\".plane_segmentations\"><b>plane_segmentations</b></summary><details><summary style=\"display: list-item; margin-left: 20px;\" class=\"container-fields field-key\" title=\".plane_segmentations['PlaneSegmentation']\"><b>PlaneSegmentation</b></summary><div style=\"margin-left: 40px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation']\">description: </span><span class=\"field-value\">output from segmenting my favorite imaging plane</span></div><details><summary style=\"display: list-item; margin-left: 40px;\" class=\"container-fields field-key\" title=\".plane_segmentations['PlaneSegmentation']\"><b>imaging_plane</b></summary><details><summary style=\"display: list-item; margin-left: 60px;\" class=\"container-fields field-key\" title=\".plane_segmentations['PlaneSegmentation'].optical_channel\"><b>optical_channel</b></summary><details><summary style=\"display: list-item; margin-left: 80px;\" class=\"container-fields field-key\" title=\".plane_segmentations['PlaneSegmentation'].optical_channel[0]\"><b>0</b></summary><div style=\"margin-left: 100px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].optical_channel[0].description\">description: </span><span class=\"field-value\">an optical channel</span></div><div style=\"margin-left: 100px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].optical_channel[0].emission_lambda\">emission_lambda: </span><span class=\"field-value\">500.0</span></div></details></details><div style=\"margin-left: 60px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].description\">description: </span><span class=\"field-value\">a very interesting part of the brain</span></div><details><summary style=\"display: list-item; margin-left: 60px;\" class=\"container-fields field-key\" title=\".plane_segmentations['PlaneSegmentation'].device\"><b>device</b></summary><div style=\"margin-left: 80px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].device.description\">description: </span><span class=\"field-value\">My two-photon microscope</span></div><div style=\"margin-left: 80px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].device.manufacturer\">manufacturer: </span><span class=\"field-value\">The best microscope manufacturer</span></div></details><div style=\"margin-left: 60px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].excitation_lambda\">excitation_lambda: </span><span class=\"field-value\">600.0</span></div><div style=\"margin-left: 60px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].imaging_rate\">imaging_rate: </span><span class=\"field-value\">30.0</span></div><div style=\"margin-left: 60px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].indicator\">indicator: </span><span class=\"field-value\">GFP</span></div><div style=\"margin-left: 60px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].location\">location: </span><span class=\"field-value\">V1</span></div><div style=\"margin-left: 60px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].conversion\">conversion: </span><span class=\"field-value\">1.0</span></div><div style=\"margin-left: 60px;\" class=\"container-fields\"><span class=\"field-key\" title=\".plane_segmentations['PlaneSegmentation'].unit\">unit: </span><span class=\"field-value\">meters</span></div></details><details><summary style=\"display: list-item; margin-left: 40px;\" class=\"container-fields field-key\" title=\".plane_segmentations['PlaneSegmentation']\"><b>table</b></summary><table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" </tr>\n",
" <tr>\n",
" <th>id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" </tbody>\n",
"</table></details></details></details></div>"
],
"text/plain": [
"ImageSegmentation pynwb.ophys.ImageSegmentation at 0x5760727760\n",
"Fields:\n",
" plane_segmentations: {\n",
" PlaneSegmentation <class 'pynwb.ophys.PlaneSegmentation'>\n",
" }"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datetime import datetime\n",
"from uuid import uuid4\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from dateutil.tz import tzlocal\n",
"\n",
"from pynwb import NWBHDF5IO, NWBFile, TimeSeries\n",
"from pynwb.image import ImageSeries\n",
"from pynwb.ophys import (\n",
" CorrectedImageStack,\n",
" Fluorescence,\n",
" ImageSegmentation,\n",
" MotionCorrection,\n",
" OnePhotonSeries,\n",
" OpticalChannel,\n",
" RoiResponseSeries,\n",
" TwoPhotonSeries,\n",
")\n",
"\n",
"nwbfile = NWBFile(\n",
" session_description=\"my first synthetic recording\",\n",
" identifier=str(uuid4()),\n",
" session_start_time=datetime.now(tzlocal()),\n",
" experimenter=[\n",
" \"Baggins, Bilbo\",\n",
" ],\n",
" lab=\"Bag End Laboratory\",\n",
" institution=\"University of Middle Earth at the Shire\",\n",
" experiment_description=\"I went on an adventure to reclaim vast treasures.\",\n",
" session_id=\"LONELYMTN001\",\n",
")\n",
"\n",
"device = nwbfile.create_device(\n",
" name=\"Microscope\",\n",
" description=\"My two-photon microscope\",\n",
" manufacturer=\"The best microscope manufacturer\",\n",
")\n",
"optical_channel = OpticalChannel(\n",
" name=\"OpticalChannel\",\n",
" description=\"an optical channel\",\n",
" emission_lambda=500.0,\n",
")\n",
"\n",
"imaging_plane = nwbfile.create_imaging_plane(\n",
" name=\"ImagingPlane\",\n",
" optical_channel=optical_channel,\n",
" imaging_rate=30.0,\n",
" description=\"a very interesting part of the brain\",\n",
" device=device,\n",
" excitation_lambda=600.0,\n",
" indicator=\"GFP\",\n",
" location=\"V1\",\n",
" grid_spacing=[0.01, 0.01],\n",
" grid_spacing_unit=\"meters\",\n",
" origin_coords=[1.0, 2.0, 3.0],\n",
" origin_coords_unit=\"meters\",\n",
")\n",
"\n",
"ophys_module = nwbfile.create_processing_module(\n",
" name=\"ophys\", description=\"optical physiology processed data\"\n",
")\n",
"\n",
"img_seg = ImageSegmentation()\n",
"\n",
"ps = img_seg.create_plane_segmentation(\n",
" name=\"PlaneSegmentation\",\n",
" description=\"output from segmenting my favorite imaging plane\",\n",
" imaging_plane=imaging_plane,\n",
")\n",
"\n",
"ophys_module.add(img_seg)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"popped_ps = img_seg.plane_segmentations.pop(\"PlaneSegmentation\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <style>\n",
" .container-fields {\n",
" font-family: \"Open Sans\", Arial, sans-serif;\n",
" }\n",
" .container-fields .field-value {\n",
" color: #00788E;\n",
" }\n",
" .container-fields details > summary {\n",
" cursor: pointer;\n",
" display: list-item;\n",
" }\n",
" .container-fields details > summary:hover {\n",
" color: #0A6EAA;\n",
" }\n",
" </style>\n",
" \n",
" <script>\n",
" function copyToClipboard(text) {\n",
" navigator.clipboard.writeText(text).then(function() {\n",
" console.log('Copied to clipboard: ' + text);\n",
" }, function(err) {\n",
" console.error('Could not copy text: ', err);\n",
" });\n",
" }\n",
"\n",
" document.addEventListener('DOMContentLoaded', function() {\n",
" let fieldKeys = document.querySelectorAll('.container-fields .field-key');\n",
" fieldKeys.forEach(function(fieldKey) {\n",
" fieldKey.addEventListener('click', function() {\n",
" let accessCode = fieldKey.getAttribute('title').replace('Access code: ', '');\n",
" copyToClipboard(accessCode);\n",
" });\n",
" });\n",
" });\n",
" </script>\n",
" <div class='container-wrap'><div class='container-header'><div class='xr-obj-type'><h3>ImageSegmentation</h3></div></div><details><summary style=\"display: list-item; margin-left: 0px;\" class=\"container-fields field-key\" title=\".plane_segmentations\"><b>plane_segmentations</b></summary></details></div>"
],
"text/plain": [
"ImageSegmentation pynwb.ophys.ImageSegmentation at 0x5760727760\n",
"Fields:"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nwbfile.processing[\"ophys\"][\"ImageSegmentation\"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ImagingPlane': ImagingPlane pynwb.ophys.ImagingPlane at 0x5759724752\n",
" Fields:\n",
" conversion: 1.0\n",
" description: a very interesting part of the brain\n",
" device: Microscope pynwb.device.Device at 0x4721265808\n",
" Fields:\n",
" description: My two-photon microscope\n",
" manufacturer: The best microscope manufacturer\n",
" \n",
" excitation_lambda: 600.0\n",
" imaging_rate: 30.0\n",
" indicator: GFP\n",
" location: V1\n",
" optical_channel: (\n",
" OpticalChannel <class 'pynwb.ophys.OpticalChannel'>\n",
" )\n",
" unit: meters}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nwbfile.imaging_planes"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "dev",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment