Skip to content

Commit

Permalink
Metrics: do not allow single NaN values to invalidate statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
tferr committed Nov 15, 2024
1 parent ad76fc4 commit 33f4cbf
Showing 1 changed file with 39 additions and 57 deletions.
96 changes: 39 additions & 57 deletions src/main/java/sc/fiji/snt/analysis/TreeStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -410,25 +410,10 @@ public static List<String> getMetrics(final String type) {
SURFACE_AREA, STRAHLER_NUMBER, TERMINAL_LENGTH, VALUES, VOLUME, X_COORDINATES, Y_COORDINATES, Z_COORDINATES};
break;
case "quick":
/* NB: This list can only include metrics supported by #getMetricWithoutChecks() */
metrics = new String[]{ //
LENGTH, MultiTreeStatistics.AVG_BRANCH_LENGTH, N_BRANCH_POINTS, N_TIPS, N_BRANCHES, //
N_PRIMARY_BRANCHES, N_TERMINAL_BRANCHES, //
PATH_MEAN_RADIUS, //
AVG_SPINE_DENSITY, //
STRAHLER_NUMBER, MultiTreeStatistics.HIGHEST_PATH_ORDER,
/* Disabled metrics (likely too specific or uncommon for most users) */
// MultiTreeStatistics.ASSIGNED_VALUE, MultiTreeStatistics.AVG_CONTRACTION,
// MultiTreeStatistics.AVG_FRACTAL_DIMENSION,
// MultiTreeStatistics.AVG_FRAGMENTATION,
// MultiTreeStatistics.AVG_REMOTE_ANGLE,
// MultiTreeStatistics.AVG_PARTITION_ASYMMETRY,
// PRIMARY_LENGTH, INNER_LENGTH, TERMINAL_LENGTH,
// N_INNER_BRANCHES,
// N_FITTED_PATHS, N_PATHS, N_NODES,
// WIDTH, HEIGHT, DEPTH,
// SHOLL_DECAY, SHOLL_MAX_VALUE,
};
metrics = new String[] { // Excludes metrics expected to be too specific or uncommon for most users
LENGTH, BRANCH_LENGTH, N_BRANCH_POINTS, N_TIPS, N_BRANCHES, //
N_PRIMARY_BRANCHES, N_TERMINAL_BRANCHES, PATH_MEAN_RADIUS, //
PATH_SPINE_DENSITY, STRAHLER_NUMBER, PATH_ORDER};
break;
default:
throw new IllegalArgumentException("Unrecognized type");
Expand Down Expand Up @@ -667,17 +652,12 @@ public static void setExactMetricMatch(final boolean exactMetricMatch) {
* @see #setExactMetricMatch(boolean)
*/
public Number getMetric(final String metric) throws UnknownMetricException {
return getMetricWithoutChecks(getNormalizedMeasurement(metric));
}

protected Number getMetricWithoutChecks(final String metric) throws UnknownMetricException {
switch (metric) {
case MultiTreeStatistics.ASSIGNED_VALUE:
return tree.getAssignedValue();
case MultiTreeStatistics.AVG_BRANCH_LENGTH:
return getAvgBranchLength();
case MultiTreeStatistics.AVG_CONTRACTION:
case BRANCH_CONTRACTION:
return getAvgContraction();
case MultiTreeStatistics.AVG_FRAGMENTATION:
return getAvgFragmentation();
Expand All @@ -688,18 +668,14 @@ protected Number getMetricWithoutChecks(final String metric) throws UnknownMetri
case MultiTreeStatistics.AVG_FRACTAL_DIMENSION:
return getAvgFractalDimension();
case MultiTreeStatistics.PRIMARY_LENGTH:
case PRIMARY_LENGTH:
return getPrimaryLength();
case MultiTreeStatistics.TERMINAL_LENGTH:
case TERMINAL_LENGTH:
return getTerminalLength();
case MultiTreeStatistics.INNER_LENGTH:
case INNER_LENGTH:
return getInnerLength();
case MultiTreeStatistics.HIGHEST_PATH_ORDER:
return getHighestPathOrder();
case AVG_SPINE_DENSITY:
case PATH_SPINE_DENSITY:
return getSpineOrVaricosityDensity();
case DEPTH:
return getDepth();
Expand Down Expand Up @@ -1246,36 +1222,36 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue(p.getContraction());
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case BRANCH_EXTENSION_ANGLE_XY:
try {
getBranches().forEach(b -> stat.addValue(b.getExtensionAngleXY(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case BRANCH_EXTENSION_ANGLE_XZ:
try {
getBranches().forEach(b -> stat.addValue(b.getExtensionAngleXZ(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case BRANCH_EXTENSION_ANGLE_ZY:
try {
getBranches().forEach(b -> stat.addValue(b.getExtensionAngleZY(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case BRANCH_FRACTAL_DIMENSION:
try {
getFractalDimension().forEach(stat::addValue);
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case BRANCH_LENGTH:
Expand All @@ -1284,7 +1260,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue(p.getLength());
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case BRANCH_MEAN_RADIUS:
Expand All @@ -1293,7 +1269,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue(p.getMeanRadius());
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case BRANCH_SURFACE_AREA:
Expand All @@ -1302,7 +1278,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue(p.getApproximatedSurface());
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case BRANCH_VOLUME:
Expand All @@ -1311,7 +1287,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue(p.getApproximatedVolume());
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case COMPLEXITY_INDEX_ACI:
Expand Down Expand Up @@ -1339,7 +1315,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue((sumBranchTipOrders + graphTips.size()) * getCableLength() / getPrimaryBranches().size());
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case CONVEX_HULL_BOUNDARY_SIZE:
Expand All @@ -1351,7 +1327,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
break;
case CONVEX_HULL_CENTROID_ROOT_DISTANCE:
final PointInImage root = tree.getRoot();
if (root == null) stat.addValue(Double.NaN);
if (root == null) stat.addNaN();
else stat.addValue(getConvexAnalyzer().getCentroid().distanceTo(root));
break;
case DEPTH:
Expand All @@ -1361,28 +1337,28 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
try {
stat.addValue(tree.getGraph().getLongestPath(true).getLength());
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case GRAPH_DIAMETER_ANGLE_XY:
try {
stat.addValue(tree.getGraph().getLongestPath(true).getExtensionAngleXY(false));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case GRAPH_DIAMETER_ANGLE_XZ:
try {
stat.addValue(tree.getGraph().getLongestPath(true).getExtensionAngleXZ(false));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case GRAPH_DIAMETER_ANGLE_ZY:
try {
stat.addValue(tree.getGraph().getLongestPath(true).getExtensionAngleZY(false));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case HEIGHT:
Expand All @@ -1392,28 +1368,28 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
try {
getInnerBranches().forEach(b -> stat.addValue(b.getExtensionAngleXY(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case INNER_EXTENSION_ANGLE_XZ:
try {
getInnerBranches().forEach(b -> stat.addValue(b.getExtensionAngleXZ(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case INNER_EXTENSION_ANGLE_ZY:
try {
getInnerBranches().forEach(b -> stat.addValue(b.getExtensionAngleZY(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case INNER_LENGTH:
try {
getInnerBranches().forEach(b -> stat.addValue(b.getLength()));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case INTER_NODE_ANGLE:
Expand Down Expand Up @@ -1447,7 +1423,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
try {
getBranches().forEach(b -> stat.addValue(b.size()));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case N_BRANCH_POINTS:
Expand Down Expand Up @@ -1496,7 +1472,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue(asymmetry);
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case PATH_CHANNEL:
Expand All @@ -1510,7 +1486,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue(p.getContraction());
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case PATH_EXT_ANGLE_XY:
Expand Down Expand Up @@ -1572,21 +1548,21 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
try {
getPrimaryBranches().forEach(b -> stat.addValue(b.getExtensionAngleXY(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case PRIMARY_EXTENSION_ANGLE_XZ:
try {
getPrimaryBranches().forEach(b -> stat.addValue(b.getExtensionAngleXZ(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case PRIMARY_EXTENSION_ANGLE_ZY:
try {
getPrimaryBranches().forEach(b -> stat.addValue(b.getExtensionAngleZY(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case PRIMARY_LENGTH:
Expand All @@ -1599,7 +1575,7 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
stat.addValue(angle);
} catch (final IllegalArgumentException iae) {
SNTUtils.log("Error: " + iae.getMessage());
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case SHOLL_DECAY:
Expand Down Expand Up @@ -1629,21 +1605,21 @@ protected void assembleStats(final StatisticsInstance stat, final String measure
try {
getTerminalBranches().forEach(b -> stat.addValue(b.getExtensionAngleXY(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case TERMINAL_EXTENSION_ANGLE_XZ:
try {
getTerminalBranches().forEach(b -> stat.addValue(b.getExtensionAngleXZ(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case TERMINAL_EXTENSION_ANGLE_ZY:
try {
getTerminalBranches().forEach(b -> stat.addValue(b.getExtensionAngleZY(false)));
} catch (final IllegalArgumentException ignored) {
stat.addValue(Double.NaN);
stat.addNaN();
}
break;
case TERMINAL_LENGTH:
Expand Down Expand Up @@ -2582,7 +2558,13 @@ static class StatisticsInstance {
this.dStatistics = dStatistics;
}

void addNaN() {
if (sStatistics != null) sStatistics.addValue(Double.NaN);
else dStatistics.addValue(Double.NaN);
}

void addValue(final double value) {
if (Double.isNaN(value)) return;
if (sStatistics != null) sStatistics.addValue(value);
else dStatistics.addValue(value);
}
Expand Down

0 comments on commit 33f4cbf

Please sign in to comment.