Skip to content

Commit

Permalink
Feature: avoid negative NaN when casting float NaN to double NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Dec 17, 2023
1 parent 0deb5f1 commit a59191d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "feature.h"

#include <cmath>
#include <limits>
#include <stdexcept>

Expand Down Expand Up @@ -48,7 +49,11 @@ Feature::set(const std::string& name, std::size_t value)
void
Feature::set(const std::string& name, float value)
{
set(name, static_cast<double>(value));
if (std::isnan(value)) {
set(name, std::numeric_limits<double>::quiet_NaN());
} else {
set(name, static_cast<double>(value));
}
}

Feature::FieldValue
Expand Down
25 changes: 23 additions & 2 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ def runner(*args, **kwargs):
else:
arglist += [f"--{k}", f"{x}"]

subprocess.run(['./exactextract', '-o', output_fname] + arglist,
check=True)
cmd = [str(x) for x in ['./exactextract', '-o', output_fname] + arglist]

#print(' '.join(cmd))

subprocess.run(cmd, check=True)

with open(output_fname, 'r') as f:
reader = csv.DictReader(f)
Expand Down Expand Up @@ -140,6 +143,24 @@ def test_stats_deferred(run, write_raster, write_features):
assert float(rows[1]['frac_3']) == 0.25


@pytest.mark.parametrize("strategy", ("feature-sequential", "raster-sequential"))
def test_feature_not_intersecting_raster(strategy, run, write_raster, write_features):

data = np.array([
[1, 2, 3],
[1, 2, 2],
[3, 3, 3]], np.float32)

rows = run(
polygons=write_features([{"id":1, "geom":"POLYGON ((100 100, 200 100, 200 200, 100 100))"}]),
fid="id",
raster=f"value:{write_raster(data)}",
stat=["count(value)", "mean(value)"])

assert len(rows) == 1
assert rows[0] == {'id' : '1', 'value_count' : '0', 'value_mean' : 'nan'}


@pytest.mark.parametrize("strategy", ("feature-sequential", "raster-sequential"))
def test_include_cols(strategy, run, write_raster, write_features):

Expand Down

0 comments on commit a59191d

Please sign in to comment.