JuliaAI/ScientificTypes.jl

Coerce to union missing type

Closed this issue · 1 comments

When writing a tutorial, I encountered several time an issue such as this one:

MethodError: no method matching coerce(::Array{Union{Missing, Int64},1}, ::Type{Union{Missing, Continuous}}; verbosity=1)
Closest candidates are:
  coerce(::AbstractArray{#s26,1} where #s26<:Union{Missing, Real}, !Matched::Type{Continuous}; verbosity) at /Users/tlienart/.julia/dev/ScientificTypes/src/conventions/mlj/mlj.jl:54
  coerce(::AbstractArray{T,1} where T, !Matched::Type{Continuous}; verbosity) at /Users/tlienart/.julia/dev/ScientificTypes/src/conventions/mlj/mlj.jl:65
  coerce(::AbstractArray{#s26,1} where #s26<:Union{Missing, Integer}, !Matched::Type{Count}; verbosity) at /Users/tlienart/.julia/dev/ScientificTypes/src/conventions/mlj/mlj.jl:82
  ...
_coerce_col(::DataFrame, ::Symbol, ::Dict{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}; args::Base.Iterators.Pairs{Symbol,Int64,Tuple{Symbol},NamedTuple{(:verbosity,),Tuple{Int64}}}) at tables.jl:41
_coerce_col at tables.jl:38 [inlined]
#49 at none:0 [inlined]
iterate at generator.jl:47 [inlined]
collect_to!(::Array{AbstractArray{T,1} where T,1}, ::Base.Generator{NTuple{24,Symbol},ScientificTypes.var"#49#50"{Int64,DataFrame,Tuple{Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}},Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}}}}, ::Int64, ::Int64) at array.jl:681
collect_to!(::Array{CategoricalArray{T,1,UInt8,Int64,CategoricalValue{Int64,UInt8},U} where U where T,1}, ::Base.Generator{NTuple{24,Symbol},ScientificTypes.var"#49#50"{Int64,DataFrame,Tuple{Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}},Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}}}}, ::Int64, ::Int64) at array.jl:689
collect_to!(::Array{CategoricalArray{Union{Missing, Int64},1,UInt8,Int64,CategoricalValue{Int64,UInt8},Missing},1}, ::Base.Generator{NTuple{24,Symbol},ScientificTypes.var"#49#50"{Int64,DataFrame,Tuple{Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}},Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}}}}, ::Int64, ::Int64) at array.jl:689
collect_to_with_first!(::Array{CategoricalArray{Union{Missing, Int64},1,UInt8,Int64,CategoricalValue{Int64,UInt8},Missing},1}, ::CategoricalArray{Union{Missing, Int64},1,UInt8,Int64,CategoricalValue{Int64,UInt8},Missing}, ::Base.Generator{NTuple{24,Symbol},ScientificTypes.var"#49#50"{Int64,DataFrame,Tuple{Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}},Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}}}}, ::Int64) at array.jl:660
collect(::Base.Generator{NTuple{24,Symbol},ScientificTypes.var"#49#50"{Int64,DataFrame,Tuple{Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}},Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}}}}) at array.jl:641
_totuple at tuple.jl:269 [inlined]
Tuple at tuple.jl:241 [inlined]
NamedTuple{(:surgery, :age, :rectal_temperature, :pulse, :respiratory_rate, :temperature_extremities, :peripheral_pulse, :mucous_membranes, :capillary_refill_time, :pain, :peristalsis, :abdominal_distension, :nasogastric_tube, :nasogastric_reflux, :nasogastric_reflux_ph, :feces, :abdomen, :packed_cell_volume, :total_protein, :abdomcentesis_appearance, :abdomcentesis_total_protein, :outcome, :surgical_lesion, :cp_data),T} where T<:Tuple(::Base.Generator{NTuple{24,Symbol},ScientificTypes.var"#49#50"{Int64,DataFrame,Tuple{Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}},Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}}}}) at namedtuple.jl:86
coerce(::DataFrame, ::Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}, ::Vararg{Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}},N} where N; verbosity::Int64) at tables.jl:74
coerce(::DataFrame, ::Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}, ::Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}, ::Vararg{Pair{Symbol,Type{#s16} where #s16<:Union{Missing, Found}},N} where N) at tables.jl:71
#coerce#51 at tables.jl:79 [inlined]
coerce(::DataFrame, ::Dict{Symbol,Type{#s16} where #s16<:Union{Missing, Found}}) at tables.jl:79
top-level scope at EX-horse.jl:81

would be good to investigate, it seems that effectively what happens is a coerce where the target type is already a union missing (which should be fine afaik); worth investigating. Maybe by allowing the coerce to take a union target type (otherwise the method is not defined);

So maybe go from

function coerce(y::AbstractVector{<:Union{Missing,AbstractFloat}}, T::Type{Continuous};

to

function coerce(y::AbstractVector{<:Union{Missing,AbstractFloat}}, T::Type{<:Union{Missing,Continuous}};

This is addressed in #32