Skip to content
Snippets Groups Projects
Verified Commit f5eda983 authored by Tamas Gal's avatar Tamas Gal :speech_balloon:
Browse files

Add categorize and most_frequent

parent 4f9a4c38
No related branches found
No related tags found
No related merge requests found
......@@ -13,16 +13,13 @@ using UnROOT
export OnlineFile
export Direction, Position, UTMPosition, Location, Quaternion
export Detector, DetectorModule, PMT, Tripod, Hydrophone
export Waveform, AcousticsTriggerParameter, piezoenabled, hydrophoneenabled
export is3dshower, ismxshower, is3dmuon, isnb
export Hit, TriggeredHit
export calibrate, floordist
export is3dshower, ismxshower, is3dmuon, isnb
export most_frequent, categorize
......
......@@ -13,3 +13,113 @@ is3dmuon(x) = nthbitset(TRIGGER.JTRIGGER3DMUON, x)
is3dshower(x) = nthbitset(TRIGGER.JTRIGGER3DSHOWER, x)
ismxshower(x) = nthbitset(TRIGGER.JTRIGGERMXSHOWER, x)
isnb(x) = nthbitset(TRIGGER.JTRIGGERNB, x)
"""
function most_frequent(iterable)
Return the most frequent value of a given iterable.
"""
function most_frequent(iterable)
d = Dict{eltype(iterable), Int}()
for element iterable
if haskey(d, element)
d[element] += 1
else
d[element] = 1
end
end
candidate = 0
key = 0
for (k, v) in d
if v > candidate
key = k
candidate = v
end
end
return key
end
"""
function most_frequent(f::Function, iterable; rettype=Int)
Return the most frequent value of a given iterable based on the return value
of a function `f` which returns (hashable) values of `rettype`.
"""
function most_frequent(f::Function, iterable; rettype=Int)
d = Dict{rettype, Int}()
for element iterable
v = f(element)
if haskey(d, v)
d[v] += 1
else
d[v] = 1
end
end
candidate = 0
key = 0
for (k, v) in d
if v > candidate
key = k
candidate = v
end
end
return key
end
"""
nthbitset(n, a) = !Bool((a >> (n - 1)) & 1)
Return `true` if the n-th bit of `a` is set, `false` otherwise.
"""
nthbitset(n, a) = Bool((a >> n) & 1)
"""
$(METHODLIST)
Categorise the struct elements of a vector by a given field into a dictionary of
`T.field => Vector{T}`.
"""
function categorize end
"""
$(TYPEDSIGNATURES)
Examples
========
```
julia> using NeRCA
julia> struct PMT
dom_id
time
end
julia> pmts = [PMT(2, 10.4), PMT(4, 23.5), PMT(2, 42.0)];
julia> categorize(:dom_id, pmts)
Dict{Any, Vector{PMT}} with 2 entries:
4 => [PMT(4, 23.5)]
2 => [PMT(2, 10.4), PMT(2, 42.0)]
```
"""
@inline function categorize(field::Symbol, elements::Vector)
_categorize(Val{field}(), elements)
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _categorize(field::Val{F}, elements::Vector{T}) where {T,F}
out = Dict{fieldtype(T, F), Vector{T}}()
for el elements
key = getfield(el, F)
if !haskey(out, key)
out[key] = T[]
end
push!(out[key], el)
end
out
end
import KM3io: nthbitset
import KM3io: nthbitset, SnapshotHit
using Test
@testset "tools" begin
......@@ -12,3 +12,29 @@ using Test
end
end
end
@testset "most_frequent()" begin
a = [1, 1, 2, 3, 1, 5]
@test 1 == most_frequent(a)
a = [[1, 2], [1, 2, 3], [1, 2], [1, 2], [1], [1]]
@test 3 == most_frequent(sum, a)
a = ['a', 'b', 'c', 'b', 'b', 'd']
@test 'b' == most_frequent(a)
@test 'B' == most_frequent(c -> uppercase(c), a; rettype=Char)
end
@testset "categorize()" begin
hits = [
SnapshotHit(1, 0, 123, 22),
SnapshotHit(2, 2, 124, 25),
SnapshotHit(1, 1, 125, 24),
SnapshotHit(1, 0, 126, 28),
SnapshotHit(4, 0, 126, 34),
]
c = categorize(:dom_id, hits)
@test 3 == length(c[1])
@test 1 == length(c[2])
@test 1 == length(c[4])
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment