@@ -113,7 +113,7 @@ VectorError ComputeError( std::valarray< double > obsIn,
113113 // We need to find the appropriate slice parameters
114114
115115 // To try and be efficient, we first scan for nans, if none: stats
116- // If there are nans, then assess the overlapping subset and slice
116+ // If there are nans, copy from the overlapping values
117117 bool nanObs = false ;
118118 bool nanPred = false ;
119119
@@ -130,57 +130,51 @@ VectorError ComputeError( std::valarray< double > obsIn,
130130 pred = std::valarray< double >( predIn );
131131 }
132132 else {
133- // Handle nans...
133+ // Handle nans
134134 // Build concurrent vector of bool pairs : isnan on obsIn, predIn
135135 std::vector< std::pair< bool , bool > > nanIndexPairs ( Nin );
136136 for ( size_t i = 0 ; i < Nin; i++ ) {
137137 nanIndexPairs[ i ] = std::make_pair ( std::isnan ( obsIn[i] ),
138138 std::isnan ( predIn[i] ) );
139139 }
140- // Find overlapping subset indices
140+ // Find overlapping subset indices or use set::intersection
141141 // Condense pairs into one boolean value in nonNanOverlap
142+ size_t Nout = 0 ;
142143 std::vector< bool > nonNanOverlap ( Nin );
143144 for ( size_t i = 0 ; i < Nin; i++ ) {
144145 if ( not nanIndexPairs[ i ].first and
145146 not nanIndexPairs[ i ].second ) {
146147 nonNanOverlap[ i ] = true ; // Both are not nan, valid index
148+ Nout++;
147149 }
148150 else {
149151 nonNanOverlap[ i ] = false ;
150152 }
151153 }
152- // Get the slice parameters
153- auto firstValid = std::find ( nonNanOverlap.begin (),
154- nonNanOverlap.end (), true );
155- auto lastValid = std::find ( nonNanOverlap.rbegin (),
156- nonNanOverlap.rend (), true );
157- size_t firstValidIndex = std::distance ( nonNanOverlap.begin (),
158- firstValid );
159- size_t lastValidIndex = std::distance ( nonNanOverlap.rbegin (),
160- lastValid );
161-
162- lastValidIndex = Nin - lastValidIndex; // reverse iterator used...
163-
164- int Nout = (int ) lastValidIndex - (int ) firstValidIndex;
165-
166- if ( Nout < 0 ) {
154+
155+ if ( Nout < 6 ) {
167156 std::stringstream msg;
168- msg << " WARNING: ComputeError(): nan predictions found"
169- << " error not computed ." << std::endl;
157+ msg << " WARNING: ComputeError(): nan found. Not enough data "
158+ << " to compute error ." << std::endl;
170159 std::cout << msg.str ();
171160
172- Nout = 0 ;
173161 obs = std::valarray< double >( 0 ., 1 ); // vector [0.] N = 1
174162 pred = std::valarray< double >( 0 ., 1 ); // vector [0.] N = 1
175163 }
176164 else {
177165 // Allocate the output arrays and fill with slices
178166 obs = std::valarray< double >( Nout );
179167 pred = std::valarray< double >( Nout );
180-
181- std::slice nonNan = std::slice ( firstValidIndex, Nout, 1 );
182- obs [ std::slice ( 0 , Nout, 1 ) ] = obsIn [ nonNan ];
183- pred[ std::slice ( 0 , Nout, 1 ) ] = predIn[ nonNan ];
168+
169+ // Copy valid values into obs & pred
170+ size_t n = 0 ;
171+ for ( size_t i = 0 ; i < nonNanOverlap.size (); i++ ) {
172+ if ( nonNanOverlap[ i ] ) {
173+ obs [ n ] = obsIn [ i ];
174+ pred[ n ] = predIn[ i ];
175+ n++;
176+ }
177+ }
184178 }
185179 }
186180
0 commit comments