/*
* Debellor
*
* Copyright (C) 2008-2009 by Marcin Wojnarski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package org.debellor.base;
import org.debellor.core.Cell;
import org.debellor.core.DataObject;
import org.debellor.core.Sample;
import org.debellor.core.Sample.SampleType;
import org.debellor.core.data.DataVector;
import org.debellor.core.data.NumericFeature;
import org.debellor.core.data.DataVector.DataVectorType;
import org.debellor.core.exception.data.DataCastException;
/**
* Introduces random distortions to numeric features of samples.
* Each numeric feature of the data vector is disturbed independently
* by adding random noise drawn from uniform distribution
* on the interval <code>[-R,R]</code>,
* where <code>R</code> is the value of the parameter "range" passed to the cell.
* This parameter is obligatory and must have a positive real value.
*
* <p>Features other than numeric are left untouched,
* as well as the decision associated with the sample.
*
* @author Marcin Wojnarski
*
*/
public class Distortion extends Cell {
private DataObject[] items;
private double scale;
private Stream input;
public Distortion() {
super(false);
}
@Override
protected SampleType onOpen() throws Exception {
input = openInputStream();
SampleType type = input.sampleType;
scale = 2.0 * parameters.getAsDouble("range");
DataVectorType data = (DataVectorType) type.data;
items = new DataObject[data.size()];
return type;
}
@Override
protected Sample onNext() throws Exception {
Sample s = input.next();
if(s == null) return null;
DataVector inData = (DataVector) s.data;
if(inData.size() != items.length) throw new Exception();
for(int i = 0; i < items.length; i++) {
items[i] = inData.get(i);
try {
NumericFeature inItem = items[i].asNumericFeature();
double d = (random.nextDouble() - 0.5) * scale;
items[i] = new NumericFeature(inItem.value + d);
}
catch(DataCastException e) {} // if the feature is not numeric, do nothing
}
DataVector data = new DataVector(items);
return s.setData(data);
}
@Override
protected void onClose() throws Exception {
input.close();
items = null;
}
}