diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index ed26d030..4b901c85 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -99,8 +99,13 @@ if(HAVE_PYTHON) file(GLOB TEST_SOURCES ../test/*_test.py ../examples/*.py ${CMAKE_BINARY_DIR}/doc_test/*.py) foreach(SRC IN ITEMS ${TEST_SOURCES}) if (SRC MATCHES "/([_a-zA-Z0-9]+)\\.py") - configure_file(${SRC} ${CMAKE_MATCH_1}.py) - add_test(${CMAKE_MATCH_1} ${PYTHON_EXECUTABLE} ${CMAKE_MATCH_1}.py) + set(TESTNAME ${CMAKE_MATCH_1}) + file(READ ${SRC} SRC_CONTENT) + if (HAVE_NUMPY OR + (NOT HAVE_NUMPY AND NOT SRC_CONTENT MATCHES "^import numpy|\nimport numpy")) + configure_file(${SRC} ${TESTNAME}.py) + add_test(${TESTNAME} ${PYTHON_EXECUTABLE} ${TESTNAME}.py) + endif() endif() endforeach() endif() diff --git a/doc/guide.qbk b/doc/guide.qbk index f7528b76..5b2941a4 100644 --- a/doc/guide.qbk +++ b/doc/guide.qbk @@ -69,14 +69,14 @@ Using a [classref boost::histogram::axis::integer integer axis] in this example Since the histogram has Python-bindings, and you can also create histograms in Python. In fact, it is quite a useful workflow to create histograms in a flexible way in Python and then pass them to some C++ code which fills them. You rarely need to change the way the histogram is filled, but you likely want to iterate the range and binning of the axis after seeing the data. Hybrid programming in C++ and Python fits the bill. Here is a conceptual example: [python]`` -# also see examples/ +# also see examples/create_python_fill_cpp.py and examples/module_cpp_filler.cpp import histogram as bh -import module_cpp_filler +import cpp_filler h = bh.histogram(bh.axis.regular(100, -1, 1), bh.axis.integer(0, 10)) -module_cpp_filler.process(h) # histogram is filled with input values +cpp_filler.process(h) # histogram is filled with input values # continue with statistical analysis of h `` diff --git a/examples/example_1d.cpp b/examples/example_1d.cpp index 719a3a39..0d7512b5 100644 --- a/examples/example_1d.cpp +++ b/examples/example_1d.cpp @@ -10,7 +10,7 @@ int main(int, char**) { namespace bh = boost::histogram; - using namespace boost::histogram::literals; // enables _c suffix + using namespace bh::literals; // enables _c suffix // create 1d-histogram with 10 equidistant bins from -1.0 to 2.0, // with axis of histogram labeled as "x" @@ -49,5 +49,6 @@ int main(int, char**) { bin 8 x in [1.4, 1.7): 0 +/- 0 bin 9 x in [1.7, 2): 1 +/- 1 bin 10 x in [2, inf): 2 +/- 1.41421 + */ } diff --git a/examples/example_1d.py b/examples/python_example_1d.py similarity index 50% rename from examples/example_1d.py rename to examples/python_example_1d.py index 7f77685b..8562f3a1 100644 --- a/examples/example_1d.py +++ b/examples/python_example_1d.py @@ -1,23 +1,19 @@ import histogram as hg -if "@HAVE_NUMPY@": - raise SystemExit import numpy as np -h = hg.histogram(hg.axis.regular(10, -3, 3)) +h = hg.histogram(hg.axis.regular(10, -3, 3, "x")) h.fill(np.random.randn(1000)) -bins = h.axis(0).bins - x = np.array(h.axis(0)) # axis instances behave like sequences y = np.asarray(h) # creates a view (no copy involved) -y = y[:bins] # cut off underflow/overflow bins -y = np.append(y, [0]) # append a zero because matplotlib's plot(...) is weird +y = y[:h.axis(0).bins] # cut off underflow/overflow bins; y[:-2] also works +y = np.append(y, [0]) # extra zero needed by matplotlib's plot(...) function try: import matplotlib.pyplot as plt plt.plot(x, y, drawstyle="steps-post") - plt.xlabel("x") - plt.ylabel("y") - plt.show() + plt.xlabel(h.axis(0).label) + plt.ylabel("counts") + plt.savefig("example_1d_python.png") except ImportError: pass \ No newline at end of file diff --git a/examples/example_2d.py b/examples/python_example_2d.py similarity index 86% rename from examples/example_2d.py rename to examples/python_example_2d.py index d8701129..2b37f229 100644 --- a/examples/example_2d.py +++ b/examples/python_example_2d.py @@ -1,6 +1,4 @@ import histogram as hg -if not "@HAVE_NUMPY@": - raise SystemExit import numpy as np h = hg.histogram(hg.axis.regular(10, -3, 3, uoflow=False), @@ -9,8 +7,6 @@ xy = np.random.randn(2000).reshape((1000, 2)) xy[:,1] *= 0.5 h.fill(xy) -bins = h.axis(0).bins - x = np.array(h.axis(0)) # axis instances behave like sequences y = np.array(h.axis(1)) z = np.asarray(h) # creates a view (no copy involved) @@ -20,6 +16,6 @@ try: plt.pcolor(x, y, z.T) plt.xlabel("x") plt.ylabel("y") - plt.show() + plt.savefig("example_2d_python.png") except ImportError: pass diff --git a/examples/create_python_fill_cpp.py b/examples/python_fill_cpp.py similarity index 100% rename from examples/create_python_fill_cpp.py rename to examples/python_fill_cpp.py