# `Nous.Eval.Scoring`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/eval/scoring.ex#L1)

Runs a sample's scorers and composes their results into a verdict.

    {:ok, scorers} = Nous.Eval.Scoring.init(specs)
    %{verdict: verdict, case_score: score} = Nous.Eval.Scoring.score(scorers, input)

`init/1` calls every scorer's `c:Nous.Eval.Scorer.init/1` once, so bad
options fail the eval before any agent runs. `score/3` runs each scorer in
its own task under `Nous.TaskSupervisor` with a time limit; a scorer that
raises, throws, exits or times out becomes a `Nous.Eval.ScorerError` and
never discards another scorer's result.

## Result validation

Every `Nous.Eval.Score` is checked, never coerced: the name must be a
string, `passed?` a boolean, and the value `:pass`, `:fail`,
`{:label, string}` or a float in `0.0..1.0`. A float outside that range is
a `:non_finite` error, any other shape `:invalid_config`. A required
scorer that skips without being declared `skippable: true` is an
`:invalid_config` error: "the gate did not look" must not read as "the
gate passed".

## Verdict

Scores inherit `required` and `weight` from the `Nous.Eval.Scorer.Spec`
that declared their scorer (a scorer emitting several scores applies its
declaration to each).

  1. `:error` if any scorer returned an error
  2. `:fail` if any required scorer's score has `passed?: false`
  3. `:fail` if the soft scores (`required: false`, `weight > 0`) exist
     and their weighted mean is below the threshold (the case's
     `threshold`, default `0.5`; `Nous.Eval.run/2` logs one warning per
     run naming the soft-scored cases that rely on the default)
  4. `:pass` otherwise

`weight: 0` tracks a metric without affecting the verdict. Skipped soft
scorers are left out of the mean, as are labels without a float mapping
(see `value_to_float/2`).

`case_score` is the gated objective for optimizers: `nil` on `:error`,
`0.0` when a required score failed, else the soft weighted mean, or `1.0`
without soft scores. It ignores the threshold.

# `result`

```elixir
@type result() :: %{
  scores: [Nous.Eval.Score.t()],
  scorer_errors: [Nous.Eval.ScorerError.t()],
  skipped: [{String.t(), String.t()}],
  verdict: :pass | :fail | :error,
  case_score: float() | nil
}
```

# `init`

```elixir
@spec init([Nous.Eval.Scorer.Spec.t()]) ::
  {:ok, [Nous.Eval.Scoring.Prepared.t()]} | {:error, {module(), term()}}
```

Initializes scorer specs. Calls each scorer's `c:Nous.Eval.Scorer.init/1`
once and stops at the first failure with `{:error, {module, reason}}`.

# `score`

```elixir
@spec score([Nous.Eval.Scoring.Prepared.t()], Nous.Eval.ScoreInput.t(), keyword()) ::
  result()
```

Scores one sample with initialized scorers and composes the verdict.

Options:

  * `:timeout` - per-scorer limit in ms, either one integer for every
    scorer or `%{deterministic: ms, model_graded: ms}` (default 5 000 ms
    deterministic, 60 000 ms model-graded)
  * `:threshold` - overrides the case's soft-group threshold
  * `:supervisor` - the `Task.Supervisor` to run scorers under (default
    `Nous.TaskSupervisor`)

Returns every score, scorer error and skip (`{scorer_id, reason}`), the
verdict and the gated `case_score`.

# `value_to_float`

```elixir
@spec value_to_float(Nous.Eval.Score.value(), %{optional(String.t()) =&gt; number()}) ::
  {:ok, float()} | :error
```

Converts a score value to a float for aggregation.

`:pass` is `1.0`, `:fail` is `0.0`, a float is itself, and
`{:label, label}` is `label_values[label]` when the scorer declared a
mapping (a scorer emitting labels puts it in the score's
`metadata.label_values`). A label without a mapping returns `:error`: it
is reported as a distribution, not averaged.

    iex> Nous.Eval.Scoring.value_to_float(:pass)
    {:ok, 1.0}
    iex> Nous.Eval.Scoring.value_to_float({:label, "good"}, %{"good" => 0.75})
    {:ok, 0.75}
    iex> Nous.Eval.Scoring.value_to_float({:label, "odd"}, %{"good" => 0.75})
    :error

---

*Consult [api-reference.md](api-reference.md) for complete listing*
