Visualize.Data (Visualize v0.2.25)

Copy Markdown View Source

Data transformation and statistical utilities.

Provides functions for summarizing data, computing statistics, and transforming data for visualization.

Examples

data = [
  %{category: "A", value: 10},
  %{category: "A", value: 20},
  %{category: "B", value: 15}
]

Visualize.Data.extent(data, & &1.value)
# => {10, 20}

Visualize.Data.group(data, & &1.category)
# => %{"A" => [...], "B" => [...]}

Summary

Functions

Creates histogram bins from continuous data.

Creates a cross product of two arrays.

Returns the standard deviation.

Returns {min, max} as a tuple.

Groups data by a key function.

Largest-Triangle-Three-Buckets: about n of the elements, chosen to keep the series' visual shape (spec/08 §5.2; Steinarsson 2013).

M4: per pixel column, the first, last, least-y and greatest-y elements — the elements a line drawn width pixels wide needs to draw the same pixels as all of them (spec/08 §5.2; Jugel et al. 2014).

Returns the maximum value.

Returns the arithmetic mean.

Returns the median value.

Returns the minimum value.

Returns the p-quantile value.

Generates a range of numbers.

Groups data and applies a reducer to each group.

Sorts data by an accessor function.

Returns the sum of values.

Generates evenly-spaced tick values for a domain.

Returns unique values by accessor.

Returns the variance of values.

Functions

bin(data, opts \\ [])

@spec bin([number()], keyword()) :: [map()]

Creates histogram bins from continuous data.

Options

  • :thresholds - number of bins or list of threshold values
  • :domain - [min, max] range to bin

Examples

Visualize.Data.bin([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], thresholds: 5)
# Returns list of bins with :x0, :x1, :values keys

cross(a, b, combine_fn \\ nil)

@spec cross([any()], [any()], (any(), any() -> any()) | nil) :: [any()]

Creates a cross product of two arrays.

Examples

Visualize.Data.cross([1, 2], ["a", "b"])
# => [{1, "a"}, {1, "b"}, {2, "a"}, {2, "b"}]

deviation(data, accessor \\ nil)

@spec deviation([any()], (any() -> number()) | nil) :: float() | nil

Returns the standard deviation.

extent(data, accessor \\ nil)

@spec extent([any()], (any() -> number()) | nil) :: {number(), number()} | nil

Returns {min, max} as a tuple.

Examples

Visualize.Data.extent([3, 1, 4, 1, 5])
# => {1, 5}

Visualize.Data.extent(data, & &1.value)
# => {min_value, max_value}

group(data, key_fn)

@spec group([any()], (any() -> any())) :: map()

Groups data by a key function.

Returns a map where keys are the result of the key function and values are lists of matching elements.

Examples

data = [%{cat: "A", val: 1}, %{cat: "A", val: 2}, %{cat: "B", val: 3}]
Visualize.Data.group(data, & &1.cat)
# => %{"A" => [%{cat: "A", val: 1}, %{cat: "A", val: 2}], "B" => [%{cat: "B", val: 3}]}

lttb(data, n, accessor \\ nil)

@spec lttb([any()], pos_integer(), (any() -> {term(), term()}) | nil) :: [any()]

Largest-Triangle-Three-Buckets: about n of the elements, chosen to keep the series' visual shape (spec/08 §5.2; Steinarsson 2013).

The accessor returns {x, y}; without one the elements are {x, y} tuples. A datum whose x or y is not a number is a gap: the defined data are reduced run by run between the gaps, and each run of gaps keeps its first element in place, so a line over the result still breaks where the input broke. Input of n or fewer elements is returned unchanged; without gaps the result is exactly n elements, the first and the last among them. n must be at least 3.

Examples

iex> Visualize.Data.lttb([{0, 0}, {1, 1}, {2, 0}, {3, 5}, {4, 0}, {5, 1}, {6, 0}], 4)
[{0, 0}, {2, 0}, {3, 5}, {6, 0}]

iex> Visualize.Data.lttb([{0, 0}, {1, 3}, {2, nil}, {3, 4}, {4, 1}], 9)
[{0, 0}, {1, 3}, {2, nil}, {3, 4}, {4, 1}]

m4(data, width, accessor \\ nil)

@spec m4([any()], pos_integer(), (any() -> {term(), term()}) | nil) :: [any()]

M4: per pixel column, the first, last, least-y and greatest-y elements — the elements a line drawn width pixels wide needs to draw the same pixels as all of them (spec/08 §5.2; Jugel et al. 2014).

The columns split the extent of the defined x readings evenly. The accessor, the gaps and the runs are as lttb/3's: each run of gaps keeps its first element in place, and each stretch of one run in one column keeps at most four elements, deduplicated and in input order. width must be at least 1.

Examples

iex> Visualize.Data.m4([{0, 1}, {1, 5}, {2, 0}, {3, 2}, {4, 3}, {5, 9}, {6, 4}, {7, 6}], 2)
[{0, 1}, {1, 5}, {2, 0}, {3, 2}, {4, 3}, {5, 9}, {7, 6}]

iex> Visualize.Data.m4([{0, 1}, {1, 2}, {2, nil}, {3, nil}, {4, 3}], 1)
[{0, 1}, {1, 2}, {2, nil}, {4, 3}]

max(data, accessor \\ nil)

@spec max([any()], (any() -> number()) | nil) :: number() | nil

Returns the maximum value.

mean(data, accessor \\ nil)

@spec mean([any()], (any() -> number()) | nil) :: float() | nil

Returns the arithmetic mean.

median(data, accessor \\ nil)

@spec median([any()], (any() -> number()) | nil) :: number() | nil

Returns the median value.

min(data, accessor \\ nil)

@spec min([any()], (any() -> number()) | nil) :: number() | nil

Returns the minimum value.

Examples

Visualize.Data.min([3, 1, 4, 1, 5])
# => 1

Visualize.Data.min(data, & &1.value)
# => minimum value from data

quantile(data, p, accessor \\ nil)

@spec quantile([any()], float(), (any() -> number()) | nil) :: number() | nil

Returns the p-quantile value.

p should be between 0 and 1.

range(start, stop, step \\ 1)

@spec range(number(), number(), number()) :: [number()]

Generates a range of numbers.

Examples

Visualize.Data.range(0, 5)
# => [0, 1, 2, 3, 4]

Visualize.Data.range(0, 1, 0.2)
# => [0, 0.2, 0.4, 0.6, 0.8]

rollup(data, key_fn, reduce_fn)

@spec rollup([any()], (any() -> any()), ([any()] -> any())) :: map()

Groups data and applies a reducer to each group.

Examples

data = [%{cat: "A", val: 1}, %{cat: "A", val: 2}, %{cat: "B", val: 3}]
Visualize.Data.rollup(data, & &1.cat, &length/1)
# => %{"A" => 2, "B" => 1}

Visualize.Data.rollup(data, & &1.cat, fn items ->
  Visualize.Data.sum(items, & &1.val)
end)
# => %{"A" => 3, "B" => 3}

sort(data, accessor, order \\ :asc)

@spec sort([any()], (any() -> any()), :asc | :desc) :: [any()]

Sorts data by an accessor function.

sum(data, accessor \\ nil)

@spec sum([any()], (any() -> number()) | nil) :: number()

Returns the sum of values.

ticks(start, stop, count)

@spec ticks(number(), number(), integer()) :: [number()]

Generates evenly-spaced tick values for a domain.

Useful for creating axis ticks without a scale.

unique(data, accessor \\ nil)

@spec unique([any()], (any() -> any()) | nil) :: [any()]

Returns unique values by accessor.

variance(data, accessor \\ nil)

@spec variance([any()], (any() -> number()) | nil) :: float() | nil

Returns the variance of values.