Commit 85dfc6395d662cf41670b9cfcc54abac1a18722a

Authored by Jordi Inglada
1 parent 23a09a96
Exists in code-review

REFAC: extract code to functions for readability

Showing 1 changed file with 66 additions and 42 deletions   Show diff stats
include/phenoFunctions.h
... ... @@ -232,6 +232,69 @@ protected:
232 232 bool fit_only_invalid{true};
233 233 bool use_mask{true};
234 234  
  235 + PixelType ComputeFit(const CoefficientType& x_1, MinMaxType mm1,
  236 + const CoefficientType& x_2, MinMaxType mm2,
  237 + const PixelType& pix,
  238 + const VectorType& mv) const
  239 + {
  240 + // Compute the approximation
  241 + auto nbDates = pix.GetSize();
  242 + auto tmpres = normalized_sigmoid::F(dv, x_1)*(mm1.second-mm1.first)+mm1.first
  243 + + normalized_sigmoid::F(dv, x_2)*(mm2.second-mm2.first)+mm2.first;
  244 + // The result uses either the original or the approximated value depending on the mask value
  245 + PixelType result(nbDates);
  246 + for(size_t i=0; i<nbDates; i++)
  247 + if(fit_only_invalid)
  248 + result[i] = ((mv[i]==(typename PixelType::ValueType{0}))?pix[i]:tmpres[i]);
  249 + else
  250 + result[i] = tmpres[i];
  251 +
  252 + return result;
  253 + }
  254 +
  255 + PixelType ComputeMetrics(std::tuple<VectorType, ApproximationResult,
  256 + ApproximationResult> approx) const
  257 + {
  258 + PixelType result(12);
  259 + auto princ_cycle = std::get<1>(approx);
  260 + auto x_princ = princ_cycle.x;
  261 + auto m_princ = pheno::normalized_sigmoid::pheno_metrics<double>(x_princ);
  262 + result[0] = std::get<0>(m_princ);
  263 + result[1] = std::get<1>(m_princ);
  264 + result[2] = std::get<2>(m_princ);
  265 + result[3] = std::get<3>(m_princ);
  266 + result[4] = std::get<4>(m_princ);
  267 + result[5] = std::get<5>(m_princ);
  268 +
  269 + auto sec_cycle = std::get<1>(approx);
  270 + auto x_sec = sec_cycle.x;
  271 + auto m_sec = pheno::normalized_sigmoid::pheno_metrics<double>(x_sec);
  272 + result[6+0] = std::get<0>(m_sec);
  273 + result[6+1] = std::get<1>(m_sec);
  274 + result[6+2] = std::get<2>(m_sec);
  275 + result[6+3] = std::get<3>(m_sec);
  276 + result[6+4] = std::get<4>(m_sec);
  277 + result[6+5] = std::get<5>(m_sec);
  278 +
  279 + return result;
  280 + }
  281 + PixelType ComputeParameters(const CoefficientType& x_1, MinMaxType mm1,
  282 + const CoefficientType& x_2, MinMaxType mm2) const
  283 + {
  284 + PixelType result(12);
  285 + result[0] = (mm1.second-mm1.first);
  286 + result[1] = mm1.first;
  287 + for(auto i=0; i<4; ++i)
  288 + result[i+2] = x_1[i];
  289 +
  290 + result[6] = (mm2.second-mm2.first);
  291 + result[7] = mm2.first;
  292 + for(auto i=0; i<4; ++i)
  293 + result[i+8] = x_2[i];
  294 +
  295 + return result;
  296 + }
  297 +
235 298 public:
236 299 struct DifferentSizes {};
237 300  
... ... @@ -311,57 +374,18 @@ public:
311 374  
312 375 if(return_fit)
313 376 {
314   - // Compute the approximation
315   - auto tmpres = normalized_sigmoid::F(dv, x_1)*(mm1.second-mm1.first)+mm1.first
316   - + normalized_sigmoid::F(dv, x_2)*(mm2.second-mm2.first)+mm2.first;
317   - // The result uses either the original or the approximated value depending on the mask value
318   - PixelType result(nbDates);
319   - for(size_t i=0; i<nbDates; i++)
320   - if(fit_only_invalid)
321   - result[i] = ((mv[i]==(typename PixelType::ValueType{0}))?pix[i]:tmpres[i]);
322   - else
323   - result[i] = tmpres[i];
324   -
325   - return result;
  377 + return ComputeFit(x_1, mm1, x_2, mm2, pix, mv);
326 378 }
327 379 else
328 380 {
329   - PixelType result(12);
330 381 if(return_metrics)
331 382 {
332   - auto princ_cycle = std::get<1>(approx);
333   - auto x_princ = princ_cycle.x;
334   - auto m_princ = pheno::normalized_sigmoid::pheno_metrics<double>(x_princ);
335   - result[0] = std::get<0>(m_princ);
336   - result[1] = std::get<1>(m_princ);
337   - result[2] = std::get<2>(m_princ);
338   - result[3] = std::get<3>(m_princ);
339   - result[4] = std::get<4>(m_princ);
340   - result[5] = std::get<5>(m_princ);
341   -
342   - auto sec_cycle = std::get<1>(approx);
343   - auto x_sec = sec_cycle.x;
344   - auto m_sec = pheno::normalized_sigmoid::pheno_metrics<double>(x_sec);
345   - result[6+0] = std::get<0>(m_sec);
346   - result[6+1] = std::get<1>(m_sec);
347   - result[6+2] = std::get<2>(m_sec);
348   - result[6+3] = std::get<3>(m_sec);
349   - result[6+4] = std::get<4>(m_sec);
350   - result[6+5] = std::get<5>(m_sec);
  383 + return ComputeMetrics(approx);
351 384 }
352 385 else
353 386 {
354   - result[0] = (mm1.second-mm1.first);
355   - result[1] = mm1.first;
356   - for(auto i=0; i<4; ++i)
357   - result[i+2] = x_1[i];
358   -
359   - result[6] = (mm2.second-mm2.first);
360   - result[7] = mm2.first;
361   - for(auto i=0; i<4; ++i)
362   - result[i+8] = x_2[i];
  387 + return ComputeParameters(x_1, mm1, x_2, mm2);
363 388 }
364   - return result;
365 389 }
366 390 }
367 391  
... ...