Beispiel #1
0
def test_Subject():
    """Test the subject class."""

    # First register events
    subject = Subject(events=["event1", "event2"])
    assert "event1" in subject.observers
    assert "event2" in subject.observers

    # Test the get_observers and register_observer and unregister_observer method
    observer = Observer()
    assert subject.get_observers("event1") == {}
    assert subject.get_observers("event2") == {}
    subject.register_observer("event1", observer)
    assert subject.get_observers("event1") == {observer: observer.update}
    assert subject.get_observers("event2") == {}
    subject.unregister_observer("event1", observer)
    assert subject.get_observers("event1") == {}
    assert subject.get_observers("event2") == {}

    # Try registering the observer with a different callback
    class MyObserver(Observer):
        """Test observer implementation class."""
        def __init__(self):
            self.count = 0

        def mycallback(self, _):
            """Custom callback method."""
            self.count += 1

    observer = MyObserver()
    subject = Subject(events=["event1", "event2"])
    subject.register_observer("event1", observer, callback="mycallback")
    assert subject.get_observers("event1") == {observer: observer.mycallback}
    assert subject.get_observers("event2") == {}

    # Now test call to nofity.
    subject.notify("event1")
    assert observer.count == 1
    subject.notify("event2")
    assert observer.count == 1

    # Now test the notify decorator
    class MySubject(Subject):
        """Test subject implementation class."""
        def __init__(self):
            super().__init__(events=["event1"])

        @Subject.notify_event("event1")
        def test_method(self):
            """Test method for warpping."""
            return "test_return_value"

    mysubject = MySubject()
    myobserver = MyObserver()
    mysubject.register_observer("event1", myobserver, callback="mycallback")
    x = mysubject.test_method()
    assert myobserver.count == 1
    assert x == "test_return_value"
Beispiel #2
0
    def __init__(self, intensities, params):
        """Initialise a CosymAnalysis object.

        Args:
          intensities (cctbx.miller.array): The intensities on which to perform
            cosym analysis.
          params (libtbx.phil.scope_extract): Parameters for the analysis.
        """
        super().__init__(
            intensities,
            normalisation=params.normalisation,
            lattice_symmetry_max_delta=params.lattice_symmetry_max_delta,
            d_min=params.d_min,
            min_i_mean_over_sigma_mean=params.min_i_mean_over_sigma_mean,
            min_cc_half=params.min_cc_half,
            relative_length_tolerance=None,
            absolute_angle_tolerance=None,
            best_monoclinic_beta=params.best_monoclinic_beta,
        )
        Subject.__init__(
            self,
            events=["optimised", "analysed_symmetry", "analysed_clusters"])

        self.params = params
        if self.params.space_group is not None:

            def _map_space_group_to_input_cell(intensities, space_group):
                from cctbx.sgtbx.bravais_types import bravais_lattice

                best_subgroup = find_matching_symmetry(
                    intensities.unit_cell(),
                    space_group,
                    best_monoclinic_beta=str(
                        bravais_lattice(group=space_group)) == "mI",
                )
                cb_op_inp_best = best_subgroup["cb_op_inp_best"]
                best_subsym = best_subgroup["best_subsym"]
                cb_op_best_primitive = (
                    best_subsym.change_of_basis_op_to_primitive_setting())

                sg_cb_op_inp_primitive = (space_group.info(
                ).change_of_basis_op_to_primitive_setting())
                sg_primitive = space_group.change_basis(sg_cb_op_inp_primitive)
                sg_best = sg_primitive.change_basis(
                    cb_op_best_primitive.inverse())
                # best_subgroup above is the bravais type, so create thin copy here with the
                # user-input space group instead
                best_subsym = best_subsym.customized_copy(
                    space_group_info=sg_best.info())
                best_subgroup = {
                    "subsym":
                    best_subsym.change_basis(cb_op_inp_best.inverse()),
                    "best_subsym": best_subsym,
                    "cb_op_inp_best": cb_op_inp_best,
                }

                intensities = intensities.customized_copy(
                    space_group_info=sg_best.change_basis(
                        cb_op_inp_best.inverse()).info())
                return intensities, best_subgroup

            self.intensities, self.best_subgroup = _map_space_group_to_input_cell(
                self.intensities, self.params.space_group.group())
            self.best_subgroup["cb_op_inp_best"] = (
                self.best_subgroup["cb_op_inp_best"] * self.cb_op_inp_min)
            self.input_space_group = self.intensities.space_group()

        else:
            self.input_space_group = None

        if self.params.lattice_group is not None:
            tmp_intensities, _ = _map_space_group_to_input_cell(
                self.intensities, self.params.lattice_group.group())
            self.params.lattice_group = tmp_intensities.space_group_info()