Skip to content

Commit 042d19e

Browse files
authored
Skip nan time steps and classify the rqa trend results into UInt8 (#101)
* Check for nan time steps and skip them * Add sentence about scope of the project * Add UInt8 classification This adds a classification into UInt8 buckets because we don't need the full accuracy of Float. This is a good compromise between flexibility and size of the output data. * Make lower and upperbound kwargs * Fix tests * Fix tests
1 parent 943cb84 commit 042d19e

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a"
2121
GeoFormatTypes = "68eda718-8dee-11e9-39e7-89f7f65f511f"
2222
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
2323
KML = "1284bf3a-1e3d-4f4e-a7a9-b9d235a28f35"
24+
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
2425
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
2526
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
2627
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
@@ -61,6 +62,7 @@ GDAL = "1"
6162
GeoFormatTypes = "0.4"
6263
Glob = "1"
6364
KML = "0.2"
65+
LazyArtifacts = "1.11.0"
6466
Libdl = "1.10"
6567
LinearAlgebra = "1.10"
6668
LoggingExtras = "1"
@@ -82,7 +84,7 @@ TestItems = "1.0"
8284
TimeseriesSurrogates = "2"
8385
UnicodePlots = "3"
8486
YAXArrayBase = "0.6, 0.7"
85-
YAXArrays = "0.5, 0.6"
87+
YAXArrays = "0.5, 0.6, 0.7"
8688
Zarr = "0.9"
8789
julia = "1.11"
8890

@@ -91,13 +93,13 @@ AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a"
9193
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
9294
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
9395
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
96+
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
9497
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
9598
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
9699
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
97100
StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a"
98101
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
99102
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
100-
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
101103

102104
[targets]
103105
test = ["Test", "TestItemRunner", "Pkg", "Random", "AllocCheck", "BenchmarkTools", "Aqua", "Documenter", "StaticTools", "PythonCall", "Libdl"]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
This is the Julia package developed within the [FAIRSenDD project](https://github.com/EarthyScience/FAIRSenDD).
1010

11+
The aim of this project is to provide a forest change detection algorithm in Julia.
12+
1113
## Citation
1214

1315
F. Cremer, M. Urbazaev, J. Cortés, J. Truckenbrodt, C. Schmullius and C. Thiel, "Potential of Recurrence Metrics from Sentinel-1 Time Series for Deforestation Mapping," in IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing, vol. 13, pp. 5233-5240, 2020, doi: [10.1109/JSTARS.2020.3019333](https://dx.doi.org/10.1109/JSTARS.2020.3019333).

src/rqatrend.jl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ using Distances
88
Compute the RQA trend metric for the datacube `cube` with the epsilon threshold `thresh`.
99
"""
1010
function rqatrend(cube; thresh=2, outpath=tempname() * ".zarr", overwrite=false, kwargs...)
11-
@show outpath
12-
mapCube(rqatrend, cube, thresh; indims=InDims("Time"), outdims=OutDims(; outtype=Float32, path=outpath, overwrite, kwargs...))
11+
mapCube(rqatrend, cube, thresh; indims=InDims("Time"), outdims=OutDims(; outtype=UInt8, path=outpath, overwrite, kwargs...))
1312
end
1413

1514
@testitem "rqatrend cube" begin
@@ -31,8 +30,8 @@ end
3130

3231
mock_trend = rqatrend(mock_cube; thresh=0.5)
3332
@test mock_trend.axes == (mock_cube.X, mock_cube.Y)
34-
diff = abs(mean(mock_trend))
35-
@test diff < 0.5
33+
@test eltype(mock_trend) == Union{Missing, UInt8}
34+
3635
end
3736

3837
"""rqatrend(path::AbstractString; thresh=2, outpath=tempname()*".zarr")
@@ -48,10 +47,24 @@ rqatrend(xout, xin, thresh)
4847
Compute the RQA trend metric for the non-missing time steps of xin, and save it to xout.
4948
`thresh` specifies the epsilon threshold of the Recurrence Plot computation
5049
"""
51-
function rqatrend(pix_trend, pix, thresh=2)
52-
pix_trend .= rqatrend_impl(pix; thresh)
50+
function rqatrend(pix_trend, pix, thresh=2, lowerbound=-5., upperbound=-0.5)
51+
pix_trend .= classify_rqatrend(rqatrend_impl(pix; thresh); lowerbound, upperbound)
52+
end
53+
54+
function classify_rqatrend(trend; lowerbound=Float32(-5.0), upperbound=Float32(-0.5))
55+
ctrend = clamp(trend, lowerbound, upperbound)
56+
rlength = upperbound - lowerbound
57+
return round(UInt8, 255-((ctrend - lowerbound) / rlength) * 255)
5358
end
5459

60+
@testitem "classify_rqatrend" begin
61+
import AllocCheck
62+
@test RQADeforestation.classify_rqatrend(-4.999) === UInt8(255)
63+
@test RQADeforestation.classify_rqatrend(1) === UInt8(0)
64+
@test RQADeforestation.classify_rqatrend(-0.52) === UInt8(1)
65+
@test RQADeforestation.classify_rqatrend(-6) === UInt8(255)
66+
@test isempty( AllocCheck.check_allocs(RQADeforestation.classify_rqatrend, (Float32,)))
67+
end
5568

5669
function rqatrend_impl(data; thresh=2, border=10, theiler=1, metric=CheckedEuclidean())
5770
# simplified implementation of https://stats.stackexchange.com/a/370175 and https://github.com/joshday/OnlineStats.jl/blob/b89a99679b13e3047ff9c93a03c303c357931832/src/stats/linreg.jl
@@ -117,7 +130,7 @@ function tau_rr(y, d; thresh=2, metric=CheckedEuclidean())
117130
nominator = 0
118131
denominator = 0
119132
@inbounds for i in 1:length(y)-d
120-
if y[i] === missing || y[i+d] === missing
133+
if y[i] === missing || y[i+d] === missing|| isnan(y[i]) || isnan(y[i+d])
121134
continue
122135
end
123136
nominator += evaluate(metric, y[i], y[i+d]) <= _thresh

test/testdata.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
@testitem "testdata main" begin
33
import Pkg: Artifacts.@artifact_str
4+
using LazyArtifacts
45
testdatapath = artifact"rqatestdata/RQADeforestationTestData-2.0"
56

67
testdir = tempname()
@@ -24,8 +25,6 @@
2425
a = open_dataset(outdir * "/E051N018T3_rqatrend_VH_D022_thresh_3.0.zarr").layer
2526

2627
@test size(a) == (50, 74)
27-
@test minimum(a) < 0
28-
@test maximum(a) > 0
2928
end
3029

3130
@testitem "testdata julia_main" begin

0 commit comments

Comments
 (0)