|
| 1 | +""" |
| 2 | + OneOf(tfms) |
| 3 | + OneOf(tfms, ps) |
| 4 | +
|
| 5 | +Apply one of `tfms` selected randomly with probability `ps` each |
| 6 | +or uniformly chosen if no `ps` is given. |
| 7 | +""" |
| 8 | +struct OneOf{T<:Transform, D<:Sampleable} <: Transform |
| 9 | + tfms::Vector{T} |
| 10 | + dist::D |
| 11 | +end |
| 12 | + |
| 13 | +function OneOf(tfms::Vector{<:Transform}, ps = ones(length(tfms)) ./ length(tfms)) |
| 14 | + @assert length(tfms) == length(ps) |
| 15 | + return OneOf(tfms, Categorical(ps)) |
| 16 | +end |
| 17 | + |
| 18 | +function getrandstate(oneof::OneOf) |
| 19 | + i = rand(oneof.dist) |
| 20 | + return i, getrandstate(oneof.tfms[i]) |
| 21 | +end |
| 22 | + |
| 23 | + |
| 24 | +function apply(oneof::OneOf, item::AbstractItem; randstate = getrandstate(oneof)) |
| 25 | + i, tfmrandstate = randstate |
| 26 | + return apply(oneof.tfms[i], item; randstate = randstate) |
| 27 | +end |
| 28 | + |
| 29 | + |
| 30 | +function makebuffer(oneof::OneOf, items) |
| 31 | + return [makebuffer(tfm, items) for tfm in oneof.tfms] |
| 32 | +end |
| 33 | + |
| 34 | +function apply!(bufs, oneof::OneOf, item::AbstractItem; randstate = getrandstate(tfm)) |
| 35 | + i, tfm, tfmrandstate = randstate |
| 36 | + buf = bufs[i] |
| 37 | + return apply!(buf, tfm, item; randstate = randstate) |
| 38 | +end |
| 39 | + |
| 40 | +""" |
| 41 | + Maybe(tfm, p = 0.5) <: Transform |
| 42 | +
|
| 43 | +With probability `p`, apply transformation `tfm`. |
| 44 | +""" |
| 45 | +Maybe(tfm, p = 0.5) = OneOf([tfm, Identity()], [p, 1-p]) |
| 46 | + |
| 47 | + |
| 48 | +struct OneOfProjective{T<:Transform, D<:Sampleable} <: ProjectiveTransform |
| 49 | + tfms::Vector{T} |
| 50 | + dist::D |
| 51 | +end |
| 52 | + |
| 53 | + |
| 54 | +function OneOf(tfms::Vector{<:ProjectiveTransform}, ps = ones(length(tfms)) ./ length(tfms)) |
| 55 | + @assert length(tfms) == length(ps) |
| 56 | + return OneOfProjective(tfms, Categorical(ps)) |
| 57 | +end |
| 58 | + |
| 59 | + |
| 60 | +function getrandstate(oneof::OneOfProjective) |
| 61 | + i = rand(oneof.dist) |
| 62 | + return i, getrandstate(oneof.tfms[i]) |
| 63 | +end |
| 64 | + |
| 65 | + |
| 66 | +function Maybe(tfm::ProjectiveTransform, p = 1/2) |
| 67 | + return OneOf([tfm, Project(IdentityTransformation())], [p, 1-p]) |
| 68 | +end |
| 69 | + |
| 70 | + |
| 71 | +function getprojection(oneof::OneOfProjective, bounds; randstate = getrandstate(oneof)) |
| 72 | + i, tfmrandstate = randstate |
| 73 | + return getprojection(oneof.tfms[i], bounds; randstate = tfmrandstate) |
| 74 | +end |
| 75 | + |
| 76 | + |
| 77 | +function makebuffer(oneof::OneOfProjective, items) |
| 78 | + return [makebuffer(tfm, items) for tfm in oneof.tfms] |
| 79 | +end |
0 commit comments