openModeller  Version 1.5.0
ScaleNormalizer.cpp
Go to the documentation of this file.
1 
28 #include <openmodeller/Sampler.hh>
29 #include <openmodeller/Log.hh>
30 
31 using namespace std;
32 
33 /*******************/
34 /*** constructor ***/
35 ScaleNormalizer::ScaleNormalizer( Scalar min, Scalar max, bool useLayerAsRef ) :
36  _min( min ),
37  _max( max ),
38  _use_layer_as_ref( useLayerAsRef ),
39  _offsets(),
40  _scales()
41 {
42 }
43 
44 /*******************/
45 /*** constructor ***/
47  _min( 0.0 ),
48  _max( 1.0 ),
49  _use_layer_as_ref( true ),
50  _offsets(),
51  _scales()
52 {
53 }
54 
55 /******************/
56 /*** destructor ***/
58 
59 /****************/
60 /*** get Copy ***/
62 
63  return new ScaleNormalizer( *this );
64 }
65 
66 /*****************************/
67 /*** compute Normalization ***/
69 
70  int dim = samplerPtr->numIndependent();
71  Sample smin(dim), smax(dim);
72 
73  bool get_minmax_from_sampler = true;
74 
75  if ( _use_layer_as_ref ) {
76 
77  Log::instance()->debug( "Using min/max from layer as reference for normalization.\n");
78 
79  EnvironmentPtr envPtr = samplerPtr->getEnvironment();
80 
81  if ( envPtr ) {
82 
83  envPtr->getMinMax( &smin, &smax );
84 
85  get_minmax_from_sampler = false;
86  }
87  else {
88 
89  Log::instance()->warn( "Sampler has no environment. ScaleNormalizaer will get min/max directly from samples instead.\n");
90  }
91  }
92  else {
93 
94  Log::instance()->debug( "Using min/max from input samples as reference for normalization.\n");
95  }
96 
97  if ( get_minmax_from_sampler ) {
98 
99  samplerPtr->getMinMax( &smin, &smax );
100  }
101 
102  _scales.resize(dim);
103  _offsets.resize(dim);
104 
105  for ( int i = 0; i < dim; ++i ) {
106 
107  if ( smax[i] == smin[i] ) {
108 
109  Log::instance()->warn( "Min/max values for variable %d are the same during normalization!\n", (i+1));
110 
111  // Avoid zero division
112  _scales[i] = 1.0;
113  }
114  else {
115 
116  _scales[i] = (_max - _min) / (smax[i] - smin[i]);
117  }
118 
119  _offsets[i] = _min - _scales[i] * smin[i];
120  }
121 }
122 
123 /*****************/
124 /*** normalize ***/
125 void ScaleNormalizer::normalize( Sample * samplePtr ) {
126 
127  if ( samplePtr->size() != 0 ) {
128 
129  *samplePtr *= _scales;
130  *samplePtr += _offsets;
131  }
132 }
133 
134 /*************************/
135 /*** get configuration ***/
137 
138  ConfigurationPtr config( new ConfigurationImpl("Normalization") );
139 
140  config->addNameValue( "Class", "ScaleNormalizer" );
141 
142  int temp_val = (_use_layer_as_ref) ? 1 : 0;
143 
144  config->addNameValue( "UseLayerAsRef", temp_val );
145 
146  config->addNameValue( "Min", _min );
147  config->addNameValue( "Max", _max );
148 
149  config->addNameValue( "Offsets", _offsets );
150  config->addNameValue( "Scales", _scales );
151 
152  return config;
153 }
154 
155 /*************************/
156 /*** set configuration ***/
158 
159  int temp_val = config->getAttributeAsInt( "UseLayerAsRef", 1 );
160 
161  _use_layer_as_ref = (temp_val == 1) ? true : false;
162 
163  _min = config->getAttributeAsDouble( "Min", 0.0 );
164  _max = config->getAttributeAsDouble( "Max", 1.0 );
165 
166  _offsets = config->getAttributeAsSample( "Offsets" );
167  _scales = config->getAttributeAsSample( "Scales" );
168 }
void warn(const char *format,...)
'Warn' level.
Definition: Log.cpp:273
void setConfiguration(const ConstConfigurationPtr &)
void computeNormalization(const ReferenceCountedPointer< const SamplerImpl > &samplerPtr)
double Scalar
Type of map values.
Definition: om_defs.hh:39
Normalizer * getCopy()
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
void normalize(Sample *samplePtr)
ConfigurationPtr getConfiguration() const
void resize(std::size_t size)
Definition: Sample.cpp:153
std::size_t size() const
Definition: Sample.hh:70
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237
int min(int v1, int v2)
Definition: rules_base.cpp:56
Definition: Sample.hh:25