diff --git a/src/main/java/org/embl/mobie/lib/table/DistanceComputer.java b/src/main/java/org/embl/mobie/lib/table/DistanceComputer.java index 367ec342f..55e610ec5 100644 --- a/src/main/java/org/embl/mobie/lib/table/DistanceComputer.java +++ b/src/main/java/org/embl/mobie/lib/table/DistanceComputer.java @@ -16,25 +16,25 @@ public class DistanceComputer { - enum Average + enum AverageMethod { Mean, Median; public static String[] asArray() { - return Arrays.stream(Average.values()).map(Enum::name).toArray(String[]::new); + return Arrays.stream( AverageMethod.values()).map(Enum::name).toArray(String[]::new); } - } - enum Metric { + enum DistanceMetric + { Euclidian, Cosine; public static String[] asArray() { - return Arrays.stream(Metric.values()).map(Enum::name).toArray(String[]::new); + return Arrays.stream( DistanceMetric.values()).map(Enum::name).toArray(String[]::new); } } @@ -51,8 +51,8 @@ public static < A extends Annotation > void showUI( AnnotationTableModel< A > ta // final GenericDialog gd = new GenericDialog( "" ); gd.addStringField( "Distance Columns RegEx", "anchor_.*" ); - gd.addChoice( "Distance Metric", Metric.asArray(), Metric.Euclidian.toString() ); - gd.addChoice( "Averaging Method", Average.asArray(), Average.Median.toString() ); + gd.addChoice( "Distance Metric", DistanceMetric.asArray(), DistanceMetric.Euclidian.toString() ); + gd.addChoice( "Averaging Method", AverageMethod.asArray(), AverageMethod.Median.toString() ); gd.addStringField( "Results Column Name", "distance" ); gd.addCheckbox( "Color by Results", true ); gd.showDialog(); @@ -61,8 +61,8 @@ public static < A extends Annotation > void showUI( AnnotationTableModel< A > ta // parse user input // final String columnNamesRegEx = gd.getNextString(); - Metric metric = Metric.valueOf( gd.getNextChoice() ); - Average average = Average.valueOf( gd.getNextChoice() ); + DistanceMetric distanceMetric = DistanceMetric.valueOf( gd.getNextChoice() ); + AverageMethod averageMethod = AverageMethod.valueOf( gd.getNextChoice() ); final String resultColumnName = gd.getNextString(); boolean colorByDistances = gd.getNextBoolean(); @@ -75,12 +75,11 @@ public static < A extends Annotation > void showUI( AnnotationTableModel< A > ta return; } - // compute + // compute distances // - Map< String, Double > referenceValues = computeReferenceValues( average, distanceFeatures, referenceRows ); - - computeDistancesAndAddToTable( tableModel, resultColumnName, metric, distanceFeatures, referenceValues ); + Map< String, Double > referenceValues = computeReferenceValues( averageMethod, distanceFeatures, referenceRows ); + computeDistancesAndAddToTable( tableModel, resultColumnName, distanceMetric, distanceFeatures, referenceValues ); // visualise // @@ -99,8 +98,7 @@ public static < A extends Annotation > void showUI( AnnotationTableModel< A > ta } - // TODO: multi-thread the computations if they become too slow - private static < A extends Annotation > void computeDistancesAndAddToTable( AnnotationTableModel< A > tableModel, String resultColumnName, Metric metric, List< String > selectedColumnNames, Map< String, Double > columnAverages ) + private static < A extends Annotation > void computeDistancesAndAddToTable( AnnotationTableModel< A > tableModel, String resultColumnName, DistanceMetric distanceMetric, List< String > selectedColumnNames, Map< String, Double > columnAverages ) { if ( tableModel.columnNames().contains( resultColumnName ) ) { @@ -113,7 +111,7 @@ private static < A extends Annotation > void computeDistancesAndAddToTable( Anno } long start = System.currentTimeMillis(); - switch ( metric ) + switch ( distanceMetric ) { case Euclidian: computeEuclidanDistances( tableModel, selectedColumnNames, columnAverages, resultColumnName ); @@ -123,26 +121,26 @@ private static < A extends Annotation > void computeDistancesAndAddToTable( Anno computeCosineDistances( tableModel, selectedColumnNames, columnAverages, resultColumnName ); break; } - IJ.log( "Computed the " + metric + " distance of " + selectedColumnNames.size() + IJ.log( "Computed the " + distanceMetric + " distance of " + selectedColumnNames.size() + " features for " + tableModel.annotations().size() + " annotations in " + ( System.currentTimeMillis() - start ) + " ms."); } @NotNull - private static < A extends Annotation > Map< String, Double > computeReferenceValues( Average average, List< String > selectedColumnNames, Set< A > selectedAnnotations ) + private static < A extends Annotation > Map< String, Double > computeReferenceValues( AverageMethod averageMethod, List< String > selectedColumnNames, Set< A > selectedAnnotations ) { - switch ( average ) + switch ( averageMethod ) { case Mean: - return averageSelectedAnnotations( selectedColumnNames, selectedAnnotations ); + return computeMeanOfSelectedAnnotations( selectedColumnNames, selectedAnnotations ); case Median: default: - return medianSelectedAnnotations( selectedColumnNames, selectedAnnotations ); + return computeMedianOfSelectedAnnotations( selectedColumnNames, selectedAnnotations ); } } @NotNull - private static < A extends Annotation > Map< String, Double > averageSelectedAnnotations( List< String > selectedColumnNames, Set< A > selectedAnnotations ) + private static < A extends Annotation > Map< String, Double > computeMeanOfSelectedAnnotations( List< String > selectedColumnNames, Set< A > selectedAnnotations ) { Map columnAverages = new HashMap<>(); for (String column : selectedColumnNames ) { @@ -156,7 +154,7 @@ private static < A extends Annotation > Map< String, Double > averageSelectedAnn } @NotNull - private static < A extends Annotation > Map< String, Double > medianSelectedAnnotations( List< String > selectedColumnNames, Set< A > selectedAnnotations ) + private static < A extends Annotation > Map< String, Double > computeMedianOfSelectedAnnotations( List< String > selectedColumnNames, Set< A > selectedAnnotations ) { Map columnMedians = new HashMap<>(); for (String column : selectedColumnNames) {