-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import time | ||
import pytest | ||
|
||
import numpy as np | ||
|
||
from extra_foam.algorithms import ( | ||
nanmean, nansum | ||
) | ||
|
||
|
||
def benchmark_nan(f_cpp, f_py, shape): | ||
data = np.random.randn(*shape) | ||
data[::2, ::2] = np.nan | ||
|
||
t0 = time.perf_counter() | ||
for i in range(100): | ||
ret_cpp = f_cpp(data) | ||
dt_cpp = time.perf_counter() - t0 | ||
|
||
t0 = time.perf_counter() | ||
for i in range(100): | ||
ret_py = f_py(data) | ||
dt_py = time.perf_counter() - t0 | ||
|
||
assert ret_cpp == pytest.approx(ret_py) | ||
|
||
print(f"\n{f_cpp.__name__} with {float} - \n" | ||
f"dt (cpp): {dt_cpp:.4f}, " | ||
f"dt (numpy): {dt_py:.4f}") | ||
|
||
|
||
if __name__ == "__main__": | ||
print("*" * 80) | ||
print("Benchmark image processing") | ||
print("*" * 80) | ||
|
||
s = (1096, 1120) | ||
|
||
for f_cpp, f_py in [(nanmean, np.nanmean), | ||
(nansum, np.nansum)]: | ||
benchmark_nan(f_cpp, f_py, s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Distributed under the terms of the BSD 3-Clause License. | ||
* | ||
* The full license is in the file LICENSE, distributed with this software. | ||
* | ||
* Author: Jun Zhu <jun.zhu@xfel.eu> | ||
* Copyright (C) European X-Ray Free-Electron Laser Facility GmbH. | ||
* All rights reserved. | ||
*/ | ||
#include "pybind11/pybind11.h" | ||
|
||
#include "f_statistics.hpp" | ||
#include "f_pyconfig.hpp" | ||
|
||
namespace py = pybind11; | ||
|
||
|
||
PYBIND11_MODULE(statistics, m) | ||
{ | ||
xt::import_numpy(); | ||
|
||
m.doc() = "A collection of statistics functions."; | ||
|
||
m.def("nansum", [] (const xt::pytensor<double, 2>& src) { return foam::nansum(src); }); | ||
m.def("nansum", [] (const xt::pytensor<float, 2>& src) { return foam::nansum(src); }); | ||
|
||
m.def("nanmean", [] (const xt::pytensor<double, 2>& src) { return foam::nanmean(src); }); | ||
m.def("nanmean", [] (const xt::pytensor<float, 2>& src) { return foam::nanmean(src); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Distributed under the terms of the BSD 3-Clause License. | ||
* | ||
* The full license is in the file LICENSE, distributed with this software. | ||
* | ||
* Author: Jun Zhu <jun.zhu@xfel.eu> | ||
* Copyright (C) European X-Ray Free-Electron Laser Facility GmbH. | ||
* All rights reserved. | ||
*/ | ||
#ifndef EXTRA_FOAM_F_STATISTICS_HPP | ||
#define EXTRA_FOAM_F_STATISTICS_HPP | ||
|
||
#include <type_traits> | ||
|
||
#include "xtensor/xreducer.hpp" | ||
|
||
#if defined(FOAM_WITH_TBB) | ||
#include "tbb/parallel_for.h" | ||
#include "tbb/blocked_range2d.h" | ||
#include "tbb/blocked_range3d.h" | ||
#endif | ||
|
||
#include "f_traits.hpp" | ||
|
||
|
||
namespace foam | ||
{ | ||
|
||
template<typename E, EnableIf<std::decay_t<E>, IsImage> = false> | ||
inline auto nansum(E&& src) | ||
{ | ||
return xt::nansum(std::forward<E>(src), xt::evaluation_strategy::immediate)[0]; | ||
} | ||
|
||
template<typename E, EnableIf<std::decay_t<E>, IsImage> = false> | ||
inline auto nanmean(E&& src) | ||
{ | ||
return xt::nanmean(std::forward<E>(src), xt::evaluation_strategy::immediate)[0]; | ||
} | ||
|
||
} // foam | ||
|
||
|
||
#endif //EXTRA_FOAM_F_STATISTICS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Distributed under the terms of the BSD 3-Clause License. | ||
* | ||
* The full license is in the file LICENSE, distributed with this software. | ||
* | ||
* Author: Jun Zhu <jun.zhu@xfel.eu> | ||
* Copyright (C) European X-Ray Free-Electron Laser Facility GmbH. | ||
* All rights reserved. | ||
*/ | ||
#include "gtest/gtest.h" | ||
#include "gmock/gmock.h" | ||
|
||
#include "xtensor/xio.hpp" | ||
#include "xtensor/xtensor.hpp" | ||
|
||
#include "f_statistics.hpp" | ||
|
||
namespace foam | ||
{ | ||
namespace test | ||
{ | ||
|
||
using ::testing::ElementsAre; | ||
using ::testing::ElementsAreArray; | ||
using ::testing::NanSensitiveFloatEq; | ||
using ::testing::FloatEq; | ||
|
||
TEST(TestNanmean, TestGeneral) | ||
{ | ||
auto nan = std::numeric_limits<float>::quiet_NaN(); | ||
auto nan_mt = NanSensitiveFloatEq(nan); | ||
|
||
xt::xtensor<float, 2> img {{1.f, -1.f, 1.f}, {4.f, 5.f, nan}}; | ||
EXPECT_EQ(2.f, foam::nanmean(img)); | ||
} | ||
|
||
TEST(TestNansum, TestGeneral) | ||
{ | ||
auto nan = std::numeric_limits<float>::quiet_NaN(); | ||
auto nan_mt = NanSensitiveFloatEq(nan); | ||
|
||
xt::xtensor<float, 2> img {{1.f, -1.f, 1.f}, {4.f, 5.f, nan}}; | ||
EXPECT_EQ(10.f, foam::nansum(img)); | ||
} | ||
|
||
} //test | ||
} //foam |