コード例 #1
0
    def _run_analysis(
        self, experiment_data: ExperimentData
    ) -> Tuple[List[AnalysisResultData], List["matplotlib.figure.Figure"]]:
        r"""Run analysis for :math:`T_\phi` experiment.
        It invokes CompositeAnalysis._run_analysis that will invoke
        _run_analysis for the two sub-experiments.
        Based on the results, it computes the result for :math:`T_phi`.
        """
        super()._run_analysis(experiment_data)

        t1_result = experiment_data.child_data(0).analysis_results("T1")
        t2star_result = experiment_data.child_data(1).analysis_results("T2star")
        tphi = 1 / (1 / t2star_result.value - 1 / (2 * t1_result.value))

        quality_tphi = (
            "good" if (t1_result.quality == "good" and t2star_result.quality == "good") else "bad"
        )

        analysis_results = [
            AnalysisResultData(
                name="T_phi",
                value=tphi,
                chisq=None,
                quality=quality_tphi,
                extra={"unit": "s"},
            )
        ]
        return analysis_results, []
コード例 #2
0
    def _initialize_components(self,
                               experiment_data: ExperimentData) -> List[str]:
        """Initialize child data components and return list of child experiment IDs"""
        # Check if component child experiment data containers have already
        # been created. If so the list of indices for their positions in the
        # ordered dict should exist. Index is used to extract the experiment
        # IDs for each child experiment which can change when re-running analysis
        # if replace_results=False, so that we update the correct child data
        # for each component experiment
        component_index = experiment_data.metadata.get("component_child_index",
                                                       [])
        if not component_index:
            experiment = experiment_data.experiment
            if experiment is None:
                raise AnalysisError(
                    "Cannot run composite analysis on an experiment data without either "
                    "a composite experiment, or composite experiment metadata."
                )
            # If the experiment Construct component data and update indices
            start_index = len(experiment_data.child_data())
            component_index = []
            for i, sub_exp in enumerate(experiment.component_experiment()):
                sub_data = sub_exp._initialize_experiment_data()
                experiment_data.add_child_data(sub_data)
                component_index.append(start_index + i)
            experiment_data.metadata["component_child_index"] = component_index

        # Child components exist so we can get their ID for accessing them
        child_ids = experiment_data._child_data.keys()
        component_ids = [child_ids[idx] for idx in component_index]
        return component_ids
コード例 #3
0
    def _add_child_data(self, experiment_data: ExperimentData):
        """Save empty component experiment data as child data.

        This will initialize empty ExperimentData objects for each component
        experiment and add them as child data to the main composite experiment
        ExperimentData container container for saving.

        Args:
            experiment_data: a composite experiment experiment data container.
        """
        component_index = experiment_data.metadata.get("component_child_index",
                                                       [])
        if component_index:
            # Child components are already initialized
            return

        # Initialize the component experiment data containers and add them
        # as child data to the current experiment data
        child_components = self._initialize_component_experiment_data(
            experiment_data)
        start_index = len(experiment_data.child_data())
        for i, subdata in enumerate(child_components):
            experiment_data.add_child_data(subdata)
            component_index.append(start_index + i)

        # Store the indices of the added child data in metadata
        experiment_data.metadata["component_child_index"] = component_index
コード例 #4
0
    def _run_analysis(self, experiment_data: ExperimentData):
        # Extract job metadata for the component experiments so it can be added
        # to the child experiment data incase it is required by the child experiments
        # analysis classes
        composite_exp = experiment_data.experiment
        component_exps = composite_exp.component_experiment()
        component_metadata = experiment_data.metadata.get(
            "component_metadata", [{}] * composite_exp.num_experiments)

        # Initialize component data for updating and get the experiment IDs for
        # the component child experiments in case there are other child experiments
        # in the experiment data
        component_ids = self._initialize_components(composite_exp,
                                                    experiment_data)

        # Compute marginalize data for each component experiment
        marginalized_data = self._marginalize_data(experiment_data.data())

        # Add the marginalized component data and component job metadata
        # to each component child experiment. Note that this will clear
        # any currently stored data in the experiment. Since copying of
        # child data is handled by the `replace_results` kwarg of the
        # parent container it is safe to always clear and replace the
        # results of child containers in this step
        for i, (sub_data,
                sub_exp) in enumerate(zip(marginalized_data, component_exps)):
            sub_exp_data = experiment_data.child_data(component_ids[i])

            # Clear any previously stored data and add marginalized data
            sub_exp_data._data.clear()
            sub_exp_data.add_data(sub_data)

            # Add component job metadata
            sub_exp_data.metadata.update(component_metadata[i])

            # Run analysis
            # Since copy for replace result is handled at the parent level
            # we always run with replace result on component analysis
            sub_exp.analysis.run(sub_exp_data, replace_results=True)

        # Wait for all component analysis to finish before returning
        # the parent experiment analysis results
        for comp_id in component_ids:
            experiment_data.child_data(comp_id).block_for_results()

        return [], []
コード例 #5
0
    def _component_experiment_data(
            self, experiment_data: ExperimentData) -> List[ExperimentData]:
        """Return a list of marginalized experiment data for component experiments.

        Args:
            experiment_data: a composite experiment experiment data container.

        Returns:
            The list of analysis-ready marginalized experiment data for each
            component experiment.

        Raises:
            AnalysisError: if the component experiment data cannot be extracted.
        """
        if not self._flatten_results:
            # Retrieve child data for component experiments for updating
            component_index = experiment_data.metadata.get(
                "component_child_index", [])
            if not component_index:
                raise AnalysisError(
                    "Unable to extract component child experiment data")
            component_expdata = [
                experiment_data.child_data(i) for i in component_index
            ]
        else:
            # Initialize temporary ExperimentData containers for
            # each component experiment to analysis on. These will
            # not be saved but results and figures will be collected
            # from them
            component_expdata = self._initialize_component_experiment_data(
                experiment_data)

        # Compute marginalize data for each component experiment
        marginalized_data = self._marginalized_component_data(
            experiment_data.data())

        # Add the marginalized component data and component job metadata
        # to each component child experiment. Note that this will clear
        # any currently stored data in the experiment. Since copying of
        # child data is handled by the `replace_results` kwarg of the
        # parent container it is safe to always clear and replace the
        # results of child containers in this step
        for sub_expdata, sub_data in zip(component_expdata, marginalized_data):
            # Clear any previously stored data and add marginalized data
            sub_expdata._data.clear()
            sub_expdata.add_data(sub_data)

        return component_expdata
コード例 #6
0
    def _component_experiment_data(
            self, experiment_data: ExperimentData) -> List[ExperimentData]:
        """Return a list of component child experiment data"""
        # Initialize component data for updating and get the experiment IDs for
        # the component child experiments in case there are other child experiments
        # in the experiment data
        component_ids = self._initialize_components(experiment_data)
        if len(component_ids) != len(self._analyses):
            raise AnalysisError(
                "Number of experiment components does not match number of"
                " component analysis classes")

        # Extract job metadata for the component experiments so it can be added
        # to the child experiment data in case it is required by the child experiments
        # analysis classes
        component_metadata = experiment_data.metadata.get(
            "component_metadata", [{}] * len(component_ids))

        # Compute marginalize data for each component experiment
        marginalized_data = self._component_data(experiment_data.data())

        # Add the marginalized component data and component job metadata
        # to each component child experiment. Note that this will clear
        # any currently stored data in the experiment. Since copying of
        # child data is handled by the `replace_results` kwarg of the
        # parent container it is safe to always clear and replace the
        # results of child containers in this step
        component_data = []
        for i, sub_data in enumerate(marginalized_data):
            sub_exp_data = experiment_data.child_data(component_ids[i])

            # Clear any previously stored data and add marginalized data
            sub_exp_data._data.clear()
            sub_exp_data.add_data(sub_data)

            # Add component job metadata
            sub_exp_data.metadata.update(component_metadata[i])
            component_data.append(sub_exp_data)

        return component_data