Skip to content

Commit

Permalink
fix plotting Union{Missing,Real} arrays (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz authored Jul 11, 2022
1 parent 8309431 commit 59ca2e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
14 changes: 11 additions & 3 deletions RecipesPipeline/src/series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ _prepare_series_data(::Nothing) = nothing
_prepare_series_data(t::Tuple{T, T}) where {T <: Number} = t
_prepare_series_data(f::Function) = f
_prepare_series_data(ar::AbstractRange{<:Number}) = ar
function _prepare_series_data(a::AbstractArray{<:MaybeNumber})
f = isimmutable(a) ? replace : replace!
a = f(x -> ismissing(x) || isinf(x) ? NaN : x, map(float, a))
function _prepare_series_data(a::AbstractArray{T}) where {T<:MaybeNumber}
# Get a non-missing AbstractFloat type for the array
# There may be a better way to do this?
F = typeof(float(zero(nonmissingtype(T))))
# Create a new array with this type to write to
float_a = similar(a, F)
# Replace missing and inf values with NaN
broadcast!(float_a, a) do x
ismissing(x) || isinf(x) ? NaN : x
end
return float_a
end
_prepare_series_data(a::AbstractArray{<:Missing}) = fill(NaN, axes(a))
_prepare_series_data(a::AbstractArray{<:MaybeString}) =
Expand Down
23 changes: 23 additions & 0 deletions RecipesPipeline/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ using StatsPlots
using Test
using TestImages

import RecipesPipeline: _prepare_series_data

@testset "_prepare_series_data" begin
@test_throws ErrorException _prepare_series_data(:test)
@test _prepare_series_data(nothing) === nothing
@test _prepare_series_data((1.0, 2.0)) === (1.0, 2.0)
@test _prepare_series_data(identity) === identity
@test _prepare_series_data(1:5:10) === 1:5:10
a = ones(Union{Missing,Float64}, 100, 100);
sd = _prepare_series_data(a)
@test sd == a
@test eltype(sd) == Float64
a .= missing
sd = _prepare_series_data(a)
@test eltype(sd) == Float64
@test all(isnan, sd)
a = fill(missing, 100, 100)
sd = _prepare_series_data(a)
@test eltype(sd) == Float64
@test all(isnan, sd)
# TODO String, Volume etc
end

@testset "unzip" begin
x, y, z = unzip([(1., 2., 3.), (1., 2., 3.)])
@test all(x .== 1.) && all(y .== 2.) && all(z .== 3.)
Expand Down

0 comments on commit 59ca2e7

Please sign in to comment.