Skip to content

Commit a98e169

Browse files
eliascarvjuliohm
andauthored
Add GeoTables.jl extension (#244)
* Add GeoTables.jl extension * Apply suggestions * Reorder files in runtests.jl --------- Co-authored-by: Júlio Hoffimann <[email protected]>
1 parent 5486ea3 commit a98e169

File tree

5 files changed

+206
-2
lines changed

5 files changed

+206
-2
lines changed

Project.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ Transducers = "28d57a85-8fef-5791-bfe6-a80928e7c999"
2222
TransformsBase = "28dd2a49-a57a-4bfb-84ca-1a49db9b96b8"
2323
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
2424

25+
[weakdeps]
26+
GeoTables = "e502b557-6362-48c1-8219-d30d308dcdb0"
27+
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
28+
29+
[extensions]
30+
TableTransformsGeoTablesExt = ["GeoTables", "Meshes"]
31+
2532
[compat]
2633
AbstractTrees = "0.4"
2734
CategoricalArrays = "0.10"
2835
CoDa = "1.2"
2936
ColumnSelectors = "0.1"
3037
DataScienceTraits = "0.2"
3138
Distributions = "0.25"
39+
GeoTables = "1.9"
3240
InverseFunctions = "0.1"
3341
LinearAlgebra = "1.9"
42+
Meshes = "0.36"
3443
NelderMead = "0.4"
3544
PrettyTables = "2"
3645
Random = "1.9"

ext/TableTransformsGeoTablesExt.jl

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# ------------------------------------------------------------------
2+
# Licensed under the MIT License. See LICENSE in the project root.
3+
# ------------------------------------------------------------------
4+
5+
module TableTransformsGeoTablesExt
6+
7+
using Meshes
8+
using GeoTables
9+
using TableTransforms
10+
11+
import TableTransforms: divide, attach
12+
import TableTransforms: applymeta, revertmeta
13+
14+
# table traits
15+
16+
divide(geotable::AbstractGeoTable) = values(geotable), domain(geotable)
17+
attach(table, dom::Domain) = georef(table, dom)
18+
19+
# transforms that change the order or number of
20+
# rows in the table need a special treatment
21+
22+
function applymeta(::Sort, dom::Domain, prep)
23+
sinds = prep
24+
25+
sdom = view(dom, sinds)
26+
27+
sdom, sinds
28+
end
29+
30+
function revertmeta(::Sort, newdom::Domain, mcache)
31+
sinds = mcache
32+
rinds = sortperm(sinds)
33+
34+
view(newdom, rinds)
35+
end
36+
37+
# --------------------------------------------------
38+
39+
function applymeta(::Filter, dom::Domain, prep)
40+
sinds, rinds = prep
41+
42+
sdom = view(dom, sinds)
43+
rdom = view(dom, rinds)
44+
45+
sdom, (rinds, rdom)
46+
end
47+
48+
function revertmeta(::Filter, newdom::Domain, mcache)
49+
geoms = collect(newdom)
50+
51+
rinds, rdom = mcache
52+
for (i, geom) in zip(rinds, rdom)
53+
insert!(geoms, i, geom)
54+
end
55+
56+
GeometrySet(geoms)
57+
end
58+
59+
# --------------------------------------------------
60+
61+
function applymeta(::DropMissing, dom::Domain, prep)
62+
ftrans, fprep, _ = prep
63+
newmeta, fmcache = applymeta(ftrans, dom, fprep)
64+
newmeta, (ftrans, fmcache)
65+
end
66+
67+
function revertmeta(::DropMissing, newdom::Domain, mcache)
68+
ftrans, fmcache = mcache
69+
revertmeta(ftrans, newdom, fmcache)
70+
end
71+
72+
# --------------------------------------------------
73+
74+
function applymeta(::DropExtrema, dom::Domain, prep)
75+
ftrans, fprep = prep
76+
newmeta, fmcache = applymeta(ftrans, dom, fprep)
77+
newmeta, (ftrans, fmcache)
78+
end
79+
80+
function revertmeta(::DropExtrema, newdom::Domain, mcache)
81+
ftrans, fmcache = mcache
82+
revertmeta(ftrans, newdom, fmcache)
83+
end
84+
85+
# --------------------------------------------------
86+
87+
function applymeta(::Sample, dom::Domain, prep)
88+
sinds, rinds = prep
89+
90+
sdom = view(dom, sinds)
91+
rdom = view(dom, rinds)
92+
93+
sdom, (sinds, rinds, rdom)
94+
end
95+
96+
function revertmeta(::Sample, newdom::Domain, mcache)
97+
geoms = collect(newdom)
98+
99+
sinds, rinds, rdom = mcache
100+
101+
uinds = indexin(sort(unique(sinds)), sinds)
102+
ugeoms = [geoms[i] for i in uinds]
103+
104+
for (i, geom) in zip(rinds, rdom)
105+
insert!(ugeoms, i, geom)
106+
end
107+
108+
GeometrySet(ugeoms)
109+
end
110+
111+
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CoDa = "5900dafe-f573-5c72-b367-76665857777b"
55
ColumnSelectors = "9cc86067-7e36-4c61-b350-1ac9833d277f"
66
DataScienceTraits = "6cb2f572-2d2b-4ba6-bdb3-e710fa044d6c"
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
8+
GeoTables = "e502b557-6362-48c1-8219-d30d308dcdb0"
89
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
910
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1011
PairPlots = "43a3c2be-4208-490b-832a-a21dcd55d7da"

test/geotables.jl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
@testset "GeoTables" begin
2+
# transforms that are revertible
3+
d = georef((z=rand(100), w=rand(100)))
4+
for p in [
5+
Select(:z),
6+
Reject(:z),
7+
Satisfies(x -> true),
8+
Rename(:z => :a),
9+
StdNames(),
10+
StdFeats(),
11+
Sort(:z),
12+
Sample(10),
13+
Filter(x -> true),
14+
DropMissing(),
15+
DropExtrema(:z),
16+
Map(:z => identity),
17+
Replace(1.0 => 2.0),
18+
Coalesce(value=0.0),
19+
Coerce(:z => DST.Continuous),
20+
Indicator(:z),
21+
Identity(),
22+
Center(),
23+
Scale(),
24+
MinMax(),
25+
Interquartile(),
26+
ZScore(),
27+
Quantile(),
28+
Functional(exp),
29+
EigenAnalysis(:V),
30+
PCA(),
31+
DRS(),
32+
SDS(),
33+
RowTable(),
34+
ColTable()
35+
]
36+
n, c = apply(p, d)
37+
t = Tables.columns(n)
38+
r = revert(p, n, c)
39+
@test n isa AbstractGeoTable
40+
@test r isa AbstractGeoTable
41+
end
42+
43+
# transforms with categorical variables
44+
d = georef((c=categorical([1, 2, 3]),))
45+
for p in [Levels(:c => [1, 2, 3]), OneHot(:c)]
46+
n, c = apply(p, d)
47+
t = Tables.columns(n)
48+
r = revert(p, n, c)
49+
@test n isa AbstractGeoTable
50+
@test r isa AbstractGeoTable
51+
end
52+
53+
d = georef((z=rand(100), w=rand(100)))
54+
p = Select(:w)
55+
n, c = apply(p, d)
56+
t = Tables.columns(n)
57+
@test Tables.columnnames(t) == (:w, :geometry)
58+
59+
d = georef((z=rand(100), w=rand(100)))
60+
p = Sample(100)
61+
n, c = apply(p, d)
62+
r = revert(p, n, c)
63+
@test r == d
64+
t = Tables.columns(n)
65+
@test Tables.columnnames(t) == (:z, :w, :geometry)
66+
67+
d = georef((a=[1, missing, 3], b=[3, 2, 1]))
68+
p = DropMissing()
69+
n, c = apply(p, d)
70+
@test Tables.columns(values(n)) == (a=[1, 3], b=[3, 1])
71+
@test length(domain(n)) == 2
72+
r = revert(p, n, c)
73+
@test r == d
74+
end

test/runtests.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using TableTransforms
22
using CoDa
33
using Tables
44
using Unitful
5+
using GeoTables
56
using TypedTables
67
using CategoricalArrays
78
using LinearAlgebra
@@ -43,8 +44,16 @@ Polynomial(args::T...) where {T<:Real} = Polynomial(collect(args))
4344
include("metatable.jl")
4445

4546
# list of tests
46-
testfiles =
47-
["distributions.jl", "assertions.jl", "transforms.jl", "metadata.jl", "tableselection.jl", "tablerows.jl", "shows.jl"]
47+
testfiles = [
48+
"distributions.jl",
49+
"assertions.jl",
50+
"tableselection.jl",
51+
"tablerows.jl",
52+
"transforms.jl",
53+
"metadata.jl",
54+
"shows.jl",
55+
"geotables.jl"
56+
]
4857

4958
@testset "TableTransforms.jl" begin
5059
for testfile in testfiles

0 commit comments

Comments
 (0)