JuliaGeometry / Contour.jl

Calculating contour curves for 2D scalar fields in Julia
Other
43 stars 13 forks source link

`contour` fails on arrays of integers #73

Closed sefffal closed 2 years ago

sefffal commented 2 years ago

Hello, and thanks for the great package!

I noticed that currently contour fails if provided an array of integers. For example:

using Contour
 contours(1:10,1:10, rand(1:10, 10, 10))

Gives

ERROR: InexactError: Int64(2.409090909090909)
Stacktrace:
  [1] Int64
    @ .\float.jl:788 [inlined]
  [2] convert
    @ .\number.jl:7 [inlined]
  [3] macro expansion
    @ C:\Users\William\.julia\packages\StaticArrays\0T5rI\src\util.jl:17 [inlined]
  [4] convert_ntuple
    @ C:\Users\William\.julia\packages\StaticArrays\0T5rI\src\util.jl:13 [inlined]
  [5] SArray
    @ C:\Users\William\.julia\packages\StaticArrays\0T5rI\src\SArray.jl:28 [inlined]
  [6] StaticArray
    @ C:\Users\William\.julia\packages\StaticArrays\0T5rI\src\convert.jl:152 [inlined]
  [7] interpolate(x::UnitRange{Int64}, y::UnitRange{Int64}, z::Matrix{Int64}, h::Float64, ind::Tuple{Int64, Int64}, edge::UInt8, #unused#::Type{StaticArrays.SVector{2, Int64}})
    @ Contour C:\Users\William\.julia\packages\Contour\YsFGr\src\interpolate.jl:39
  [8] trace_contour(x::UnitRange{Int64}, y::UnitRange{Int64}, z::Matrix{Int64}, h::Float64, cells::Dict{Tuple{Int64, Int64}, UInt8})
    @ Contour C:\Users\William\.julia\packages\Contour\YsFGr\src\Contour.jl:294
  [9] contour
    @ C:\Users\William\.julia\packages\Contour\YsFGr\src\Contour.jl:69 [inlined]
 [10] #1
    @ .\none:0 [inlined]
 [11] iterate
    @ .\generator.jl:47 [inlined]
 [12] collect(itr::Base.Generator{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, Contour.var"#1#2"{UnitRange{Int64}, UnitRange{Int64}, Matrix{Int64}}})
    @ Base .\array.jl:787
 [13] contours
    @ C:\Users\William\.julia\packages\Contour\YsFGr\src\Contour.jl:83 [inlined]
 [14] contours
    @ C:\Users\William\.julia\packages\Contour\YsFGr\src\Contour.jl:90 [inlined]
 [15] contours(x::UnitRange{Int64}, y::UnitRange{Int64}, z::Matrix{Int64})
    @ Contour C:\Users\William\.julia\packages\Contour\YsFGr\src\Contour.jl:96
 [16] top-level scope
    @ REPL[94]:1

If it's not possible for contour to work with these arrays, may I suggest either restricting the type signature or automatically calling float.(arr) on the input?

Thanks!

sjkelly commented 2 years ago

I am not sure if this is a recent regression with the removal of StaticArrays as a dependency. I realize we left a hole in the API as well for contours. In the mean time, the contour method for a single level should work:

contour(1:10,1:10, rand(1:10, 10, 10), 1, VT=NTuple{2, Float64})
sjkelly commented 2 years ago

Okay so on v0.5.7 this was also an issue. It is not introduced in v0.6.0. Thanks for the report!