7  Building pipelines

In previous chapters, we learned a large number of transforms for manipulating and processing geotables. In all those code examples, we used Juliaโ€™s pipe operator |> to apply the transform and send the resulting geotable to the next transform:

geotable |> transform1 |> transform2 |> ... |> viewer

In this chapter, we will learn two new powerful operators โ†’ and โŠ” provided by the framework to combine transforms into pipelines that can be optimized and reused with different geotables.

7.1 Motivation

The pipe operator |> in Julia is very convenient for sequential application of functions. Given an input x, we can type x |> f1 |> f2 to apply functions f1 and f2 in sequence, in a way that is equivalent to f2(f1(x)) or, alternatively, to the function composition (f2 โˆ˜ f1)(x). Its syntax can drastically improve code readability when the number of functions is large. However, the operator has a major limitation in the context of geospatial data science: it evaluates all intermediate results as soon as the data is inserted in the pipe. This is known in computer science as eager evaluation.

Taking the expression above as an example, the operator will first evaluate f1(x) and store the result in a variable y. After f1 is completed, the operator evaluates f2(y) and produces the final (desired) result. If y requires a lot of computer memory as it is usually the case with large geotables, the application of the pipeline will be slow.

Another evaluation strategy, known as lazy evaluation, consists of building the entire pipeline without the data in it. The major advantage of this strategy is that it can analyze the functions, and potentially simplify the code before evaluation. For example, the pipeline cos โ†’ acos can be replaced by the much simpler pipeline identity for some values of the input x.

7.2 Operator โ†’

In our framework, the operator โ†’ (\to) can be used in place of the pipe operator to build lazy sequential pipelines of transforms. Consider the synthetic data from previous chapters:

N = 10000
a = [2randn(Nรท2) .+ 6; randn(Nรท2)]
b = [3randn(Nรท2); 2randn(Nรท2)]
c = randn(N)
d = c .+ 0.6randn(N)

table = (; a, b, c, d)

gt = georef(table, CartesianGrid(100, 100))
10000ร—5 GeoTable over 100ร—100 CartesianGrid{2,Float64}
a b c d geometry
Continuous Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits]
8.83977 -1.65672 1.276 0.835859 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
6.22602 -2.29632 -1.11046 -0.642502 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
5.32921 -2.82279 -1.23917 -0.562263 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
7.94377 -2.17085 -0.701776 -1.36479 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
5.4261 -0.626655 1.57555 2.61682 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
7.67364 -4.89681 -0.230187 0.521983 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
6.83287 2.97137 1.62094 1.58288 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
3.69377 -1.72245 -2.15464 -2.32146 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
9.16341 -2.57812 -2.30637 -2.24406 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
4.21339 1.88798 -1.05639 -0.698802 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ

And suppose that we are interested in converting the columns โ€œaโ€, โ€œbโ€ and โ€œcโ€ of the geotable with the Quantile transform. Instead of creating the intermediate geotable with the Select transform, and then sending the result to the Quantile transform, we can create the entire pipeline without reference to the data:

pipeline = Select("a", "b", "c") โ†’ Quantile()
SequentialTransform
โ”œโ”€ Select([:a, :b, :c], nothing)
โ””โ”€ Quantile(all, Normal{Float64}(ฮผ=0.0, ฯƒ=1.0))

The operator โ†’ creates a special SequentialTransform, which can be applied like any other transform in the framework:

gt |> pipeline
10000ร—4 GeoTable over 100ร—100 CartesianGrid{2,Float64}
a b c geometry
Continuous Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits]
1.78046 -0.680165 1.29419 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
0.761765 -0.92263 -1.12592 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
0.491886 -1.12262 -1.25302 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
1.39904 -0.873483 -0.704374 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
0.514647 -0.253865 1.59729 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
1.28727 -1.86487 -0.237331 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
0.980174 1.21754 1.63428 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
0.152998 -0.703731 -2.15707 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
1.93983 -1.02917 -2.32261 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
0.23295 0.800537 -1.06605 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ

It will perform optimizations whenever possible. For instance, we know a priori that adding the Identity transform anywhere in the pipeline doesnโ€™t have any effect:

pipeline โ†’ Identity()
SequentialTransform
โ”œโ”€ Select([:a, :b, :c], nothing)
โ””โ”€ Quantile(all, Normal{Float64}(ฮผ=0.0, ฯƒ=1.0))

7.3 Operator โŠ”

The operator โŠ” (\sqcup) can be used to create lazy parallel transforms. There is no equivalent in Julia as this operator is very specific to tables. It combines the geotables produced by two or more pipelines into a single geotable with the disjoint union of all columns.

Letโ€™s illustrate this concept with two pipelines:

pipeline1 = Select("a") โ†’ Indicator("a", k=3)
SequentialTransform
โ”œโ”€ Select([:a], nothing)
โ””โ”€ Indicator(:a, 3, :quantile, false)
pipeline2 = Select("b", "c", "d") โ†’ PCA(maxdim=2)
SequentialTransform
โ”œโ”€ Select([:b, :c, :d], nothing)
โ”œโ”€ ZScore(all)
โ””โ”€ EigenAnalysis(:V, 2, 1.0)

The first pipeline creates 3 indicator variables from variable โ€œaโ€:

gt |> pipeline1
10000ร—4 GeoTable over 100ร—100 CartesianGrid{2,Float64}
a_1 a_2 a_3 geometry
Categorical Categorical Categorical Quadrangle
[NoUnits] [NoUnits] [NoUnits]
false false true Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
false false true Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
false false true Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
false false true Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
false false true Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
false false true Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
false false true Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
false true true Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
false false true Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
false true true Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ

The second pipeline runs principal component analysis with variables โ€œbโ€, โ€œcโ€ and โ€œdโ€ and produces 2 principal components:

gt |> pipeline2
10000ร—3 GeoTable over 100ร—100 CartesianGrid{2,Float64}
PC1 PC2 geometry
Continuous Continuous Quadrangle
[NoUnits] [NoUnits]
-1.43202 -0.609854 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
1.17278 -0.909732 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
1.21252 -1.11878 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
1.32205 -0.857163 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
-2.72651 -0.189985 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
-0.183824 -1.91267 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
-2.10525 1.22205 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
2.94771 -0.710164 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
3.00357 -1.04923 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
1.19496 0.738356 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
โ‹ฎ โ‹ฎ โ‹ฎ

We can combine the two pipelines into a single pipeline that executes in parallel:

pipeline = pipeline1 โŠ” pipeline2
ParallelTableTransform
โ”œโ”€ SequentialTransform
โ”‚  โ”œโ”€ Select([:a], nothing)
โ”‚  โ””โ”€ Indicator(:a, 3, :quantile, false)
โ””โ”€ SequentialTransform
   โ”œโ”€ Select([:b, :c, :d], nothing)
   โ”œโ”€ ZScore(all)
   โ””โ”€ EigenAnalysis(:V, 2, 1.0)
gt |> pipeline
10000ร—6 GeoTable over 100ร—100 CartesianGrid{2,Float64}
a_1 a_2 a_3 PC1 PC2 geometry
Categorical Categorical Categorical Continuous Continuous Quadrangle
[NoUnits] [NoUnits] [NoUnits] [NoUnits] [NoUnits]
false false true -1.43202 -0.609854 Quadrangle((0.0, 0.0), ..., (0.0, 1.0))
false false true 1.17278 -0.909732 Quadrangle((1.0, 0.0), ..., (1.0, 1.0))
false false true 1.21252 -1.11878 Quadrangle((2.0, 0.0), ..., (2.0, 1.0))
false false true 1.32205 -0.857163 Quadrangle((3.0, 0.0), ..., (3.0, 1.0))
false false true -2.72651 -0.189985 Quadrangle((4.0, 0.0), ..., (4.0, 1.0))
false false true -0.183824 -1.91267 Quadrangle((5.0, 0.0), ..., (5.0, 1.0))
false false true -2.10525 1.22205 Quadrangle((6.0, 0.0), ..., (6.0, 1.0))
false true true 2.94771 -0.710164 Quadrangle((7.0, 0.0), ..., (7.0, 1.0))
false false true 3.00357 -1.04923 Quadrangle((8.0, 0.0), ..., (8.0, 1.0))
false true true 1.19496 0.738356 Quadrangle((9.0, 0.0), ..., (9.0, 1.0))
โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ

All 5 columns are present in the final geotable.

7.4 Revertibility

An important concept related to pipelines that is very useful in geospatial data science is revertibility. The concept is useful whenever we need to answer geoscientific questions in terms of variables that have been transformed for geostatistical analysis.

Letโ€™s illustrate the concept with the following geotable and pipeline:

a = [-1.0, 4.0, 1.6, 3.4]
b = [1.6, 3.4, -1.0, 4.0]
c = [3.4, 2.0, 3.6, -1.0]
table = (; a, b, c)

geotable = georef(table, rand(2, 4))
4ร—4 GeoTable over 4 PointSet{2,Float64}
a b c geometry
Continuous Continuous Continuous Point2
[NoUnits] [NoUnits] [NoUnits]
-1.0 1.6 3.4 (0.169381, 0.090882)
4.0 3.4 2.0 (0.650155, 0.748317)
1.6 -1.0 3.6 (0.700675, 0.314274)
3.4 4.0 -1.0 (0.16536, 0.243279)
pipeline = Center()
Center transform
โ””โ”€ selector = all

We saw that our pipelines can be evaluated with Juliaโ€™s pipe operator:

geotable |> pipeline
4ร—4 GeoTable over 4 PointSet{2,Float64}
a b c geometry
Continuous Continuous Continuous Point2
[NoUnits] [NoUnits] [NoUnits]
-3.0 -0.4 1.4 (0.169381, 0.090882)
2.0 1.4 0.0 (0.650155, 0.748317)
-0.4 -3.0 1.6 (0.700675, 0.314274)
1.4 2.0 -3.0 (0.16536, 0.243279)

In order to revert a pipeline, however; we need to save auxiliary constants that were used to transform the data (e.g., mean of selected columns). The apply function serves this purpose:

newtable, cache = apply(pipeline, geotable)

newtable
4ร—4 GeoTable over 4 PointSet{2,Float64}
a b c geometry
Continuous Continuous Continuous Point2
[NoUnits] [NoUnits] [NoUnits]
-3.0 -0.4 1.4 (0.169381, 0.090882)
2.0 1.4 0.0 (0.650155, 0.748317)
-0.4 -3.0 1.6 (0.700675, 0.314274)
1.4 2.0 -3.0 (0.16536, 0.243279)

The function produces the new geotable as usual and an additional cache with all the information needed to revert the transforms in the pipeline. We say that a pipeline isrevertible, if there is an efficient way to revert its transforms starting from any geotable that has the same schema of the geotable produced by the apply function:

isrevertible(pipeline)
true
revert(pipeline, newtable, cache)
4ร—4 GeoTable over 4 PointSet{2,Float64}
a b c geometry
Continuous Continuous Continuous Point2
[NoUnits] [NoUnits] [NoUnits]
-1.0 1.6 3.4 (0.169381, 0.090882)
4.0 3.4 2.0 (0.650155, 0.748317)
1.6 -1.0 3.6 (0.700675, 0.314274)
3.4 4.0 -1.0 (0.16536, 0.243279)

A very common workflow in geospatial data science consists of:

  1. Transforming the data to an appropriate sample space for geostatistical analysis
  2. Doing additional modeling to predict variables in new geospatial locations
  3. Reverting the modeling results with the saved pipeline and cache

We will see examples of this workflow in Part V of the book.

7.5 Congratulations!

Congratulations on finishing Part II of the book. Letโ€™s quickly review what we learned so far:

  • Transforms and pipelines are powerful tools to achieve reproducible geospatial data science.
  • The operators โ†’ and โŠ” can be used to build lazy pipelines. After a pipeline is built, it can be applied to different geotables, which may have different types of geospatial domain.
  • Lazy pipelines can always be optimized for computational performance, and the Julia language really thrives to dispatch the appropriate optimizations when they are available.
  • Map projections are specific types of coordinate transforms. They can be combined with many other transforms in the framework to produce advanced geostatistical visualizations.

There is a long journey until the technology reaches its full potential. The good news is that Julia code is easy to read and modify, and you can become an active contributor after just a few weeks working with the language. We invite you to contribute new transforms and optimizations as soon as you feel comfortable with the framework.