-
Notifications
You must be signed in to change notification settings - Fork 18
/
registration.jl
138 lines (122 loc) · 5.12 KB
/
registration.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
export InDims, OutDims, MovingWindow
#using ..Cubes.Axes: get_descriptor, findAxis, Axes
import ..YAXArrays: get_descriptor, findAxis, AxisDescriptor
using ..YAXArrays: YAXDefaults
using YAXArrayBase: yaxcreate
"""
MovingWindow(desc, pre, after)
Constructs a `MovingWindow` object to be passed to an `InDims` constructor to define that
the axis in `desc` shall participate in the inner function (i.e. shall be looped over), but inside
the inner function `pre` values before and `after` values after the center value
will be passed as well.
For example passing `MovingWindow("Time", 2, 0)` will loop over the time axis and
always pass the current time step plus the 2 previous steps. So in the inner function
the array will have an additional dimension of size 3.
"""
struct MovingWindow
desc::Any
pre::Int
after::Int
end
get_descriptor(m::MovingWindow) = MovingWindow(get_descriptor(m.desc), m.pre, m.after)
findAxis(m::MovingWindow, c) = findAxis(m.desc, c)
wrapWorkArray(::Type{Array}, a, axes) = a
wrapWorkArray(T, a, axes) =
yaxcreate(T, a, map(DD.name, axes), map(i -> i.values, axes), Dict{String, Any}())
abstract type ProcFilter end
struct AllMissing <: ProcFilter end
struct NValid <: ProcFilter
n::Int
end
struct AnyMissing <: ProcFilter end
struct AnyOcean <: ProcFilter end
struct NoFilter <: ProcFilter end
struct StdZero <: ProcFilter end
struct UserFilter{F} <: ProcFilter
f::F
end
checkskip(::NoFilter, x) = false
checkskip(::AllMissing, x) = all(ismissing, x)
checkskip(::AnyMissing, x) = any(ismissing, x)
checkskip(nv::NValid, x) = count(!ismissing, x) <= nv.n
checkskip(uf::UserFilter, x) = uf.f(x)
checkskip(::StdZero, x) = all(i -> i == x[1], x)
docheck(pf::ProcFilter, x)::Bool = checkskip(pf, YAXArrayBase.getdata(x))
docheck(pf::Tuple, x) = reduce(|, map(i -> docheck(i, x), pf))
getprocfilter(f::Function) = (UserFilter(f),)
getprocfilter(pf::ProcFilter) = (pf,)
getprocfilter(pf::NTuple{N,<:ProcFilter}) where {N} = pf
"""
InDims(axisdesc...;...)
Creates a description of an Input Data Cube for cube operations. Takes a single
or multiple axis descriptions as first arguments. Alternatively a MovingWindow(@ref) struct can be passed to include
neighbour slices of one or more axes in the computation.
Axes can be specified by their
name (String), through an Axis type, or by passing a concrete axis.
### Keyword arguments
* `artype` how shall the array be represented in the inner function. Defaults to `Array`, alternatives are `DataFrame` or `AsAxisArray`
* `filter` define some filter to skip the computation, e.g. when all values are missing. Defaults to
`AllMissing()`, possible values are `AnyMissing()`, `AnyOcean()`, `StdZero()`, `NValid(n)`
(for at least n non-missing elements). It is also possible to provide a custom one-argument function
that takes the array and returns `true` if the compuation shall be skipped and `false` otherwise.
* `window_oob_value` if one of the input dimensions is a MowingWindow, this value will be used to fill out-of-bounds areas
"""
mutable struct InDims
axisdesc::Tuple
artype::Any
procfilter::Tuple
window_oob_value::Any
end
function InDims(
axisdesc::Union{String,DD.Dimension,Symbol,MovingWindow,AxisDescriptor}...;
artype = Array,
filter = AllMissing(),
window_oob_value = missing,
)
descs = get_descriptor.(axisdesc)
InDims(descs, artype, getprocfilter(filter), window_oob_value)
end
function InDims(
axisdesc::Tuple{Union{String,DD.Dimension,Symbol,MovingWindow, AxisDescriptor}};
artype = Array,
filter = AllMissing(),
window_oob_value = missing,
)
descs = get_descriptor.(axisdesc)
InDims(descs, artype, getprocfilter(filter), window_oob_value)
end
struct OutDims
axisdesc::Any
backend::Symbol
backendargs::Any
update::Bool
artype::Any
chunksize::Any
outtype::Union{Int,DataType}
end
"""
OutDims(axisdesc;...)
Creates a description of an Output Data Cube for cube operations. Takes a single
or a Vector/Tuple of axes as first argument. Axes can be specified by their
name (String), through an Axis type, or by passing a concrete axis.
- `axisdesc`: List of input axis names
- `backend` : specifies the dataset backend to write data to, must be either :auto or a key in `YAXArrayBase.backendlist`
- `update` : specifies wether the function operates inplace or if an output is returned
- `artype` : specifies the Array type inside the inner function that is mapped over
- `chunksize`: A Dict specifying the chunksizes for the output dimensions of the cube, or `:input` to copy chunksizes from input cube axes or `:max` to not chunk the inner dimensions
- `outtype`: force the output type to a specific type, defaults to `Any` which means that the element type of the first input cube is used
"""
function OutDims(
axisdesc...;
backend = :auto,
update = false,
artype = Array,
chunksize = YAXDefaults.chunksize[],
outtype = 1,
backendargs...,
)
descs = get_descriptor.(axisdesc)
OutDims(descs, backend, backendargs, update, artype, chunksize, outtype)
end
registerDATFunction(a...; kwargs...) =
@warn("Registration does not exist anymore, ignoring....")