Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in overriding global probability thresholds with an all dat… #354

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions wres-config/src/wres/config/yaml/DeclarationInterpolator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,8 @@ private static Metric addThresholdsToMetric( Map<wres.config.yaml.components.Thr
MetricParameters nextParameters = metric.parameters();
MetricParametersBuilder parametersBuilder = MetricParametersBuilder.builder();

boolean overrideAllData = false;

// Add existing parameters where available
if ( Objects.nonNull( nextParameters ) )
{
Expand All @@ -1891,21 +1893,20 @@ private static Metric addThresholdsToMetric( Map<wres.config.yaml.components.Thr
// Special case: an 'all data' threshold is declared explicitly for a metric. This indicates that the metric
// should be computed for 'all data' only, as the 'all data' threshold is otherwise added by default, which
// would render the request meaningless
if ( parametersBuilder.thresholds()
.size() == 1
// Declared by a user, i.e., not generated by the software?
&& !parametersBuilder.thresholds()
.iterator()
.next()
.generated()
&& parametersBuilder.thresholds()
.iterator()
.next()
.threshold()
.equals( DeclarationUtilities.ALL_DATA_THRESHOLD.threshold() ) )
Set<Threshold> test = parametersBuilder.thresholds()
.stream()
.filter( t -> !t.generated() )
.collect( Collectors.toSet() );

if ( test.size() == 1
&& test.iterator()
.next()
.threshold()
.equals( DeclarationUtilities.ALL_DATA_THRESHOLD.threshold() ) )
{
LOGGER.debug( "The metric {} contained a threshold override for 'all data' only.", name );
globalThresholds = Map.of();
overrideAllData = true;
}
}

Expand All @@ -1919,14 +1920,10 @@ private static Metric addThresholdsToMetric( Map<wres.config.yaml.components.Thr

// Add "all data" thresholds?
if ( name.isContinuous()
&& addAllData )
&& addAllData
&& !overrideAllData )
{
// Flag this as an internally generated threshold
Threshold allData = ThresholdBuilder.builder( DeclarationUtilities.ALL_DATA_THRESHOLD )
.generated( true )
.build();

valueThresholds.add( allData );
valueThresholds.add( DeclarationUtilities.GENERATED_ALL_DATA_THRESHOLD );
}

// Render the value thresholds featureful, if possible
Expand All @@ -1942,6 +1939,7 @@ private static Metric addThresholdsToMetric( Map<wres.config.yaml.components.Thr
DeclarationInterpolator.getCombinedThresholds( probByType,
parametersBuilder.probabilityThresholds(),
addThresholds );

// Render the probability thresholds featureful, if possible
probabilityThresholds = DeclarationInterpolator.getFeatureFulThresholds( probabilityThresholds, builder );
parametersBuilder.probabilityThresholds( probabilityThresholds );
Expand Down
10 changes: 8 additions & 2 deletions wres-config/src/wres/config/yaml/DeclarationUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public class DeclarationUtilities
.generated( false )
.build();

/** An all data threshold generated by the software, rather than declared by a user. */
public static final Threshold GENERATED_ALL_DATA_THRESHOLD =
ThresholdBuilder.builder( DeclarationUtilities.ALL_DATA_THRESHOLD )
.generated( true )
.build();

/** Re-used message. */
private static final String CANNOT_DETERMINE_TIME_WINDOWS_FROM_MISSING_DECLARATION = "Cannot determine time "
+ "windows from missing "
Expand Down Expand Up @@ -180,8 +186,8 @@ else if ( Objects.nonNull( leadDurationPools ) )
}
}
// One big pool if no explicitly declared time windows
else if( declaration.timeWindows()
.isEmpty() )
else if ( declaration.timeWindows()
.isEmpty() )
{
LOGGER.debug( "Building one big time window." );

Expand Down
68 changes: 68 additions & 0 deletions wres-config/test/wres/config/yaml/DeclarationInterpolatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,74 @@ void testInterpolateMissingTimeWindowComponents()
assertEquals( expected, actual );
}

@Test
void testInterpolateMetricThresholdsWithAllDataOverride()
{
Metric metric = new Metric( MetricConstants.MEAN_ABSOLUTE_ERROR, MetricParametersBuilder.builder()
.build() );

Metric anotherMetric =
new Metric( MetricConstants.MEAN_ERROR,
MetricParametersBuilder.builder()
.thresholds( Set.of( DeclarationUtilities.ALL_DATA_THRESHOLD ) )
.build() );

Threshold one = Threshold.newBuilder()
.setLeftThresholdValue( 0.1 )
.setOperator( Threshold.ThresholdOperator.GREATER )
.build();
wres.config.yaml.components.Threshold oneWrapped =
ThresholdBuilder.builder()
.threshold( one )
.type( ThresholdType.PROBABILITY )
.build();

// Preserve insertion order
Set<wres.config.yaml.components.Threshold> thresholds = Set.of( oneWrapped );

EvaluationDeclaration declaration = EvaluationDeclarationBuilder.builder()
.left( this.observedDataset )
.right( this.predictedDataset )
.probabilityThresholds( thresholds )
.metrics( Set.of( metric, anotherMetric ) )
.build();

VariableNames variableNames = new VariableNames( null,
null,
null,
null );
DataTypes dataTypes = new DataTypes( null, null, null, null );
TimeScale timeScale = TimeScale.newBuilder()
.setPeriod( com.google.protobuf.Duration.newBuilder().setSeconds( 64 ) )
.setFunction( TimeScale.TimeScaleFunction.MEAN )
.build();

EvaluationDeclaration interpolated = DeclarationInterpolator.interpolate( declaration,
dataTypes,
variableNames,
"foo",
timeScale,
false );

Metric expectedOne =
new Metric( MetricConstants.MEAN_ERROR,
MetricParametersBuilder.builder()
.thresholds( Set.of( DeclarationUtilities.ALL_DATA_THRESHOLD ) )
.build() );

Metric expectedTwo =
new Metric( MetricConstants.MEAN_ABSOLUTE_ERROR,
MetricParametersBuilder.builder()
.thresholds( Set.of( DeclarationUtilities.GENERATED_ALL_DATA_THRESHOLD ) )
.probabilityThresholds( thresholds )
.build() );

Set<Metric> expected = Set.of( expectedOne, expectedTwo );
Set<Metric> actual = interpolated.metrics();

assertEquals( expected, actual );
}

// The testDeserializeAndInterpolate* tests are integration tests of deserialization plus interpolation

@Test
Expand Down
Loading