{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# McCormik Validation\n", "\n", "## Import Statement :\n", "\n", "First make sure you have installed pandas and ipyvolume. When using virtualenv and working in an activated virtual environment, the --sys-prefix option may be required to enable the extension and keep the environment isolated (i.e. jupyter nbextension enable --py widgetsnbextension --sys-prefix)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy\n", "import pandas\n", "\n", "\n", "import openalea.phenomenal.object as phm_obj\n", "import openalea.phenomenal.mesh as phm_mesh\n", "import openalea.phenomenal.segmentation as phm_seg\n", "import openalea.phenomenal.display.notebook as phm_display_notebook\n", "import openalea.phenomenal.data as phm_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Import McCormik data\n", "\n", "### 1.1 Select plant_number" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vertices, faces, colors = phm_data.mesh_mccormik_plant(\n", " \"data/mccormick_plant_2\"\n", ") # only 1 or 2 available\n", "colors = colors[:, :3].astype(float) / 255.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2 Viewing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phm_display_notebook.show_mesh(vertices, faces, color=colors)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Phenomenal Measurements\n", "\n", "### 2.1 Voxelization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "voxels_size = 4.0\n", "voxels_position = phm_mesh.from_vertices_faces_to_voxels_position(\n", " vertices, faces, voxels_size=voxels_size\n", ")\n", "voxels_position = numpy.array(voxels_position)\n", "voxel_grid = phm_obj.VoxelGrid(voxels_position, voxels_size)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2 Viewing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phm_display_notebook.show_voxel_grid(voxel_grid, size=0.4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3 Skeletonization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = phm_seg.graph_from_voxel_grid(voxel_grid)\n", "voxel_skeleton = phm_seg.skeletonize(voxel_grid, graph)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Display it\n", "phm_display_notebook.show_skeleton(voxel_skeleton, with_voxel=True, size=0.4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.4 Cereals Segmentation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vms = phm_seg.maize_segmentation(voxel_skeleton, graph)\n", "vmsi = phm_seg.maize_analysis(vms)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Phenomenal measurements of each organs\n", "pm_rows = [vo.info for vo in vmsi.voxel_organs] + [vmsi.info]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Display it\n", "phm_display_notebook.show_segmentation(vmsi, size=0.4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. McCormik measurement" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stem_color = (0, 1, 1)\n", "index = numpy.apply_along_axis(numpy.array_equal, 1, colors, stem_color)\n", "v = vertices[index]\n", "\n", "mccm_rows = list()\n", "row = dict()\n", "row[\"mccm_label\"] = \"plant\"\n", "row[\"mccm_number_of_leaf\"] = len(set(map(tuple, colors))) - 1\n", "mccm_rows.append(row)\n", "\n", "row = dict()\n", "row[\"mccm_label\"] = \"stem\"\n", "row[\"mccm_length\"] = numpy.max(v[:, 2]) - numpy.min(v[:, 0])\n", "mccm_rows.append(row)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Measures registration" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def registration_row(pm_rows, sm_rows):\n", " def same_label(pm_row, sm_row):\n", " for label in [\"stem\", \"plant\"]:\n", " if pm_row[\"pm_label\"] == label and sm_row[\"mccm_label\"] == label:\n", " return True\n", " return False\n", "\n", " registered_row = list()\n", " for pm_row in pm_rows:\n", " for sm_row in sm_rows:\n", " if same_label(pm_row, sm_row):\n", " registered_row.append((pm_row, sm_row, 0))\n", " continue\n", " else:\n", " continue\n", "\n", " return registered_row" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "labels = [\"pm_label\", \"pm_length\", \"pm_number_of_leaf\"]\n", "\n", "rows = list()\n", "for pm_row, mccm_row, d in registration_row(pm_rows, mccm_rows):\n", " for label in labels:\n", " if label in pm_row:\n", " mccm_row[label] = pm_row[label]\n", " rows.append(mccm_row)\n", "\n", "df = pandas.DataFrame(rows)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Measures Comparison" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_filtred_label = df[df[\"pm_label\"] == \"stem\"]\n", "plt.plot(df_filtred_label[\"mccm_length\"], df_filtred_label[\"pm_length\"], \"g+\")\n", "df_filtred_label = df[df[\"pm_label\"] == \"plant\"]\n", "plt.plot(\n", " df_filtred_label[\"mccm_number_of_leaf\"], df_filtred_label[\"pm_number_of_leaf\"], \"k+\"\n", ")\n", "\n", "plt.xlabel(\"Phenomenal measurements\")\n", "plt.ylabel(\"McCormik measurements\")\n", "\n", "m = int(max([df[k].max() for k in [\"pm_length\", \"mccm_length\"]]))\n", "\n", "plt.plot(range(m), range(m), \"k--\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }