Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
KM3io.jl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
common
KM3io.jl
Commits
f5eda983
Verified
Commit
f5eda983
authored
2 years ago
by
Tamas Gal
Browse files
Options
Downloads
Patches
Plain Diff
Add categorize and most_frequent
parent
4f9a4c38
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/KM3io.jl
+2
-5
2 additions, 5 deletions
src/KM3io.jl
src/tools.jl
+110
-0
110 additions, 0 deletions
src/tools.jl
test/tools.jl
+27
-1
27 additions, 1 deletion
test/tools.jl
with
139 additions
and
6 deletions
src/KM3io.jl
+
2
−
5
View file @
f5eda983
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
src/tools.jl
+
110
−
0
View file @
f5eda983
...
...
@@ -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
This diff is collapsed.
Click to expand it.
test/tools.jl
+
27
−
1
View file @
f5eda983
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment