Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycalibration
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor 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
pycalibration
Commits
b984708b
Commit
b984708b
authored
3 years ago
by
Karim Ahmed
Browse files
Options
Downloads
Patches
Plain Diff
variant in a separate dict and move retrieve_constant_and_time in init_constants
parent
0536e7fe
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!619
Feat: Expose the CCV `variant` to cal_tools sending and retrieving and expose it in agipdlib
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/cal_tools/agipdlib.py
+45
-78
45 additions, 78 deletions
src/cal_tools/agipdlib.py
src/cal_tools/tools.py
+2
-2
2 additions, 2 deletions
src/cal_tools/tools.py
tests/test_cal_tools.py
+1
-0
1 addition, 0 deletions
tests/test_cal_tools.py
with
48 additions
and
80 deletions
src/cal_tools/agipdlib.py
+
45
−
78
View file @
b984708b
...
@@ -988,58 +988,7 @@ class AgipdCorrections:
...
@@ -988,58 +988,7 @@ class AgipdCorrections:
data
=
cntsv
,
data
=
cntsv
,
fletcher32
=
True
)
fletcher32
=
True
)
def
retrieve_constant_and_time
(
self
,
karabo_id
:
str
,
karabo_da
:
str
,
def
init_constants
(
self
,
cons_data
,
when
,
module_idx
,
variant
):
const_dict
:
Dict
[
str
,
Any
],
cal_db_interface
:
str
,
creation_time
:
str
)
->
Tuple
[
Dict
[
str
,
Any
],
Dict
[
str
,
Any
]]:
"""
A wrapper around get_constant_from_db_and_time to get
constant image and creation time using a dictionary of
the names of the required constant from the data base
and their parameter operating conditions.
:param karabo_id: karabo identifier
:param karabo_da: karabo data aggregator
:param const_dict: (Dict) A dictionary with constants to retrieve
and their operating conditions.
e.g.{
"
Offset
"
: [
"
zeros
"
, (128, 512, memory_cells, 3),
"
Dark
"
]
"
Dark-cond
"
: {Condition-name}: condition-value }
:param cal_db_interface: (Str) The port interface for
calibrationDBRemote
:param creation_time: (Str) Raw data creation time
:return:
cons_data: (np.array) Constant data
when: (Dict) A dictionary with all retrieved constants
and their creation-time. {
"
Constant-Name
"
:
"
creation-time
"
}
"""
when
=
{}
cons_data
=
{}
for
cname
,
cval
in
const_dict
.
items
():
condition
=
getattr
(
Conditions
,
cval
[
2
][
0
]).
AGIPD
(
**
cval
[
2
][
1
])
cdata
,
md
=
get_from_db
(
karabo_id
=
karabo_id
,
karabo_da
=
karabo_da
,
constant
=
getattr
(
Constants
.
AGIPD
,
cname
)(),
condition
=
condition
,
empty_constant
=
getattr
(
np
,
cval
[
0
])(
cval
[
1
]),
cal_db_interface
=
cal_db_interface
,
creation_time
=
creation_time
,
verbosity
=
0
,
)
cons_data
[
cname
]
=
{
"
data
"
:
cdata
,
"
variant
"
:
md
.
calibration_constant_version
.
variant
}
when
[
cname
]
=
None
# Read the CCV begin at if constant was retrieved successfully.
if
md
and
md
.
comm_db_success
:
when
[
cname
]
=
md
.
calibration_constant_version
.
begin_at
return
cons_data
,
when
def
init_constants
(
self
,
cons_data
,
when
,
module_idx
):
"""
"""
For CI derived gain, a mean multiplication factor of 4.48 compared
For CI derived gain, a mean multiplication factor of 4.48 compared
to medium gain is used, as no reliable CI data for all memory cells
to medium gain is used, as no reliable CI data for all memory cells
...
@@ -1081,6 +1030,7 @@ class AgipdCorrections:
...
@@ -1081,6 +1030,7 @@ class AgipdCorrections:
:param when: A dictionary for the creation time
:param when: A dictionary for the creation time
of each retrieved constant.
of each retrieved constant.
:param module_idx: A module_idx index
:param module_idx: A module_idx index
:param variant: A dictionary for the variant of each retrieved CCV.
:return:
:return:
"""
"""
...
@@ -1088,32 +1038,29 @@ class AgipdCorrections:
...
@@ -1088,32 +1038,29 @@ class AgipdCorrections:
# assuming this method runs in parallel.
# assuming this method runs in parallel.
calgs_opts
=
dict
(
num_threads
=
os
.
cpu_count
()
//
len
(
self
.
offset
))
calgs_opts
=
dict
(
num_threads
=
os
.
cpu_count
()
//
len
(
self
.
offset
))
calgs
.
transpose_constant
(
calgs
.
transpose_constant
(
self
.
offset
[
module_idx
],
cons_data
[
'
Offset
'
],
**
calgs_opts
)
self
.
offset
[
module_idx
],
cons_data
[
"
Offset
"
][
"
data
"
],
**
calgs_opts
)
# noqa
calgs
.
transpose_constant
(
self
.
noise
[
module_idx
],
cons_data
[
'
Noise
'
],
**
calgs_opts
)
calgs
.
transpose_constant
(
self
.
noise
[
module_idx
],
cons_data
[
"
Noise
"
][
"
data
"
],
**
calgs_opts
)
if
self
.
gain_mode
is
AgipdGainMode
.
ADAPTIVE_GAIN
:
if
self
.
gain_mode
is
AgipdGainMode
.
ADAPTIVE_GAIN
:
calgs
.
transpose_constant
(
calgs
.
transpose_constant
(
self
.
thresholds
[
module_idx
],
self
.
thresholds
[
module_idx
],
cons_data
[
'
ThresholdsDark
'
][...,
:
3
],
cons_data
[
"
ThresholdsDark
"
][
"
data
"
][...,
:
3
],
**
calgs_opts
)
**
calgs_opts
)
if
self
.
corr_bools
.
get
(
"
low_medium_gap
"
):
if
self
.
corr_bools
.
get
(
"
low_medium_gap
"
):
t0
=
self
.
thresholds
[
module_idx
][
0
]
t0
=
self
.
thresholds
[
module_idx
][
0
]
t1
=
self
.
thresholds
[
module_idx
][
1
]
t1
=
self
.
thresholds
[
module_idx
][
1
]
t1
[
t1
<=
1.05
*
t0
]
=
np
.
iinfo
(
np
.
uint16
).
max
t1
[
t1
<=
1.05
*
t0
]
=
np
.
iinfo
(
np
.
uint16
).
max
bpixels
=
cons_data
[
"
BadPixelsDark
"
]
[
"
data
"
]
.
astype
(
np
.
uint32
)
bpixels
=
cons_data
[
"
BadPixelsDark
"
].
astype
(
np
.
uint32
)
if
self
.
corr_bools
.
get
(
"
xray_corr
"
):
if
self
.
corr_bools
.
get
(
"
xray_corr
"
):
if
when
[
"
BadPixelsFF
"
]:
if
when
[
"
BadPixelsFF
"
]:
bpixels
|=
cons_data
[
"
BadPixelsFF
"
][
"
data
"
].
astype
(
np
.
uint32
)[
bpixels
|=
cons_data
[
"
BadPixelsFF
"
].
astype
(
np
.
uint32
)[...,
...,
:
bpixels
.
shape
[
2
],
None
]
:
bpixels
.
shape
[
2
],
# noqa
None
]
if
when
[
"
SlopesFF
"
]:
# Checking if constant was retrieved
if
when
[
"
SlopesFF
"
]:
# Checking if constant was retrieved
slopesFF
=
cons_data
[
"
SlopesFF
"
]
[
"
data
"
]
slopesFF
=
cons_data
[
"
SlopesFF
"
]
# This could be used for backward compatibility
# This could be used for backward compatibility
# for very old SlopesFF constants
# for very old SlopesFF constants
if
len
(
slopesFF
.
shape
)
==
4
:
if
len
(
slopesFF
.
shape
)
==
4
:
...
@@ -1150,16 +1097,15 @@ class AgipdCorrections:
...
@@ -1150,16 +1097,15 @@ class AgipdCorrections:
# add additional bad pixel information
# add additional bad pixel information
if
any
(
self
.
pc_bools
):
if
any
(
self
.
pc_bools
):
if
when
[
"
BadPixelsPC
"
]:
if
when
[
"
BadPixelsPC
"
]:
bppc
=
np
.
moveaxis
(
bppc
=
np
.
moveaxis
(
cons_data
[
"
BadPixelsPC
"
].
astype
(
np
.
uint32
),
cons_data
[
"
BadPixelsPC
"
][
"
data
"
].
astype
(
np
.
uint32
),
0
,
2
)
0
,
2
)
bpixels
|=
bppc
[...,
:
bpixels
.
shape
[
2
],
None
]
bpixels
|=
bppc
[...,
:
bpixels
.
shape
[
2
],
None
]
# calculate relative gain from the constants
# calculate relative gain from the constants
rel_gain
=
np
.
ones
((
128
,
512
,
self
.
max_cells
,
3
),
np
.
float32
)
rel_gain
=
np
.
ones
((
128
,
512
,
self
.
max_cells
,
3
),
np
.
float32
)
if
when
[
"
SlopesPC
"
]:
if
when
[
"
SlopesPC
"
]:
slopesPC
=
cons_data
[
"
SlopesPC
"
][
"
data
"
].
astype
(
slopesPC
=
cons_data
[
"
SlopesPC
"
].
astype
(
np
.
float32
,
copy
=
False
)
np
.
float32
,
copy
=
False
)
# This will handle some historical data in a different format
# This will handle some historical data in a different format
# constant dimension injected first
# constant dimension injected first
...
@@ -1258,6 +1204,7 @@ class AgipdCorrections:
...
@@ -1258,6 +1204,7 @@ class AgipdCorrections:
# string of the device name.
# string of the device name.
cons_data
=
dict
()
cons_data
=
dict
()
when
=
dict
()
when
=
dict
()
variant
=
dict
()
db_module
=
const_yaml
[
karabo_da
][
"
physical-detector-unit
"
]
db_module
=
const_yaml
[
karabo_da
][
"
physical-detector-unit
"
]
for
cname
,
mdata
in
const_yaml
[
karabo_da
][
"
constants
"
].
items
():
for
cname
,
mdata
in
const_yaml
[
karabo_da
][
"
constants
"
].
items
():
cons_data
[
cname
]
=
{}
cons_data
[
cname
]
=
{}
...
@@ -1265,16 +1212,15 @@ class AgipdCorrections:
...
@@ -1265,16 +1212,15 @@ class AgipdCorrections:
when
[
cname
]
=
mdata
[
"
creation-time
"
]
when
[
cname
]
=
mdata
[
"
creation-time
"
]
if
when
[
cname
]:
if
when
[
cname
]:
with
h5py
.
File
(
mdata
[
"
file-path
"
],
"
r
"
)
as
cf
:
with
h5py
.
File
(
mdata
[
"
file-path
"
],
"
r
"
)
as
cf
:
cons_data
[
cname
]
[
"
data
"
]
=
np
.
copy
(
cf
[
f
"
{
base_key
}
/data
"
])
cons_data
[
cname
]
=
np
.
copy
(
cf
[
f
"
{
base_key
}
/data
"
])
# The vairant attribute is missing for old constants.
# The vairant attribute is missing for old constants.
if
"
variant
"
in
cf
[
base_key
].
attrs
.
keys
():
if
"
variant
"
in
cf
[
base_key
].
attrs
.
keys
():
cons_data
[
cname
][
"
variant
"
]
=
cf
[
base_key
].
attrs
[
"
variant
"
]
# noqa
variant
[
cname
]
=
cf
[
base_key
].
attrs
[
"
variant
"
]
# noqa
else
:
else
:
# Create empty constant using the list elements
# Create empty constant using the list elements
cons_data
[
cname
][
"
data
"
]
=
getattr
(
cons_data
[
cname
]
=
getattr
(
np
,
mdata
[
"
file-path
"
][
0
])(
mdata
[
"
file-path
"
][
1
])
# noqa
np
,
mdata
[
"
file-path
"
][
0
])(
mdata
[
"
file-path
"
][
1
])
self
.
init_constants
(
cons_data
,
when
,
module_idx
)
self
.
init_constants
(
cons_data
,
when
,
module_idx
,
variant
)
return
when
return
when
...
@@ -1353,11 +1299,32 @@ class AgipdCorrections:
...
@@ -1353,11 +1299,32 @@ class AgipdCorrections:
integration_time
=
integration_time
integration_time
=
integration_time
)
)
cons_data
,
when
=
self
.
retrieve_constant_and_time
(
when
=
{}
karabo_id
,
karabo_da
,
const_dict
,
cal_db_interface
,
creation_time
cons_data
=
{}
)
variant
=
{}
for
cname
,
cval
in
const_dict
.
items
():
condition
=
getattr
(
Conditions
,
cval
[
2
][
0
]).
AGIPD
(
**
cval
[
2
][
1
])
cdata
,
md
=
get_from_db
(
karabo_id
=
karabo_id
,
karabo_da
=
karabo_da
,
constant
=
getattr
(
Constants
.
AGIPD
,
cname
)(),
condition
=
condition
,
empty_constant
=
getattr
(
np
,
cval
[
0
])(
cval
[
1
]),
cal_db_interface
=
cal_db_interface
,
creation_time
=
creation_time
,
verbosity
=
0
,
)
cons_data
[
cname
]
=
cdata
variant
[
cname
]
=
md
.
calibration_constant_version
.
variant
when
[
cname
]
=
None
# Read the CCV begin at if constant was retrieved successfully.
if
md
and
md
.
comm_db_success
:
when
[
cname
]
=
md
.
calibration_constant_version
.
begin_at
self
.
init_constants
(
cons_data
,
when
,
module_idx
)
self
.
init_constants
(
cons_data
,
when
,
module_idx
,
variant
)
return
when
return
when
...
...
This diff is collapsed.
Click to expand it.
src/cal_tools/tools.py
+
2
−
2
View file @
b984708b
...
@@ -642,8 +642,8 @@ def send_to_db(db_module: str, karabo_id: str, constant, condition,
...
@@ -642,8 +642,8 @@ def send_to_db(db_module: str, karabo_id: str, constant, condition,
:param creation_time: Latest time for constant to be created
:param creation_time: Latest time for constant to be created
:param timeout: Timeout for zmq request
:param timeout: Timeout for zmq request
:param ntries: number of tries to contact the database,
:param ntries: number of tries to contact the database,
ntries is set to 7 so that if the timeout started
ntries is set to 7 so that if the timeout started
at 30s last timeout will be ~ 1h.
at 30s last timeout will be ~ 1h.
:param doraise: if True raise errors during communication with DB
:param doraise: if True raise errors during communication with DB
:param variant: A calibration constant version variant attribute
:param variant: A calibration constant version variant attribute
for the constant file.
for the constant file.
...
...
This diff is collapsed.
Click to expand it.
tests/test_cal_tools.py
+
1
−
0
View file @
b984708b
...
@@ -335,6 +335,7 @@ def test_get_pdu_from_db(_agipd_const_cond):
...
@@ -335,6 +335,7 @@ def test_get_pdu_from_db(_agipd_const_cond):
"
CAL_PHYSICAL_DETECTOR_UNIT-2_TEST
"
]
"
CAL_PHYSICAL_DETECTOR_UNIT-2_TEST
"
]
# TODO add a marker for accessing zmq end_point
@pytest.mark.requires_gpfs
@pytest.mark.requires_gpfs
@pytest.mark.requires_caldb
@pytest.mark.requires_caldb
def
test_initialize_from_db
():
def
test_initialize_from_db
():
...
...
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