From d74643ba9b48dfaa3ae687234f44d2a2a08ed16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Tcho=C5=84?= Date: Mon, 23 Jan 2023 11:37:07 -0700 Subject: [PATCH] combine_experiments: Handle default max_clusters (#2311) Prevent dials.combine_experiments from raising TypeError in cases where clustering.use=True but the default clustering.max_clusters=None. Instead, interpret max_clusters=None as requesting all of the clusters. --- AUTHORS | 1 + newsfragments/2311.bugfix | 1 + src/dials/command_line/combine_experiments.py | 8 +++++--- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 newsfragments/2311.bugfix diff --git a/AUTHORS b/AUTHORS index 62c4522fd6..9d94f7e9e5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -6,6 +6,7 @@ Asmit Bhowmick Benjamin Williams Billy Poon Daniel Paley +Daniel Tchon David Waterman Derek Mendez Graeme Winter diff --git a/newsfragments/2311.bugfix b/newsfragments/2311.bugfix new file mode 100644 index 0000000000..0561a77435 --- /dev/null +++ b/newsfragments/2311.bugfix @@ -0,0 +1 @@ +``dials.combine_experiments``: Prevent default ``clustering.max_clusters=None`` from raising error when clustering. diff --git a/src/dials/command_line/combine_experiments.py b/src/dials/command_line/combine_experiments.py index ac18992d00..f8e2fd13f3 100644 --- a/src/dials/command_line/combine_experiments.py +++ b/src/dials/command_line/combine_experiments.py @@ -745,6 +745,8 @@ def save_in_batches( # cluster the resulting experiments if requested if params.clustering.use and len(experiments) > 1: + if params.clustering.max_clusters == 0: + sys.exit("Error: max_clusters must be None or >0") clustered = do_unit_cell_clustering( experiments, reflections, @@ -752,9 +754,9 @@ def save_in_batches( threshold=params.clustering.threshold, ) n_clusters = len(clustered) - clusters = sorted(clustered.clusters, key=len, reverse=True)[ - : min(params.clustering.max_clusters, n_clusters) - ] + clusters = sorted(clustered.clusters, key=len, reverse=True) + if params.clustering.max_clusters is not None: + clusters = clusters[: params.clustering.max_clusters] if params.clustering.exclude_single_crystal_clusters: clusters = [c for c in clusters if len(c) > 1] clustered_experiments = [