Skip to content
Snippets Groups Projects
Commit b3066fb1 authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Create MultiModuleConstant on demand

parent 68016f5c
No related branches found
No related tags found
1 merge request!885Revised CalCat API
...@@ -253,6 +253,7 @@ class MultiModuleConstant: ...@@ -253,6 +253,7 @@ class MultiModuleConstant:
constants: Dict[str, SingleConstant] # Keys e.g. 'LPD00' constants: Dict[str, SingleConstant] # Keys e.g. 'LPD00'
module_details: List[Dict] module_details: List[Dict]
detector_name: str # e.g. 'HED_DET_AGIPD500K2G'
def select_modules( def select_modules(
self, module_nums=None, *, aggregator_names=None, qm_names=None self, module_nums=None, *, aggregator_names=None, qm_names=None
...@@ -262,7 +263,7 @@ class MultiModuleConstant: ...@@ -262,7 +263,7 @@ class MultiModuleConstant:
) )
d = {aggr: scv for (aggr, scv) in self.constants.items() if aggr in aggs} d = {aggr: scv for (aggr, scv) in self.constants.items() if aggr in aggs}
mods = [m for m in self.module_details if m["karabo_da"] in d] mods = [m for m in self.module_details if m["karabo_da"] in d]
return MultiModuleConstant(d, mods) return MultiModuleConstant(d, mods, self.detector_name)
# These properties label only the modules we have constants for, which may # These properties label only the modules we have constants for, which may
# be a subset of what's in module_details # be a subset of what's in module_details
...@@ -371,6 +372,7 @@ class CalibrationData(Mapping): ...@@ -371,6 +372,7 @@ class CalibrationData(Mapping):
"""Collected constants for a given detector""" """Collected constants for a given detector"""
def __init__(self, constant_groups, module_details, detector_name): def __init__(self, constant_groups, module_details, detector_name):
# {calibration: {karabo_da: SingleConstant}}
self.constant_groups = constant_groups self.constant_groups = constant_groups
self.module_details = module_details self.module_details = module_details
self.detector_name = detector_name self.detector_name = detector_name
...@@ -456,11 +458,7 @@ class CalibrationData(Mapping): ...@@ -456,11 +458,7 @@ class CalibrationData(Mapping):
const_group = constant_groups.setdefault(cal_type, {}) const_group = constant_groups.setdefault(cal_type, {})
const_group[aggr] = SingleConstant.from_response(ccv) const_group[aggr] = SingleConstant.from_response(ccv)
mmcs = { return cls(constant_groups, module_details, detector_name)
const_type: MultiModuleConstant(d, module_details)
for const_type, d in constant_groups.items()
}
return cls(mmcs, module_details, detector_name)
@classmethod @classmethod
def from_report( def from_report(
...@@ -509,14 +507,12 @@ class CalibrationData(Mapping): ...@@ -509,14 +507,12 @@ class CalibrationData(Mapping):
det_name = detector_id_to_name(det_ids.pop(), client) det_name = detector_id_to_name(det_ids.pop(), client)
module_details = sorted(pdus.values(), key=lambda d: d["karabo_da"]) module_details = sorted(pdus.values(), key=lambda d: d["karabo_da"])
mmcs = { return cls(constant_groups, module_details, det_name)
const_type: MultiModuleConstant(d, module_details)
for const_type, d in constant_groups.items()
}
return cls(mmcs, module_details, det_name)
def __getitem__(self, key) -> MultiModuleConstant: def __getitem__(self, key) -> MultiModuleConstant:
return self.constant_groups[key] return MultiModuleConstant(
self.constant_groups[key], self.module_details, self.detector_name
)
def __iter__(self): def __iter__(self):
return iter(self.constant_groups) return iter(self.constant_groups)
...@@ -564,25 +560,29 @@ class CalibrationData(Mapping): ...@@ -564,25 +560,29 @@ class CalibrationData(Mapping):
aggs = prepare_selection( aggs = prepare_selection(
self.module_details, module_nums, aggregator_names, qm_names self.module_details, module_nums, aggregator_names, qm_names
) )
mmcs = { constant_groups = {}
cal_type: mmc.select_modules( matched_aggregators = set()
aggregator_names=set(aggs).intersection(mmc.aggregator_names) for cal_type, const_group in self.constant_groups.items():
) constant_groups[cal_type] = d = {
for (cal_type, mmc) in self.constant_groups.items() aggr: const for (aggr, const) in const_group.items() if aggr in aggs
} }
aggs = set().union(*[c.aggregator_names for c in mmcs.values()]) matched_aggregators.update(d.keys())
module_details = [m for m in self.module_details if m["karabo_da"] in aggs] module_details = [
return type(self)(mmcs, module_details, self.detector_name) m for m in self.module_details if m["karabo_da"] in matched_aggregators
]
return type(self)(constant_groups, module_details, self.detector_name)
def select_calibrations(self, calibrations) -> "CalibrationData": def select_calibrations(self, calibrations) -> "CalibrationData":
mmcs = {c: self.constant_groups[c] for c in calibrations} const_groups = {c: self.constant_groups[c] for c in calibrations}
return type(self)(mmcs, self.module_details, self.detector_name) return type(self)(const_groups, self.module_details, self.detector_name)
def merge(self, *others: "CalibrationData") -> "CalibrationData": def merge(self, *others: "CalibrationData") -> "CalibrationData":
det_names = set(cd.detector_name for cd in (self,) + others) det_names = set(cd.detector_name for cd in (self,) + others)
if len(det_names) > 1: if len(det_names) > 1:
raise Exception("Cannot merge calibration data for different " raise Exception(
"detectors: " + ", ".join(sorted(det_names))) "Cannot merge calibration data for different "
"detectors: " + ", ".join(sorted(det_names))
)
det_name = det_names.pop() det_name = det_names.pop()
cal_types = set(self.constant_groups) cal_types = set(self.constant_groups)
...@@ -608,15 +608,14 @@ class CalibrationData(Mapping): ...@@ -608,15 +608,14 @@ class CalibrationData(Mapping):
module_details = sorted(pdus_d.values(), key=lambda d: d["karabo_da"]) module_details = sorted(pdus_d.values(), key=lambda d: d["karabo_da"])
mmcs = {} constant_groups = {}
for cal_type in cal_types: for cal_type in cal_types:
d = {} d = constant_groups[cal_type] = {}
for caldata in (self,) + others: for caldata in (self,) + others:
if cal_type in caldata: if cal_type in caldata:
d.update(caldata[cal_type].constants) d.update(caldata.constant_groups[cal_type])
mmcs[cal_type] = MultiModuleConstant(d, module_details)
return type(self)(mmcs, module_details, det_name) return type(self)(constant_groups, module_details, det_name)
class ConditionsBase: class ConditionsBase:
......
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