def _merge_simulation_results(
        azure_simulation_results: NeuronSimulationResults | AttentionSimulationResults | None,
        local_simulation_results: NeuronSimulationResults | AttentionSimulationResults | None,
    ) -> NeuronSimulationResults | AttentionSimulationResults | None:
        """Merge scored explanations from the local cache and azure into a single NeuronSimulationResults
        or AttentionSimulationResults object."""
        if azure_simulation_results is None and local_simulation_results is None:
            return None

        if isinstance(azure_simulation_results, NeuronSimulationResults) or isinstance(
            local_simulation_results, NeuronSimulationResults
        ):
            assert (
                isinstance(azure_simulation_results, NeuronSimulationResults)
                or azure_simulation_results is None
            )
            assert (
                isinstance(local_simulation_results, NeuronSimulationResults)
                or local_simulation_results is None
            )
            unique_scored_explanations = {}
            if azure_simulation_results is not None:
                for scored_explanation in azure_simulation_results.scored_explanations:
                    unique_scored_explanations[scored_explanation.explanation] = scored_explanation
            if local_simulation_results is not None:
                for scored_explanation in local_simulation_results.scored_explanations:
                    unique_scored_explanations[scored_explanation.explanation] = scored_explanation
            return NeuronSimulationResults(
                neuron_id=(
                    azure_simulation_results.neuron_id
                    if azure_simulation_results is not None
                    else local_simulation_results.neuron_id  # type: ignore # mypy doesn't understand that both can't be None
                ),
                scored_explanations=list(unique_scored_explanations.values()),
            )
        else:
            assert (
                isinstance(azure_simulation_results, AttentionSimulationResults)
                or azure_simulation_results is None
            )
            assert (
                isinstance(local_simulation_results, AttentionSimulationResults)
                or local_simulation_results is None
            )
            unique_scored_attn_explanations = {}
            if azure_simulation_results is not None:
                for scored_attn_explanation in azure_simulation_results.scored_explanations:
                    unique_scored_attn_explanations[
                        scored_attn_explanation.explanation
                    ] = scored_attn_explanation
            if local_simulation_results is not None:
                for scored_attn_explanation in local_simulation_results.scored_explanations:
                    unique_scored_attn_explanations[
                        scored_attn_explanation.explanation
                    ] = scored_attn_explanation
            return AttentionSimulationResults(
                attention_head_id=(
                    azure_simulation_results.attention_head_id
                    if azure_simulation_results is not None
                    else local_simulation_results.attention_head_id  # type: ignore # mypy doesn't understand that both can't be None
                ),
                scored_explanations=list(unique_scored_attn_explanations.values()),
            )
Merge scored explanations from the local cache and azure into a single NeuronSimulationResults
        or AttentionSimulationResults object.
neuron_explainer/activation_server/explainer_routes.py