@@ -12,7 +12,15 @@ struct GroupMapping{TClass,TUC} <: AbstractGroupMapping where {TClass <: Abstrac
12
12
unique_class:: TUC
13
13
end
14
14
15
- struct GroupedDataset{TDS,TF,TGM,TM,TRF} <: AbstractDataset
15
+ struct GroupedDataset{TDS<: AbstractDataset ,TF,TGM,TM}
16
+ ds:: TDS # dataset
17
+ coordname:: Symbol
18
+ group_fun:: TF # mapping function
19
+ groupmap:: TGM
20
+ map_fun:: TM
21
+ end
22
+
23
+ struct ReducedGroupedDataset{TDS,TF,TGM,TM,TRF} <: AbstractDataset
16
24
ds:: TDS # dataset
17
25
coordname:: Symbol
18
26
group_fun:: TF # mapping function
@@ -110,12 +118,12 @@ _dest_indices(j,ku,indices) = _indices_helper(j,ku,1,:,indices...)
110
118
@inline _size_getindex (array,sh,n) = sh
111
119
112
120
#
113
- # methods with GroupedDataset as main argument
121
+ # methods with ReducedGroupedDataset as main argument
114
122
#
115
123
116
- Base. keys (gds:: GroupedDataset ) = keys (gds. ds)
124
+ Base. keys (gds:: ReducedGroupedDataset ) = keys (gds. ds)
117
125
118
- function variable (gds:: GroupedDataset ,varname:: SymbolOrString )
126
+ function variable (gds:: ReducedGroupedDataset ,varname:: SymbolOrString )
119
127
v = variable (gds. ds,varname)
120
128
121
129
dim = findfirst (== (gds. coordname),Symbol .(dimnames (v)))
@@ -355,7 +363,17 @@ function groupby(v::AbstractVariable,(coordname,group_fun)::Pair{<:SymbolOrStrin
355
363
dim = findfirst (== (Symbol (coordname)),Symbol .(dimnames (v)))
356
364
map_fun = identity
357
365
groupmap = GroupMapping (class,unique_class)
358
- return GroupedVariable (v,coordname,group_fun,groupmap,dim,map_fun)
366
+ return GroupedVariable (v,Symbol (coordname),group_fun,groupmap,dim,map_fun)
367
+ end
368
+
369
+
370
+ function groupby (ds:: AbstractDataset ,(coordname,group_fun):: Pair{<:SymbolOrString,TF} ) where TF
371
+ c = ds[String (coordname)][:]
372
+ class = group_fun .(c)
373
+ unique_class = sort (unique (class))
374
+ map_fun = identity
375
+ groupmap = GroupMapping (class,unique_class)
376
+ return GroupedDataset (ds,Symbol (coordname),group_fun,groupmap,map_fun)
359
377
end
360
378
361
379
"""
@@ -376,13 +394,22 @@ end
376
394
377
395
function ReducedGroupedVariable (gv:: GroupedVariable ,reduce_fun)
378
396
T = eltype (gv. v)
379
- # @show T, reduce_fun
380
- # @show Base.return_types(reduce_fun, (Vector{T},))
381
-
397
+ @debug " inference " T reduce_fun Base. return_types (reduce_fun, (Vector{T},))
382
398
N = ndims (gv. v)
383
399
ReducedGroupedVariable {T,N,typeof(gv),typeof(reduce_fun)} (gv,reduce_fun)
384
400
end
385
401
402
+ function ReducedGroupedDataset (gds:: GroupedDataset ,reduce_fun)
403
+ return ReducedGroupedDataset (
404
+ gds. ds,
405
+ gds. coordname,
406
+ gds. group_fun,
407
+ gds. groupmap,
408
+ gds. map_fun,
409
+ reduce_fun,
410
+ )
411
+ end
412
+
386
413
"""
387
414
gr = reduce(f,gv::GroupedVariable)
388
415
@@ -392,19 +419,15 @@ of `gv`) and `d` is an integer of the dimension overwhich one need to reduce
392
419
`x`.
393
420
"""
394
421
Base. reduce (f,gv:: GroupedVariable ) = ReducedGroupedVariable (gv,f)
422
+ Base. reduce (f,gds:: GroupedDataset ) = ReducedGroupedDataset (gds,f)
395
423
396
424
for fun in (:maximum , :mean , :median , :minimum , :std , :sum , :var )
397
425
@eval $ fun (gv:: GroupedVariable ) = reduce ($ fun,gv)
426
+ @eval $ fun (gds:: GroupedDataset ) = reduce ($ fun,gds)
398
427
end
399
428
400
429
# methods with ReducedGroupedVariable as main argument
401
430
402
- function Base. show (io:: IO ,:: MIME"text/plain" ,gv:: ReducedGroupedVariable )
403
- println (
404
- io,join (string .(size (gv)),' ×' )," array after reducing using " ,
405
- " $(gv. reduce_fun) " )
406
- end
407
-
408
431
Base. ndims (gr:: ReducedGroupedVariable ) = ndims (gr. gv. v)
409
432
Base. size (gr:: ReducedGroupedVariable ) = ntuple (ndims (gr)) do i
410
433
if i == gr. gv. dim
@@ -531,7 +554,7 @@ function dataset(gr::ReducedGroupedVariable)
531
554
gv = gr. gv
532
555
ds = dataset (gv. v)
533
556
534
- return GroupedDataset (
557
+ return ReducedGroupedDataset (
535
558
ds,gv. coordname,gv. group_fun,
536
559
gv. groupmap,
537
560
gv. map_fun,
0 commit comments