[ab7503]: / testing / scratch / python39_BoneMesh_create_mesh_test.ipynb

Download this file

119 lines (118 with data), 2.7 kB

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pytest\n",
    "import SimpleITK as sitk\n",
    "\n",
    "import pymskt as mskt\n",
    "from pymskt.utils import testing\n",
    "\n",
    "SEG_IMAGE_PATH = \"../../data/right_knee_example.nrrd\"\n",
    "SEG_IMAGE = sitk.ReadImage(SEG_IMAGE_PATH)\n",
    "MESH_FEMUR_CROPPED = mskt.mesh.io.read_vtk(\"../../data/femur_cropped_cartilage_thick_roi_full_pts.vtk\")\n",
    "MESH_TIBIA_CROPPED = mskt.mesh.io.read_vtk(\"../../data/tibia_smoothed_image_cropped.vtk\")\n",
    "\n",
    "from pymskt import ATOL, RTOL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "seg_image=SEG_IMAGE\n",
    "ref_mesh=MESH_FEMUR_CROPPED\n",
    "label_idx=5\n",
    "crop_percent=0.7\n",
    "bone=\"femur\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "seg_array = sitk.GetArrayFromImage(seg_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(2332148)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(seg_array == label_idx)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING: Mesh is now synonymous with pyvista.PolyData and thus this property is redundant and the Mesh object can be used for anything that pyvista.PolyData or vtk.vtkPolyData can be used for.\n"
     ]
    }
   ],
   "source": [
    "mesh = mskt.mesh.BoneMesh(seg_image=seg_image, label_idx=label_idx, bone=bone)\n",
    "mesh.create_mesh(\n",
    "    smooth_image=True,\n",
    "    crop_percent=crop_percent,\n",
    ")\n",
    "\n",
    "testing.assert_mesh_coordinates_same(ref_mesh, mesh.mesh, rtol=RTOL, atol=ATOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pymskt_3_9",
   "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.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}