Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XMas-Day-4: DataSet<T>: helpers, estimators, math functions, restructure DataSet #494

Merged
merged 3 commits into from
Jan 14, 2025

Conversation

RalphSteinhagen
Copy link
Member

On the fourth day of Christmas 🎄 👼 4️⃣ 🎄 , my code base gave to me...

Helpers, estimators, and math functions, oh my!

This PR enhances the DataSet<T> by introducing:

  1. Helper Methods:

    • Added convenience accessors to retrieve individual axes and signals as std::span<T> and std::span<const T> depending on constness.
  2. DataSet Restructure:

    • Introduced Range<T> with explicit min and max members for clear range definitions.
    • Removed signal_error in favor of using UncertainValue<T> to represent uncertainties.
  3. UncertainValue Enhancements:

    • Added missing isfinite and log10 functions for better mathematical operations.
    • Implemented the << operator to facilitate easy formatting and output.
  4. Estimators and Math Functions:

    • Introduced simple estimators for data analysis like mean, median, RMS, etc.
    • Implemented basic math functions to perform operations directly on DataSet<T>.
  5. Unit Tests:

    • Added comprehensive unit tests for the new estimators and math functionalities.
    • Updated existing tests to align with the DataSet restructuring.
  6. Utility Updates:

    • Updated accessor and utility functions to support the new features and ensure seamless integration.
  7. Documentation:

    • Updated doxygen comments and added code examples to illustrate the new methods and features.

Feel free to explore the new additions and share your feedback!
Happy coding and joyful holidays! 🎅🤶🎁

image

Code Example:

#include <gnuradio-4.0/algorithm/dataset/DataSetTestFunctions.hpp>
#include <gnuradio-4.0/algorithm/dataset/DataSetEstimators.hpp>
#include <gnuradio-4.0/algorithm/dataset/DataSetMath.hpp>

int main() {
    using namespace gr::dataset;

    // Generate a triangular DataSet
    auto triagDS = generate::triangular<double>("triag", 11, 0.0, 1.0);
    gr::dataset::draw(triagDS); // Visualize the DataSet

    // Generate a ramp DataSet
    auto rampDS = generate::ramp<double>("ramp", 11, 0.0, 0.2);
    gr::dataset::draw(rampDS); // Visualize the DataSet

    // Generate a Gaussian DataSet
    auto gaussDS = generate::gaussFunction<double>("gauss", 11, 5.0, 1.0, 0.0, 1.0);
    gr::dataset::draw(gaussDS); // Visualize the DataSet

    // Using Estimators
    double meanTriag = estimators::getMean(triagDS);
    double medianTriag = estimators::getMedian(triagDS);
    double rmsTriag = estimators::getRms(triagDS);
    double fwhmTriag = estimators::computeFWHM(triagDS.signalValues(0), 5); // Assuming peak at index 5

    fmt::print("Triangular DataSet - Mean: {}, Median: {}, RMS: {}, FWHM: {}\n", meanTriag, medianTriag, rmsTriag, fwhmTriag);

    // Performing Math Operations
    auto dsAdded = estimators::addFunction(triagDS, 2.0); // Adds 2 to all signal values
    gr::dataset::draw(dsAdded); // Visualize the modified DataSet

    auto dsCombined = estimators::addFunction(rampDS, gaussDS); // Adds two DataSets
    gr::dataset::draw(dsCombined); // Visualize the combined DataSet

    // Frequency Estimate
    double freqEstimate = estimators::getFrequencyEstimate(dsCombined);
    fmt::print("Frequency Estimate of Combined DataSet: {}\n", freqEstimate);

    // Edge Detection
    double edgeDetect = estimators::getEdgeDetect(dsCombined, 0.5);
    fmt::print("Edge Detection at 50% Threshold: {}\n", edgeDetect);

    // Integral Calculation
    double integral = estimators::getIntegral(dsCombined);
    fmt::print("Integral of Combined DataSet: {}\n", integral);

    return 0;
}

P.S. Did I mention that I like unit-tests?

@RalphSteinhagen RalphSteinhagen force-pushed the dataset_estimators branch 7 times, most recently from e609fb4 to ff23424 Compare December 29, 2024 17:03
…Set, and add tests

- Introduced helper utilities for DataSet<T> access and manipulation.
- Implemented simple estimators and basic mathematical functions.
- Restructured DataSet<T>:
  - Added explicit Range<T>::min and Range<T>::max.
  - Removed `signal_error` in favor of `UncertainValue<T>`.
- Enhanced UncertainValue<T>:
  - Added `isfinite` and `log10` functions.
  - Implemented the `<<` operator for easy formatting.
- Added unit tests for DataSetEstimators, DataSetMath, DataSetTestFunctions, and DataSet functionalities.
- Updated accessors and utility functions to support new features.

Signed-off-by: Ralph J. Steinhagen <[email protected]>
@RalphSteinhagen
Copy link
Member Author

Added applyFilter(..) implementation:

Example:
Screenshot_20250107_082606

for (const auto& zeta : {0.01, 0.1, 0.99}) {
    auto                filter = Filter<double>(iir::designResonatorPhysical(fs, 10., zeta));
    std::vector<double> yValue(xValues.size());
    std::vector<double> yFiltered(xValues.size());
    std::transform(xValues.cbegin(), xValues.cend(), yValue.begin(), [](double t) { return t < 0.05 ? 0.0 : 1.0; });
    std::transform(yValue.cbegin(), yValue.cend(), yFiltered.begin(), [&filter](double y) { return filter.processOne(y); });
    expect(nothrow([&] { chart.draw(xValues, yFiltered, fmt::format("resonance@zeta={}", zeta)); })) << fmt::format("resonance@zeta={} does not throw", zeta);
}

expect(nothrow([&] { chart.draw(); }));

@RalphSteinhagen
Copy link
Member Author

added estimators::analyzeStepPulseResponse(...). Example:

image

Includes (incl. optional tags):

  • (first-edge) trigger time
  • 10%-90% rise-time
  • overshoot peak position
  • relative overshoot
  • settling time
  • initial- and steady-state level

 * added 2nd-order resonance filter
 * added apply[Symmetric]Filter(..) math function
 * added enum to declare if math operation is performed `ProcessMode::InPlace` or on a `ProcessMode::Copy`
 * added enum to declare if estimates are added as meta-info (`ProcessMode::None`)
 * improved recurring `gr::dataset::detail::checkIndexRange(..)` helper function
 * fixed some missing `(ds, minIndex, maxIndex, ..)` method signatures.
 * added `gr::cast<T>(..)` to suppress float-double compiler warnings in templating code that cannot be resolved programmatically
 * added `filter::applyMedian(..)`
 * added `filter::applyRms(..)`
 * added `filter::applyPeakToPeak(..)`

Signed-off-by: Ralph J. Steinhagen <[email protected]>
Copy link
Contributor

@drslebedev drslebedev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR!

The code in this PR looks very good and is thoroughly tested.

There are a few suggestions regarding consistency and optimizations that I believe should be reviewed before merge.

Once those points are resolved, this PR will be ready for merging.

Comment on lines +561 to +562
T minVal = getMinimum(dataSet, indexMin, indexMax, signalIndex).value().value;
T maxVal = getMaximum(dataSet, indexMin, indexMax, signalIndex).value().value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be checked for std::nullopt and return std::numeric_limits<TValue>::quiet_NaN(), otherwise an exception can be thrown.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is guaranteed to always have a value in this context of the earlier checkIndexRange(..) check above and also the same check within the get[Minimum, Maximum](...) functions.

}

template<std::ranges::random_access_range T, typename TValue = typename T::value_type>
[[nodiscard]] constexpr TValue computeInterpolatedFWHM(const T& data, std::size_t index) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for computeFWHM. Should be clarified in the documentation that function returns a difference in indices but not actual values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be clear from the context here since there is no x-axis provided. Thus any width is always expressed in terms of the indexing.

This is different for the FWHM estimators that work on DataSet<T> though.

Comment on lines +160 to +165
T sum = 0;
T count = 0;
for (const auto& val : finiteValues) {
sum += val;
count += T(1);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only here but a general question, should we stick to use more std::ranges?

Suggested change
T sum = 0;
T count = 0;
for (const auto& val : finiteValues) {
sum += val;
count += T(1);
}
T sum = std::accumulate(finiteValues.begin(), finiteValues.end(), T(0));
T count = static_cast<T>(std::ranges::distance(finiteValues));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't use it here because this performs two accumulates/counting. To note this is because the finiteValues and signalRange views can have different sizes depending on how many actual 'NaN' are present in the data range.

We should discuss from a design point-of-view, whether we support or intercept 'NaN' floating point values. I took this implementation/choice based upon one of our other DataSet implementing math libraries (in Chart-FX) that supports 'NaN' by default. However, this choice is opinionated and we may equally disallow or explicitly not support this for GR4.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
62.7% Coverage on New Code (required ≥ 80%)
3.9% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@RalphSteinhagen RalphSteinhagen merged commit cb9487a into main Jan 14, 2025
11 of 14 checks passed
@RalphSteinhagen RalphSteinhagen deleted the dataset_estimators branch January 14, 2025 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants