-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInferenceObjectsNetCDF.jl
More file actions
198 lines (167 loc) · 6.61 KB
/
InferenceObjectsNetCDF.jl
File metadata and controls
198 lines (167 loc) · 6.61 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
module InferenceObjectsNetCDF
using DimensionalData: DimensionalData, Dimensions, LookupArrays
using NCDatasets: NCDatasets
using Reexport: @reexport
@reexport using InferenceObjects
export from_netcdf, to_netcdf
"""
from_netcdf(path::AbstractString; kwargs...) -> InferenceData
Load an [`InferenceData`](@ref) from an unopened NetCDF file.
Remaining `kwargs` are passed to [`NCDatasets.NCDataset`](https://alexander-barth.github.io/NCDatasets.jl/stable/dataset/#NCDatasets.NCDataset).
This method loads data eagerly. To instead load data lazily, pass an opened `NCDataset` to
`from_netcdf`.
# Examples
```julia
julia> idata = from_netcdf("centered_eight.nc")
InferenceData with groups:
> posterior
> posterior_predictive
> sample_stats
> prior
> observed_data
```
from_netcdf(ds::NCDatasets.NCDataset; load_mode) -> InferenceData
Load an [`InferenceData`](@ref) from an opened NetCDF file.
`load_mode` defaults to `:lazy`, which avoids reading variables into memory. Operations on
these arrays will be slow. `load_mode` can also be `:eager`, which copies all variables into
memory. It is then safe to close `ds`. If `load_mode` is `:lazy` and `ds` is closed after
constructing `InferenceData`, using the variable arrays will have undefined behavior.
# Examples
Here is how we might load an `InferenceData` from an `InferenceData` lazily from a
web-hosted NetCDF file.
```julia
julia> using HTTP, NCDatasets
julia> resp = HTTP.get("https://github.com/arviz-devs/arviz_example_data/blob/main/data/centered_eight.nc?raw=true");
julia> ds = NCDataset("centered_eight", "r"; memory = resp.body);
julia> idata = from_netcdf(ds)
InferenceData with groups:
> posterior
> posterior_predictive
> sample_stats
> prior
> observed_data
julia> idata_copy = copy(idata); # disconnect from the loaded dataset
julia> close(ds);
```
"""
function from_netcdf end
function from_netcdf(path::AbstractString; kwargs...)
return NCDatasets.NCDataset(path, "r"; kwargs...) do ds
return from_netcdf(ds; load_mode=:eager)
end
end
function from_netcdf(ds::NCDatasets.NCDataset; load_mode::Symbol=:lazy)
return _from_netcdf(ds, Val(load_mode))
end
function _from_netcdf(ds, load_mode)
idata = InferenceData()
for (group_name, group) in ds.group
layerdims = (;
map(NCDatasets.dimnames(group)) do dim_name
index = collect(group[dim_name])
if index == eachindex(index)
# discard the index if it is just the default
index = LookupArrays.NoLookup()
end
return Symbol(dim_name) => Dimensions.Dim{Symbol(dim_name)}(index)
end...
)
var_iter = Iterators.filter(∉(keys(layerdims)) ∘ Symbol ∘ first, group)
data = (;
map(var_iter) do (var_name, var)
vals = _var_to_array(var, load_mode)
dims = Tuple(NamedTuple{map(Symbol, NCDatasets.dimnames(var))}(layerdims))
name = Symbol(var_name)
attrib = if load_mode isa Val{:eager}
filter(!=("_FillValue") ∘ first, Dict{String,Any}(var.attrib))
else
var.attrib
end
metadata = isempty(attrib) ? LookupArrays.NoMetadata() : attrib
da = DimensionalData.DimArray(vals, dims; name, metadata)
return name => da
end...
)
group_metadata = if load_mode isa Val{:eager}
Dict{String,Any}(group.attrib)
else
group.attrib
end
idata[Symbol(group_name)] = Dataset(data; metadata=group_metadata)
end
return idata
end
_var_to_array(var, load_mode) = var
function _var_to_array(var, load_mode::Val{:eager})
arr = as_array(Array(var))
attr = var.attrib
try
arr_nomissing = NCDatasets.nomissing(arr)
if eltype(arr_nomissing) <: Integer && (get(attr, "dtype", nothing) == "bool")
return convert(Array{Bool}, arr_nomissing)
end
return arr_nomissing
catch e
if eltype(arr) <: Union{Integer,Missing} && (get(attr, "dtype", nothing) == "bool")
return convert(Array{Union{Missing,Bool}}, arr)
end
return arr
end
end
function InferenceObjects.convert_to_inference_data(ds::NCDatasets.NCDataset; kwargs...)
return from_netcdf(ds)
end
"""
to_netcdf(data, dest::AbstractString; group::Symbol=:posterior, kwargs...)
to_netcdf(data, dest::NCDatasets.NCDataset; group::Symbol=:posterior)
Write `data` to a NetCDF file.
`data` is any type that can be converted to an [`InferenceData`](@ref) using
[`convert_to_inference_data`](@ref). If not an `InferenceData`, then `group` specifies which
group the data represents.
`dest` specifies either the path to the NetCDF file or an opened NetCDF file.
If `dest` is a path, remaining `kwargs` are passed to
[`NCDatasets.NCDataset`](https://alexander-barth.github.io/NCDatasets.jl/stable/dataset/#NCDatasets.NCDataset).
# Examples
```julia
julia> using NCDatasets
julia> idata = from_namedtuple((; x = randn(4, 100, 3), z = randn(4, 100)))
InferenceData with groups:
> posterior
julia> to_netcdf(idata, "data.nc")
"data.nc"
```
"""
function to_netcdf end
function to_netcdf(data, path::AbstractString; group::Symbol=:posterior, kwargs...)
NCDatasets.NCDataset(ds -> to_netcdf(data, ds; group), path, "c"; kwargs...)
return path
end
function to_netcdf(data, ds::NCDatasets.NCDataset; group::Symbol=:posterior)
idata = convert_to_inference_data(data; group)
for (group_name, group_data) in pairs(idata)
group_attrib = collect(InferenceObjects.attributes(group_data))
group_ds = NCDatasets.defGroup(ds, String(group_name); attrib=group_attrib)
for dim in Dimensions.dims(group_data)
dim_name = String(Dimensions.name(dim))
NCDatasets.defDim(group_ds, dim_name, length(dim))
index = LookupArrays.index(group_data, dim)
var = NCDatasets.defVar(group_ds, dim_name, eltype(index), (dim_name,))
copyto!(var, index)
end
for (var_name, da) in pairs(group_data)
dimnames = map(String, Dimensions.name(Dimensions.dims(da)))
attrib = Dict(DimensionalData.metadata(da))
if eltype(da) <: Bool && (get(attrib, "dtype", "bool") == "bool")
da = convert(AbstractArray{Int8}, da)
attrib["dtype"] = "bool"
end
NCDatasets.defVar(
group_ds, String(var_name), parent(da), dimnames; attrib=collect(attrib)
)
end
end
return ds
end
as_array(x) = fill(x)
as_array(x::AbstractArray) = x
end