Skip to content

Commit

Permalink
fix segfault for NaN values
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger committed Jul 13, 2023
1 parent f91343a commit 492c086
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/_build.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#
# Template workflow to build and install BiasAdjustCXX
#
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_build_doc.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#
# Template workflow to build documentation.
#
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_build_docker.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Checks the code logic, style and more
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#
# Workflow to build the docker image.

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_pre_commit.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#
# Template workflow to run pre-commit.
#
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_test.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Checks the code logic, style and more
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#
# Workflow to build the test suite and run the unit tests.

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Checks the code logic, style and more
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#
# Workflow to apply pre-commit, build, test and upload the docker
# image(s).
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Checks the code logic, style and more
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#
# Workflow to apply pre-commit, build, test and upload the docker
# image. This job is only triggered during a release.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Checks the code logic, style and more
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#
# Workflow to apply pre-commit, build, test and upload the docker
# image. This job is only triggered during a release.
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding:utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger
#

FROM alpine:3.17 AS builder
Expand Down
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!make
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# Github: https://github.com/btschwertfeger
# GitHub: https://github.com/btschwertfeger

PROJECT := BiasAdjustCXX
TEST_PROJECT := TestBiasAdjustCXX
Expand Down Expand Up @@ -46,12 +46,12 @@ redoc: clean doc

## ======= U N -/ I N S T A L L A T I O N =======
## install Installation of the BiasAdjustCXX tool
## (after a successfull the build)
## (after a successful the build)
##
install:
cd build && make install

## uninstall Uninstallation of the BiasAdjustCXX tool
## uninstall Remove of the BiasAdjustCXX tool
##
uninstall:
cd build && make uninstall
Expand All @@ -73,6 +73,8 @@ uninstall-val:
test:
cd build/tests && ctest

retest: dev test

## ======= M I S C E L A N I O U S =======
## changelog Create the changelog
##
Expand Down
58 changes: 51 additions & 7 deletions src/CMethods.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
#include "CMethods.hxx"

#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <vector>

#include "MathUtils.hxx"
Expand Down Expand Up @@ -424,6 +426,48 @@ void CMethods::Delta_Method(
}
}

/**
* Helper function used in `get_xbins`
*
* It takes two float values and returns a bool indicating whether the first
* value is considered smaller than the second.
* example:
* a_max = *std::max_element(a.begin(), a.end(), is_smaller),
*
* @param a some value
* @param b what a wonder... another value to compare to
*
* … will return the maximum value without failing if any value is NaN
*/
bool is_smaller(double a, double b) {
if (std::isnan(a))
return false; // a is NaN, consider it as larger
if (std::isnan(b))
return true; // b is NaN, consider a as smaller
return a < b; // neither a nor b is NaN, compare normally
}

/**
* Helper function used in `get_xbins`
*
* It takes two float values and returns a bool indicating whether the first
* value is considered larger than the second.
* example:
* a_max = *std::min_element(a.begin(), a.end(), is_larger),
*
* @param a some value
* @param b what a wonder... another value to compare to
*
* … will return the minimum value without failing if any value is NaN.
*/
bool is_larger(double a, double b) {
if (std::isnan(a))
return true;
if (std::isnan(b))
return false;
return a < b;
}

/**
* Returns the probability boundaries based on two input time series
* -> This is required to compute the bin boundaries for the Probability Density and Cumulative Distribution Function.
Expand All @@ -433,7 +477,7 @@ void CMethods::Delta_Method(
* @param a time series
* @param b another time series
* @param n_quantiles number of quantiles to use/respect
* @param kind regular or bounded: defines if mininum value of a (climate) variable can be lower than in the data
* @param kind regular or bounded: defines if minimum value of a (climate) variable can be lower than in the data
* or is set to 0 (ratio based variables => 0)
* @return the probability boundaries mentioned above
*/
Expand All @@ -445,10 +489,10 @@ std::vector<double> CMethods::get_xbins(
) {
if (kind == "regular") {
const double
a_max = *std::max_element(std::begin(a), std::end(a)),
a_min = *std::min_element(std::begin(a), std::end(a)),
b_max = *std::max_element(std::begin(b), std::end(b)),
b_min = *std::min_element(std::begin(b), std::end(b));
a_max = *std::max_element(a.begin(), a.end(), is_smaller),
a_min = *std::min_element(a.begin(), a.end(), is_larger),
b_max = *std::max_element(b.begin(), b.end(), is_smaller),
b_min = *std::min_element(b.begin(), b.end(), is_larger);

const double
global_max = std::max(a_max, b_max),
Expand All @@ -463,8 +507,8 @@ std::vector<double> CMethods::get_xbins(

} else if (kind == "bounded") {
const double
a_max = *std::max_element(std::begin(a), std::end(a)),
b_max = *std::max_element(std::begin(b), std::end(b));
a_max = *std::max_element(std::begin(a), std::end(a), is_smaller),
b_max = *std::max_element(std::begin(b), std::end(b), is_smaller);
const double global_max = std::max(a_max, b_max);
const double wide = global_max / n_quantiles;

Expand Down

0 comments on commit 492c086

Please sign in to comment.