Skip to content
Open
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 106 additions & 8 deletions src/Missings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Missings

export allowmissing, disallowmissing, ismissing, missing, missings,
Missing, MissingException, levels, coalesce, passmissing, nonmissingtype,
skipmissings
skipmissings, spreadmissings

using Base: ismissing, missing, Missing, MissingException
using Base: @deprecate
Expand Down Expand Up @@ -208,6 +208,104 @@ missing
"""
passmissing(f) = PassMissing{Core.Typeof(f)}(f)

struct SpreadMissings{F} <: Function
f::F
end

function (f::SpreadMissings{F})(x) where {F}
@assert x isa AbstractVector

if x isa AbstractVector{>:Missing}
nonmissinginds = collect(eachindex(skipmissing(x)))
res = f.f(view(x, nonmissinginds))

out = Vector{Union{Missing, eltype(res)}}(missing, length(x))
out[nonmissinginds] .= res

return out
else
return f.f(x)
end
end

function (f::SpreadMissings{F})(xs...) where {F}
if any(x -> x isa AbstractVector{>:Missing}, xs)
vecs = Base.filter(x -> x isa AbstractVector, xs)
s = skipmissings(vecs...)
vecs_counter = 1
newargs = ntuple(length(xs)) do i
if xs[i] isa AbstractVector
t = s[vecs_counter]
vecs_counter += 1
else
t = xs[i]
end
t
end

res = f.f(newargs...)

if res isa AbstractVector
out = Vector{Union{Missing, eltype(res)}}(missing, length(first(vecs)))
out[collect(eachindex(first(s)))] .= res
else
out = Vector{Union{Missing, typeof(res)}}(missing, length(first(vecs)))
out[collect(eachindex(first(s)))] .= Ref(res)
end

return out
else
return f.f(xs...)
end
end

"""
spreadmissings(f)

Given a function `f`, wraps a function `f` but performs a transformation
on arguments before executing. Given the call

```
spreadmissings(f)(x::AbstractVector, y::Integer, z::AbstractVector)
```

will construct the intermedaite variables

```
sx, sy = skipmissings(x, y)
```

and call

```
f(sx, y, sy)

```

# Examples
```
julia> x = [0, 1, 2, missing]; y = [-1, 0, missing, 2];

julia> function restricted_fun(x, y)
map(x, y) do xi, yi
if xi < 1 || yi < 1 # will error on missings
return 1
else
return 2
end
end
end;

julia> spreadmissings(restricted_fun)(x, y)
4-element Vector{Union{Missing, Int64}}:
1
1
missing
missing
```
"""
spreadmissings(f) = SpreadMissings{Core.Typeof(f)}(f)

"""
skipmissings(args...)

Expand Down Expand Up @@ -258,7 +356,7 @@ struct SkipMissings{V, T}
others::T
end

Base.@propagate_inbounds function _anymissingindex(others::Tuple{Vararg{AbstractArray}}, i)
Base.@propagate_inbounds function _anymissingindex(others::Tuple{Vararg{AbstractArray}}, i)
for oth in others
oth[i] === missing && return true
end
Expand All @@ -267,7 +365,7 @@ Base.@propagate_inbounds function _anymissingindex(others::Tuple{Vararg{Abstract
end

@inline function _anymissingiterate(others::Tuple, state)
for oth in others
for oth in others
y = iterate(oth, state)
y !== nothing && first(y) === missing && return true
end
Expand All @@ -278,7 +376,7 @@ end
const SkipMissingsofArrays = SkipMissings{V, T} where
{V <: AbstractArray, T <: Tuple{Vararg{AbstractArray}}}

function Base.show(io::IO, mime::MIME"text/plain", itr::SkipMissings{V}) where V
function Base.show(io::IO, mime::MIME"text/plain", itr::SkipMissings{V}) where V
print(io, SkipMissings, '{', V, '}', '(', itr.x, ')', " comprised of " *
"$(length(itr.others) + 1) iterators")
end
Expand Down Expand Up @@ -335,7 +433,7 @@ end
@inline function Base.getindex(itr::SkipMissingsofArrays, i)
@boundscheck checkbounds(itr.x, i)
@inbounds xi = itr.x[i]
if xi === missing || @inbounds _anymissingindex(itr.others, i)
if xi === missing || @inbounds _anymissingindex(itr.others, i)
throw(MissingException("the value at index $i is missing for some element"))
end
return xi
Expand Down Expand Up @@ -380,9 +478,9 @@ Base.mapreduce_impl(f, op, A::SkipMissingsofArrays, ifirst::Integer, ilast::Inte
A = itr.x
if ifirst == ilast
@inbounds a1 = A[ifirst]
if a1 === missing
if a1 === missing
return nothing
elseif _anymissingindex(itr.others, ifirst)
elseif _anymissingindex(itr.others, ifirst)
return nothing
else
return Some(Base.mapreduce_first(f, op, a1))
Expand Down Expand Up @@ -436,7 +534,7 @@ end
Return a vector similar to the array wrapped by the given `SkipMissings` iterator
but skipping all elements with a `missing` value in one of the iterators passed
to `skipmissing` and elements for which `f` returns `false`. This method
only applies when all iterators passed to `skipmissings` are arrays.
only applies when all iterators passed to `skipmissings` are arrays.

# Examples
```
Expand Down