Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds table transforms #45

Merged
merged 18 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["lorenzoh <[email protected]>"]
version = "0.2.3"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
ColorBlendModes = "60508b50-96e1-4007-9d6c-f475c410f16b"
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
7 changes: 6 additions & 1 deletion src/DataAugmentation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module DataAugmentation

using ColorBlendModes
using CoordinateTransformations
using CategoricalArrays
using Distributions: Sampleable, Uniform, Categorical
using ImageDraw
using Images
Expand All @@ -28,6 +29,7 @@ include("./sequence.jl")
include("./items/arrayitem.jl")
include("./projective/base.jl")
include("./items/image.jl")
include("./items/table.jl")
include("./items/keypoints.jl")
include("./items/mask.jl")
include("./projective/compose.jl")
Expand All @@ -36,6 +38,7 @@ include("./projective/affine.jl")
include("./projective/warp.jl")
include("./oneof.jl")
include("./preprocessing.jl")
include("./rowtransforms.jl")
include("./colortransforms.jl")
include("testing.jl")
include("./visualization.jl")
Expand All @@ -49,6 +52,7 @@ export Item,
Sequence,
Project,
Image,
TabularItem,
Keypoints,
Polygon,
ToEltype,
Expand Down Expand Up @@ -88,7 +92,8 @@ export Item,
onehot,
showitems,
showgrid,
Bounds
Bounds,
getcategorypools


end # module
4 changes: 4 additions & 0 deletions src/items/table.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct TabularItem{T} <: Item
data::T
columns
end
manikyabard marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 50 additions & 0 deletions src/rowtransforms.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
struct NormalizeRow{T, S} <: DataAugmentation.Transform
normstats::T
normcols::S
end
NormalizeRow(normstats::T, normcols::S) where {T, S} = NormalizeRow{T, S}(normstats, normcols)
manikyabard marked this conversation as resolved.
Show resolved Hide resolved

struct Categorify{T, S} <: DataAugmentation.Transform
catdict::T
categorycols::S
end
Categorify(catdict::T, categorycols::S) where {T, S} = Categorify{T, S}(catdict, categorycols)
manikyabard marked this conversation as resolved.
Show resolved Hide resolved

struct FillMissing{T, S} <: DataAugmentation.Transform
fmvals::T
fmcols::S
end
FillMissing(fmvals::T, fmcols::S) where {T, S} = FillMissing{T, S}(fmvals, fmcols)
manikyabard marked this conversation as resolved.
Show resolved Hide resolved

function DataAugmentation.apply(tfm::FillMissing, item::TabularItem; randstate=nothing)
x = [val for val in item.data]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does collect(item.data) not work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to use that.

for col in tfm.fmcols
idx = findfirst(col .== item.columns)
if ismissing(x[idx])
x[idx] = tfm.fmvals[col]
end
end
x = (; zip(item.columns, x)...)
TabularItem(x, item.columns)
end

function DataAugmentation.apply(tfm::NormalizeRow, item::TabularItem; randstate=nothing)
x = [val for val in item.data]
for col in tfm.normcols
idx = findfirst(col .== item.columns)
colmean, colstd = tfm.normstats[col]
x[idx] = (x[idx] - colmean)/colstd
end
x = (; zip(item.columns, x)...)
TabularItem(x, item.columns)
end

function DataAugmentation.apply(tfm::Categorify, item::TabularItem; randstate=nothing)
x = [val for val in item.data]
for col in tfm.categorycols
idx = findfirst(col .== item.columns)
x[idx] = ismissing(x[idx]) ? 1 : findfirst(skipmissing(x[idx] .== tfm.catdict[col])) + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x[idx] = ismissing(x[idx]) ? 1 : findfirst(skipmissing(x[idx] .== tfm.catdict[col])) + 1
x[idx] = ismissing(x[idx]) ? 1 : findfirst(x[idx] .== tfm.catdict[col]) + 1

No need for skipmissing here, right? x[idx] is a value and tfm.catdict[col] is a vector of categorical values (which doesn't contain missing). findfirst is just assigning an index based on which symbol in tfm.catdict[col] matches x[idx].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I was thinking if someone creates catdict using unique or something, and if somehow missing is a part of this vector then an error could be thrown, but yeah it might just be better to remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to map(v -> filter!(!ismissing, v), values(catdict)) when constructing the transform. We could throw a warning when that happens too.

end
x = (; zip(item.columns, x)...)
TabularItem(x, item.columns)
end