Skip to content

Commit

Permalink
Refactor the utilities for time windows and add unit tests, #130
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-brown committed Dec 26, 2024
1 parent b5c19aa commit cd5f16b
Show file tree
Hide file tree
Showing 21 changed files with 2,565 additions and 2,295 deletions.
13 changes: 7 additions & 6 deletions src/wres/pipeline/EvaluationUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import wres.config.yaml.components.GeneratedBaselines;
import wres.config.yaml.components.ThresholdType;
import wres.datamodel.bootstrap.BootstrapUtilities;
import wres.datamodel.time.TimeWindowSlicer;
import wres.datamodel.types.Ensemble;
import wres.datamodel.MissingValues;
import wres.datamodel.pools.Pool;
Expand Down Expand Up @@ -1416,18 +1417,18 @@ private static ToIntFunction<Pool<TimeSeries<Pair<Double, Ensemble>>>> getEnsemb
* @param separateMetricsForBaseline whether to generate a filter for baseline pools with separate metrics
* @return the filters
*/
private static List<Predicate<Statistics>> getTimeWindowFilters( Set<TimeWindow> timeWindows,
private static List<Predicate<Statistics>> getTimeWindowFilters( Set<TimeWindowOuter> timeWindows,
boolean separateMetricsForBaseline )
{
List<Predicate<Statistics>> filters = new ArrayList<>();

for ( TimeWindow timeWindow : timeWindows )
for ( TimeWindowOuter timeWindow : timeWindows )
{
// Filter for main pools
Predicate<Statistics> nextMainFilter = statistics -> statistics.hasPool()
&& statistics.getPool()
.getTimeWindow()
.equals( timeWindow );
.equals( timeWindow.getTimeWindow() );
filters.add( nextMainFilter );

// Separate metrics for baseline?
Expand All @@ -1437,7 +1438,7 @@ private static List<Predicate<Statistics>> getTimeWindowFilters( Set<TimeWindow>
&& statistics.hasBaselinePool()
&& statistics.getBaselinePool()
.getTimeWindow()
.equals( timeWindow );
.equals( timeWindow.getTimeWindow() );
filters.add( nextBaseFilter );
}
}
Expand Down Expand Up @@ -1610,7 +1611,7 @@ private static Map<String, List<SummaryStatisticsCalculator>> getSummaryStatisti
.separateMetrics();

// Get the time window and threshold filters
Set<TimeWindow> timeWindows = DeclarationUtilities.getTimeWindows( declaration );
Set<TimeWindowOuter> timeWindows = TimeWindowSlicer.getTimeWindows( declaration );
Set<wres.config.yaml.components.Threshold> thresholds = DeclarationUtilities.getInbandThresholds( declaration );
List<TimeWindowAndThresholdFilterAdapter> timeWindowAndThresholdFilters =
EvaluationUtilities.getTimeWindowAndThresholdFilters( timeWindows,
Expand Down Expand Up @@ -2053,7 +2054,7 @@ private static FeatureGroupFilterAdapter getBaselineFeatureGroupForSummaryStatis
* @param clearThresholdValues is true to clear event threshold values, false otherwise
* @return the filters
*/
private static List<TimeWindowAndThresholdFilterAdapter> getTimeWindowAndThresholdFilters( Set<TimeWindow> timeWindows,
private static List<TimeWindowAndThresholdFilterAdapter> getTimeWindowAndThresholdFilters( Set<TimeWindowOuter> timeWindows,
Set<wres.config.yaml.components.Threshold> thresholds,
boolean separateMetricsForBaseline,
boolean clearThresholdValues )
Expand Down
22 changes: 12 additions & 10 deletions src/wres/pipeline/pooling/PoolFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import wres.config.yaml.components.Season;
import wres.config.yaml.components.Source;
import wres.config.yaml.components.Values;
import wres.datamodel.time.TimeWindowSlicer;
import wres.datamodel.types.Ensemble;
import wres.datamodel.types.Ensemble.Labels;
import wres.datamodel.MissingValues;
Expand Down Expand Up @@ -498,17 +499,14 @@ private Map<FeatureGroup, Set<TimeWindowOuter>> getTimeWindows( EvaluationDeclar
RetrieverFactory<Double, Double, Double> eventRetriever )
{
// Declared time windows
Set<TimeWindow> timeWindows = DeclarationUtilities.getTimeWindows( declaration );
Set<TimeWindowOuter> timeWindows = TimeWindowSlicer.getTimeWindows( declaration );

// Time windows without event detection
if ( Objects.isNull( declaration.eventDetection() ) )
{
Set<TimeWindowOuter> timeWindowsWrapped = timeWindows.stream()
.map( TimeWindowOuter::of )
.collect( Collectors.toCollection( TreeSet::new ) );
return featureGroups.stream()
.collect( Collectors.toMap( Function.identity(),
g -> timeWindowsWrapped ) );
g -> timeWindows ) );
}

// Time windows based on event detection
Expand All @@ -524,14 +522,18 @@ private Map<FeatureGroup, Set<TimeWindowOuter>> getTimeWindows( EvaluationDeclar
Set<TimeWindowOuter> adjustedEvents = new HashSet<>();
for ( TimeWindowOuter next : events )
{
for ( TimeWindow adjust : timeWindows )
for ( TimeWindowOuter adjust : timeWindows )
{
TimeWindow adjusted = next.getTimeWindow()
.toBuilder()
.setEarliestReferenceTime( adjust.getEarliestReferenceTime() )
.setLatestReferenceTime( adjust.getLatestReferenceTime() )
.setEarliestLeadDuration( adjust.getEarliestLeadDuration() )
.setLatestLeadDuration( adjust.getLatestLeadDuration() )
.setEarliestReferenceTime( adjust.getTimeWindow()
.getEarliestReferenceTime() )
.setLatestReferenceTime( adjust.getTimeWindow()
.getLatestReferenceTime() )
.setEarliestLeadDuration( adjust.getTimeWindow()
.getEarliestLeadDuration() )
.setLatestLeadDuration( adjust.getTimeWindow()
.getLatestLeadDuration() )
.build();
adjustedEvents.add( TimeWindowOuter.of( adjusted ) );
}
Expand Down
3 changes: 2 additions & 1 deletion src/wres/pipeline/pooling/PoolsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import wres.config.yaml.components.DataType;
import wres.config.yaml.components.DatasetOrientation;
import wres.config.yaml.components.Variable;
import wres.datamodel.time.TimeWindowSlicer;
import wres.datamodel.types.Climatology;
import wres.datamodel.baselines.BaselineGenerator;
import wres.datamodel.pools.Pool;
Expand Down Expand Up @@ -752,7 +753,7 @@ private Map<TimeWindowOuter, Supplier<Stream<TimeSeries<L>>>> getLeftRetrievers(
{
// Find the union of the time windows, bearing in mind that lead durations can influence the valid
// datetimes for observation selection
TimeWindowOuter unionWindow = TimeWindowOuter.unionOf( timeWindows );
TimeWindowOuter unionWindow = TimeWindowSlicer.union( timeWindows );
Supplier<Stream<TimeSeries<L>>> leftRetriever = factory.getLeftRetriever( features, unionWindow );
Supplier<Stream<TimeSeries<L>>> cachingRetriever = CachingRetriever.of( leftRetriever );

Expand Down
8 changes: 4 additions & 4 deletions test/wres/pipeline/EvaluationUtilitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import wres.config.MetricConstants;
import wres.config.yaml.DeclarationUtilities;
import wres.config.yaml.components.EvaluationDeclaration;
import wres.config.yaml.components.EvaluationDeclarationBuilder;
import wres.config.yaml.components.FeatureGroups;
Expand All @@ -27,6 +26,8 @@
import wres.datamodel.space.FeatureTuple;
import wres.datamodel.thresholds.MetricsAndThresholds;
import wres.datamodel.thresholds.ThresholdOuter;
import wres.datamodel.time.TimeWindowOuter;
import wres.datamodel.time.TimeWindowSlicer;
import wres.metrics.SummaryStatisticsCalculator;
import wres.statistics.generated.Geometry;
import wres.statistics.generated.GeometryGroup;
Expand All @@ -35,7 +36,6 @@
import wres.statistics.generated.Statistics;
import wres.statistics.generated.SummaryStatistic;
import wres.statistics.generated.Threshold;
import wres.statistics.generated.TimeWindow;

/**
* Tests the {@link EvaluationUtilities}.
Expand Down Expand Up @@ -188,13 +188,13 @@ void testGetSummaryStatisticsCalculatorsForNamedThresholdsAcrossAllFeatures()
.setLeftThresholdValue( 23.0 )
.build();

TimeWindow big = DeclarationUtilities.getOneBigTimeWindow( evaluation );
TimeWindowOuter big = TimeWindowSlicer.getOneBigTimeWindow( evaluation );

Statistics statistics =
Statistics.newBuilder()
.setPool( Pool.newBuilder()
.setEventThreshold( eventThreshold )
.setTimeWindow( big ) )
.setTimeWindow( big.getTimeWindow() ) )
.build();

assertTrue( calculators.values().stream()
Expand Down
Loading

0 comments on commit cd5f16b

Please sign in to comment.