Skip to content

Commit

Permalink
Apply valid time constraints to a time-series before it is used for e…
Browse files Browse the repository at this point in the history
…vent detection, #388.
  • Loading branch information
james-d-brown committed Feb 10, 2025
1 parent f42b99c commit 61eaeaf
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 55 deletions.
57 changes: 44 additions & 13 deletions src/wres/pipeline/pooling/EventsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.protobuf.Timestamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -146,7 +147,8 @@ Set<TimeWindowOuter> doEventDetection( Project project,
.name(),
covariateTimeScale,
this.covariateUpscaler(),
null );
null,
declaration );

innerEvents = this.doEventDetection( details );
}
Expand All @@ -163,7 +165,8 @@ Set<TimeWindowOuter> doEventDetection( Project project,
.name(),
covariateTimeScale,
this.covariateUpscaler(),
null );
null,
declaration );
innerEvents = this.doEventDetection( details );
}
case BASELINE ->
Expand All @@ -179,7 +182,8 @@ Set<TimeWindowOuter> doEventDetection( Project project,
.name(),
covariateTimeScale,
this.covariateUpscaler(),
null );
null,
declaration );
innerEvents = this.doEventDetection( details );
}
case COVARIATE -> throw new IllegalStateException( "Covariate dataset cannot have a "
Expand All @@ -203,7 +207,8 @@ Set<TimeWindowOuter> doEventDetection( Project project,
null,
desiredTimeScale,
this.leftUpscaler(),
this.measurementUnit() );
this.measurementUnit(),
declaration );
Set<TimeWindowOuter> innerEvents = this.doEventDetection( details );
this.combineEvents( detectionAttempted, events, innerEvents, combination );
detectionAttempted = true;
Expand All @@ -220,7 +225,8 @@ Set<TimeWindowOuter> doEventDetection( Project project,
null,
desiredTimeScale,
this.rightUpscaler(),
this.measurementUnit() );
this.measurementUnit(),
declaration );
Set<TimeWindowOuter> innerEvents = this.doEventDetection( details );
this.combineEvents( detectionAttempted, events, innerEvents, combination );
detectionAttempted = true;
Expand All @@ -237,7 +243,8 @@ Set<TimeWindowOuter> doEventDetection( Project project,
null,
desiredTimeScale,
this.baselineUpscaler(),
this.measurementUnit() );
this.measurementUnit(),
declaration );
Set<TimeWindowOuter> innerEvents = this.doEventDetection( details );
this.combineEvents( detectionAttempted, events, innerEvents, combination );
detectionAttempted = true;
Expand Down Expand Up @@ -283,17 +290,37 @@ private Set<TimeWindowOuter> doEventDetection( EventDetectionDetails details )
}

Set<TimeWindowOuter> events = new TreeSet<>();
TimeWindowOuter unbounded = TimeWindowOuter.of( MessageUtilities.getTimeWindow() );
FeatureGroup featureGroup = details.featureGroup();
Function<FeatureTuple, Feature> featureGetter = details.featureGetter();
RetrieverFactory<Double, Double, Double> eventRetriever = details.eventRetriever();

// Get any valid time constraints on retrieval, accounting for the timescale
TimeWindowOuter timeWindow = TimeWindowOuter.of( MessageUtilities.getTimeWindow() );
EvaluationDeclaration declaration = details.declaration();
TimeInterval validDates = declaration.validDates();
if ( Objects.nonNull( validDates ) )
{
Instant minimumInstant = validDates.minimum();
Instant maximumInstant = validDates.maximum();
Timestamp minimum = MessageUtilities.getTimestamp( minimumInstant );
Timestamp maximum = MessageUtilities.getTimestamp( maximumInstant );
TimeWindow adjusted = timeWindow.getTimeWindow()
.toBuilder()
.setEarliestValidTime( minimum )
.setLatestValidTime( maximum )
.build();
TimeWindowOuter adjustedOuter = TimeWindowOuter.of( adjusted );

// Adjust for timescale
timeWindow = TimeWindowSlicer.adjustTimeWindowForTimeScale( adjustedOuter, details.desiredTimeScale() );
}

switch ( details.dataset() )
{
case OBSERVED ->
{
Set<Feature> features = this.getFeatures( featureGroup.getFeatures(), featureGetter );
Stream<TimeSeries<Double>> series = eventRetriever.getLeftRetriever( features )
Stream<TimeSeries<Double>> series = eventRetriever.getLeftRetriever( features, timeWindow )
.get();

Set<TimeWindowOuter> innerEvents = series.flatMap( s -> this.doEventDetection( s,
Expand All @@ -310,7 +337,7 @@ private Set<TimeWindowOuter> doEventDetection( EventDetectionDetails details )
case PREDICTED ->
{
Set<Feature> features = this.getFeatures( featureGroup.getFeatures(), featureGetter );
Stream<TimeSeries<Double>> series = eventRetriever.getRightRetriever( features, unbounded )
Stream<TimeSeries<Double>> series = eventRetriever.getRightRetriever( features, timeWindow )
.get();

Set<TimeWindowOuter> innerEvents = series.flatMap( s -> this.doEventDetection( s,
Expand All @@ -327,7 +354,7 @@ private Set<TimeWindowOuter> doEventDetection( EventDetectionDetails details )
case BASELINE ->
{
Set<Feature> features = this.getFeatures( featureGroup.getFeatures(), featureGetter );
Stream<TimeSeries<Double>> series = eventRetriever.getBaselineRetriever( features, unbounded )
Stream<TimeSeries<Double>> series = eventRetriever.getBaselineRetriever( features, timeWindow )
.get();

Set<TimeWindowOuter> innerEvents = series.flatMap( s -> this.doEventDetection( s,
Expand All @@ -345,7 +372,8 @@ private Set<TimeWindowOuter> doEventDetection( EventDetectionDetails details )
{
Set<Feature> features = this.getFeatures( featureGroup.getFeatures(), featureGetter );
Stream<TimeSeries<Double>> series = eventRetriever.getCovariateRetriever( features,
details.covariateName() )
details.covariateName(),
timeWindow )
.get();

Set<TimeWindowOuter> innerEvents =
Expand Down Expand Up @@ -627,7 +655,8 @@ private EventDetectionDetails getAdjustedDetails( EventDetectionDetails details,
details.covariateName(),
details.desiredTimeScale(),
details.upscaler(),
measurementUnit );
measurementUnit,
details.declaration() );
}

/**
Expand Down Expand Up @@ -658,6 +687,7 @@ private Set<Feature> getFeatures( Set<FeatureTuple> featureTuples, Function<Feat
* @param desiredTimeScale the desired timescale
* @param upscaler the upscaler
* @param measurementUnit the measurement unit
* @param declaration the declaration
*/

private record EventDetectionDetails( EventDetectionDataset dataset,
Expand All @@ -668,7 +698,8 @@ private record EventDetectionDetails( EventDetectionDataset dataset,
String covariateName,
TimeScaleOuter desiredTimeScale,
TimeSeriesUpscaler<Double> upscaler,
String measurementUnit )
String measurementUnit,
EvaluationDeclaration declaration )
{
}

Expand Down
Loading

0 comments on commit 61eaeaf

Please sign in to comment.