Skip to content

Commit 8e8f0b7

Browse files
committed
Add vector functions
1 parent 9afaa0b commit 8e8f0b7

File tree

4 files changed

+141
-12
lines changed

4 files changed

+141
-12
lines changed

pyTigerGraph/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
from pyTigerGraph.pytgasync.pyTigerGraph import AsyncTigerGraphConnection
33
from pyTigerGraph.common.exception import TigerGraphException
44

5-
__version__ = "1.8.4"
5+
__version__ = "1.8.5"
66

77
__license__ = "Apache 2"

pyTigerGraph/common/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ def _error_check(self, res: dict) -> bool:
271271
if "error" in res and res["error"] and res["error"] != "false":
272272
# Endpoint might return string "false" rather than Boolean false
273273
raise TigerGraphException(
274-
res["message"], (res["code"] if "code" in res else None))
274+
res["message"], (res["code"] if "code" in res else None)
275+
)
275276
return False
276277

277278
def _prep_req(self, authMode, headers, url, method, data):

pyTigerGraph/pyTigerGraphVertex.py

+70-6
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,86 @@ def getVertexAttrs(self, vertexType: str) -> list:
7777
- "map_type(key_type,value_type)"
7878
and it is a string.
7979
"""
80-
logger.info("entry: getAttributes")
80+
logger.info("entry: getVertexAttrs")
8181
if logger.level == logging.DEBUG:
8282
logger.debug("params: " + self._locals(locals()))
8383

8484
et = self.getVertexType(vertexType)
8585
ret = []
8686

8787
for at in et["Attributes"]:
88+
at["AttributeType"]["AttributeType"] = at["AttributeType"].pop("Name")
8889
ret.append(
89-
(at["AttributeName"], self._getAttrType(at["AttributeType"])))
90+
(at["AttributeName"], at["AttributeType"])
91+
)
9092

9193
if logger.level == logging.DEBUG:
9294
logger.debug("return: " + str(ret))
93-
logger.info("exit: getAttributes")
95+
logger.info("exit: getVertexAttrs")
9496

9597
return ret
9698

99+
def getVertexVectors(self, vertexType: str) -> list:
100+
"""Returns the names and types of the embedding attributes of the vertex type.
101+
102+
Args:
103+
vertexType:
104+
The name of the vertex type.
105+
106+
Returns:
107+
A list of (vector_name, vector_type) tuples.
108+
The format of vector_type is one of
109+
- "scalar_type"
110+
- "complex_type(scalar_type)"
111+
- "map_type(key_type,value_type)"
112+
and it is a string.
113+
"""
114+
logger.info("entry: getVertexVectors")
115+
if logger.level == logging.DEBUG:
116+
logger.debug("params: " + self._locals(locals()))
117+
118+
et = self.getVertexType(vertexType)
119+
ret = []
120+
121+
for vt in et["EmbeddingAttributes"]:
122+
ret.append(
123+
(vt["Name"], vt)
124+
)
125+
126+
if logger.level == logging.DEBUG:
127+
logger.debug("return: " + str(ret))
128+
logger.info("exit: getVertexVectors")
129+
130+
return ret
131+
132+
def getVectorStatus(self, vertexType: str, vectorAttr: str = "") -> bool:
133+
"""Check the rebuild status of the vertex type or the embedding attribute
134+
135+
Args:
136+
vertexType:
137+
The name of the vertex type.
138+
vectorAttr:
139+
The name of the vector attribute, optional.
140+
141+
Returns:
142+
a bool indicates whether vector rebuild is done or not
143+
144+
Endpoint:
145+
- `GET /vector/status/{graph_name}/{vertex_type}/[{vector_name}]`
146+
"""
147+
logger.info("entry: getVectorStatus")
148+
if logger.level == logging.DEBUG:
149+
logger.debug("params: " + self._locals(locals()))
150+
151+
ret = self._req("GET", self.restppUrl + "/vector/status/" +
152+
self.graphname + "/" + vertexType + "/" + vectorName)
153+
154+
if logger.level == logging.DEBUG:
155+
logger.debug("return: " + str(ret))
156+
logger.info("exit: getVectorStatus")
157+
158+
return len(ret["NeedRebuildServers"]) == 0
159+
97160
def getVertexType(self, vertexType: str, force: bool = False) -> dict:
98161
"""Returns the details of the specified vertex type.
99162
@@ -215,7 +278,7 @@ def upsertVertex(self, vertexType: str, vertexId: str, attributes: dict = None)
215278
```
216279
Example:
217280
```
218-
{"name": "Thorin", points: (10, "+"), "bestScore": (67, "max")}
281+
{"name": "Thorin", points: (10, "+"), "bestScore": (83, "max"), "embedding": [0.1, -0.2, 3.1e-2]}
219282
```
220283
For valid values of `<operator>` see xref:tigergraph-server:API:built-in-endpoints.adoc#_operation_codes[Operation codes].
221284
@@ -268,7 +331,8 @@ def upsertVertices(self, vertexType: str, vertices: list, atomic: bool = False)
268331
----
269332
[
270333
(2, {"name": "Balin", "points": (10, "+"), "bestScore": (67, "max")}),
271-
(3, {"name": "Dwalin", "points": (7, "+"), "bestScore": (35, "max")})
334+
(3, {"name": "Dwalin", "points": (7, "+"), "bestScore": (35, "max")}),
335+
(4, {"name": "Thorin", points: (10, "+"), "bestScore": (83, "max"), "embedding": [0.1, -0.2, 3.1e-2]})
272336
]
273337
----
274338
@@ -799,4 +863,4 @@ def vertexSetToDataFrame(self, vertexSet: dict, withId: bool = True, withType: b
799863
logger.debug("return: " + str(ret))
800864
logger.info("exit: vertexSetToDataFrame")
801865

802-
return ret
866+
return ret

pyTigerGraph/pytgasync/pyTigerGraphVertex.py

+68-4
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,78 @@ async def getVertexAttrs(self, vertexType: str) -> list:
8787
ret = []
8888

8989
for at in et["Attributes"]:
90+
at["AttributeType"]["AttributeType"] = at["AttributeType"].pop("Name")
9091
ret.append(
91-
(at["AttributeName"], self._getAttrType(at["AttributeType"])))
92+
(at["AttributeName"], at["AttributeType"])
93+
)
9294

9395
if logger.level == logging.DEBUG:
9496
logger.debug("return: " + str(ret))
9597
logger.info("exit: getAttributes")
9698

9799
return ret
98100

101+
async def getVertexVectors(self, vertexType: str) -> list:
102+
"""Returns the names and types of the embedding attributes of the vertex type.
103+
104+
Args:
105+
vertexType:
106+
The name of the vertex type.
107+
108+
Returns:
109+
A list of (vector_name, vector_type) tuples.
110+
The format of vector_type is one of
111+
- "scalar_type"
112+
- "complex_type(scalar_type)"
113+
- "map_type(key_type,value_type)"
114+
and it is a string.
115+
"""
116+
logger.info("entry: getVertexVectors")
117+
if logger.level == logging.DEBUG:
118+
logger.debug("params: " + self._locals(locals()))
119+
120+
et = await self.getVertexType(vertexType)
121+
ret = []
122+
123+
for vt in et["EmbeddingAttributes"]:
124+
ret.append(
125+
(vt["Name"], vt)
126+
)
127+
128+
if logger.level == logging.DEBUG:
129+
logger.debug("return: " + str(ret))
130+
logger.info("exit: getVertexVectors")
131+
132+
return ret
133+
134+
async def getVectorStatus(self, vertexType: str, vectorAttr: str = "") -> bool:
135+
"""Check the rebuild status of the vertex type or the embedding attribute
136+
137+
Args:
138+
vertexType:
139+
The name of the vertex type.
140+
vectorAttr:
141+
The name of the vector attribute, optional.
142+
143+
Returns:
144+
a bool indicates whether vector rebuild is done or not
145+
146+
Endpoint:
147+
- `GET /vector/status/{graph_name}/{vertex_type}/[{vector_name}]`
148+
"""
149+
logger.info("entry: getVectorStatus")
150+
if logger.level == logging.DEBUG:
151+
logger.debug("params: " + self._locals(locals()))
152+
153+
ret = await self._req("GET", self.restppUrl + "/vector/status/" +
154+
self.graphname + "/" + vertexType + "/" + vectorName)
155+
156+
if logger.level == logging.DEBUG:
157+
logger.debug("return: " + str(ret))
158+
logger.info("exit: getVectorStatus")
159+
160+
return len(ret["NeedRebuildServers"]) == 0
161+
99162
async def getVertexType(self, vertexType: str, force: bool = False) -> dict:
100163
"""Returns the details of the specified vertex type.
101164
@@ -222,7 +285,7 @@ async def upsertVertex(self, vertexType: str, vertexId: str, attributes: dict =
222285
```
223286
Example:
224287
```
225-
{"name": "Thorin", points: (10, "+"), "bestScore": (67, "max")}
288+
{"name": "Thorin", points: (10, "+"), "bestScore": (83, "max"), "embedding": [0.1, -0.2, 3.1e-2]}
226289
```
227290
For valid values of `<operator>` see xref:tigergraph-server:API:built-in-endpoints.adoc#_operation_codes[Operation codes].
228291
@@ -275,7 +338,8 @@ async def upsertVertices(self, vertexType: str, vertices: list, atomic: bool = F
275338
----
276339
[
277340
(2, {"name": "Balin", "points": (10, "+"), "bestScore": (67, "max")}),
278-
(3, {"name": "Dwalin", "points": (7, "+"), "bestScore": (35, "max")})
341+
(3, {"name": "Dwalin", "points": (7, "+"), "bestScore": (35, "max")}),
342+
(4, {"name": "Thorin", points: (10, "+"), "bestScore": (83, "max"), "embedding": [0.1, -0.2, 3.1e-2]})
279343
]
280344
----
281345
@@ -810,4 +874,4 @@ async def vertexSetToDataFrame(self, vertexSet: dict, withId: bool = True, withT
810874
logger.debug("return: " + str(ret))
811875
logger.info("exit: vertexSetToDataFrame")
812876

813-
return ret
877+
return ret

0 commit comments

Comments
 (0)