.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_quick-start/01_01_tensorspline_class.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_quick-start_01_01_tensorspline_class.py: Class TensorSpline ================== Showcase TensorSpline class basic functionality. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from splineops.interpolate.tensorspline import TensorSpline .. GENERATED FROM PYTHON SOURCE LINES 21-25 Data Preparation ---------------- General configuration and sample data. .. GENERATED FROM PYTHON SOURCE LINES 25-38 .. code-block:: Python dtype = "float32" nx, ny = 2, 5 xmin, xmax = 0, 2.0 ymin, ymax = 0, 5.0 xx = np.linspace(xmin, xmax, nx, dtype=dtype) yy = np.linspace(ymin, ymax, ny, dtype=dtype) coordinates = xx, yy prng = np.random.default_rng(seed=5250) data = prng.standard_normal(size=tuple(c.size for c in coordinates)) data = np.ascontiguousarray(data, dtype=dtype) .. GENERATED FROM PYTHON SOURCE LINES 39-43 TensorSpline Setup ------------------ Configure bases and modes for TensorSpline. .. GENERATED FROM PYTHON SOURCE LINES 43-48 .. code-block:: Python bases = "bspline3" modes = "mirror" tensor_spline = TensorSpline(data=data, coordinates=coordinates, bases=bases, modes=modes) .. GENERATED FROM PYTHON SOURCE LINES 49-53 Evaluation Coordinates ---------------------- Define evaluation coordinates to extend and oversample the original grid. .. GENERATED FROM PYTHON SOURCE LINES 53-63 .. code-block:: Python dx = (xx[-1] - xx[0]) / (nx - 1) dy = (yy[-1] - yy[0]) / (ny - 1) pad_fct = 1.0 px = pad_fct * nx * dx py = pad_fct * ny * dy eval_xx = np.linspace(xx[0] - px, xx[-1] + px, 100 * nx) eval_yy = np.linspace(yy[0] - py, yy[-1] + py, 100 * ny) eval_coords = eval_xx, eval_yy .. GENERATED FROM PYTHON SOURCE LINES 64-68 Interpolation and Visualization ------------------------------- Perform interpolation and visualize the original and interpolated data. .. GENERATED FROM PYTHON SOURCE LINES 68-87 .. code-block:: Python data_eval = tensor_spline(coordinates=eval_coords) extent = [xx[0] - dx / 2, xx[-1] + dx / 2, yy[0] - dy / 2, yy[-1] + dy / 2] eval_extent = [ eval_xx[0] - dx / 2, eval_xx[-1] + dx / 2, eval_yy[0] - dy / 2, eval_yy[-1] + dy / 2, ] fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 6), sharex="all", sharey="all") axes[0].imshow(data.T, extent=extent, cmap="gray", aspect="equal") axes[0].set_title("Original Data Samples") axes[1].imshow(data_eval.T, extent=eval_extent, cmap="gray", aspect="equal") axes[1].set_title("Interpolated Data") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/01_quick-start/images/sphx_glr_01_01_tensorspline_class_001.png :alt: Original Data Samples, Interpolated Data :srcset: /auto_examples/01_quick-start/images/sphx_glr_01_01_tensorspline_class_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-93 GPU Support ----------- We leverage the GPU for TensorSpline if cupy is installed. If cupy is not available, we skip this section. .. GENERATED FROM PYTHON SOURCE LINES 93-122 .. code-block:: Python try: import cupy as cp HAS_CUPY = True except ImportError: HAS_CUPY = False if not HAS_CUPY: print("CuPy is not installed, skipping GPU demonstration.") else: # Convert existing data/coordinates to CuPy data_cp = cp.asarray(data) coords_cp = tuple(cp.asarray(c) for c in coordinates) # Create CuPy-based spline ts_cp = TensorSpline(data=data_cp, coordinates=coords_cp, bases=bases, modes=modes) # Convert evaluation coordinates to CuPy eval_coords_cp = tuple(cp.asarray(c) for c in eval_coords) # Evaluate on the GPU data_eval_cp = ts_cp(coordinates=eval_coords_cp) # Compare with NumPy evaluation # (Ensure you already have data_eval from the CPU version above.) data_eval_cp_np = data_eval_cp.get() # Move from GPU to CPU diff = data_eval_cp_np - data_eval # 'data_eval' is from the CPU TensorSpline mse = np.mean(diff**2) print(f"Max abs diff (CPU vs GPU): {np.max(np.abs(diff)):.3e}") print(f"MSE (CPU vs GPU): {mse:.3e}") .. rst-class:: sphx-glr-script-out .. code-block:: none CuPy is not installed, skipping GPU demonstration. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.270 seconds) .. _sphx_glr_download_auto_examples_01_quick-start_01_01_tensorspline_class.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/splineops/splineops.github.io/main?urlpath=lab/tree/notebooks_binder/auto_examples/01_quick-start/01_01_tensorspline_class.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/01_quick-start/01_01_tensorspline_class.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01_01_tensorspline_class.ipynb <01_01_tensorspline_class.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01_01_tensorspline_class.py <01_01_tensorspline_class.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01_01_tensorspline_class.zip <01_01_tensorspline_class.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_