{ "cells": [ { "cell_type": "markdown", "id": "5019a615", "metadata": {}, "source": [ "# Thin-plate spline" ] }, { "cell_type": "code", "execution_count": null, "id": "55074a5d", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "from ktch.landmark import GeneralizedProcrustesAnalysis\n", "from ktch.landmark import tps_grid_2d_plot\n", "from ktch.datasets import load_landmark_mosquito_wings" ] }, { "cell_type": "markdown", "id": "d2748c70", "metadata": {}, "source": [ "## Load mosquito wing landmark dataset\n", "from Rohlf and Slice 1990 Syst. Zool." ] }, { "cell_type": "code", "execution_count": null, "id": "23898f3d", "metadata": {}, "outputs": [], "source": [ "data_landmark_mosquito_wings = load_landmark_mosquito_wings(as_frame=True)\n", "data_landmark_mosquito_wings.coords" ] }, { "cell_type": "markdown", "id": "e37d6ae7", "metadata": {}, "source": [ "## GPA\n", "see also :ref:`generalized_Procrustes_analysis`" ] }, { "cell_type": "code", "execution_count": null, "id": "ab680637", "metadata": {}, "outputs": [], "source": [ "X = data_landmark_mosquito_wings.coords.to_numpy().reshape(-1,18*2)" ] }, { "cell_type": "code", "execution_count": null, "id": "8e1d54da", "metadata": {}, "outputs": [], "source": [ "gpa = GeneralizedProcrustesAnalysis(tol=10**-5)" ] }, { "cell_type": "code", "execution_count": null, "id": "195edd44", "metadata": {}, "outputs": [], "source": [ "X_aligned = gpa.fit_transform(X)" ] }, { "cell_type": "markdown", "id": "51f5803d", "metadata": {}, "source": [ "### Mean shape and an aligned shape" ] }, { "cell_type": "code", "execution_count": null, "id": "69fd0a74", "metadata": {}, "outputs": [], "source": [ "X_reference = gpa.mu_ # mean shape\n", "X_target = X_aligned.reshape(-1, 18, 2)[0] # the 0-th aligned shape" ] }, { "cell_type": "code", "execution_count": null, "id": "2df81974", "metadata": {}, "outputs": [], "source": [ "fig = plt.figure()\n", "ax = fig.add_subplot(111)\n", "\n", "sns.scatterplot(x=X_reference[:,0], y=X_reference[:,1], ax=ax)\n", "sns.scatterplot(x=X_target[:,0], y=X_target[:,1], ax=ax)\n", "\n", "ax.set_aspect('equal')" ] }, { "cell_type": "markdown", "id": "29a60fd5", "metadata": {}, "source": [ "## Transformation grids of thin-plate splines" ] }, { "cell_type": "code", "execution_count": null, "id": "e73034b6", "metadata": {}, "outputs": [], "source": [ "tps_grid_2d_plot(X_reference, X_target, outer = 0.2, grid_size = 0.03)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }