1   /* Copyright 2002-2025 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.orekit.estimation.common;
19  
20  import org.hipparchus.stat.descriptive.StreamingStatistics;
21  import org.orekit.estimation.measurements.EstimatedMeasurement;
22  import org.orekit.estimation.measurements.ObservedMeasurement;
23  
24  import java.io.PrintStream;
25  import java.util.Comparator;
26  import java.util.SortedSet;
27  import java.util.TreeSet;
28  
29  /** Local class for measurement-specific log.
30   * @param T type of mesurement
31   * @author Luc Maisonobe
32   */
33  abstract class MeasurementLog<T extends ObservedMeasurement<T>> implements EvaluationLogger<T> {
34  
35      /** Residuals. */
36      private final SortedSet<EstimatedMeasurement<T>> evaluations;
37  
38      /** Simple constructor.
39       */
40      MeasurementLog() {
41          this.evaluations = new TreeSet<EstimatedMeasurement<T>>(Comparator.naturalOrder());
42      }
43  
44      /** Compute residual value.
45       * @param evaluation evaluation to consider
46       * @return residual value
47       */
48      abstract double residual(EstimatedMeasurement<T> evaluation);
49  
50      /** {@inheritDoc} */
51      @Override
52      public void log(final EstimatedMeasurement<T> evaluation) {
53          evaluations.add(evaluation);
54      }
55  
56      /** Create a  statistics summary
57       */
58      StreamingStatistics createStatisticsSummary() {
59  
60          if (evaluations.isEmpty()) {
61              return null;
62          }
63  
64          // compute statistics
65          final StreamingStatistics stats = new StreamingStatistics();
66          for (final EstimatedMeasurement<T> evaluation : evaluations) {
67              stats.addValue(residual(evaluation));
68          }
69          return stats;
70  
71      }
72  
73      /** Display summary statistics in the general log file.
74       * @param logStream log stream
75       */
76      public void displaySummary(final PrintStream logStream) {
77          if (!evaluations.isEmpty()) {
78  
79              // Compute statistics
80              final StreamingStatistics stats = createStatisticsSummary();
81  
82              // Display statistics
83              final String name = evaluations.first().getObservedMeasurement().getClass().getSimpleName();
84              logStream.println("Measurements type: " + name);
85              logStream.println("   number of measurements: " + stats.getN() + "/" + evaluations.size());
86              logStream.println("   residuals min  value  : " + stats.getMin());
87              logStream.println("   residuals max  value  : " + stats.getMax());
88              logStream.println("   residuals mean value  : " + stats.getMean());
89              logStream.println("   residuals σ           : " + stats.getStandardDeviation());
90              logStream.println("   residuals median      : " + stats.getMedian());
91  
92          }
93      }
94  
95  }