-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48f9349
commit 65feb9c
Showing
6 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
struct VarianceWelfordAggegation{Ti,T} | ||
count::Ti | ||
mean::T | ||
M2::T | ||
end | ||
|
||
function VarianceWelfordAggegation(T) | ||
VarianceWelfordAggegation(0,zero(T),zero(T)) | ||
end | ||
|
||
#function VarianceWelfordAggegation(::Type{<:Array{T,N}}) where {T,N} | ||
# VarianceWelfordAggegation(0,zero(T),zero(T)) | ||
#end | ||
|
||
# Welford's online algorithm | ||
@inline function update(ag::VarianceWelfordAggegation, new_value) | ||
count = ag.count | ||
mean = ag.mean | ||
M2 = ag.M2 | ||
|
||
count += 1 | ||
delta = new_value - mean | ||
mean += delta / count | ||
delta2 = new_value - mean | ||
M2 += delta * delta2 | ||
return VarianceWelfordAggegation(count, mean, M2) | ||
end | ||
|
||
function result(ag::VarianceWelfordAggegation) | ||
sample_variance = ag.M2 / (ag.count - 1) | ||
return sample_variance | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using BenchmarkTools | ||
using NCDatasets | ||
using Dates | ||
using CommonDataModel: @groupby | ||
|
||
fname = expanduser("~/sample_perf2.nc") | ||
ds = NCDataset(fname) | ||
|
||
v = ds[:data] | ||
|
||
mean_ref = cat( | ||
[mean(v[:,:,findall(Dates.month.(ds[:time][:]) .== m)],dims=3) | ||
for m in 1:12]...,dims=3); | ||
|
||
std_ref = cat( | ||
[std(v[:,:,findall(Dates.month.(ds[:time][:]) .== m)],dims=3) | ||
for m in 1:12]...,dims=3); | ||
|
||
|
||
gm = @btime mean(@groupby(ds[:data],Dates.Month(time)))[:,:,:]; | ||
# 1.005 s (523137 allocations: 2.67 GiB) | ||
|
||
@show sqrt(mean((gm - mean_ref).^2)) | ||
|
||
# Welford | ||
gs = @btime std(@groupby(ds[:data],Dates.Month(time)))[:,:,:]; | ||
@show sqrt(mean((gs - std_ref).^2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# create the test file | ||
|
||
using NCDatasets | ||
using Dates | ||
|
||
sz = (360,180) | ||
time = DateTime(1980,1,1):Day(1):DateTime(2010,1,1); | ||
varname = "data" | ||
|
||
fname = expanduser("~/sample_perf2.nc") | ||
|
||
isfile(fname) && rm(fname) | ||
|
||
NCDataset(fname,"c") do ds | ||
defVar(ds,"lon",1:sz[1],("lon",)) | ||
defVar(ds,"lat",1:sz[2],("lat",)) | ||
defVar(ds,"time",time,("time",)) | ||
ncv = defVar(ds,"data",Float32,("lon","lat","time"),attrib=Dict("foo" => "bar")) | ||
|
||
for n = 1:length(time) | ||
ncv[:,:,n] = randn(Float32,sz...) .+ 100 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import timeit | ||
import xarray as xr | ||
import numpy | ||
|
||
# xarray-2023.12.0 | ||
# Python 3.10.12 | ||
|
||
# mean | ||
# minimum runtime of 30 trials | ||
# 0.7370511470362544 seconds | ||
|
||
# std | ||
# 3.9330708980560303 seconds | ||
tests = [ | ||
"""vm = ds["data"].groupby("time.month").mean().to_numpy();""", | ||
"""vm = ds["data"].groupby("time.month").std().to_numpy();""", | ||
] | ||
|
||
|
||
print("runtime") | ||
|
||
for tt in tests: | ||
t = timeit.repeat(tt, | ||
setup=""" | ||
import xarray as xr | ||
fname = "/home/abarth/sample_perf2.nc" | ||
ds = xr.open_dataset(fname) | ||
""", | ||
number=1, | ||
repeat=30, | ||
) | ||
|
||
print("timeit ",min(t),tt) | ||
|
||
|
||
fname = "/home/abarth/sample_perf2.nc" | ||
ds = xr.open_dataset(fname) | ||
|
||
month = ds["time.month"].to_numpy() | ||
|
||
print("accuracy") | ||
|
||
|
||
mean_ref = numpy.stack( | ||
[ds["data"].data[(month == mm).nonzero()[0],:,:].mean(axis=0) for mm in range(1,13)],axis=0) | ||
|
||
std_ref = numpy.stack( | ||
[ds["data"].data[(month == mm).nonzero()[0],:,:].std(axis=0,ddof=1) for mm in range(1,13)],axis=0) | ||
|
||
|
||
vm = ds["data"].groupby("time.month").mean().to_numpy(); | ||
|
||
print("accuracy of mean", | ||
numpy.sqrt(numpy.mean((mean_ref - vm)**2))) | ||
# output 0 | ||
|
||
vs = ds["data"].groupby("time.month").std() | ||
|
||
|
||
print("accuracy of std", | ||
numpy.sqrt(numpy.mean((std_ref - vs)**2))) | ||
|
||
# 0.00053720415 |