# `Macaw.Draw.Contour.Density`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/contour/density.ex#L1)

Two-dimensional Gaussian kernel density over pixel positions, contoured (spec/09 §12),
copied from visualize's `Visualize.Contour.Density` and the `density` step of its
`Visualize.Chart.Transform.Geo`.

    iex> {:ok, d} = Macaw.Draw.Contour.Density.new(size: {40, 40}, cell_size: 2, bandwidth: 4, thresholds: [0.001])
    iex> [%{value: 0.001, coordinates: [[ring]]}] = Macaw.Draw.Contour.Density.compute(d, [20], [20], nil)
    iex> hd(ring) == List.last(ring)
    true

It is spatial work and stays in the frame: it bins where points land on the plot. The
density of values along one axis (a violin, a ridgeline) is `macaw_data`'s `density`
operation, not this. Points come as columns, never rows: visualize's `x/2`, `y/2` and
`weight/2` accessors over row maps are not carried over.

The grid has `div(w, cell_size) + 1` columns and `div(h, cell_size) + 1` rows. Each point
adds `wt · exp(−d² / 2σ²) / 2πσ²` to every cell within `ceil(3 · bandwidth / cell_size)`
cells of it, `σ = bandwidth / cell_size`, accumulated by cell index in point order and
laid out as the grid once, so the estimate costs the points times the footprint plus the
cells, never the points times the grid (D-72). Contours are traced with smoothing on and
every coordinate multiplied by `cell_size`, so they are in pixels.

# `error`

```elixir
@type error() :: {:error, [Macaw.Base.Fault.t()]}
```

A refusal: faults with their paths.

# `t`

```elixir
@type t() :: %Macaw.Draw.Contour.Density{
  bandwidth: number(),
  cell_size: pos_integer(),
  height: number(),
  thresholds: [number()] | pos_integer(),
  width: number()
}
```

# `columns`

```elixir
@spec columns([Macaw.Draw.Contour.contour_result()]) ::
  %{required(String.t()) =&gt; [term()]} | error()
```

The density layer's columns: one row per ring of every threshold, `"value"` and `"path"` (closed).

# `compute`

```elixir
@spec compute(t(), [number() | nil], [number() | nil], [number() | nil] | nil) ::
  [Macaw.Draw.Contour.contour_result()] | error()
```

The density contours of the points `{xs[i], ys[i]}` in pixels, each weighted
`weights[i]` (spec/09 §12.2): `weights` a list of one length with the points, or `nil`
for weight 1 each. A point with a `nil` or non-numeric coordinate is skipped; a `nil`
weight is 1. Lists of different lengths are `{[n], {:length, length(xs)}}` at the
shorter's position.

# `new`

```elixir
@spec new(keyword() | map()) :: {:ok, t()} | error()
```

A density estimator, its options checked at once (spec/09 §12.1): `size` (`{w, h}`
positive, `{960, 500}`; the Frame passes the plot), `cell_size` (a positive integer, `4`),
`bandwidth` (positive, `20`, the kernel's standard deviation in pixels) and `thresholds`
(a list of numbers or a positive count, `20`, over density values).

# `paths`

```elixir
@spec paths([Macaw.Draw.Contour.contour_result()]) ::
  [%{value: number(), path: Macaw.Draw.IR.Path.t()}] | error()
```

One IR path per threshold, `%{value, path}`, every ring closed (spec/09 §12.2).

---

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