@@ -1453,6 +1453,36 @@ cdef class SChunk:
1453
1453
if self .schunk.cctx == NULL :
1454
1454
raise RuntimeError (" Could not create compression context" )
1455
1455
1456
+
1457
+ def _set_aux_numba (self , func , inputs_id , dtype_output ):
1458
+ if self .schunk.storage.cparams.nthreads > 1 :
1459
+ raise AttributeError (" compress `nthreads` must be 1 when assigning a prefilter" )
1460
+
1461
+ func_id = func.__name__
1462
+ blosc2.prefilter_funcs[func_id] = func
1463
+ func_id = func_id.encode(" utf-8" ) if isinstance (func_id, str ) else func_id
1464
+
1465
+ # Set prefilter
1466
+ cdef blosc2_cparams* cparams = self .schunk.storage.cparams
1467
+ cparams.prefilter = < blosc2_prefilter_fn> general_numba
1468
+
1469
+ cdef blosc2_prefilter_params* preparams = < blosc2_prefilter_params * > malloc(sizeof(blosc2_prefilter_params))
1470
+ cdef filler_udata* fill_udata = < filler_udata * > malloc(sizeof(filler_udata))
1471
+ fill_udata.py_func = < char * > malloc(strlen(func_id) + 1 )
1472
+ strcpy(fill_udata.py_func, func_id)
1473
+ fill_udata.inputs_id = inputs_id
1474
+ fill_udata.output_cdtype = np.dtype(dtype_output).num
1475
+ fill_udata.chunkshape = self .schunk.chunksize // self .schunk.typesize
1476
+
1477
+ preparams.user_data = fill_udata
1478
+ cparams.preparams = preparams
1479
+ _check_cparams(cparams)
1480
+
1481
+ blosc2_free_ctx(self .schunk.cctx)
1482
+ self .schunk.cctx = blosc2_create_cctx(dereference(cparams))
1483
+ if self .schunk.cctx == NULL :
1484
+ raise RuntimeError (" Could not create compression context" )
1485
+
1456
1486
def _set_prefilter (self , func , dtype_input , dtype_output = None ):
1457
1487
if self .schunk.storage.cparams.nthreads > 1 :
1458
1488
raise AttributeError (" compress `nthreads` must be 1 when assigning a prefilter" )
@@ -1544,6 +1574,35 @@ cdef int general_filler(blosc2_prefilter_params *params):
1544
1574
1545
1575
return 0
1546
1576
1577
+
1578
+ cdef int general_numba(blosc2_prefilter_params * params):
1579
+ cdef filler_udata * udata = < filler_udata * > params.user_data
1580
+ cdef int nd = 1
1581
+ cdef np.npy_intp dims = params.output_size // params.output_typesize
1582
+
1583
+ inputs_tuple = _ctypes.PyObj_FromPtr(udata.inputs_id)
1584
+
1585
+ output = np.PyArray_SimpleNewFromData(nd, & dims, udata.output_cdtype, < void * > params.output)
1586
+ offset = params.nchunk * udata.chunkshape + params.output_offset // params.output_typesize
1587
+
1588
+ inputs = []
1589
+ for obj, dtype in inputs_tuple:
1590
+ if isinstance (obj, blosc2.SChunk):
1591
+ out = np.empty(dims, dtype = dtype)
1592
+ obj.get_slice(start = offset, stop = offset + dims, out = out)
1593
+ inputs.append(out)
1594
+ elif isinstance (obj, np.ndarray):
1595
+ inputs.append(obj[offset : offset + dims])
1596
+ elif isinstance (obj, (int , float , bool , complex )):
1597
+ inputs.append(np.full(dims, obj, dtype = dtype))
1598
+ else :
1599
+ raise ValueError (" Unsupported operand" )
1600
+
1601
+ func_id = udata.py_func.decode(" utf-8" )
1602
+ blosc2.prefilter_funcs[func_id](tuple (inputs), output, offset)
1603
+
1604
+ return 0
1605
+
1547
1606
def nelem_from_inputs (inputs_tuple , nelem = None ):
1548
1607
for obj, dtype in inputs_tuple:
1549
1608
if isinstance (obj, blosc2.SChunk):
0 commit comments