use of com.simiacryptus.util.data.DoubleStatistics in project MindsEye by SimiaCryptus.
the class EncodingUtil method decompositionSvg.
/**
* To svg string.
*
* @param log the log
* @param baseline the baseline
* @param signedComponents the signed components
* @return the string
*/
public static String decompositionSvg(@Nonnull final NotebookOutput log, @Nonnull final Tensor baseline, @Nonnull final List<Tensor> signedComponents) {
final List<DoubleStatistics> componentStats = signedComponents.stream().map(t -> new DoubleStatistics().accept(t.getData())).collect(Collectors.toList());
@Nonnull final CharSequence positiveFilter = IntStream.range(0, signedComponents.size()).mapToObj(i -> {
String name;
try {
name = String.format("component_%s.png", EncodingUtil.imageNumber++);
ImageIO.write(signedComponents.get(i).map(v -> v > 0 ? v * (0xFF / componentStats.get(i).getMax()) : 0).toImage(), "png", log.file(name));
} catch (@Nonnull final IOException e) {
throw new RuntimeException(e);
}
return String.format(" <feImage xlink:href=\"%s\" result=\"pos_image_%s\" />\n", name, i);
}).reduce((a, b) -> a + "\n" + b).get();
@Nonnull final CharSequence negativeFilter = IntStream.range(0, signedComponents.size()).mapToObj(i -> {
String name;
try {
name = String.format("component_%s.png", EncodingUtil.imageNumber++);
ImageIO.write(signedComponents.get(i).map(v -> v < 0 ? 0xFF - v * (0xFF / componentStats.get(i).getMin()) : 0).toImage(), "png", log.file(name));
} catch (@Nonnull final IOException e) {
throw new RuntimeException(e);
}
return String.format(" <feImage xlink:href=\"%s\" result=\"neg_image_%s\" />\n", name, i);
}).reduce((a, b) -> a + "\n" + b).get();
@Nonnull final CharSequence compositingFilters = IntStream.range(0, signedComponents.size()).mapToObj(i -> {
final double fPos = componentStats.get(i).getMax() / 0xFF;
final double fNeg = componentStats.get(i).getMin() / 0xFF;
return " <feComposite in=\"" + (i == 0 ? "FillPaint" : "lastResult") + "\" in2=\"neg_image_" + i + "\" result=\"lastResult\" operator=\"arithmetic\" k1=\"0.0\" k2=\"1.0\" k3=\"" + -fNeg + "\" k4=\"" + fNeg + "\"/>\n" + " <feComposite in=\"lastResult\" in2=\"pos_image_" + i + "\" result=\"lastResult\" operator=\"arithmetic\" k1=\"0.0\" k2=\"1.0\" k3=\"" + fPos + "\" k4=\"" + 0.0 + "\"/>\n";
}).reduce((a, b) -> a + "\n" + b).get();
final int red = (int) baseline.get(0, 0, 0);
final int green = (int) baseline.get(0, 0, 1);
final int blue = (int) baseline.get(0, 0, 2);
@Nonnull final CharSequence avgHexColor = Long.toHexString(red + (green << 8) + (blue << 16));
return "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n" + ("<defs>\n" + "<filter id=\"image\" >\n" + (positiveFilter + "\n" + negativeFilter + "\n" + compositingFilters).replaceAll("\n", "\n\t") + "\n" + "</filter>\n" + "</defs>\n" + "<rect style=\"filter:url(#image);\" setByCoord=\"#" + avgHexColor + "\" width=\"256\" height=\"256\"/>").replaceAll("\n", "\n\t") + "\n</svg>";
}
use of com.simiacryptus.util.data.DoubleStatistics in project MindsEye by SimiaCryptus.
the class ArtistryUtil method paint_Circles.
/**
* Paint circles.
*
* @param canvas the canvas
* @param scale the scale
*/
public static void paint_Circles(final Tensor canvas, final int scale) {
BufferedImage originalImage = canvas.toImage();
BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D) newImage.getGraphics();
IntStream.range(0, 10000).forEach(i -> {
Random random = new Random();
int positionX = random.nextInt(originalImage.getWidth());
int positionY = random.nextInt(originalImage.getHeight());
int width = 1 + random.nextInt(2 * scale);
int height = 1 + random.nextInt(2 * scale);
DoubleStatistics[] stats = { new DoubleStatistics(), new DoubleStatistics(), new DoubleStatistics() };
canvas.coordStream(false).filter(c -> {
int[] coords = c.getCoords();
int x = coords[0];
int y = coords[1];
double relX = Math.pow(1 - 2 * ((double) (x - positionX) / width), 2);
double relY = Math.pow(1 - 2 * ((double) (y - positionY) / height), 2);
return relX + relY < 1.0;
}).forEach(c -> stats[c.getCoords()[2]].accept(canvas.get(c)));
graphics.setStroke(new Stroke() {
@Override
public Shape createStrokedShape(final Shape p) {
return null;
}
});
graphics.setColor(new Color((int) stats[0].getAverage(), (int) stats[1].getAverage(), (int) stats[2].getAverage()));
graphics.fillOval(positionX, positionY, width, height);
});
canvas.set(Tensor.fromRGB(newImage));
}
use of com.simiacryptus.util.data.DoubleStatistics in project MindsEye by SimiaCryptus.
the class TestUtil method renderToImages.
/**
* Render to images stream.
*
* @param tensor the tensor
* @param normalize the normalize
* @return the stream
*/
public static Stream<BufferedImage> renderToImages(@Nonnull final Tensor tensor, final boolean normalize) {
final DoubleStatistics[] statistics = IntStream.range(0, tensor.getDimensions()[2]).mapToObj(band -> {
return new DoubleStatistics().accept(tensor.coordStream(false).filter(x -> x.getCoords()[2] == band).mapToDouble(c -> tensor.get(c)).toArray());
}).toArray(i -> new DoubleStatistics[i]);
@Nonnull final BiFunction<Double, DoubleStatistics, Double> transform = (value, stats) -> {
final double width = Math.sqrt(2) * stats.getStandardDeviation();
final double centered = value - stats.getAverage();
final double distance = Math.abs(value - stats.getAverage());
final double positiveMax = stats.getMax() - stats.getAverage();
final double negativeMax = stats.getAverage() - stats.getMin();
final double unitValue;
if (value < centered) {
if (distance > width) {
unitValue = 0.25 - 0.25 * ((distance - width) / (negativeMax - width));
} else {
unitValue = 0.5 - 0.25 * (distance / width);
}
} else {
if (distance > width) {
unitValue = 0.75 + 0.25 * ((distance - width) / (positiveMax - width));
} else {
unitValue = 0.5 + 0.25 * (distance / width);
}
}
return 0xFF * unitValue;
};
tensor.coordStream(true).collect(Collectors.groupingBy(x -> x.getCoords()[2], Collectors.toList()));
@Nullable final Tensor normal = tensor.mapCoords((c) -> transform.apply(tensor.get(c), statistics[c.getCoords()[2]])).map(v -> Math.min(0xFF, Math.max(0, v)));
return (normalize ? normal : tensor).toImages().stream();
}
use of com.simiacryptus.util.data.DoubleStatistics in project MindsEye by SimiaCryptus.
the class TestUtil method normalizeBands.
/**
* Normalize bands tensor.
*
* @param image the image
* @param max the max
* @return the tensor
*/
public static Tensor normalizeBands(final Tensor image, final int max) {
DoubleStatistics[] statistics = IntStream.range(0, image.getDimensions()[2]).mapToObj(i -> new DoubleStatistics()).toArray(i -> new DoubleStatistics[i]);
image.coordStream(false).forEach(c -> {
double value = image.get(c);
statistics[c.getCoords()[2]].accept(value);
});
return image.mapCoords(c -> {
double value = image.get(c);
DoubleStatistics statistic = statistics[c.getCoords()[2]];
return max * (value - statistic.getMin()) / (statistic.getMax() - statistic.getMin());
});
}
use of com.simiacryptus.util.data.DoubleStatistics in project MindsEye by SimiaCryptus.
the class QuantifyOrientationWrapper method orient.
@Override
public LineSearchCursor orient(final Trainable subject, final PointSample measurement, @Nonnull final TrainingMonitor monitor) {
final LineSearchCursor cursor = inner.orient(subject, measurement, monitor);
if (cursor instanceof SimpleLineSearchCursor) {
final DeltaSet<Layer> direction = ((SimpleLineSearchCursor) cursor).direction;
@Nonnull final StateSet<Layer> weights = ((SimpleLineSearchCursor) cursor).origin.weights;
final Map<CharSequence, CharSequence> dataMap = weights.stream().collect(Collectors.groupingBy(x -> getId(x), Collectors.toList())).entrySet().stream().collect(Collectors.toMap(x -> x.getKey(), list -> {
final List<Double> doubleList = list.getValue().stream().map(weightDelta -> {
final DoubleBuffer<Layer> dirDelta = direction.getMap().get(weightDelta.layer);
final double denominator = weightDelta.deltaStatistics().rms();
final double numerator = null == dirDelta ? 0 : dirDelta.deltaStatistics().rms();
return numerator / (0 == denominator ? 1 : denominator);
}).collect(Collectors.toList());
if (1 == doubleList.size())
return Double.toString(doubleList.get(0));
return new DoubleStatistics().accept(doubleList.stream().mapToDouble(x -> x).toArray()).toString();
}));
monitor.log(String.format("Line search stats: %s", dataMap));
} else {
monitor.log(String.format("Non-simple cursor: %s", cursor));
}
return cursor;
}
Aggregations