Skip to content

Commit

Permalink
BUG: Example notebook fixups
Browse files Browse the repository at this point in the history
- Updates kernel to "Python 3" default
- Updates notebook with Pythonic styling and removes unused logic
- Styles notebook with "black"
  • Loading branch information
tbirdso committed Jan 25, 2023
1 parent e597c84 commit f963101
Showing 1 changed file with 61 additions and 91 deletions.
152 changes: 61 additions & 91 deletions examples/itkPolyDataToMeshFilter.ipynb
Expand Up @@ -35,46 +35,68 @@
"metadata": {},
"outputs": [],
"source": [
"MESH_FILE = 'Input/cow.vtk'\n",
"MESH_FILE = \"Input/cow.vtk\"\n",
"\n",
"# Download mesh\n",
"os.makedirs('Input', exist_ok=True)\n",
"os.makedirs(\"Input\", exist_ok=True)\n",
"if not os.path.exists(MESH_FILE):\n",
" url = 'https://data.kitware.com/api/v1/file/5c43feba8d777f072b0cd3da/download'\n",
" url = \"https://data.kitware.com/api/v1/file/5c43feba8d777f072b0cd3da/download\"\n",
" urlretrieve(url, MESH_FILE)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"mesh_input = itk.meshread(MESH_FILE, itk.D)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2903\n",
"3263\n"
"Mesh (0000026EE123F310)\n",
" RTTI typeinfo: class itk::Mesh<double,3,class itk::DefaultStaticMeshTraits<double,3,3,float,float,double> >\n",
" Reference Count: 1\n",
" Modified Time: 28464\n",
" Debug: Off\n",
" Object Name: \n",
" Observers: \n",
" none\n",
" Source: (none)\n",
" Source output name: (none)\n",
" Release Data: Off\n",
" Data Released: False\n",
" Global Release Data: Off\n",
" PipelineMTime: 19016\n",
" UpdateMTime: 28463\n",
" RealTimeStamp: 0 seconds \n",
" Number Of Points: 2903\n",
" Requested Number Of Regions: 1\n",
" Requested Region: 0\n",
" Buffered Region: 0\n",
" Maximum Number Of Regions: 1\n",
" Point Data Container pointer: 0000000000000000\n",
" Size of Point Data Container: 0\n",
" Number Of Points: 2903\n",
" Number Of Cell Links: 0\n",
" Number Of Cells: 3263\n",
" Cell Data Container pointer: 0000000000000000\n",
" Size of Cell Data Container: 0\n",
" Number of explicit cell boundary assignments: 3\n",
" CellsAllocationMethod: itk::MeshEnums::MeshClassCellsAllocationMethod::CellsAllocatedDynamicallyCellByCell\n",
"\n"
]
}
],
"source": [
"print(mesh_input.GetNumberOfPoints())\n",
"print(mesh_input.GetNumberOfCells())"
"mesh_input = itk.meshread(MESH_FILE, itk.D)\n",
"\n",
"assert type(mesh_input) == itk.Mesh[itk.D, 3]\n",
"print(mesh_input)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -90,20 +112,20 @@
}
],
"source": [
"# Visualize points\n",
"for i in range(0,5):\n",
"# Inspect a few points\n",
"for i in range(0, 5):\n",
" print(mesh_input.GetPoints().GetElement(i))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9a86a686b49a44bb982404601090aa47",
"model_id": "dd14dc2d3e1942c48e154f89573a872d",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -119,64 +141,28 @@
"view(geometries=[mesh_input])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Test preservation of point data\n",
"mesh_input.GetPointData().Reserve(1)\n",
"mesh_input.GetPointData().SetElement(0,500) #FIXME remove"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Convert to `itk.PolyData`"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"filter = itk.MeshToPolyDataFilter[type(mesh_input)].New(Input=mesh_input)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"filter.Update()"
"poly_data = itk.mesh_to_poly_data_filter(mesh_input)\n",
"\n",
"assert type(poly_data) == itk.PolyData[itk.D]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"poly_data = filter.GetOutput()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"assert(type(poly_data) == itk.PolyData[itk.D])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -407,7 +393,7 @@
"polygons = poly_data.GetPolygons()\n",
"while i < 1000:\n",
" x = list()\n",
" for j in range(0,polygons.ElementAt(i)):\n",
" for j in range(0, polygons.ElementAt(i)):\n",
" x.append(polygons.ElementAt(i + 1 + j))\n",
" print(x)\n",
" i += polygons.ElementAt(i) + 1"
Expand All @@ -422,54 +408,38 @@
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"filter = itk.PolyDataToMeshFilter[type(poly_data)].New(Input=poly_data)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"filter.Update()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"mesh_output = filter.GetOutput()"
"mesh_output = itk.poly_data_to_mesh_filter(poly_data)\n",
"\n",
"assert type(mesh_output) == itk.Mesh[itk.D, 3]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 13,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"assert(mesh_output.GetNumberOfPoints() == mesh_input.GetNumberOfPoints())\n",
"assert(mesh_output.GetNumberOfCells() == mesh_input.GetNumberOfCells())\n",
"for i in range(0,mesh_output.GetNumberOfPoints()):\n",
" assert(mesh_output.GetPoint(i) == mesh_input.GetPoint(i))"
"assert mesh_output.GetNumberOfPoints() == mesh_input.GetNumberOfPoints()\n",
"assert mesh_output.GetNumberOfCells() == mesh_input.GetNumberOfCells()\n",
"for i in range(0, mesh_output.GetNumberOfPoints()):\n",
" assert mesh_output.GetPoint(i) == mesh_input.GetPoint(i)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "de7f8f07558e443392b3dbd6859b2ef0",
"model_id": "71ab9ed224294f46a4ecc1711b479cea",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -488,9 +458,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "venv-itk4",
"display_name": "Python 3",
"language": "python",
"name": "venv-itk4"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down

0 comments on commit f963101

Please sign in to comment.