From 4052196d0b48345bf5f3f26ba74b71a7abe883a9 Mon Sep 17 00:00:00 2001 From: Dongdong Kong Date: Thu, 3 Oct 2024 11:53:53 +0800 Subject: [PATCH] minor --- ext/IpaperNaNExt.jl | 2 +- ext/IpaperSlopeExt/slope_mk.jl | 10 ++++------ src/sf/Ops.jl | 10 +++++----- src/sf/SpatRaster.jl | 21 ++++++++++----------- src/sf/st_resample.jl | 10 +++++----- src/tools.jl | 12 ++++++------ test/sf/test-rast.jl | 18 +++++++++++++++++- test/sf/test-st_extract.jl | 4 ++-- test/test-Ipaper.jl | 9 ++++++++- 9 files changed, 58 insertions(+), 38 deletions(-) diff --git a/ext/IpaperNaNExt.jl b/ext/IpaperNaNExt.jl index 8b5a1ad..a4b995e 100644 --- a/ext/IpaperNaNExt.jl +++ b/ext/IpaperNaNExt.jl @@ -11,7 +11,7 @@ function _nanquantile!(q::AbstractVector, x::AbstractVector, for k = eachindex(probs) q[k] = NaNStatistics._nanquantile!(x, probs[k], (1,))[1] end - q + return q end function _nanquantile!(A, q::Real, dims::Int64) diff --git a/ext/IpaperSlopeExt/slope_mk.jl b/ext/IpaperSlopeExt/slope_mk.jl index 082ce18..a49d641 100644 --- a/ext/IpaperSlopeExt/slope_mk.jl +++ b/ext/IpaperSlopeExt/slope_mk.jl @@ -101,17 +101,15 @@ function slope_mk(y::AbstractVector, x::AbstractVector=1:length(y); end VS = var_S * essf - if S == 0 - z = 0.0 - z0 = 0.0 - elseif S > 0 + z = 0.0 + z0 = 0.0 + if S > 0 z = (S - 1) / sqrt2(VS) z0 = (S - 1) / sqrt(var_S) - else + elseif S < 0 z = (S + 1) / sqrt2(VS) z0 = (S + 1) / sqrt(var_S) end - # pvalue0 = 2 * pnorm(-abs(z0)) # Tau = S / (0.5 * n * (n - 1)) pvalue = 2 * pnorm(-abs(z)) diff --git a/src/sf/Ops.jl b/src/sf/Ops.jl index 7c8b0af..588733c 100644 --- a/src/sf/Ops.jl +++ b/src/sf/Ops.jl @@ -1,7 +1,6 @@ # https://gdal.org/tutorials/geotransforms_tut.html function getgeotransform(ra::AbstractSpatRaster) x, y = st_dims(ra) - cellx, celly = st_cellsize(ra) y0 = y[1] - celly / 2 x0 = x[1] - cellx / 2 @@ -9,8 +8,9 @@ function getgeotransform(ra::AbstractSpatRaster) end -flipud(x::AbstractArray{T,2}) where {T<:Real} = x[end:-1:1, :] -flipud(x::AbstractArray{T,3}) where {T<:Real} = x[end:-1:1, :, :] +flipud(x::AbstractArray{T,2}) where {T<:Real} = @view x[end:-1:1, :] +flipud(x::AbstractArray{T,3}) where {T<:Real} = @view x[end:-1:1, :, :] +fliplr(x::AbstractArray{T,2}) where {T<:Real} = @view x[:, end:-1:1] +fliplr(x::AbstractArray{T,3}) where {T<:Real} = @view x[:, end:-1:1, :] -fliplr(x::AbstractArray{T,2}) where {T<:Real} = x[:, end:-1:1] -fliplr(x::AbstractArray{T,3}) where {T<:Real} = x[:, end:-1:1, :] +export flipud, fliplr diff --git a/src/sf/SpatRaster.jl b/src/sf/SpatRaster.jl index 2e1976e..562350a 100644 --- a/src/sf/SpatRaster.jl +++ b/src/sf/SpatRaster.jl @@ -25,7 +25,7 @@ function SpatRaster(A::AbstractArray{T,N}, b::bbox; reverse_lat=true, time=nothi SpatRaster(; A, b, cellsize, lon, lat, time, bands, name) end -function SpatRaster(r::SpatRaster, A::AbstractArray; reverse_lat=true) +function SpatRaster(A::AbstractArray, r::SpatRaster; reverse_lat=true) (; b, time, bands, name) = r if size(A)[1:2] != size(r.A)[1:2] lon, lat = bbox2dims(b; size=size(A), reverse_lat) @@ -45,13 +45,12 @@ end Base.ndims(ra::AbstractSpatRaster) = ndims(ra.A) Base.size(ra::AbstractSpatRaster) = size(ra.A) Base.size(ra::AbstractSpatRaster{T,2}) where {T} = (size(ra.A)..., 1) - -Base.parent(ra::AbstractSpatRaster) = ra.A -Base.iterate(ra::AbstractSpatRaster) = iterate(ra.A) -Base.length(ra::AbstractSpatRaster) = length(ra.A) +# Base.parent(ra::AbstractSpatRaster) = ra.A +# Base.iterate(ra::AbstractSpatRaster) = iterate(ra.A) +# Base.length(ra::AbstractSpatRaster) = length(ra.A) # Base.size(ra::AbstractSpatRaster) = size(ra.A) -Base.eltype(::Type{AbstractSpatRaster{T}}) where {T} = T -Base.map(f, ra::AbstractSpatRaster) = SpatRaster(ra, map(f, ra.A)) +# Base.eltype(::Type{AbstractSpatRaster{T}}) where {T} = T +# Base.map(f, ra::AbstractSpatRaster) = SpatRaster(map(f, ra.A), ra) # !note about NaN values Base_ops = ((:Base, :+), (:Base, :-), (:Base, :*), (:Base, :/), @@ -63,12 +62,12 @@ for (m, f) in Base_ops # _f = Symbol(m, ".:", f) @eval begin $m.$f(a::AbstractSpatRaster, b::AbstractSpatRaster) = begin - size(a) != size(b) || throw(DimensionMismatch("size mismatch")) - SpatRaster(a, $m.$f.(a.A, b.A)) + size(a) != size(b) && throw(DimensionMismatch("size mismatch")) + SpatRaster($m.$f.(a.A, b.A), a) end - $m.$f(a::AbstractSpatRaster, b::Real) = SpatRaster(a, $m.$f.(a.A, b)) - $m.$f(a::Real, b::AbstractSpatRaster) = SpatRaster(a, $m.$f.(a, b.A)) + $m.$f(a::AbstractSpatRaster, b::Real) = SpatRaster($m.$f.(a.A, b), a) + $m.$f(a::Real, b::AbstractSpatRaster) = SpatRaster($m.$f.(a, b.A), b) end end diff --git a/src/sf/st_resample.jl b/src/sf/st_resample.jl index cfd95ae..1b5bf98 100644 --- a/src/sf/st_resample.jl +++ b/src/sf/st_resample.jl @@ -14,12 +14,12 @@ function resample2(r::AbstractArray; fact=10, deepcopy=false) end # for Raster -st_resample(x::AbstractArray; fact=10, kw...) = resample2(x; fact, kw...) +# st_resample(x::AbstractArray; fact=10, kw...) = resample2(x; fact, kw...) -function st_resample(ra::SpatRaster; fact=10, deepcopy=false) - A = resample2(ra.A; fact, deepcopy) - SpatRaster(A, ra.b) -end +# function st_resample(ra::SpatRaster; fact=10, deepcopy=false) +# A = resample2(ra.A; fact, deepcopy) +# SpatRaster(A, ra.b) +# end # function st_resample(z::ZArray; fact=10, missingval=0) # dat = resample2(z; fact) diff --git a/src/tools.jl b/src/tools.jl index 98585cc..1a4f0b9 100644 --- a/src/tools.jl +++ b/src/tools.jl @@ -21,13 +21,12 @@ end which_isnull(x) = findall(x .== nothing) which_notnull(x) = findall(x .!= nothing) -which_isnan(x) = findall(isnan(x)) -which_notnan(x) = findall(.!isnan(x)) +which_isnan(x) = findall(isnan.(x)) +which_notnan(x) = findall(.!isnan.(x)) -Base.isnan(x::AbstractArray) = isnan.(x) - -all_isnan(x::AbstractArray) = all(isnan(x)) -any_isnan(x::AbstractArray) = any(isnan(x)) +# Base.isnan(x::AbstractArray) = isnan.(x) +all_isnan(x::AbstractArray) = all(isnan.(x)) +any_isnan(x::AbstractArray) = any(isnan.(x)) # TODO: need to test @@ -187,6 +186,7 @@ export which_isnull, which_notnull, abind, findnear, set_seed; +export array export isnan, all_isnan, any_isnan; export obj_size, r_summary, r_split export zip_continue diff --git a/test/sf/test-rast.jl b/test/sf/test-rast.jl index be51535..5151e35 100644 --- a/test/sf/test-rast.jl +++ b/test/sf/test-rast.jl @@ -5,6 +5,10 @@ using Test, Ipaper, Ipaper.sf, ArchGDAL A = rand(4, 4) r2 = rast(A, b) + @test ndims(r2) == 2 + @test (r2 + 1).A == (1 + r2).A + @test (r2 + r2).A == 2 * r2.A + f = "test.tif" write_gdal(r2, f) @test read_gdal(f)[:, :, 1] == A @@ -18,7 +22,7 @@ using Test, Ipaper, Ipaper.sf, ArchGDAL @test st_bbox(f) == b isfile(f) && rm(f) - print(r2) + print(r3) @test size(r2) == (4, 4, 1) @test size(r3) == (4, 4, 3) @@ -28,6 +32,8 @@ using Test, Ipaper, Ipaper.sf, ArchGDAL @test (r3 / 2).A == r3.A ./ 2 end + + @testset "gdal_nodata" begin b = bbox(-180.0, -60.0, 180.0, 90.0) A = rand(4, 4) @@ -41,3 +47,13 @@ end @test gdal_nodata("test2.tif")[1] == 2.0 rm.(["test.tif", "test2.tif"]) end + + +@testset "flipud" begin + A = array(1:16; dims=(4, 4)) + A |> flipud |> flipud == A + @test flipud(A)[:, 1] == [4, 3, 2, 1] + + A |> fliplr |> fliplr == A + @test fliplr(A)[1, :] == [13, 9, 5, 1] +end diff --git a/test/sf/test-st_extract.jl b/test/sf/test-st_extract.jl index 17870bc..bf6e585 100644 --- a/test/sf/test-st_extract.jl +++ b/test/sf/test-st_extract.jl @@ -11,6 +11,6 @@ inds, vals = st_extract(ra, points) @test length(vals) == 2 - r2 = st_resample(ra; fact=10) - @test size(r2) == (16, 12, 1) + # r2 = st_resample(ra; fact=10) + # @test size(r2) == (16, 12, 1) end diff --git a/test/test-Ipaper.jl b/test/test-Ipaper.jl index ab741dc..8588a01 100644 --- a/test/test-Ipaper.jl +++ b/test/test-Ipaper.jl @@ -101,4 +101,11 @@ end @test collect(values(tbl)) == [3, 4] end - +@testset "nan" begin + x = [1, 2, NaN, 4] + @test all_isnan(x) == false + @test all_isnan([NaN, NaN]) + @test all_isnan([NaN]) + @test which_isnan(x) == [3] + @test which_notnan(x) == [1, 2, 4] +end