Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
calng
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
calibration
calng
Commits
193264ed
Commit
193264ed
authored
2 years ago
by
David Hammer
Browse files
Options
Downloads
Patches
Plain Diff
Adding Philipp's proposed changes to TrainMatcher for merging
parent
f07657ce
No related branches found
Branches containing commit
Tags
0.2.3-2.14.1
2 merge requests
!10
DetectorAssembler: assemble with extra shape (multiple frames)
,
!9
Stacking shmem matcher
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/calng/ShmemTrainMatcher.py
+156
-1
156 additions, 1 deletion
src/calng/ShmemTrainMatcher.py
with
156 additions
and
1 deletion
src/calng/ShmemTrainMatcher.py
+
156
−
1
View file @
193264ed
from
karabo.bound
import
KARABO_CLASSINFO
import
re
import
numpy
as
np
from
karabo.bound
import
(
BOOL_ELEMENT
,
KARABO_CLASSINFO
,
STRING_ELEMENT
,
TABLE_ELEMENT
,
Hash
,
Schema
,
State
,
)
from
TrainMatcher
import
TrainMatcher
from
.
import
shmem_utils
from
._version
import
version
as
deviceVersion
def
merge_schema
():
schema
=
Schema
()
(
BOOL_ELEMENT
(
schema
)
.
key
(
"
select
"
)
.
displayedName
(
"
Select
"
)
.
assignmentOptional
()
.
defaultValue
(
False
)
.
reconfigurable
()
.
commit
(),
STRING_ELEMENT
(
schema
)
.
key
(
"
source_pattern
"
)
.
displayedName
(
"
Source pattern
"
)
.
assignmentOptional
()
.
defaultValue
(
""
)
.
reconfigurable
()
.
commit
(),
STRING_ELEMENT
(
schema
)
.
key
(
"
key_pattern
"
)
.
displayedName
(
"
Key pattern
"
)
.
assignmentOptional
()
.
defaultValue
(
""
)
.
reconfigurable
()
.
commit
(),
STRING_ELEMENT
(
schema
)
.
key
(
"
replacement
"
)
.
displayedName
(
"
Replacement
"
)
.
assignmentOptional
()
.
defaultValue
(
""
)
.
reconfigurable
()
.
commit
(),
)
return
schema
@KARABO_CLASSINFO
(
"
ShmemTrainMatcher
"
,
deviceVersion
)
class
ShmemTrainMatcher
(
TrainMatcher
.
TrainMatcher
):
@staticmethod
def
expectedParameters
(
expected
):
(
TABLE_ELEMENT
(
expected
)
.
key
(
"
merge
"
)
.
displayedName
(
"
Array merging
"
)
.
allowedStates
(
State
.
PASSIVE
)
.
description
(
"
List source or key patterns to merge their data arrays, e.g. to
"
"
combine multiple detector sources or digitizer channels into a single
"
"
source or key. Both source or key patterns may be regular expressions,
"
"
but only one may have multiple matches at the same time. The merged
"
"
source or key is substituted by the replacement value.
"
)
.
setColumns
(
merge_schema
())
.
assignmentOptional
()
.
defaultValue
([])
.
reconfigurable
()
.
commit
(),
)
def
initialization
(
self
):
self
.
_compile_merge_patterns
(
self
.
get
(
"
merge
"
))
super
().
initialization
()
self
.
_shmem_handler
=
shmem_utils
.
ShmemCircularBufferReceiver
()
def
preReconfigure
(
self
,
conf
):
super
().
preReconfigure
(
conf
)
if
conf
.
has
(
"
merge
"
):
self
.
_compile_merge_patterns
(
conf
[
"
merge
"
])
def
_compile_merge_patterns
(
self
,
merge
):
self
.
_merge_patterns
=
[
(
re
.
compile
(
row
[
"
source_pattern
"
]),
re
.
compile
(
row
[
"
key_pattern
"
]),
row
[
"
replacement
"
],
)
for
row
in
merge
if
row
[
"
select
"
]
]
def
on_matched_data
(
self
,
train_id
,
sources
):
# dereference calng shmem handles
for
source
,
(
data
,
timestamp
)
in
sources
.
items
():
if
data
.
has
(
"
calngShmemPaths
"
):
shmem_paths
=
list
(
data
[
"
calngShmemPaths
"
])
...
...
@@ -23,4 +112,70 @@ class ShmemTrainMatcher(TrainMatcher.TrainMatcher):
dereferenced
=
self
.
_shmem_handler
.
get
(
data
[
shmem_path
])
data
[
shmem_path
]
=
dereferenced
# merge arrays
for
source_re
,
key_re
,
replacement
in
self
.
_merge_patterns
:
# Find all sources matching the source pattern.
merge_sources
=
[
source
for
source
in
sources
.
keys
()
if
source_re
.
match
(
source
)
]
if
len
(
merge_sources
)
>
1
:
# More than one source match, merge by source.
if
key_re
.
pattern
in
sources
[
merge_sources
[
0
]][
0
]:
# Short-circuit the pattern for performance if itself is a key.
new_key
=
key_re
.
pattern
else
:
# Find the first key matching the pattern.
for
new_key
in
sources
[
merge_sources
[
0
]][
0
].
paths
():
if
key_re
.
match
(
new_key
):
break
merge_keys
=
[
new_key
]
new_source
=
replacement
to_merge
=
[
sources
[
source
][
0
][
new_key
]
for
source
in
sorted
(
merge_sources
)
]
if
len
(
to_merge
)
!=
len
(
merge_sources
):
# Make sure all matched sources contain the key.
break
elif
len
(
merge_sources
)
==
1
:
# Exactly one source match, merge by key.
new_source
=
merge_sources
[
0
]
new_key
=
replacement
merge_keys
=
[
key
for
key
in
sources
[
new_source
][
0
].
paths
()
if
key_re
.
match
(
key
)
]
if
not
merge_keys
:
# No key match, ignore.
continue
to_merge
=
[
sources
[
new_source
][
0
][
key
]
for
key
in
sorted
(
merge_keys
)]
else
:
# No source match, ignore.
continue
# Stack data and insert into source data.
try
:
new_data
=
np
.
stack
(
to_merge
,
axis
=
0
)
except
ValueError
as
e
:
self
.
log
.
ERROR
(
f
"
Failed to merge data for
"
f
"
{
new_source
}
.
{
new_key
}
:
{
e
}
"
)
continue
sources
.
setdefault
(
new_source
,
(
Hash
(),
sources
[
merge_sources
[
0
]][
1
]))[
0
][
new_key
]
=
new_data
# Unset keys merged together across all source matches.
for
source
in
merge_sources
:
for
key
in
merge_keys
:
sources
[
source
][
0
].
erase
(
key
)
super
().
on_matched_data
(
train_id
,
sources
)
This diff is collapsed.
Click to expand it.
David Hammer
@hammerd
mentioned in commit
9495dda8
·
2 years ago
mentioned in commit
9495dda8
mentioned in commit 9495dda895756fcd9a2ebb5f8adbaa050e067f33
Toggle commit list
David Hammer
@hammerd
mentioned in commit
99de26dd
·
2 years ago
mentioned in commit
99de26dd
mentioned in commit 99de26dda08d02c1c2b5efc1f1f462cfc4f0ce63
Toggle commit list
David Hammer
@hammerd
mentioned in commit
3566342d
·
2 years ago
mentioned in commit
3566342d
mentioned in commit 3566342d031db1bb435b6430f100afdbaaa6c4bf
Toggle commit list
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