Skip to content

Commit

Permalink
Cosmetic fixes in DistanceComputer
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Dec 21, 2023
1 parent 82a0065 commit 5711ea2
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/main/java/org/embl/mobie/lib/table/DistanceComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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
//
Expand All @@ -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 ) )
{
Expand All @@ -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 );
Expand All @@ -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<String, Double> columnAverages = new HashMap<>();
for (String column : selectedColumnNames ) {
Expand All @@ -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<String, Double> columnMedians = new HashMap<>();
for (String column : selectedColumnNames) {
Expand Down

0 comments on commit 5711ea2

Please sign in to comment.