use of de.lmu.ifi.dbs.elki.math.DoubleMinMax in project elki by elki-project.
the class AttributeWiseMinMaxNormalizationTest method testNaNParameters.
/**
* Test with default parameters and for correcting handling of NaN and Inf.
*/
@Test
public void testNaNParameters() {
String filename = UNITTEST + "nan-test-1.csv";
AttributeWiseMinMaxNormalization<DoubleVector> filter = new ELKIBuilder<AttributeWiseMinMaxNormalization<DoubleVector>>(AttributeWiseMinMaxNormalization.class).build();
MultipleObjectsBundle bundle = readBundle(filename, filter);
// Ensure the first column are the vectors.
assertTrue("Test file not as expected", TypeUtil.NUMBER_VECTOR_FIELD.isAssignableFromType(bundle.meta(0)));
// This cast is now safe (vector field):
int dim = ((FieldTypeInformation) bundle.meta(0)).getDimensionality();
// We verify that minimum and maximum values in each column are 0 and 1:
DoubleMinMax[] mms = DoubleMinMax.newArray(dim);
for (int row = 0; row < bundle.dataLength(); row++) {
DoubleVector d = get(bundle, row, 0, DoubleVector.class);
for (int col = 0; col < dim; col++) {
final double val = d.doubleValue(col);
if (val > Double.NEGATIVE_INFINITY && val < Double.POSITIVE_INFINITY) {
mms[col].put(val);
}
}
}
for (int col = 0; col < dim; col++) {
assertEquals("Minimum not as expected", 0., mms[col].getMin(), 0.);
assertEquals("Maximum not as expected", 1., mms[col].getMax(), 0.);
}
}
use of de.lmu.ifi.dbs.elki.math.DoubleMinMax in project elki by elki-project.
the class AffineProjection method estimateViewport.
@Override
public CanvasSize estimateViewport() {
if (viewport == null) {
final int dim = proj.getDimensionality();
DoubleMinMax minmaxx = new DoubleMinMax();
DoubleMinMax minmaxy = new DoubleMinMax();
// Origin
final double[] vec = new double[dim];
double[] orig = projectScaledToRender(vec);
minmaxx.put(orig[0]);
minmaxy.put(orig[1]);
// Diagonal point
Arrays.fill(vec, 1.);
double[] diag = projectScaledToRender(vec);
minmaxx.put(diag[0]);
minmaxy.put(diag[1]);
// Axis end points
for (int d = 0; d < dim; d++) {
Arrays.fill(vec, 0.);
vec[d] = 1.;
double[] ax = projectScaledToRender(vec);
minmaxx.put(ax[0]);
minmaxy.put(ax[1]);
}
viewport = new CanvasSize(minmaxx.getMin(), minmaxx.getMax(), minmaxy.getMin(), minmaxy.getMax());
}
return viewport;
}
use of de.lmu.ifi.dbs.elki.math.DoubleMinMax in project elki by elki-project.
the class HistogramVisualization method makeVisualization.
@Override
public Visualization makeVisualization(VisualizerContext context, VisualizationTask task, VisualizationPlot plot, double width, double height, Projection proj) {
HistogramResult curve = task.getResult();
final StyleLibrary style = context.getStyleLibrary();
final double sizex = StyleLibrary.SCALE;
final double sizey = StyleLibrary.SCALE * height / width;
final double margin = style.getSize(StyleLibrary.MARGIN);
Element layer = SVGUtil.svgElement(plot.getDocument(), SVGConstants.SVG_G_TAG);
final String transform = SVGUtil.makeMarginTransform(width, height, sizex, sizey, margin);
SVGUtil.setAtt(layer, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, transform);
// find maximum, determine step size
int dim = -1;
DoubleMinMax xminmax = new DoubleMinMax();
DoubleMinMax yminmax = new DoubleMinMax();
for (double[] point : curve) {
xminmax.put(point[0]);
dim = dim < point.length ? point.length : dim;
for (int i = 1; i < point.length; i++) {
yminmax.put(point[i]);
}
}
// Minimum should always start at 0 for histograms
yminmax.put(0.0);
// remove one dimension which are the x values.
dim = dim - 1;
int size = curve.size();
double range = xminmax.getMax() - xminmax.getMin();
double binwidth = range / (size - 1);
LinearScale xscale = new LinearScale(xminmax.getMin() - binwidth * .49999, xminmax.getMax() + binwidth * .49999);
LinearScale yscale = new LinearScale(yminmax.getMin(), yminmax.getMax());
SVGPath[] path = new SVGPath[dim];
for (int i = 0; i < dim; i++) {
path[i] = new SVGPath(sizex * xscale.getScaled(xminmax.getMin() - binwidth * .5), sizey);
}
// draw curves.
for (double[] point : curve) {
for (int d = 0; d < dim; d++) {
path[d].lineTo(sizex * (xscale.getScaled(point[0] - binwidth * .5)), sizey * (1 - yscale.getScaled(point[d + 1])));
path[d].lineTo(sizex * (xscale.getScaled(point[0] + binwidth * .5)), sizey * (1 - yscale.getScaled(point[d + 1])));
}
}
// close all histograms
for (int i = 0; i < dim; i++) {
path[i].lineTo(sizex * xscale.getScaled(xminmax.getMax() + binwidth * .5), sizey);
}
// add axes
try {
SVGSimpleLinearAxis.drawAxis(plot, layer, yscale, 0, sizey, 0, 0, SVGSimpleLinearAxis.LabelStyle.LEFTHAND, style);
SVGSimpleLinearAxis.drawAxis(plot, layer, xscale, 0, sizey, sizex, sizey, SVGSimpleLinearAxis.LabelStyle.RIGHTHAND, style);
} catch (CSSNamingConflict e) {
LoggingUtil.exception(e);
}
// Setup line styles and insert lines.
ColorLibrary cl = style.getColorSet(StyleLibrary.PLOT);
for (int d = 0; d < dim; d++) {
CSSClass csscls = new CSSClass(this, SERIESID + "_" + d);
csscls.setStatement(SVGConstants.SVG_FILL_ATTRIBUTE, SVGConstants.SVG_NONE_VALUE);
csscls.setStatement(SVGConstants.SVG_STROKE_ATTRIBUTE, cl.getColor(d));
csscls.setStatement(SVGConstants.SVG_STROKE_WIDTH_ATTRIBUTE, style.getLineWidth(StyleLibrary.PLOT));
plot.addCSSClassOrLogError(csscls);
Element line = path[d].makeElement(plot);
line.setAttribute(SVGConstants.SVG_CLASS_ATTRIBUTE, csscls.getName());
layer.appendChild(line);
}
return new StaticVisualizationInstance(context, task, plot, width, height, layer);
}
use of de.lmu.ifi.dbs.elki.math.DoubleMinMax in project elki by elki-project.
the class UniformMinMaxEstimator method estimate.
@Override
public <A> UniformDistribution estimate(A data, NumberArrayAdapter<?, A> adapter) {
final int len = adapter.size(data);
DoubleMinMax mm = new DoubleMinMax();
for (int i = 0; i < len; i++) {
final double val = adapter.getDouble(data, i);
if (val > Double.NEGATIVE_INFINITY && val < Double.POSITIVE_INFINITY) {
mm.put(val);
}
}
return estimate(mm);
}
use of de.lmu.ifi.dbs.elki.math.DoubleMinMax in project elki by elki-project.
the class Scales method calcScales.
/**
* Compute a linear scale for each dimension.
*
* @param rel Relation
* @return Scales, indexed starting with 0 (like Vector, not database
* objects!)
*/
public static LinearScale[] calcScales(Relation<? extends SpatialComparable> rel) {
int dim = RelationUtil.dimensionality(rel);
DoubleMinMax[] minmax = DoubleMinMax.newArray(dim);
LinearScale[] scales = new LinearScale[dim];
// analyze data
for (DBIDIter iditer = rel.iterDBIDs(); iditer.valid(); iditer.advance()) {
SpatialComparable v = rel.get(iditer);
if (v instanceof NumberVector) {
for (int d = 0; d < dim; d++) {
final double mi = v.getMin(d);
if (mi != mi) {
// NaN
continue;
}
minmax[d].put(mi);
}
} else {
for (int d = 0; d < dim; d++) {
final double mi = v.getMin(d);
if (mi == mi) {
// No NaN
minmax[d].put(mi);
}
final double ma = v.getMax(d);
if (ma == ma) {
// No NaN
minmax[d].put(ma);
}
}
}
}
// generate scales
for (int d = 0; d < dim; d++) {
scales[d] = new LinearScale(minmax[d].getMin(), minmax[d].getMax());
}
return scales;
}
Aggregations