From 07910db57a97651f12f0799b2fa86969debcc7d5 Mon Sep 17 00:00:00 2001 From: "Fernando J. Iglesias Garcia" Date: Mon, 20 May 2024 06:39:03 +0200 Subject: [PATCH] Integrate basic test. --- .github/workflows/nanobind.yml | 4 ++++ examples/nanobind.py | 16 +--------------- src/python/nanobind_extension.cpp | 3 ++- test/test_nanobind_extension.py | 21 +++++++++++++++++++++ 4 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 test/test_nanobind_extension.py diff --git a/.github/workflows/nanobind.yml b/.github/workflows/nanobind.yml index 822197d..bb4ea8e 100644 --- a/.github/workflows/nanobind.yml +++ b/.github/workflows/nanobind.yml @@ -31,3 +31,7 @@ jobs: - name: Build # TODO there are A LOT of warnings in the logs from nanobind (-pedantic, -Wshadow). run: cmake --build ${{github.workspace}}/build + + - name: Test + working-directory: ${{github.workspace}} + run: PYTHONPATH=. python test/test_nanobind_extension.py diff --git a/examples/nanobind.py b/examples/nanobind.py index dd296fc..e79db19 100755 --- a/examples/nanobind.py +++ b/examples/nanobind.py @@ -1,23 +1,9 @@ -#!/usr/bin/python3.12 - import lib.tapkee as tapkee -import numpy as np from utils import generate_data, plot -def parse_reduction_method(method_name: str): - return tapkee.parse_reduction_method(method_name) - -def test_exception_unknown_method(): - try: - parse_reduction_method('unknown') - assert(False) - except: - pass - if __name__=='__main__': - test_exception_unknown_method() parameters = tapkee.ParametersSet() - method = parse_reduction_method('lle') + method = tapkee.parse_reduction_method('lle') parameters.add(tapkee.Parameter.create('dimension reduction method', method)) data, colors = generate_data('swissroll') embedded_data = tapkee.initialize().withParameters(parameters).embedUsing(data).embedding diff --git a/src/python/nanobind_extension.cpp b/src/python/nanobind_extension.cpp index b016e5d..e292688 100644 --- a/src/python/nanobind_extension.cpp +++ b/src/python/nanobind_extension.cpp @@ -43,5 +43,6 @@ NB_MODULE(tapkee, m) { .def_rw("name", &DimensionReductionMethod::name_); nb::class_(m, "Parameter") - .def_static("create", &Parameter::create); + .def_static("create", &Parameter::create) + .def_static("create", &Parameter::create); } diff --git a/test/test_nanobind_extension.py b/test/test_nanobind_extension.py new file mode 100644 index 0000000..38028b3 --- /dev/null +++ b/test/test_nanobind_extension.py @@ -0,0 +1,21 @@ +import lib.tapkee as tapkee +import numpy as np + +def test_exception_unknown_method(): + try: + tapkee.parse_reduction_method('unknown') + assert(False) + except: + pass + +if __name__=='__main__': + test_exception_unknown_method() + parameters = tapkee.ParametersSet() + method = tapkee.parse_reduction_method('spe') + assert(method.name == 'Stochastic Proximity Embedding (SPE)') + parameters.add(tapkee.Parameter.create('dimension reduction method', method)) + target_dimension = 2 + parameters.add(tapkee.Parameter.create('target dimension', target_dimension)) + data = np.random.randn(124, 3) + embedded_data = tapkee.initialize().withParameters(parameters).embedUsing(data).embedding + assert(embedded_data.shape == tuple([data.shape[1], target_dimension]))