Module: Breakout

Defined in:
lib/breakout.rb,
lib/breakout/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.detect(series, min_size: 30, method: "multi", alpha: 2, beta: nil, degree: 1, percent: nil, exact: true) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/breakout.rb', line 9

def detect(series, min_size: 30, method: "multi", alpha: 2, beta: nil, degree: 1, percent: nil, exact: true)
  raise ArgumentError, "min_size must be at least 2" if min_size < 2
  raise ArgumentError, "beta and percent cannot be passed together" unless beta.nil? || percent.nil?
  raise ArgumentError, "alpha must be between 0 and 2" if alpha < 0 || alpha > 2
  raise ArgumentError, "degree must be 0, 1, or 2" unless [0, 1, 2].include?(degree)
  raise ArgumentError, "method must be amoc or multi" unless ["amoc", "multi"].include?(method)

  return [] if series.size < min_size

  if series.is_a?(Hash)
    sorted = series.sort_by { |k, _| k }
    z = sorted.map(&:last)
  else
    z = series
  end

  res = _detect(z, min_size, method, alpha, beta, degree, percent, exact)
  res.map! { |i| sorted[i][0] } if series.is_a?(Hash)
  res
end

.plot(series, breakouts) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/breakout.rb', line 30

def plot(series, breakouts)
  require "vega"

  data =
    if series.is_a?(Hash)
      series.map { |k, v| {x: iso8601(k), y: v, breakout: breakouts.include?(k)} }
    else
      series.map.with_index { |v, i| {x: i, y: v, breakout: breakouts.include?(i)} }
    end

  if series.is_a?(Hash)
    x = {field: "x", type: "temporal"}
    x["scale"] = {type: "utc"} if series.keys.first.is_a?(Date)
  else
    x = {field: "x", type: "quantitative"}
  end

  Vega.lite
    .data(data)
    .layer([
      {
        mark: {type: "line"},
        encoding: {
          x: x,
          y: {field: "y", type: "quantitative", scale: {zero: false}},
          color: {value: "#fa9088"}
        }
      },
      {
        transform: [{"filter": "datum.breakout == true"}],
        mark: {type: "rule"},
        encoding: {
          x: x,
          color: {value: "#19c7ca"},
          strokeWidth: {value: 2},
          strokeDash: {value: [6, 6]}
        }
      }
    ])
    .config(axis: {title: nil, labelFontSize: 12})
end