[dc3c86]: / interpolate-scans.ipynb

Download this file

835 lines (834 with data), 42.8 kB

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interpolate scans so that all have same number of slices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy import ndimage as nd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[01;34m..\u001b[00m\r\n",
      "├── \u001b[01;34mdata\u001b[00m\r\n",
      "│   ├── \u001b[01;34mtrain\u001b[00m\r\n",
      "│   │   ├── \u001b[01;34maxial\u001b[00m\r\n",
      "│   │   ├── \u001b[01;34mcoronal\u001b[00m\r\n",
      "│   │   └── \u001b[01;34msagittal\u001b[00m\r\n",
      "│   └── \u001b[01;34mvalid\u001b[00m\r\n",
      "│       ├── \u001b[01;34maxial\u001b[00m\r\n",
      "│       ├── \u001b[01;34mcoronal\u001b[00m\r\n",
      "│       └── \u001b[01;34msagittal\u001b[00m\r\n",
      "├── \u001b[01;34mdata24\u001b[00m\r\n",
      "│   ├── \u001b[01;34mtrain\u001b[00m\r\n",
      "│   │   ├── \u001b[01;34maxial\u001b[00m\r\n",
      "│   │   ├── \u001b[01;34mcoronal\u001b[00m\r\n",
      "│   │   └── \u001b[01;34msagittal\u001b[00m\r\n",
      "│   └── \u001b[01;34mvalid\u001b[00m\r\n",
      "│       ├── \u001b[01;34maxial\u001b[00m\r\n",
      "│       ├── \u001b[01;34mcoronal\u001b[00m\r\n",
      "│       └── \u001b[01;34msagittal\u001b[00m\r\n",
      "└── \u001b[01;34mmrnet-fastai\u001b[00m\r\n",
      "    ├── \u001b[01;34m__pycache__\u001b[00m\r\n",
      "    ├── \u001b[01;34mexp\u001b[00m\r\n",
      "    ├── \u001b[01;34mmodels\u001b[00m\r\n",
      "    └── \u001b[01;34mpaper-code\u001b[00m\r\n",
      "        └── \u001b[01;34mMRNet-master\u001b[00m\r\n",
      "\r\n",
      "24 directories\r\n"
     ]
    }
   ],
   "source": [
    "! tree -d .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir('../data/train/axial')[0][-4:]=='.npy'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copy data to another directory, for example `/data24`, for 24 slices per scan. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#! cp -R ../data ../data24"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Recurse through /train and /valid directories, load the scan file (.npy), interpolate to 24 slices and overwrite."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 50 .npy files in ../data24/train/axial\n",
      "Converting ../data24/train/axial/0032.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0026.npy\n",
      "Number of slices for this scan: 39\n",
      "Interpolation factors for this scan: [0.6153846153846154, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0027.npy\n",
      "Number of slices for this scan: 22\n",
      "Interpolation factors for this scan: [1.0909090909090908, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0033.npy\n",
      "Number of slices for this scan: 45\n",
      "Interpolation factors for this scan: [0.5333333333333333, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0019.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0025.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0031.npy\n",
      "Number of slices for this scan: 51\n",
      "Interpolation factors for this scan: [0.47058823529411764, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0030.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0024.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0018.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0020.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0034.npy\n",
      "Number of slices for this scan: 39\n",
      "Interpolation factors for this scan: [0.6153846153846154, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0008.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0009.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0035.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0021.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0037.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0023.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0022.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0036.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0045.npy\n",
      "Number of slices for this scan: 46\n",
      "Interpolation factors for this scan: [0.5217391304347826, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0044.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0046.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0047.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0043.npy\n",
      "Number of slices for this scan: 44\n",
      "Interpolation factors for this scan: [0.5454545454545454, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0042.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0040.npy\n",
      "Number of slices for this scan: 29\n",
      "Interpolation factors for this scan: [0.8275862068965517, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0041.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0049.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0048.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0013.npy\n",
      "Number of slices for this scan: 39\n",
      "Interpolation factors for this scan: [0.6153846153846154, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0007.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0006.npy\n",
      "Number of slices for this scan: 29\n",
      "Interpolation factors for this scan: [0.8275862068965517, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0012.npy\n",
      "Number of slices for this scan: 46\n",
      "Interpolation factors for this scan: [0.5217391304347826, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0038.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0004.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0010.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0011.npy\n",
      "Number of slices for this scan: 44\n",
      "Interpolation factors for this scan: [0.5454545454545454, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0005.npy\n",
      "Number of slices for this scan: 41\n",
      "Interpolation factors for this scan: [0.5853658536585366, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0039.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0001.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0015.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0029.npy\n",
      "Number of slices for this scan: 45\n",
      "Interpolation factors for this scan: [0.5333333333333333, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0028.npy\n",
      "Number of slices for this scan: 33\n",
      "Interpolation factors for this scan: [0.7272727272727273, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0014.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0000.npy\n",
      "Number of slices for this scan: 44\n",
      "Interpolation factors for this scan: [0.5454545454545454, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0016.npy\n",
      "Number of slices for this scan: 48\n",
      "Interpolation factors for this scan: [0.5, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0002.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0003.npy\n",
      "Number of slices for this scan: 33\n",
      "Interpolation factors for this scan: [0.7272727272727273, 1.0, 1.0]\n",
      "Converting ../data24/train/axial/0017.npy\n",
      "Number of slices for this scan: 25\n",
      "Interpolation factors for this scan: [0.96, 1.0, 1.0]\n",
      "===============================\n",
      "===============================\n",
      "Found 50 .npy files in ../data24/train/coronal\n",
      "Converting ../data24/train/coronal/0032.npy\n",
      "Number of slices for this scan: 31\n",
      "Interpolation factors for this scan: [0.7741935483870968, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0026.npy\n",
      "Number of slices for this scan: 42\n",
      "Interpolation factors for this scan: [0.5714285714285714, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0027.npy\n",
      "Number of slices for this scan: 28\n",
      "Interpolation factors for this scan: [0.8571428571428571, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0033.npy\n",
      "Number of slices for this scan: 41\n",
      "Interpolation factors for this scan: [0.5853658536585366, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0019.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0025.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0031.npy\n",
      "Number of slices for this scan: 35\n",
      "Interpolation factors for this scan: [0.6857142857142857, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0030.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Converting ../data24/train/coronal/0024.npy\n",
      "Number of slices for this scan: 37\n",
      "Interpolation factors for this scan: [0.6486486486486487, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0018.npy\n",
      "Number of slices for this scan: 22\n",
      "Interpolation factors for this scan: [1.0909090909090908, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0020.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0034.npy\n",
      "Number of slices for this scan: 42\n",
      "Interpolation factors for this scan: [0.5714285714285714, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0008.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0009.npy\n",
      "Number of slices for this scan: 27\n",
      "Interpolation factors for this scan: [0.8888888888888888, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0035.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0021.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0037.npy\n",
      "Number of slices for this scan: 22\n",
      "Interpolation factors for this scan: [1.0909090909090908, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0023.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0022.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0036.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0045.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0044.npy\n",
      "Number of slices for this scan: 21\n",
      "Interpolation factors for this scan: [1.1428571428571428, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0046.npy\n",
      "Number of slices for this scan: 18\n",
      "Interpolation factors for this scan: [1.3333333333333333, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0047.npy\n",
      "Number of slices for this scan: 18\n",
      "Interpolation factors for this scan: [1.3333333333333333, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0043.npy\n",
      "Number of slices for this scan: 46\n",
      "Interpolation factors for this scan: [0.5217391304347826, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0042.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0040.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0041.npy\n",
      "Number of slices for this scan: 37\n",
      "Interpolation factors for this scan: [0.6486486486486487, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0049.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0048.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0013.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0007.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0006.npy\n",
      "Number of slices for this scan: 23\n",
      "Interpolation factors for this scan: [1.0434782608695652, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0012.npy\n",
      "Number of slices for this scan: 41\n",
      "Interpolation factors for this scan: [0.5853658536585366, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0038.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0004.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0010.npy\n",
      "Number of slices for this scan: 42\n",
      "Interpolation factors for this scan: [0.5714285714285714, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0011.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0005.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0039.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0001.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0015.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0029.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0028.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0014.npy\n",
      "Number of slices for this scan: 22\n",
      "Interpolation factors for this scan: [1.0909090909090908, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0000.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0016.npy\n",
      "Number of slices for this scan: 44\n",
      "Interpolation factors for this scan: [0.5454545454545454, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0002.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0003.npy\n",
      "Number of slices for this scan: 28\n",
      "Interpolation factors for this scan: [0.8571428571428571, 1.0, 1.0]\n",
      "Converting ../data24/train/coronal/0017.npy\n",
      "Number of slices for this scan: 25\n",
      "Interpolation factors for this scan: [0.96, 1.0, 1.0]\n",
      "===============================\n",
      "===============================\n",
      "Found 50 .npy files in ../data24/train/sagittal\n",
      "Converting ../data24/train/sagittal/0032.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0026.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0027.npy\n",
      "Number of slices for this scan: 19\n",
      "Interpolation factors for this scan: [1.263157894736842, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0033.npy\n",
      "Number of slices for this scan: 44\n",
      "Interpolation factors for this scan: [0.5454545454545454, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0019.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0025.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0031.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0030.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0024.npy\n",
      "Number of slices for this scan: 33\n",
      "Interpolation factors for this scan: [0.7272727272727273, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0018.npy\n",
      "Number of slices for this scan: 21\n",
      "Interpolation factors for this scan: [1.1428571428571428, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0020.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0034.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0008.npy\n",
      "Number of slices for this scan: 35\n",
      "Interpolation factors for this scan: [0.6857142857142857, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0009.npy\n",
      "Number of slices for this scan: 29\n",
      "Interpolation factors for this scan: [0.8275862068965517, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0035.npy\n",
      "Number of slices for this scan: 33\n",
      "Interpolation factors for this scan: [0.7272727272727273, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0021.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Converting ../data24/train/sagittal/0037.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0023.npy\n",
      "Number of slices for this scan: 33\n",
      "Interpolation factors for this scan: [0.7272727272727273, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0022.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0036.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0045.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0044.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0046.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0047.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0043.npy\n",
      "Number of slices for this scan: 46\n",
      "Interpolation factors for this scan: [0.5217391304347826, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0042.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0040.npy\n",
      "Number of slices for this scan: 27\n",
      "Interpolation factors for this scan: [0.8888888888888888, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0041.npy\n",
      "Number of slices for this scan: 35\n",
      "Interpolation factors for this scan: [0.6857142857142857, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0049.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0048.npy\n",
      "Number of slices for this scan: 28\n",
      "Interpolation factors for this scan: [0.8571428571428571, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0013.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0007.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0006.npy\n",
      "Number of slices for this scan: 28\n",
      "Interpolation factors for this scan: [0.8571428571428571, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0012.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0038.npy\n",
      "Number of slices for this scan: 45\n",
      "Interpolation factors for this scan: [0.5333333333333333, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0004.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0010.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0011.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0005.npy\n",
      "Number of slices for this scan: 37\n",
      "Interpolation factors for this scan: [0.6486486486486487, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0039.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0001.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0015.npy\n",
      "Number of slices for this scan: 33\n",
      "Interpolation factors for this scan: [0.7272727272727273, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0029.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0028.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0014.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0000.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0016.npy\n",
      "Number of slices for this scan: 42\n",
      "Interpolation factors for this scan: [0.5714285714285714, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0002.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0003.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/train/sagittal/0017.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "===============================\n",
      "===============================\n",
      "Found 20 .npy files in ../data24/valid/axial\n",
      "Converting ../data24/valid/axial/1139.npy\n",
      "Number of slices for this scan: 33\n",
      "Interpolation factors for this scan: [0.7272727272727273, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1138.npy\n",
      "Number of slices for this scan: 28\n",
      "Interpolation factors for this scan: [0.8571428571428571, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1148.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1149.npy\n",
      "Number of slices for this scan: 29\n",
      "Interpolation factors for this scan: [0.8275862068965517, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1144.npy\n",
      "Number of slices for this scan: 42\n",
      "Interpolation factors for this scan: [0.5714285714285714, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1145.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1147.npy\n",
      "Number of slices for this scan: 29\n",
      "Interpolation factors for this scan: [0.8275862068965517, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1146.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1142.npy\n",
      "Number of slices for this scan: 37\n",
      "Interpolation factors for this scan: [0.6486486486486487, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1143.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1141.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1140.npy\n",
      "Number of slices for this scan: 42\n",
      "Interpolation factors for this scan: [0.5714285714285714, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1133.npy\n",
      "Number of slices for this scan: 25\n",
      "Interpolation factors for this scan: [0.96, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1132.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1130.npy\n",
      "Number of slices for this scan: 25\n",
      "Interpolation factors for this scan: [0.96, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1131.npy\n",
      "Number of slices for this scan: 28\n",
      "Interpolation factors for this scan: [0.8571428571428571, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1135.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1134.npy\n",
      "Number of slices for this scan: 31\n",
      "Interpolation factors for this scan: [0.7741935483870968, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1136.npy\n",
      "Number of slices for this scan: 52\n",
      "Interpolation factors for this scan: [0.46153846153846156, 1.0, 1.0]\n",
      "Converting ../data24/valid/axial/1137.npy\n",
      "Number of slices for this scan: 23\n",
      "Interpolation factors for this scan: [1.0434782608695652, 1.0, 1.0]\n",
      "===============================\n",
      "===============================\n",
      "Found 20 .npy files in ../data24/valid/coronal\n",
      "Converting ../data24/valid/coronal/1139.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1138.npy\n",
      "Number of slices for this scan: 17\n",
      "Interpolation factors for this scan: [1.411764705882353, 1.0, 1.0]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Converting ../data24/valid/coronal/1148.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1149.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1144.npy\n",
      "Number of slices for this scan: 35\n",
      "Interpolation factors for this scan: [0.6857142857142857, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1145.npy\n",
      "Number of slices for this scan: 42\n",
      "Interpolation factors for this scan: [0.5714285714285714, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1147.npy\n",
      "Number of slices for this scan: 20\n",
      "Interpolation factors for this scan: [1.2, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1146.npy\n",
      "Number of slices for this scan: 21\n",
      "Interpolation factors for this scan: [1.1428571428571428, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1142.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1143.npy\n",
      "Number of slices for this scan: 36\n",
      "Interpolation factors for this scan: [0.6666666666666666, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1141.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1140.npy\n",
      "Number of slices for this scan: 39\n",
      "Interpolation factors for this scan: [0.6153846153846154, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1133.npy\n",
      "Number of slices for this scan: 22\n",
      "Interpolation factors for this scan: [1.0909090909090908, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1132.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1130.npy\n",
      "Number of slices for this scan: 25\n",
      "Interpolation factors for this scan: [0.96, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1131.npy\n",
      "Number of slices for this scan: 22\n",
      "Interpolation factors for this scan: [1.0909090909090908, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1135.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1134.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1136.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/valid/coronal/1137.npy\n",
      "Number of slices for this scan: 21\n",
      "Interpolation factors for this scan: [1.1428571428571428, 1.0, 1.0]\n",
      "===============================\n",
      "===============================\n",
      "Found 20 .npy files in ../data24/valid/sagittal\n",
      "Converting ../data24/valid/sagittal/1139.npy\n",
      "Number of slices for this scan: 27\n",
      "Interpolation factors for this scan: [0.8888888888888888, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1138.npy\n",
      "Number of slices for this scan: 22\n",
      "Interpolation factors for this scan: [1.0909090909090908, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1148.npy\n",
      "Number of slices for this scan: 22\n",
      "Interpolation factors for this scan: [1.0909090909090908, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1149.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1144.npy\n",
      "Number of slices for this scan: 38\n",
      "Interpolation factors for this scan: [0.631578947368421, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1145.npy\n",
      "Number of slices for this scan: 40\n",
      "Interpolation factors for this scan: [0.6, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1147.npy\n",
      "Number of slices for this scan: 26\n",
      "Interpolation factors for this scan: [0.9230769230769231, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1146.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1142.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1143.npy\n",
      "Number of slices for this scan: 34\n",
      "Interpolation factors for this scan: [0.7058823529411765, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1141.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1140.npy\n",
      "Number of slices for this scan: 41\n",
      "Interpolation factors for this scan: [0.5853658536585366, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1133.npy\n",
      "Number of slices for this scan: 21\n",
      "Interpolation factors for this scan: [1.1428571428571428, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1132.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1130.npy\n",
      "Number of slices for this scan: 27\n",
      "Interpolation factors for this scan: [0.8888888888888888, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1131.npy\n",
      "Number of slices for this scan: 23\n",
      "Interpolation factors for this scan: [1.0434782608695652, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1135.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1134.npy\n",
      "Number of slices for this scan: 30\n",
      "Interpolation factors for this scan: [0.8, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1136.npy\n",
      "Number of slices for this scan: 32\n",
      "Interpolation factors for this scan: [0.75, 1.0, 1.0]\n",
      "Converting ../data24/valid/sagittal/1137.npy\n",
      "Number of slices for this scan: 24\n",
      "Interpolation factors for this scan: [1.0, 1.0, 1.0]\n",
      "===============================\n",
      "===============================\n"
     ]
    }
   ],
   "source": [
    "slices = 24\n",
    "for d in ('train','valid'):\n",
    "    for p in ('axial','coronal','sagittal'):\n",
    "        dirpath = '../data{}/{}/{}'.format(slices, d, p)\n",
    "        npy_files = [f for f in os.listdir(dirpath) if f[-4:]=='.npy']\n",
    "        print('Found {} .npy files in {}'.format(len(npy_files), dirpath))\n",
    "\n",
    "        for f in npy_files:\n",
    "            filepath = dirpath + '/' + f\n",
    "            print('Converting {}'.format(filepath))\n",
    "            # load the array\n",
    "            scanarray = np.load(filepath)\n",
    "            # calculate interpolation factor(s)\n",
    "            print('Number of slices for this scan: {}'.format(scanarray.shape[0]))\n",
    "            interpolation_factors = [w/float(f) for w,f in zip([slices,256,256], scanarray.shape)]\n",
    "            print('Interpolation factors for this scan: {}'.format(interpolation_factors))\n",
    "            # interpolate/zoom\n",
    "            interpolated_scan = nd.interpolation.zoom(scanarray, zoom=interpolation_factors)\n",
    "            assert interpolated_scan.shape == (slices, 256, 256)\n",
    "            # write over old data (since working with copy of data)\n",
    "            np.save(filepath, interpolated_scan)\n",
    "        \n",
    "        print('===============================')\n",
    "        print('===============================')        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}