diff --git a/docs/make.jl b/docs/make.jl index f5993ae08..25c03fd6e 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -20,7 +20,7 @@ end flush_info_and_warnings() -Logging.disable_logging(Logging.Warn) +# Logging.disable_logging(Logging.Warn) # Make the docs, without running the tests again # We need to explicitly add all the extensions here @@ -51,7 +51,7 @@ doc = makedocs( ) # Enable logging to console again -Logging.disable_logging(Logging.BelowMinLevel) +# Logging.disable_logging(Logging.BelowMinLevel) DocumenterVitepress.deploydocs(; repo="github.com/rafaqz/Rasters.jl", branch = "gh-pages", @@ -61,4 +61,4 @@ DocumenterVitepress.deploydocs(; repo="github.com/rafaqz/Rasters.jl", if !isempty(doc.internal.errors) error("Documentation build failed with errors:\n$(collect(doc.internal.errors))") -end \ No newline at end of file +end diff --git a/src/methods/rasterize.jl b/src/methods/rasterize.jl index 1f4f78d22..656e0f20f 100644 --- a/src/methods/rasterize.jl +++ b/src/methods/rasterize.jl @@ -9,14 +9,27 @@ _reduce_op(::typeof(prod)) = Base.mul_prod _reduce_op(::typeof(minimum)) = min _reduce_op(::typeof(maximum)) = max _reduce_op(::typeof(last)) = _take_last +_reduce_op(::typeof(any)) = | +_reduce_op(::typeof(all)) = & _reduce_op(f, missingval) = _reduce_op(f) _reduce_op(::typeof(first), missingval) = _TakeFirst(missingval) _reduce_op(x) = nothing -_is_op_threadsafe(::typeof(sum)) = true -_is_op_threadsafe(::typeof(prod)) = true -_is_op_threadsafe(::typeof(minimum)) = true -_is_op_threadsafe(::typeof(maximum)) = true +# Identical to Base.PermutedDimsArrays.CommutativeOps but define here to avoid +# using base internals +const CommutativeOps = Union{ + typeof(&), + typeof(+), + typeof(Base._extrema_rf), + typeof(Base.add_sum), + typeof(max), + typeof(min), + typeof(|), + typeof(Base.mul_prod), # these are not in Base.PermutedDimsArrays.CommutativeOps but should be safe? + typeof(*) +} + +_is_op_threadsafe(::CommutativeOps) = true _is_op_threadsafe(f) = false _reduce_init(reducer, st::AbstractRasterStack, missingval) = map(A -> _reduce_init(reducer, A, missingval), st) @@ -103,7 +116,7 @@ struct Rasterizer{T,G,F,R,O,I,M} end function Rasterizer(geom, fill, fillitr; reducer=nothing, - op=nothing, + op=_reduce_op(reducer), missingval=nothing, shape=nothing, eltype=nothing, @@ -121,8 +134,6 @@ function Rasterizer(geom, fill, fillitr; isnothing(reducer) && isnothing(op) && !(fill isa Function) && throw(ArgumentError("either reducer, op or fill must be a function")) end - op = isnothing(op) ? _reduce_op(reducer) : op - threadsafe_op = isnothing(threadsafe) ? _is_op_threadsafe(op) : threadsafe shape = if isnothing(shape) diff --git a/test/rasterize.jl b/test/rasterize.jl index c934a2c67..1aee17267 100644 --- a/test/rasterize.jl +++ b/test/rasterize.jl @@ -584,4 +584,23 @@ end @test count(x -> x == [2], result) == 12 @test count(x -> x == [1, 2], result) == 12 end -end \ No newline at end of file +end + + +@testset "threaded reduction warnings" begin + commutative_fs = (sum, prod, maximum, minimum, any, all, mean) + geom = GI.GeometryCollection([polygon,polygon,polygon]) + + for f in commutative_fs + @test_logs rasterize(f, geom; to=A1, fill=true, missingval = false, threaded=true) + end + @test_logs rasterize(count, geom; to=A1, threaded=true) # count has no fill or missingval + + other_fs = (median, first, last, x -> sum(x)) + + for f in other_fs + @test_logs (:warn, "if `op` is not threadsafe, `threaded=true` may be slower than `threaded=false`") rasterize( + f, geom; to=A1, fill=true, missingval = false, threaded=true) + @test_logs rasterize(f, geom; to=A1, fill=true, missingval = false, threaded=false) + end +end