async def _score_attention_head(
        cached_simulation_results: AttentionSimulationResults | None,
        attention_record: NeuronRecord,
        request: ScoreRequest,
        max_sequences: int | None = None,
    ) -> tuple[float | None, AttentionSimulationResults]:
        """Score an explanation for an attention head. Add it to the cached set of simulation
        results, or create the simulation results object if the cache was empty."""
        if attention_record.max_activation < 0:
            raise HTTPException(
                status_code=422, detail="Attention head is not activated on the dataset"
            )
        valid_activation_records = attention_record.valid_activation_records(
            activation_record_slice_params=ActivationRecordSliceParams(n_examples_per_split=5)
        )

        if max_sequences is not None:
            valid_activation_records = valid_activation_records[:max_sequences]

        scored_attention_simulation = await attention_head_scorer.score_explanation(
            activation_records=valid_activation_records,
            explanation=request.explanation,
            max_activation=attention_record.max_activation,
        )

        scored_explanation = ScoredAttentionExplanation(
            explanation=request.explanation,
            scored_attention_simulation=scored_attention_simulation,
        )

        if cached_simulation_results is not None:
            cached_simulation_results.scored_explanations.append(scored_explanation)
        else:
            cached_simulation_results = AttentionSimulationResults(
                attention_head_id=NeuronId(
                    neuron_index=request.activation_index,
                    layer_index=request.layer_index,
                ),
                scored_explanations=[scored_explanation],
            )
        return scored_explanation.get_preferred_score(), cached_simulation_results
Score an explanation for an attention head. Add it to the cached set of simulation
        results, or create the simulation results object if the cache was empty.
neuron_explainer/activation_server/explainer_routes.py