-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python Interface with Nanobind (#99)
Summary cherry-picking the squashed messages: * Nanobind extension * Basic test and example. * CI workflow. * Update extension.
- Loading branch information
Showing
10 changed files
with
295 additions
and
97 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,37 @@ | ||
name: Nanobind | ||
|
||
on: | ||
# TODO probably change/add main later. Ah, perhaps this wf can be part of linux.yml? | ||
push: | ||
branches: [ "nanobind" ] | ||
# pull_request: | ||
# branches: [ ] | ||
|
||
jobs: | ||
build: | ||
name: "Build Python interface" | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Python setup | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.11.9' | ||
|
||
- name: Install dependencies | ||
run: | | ||
sudo apt-get install -y libarpack2-dev libcxxopts-dev libeigen3-dev libfmt-dev python3.11-dev | ||
python -m pip install nanobind numpy | ||
- name: Configure | ||
run: cmake -B ${{github.workspace}}/build -DBUILD_NANOBIND=ON | ||
|
||
- 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 |
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,10 @@ | ||
import lib.tapkee as tapkee | ||
from utils import generate_data, plot | ||
|
||
if __name__=='__main__': | ||
parameters = tapkee.ParametersSet() | ||
method = tapkee.parse_reduction_method('lle') | ||
parameters.add(tapkee.Parameter.create('dimension reduction method', method)) | ||
data, colors = generate_data('swissroll') | ||
embedded_data = tapkee.withParameters(parameters).embedUsing(data).embedding | ||
plot(data, embedded_data.T, colors) |
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,91 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
def generate_data(type, N=1000, random_state=None): | ||
rng = np.random.RandomState(random_state) | ||
if type=='swissroll': | ||
tt = np.array((3*np.pi/2)*(1+2*rng.rand(N))) | ||
height = np.array((rng.rand(N)-0.5)) | ||
X = np.array([tt*np.cos(tt), 10*height, tt*np.sin(tt)]) | ||
return X, tt | ||
if type=='scurve': | ||
tt = np.array((3*np.pi*(rng.rand(N)-0.5))) | ||
height = np.array((rng.rand(N)-0.5)) | ||
X = np.array([np.sin(tt), 10*height, np.sign(tt)*(np.cos(tt)-1)]) | ||
return X, tt | ||
if type=='helix': | ||
tt = np.linspace(1,N,N).T / N | ||
tt = tt*2*np.pi | ||
X = np.r_[[(2+np.cos(8*tt))*np.cos(tt)], | ||
[(2+np.cos(8*tt))*np.sin(tt)], | ||
[np.sin(8*tt)]] | ||
return X, tt | ||
if type=='twinpeaks': | ||
X = rng.uniform(-1, 1, size=(N, 2)) | ||
tt = np.sin(np.pi * X[:, 0]) * np.tanh(X[:, 1]) | ||
tt += 0.1 * rng.normal(size=tt.shape) | ||
X = np.vstack([X.T, tt]) | ||
return X, tt | ||
if type=='klein': | ||
u = rng.uniform(0, 2 * np.pi, N) | ||
v = rng.uniform(0, 2 * np.pi, N) | ||
x = (2 + np.cos(u / 2) * np.sin(v) - np.sin(u / 2) * np.sin(2 * v)) * np.cos(u) | ||
y = (2 + np.cos(u / 2) * np.sin(v) - np.sin(u / 2) * np.sin(2 * v)) * np.sin(u) | ||
z = np.sin(u / 2) * np.sin(v) + np.cos(u / 2) * np.sin(2 * v) | ||
|
||
noise = 0.01 | ||
x += noise * rng.normal(size=x.shape) | ||
y += noise * rng.normal(size=y.shape) | ||
z += noise * rng.normal(size=z.shape) | ||
return np.vstack((x, y, z)), u | ||
|
||
raise Exception('Dataset is not supported') | ||
|
||
def plot(data, embedded_data, colors='m', method=None): | ||
fig = plt.figure() | ||
fig.set_facecolor('white') | ||
|
||
ax_original = fig.add_subplot(121, projection='3d') | ||
scatter_original = ax_original.scatter(data[0], data[1], data[2], c=colors, cmap=plt.cm.Spectral, s=5, picker=True) | ||
plt.axis('tight') | ||
plt.axis('off') | ||
plt.title('Original', fontsize=9) | ||
|
||
ax_embedding = fig.add_subplot(122) | ||
scatter_embedding = ax_embedding.scatter(embedded_data[0], embedded_data[1], c=colors, cmap=plt.cm.Spectral, s=5, picker=True) | ||
plt.axis('tight') | ||
plt.axis('off') | ||
plt.title('Embedding' + (' with ' + method) if method else '', fontsize=9, wrap=True) | ||
|
||
highlighted_points = [] # To store highlighted points | ||
|
||
# Function to highlight points on both plots | ||
def highlight(index): | ||
# Reset previous highlighted points | ||
for point in highlighted_points: | ||
point.remove() | ||
highlighted_points.clear() | ||
|
||
# Highlight the current point on both scatter plots | ||
point1 = ax_original.scatter([data[0][index]], [data[1][index]], [data[2][index]], color='white', s=25, edgecolor='black', zorder=3) | ||
point2 = ax_embedding.scatter([embedded_data[0][index]], [embedded_data[1][index]], color='white', s=25, edgecolor='black', zorder=3) | ||
highlighted_points.append(point1) | ||
highlighted_points.append(point2) | ||
fig.canvas.draw_idle() | ||
|
||
# Event handler for mouse motion | ||
def on_hover(event): | ||
if event.inaxes == ax_original: | ||
cont, ind = scatter_original.contains(event) | ||
elif event.inaxes == ax_embedding: | ||
cont, ind = scatter_embedding.contains(event) | ||
else: | ||
return | ||
|
||
if cont: | ||
index = ind['ind'][0] | ||
highlight(index) | ||
|
||
fig.canvas.mpl_connect('motion_notify_event', on_hover) | ||
|
||
plt.show() |
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
Oops, something went wrong.