GeezerVison is showing github.com — Custom operations over user defined types that creates new instance of userdefined type · Issue #564 · python-graphblas/python-graphblas
Custom operations over user defined types that creates new instance of userdefined type #564DescriptionIs it possible to define custom operation that creates instances of user defined type?
NP_UDT = np.dtype([("x", np.int64), ("y", np.float64)], align=True)
UDT = gb.dtypes.register_new("UDT", NP_UDT)
v1 = gb.Vector(Point, size=10)
v1[0] = (1, 4.5) # Tuple works here
# I want to define a function that creates new UDT from int and float passed as parameters
def udt_add_op(x, y):
# When I try to use tuple here (similarly to assignment to v1[0]) it fails with exception below.
return ??? # What should be here to create new UDT(x,y) ?
BinaryOp.register_new("udt_add", udt_add_op,is_udt=True)
def v_add(v1, v2):
gb.Vector.ewise_add(v1, v2, "udt_add")
...
Exception on attempt to return tuple from udt_add_op:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "example.py", line 24, in v_add
v3 << gb.Vector.ewise_add(v1, v2, "points_add")
File "/miniconda3/lib/python3.10/site-packages/graphblas/core/vector.py", line 998, in ewise_add
return self._ewise_add(other, op)
File "/miniconda3/lib/python3.10/site-packages/graphblas/core/vector.py", line 1029, in _ewise_add
op = get_typed_op(op, self.dtype, other.dtype, kind="binary")
File "/miniconda3/lib/python3.10/site-packages/graphblas/core/operator/utils.py", line 112, in get_typed_op
return get_typed_op(
File "/miniconda3/lib/python3.10/site-packages/graphblas/core/operator/utils.py", line 43, in get_typed_op
return op._compile_udt(dtype, dtype2)
File "/miniconda3/lib/python3.10/site-packages/graphblas/core/operator/binary.py", line 609, in _compile_udt
ret_type = lookup_dtype(numba_func.overloads[sig].signature.return_type)
File "/miniconda3/lib/python3.10/site-packages/graphblas/core/dtypes.py", line 374, in lookup_dtype
raise ValueError(f"Unknown dtype: {key} of type {type(key)}")
ValueError: Unknown dtype: Tuple(Literal[int](1), float64) of type <class 'numba.core.types.containers.Tuple'>
Reacted by Anya Chistyakova Activityeriknw commented on Sep 23, 2025 Hi @gsvgit, great question!
This does not yet work, sorry. We should probably update the docstring to indicate this. There's a possible/promising solution that was explored in #298 that I think should be able to be adapted to make this work.
This is a feature we would like to support. How important/urgent is this for you? I understand many things are best (only?) done using UDFs on UDTs. Please note that our team are all volunteers and our bandwidth is limited (I'm about to become a dad!), so even doing our best this may take some time.
We would also enthusiastically help guide and coach you if you wanted to contribute and try to integrate the approach in #298 (which hopefully could work with struct types too). This touches on some technically challenging and interesting bits. Feel free to join our community call if you want to discuss further: #247
A possible workaround is to use the SuiteSparse:GraphBLAS JIT where you give strings of the C code for the UDTs and UDFs. It's more verbose and not very Pythonic--and it needs a compiler installed--but I believe it should work today. Please let us know how we can further help you and improve python-graphblas--thanks! eriknw commented on Sep 18, 2026 I'm closing this as completed 🎉. I believe this was added in #576 (and continued to be tweaked in subsequent PRs).
This has not been published in a release yet. We're working towards getting a release out.
I think UDFs, UDTs, and the JIT should be much more convenient to use now!
Thanks again for the well-written issue @gsvgit. I'm sorry this took so long to get in! Reacted by Anya Chistyakova
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|