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:
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 =10000a = [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)gtb =georef(table, CartesianGrid(100, 100))
9990 rows omitted
10000×5 GeoTable over 100×100 CartesianGrid
a
b
c
d
geometry
Continuous
Continuous
Continuous
Continuous
Quadrangle
[NoUnits]
[NoUnits]
[NoUnits]
[NoUnits]
🖈 Cartesian{NoDatum}
5.69324
-0.318443
-0.437185
0.169292
Quadrangle((x: 0.0 m, y: 0.0 m), ..., (x: 0.0 m, y: 1.0 m))
6.59929
0.455213
1.47897
1.07508
Quadrangle((x: 1.0 m, y: 0.0 m), ..., (x: 1.0 m, y: 1.0 m))
6.69226
0.27549
0.87714
0.853007
Quadrangle((x: 2.0 m, y: 0.0 m), ..., (x: 2.0 m, y: 1.0 m))
3.67113
-0.262194
-1.08956
-1.17638
Quadrangle((x: 3.0 m, y: 0.0 m), ..., (x: 3.0 m, y: 1.0 m))
9.25026
-4.44372
-0.196566
-0.17191
Quadrangle((x: 4.0 m, y: 0.0 m), ..., (x: 4.0 m, y: 1.0 m))
9.07536
1.83108
0.0595348
-0.170243
Quadrangle((x: 5.0 m, y: 0.0 m), ..., (x: 5.0 m, y: 1.0 m))
7.53859
-1.17863
0.293477
0.41661
Quadrangle((x: 6.0 m, y: 0.0 m), ..., (x: 6.0 m, y: 1.0 m))
8.07012
5.16173
-0.953416
-0.79797
Quadrangle((x: 7.0 m, y: 0.0 m), ..., (x: 7.0 m, y: 1.0 m))
4.22035
1.72536
-0.890787
-1.74594
Quadrangle((x: 8.0 m, y: 0.0 m), ..., (x: 8.0 m, y: 1.0 m))
5.85789
-4.03981
0.880018
1.73982
Quadrangle((x: 9.0 m, y: 0.0 m), ..., (x: 9.0 m, y: 1.0 m))
⋮
⋮
⋮
⋮
⋮
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:
The operator → creates a special SequentialTransform, which can be applied like any other transform in the framework:
gtb |> pipeline
9990 rows omitted
10000×4 GeoTable over 100×100 CartesianGrid
a
b
c
geometry
Continuous
Continuous
Continuous
Quadrangle
[NoUnits]
[NoUnits]
[NoUnits]
🖈 Cartesian{NoDatum}
0.58017
-0.152998
-0.441571
Quadrangle((x: 0.0 m, y: 0.0 m), ..., (x: 0.0 m, y: 1.0 m))
0.876055
0.174083
1.46912
Quadrangle((x: 1.0 m, y: 0.0 m), ..., (x: 1.0 m, y: 1.0 m))
0.905879
0.100182
0.868721
Quadrangle((x: 2.0 m, y: 0.0 m), ..., (x: 2.0 m, y: 1.0 m))
0.160865
-0.126925
-1.09893
Quadrangle((x: 3.0 m, y: 0.0 m), ..., (x: 3.0 m, y: 1.0 m))
1.92523
-1.74148
-0.20087
Quadrangle((x: 4.0 m, y: 0.0 m), ..., (x: 4.0 m, y: 1.0 m))
1.84526
0.740494
0.0624556
Quadrangle((x: 5.0 m, y: 0.0 m), ..., (x: 5.0 m, y: 1.0 m))
1.21912
-0.503518
0.283492
Quadrangle((x: 6.0 m, y: 0.0 m), ..., (x: 6.0 m, y: 1.0 m))
1.42624
1.95486
-0.949434
Quadrangle((x: 7.0 m, y: 0.0 m), ..., (x: 7.0 m, y: 1.0 m))
0.243007
0.699243
-0.883808
Quadrangle((x: 8.0 m, y: 0.0 m), ..., (x: 8.0 m, y: 1.0 m))
0.639035
-1.59909
0.872749
Quadrangle((x: 9.0 m, y: 0.0 m), ..., (x: 9.0 m, y: 1.0 m))
⋮
⋮
⋮
⋮
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:
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.
Quadrangle((x: 0.0 m, y: 0.0 m), ..., (x: 0.0 m, y: 1.0 m))
false
false
true
-1.69747
0.153096
Quadrangle((x: 1.0 m, y: 0.0 m), ..., (x: 1.0 m, y: 1.0 m))
false
false
true
-1.13536
0.0866283
Quadrangle((x: 2.0 m, y: 0.0 m), ..., (x: 2.0 m, y: 1.0 m))
false
true
true
1.4932
-0.108655
Quadrangle((x: 3.0 m, y: 0.0 m), ..., (x: 3.0 m, y: 1.0 m))
false
false
true
0.257714
-1.73605
Quadrangle((x: 4.0 m, y: 0.0 m), ..., (x: 4.0 m, y: 1.0 m))
false
false
true
0.0630665
0.695603
Quadrangle((x: 5.0 m, y: 0.0 m), ..., (x: 5.0 m, y: 1.0 m))
false
false
true
-0.453543
-0.473583
Quadrangle((x: 6.0 m, y: 0.0 m), ..., (x: 6.0 m, y: 1.0 m))
false
false
true
1.15646
1.99288
Quadrangle((x: 7.0 m, y: 0.0 m), ..., (x: 7.0 m, y: 1.0 m))
false
true
true
1.69432
0.662209
Quadrangle((x: 8.0 m, y: 0.0 m), ..., (x: 8.0 m, y: 1.0 m))
false
false
true
-1.66776
-1.58832
Quadrangle((x: 9.0 m, y: 0.0 m), ..., (x: 9.0 m, y: 1.0 m))
⋮
⋮
⋮
⋮
⋮
⋮
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:
We saw that our pipelines can be evaluated with Julia’s pipe operator:
gtb |> pipeline
4×4 GeoTable over 4 PointSet
a
b
c
geometry
Continuous
Continuous
Continuous
Point
[NoUnits]
[NoUnits]
[NoUnits]
🖈 Cartesian{NoDatum}
-3.0
-0.4
1.4
(x: 0.0 m, y: 0.0 m)
2.0
1.4
0.0
(x: 1.0 m, y: 0.0 m)
-0.4
-3.0
1.6
(x: 1.0 m, y: 1.0 m)
1.4
2.0
-3.0
(x: 0.0 m, y: 1.0 m)
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:
newgtb, cache =apply(pipeline, gtb)newgtb
4×4 GeoTable over 4 PointSet
a
b
c
geometry
Continuous
Continuous
Continuous
Point
[NoUnits]
[NoUnits]
[NoUnits]
🖈 Cartesian{NoDatum}
-3.0
-0.4
1.4
(x: 0.0 m, y: 0.0 m)
2.0
1.4
0.0
(x: 1.0 m, y: 0.0 m)
-0.4
-3.0
1.6
(x: 1.0 m, y: 1.0 m)
1.4
2.0
-3.0
(x: 0.0 m, y: 1.0 m)
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, newgtb, cache)
4×4 GeoTable over 4 PointSet
a
b
c
geometry
Continuous
Continuous
Continuous
Point
[NoUnits]
[NoUnits]
[NoUnits]
🖈 Cartesian{NoDatum}
-1.0
1.6
3.4
(x: 0.0 m, y: 0.0 m)
4.0
3.4
2.0
(x: 1.0 m, y: 0.0 m)
1.6
-1.0
3.6
(x: 1.0 m, y: 1.0 m)
3.4
4.0
-1.0
(x: 0.0 m, y: 1.0 m)
A very common workflow in geospatial data science consists of:
Transforming the data to an appropriate sample space for geostatistical analysis
Doing additional modeling to predict variables in new geospatial locations
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.