Skip to content

Commit e306180

Browse files
authored
fix: Fix Tesseract SDK decoding and error handling (#123)
#### Relevant issue or PR n/a #### Description of changes - Ensure returned arrays have the correct shape and dtype. - Fix an internal error when triggering certain validation errors that rely on a context object. #### Testing done manual #### License - [x] By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license](https://pasteurlabs.github.io/tesseract/LICENSE). - [x] I sign the Developer Certificate of Origin below by adding my name and email address to the `Signed-off-by` line. <details> <summary><b>Developer Certificate of Origin</b></summary> ```text Developer Certificate of Origin Version 1.1 Copyright (C) 2004, 2006 The Linux Foundation and its contributors. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. ``` </details> Signed-off-by: Dion Häfner <dion.haefner@simulation.science>
1 parent a0b4892 commit e306180

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

tesseract_core/sdk/tesseract.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,10 @@ def _decode_array(encoded_arr: dict):
355355
data = base64.b64decode(encoded_arr["data"]["buffer"])
356356
arr = np.frombuffer(data, dtype=encoded_arr["dtype"])
357357
else:
358-
arr = np.array(encoded_arr["data"]["buffer"])
358+
arr = np.array(encoded_arr["data"]["buffer"], dtype=encoded_arr["dtype"])
359359
else:
360-
arr = encoded_arr
360+
raise ValueError("Encoded array does not contain 'data' key. Cannot decode.")
361+
arr = arr.reshape(encoded_arr["shape"])
361362
return arr
362363

363364

@@ -410,6 +411,7 @@ def _request(
410411
type=e["type"],
411412
loc=tuple(e["loc"]),
412413
input=e.get("input"),
414+
ctx=e.get("ctx", {}),
413415
)
414416
for e in data["detail"]
415417
]

tests/sdk_tests/test_tesseract.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,21 @@ def test_encode_array(b64, expected_data):
176176
"dtype": "float32",
177177
"data": {"buffer": [1.0, 2.0, 3.0], "encoding": "raw"},
178178
},
179-
[1.0, 2.0, 3.0],
179+
np.array([1.0, 2.0, 3.0], dtype="float32"),
180180
),
181181
(
182182
{
183-
"shape": (3,),
183+
"shape": (1, 3),
184184
"dtype": "float32",
185185
"data": {"buffer": "AACAPwAAAEAAAEBA", "encoding": "base64"},
186186
},
187-
[1.0, 2.0, 3.0],
187+
np.array([[1.0, 2.0, 3.0]], dtype="float32"),
188188
),
189189
],
190190
)
191191
def test_decode_array(encoded, expected):
192192
decoded = _decode_array(encoded)
193-
assert np.all(decoded == expected)
193+
np.testing.assert_array_equal(decoded, expected, strict=True)
194194

195195

196196
def test_tree_map():

0 commit comments

Comments
 (0)