Class: OpenTox::Transform::AutoScale

Inherits:
Object
  • Object
show all
Defined in:
lib/transform.rb

Overview

Auto-Scaler for GSL vectors. Center on mean and divide by standard deviation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ AutoScale

Returns a new instance of AutoScale.

Parameters:

  • values (GSL::Vector)

    to transform using AutoScaling.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/transform.rb', line 54

def initialize values
  begin
    raise "Cannot transform, values empty." if values.size==0
    vs = values.clone
    @mean = vs.to_scale.mean
    @stdev = vs.to_scale.standard_deviation_population
    @stdev = 0.0 if @stdev.nan? 
    @vs = transform vs
  rescue Exception => e
    LOGGER.debug "#{e.class}: #{e.message}"
    LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
  end
end

Instance Attribute Details

#meanObject

Returns the value of attribute mean.



51
52
53
# File 'lib/transform.rb', line 51

def mean
  @mean
end

#stdevObject

Returns the value of attribute stdev.



51
52
53
# File 'lib/transform.rb', line 51

def stdev
  @stdev
end

#vsObject

Returns the value of attribute vs.



51
52
53
# File 'lib/transform.rb', line 51

def vs
  @vs
end

Instance Method Details

#autoscale(values) ⇒ GSL::Vector

Returns transformed values.

Parameters:

  • values (GSL::Vector)

    to transform.

Returns:

  • (GSL::Vector)

    transformed values.



95
96
97
98
# File 'lib/transform.rb', line 95

def autoscale values
  vs_ss = values.clone.to_scale - @mean
  @stdev == 0.0 ? vs_ss.to_gsl : ( vs_ss * ( 1 / @stdev) ).to_gsl
end

#restore(values) ⇒ GSL::Vector

Returns transformed values.

Parameters:

  • Values (GSL::Vector)

    to restore.

Returns:

  • (GSL::Vector)

    transformed values.



82
83
84
85
86
87
88
89
90
91
# File 'lib/transform.rb', line 82

def restore values
  begin
    raise "Cannot transform, values empty." if values.size==0
    rv_ss = values.clone.to_scale * @stdev unless @stdev == 0.0
    (rv_ss + @mean).to_gsl
  rescue Exception => e
    LOGGER.debug "#{e.class}: #{e.message}"
    LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
  end
end

#transform(values) ⇒ GSL::Vector

Returns transformed values.

Parameters:

  • values (GSL::Vector)

    to transform.

Returns:

  • (GSL::Vector)

    transformed values.



70
71
72
73
74
75
76
77
78
# File 'lib/transform.rb', line 70

def transform values
  begin
    raise "Cannot transform, values empty." if values.size==0
    autoscale values.clone
  rescue Exception => e
    LOGGER.debug "#{e.class}: #{e.message}"
    LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
  end
end