prop/lib/mix/tasks/scorer_diff.ex
Graham McIntire 1174ecd9e5 Fix all 12 dialyzer warnings
- Replace MapSet with plain list + `in` (features.ex, scorer_diff.ex)
- Remove undefined Beacon.t() type reference (range_estimate.ex)
- Remove dead else branch in find_region (inversion.ex)
- Handle Nx special values in to_float catch-all (recalibrator.ex)
- Remove unreachable catch-all clauses (hrrr_native_client.ex, ncei_metar_client.ex)
- Remove unnecessary nil guards on always-typed values (show.ex)
- Remove dead sky_note/wind_note non-nil clauses (show.ex)
- Remove dead if-guard on always-truthy derive result (hrrr_native_derive.ex)
- Add @spec to path_integrated_conditions (scorer.ex)
2026-04-11 18:08:18 -05:00

92 lines
2.7 KiB
Elixir

defmodule Mix.Tasks.ScorerDiff do
@shortdoc "Compare two weight sets on recent propagation scores"
@moduledoc """
Loads the most recent HRRR frame's propagation scores and compares
the current weights against a new set, printing a markdown diff report.
## Usage
mix scorer_diff --new-weights '{"humidity":0.18,"time_of_day":0.20,"td_depression":0.10,"refractivity":0.08,"sky":0.08,"season":0.08,"wind":0.05,"rain":0.08,"pressure":0.05,"pwat":0.10}'
## Options
* `--new-weights` — JSON string of new weights (required)
* `--out` — optional file path to write the report
"""
use Mix.Task
alias Microwaveprop.Propagation.BandConfig
alias Microwaveprop.Propagation.ScorerDiff
@impl Mix.Task
def run(argv) do
Mix.Task.run("app.start")
Oban.pause_all_queues(Oban)
{opts, _, _} =
OptionParser.parse(argv,
switches: [new_weights: :string, out: :string]
)
new_weights_json = Keyword.fetch!(opts, :new_weights)
out_path = Keyword.get(opts, :out)
new_weights = Jason.decode!(new_weights_json)
validate_weights!(new_weights)
old_weights = stringify_weights(BandConfig.weights())
Mix.shell().info("Comparing current weights vs new weights...")
Mix.shell().info("Old: #{inspect(old_weights)}")
Mix.shell().info("New: #{inspect(new_weights)}")
Mix.shell().info("")
result = ScorerDiff.compare(old_weights, new_weights)
markdown = ScorerDiff.to_markdown(result)
IO.puts(markdown)
if out_path do
File.mkdir_p!(Path.dirname(out_path))
File.write!(out_path, markdown)
Mix.shell().info("Wrote report to #{out_path}")
end
end
defp stringify_weights(weights) do
Map.new(weights, fn {k, v} -> {to_string(k), v} end)
end
defp validate_weights!(weights) do
validate_weight_keys!(weights)
validate_weight_sum!(weights)
end
defp validate_weight_keys!(weights) do
expected_keys = ~w(humidity time_of_day td_depression refractivity sky season wind rain pressure pwat)
provided_keys = Map.keys(weights)
missing = expected_keys -- provided_keys
extra = provided_keys -- expected_keys
errors =
[]
|> prepend_if(missing != [], "Missing: #{inspect(missing)}")
|> prepend_if(extra != [], "Extra: #{inspect(extra)}")
if errors != [] do
Mix.raise("Invalid weight keys. " <> Enum.join(errors, " "))
end
end
defp validate_weight_sum!(weights) do
sum = weights |> Map.values() |> Enum.sum() |> Float.round(4)
if !(sum >= 0.99 and sum <= 1.01) do
Mix.raise("Weights must sum to ~1.0, got #{sum}")
end
end
defp prepend_if(list, true, item), do: list ++ [item]
defp prepend_if(list, false, _item), do: list
end