{ "cells": [ { "cell_type": "markdown", "id": "28239470", "metadata": {}, "source": [ "# Thin-plate spline" ] }, { "cell_type": "code", "execution_count": null, "id": "4d76ed7d", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "from ktch.landmark import GeneralizedProcrustesAnalysis\n", "from ktch.plot import tps_grid_2d_plot\n", "from ktch.datasets import load_landmark_mosquito_wings" ] }, { "cell_type": "markdown", "id": "b6a39738", "metadata": {}, "source": [ "## Load mosquito wing landmark dataset\n", "\n", "from Rohlf and Slice 1990 Syst. Zool." ] }, { "cell_type": "code", "execution_count": null, "id": "2c9ab38f", "metadata": {}, "outputs": [], "source": [ "data_landmark_mosquito_wings = load_landmark_mosquito_wings(as_frame=True)\n", "data_landmark_mosquito_wings.coords" ] }, { "cell_type": "markdown", "id": "780bc4d2", "metadata": {}, "source": [ "## GPA\n", "\n", "see also :ref:`generalized_Procrustes_analysis`" ] }, { "cell_type": "code", "execution_count": null, "id": "021af21c", "metadata": {}, "outputs": [], "source": [ "X = data_landmark_mosquito_wings.coords.to_numpy().reshape(-1, 18 * 2)" ] }, { "cell_type": "code", "execution_count": null, "id": "d898238c", "metadata": {}, "outputs": [], "source": [ "gpa = GeneralizedProcrustesAnalysis(tol=10**-5)" ] }, { "cell_type": "code", "execution_count": null, "id": "5a25e9ae", "metadata": {}, "outputs": [], "source": [ "X_aligned = gpa.fit_transform(X)" ] }, { "cell_type": "markdown", "id": "18406dd5", "metadata": {}, "source": [ "### Mean shape and an aligned shape" ] }, { "cell_type": "code", "execution_count": null, "id": "fb4da7e2", "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": "e7baa86e", "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": "596ea307", "metadata": {}, "source": [ "## Transformation grids of thin-plate splines" ] }, { "cell_type": "code", "execution_count": null, "id": "b61b45e4", "metadata": {}, "outputs": [], "source": [ "tps_grid_2d_plot(X_reference, X_target, outer=0.2, grid_size=0.03)" ] } ], "metadata": { "jupytext": { "default_lexer": "ipython3" }, "kernelspec": { "display_name": "ktch", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }