def _unit_cell_clustering(self, experiments): crystal_symmetries = [ expt.crystal.get_crystal_symmetry() for expt in experiments ] lattice_ids = experiments.identifiers() from dials.algorithms.clustering.unit_cell import UnitCellCluster from xfel.clustering.cluster_groups import unit_cell_info ucs = UnitCellCluster.from_crystal_symmetries(crystal_symmetries, lattice_ids=lattice_ids) self.unit_cell_clusters, self.unit_cell_dendrogram, _ = ucs.ab_cluster( self.params.unit_cell_clustering.threshold, log=self.params.unit_cell_clustering.log, labels="lattice_id", write_file_lists=False, schnell=False, doplot=False, ) logger.info(unit_cell_info(self.unit_cell_clusters)) largest_cluster_lattice_ids = None for cluster in self.unit_cell_clusters: cluster_lattice_ids = [m.lattice_id for m in cluster.members] if largest_cluster_lattice_ids is None: largest_cluster_lattice_ids = cluster_lattice_ids elif len(cluster_lattice_ids) > len(largest_cluster_lattice_ids): largest_cluster_lattice_ids = cluster_lattice_ids dataset_selection = largest_cluster_lattice_ids return dataset_selection
def unit_cell_clustering(experiments, threshold, log=True, plot_name=None): from dials.algorithms.clustering.unit_cell import UnitCellCluster crystal_symmetries = [] for expt in experiments: crystal_symmetry = expt.crystal.get_crystal_symmetry( assert_is_compatible_unit_cell=False) crystal_symmetries.append(crystal_symmetry.niggli_cell()) lattice_ids = [expt.identifier for expt in experiments] ucs = UnitCellCluster.from_crystal_symmetries(crystal_symmetries, lattice_ids=lattice_ids) if plot_name is not None: import matplotlib matplotlib.use("Agg") from matplotlib import pyplot as plt plt.figure("Andrews-Bernstein distance dendogram", figsize=(12, 8)) ax = plt.gca() else: ax = None clusters, dendrogram, _ = ucs.ab_cluster( threshold, log=log, labels="lattice_id", write_file_lists=False, schnell=False, doplot=(plot_name is not None), ax=ax, ) if plot_name is not None: plt.tight_layout() plt.savefig(plot_name) plt.clf() return clusters, dendrogram
def _unit_cell_clustering(self, experiments): crystal_symmetries = [ expt.crystal.get_crystal_symmetry() for expt in experiments ] # lattice ids used to label plots, so want numerical ids lattice_ids = [ self.identifiers_to_ids_map[i] for i in experiments.identifiers() ] ucs = UnitCellCluster.from_crystal_symmetries(crystal_symmetries, lattice_ids=lattice_ids) self.unit_cell_clusters, self.unit_cell_dendrogram, _ = ucs.ab_cluster( self.params.unit_cell_clustering.threshold, log=self.params.unit_cell_clustering.log, labels="lattice_id", write_file_lists=False, schnell=False, doplot=False, ) logger.info(unit_cell_info(self.unit_cell_clusters)) largest_cluster_lattice_ids = None for cluster in self.unit_cell_clusters: cluster_lattice_ids = [m.lattice_id for m in cluster.members] if largest_cluster_lattice_ids is None: largest_cluster_lattice_ids = cluster_lattice_ids elif len(cluster_lattice_ids) > len(largest_cluster_lattice_ids): largest_cluster_lattice_ids = cluster_lattice_ids dataset_selection = largest_cluster_lattice_ids # now convert to actual identifiers for selection return [self.ids_to_identifiers_map[i] for i in dataset_selection]
def test_unit_cell(): # generate some random unit cells sgi = sgtbx.space_group_info("P1") crystal_symmetries = [ sgi.any_compatible_crystal_symmetry(volume=random.uniform(990, 1010)) for i in range(10) ] lattice_ids = flex.int_range(0, len(crystal_symmetries)).as_string() ucs = UnitCellCluster.from_crystal_symmetries(crystal_symmetries, lattice_ids=lattice_ids) clusters, dendrogram, _ = ucs.ab_cluster(write_file_lists=False, doplot=False)
def test_scipy_dendrogram_to_plotly_json(): # generate some random unit cells sgi = sgtbx.space_group_info("P1") crystal_symmetries = [ sgi.any_compatible_crystal_symmetry(volume=random.uniform(990, 1010)) for i in range(10) ] lattice_ids = flex.int_range(0, len(crystal_symmetries)).as_string() ucs = UnitCellCluster.from_crystal_symmetries(crystal_symmetries, lattice_ids=lattice_ids) _, dendrogram, _ = ucs.ab_cluster(write_file_lists=False, doplot=False) d = plots.scipy_dendrogram_to_plotly_json(dendrogram, title="Unit cell clustering") assert set(d) == {"layout", "data"}
def test_UnitCellAnalysisObserver(): # generate some random unit cells sgi = sgtbx.space_group_info("P1") unit_cells = [ sgi.any_compatible_unit_cell(volume=random.uniform(990, 1010)) for i in range(10) ] # generate experiment list experiments = ExperimentList() U = matrix.identity(3) for uc in unit_cells: B = matrix.sqr(uc.fractionalization_matrix()).transpose() direct_matrix = (U * B).inverse() experiments.append( Experiment(crystal=Crystal( direct_matrix[:3], direct_matrix[3:6], direct_matrix[6:9], space_group=sgi.group(), ))) # generate dendrogram crystal_symmetries = [ expt.crystal.get_crystal_symmetry() for expt in experiments ] lattice_ids = experiments.identifiers() ucs = UnitCellCluster.from_crystal_symmetries(crystal_symmetries, lattice_ids=lattice_ids) _, dendrogram, _ = ucs.ab_cluster(write_file_lists=False, doplot=False) # setup script script = mock.Mock() script._experiments = experiments script.unit_cell_dendrogram = dendrogram # test the observer observer = observers.UnitCellAnalysisObserver() observer.update(script) assert set(observer.data) == {"experiments", "dendrogram"} d = observer.make_plots() assert "unit_cell_graphs" in d