Skip to content

Commit d70c2ad

Browse files
authored
EMCellMesh and EMDensityReconstructionDataset implementation. (#122)
* em_cell_mesh and em_dense_reconstruction_dataset api * change downloader parameter to contenttype * adding emcell mesh example
1 parent 3437661 commit d70c2ad

File tree

13 files changed

+873
-3
lines changed

13 files changed

+873
-3
lines changed

examples/05_emcell_mesh.ipynb

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 24,
6+
"id": "c27a2580-0100-406d-8d3e-3fa4b6d28f57",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import obi_auth\n",
11+
"from entitysdk import Client\n",
12+
"from obi_auth import get_token\n",
13+
"from obi_notebook import get_projects"
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"id": "b914d46e-6ec8-42f6-bab4-a19960474ff3",
19+
"metadata": {},
20+
"source": [
21+
"# initialize token and project environment"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 25,
27+
"id": "41951493-dd21-4483-99e2-3b53ea9cb6a4",
28+
"metadata": {},
29+
"outputs": [
30+
{
31+
"data": {
32+
"application/vnd.jupyter.widget-view+json": {
33+
"model_id": "bf30a255200f48ce9d942ef4c7962ef3",
34+
"version_major": 2,
35+
"version_minor": 0
36+
},
37+
"text/plain": [
38+
"Dropdown(description='Select:', options=(('Understanding SSCx', {'id': 'f373e771-3a2f-4f45-ab59-0955efd7b1f4',…"
39+
]
40+
},
41+
"metadata": {},
42+
"output_type": "display_data"
43+
}
44+
],
45+
"source": [
46+
"token = get_token(environment=\"staging\", auth_mode=\"daf\")\n",
47+
"project_context = get_projects.get_projects(token, env=\"staging\")"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"id": "59abe72f-7f83-4fc1-9086-55706ffb4ece",
53+
"metadata": {},
54+
"source": [
55+
"# get Client object."
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 26,
61+
"id": "f007168f-d6bf-4d70-a816-e9ce0938a965",
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"client = Client(environment=\"staging\", project_context=project_context, token_manager=token)"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"id": "74e14fbd-2bd1-4353-8733-d252bdb0d6db",
71+
"metadata": {},
72+
"source": [
73+
"# create an EMDenseReconstructionDataset"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 29,
79+
"id": "85a0c2f2-153e-48c9-9108-a70dd5ff3228",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"from entitysdk.models import EMDenseReconstructionDataset,BrainRegion,Subject\n",
84+
"\n",
85+
"brain_region = client.search_entity(entity_type=BrainRegion, query={\"annotation_value\": 68}).one()\n",
86+
"subject = client.search_entity(entity_type=Subject).first()\n",
87+
"\n",
88+
"em_dense_dataset = EMDenseReconstructionDataset(\n",
89+
" brain_region= brain_region,\n",
90+
" subject=subject,\n",
91+
" **{\n",
92+
" 'name': 'this is an example',\n",
93+
" 'description': 'this is a test dataset',\n",
94+
" 'fixation': None,\n",
95+
" 'staining_type': None,\n",
96+
" 'slicing_thickness': 40.0,\n",
97+
" 'tissue_shrinkage': None,\n",
98+
" 'microscope_type': None,\n",
99+
" 'detector': None,\n",
100+
" 'slicing_direction': None,\n",
101+
" 'landmarks': None,\n",
102+
" 'voltage': None,\n",
103+
" 'current': None,\n",
104+
" 'dose': None,\n",
105+
" 'temperature': None,\n",
106+
" 'volume_resolution_x_nm': 4.0,\n",
107+
" 'volume_resolution_y_nm': 4.0,\n",
108+
" 'volume_resolution_z_nm': 40.0,\n",
109+
" 'release_url': 'http://microns-explorer.org',\n",
110+
" 'cave_client_url': 'https://global.daf-apis.com',\n",
111+
" 'cave_datastack': 'minnie65_public',\n",
112+
" 'precomputed_mesh_url': 'precomputed://gs://iarpa_microns/minnie/minnie65/seg_m1300/',\n",
113+
" 'cell_identifying_property': 'pt_root_id'})\n",
114+
"em_dense_reconstruction = client.register_entity(entity=em_dense_dataset, project_context=project_context)"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": 30,
120+
"id": "4d84409c-7007-42fa-b165-d15103830fe6",
121+
"metadata": {},
122+
"outputs": [],
123+
"source": [
124+
"em_dense_reconstruction = client.search_entity(entity_type=EMDenseReconstructionDataset, query={'name':'this is an example'} ).one()"
125+
]
126+
},
127+
{
128+
"cell_type": "markdown",
129+
"id": "f59d7ad2-5392-412a-a653-e6c8bbcc4360",
130+
"metadata": {},
131+
"source": [
132+
"# create an EMCellMesh"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": 31,
138+
"id": "74993e03-3996-442a-8249-c5799a402283",
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"from entitysdk.models import EMCellMesh, EMCellMeshGenerationMethod, EMCellMeshType\n",
143+
"em_mesh = EMCellMesh(\n",
144+
" generation_method=EMCellMeshGenerationMethod.marching_cubes,\n",
145+
" mesh_type=EMCellMeshType.dynamic,\n",
146+
" brain_region=brain_region,\n",
147+
" subject=subject,\n",
148+
" em_dense_reconstruction_dataset=em_dense_reconstruction,\n",
149+
" **{\"release_version\": 1512,\n",
150+
" \"dense_reconstruction_cell_id\": 12,\n",
151+
" \"level_of_detail\": 1,\n",
152+
" })\n",
153+
"em_mesh_registered = client.register_entity(entity=em_mesh, project_context=project_context)"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 32,
159+
"id": "cbd40f00-7a9b-4329-a193-4e0dfa43188e",
160+
"metadata": {},
161+
"outputs": [],
162+
"source": [
163+
"em_mesh_registered = client.search_entity(entity_type=EMCellMesh, query={'release_version':1512} ).one()"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"id": "e9b43270-2e02-45f5-809a-88dbfc1fafef",
169+
"metadata": {},
170+
"source": [
171+
"# uploading an .obj file"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": 33,
177+
"id": "55da1866-dfea-445b-9afd-cea189276af6",
178+
"metadata": {},
179+
"outputs": [],
180+
"source": [
181+
"asset_obj = client.upload_file(\n",
182+
" entity_id=em_mesh_registered.id,\n",
183+
" entity_type=EMCellMesh,\n",
184+
" file_path=\"90.obj\",\n",
185+
" file_content_type=\"application/obj\",\n",
186+
" asset_label=\"cell_surface_mesh\",\n",
187+
" project_context=project_context,\n",
188+
" )"
189+
]
190+
}
191+
],
192+
"metadata": {
193+
"kernelspec": {
194+
"display_name": "Python 3 (ipykernel)",
195+
"language": "python",
196+
"name": "python3"
197+
},
198+
"language_info": {
199+
"codemirror_mode": {
200+
"name": "ipython",
201+
"version": 3
202+
},
203+
"file_extension": ".py",
204+
"mimetype": "text/x-python",
205+
"name": "python",
206+
"nbconvert_exporter": "python",
207+
"pygments_lexer": "ipython3",
208+
"version": "3.12.8"
209+
}
210+
},
211+
"nbformat": 4,
212+
"nbformat_minor": 5
213+
}

src/entitysdk/_server_schemas.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ class EntityRoute(StrEnum):
368368
ion_channel_model = "ion-channel-model"
369369
ion_channel_recording = "ion-channel-recording"
370370
memodel = "memodel"
371-
mesh = "mesh"
372371
memodel_calibration_result = "memodel-calibration-result"
373372
me_type_density = "me-type-density"
374373
reconstruction_morphology = "reconstruction-morphology"
@@ -405,7 +404,6 @@ class EntityType(StrEnum):
405404
ion_channel_model = "ion_channel_model"
406405
ion_channel_recording = "ion_channel_recording"
407406
memodel = "memodel"
408-
mesh = "mesh"
409407
memodel_calibration_result = "memodel_calibration_result"
410408
me_type_density = "me_type_density"
411409
reconstruction_morphology = "reconstruction_morphology"
@@ -960,7 +958,6 @@ class ResourceRoute(StrEnum):
960958
measurement_annotation = "measurement-annotation"
961959
memodel = "memodel"
962960
memodel_calibration_result = "memodel-calibration-result"
963-
mesh = "mesh"
964961
mtype = "mtype"
965962
mtype_classification = "mtype-classification"
966963
organization = "organization"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Download functions for EM Cell Mesh entities."""
2+
3+
import logging
4+
from pathlib import Path
5+
6+
from entitysdk.client import Client
7+
from entitysdk.models.em_cell_mesh import EMCellMesh
8+
from entitysdk.types import ContentType
9+
from entitysdk.utils.filesystem import create_dir
10+
11+
logger = logging.getLogger(__name__)
12+
13+
14+
def download_mesh_file(
15+
client: Client,
16+
em_cell_mesh: EMCellMesh,
17+
output_dir: str | Path,
18+
content_type: ContentType,
19+
) -> Path:
20+
"""Download mesh file.
21+
22+
Args:
23+
client (Client): EntitySDK client
24+
em_cell_mesh (EMCellMesh): EM Cell Mesh entitysdk object
25+
output_dir (str or Path): directory to save the mesh file
26+
content_type (ContentType): type of the mesh file
27+
28+
Returns:
29+
Path: Path to the downloaded file
30+
"""
31+
output_dir = create_dir(output_dir)
32+
asset = client.download_assets(
33+
em_cell_mesh,
34+
selection={"content_type": content_type},
35+
output_path=output_dir,
36+
).one()
37+
38+
return asset.path

src/entitysdk/models/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
from entitysdk.models.derivation import Derivation
1212
from entitysdk.models.electrical_cell_recording import ElectricalCellRecording
1313
from entitysdk.models.electrical_recording import ElectricalRecordingStimulus
14+
from entitysdk.models.em_cell_mesh import EMCellMesh, EMCellMeshGenerationMethod, EMCellMeshType
15+
from entitysdk.models.em_dense_reconstruction_dataset import (
16+
EMDenseReconstructionDataset,
17+
SlicingDirectionType,
18+
)
1419
from entitysdk.models.emodel import EModel
1520
from entitysdk.models.ion_channel import IonChannel
1621
from entitysdk.models.ion_channel_model import IonChannelModel, NeuronBlock, UseIon
@@ -45,6 +50,10 @@
4550
"Derivation",
4651
"ElectricalCellRecording",
4752
"ElectricalRecordingStimulus",
53+
"EMCellMesh",
54+
"EMCellMeshGenerationMethod",
55+
"EMCellMeshType",
56+
"EMDenseReconstructionDataset",
4857
"EModel",
4958
"ETypeClassification",
5059
"IonChannel",
@@ -70,6 +79,7 @@
7079
"SimulationExecution",
7180
"SimulationGeneration",
7281
"SimulationResult",
82+
"SlicingDirectionType",
7383
"Species",
7484
"Strain",
7585
"Subject",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""EM Cell Mesh models."""
2+
3+
from typing import Annotated
4+
5+
from pydantic import Field
6+
7+
from entitysdk.models.entity import Entity
8+
from entitysdk.models.scientific_artifact import ScientificArtifact
9+
from entitysdk.types import EMCellMeshGenerationMethod, EMCellMeshType
10+
11+
12+
class EMCellMesh(ScientificArtifact):
13+
"""EM Cell Mesh model."""
14+
15+
release_version: Annotated[
16+
int,
17+
Field(
18+
description="The release version of the mesh.",
19+
),
20+
]
21+
dense_reconstruction_cell_id: Annotated[
22+
int,
23+
Field(
24+
description="The cell ID in the dense reconstruction dataset.",
25+
),
26+
]
27+
generation_method: Annotated[
28+
EMCellMeshGenerationMethod,
29+
Field(
30+
description="The algorithm used to generate the mesh from a volume.",
31+
),
32+
]
33+
level_of_detail: Annotated[
34+
int,
35+
Field(
36+
description="The level of detail of the mesh.",
37+
),
38+
]
39+
generation_parameters: Annotated[
40+
dict | None,
41+
Field(
42+
description="Parameters used for mesh generation.",
43+
),
44+
] = None
45+
mesh_type: Annotated[
46+
EMCellMeshType,
47+
Field(
48+
description="How the mesh was created (static or dynamic).",
49+
),
50+
]
51+
em_dense_reconstruction_dataset: Annotated[
52+
Entity | None,
53+
Field(
54+
description="The dense reconstruction dataset this mesh belongs to.",
55+
),
56+
] = None

0 commit comments

Comments
 (0)