Commit 92ae618c authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

Merge branch '2D_heatmap_control' into 'master'

Adding 2D heatmap for batch processing

See merge request grp-fidentis/analyst2!396
parents a45e0d39 db6fc748
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
package cz.fidentis.analyst.engines.face.batch.clustering;

import cz.fidentis.analyst.engines.face.batch.clustering.dto.ClusterNode;

import java.util.List;

/**
 * Class that provides cluster node
 *
 * @author Sabrina Oralkova
 */
public class ClusterNodeProvider {

    /**
     * Provides list of sorted leaf names od the root cluster node
     *
     * @param clusterer chosen clusterer
     * @return list of sorted leaf names
     */
    public List<String> getClusterNodeNames(Clusterer clusterer) {
        ClusterNode rootNode = clusterer.performClustering();
        return rootNode.getLeafNames();
    }
}
+28 −0
Original line number Diff line number Diff line
package cz.fidentis.analyst.engines.face.batch.clustering;

import cz.fidentis.analyst.engines.face.batch.clustering.impl.AverageLinkageStrategy;
import cz.fidentis.analyst.engines.face.batch.clustering.impl.CompleteLinkageStrategy;
import cz.fidentis.analyst.engines.face.batch.clustering.impl.SingleLinkageStrategy;

/**
 * Class that provides linkage strategy chosen on the input
 *
 * @author Sabrina Oralkova
 */
public class LinkageStrategyProvider {

    /**
     * Provides linkage strategy chosen on the input
     *
     * @param clusteringStrategy chosen strategy on the UI
     * @return chosen linkage strategy
     */
    public static LinkageStrategy getLinkageStrategy(String clusteringStrategy) {
        return switch (clusteringStrategy) {
            case "Agglomerative - complete strategy" -> new CompleteLinkageStrategy();
            case "Agglomerative - average strategy" -> new AverageLinkageStrategy();
            case "Agglomerative - single strategy" -> new SingleLinkageStrategy();
            default ->  null;
        };
    }
}
+6 −3
Original line number Diff line number Diff line
@@ -24,6 +24,11 @@ public class DivisiveClustererImpl extends AbstractClusterer implements Clustere

    @Override
    public ClusterNode performClustering() {
        if (getClusterNames().size() == 1 && getDistances().length == 1) {
            ClusterNode rootNode = new ClusterNode(getClusterNames().getFirst());
            rootNode.addLeafName(getClusterNames().getFirst());
            return rootNode;
        }
        ClusterNode rootCluster = createInitialCluster();
        List<ClusterNode> individualClusters = createSingleClusters(this.getClusterNames(), rootCluster);
        ClusterDistanceMap linkages = new ClusterDistanceMap(this.getDistances(), individualClusters);
@@ -33,9 +38,7 @@ public class DivisiveClustererImpl extends AbstractClusterer implements Clustere
    }

    private ClusterNode createInitialCluster() {
        ClusterNode rootCluster = new ClusterNode("cluster#0");
        this.getClusterNames().forEach(rootCluster::addLeafName);
        return rootCluster;
        return new ClusterNode("cluster#0");
    }

    /**
+2 −4
Original line number Diff line number Diff line
@@ -46,7 +46,6 @@ public class DivisiveHierarchyBuilder {
        ClusterNode leftCluster = new ClusterNode(getClusterName());
        ClusterNode rightCluster = new ClusterNode(getClusterName());

        leftCluster.appendLeafNames(clusterNode.getLeafNames());
        for (ClusterNode node : clusterNode.getChildren()) {
            leftCluster.addChild(node);
        }
@@ -61,6 +60,8 @@ public class DivisiveHierarchyBuilder {
        fixRelations(clusterNode, leftCluster, rightCluster);
        divideClusters(leftCluster);
        divideClusters(rightCluster);
        clusterNode.appendLeafNames(clusterNode.getChildren().getFirst().getLeafNames()); //////
        clusterNode.appendLeafNames(clusterNode.getChildren().getLast().getLeafNames());  //////
    }

    private void removeNodeFromTree(ClusterNode clusterNode) {
@@ -99,10 +100,7 @@ public class DivisiveHierarchyBuilder {
        }

        rightCluster.addChild(furthestNode);
        rightCluster.addLeafName(furthestNode.getName());

        leftCluster.removeChild(furthestNode);
        leftCluster.removeLeafName(furthestNode.getName());
    }

    private ClusterNode getFurthestNode(ClusterNode leftCluster, ClusterNode rightCluster) {
+70 −0
Original line number Diff line number Diff line
package cz.fidentis.analyst.gui.elements.colorscalepanel;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

/**
 * @author Sabrina Oralkova
 */
public class ColorScalePanel extends JPanel {

    private final BufferedImage colorScaleImage;

    /**
     * Constructor
     *
     * @param width wanted width of the panel
     * @param height wanted height of the panel
     */
    public ColorScalePanel(int width, int height) {
        colorScaleImage = createColorScaleImage(width, height);
        setPreferredSize(new Dimension(width, height));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(colorScaleImage, 0, 0, this);
    }

    /**
     * Creates an image of descending color
     * @param width  wanted width of the panel
     * @param height wanted height of the panel
     * @return an image of color scale panel
     */
    public BufferedImage createColorScaleImage(int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        WritableRaster raster = image.getRaster();

        for (int pixelRow = 0; pixelRow < height; pixelRow++) {
            double value = (double) pixelRow / height;
            Color color = getColorForValue(value);

            for (int pixelColumn = 0; pixelColumn < width; pixelColumn++) {
                setColorInRaster(raster, pixelColumn, pixelRow, color);
            }
        }

        return image;
    }

    private void setColorInRaster(WritableRaster raster, int x, int y, Color color) {
        int[] rgb = { color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() };
        raster.setPixel(x, y, rgb);
    }

    private Color getColorForValue(double value) {
        float progress = (float) value;

        // This is a calculation of continuous scale for range of blue colors from dark and saturated to bright and light
        // Calculates saturation from 0.1 up to 1.0
        float saturation = 0.1f + progress * 0.9f;
        // Calculates brightness from 1.0 down to 0.5
        float brightness = 1.0f - progress * 0.5f;

        return Color.getHSBColor(0.61f, saturation, brightness);
    }
}
Loading