@@ -2031,8 +2031,25 @@ def reduce_slices( # noqa: C901
20312031 res_eval = blosc2 .uninit (shape , dtype , chunks = chunks , blocks = blocks , cparams = cparams , ** kwargs )
20322032 # Compute the number of blocks in the result
20332033 nblocks = res_eval .nbytes // res_eval .blocksize
2034- # Initialize to zeros since some blocks may be padding and won't be written
2035- aux_reduc = np .zeros (nblocks , dtype = dtype )
2034+ # Initialize aux_reduc based on the reduction operation
2035+ # Padding blocks won't be written, so initial values matter for the final reduction
2036+ if reduce_op == ReduceOp .SUM or reduce_op == ReduceOp .ANY :
2037+ aux_reduc = np .zeros (nblocks , dtype = dtype )
2038+ elif reduce_op == ReduceOp .PROD or reduce_op == ReduceOp .ALL :
2039+ aux_reduc = np .ones (nblocks , dtype = dtype )
2040+ elif reduce_op == ReduceOp .MIN :
2041+ if np .issubdtype (dtype , np .integer ):
2042+ aux_reduc = np .full (nblocks , np .iinfo (dtype ).max , dtype = dtype )
2043+ else :
2044+ aux_reduc = np .full (nblocks , np .inf , dtype = dtype )
2045+ elif reduce_op == ReduceOp .MAX :
2046+ if np .issubdtype (dtype , np .integer ):
2047+ aux_reduc = np .full (nblocks , np .iinfo (dtype ).min , dtype = dtype )
2048+ else :
2049+ aux_reduc = np .full (nblocks , - np .inf , dtype = dtype )
2050+ else :
2051+ # For other operations, zeros should be safe
2052+ aux_reduc = np .zeros (nblocks , dtype = dtype )
20362053 try :
20372054 print ("expr->miniexpr:" , expression , reduce_op )
20382055 expression = f"{ reduce_op_str } ({ expression } )"
0 commit comments