Skip to content

Commit

Permalink
Easier override of panel geometry (cctbx#644)
Browse files Browse the repository at this point in the history
When setting panels by PHIL, merge multiple definitions first by id. This fixes cctbx#299.
  • Loading branch information
dagewa authored and toastisme committed Jul 11, 2024
1 parent 0064557 commit ff9f5f8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions newsfragments/299.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Panel geometry definitions in PHIL are merged by panel id before constructing panels
48 changes: 46 additions & 2 deletions src/dxtbx/model/detector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
from copy import deepcopy

import pycbf

Expand Down Expand Up @@ -212,6 +213,42 @@
)


def merge_panel_scope_extracts_by_id(panel_params):

id_to_params = {}
for (i, params) in enumerate(panel_params):
if params.id not in id_to_params:
id_to_params[params.id] = [
i,
]
else:
id_to_params[params.id].append(i)

merged_params = []
for params_set in id_to_params.values():
params0 = deepcopy(panel_params[params_set[0]])
for i in params_set[1:]:
params1 = deepcopy(panel_params[i])
for key in params1.__dict__:
if (
key.startswith("_")
or key == "id"
or params0.__dict__[key] == params1.__dict__[key]
):
continue
if (
params0.__dict__[key] is not None
and params1.__dict__[key] is not None
):
raise RuntimeError(
f"Multiple definitions for {key} for panel id={params0.id}"
)
if params0.__dict__[key] is None:
params0.__dict__[key] = params1.__dict__[key]
merged_params.append(params0)
return merged_params


class DetectorFactory:
"""A factory class for detector objects, which will encapsulate standard
detector designs to make it a little easier to get started with these. In
Expand All @@ -229,7 +266,11 @@ def generate_from_phil(params, beam=None):

# Create a list of panels
panel_list = {}
for panel_params in params.detector.panel:

# merge panel params by id first
merged = merge_panel_scope_extracts_by_id(params.detector.panel)

for panel_params in merged:
panel = Panel()
if panel_params.name is not None:
panel.set_name(panel_params.name)
Expand Down Expand Up @@ -335,8 +376,11 @@ def overwrite_from_phil(params, detector, beam=None):
Overwrite from phil parameters
"""
# merge panel params by id first
merged = merge_panel_scope_extracts_by_id(params.detector.panel)

# Override any panel parameters
for panel_params in params.detector.panel:
for panel_params in merged:
panel = detector[panel_params.id]
if panel_params.name is not None:
panel.set_name(panel_params.name)
Expand Down

0 comments on commit ff9f5f8

Please sign in to comment.