Class: RSpec::FlakeClassifier::Predictor

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/flake/classifier/predictor.rb,
sig/rspec/flake/classifier.rbs

Defined Under Namespace

Classes: Trainer

Constant Summary collapse

WEIGHTS =
{
  "duration" => 0.15,
  "uses_network" => 0.2,
  "uses_filesystem" => 0.08,
  "uses_time" => 0.15,
  "uses_randomness" => 0.15,
  "uses_concurrency" => 0.17,
  "uses_async_wait" => 0.2
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(weights: WEIGHTS) ⇒ Predictor

Returns a new instance of Predictor.

Parameters:

  • weights: (Hash[String | Symbol, untyped]) (defaults to: WEIGHTS)


29
30
31
# File 'lib/rspec/flake/classifier/predictor.rb', line 29

def initialize(weights: WEIGHTS)
  @weights = normalize_weights(weights)
end

Instance Attribute Details

#weightsHash[String, Float] (readonly)

Returns the value of attribute weights.

Returns:

  • (Hash[String, Float])


18
19
20
# File 'lib/rspec/flake/classifier/predictor.rb', line 18

def weights
  @weights
end

Class Method Details

.load(path) ⇒ Predictor

Parameters:

  • path (String)

Returns:



24
25
26
27
# File 'lib/rspec/flake/classifier/predictor.rb', line 24

def self.load(path)
  payload = JSON.parse(File.read(path))
  new(weights: payload.fetch("weights", payload))
end

.train(examples) ⇒ Predictor

Parameters:

  • examples (Array[Hash[String | Symbol, untyped]])

Returns:



20
21
22
# File 'lib/rspec/flake/classifier/predictor.rb', line 20

def self.train(examples)
  new(weights: Trainer.new(Array(examples)).weights)
end

Instance Method Details

#rank(feature_sets) ⇒ Array[Hash[String, untyped]]

Parameters:

  • feature_sets (Array[Hash[String | Symbol, untyped]])

Returns:

  • (Array[Hash[String, untyped]])


40
41
42
43
44
# File 'lib/rspec/flake/classifier/predictor.rb', line 40

def rank(feature_sets)
  Array(feature_sets).map do |features|
    features.merge("flake_score" => score(features), "priority" => priority(score(features)))
  end.sort_by { |features| -features.fetch("flake_score") }
end

#score(features) ⇒ Float

Parameters:

  • features (Hash[String | Symbol, untyped])

Returns:

  • (Float)


33
34
35
36
37
38
# File 'lib/rspec/flake/classifier/predictor.rb', line 33

def score(features)
  score = weights.sum do |key, weight|
    feature_score(features, key) * weight
  end
  [[score, 0.0].max, 1.0].min.round(4)
end

#to_hHash[String, Hash[String, Float]]

Returns:

  • (Hash[String, Hash[String, Float]])


46
47
48
# File 'lib/rspec/flake/classifier/predictor.rb', line 46

def to_h
  { "weights" => weights }
end