from enum import Enum
import warnings
[docs]class DetectorModule:
module_uuid = None
detector_uuid = None
detector_type = None
version = None
owner = None
def __init__(self, uuid=0, version=0):
self.version = version
self.module_uuid = uuid
@property
def type_name(self):
return self.detector_type.value
@property
def device_name(self):
if self.detector_type.name.upper() in self.__name__.upper():
return self.__name__
return "{}_{}".format(self.__name__, self.detector_type.name)
def __repr__(self):
return "{}: {}".format(self.device_name, self.uuid)
[docs]class DetectorSpec:
detector_type = None
def __init__(self):
for key in dir(self):
property = getattr(self, key)
def _find_pos(self, mod):
if mod.owner is None:
return "not installed"
for k, v in vars(mod.owner).items():
if v is mod:
return k.replace("_", "")
return "not installed"
def _repr_html_(self):
if isinstance(self, DetectorModule):
mods = [self]
else:
mods = [getattr(self, d, None) for d in dir(self)
if isinstance(getattr(self, d, None), DetectorModule)]
bstyle = 'style="border: 1px solid black;"'
html = """
<p>
<h2>Detector: {det}, Modules: {nmods}</h2>
</p>
<table style="border: 1px solid black; border-collapse: collapse;">
<tr><th {bstyle}>Module name</th><th {bstyle}>UUID</th>
<th {bstyle}>Installed at</th><th {bstyle}>Position</th>
</tr>
""".format(det=self.__class__.__name__.replace("_", ""),
nmods=len(mods), bstyle=bstyle)
for mod in mods:
owner = mod.owner
owner = None if not owner else owner.__name__.replace("_", "")
html += """
<tr>
<td {bstyle}>{name}</td><td {bstyle}>
<span style="background-color: #b7f7c6">{uuid1}</span>
<span style="background-color: #bfd2f2">{uuid2}</span>
<span style="background-color: #d6d6d6">{uuid3}</span>
</td {bstyle}><td {bstyle}>{inst}
</td><td {bstyle}>{pos}</td>
</tr>
""".format(name=mod.device_name,
uuid1=mod.uuid[:3],
uuid2=mod.uuid[3:9],
uuid3=mod.uuid[9:],
inst=owner,
pos=self._find_pos(mod), bstyle=bstyle)
html += """
</table>
"""
return html
[docs]class DetectorInstance:
def _repr_html_(self):
b1 = 'bgcolor="#7ee878" width="250px" height="50px"'
view = """
<h2> Detector: {det}, Detector UUID: {duuid:03d},
Modules: {nmods}
</h2>
<table width="250px" height="30px" padding=0>
<tr>
<td {b1}>{M1}</td>
</tr>
</table>
"""
rdr = view.format(det=self.__class__.__name__.replace("_", ""),
duuid=self.detector_uuid,
nmods=1,
M1=repr(self),
b1=b1)
return rdr
[docs]class DetectorTypes(Enum):
AGIPD = "AGIPD-Type"
LPD = "LPD-Type"
DSSC = "DSSC-Type"
pnCCD = "pnCCD-Type"
fastCCD = "fastCCD-Type"
jungfrau = "jungfrau-Type"
ePix100 = "ePix100-Type"
ePix10K = "ePix10K-Type"
[docs]def make_detector_instance(detector):
for key, property in vars(detector).items():
if hasattr(property, "detector_uuid"):
uuid = getattr(property, "detector_uuid", None)
if uuid is None:
property.detector_uuid = detector.detector_uuid
property.owner = detector
if hasattr(detector, "parent"):
name = None
for pkey, pprop in vars(detector.parent).items():
if property is pprop:
name = pkey
break
property.__name__ = name
else:
property.__name__ = key
else:
msg = "{}: Module {} is already assigned to {}"
warnings.warn(msg.format(detector, property.__name__,
property.owner),
Warning)
if hasattr(property, "detector_type"):
if hasattr(detector, "parent") and detector.parent.detector_type:
property.detector_type = detector.parent.detector_type
inst = detector()
name = detector.__name__
if name.startswith("_"):
name = name[1:]
inst.__name__ = name
return inst