1
1
"""Get Morpho tool."""
2
2
3
3
import logging
4
- from typing import Any , ClassVar
4
+ from typing import ClassVar
5
5
6
6
from httpx import AsyncClient
7
7
from pydantic import BaseModel , Field
8
8
9
+ from neuroagent .tools .autogenerated_types .entitycore .models import (
10
+ ListResponseReconstructionMorphologyRead ,
11
+ )
9
12
from neuroagent .tools .base_tool import BaseMetadata , BaseTool
10
13
11
14
logger = logging .getLogger (__name__ )
@@ -15,12 +18,13 @@ class GetMorphoInput(BaseModel):
15
18
"""Inputs of the knowledge graph API."""
16
19
17
20
brain_region_id : str = Field (
18
- description = "ID of the brain region of interest in UUID format. To find the ID use the resolve-entity -tool first."
21
+ description = "ID of the brain region of interest in UUID format. To find the ID use the resolve-brain-region -tool first."
19
22
)
23
+ page_size : int = Field (default = 10 , description = "Number of results per page." )
20
24
page : int = Field (default = 1 , description = "Page number for pagination." )
21
25
mtype_id : str | None = Field (
22
26
default = None ,
23
- description = "ID of the M-type of interest. To find the ID use the resolve-entity -tool first." ,
27
+ description = "ID of the M-type of interest. To find the ID use the resolve-mtype -tool first." ,
24
28
)
25
29
26
30
@@ -29,7 +33,8 @@ class GetMorphoMetadata(BaseMetadata):
29
33
30
34
entitycore_url : str
31
35
token : str
32
- morpho_search_size : int
36
+ vlab_id : str | None
37
+ project_id : str | None
33
38
34
39
35
40
class MtypeOutput (BaseModel ):
@@ -40,29 +45,6 @@ class MtypeOutput(BaseModel):
40
45
alt_label : str | None
41
46
42
47
43
- class MorphologieOutput (BaseModel ):
44
- """Output schema for the knowledge graph API."""
45
-
46
- morphology_id : str
47
- morphology_name : str | None
48
- morphology_description : str | None
49
- mtype : list [MtypeOutput ] | None
50
-
51
- brain_region_id : str
52
- brain_region_name : str | None
53
-
54
- subject_species_name : str | None
55
-
56
-
57
- class GetMorphoToolOutput (BaseModel ):
58
- """Output schema for the Morpho tool."""
59
-
60
- morphologies : list [MorphologieOutput ]
61
- current_page : int
62
- page_size : int
63
- total_items_found : int
64
-
65
-
66
48
class GetMorphoTool (BaseTool ):
67
49
"""Class defining the Get Morpho logic."""
68
50
@@ -92,7 +74,7 @@ class GetMorphoTool(BaseTool):
92
74
metadata : GetMorphoMetadata
93
75
input_schema : GetMorphoInput
94
76
95
- async def arun (self ) -> GetMorphoToolOutput :
77
+ async def arun (self ) -> ListResponseReconstructionMorphologyRead :
96
78
"""From a brain region ID, extract morphologies.
97
79
98
80
Returns
@@ -105,9 +87,22 @@ async def arun(self) -> GetMorphoToolOutput:
105
87
106
88
response = await self .metadata .httpx_client .get (
107
89
url = self .metadata .entitycore_url + "/reconstruction-morphology" ,
108
- headers = {"Authorization" : f"Bearer { self .metadata .token } " },
90
+ headers = {
91
+ "Authorization" : f"Bearer { self .metadata .token } " ,
92
+ ** (
93
+ {"virtual-lab-id" : self .metadata .vlab_id }
94
+ if self .metadata .vlab_id is not None
95
+ else {}
96
+ ),
97
+ ** (
98
+ {"project-id" : self .metadata .project_id }
99
+ if self .metadata .project_id is not None
100
+ else {}
101
+ ),
102
+ },
109
103
params = {
110
- "page_size" : self .metadata .morpho_search_size ,
104
+ "page_size" : self .input_schema .page_size ,
105
+ "page" : self .input_schema .page ,
111
106
"within_brain_region_hierachy_id" : "e3e70682-c209-4cac-a29f-6fbed82c07cd" , # TEMP for mouse brain
112
107
"within_brain_region_brain_region_id" : self .input_schema .brain_region_id ,
113
108
"within_brain_region_ascendants" : False ,
@@ -118,48 +113,11 @@ async def arun(self) -> GetMorphoToolOutput:
118
113
),
119
114
},
120
115
)
121
-
122
- return self ._process_output (response .json ())
123
-
124
- @staticmethod
125
- def _process_output (output : Any ) -> GetMorphoToolOutput :
126
- """Process output to fit the KnowledgeGraphOutput pydantic class defined above.
127
-
128
- Parameters
129
- ----------
130
- output
131
- Raw output of the arun method, which comes from the KG
132
-
133
- Returns
134
- -------
135
- list of KGMorphoFeatureOutput to describe the morphology and its metadata.
136
- """
137
- formatted_output = [
138
- MorphologieOutput (
139
- morphology_id = res ["id" ],
140
- morphology_name = res .get ("name" ),
141
- morphology_description = res .get ("description" ),
142
- mtype = [
143
- MtypeOutput (
144
- mtype_id = mtype ["id" ],
145
- pref_label = mtype .get ("pref_label" ),
146
- alt_label = mtype .get ("alt_label" ),
147
- )
148
- for mtype in res .get ("mtypes" )
149
- ],
150
- brain_region_id = res ["brain_region" ]["id" ],
151
- brain_region_name = res ["brain_region" ].get ("Name" ),
152
- subject_species_name = res ["species" ].get ("name" ),
116
+ if response .status_code != 200 :
117
+ raise ValueError (
118
+ f"The morphology endpoint returned a non 200 response code. Error: { response .text } "
153
119
)
154
- for res in output ["data" ]
155
- ]
156
- pagination_data = output ["pagination" ]
157
- return GetMorphoToolOutput (
158
- morphologies = formatted_output ,
159
- current_page = pagination_data ["page" ],
160
- page_size = pagination_data ["page_size" ],
161
- total_items_found = pagination_data ["total_items" ],
162
- )
120
+ return ListResponseReconstructionMorphologyRead (** response .json ())
163
121
164
122
@classmethod
165
123
async def is_online (cls , * , httpx_client : AsyncClient , entitycore_url : str ) -> bool :
0 commit comments