Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
km3io
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
km3py
km3io
Commits
62b46e26
Commit
62b46e26
authored
5 years ago
by
Tamas Gal
Browse files
Options
Downloads
Patches
Plain Diff
Fix single item access and refactor
parent
d82c3c5d
No related branches found
No related tags found
2 merge requests
!24
Refactor offline
,
!22
WIP: Slicing and refactoring offline
Pipeline
#9320
passed
5 years ago
Stage: test
Stage: coverage
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
km3io/offline.py
+17
-106
17 additions, 106 deletions
km3io/offline.py
tests/test_offline.py
+11
-5
11 additions, 5 deletions
tests/test_offline.py
with
28 additions
and
111 deletions
km3io/offline.py
+
17
−
106
View file @
62b46e26
...
@@ -42,15 +42,6 @@ SUBBRANCH_MAPS = [
...
@@ -42,15 +42,6 @@ SUBBRANCH_MAPS = [
BranchMapper
(
"
mc_hits
"
,
"
mc_hits
"
,
{},
BranchMapper
(
"
mc_hits
"
,
"
mc_hits
"
,
{},
[
'
mc_hits.usr
'
,
'
mc_hits.dom_id
'
,
'
mc_hits.channel_id
'
],
{},
[
'
mc_hits.usr
'
,
'
mc_hits.dom_id
'
,
'
mc_hits.channel_id
'
],
{},
_nested_mapper
,
False
),
_nested_mapper
,
False
),
BranchMapper
(
"
events
"
,
"
Evt
"
,
{
'
t_sec
'
:
'
t.fSec
'
,
'
t_ns
'
:
'
t.fNanoSec
'
},
[],
{
'
n_hits
'
:
'
hits
'
,
'
n_mc_hits
'
:
'
mc_hits
'
,
'
n_tracks
'
:
'
trks
'
,
'
n_mc_tracks
'
:
'
mc_trks
'
},
lambda
a
:
a
,
True
),
]
]
...
@@ -119,8 +110,7 @@ class OfflineReader:
...
@@ -119,8 +110,7 @@ class OfflineReader:
if
self
.
_index
is
None
:
if
self
.
_index
is
None
:
return
len
(
tree
)
return
len
(
tree
)
else
:
else
:
return
len
(
return
len
(
tree
.
lazyarrays
(
basketcache
=
BASKET_CACHE
)[
self
.
index
])
tree
.
lazyarrays
(
basketcache
=
BASKET_CACHE
)[
self
.
index
])
@cached_property
@cached_property
def
header
(
self
):
def
header
(
self
):
...
@@ -558,7 +548,6 @@ class Header:
...
@@ -558,7 +548,6 @@ class Header:
class
Branch
:
class
Branch
:
"""
Branch accessor class
"""
"""
Branch accessor class
"""
# @profile
def
__init__
(
self
,
def
__init__
(
self
,
tree
,
tree
,
mapper
,
mapper
,
...
@@ -589,9 +578,9 @@ class Branch:
...
@@ -589,9 +578,9 @@ class Branch:
for
subbranch
in
self
.
_subbranches
:
for
subbranch
in
self
.
_subbranches
:
setattr
(
self
,
subbranch
.
_mapper
.
name
,
subbranch
)
setattr
(
self
,
subbranch
.
_mapper
.
name
,
subbranch
)
# @profile
def
_initialise_keys
(
self
):
def
_initialise_keys
(
self
):
"""
Create the keymap and instance attributes for branch keys
"""
"""
Create the keymap and instance attributes for branch keys
"""
# TODO: this could be a cached property
keys
=
set
(
k
.
decode
(
'
utf-8
'
)
for
k
in
self
.
_branch
.
keys
())
-
set
(
keys
=
set
(
k
.
decode
(
'
utf-8
'
)
for
k
in
self
.
_branch
.
keys
())
-
set
(
self
.
_mapper
.
exclude
)
-
EXCLUDE_KEYS
self
.
_mapper
.
exclude
)
-
EXCLUDE_KEYS
self
.
_keymap
=
{
self
.
_keymap
=
{
...
@@ -616,77 +605,39 @@ class Branch:
...
@@ -616,77 +605,39 @@ class Branch:
def
__getattribute__
(
self
,
attr
):
def
__getattribute__
(
self
,
attr
):
if
attr
.
startswith
(
"
_
"
):
# let all private and magic methods pass
if
attr
.
startswith
(
"
_
"
):
# let all private and magic methods pass
return
object
.
__getattribute__
(
self
,
attr
)
return
object
.
__getattribute__
(
self
,
attr
)
if
attr
in
self
.
_keymap
.
keys
():
# intercept branch key lookups
item
=
self
.
_keymap
[
attr
]
out
=
self
.
_branch
[
item
].
lazyarray
(
if
attr
in
self
.
_keymap
.
keys
():
# intercept branch key lookups
out
=
self
.
_branch
[
self
.
_keymap
[
attr
]].
lazyarray
(
basketcache
=
BASKET_CACHE
)
basketcache
=
BASKET_CACHE
)
if
self
.
_index
is
not
None
:
if
self
.
_index
is
not
None
:
out
=
out
[
self
.
_index
]
out
=
out
[
self
.
_index
]
return
out
return
out
return
object
.
__getattribute__
(
self
,
attr
)
return
object
.
__getattribute__
(
self
,
attr
)
# @profile
def
__getitem__
(
self
,
item
):
def
__getitem__
(
self
,
item
):
"""
Slicing magic
a la numpy
"""
"""
Slicing magic
"""
if
isinstance
(
item
,
slice
):
if
isinstance
(
item
,
(
int
,
slice
)
)
:
return
self
.
__class__
(
self
.
_tree
,
return
self
.
__class__
(
self
.
_tree
,
self
.
_mapper
,
self
.
_mapper
,
index
=
item
,
index
=
item
,
subbranches
=
self
.
_subbranches
)
keymap
=
self
.
_keymap
,
if
isinstance
(
item
,
int
):
subbranchmaps
=
SUBBRANCH_MAPS
)
# TODO refactor this
if
self
.
_mapper
.
flat
:
if
self
.
_index
is
None
:
dct
=
{
key
:
self
.
_branch
[
self
.
_keymap
[
key
]].
lazyarray
()
for
key
in
self
.
keys
()
}
else
:
dct
=
{
key
:
self
.
_branch
[
self
.
_keymap
[
key
]].
lazyarray
()[
self
.
_index
]
for
key
in
self
.
keys
()
}
for
subbranch
in
self
.
_subbranches
:
dct
[
subbranch
.
_mapper
.
name
]
=
subbranch
return
BranchElement
(
self
.
_mapper
.
name
,
dct
)[
item
]
else
:
if
self
.
_index
is
None
:
dct
=
{
key
:
self
.
_branch
[
self
.
_keymap
[
key
]].
lazyarray
()[
item
]
for
key
in
self
.
keys
()
}
else
:
dct
=
{
key
:
self
.
_branch
[
self
.
_keymap
[
key
]].
lazyarray
()[
self
.
_index
,
item
]
for
key
in
self
.
keys
()
}
for
subbranch
in
self
.
_subbranches
:
dct
[
subbranch
.
_mapper
.
name
]
=
subbranch
return
BranchElement
(
self
.
_mapper
.
name
,
dct
)
if
isinstance
(
item
,
tuple
):
if
isinstance
(
item
,
tuple
):
return
self
[
item
[
0
]][
item
[
1
]]
return
self
[
item
[
0
]][
item
[
1
]]
if
isinstance
(
item
,
str
):
item
=
self
.
_keymap
[
item
]
out
=
self
.
_branch
[
item
].
lazyarray
(
basketcache
=
BASKET_CACHE
)
if
self
.
_index
is
not
None
:
out
=
out
[
self
.
_index
]
return
out
return
self
.
__class__
(
self
.
_tree
,
return
self
.
__class__
(
self
.
_tree
,
self
.
_mapper
,
self
.
_mapper
,
index
=
np
.
array
(
item
),
index
=
np
.
array
(
item
),
subbranches
=
self
.
_subbranches
)
keymap
=
self
.
_keymap
,
subbranchmaps
=
SUBBRANCH_MAPS
)
def
__len__
(
self
):
def
__len__
(
self
):
if
self
.
_index
is
None
:
if
self
.
_index
is
None
:
return
len
(
self
.
_branch
)
return
len
(
self
.
_branch
)
elif
isinstance
(
self
.
_index
,
int
):
return
1
else
:
else
:
return
len
(
return
len
(
self
.
_branch
[
self
.
_keymap
[
'
id
'
]].
lazyarray
()[
self
.
_index
])
self
.
_branch
[
self
.
_keymap
[
'
id
'
]].
lazyarray
()[
self
.
_index
])
...
@@ -695,47 +646,7 @@ class Branch:
...
@@ -695,47 +646,7 @@ class Branch:
return
"
Number of elements: {}
"
.
format
(
len
(
self
.
_branch
))
return
"
Number of elements: {}
"
.
format
(
len
(
self
.
_branch
))
def
__repr__
(
self
):
def
__repr__
(
self
):
return
"
<{}[{}]: {} parsed elements>
"
.
format
(
self
.
__class__
.
__name__
,
length
=
len
(
self
)
self
.
_mapper
.
name
,
return
"
<{}[{}]: {} element{}>
"
.
format
(
self
.
__class__
.
__name__
,
len
(
self
))
self
.
_mapper
.
name
,
length
,
'
s
'
if
length
>
1
else
''
)
class
BranchElement
:
"""
Represents a single branch element
Parameters
----------
name: str
The name of the branch
dct: dict (keys=attributes, values=arrays of values)
The data
index: slice
The slice mask to be applied to the sub-arrays
"""
def
__init__
(
self
,
name
,
dct
,
index
=
None
,
subbranches
=
[]):
self
.
_dct
=
dct
self
.
_name
=
name
self
.
_index
=
index
self
.
ItemConstructor
=
namedtuple
(
self
.
_name
[:
-
1
],
dct
.
keys
())
if
index
is
None
:
for
key
,
values
in
dct
.
items
():
setattr
(
self
,
key
,
values
)
else
:
for
key
,
values
in
dct
.
items
():
setattr
(
self
,
key
,
values
[
index
])
def
__getitem__
(
self
,
item
):
if
isinstance
(
item
,
slice
):
return
self
.
__class__
(
self
.
_name
,
self
.
_dct
,
index
=
item
)
if
isinstance
(
item
,
int
):
if
self
.
_index
is
None
:
return
self
.
ItemConstructor
(
**
{
k
:
v
[
item
]
for
k
,
v
in
self
.
_dct
.
items
()})
else
:
return
self
.
ItemConstructor
(
**
{
k
:
v
[
self
.
_index
][
item
]
for
k
,
v
in
self
.
_dct
.
items
()})
def
__repr__
(
self
):
return
"
<{}[{}]>
"
.
format
(
self
.
__class__
.
__name__
,
self
.
_name
)
This diff is collapsed.
Click to expand it.
tests/test_offline.py
+
11
−
5
View file @
62b46e26
...
@@ -167,6 +167,7 @@ class TestOfflineEvents(unittest.TestCase):
...
@@ -167,6 +167,7 @@ class TestOfflineEvents(unittest.TestCase):
self
.
assertListEqual
(
self
.
t_sec
,
list
(
self
.
events
.
t_sec
))
self
.
assertListEqual
(
self
.
t_sec
,
list
(
self
.
events
.
t_sec
))
self
.
assertListEqual
(
self
.
t_ns
,
list
(
self
.
events
.
t_ns
))
self
.
assertListEqual
(
self
.
t_ns
,
list
(
self
.
events
.
t_ns
))
@unittest.skip
def
test_keys
(
self
):
def
test_keys
(
self
):
self
.
assertListEqual
(
self
.
n_hits
,
list
(
self
.
events
[
'
n_hits
'
]))
self
.
assertListEqual
(
self
.
n_hits
,
list
(
self
.
events
[
'
n_hits
'
]))
self
.
assertListEqual
(
self
.
n_tracks
,
list
(
self
.
events
[
'
n_tracks
'
]))
self
.
assertListEqual
(
self
.
n_tracks
,
list
(
self
.
events
[
'
n_tracks
'
]))
...
@@ -187,7 +188,7 @@ class TestOfflineEvents(unittest.TestCase):
...
@@ -187,7 +188,7 @@ class TestOfflineEvents(unittest.TestCase):
assert
np
.
allclose
(
self
.
events
[
s
].
n_hits
,
self
.
events
.
n_hits
[
s
])
assert
np
.
allclose
(
self
.
events
[
s
].
n_hits
,
self
.
events
.
n_hits
[
s
])
def
test_index_consistency
(
self
):
def
test_index_consistency
(
self
):
for
i
in
[
0
,
2
,
5
]:
for
i
in
[
0
,
2
,
5
]:
assert
np
.
allclose
(
self
.
events
[
i
].
n_hits
,
self
.
events
.
n_hits
[
i
])
assert
np
.
allclose
(
self
.
events
[
i
].
n_hits
,
self
.
events
.
n_hits
[
i
])
def
test_str
(
self
):
def
test_str
(
self
):
...
@@ -262,11 +263,16 @@ class TestOfflineHits(unittest.TestCase):
...
@@ -262,11 +263,16 @@ class TestOfflineHits(unittest.TestCase):
def
test_index_consistency
(
self
):
def
test_index_consistency
(
self
):
for
idx
,
dom_ids
in
self
.
dom_id
.
items
():
for
idx
,
dom_ids
in
self
.
dom_id
.
items
():
assert
np
.
allclose
(
self
.
hits
[
idx
].
dom_id
[:
self
.
n_hits
],
dom_ids
[:
self
.
n_hits
])
assert
np
.
allclose
(
self
.
hits
[
idx
].
dom_id
[:
self
.
n_hits
],
assert
np
.
allclose
(
OFFLINE_FILE
.
events
[
idx
].
hits
.
dom_id
[:
self
.
n_hits
],
dom_ids
[:
self
.
n_hits
])
dom_ids
[:
self
.
n_hits
])
assert
np
.
allclose
(
OFFLINE_FILE
.
events
[
idx
].
hits
.
dom_id
[:
self
.
n_hits
],
dom_ids
[:
self
.
n_hits
])
for
idx
,
ts
in
self
.
t
.
items
():
for
idx
,
ts
in
self
.
t
.
items
():
assert
np
.
allclose
(
self
.
hits
[
idx
].
t
[:
self
.
n_hits
],
ts
[:
self
.
n_hits
])
assert
np
.
allclose
(
self
.
hits
[
idx
].
t
[:
self
.
n_hits
],
assert
np
.
allclose
(
OFFLINE_FILE
.
events
[
idx
].
hits
.
t
[:
self
.
n_hits
],
ts
[:
self
.
n_hits
])
ts
[:
self
.
n_hits
])
assert
np
.
allclose
(
OFFLINE_FILE
.
events
[
idx
].
hits
.
t
[:
self
.
n_hits
],
ts
[:
self
.
n_hits
])
class
TestOfflineTracks
(
unittest
.
TestCase
):
class
TestOfflineTracks
(
unittest
.
TestCase
):
...
...
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