Switch to unified view

a b/.ipynb_checkpoints/Untitled-Copy1-checkpoint.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 5,
6
   "metadata": {},
7
   "outputs": [],
8
   "source": [
9
    "import os\n",
10
    "import h5py\n",
11
    "import imageio\n",
12
    "import numpy as np\n",
13
    "import pandas as pd\n",
14
    "import nibabel as nib\n",
15
    "import seaborn as sns\n",
16
    "from utils import metrics\n",
17
    "from data import base_dataset"
18
   ]
19
  },
20
  {
21
   "cell_type": "code",
22
   "execution_count": 2,
23
   "metadata": {},
24
   "outputs": [],
25
   "source": [
26
    "from utils import visualizer\n",
27
    "import matplotlib.pylab as plt\n",
28
    "from scipy.ndimage.measurements import center_of_mass"
29
   ]
30
  },
31
  {
32
   "cell_type": "code",
33
   "execution_count": 3,
34
   "metadata": {},
35
   "outputs": [],
36
   "source": [
37
    "from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas\n",
38
    "from matplotlib.figure import Figure"
39
   ]
40
  },
41
  {
42
   "cell_type": "code",
43
   "execution_count": null,
44
   "metadata": {},
45
   "outputs": [],
46
   "source": []
47
  },
48
  {
49
   "cell_type": "code",
50
   "execution_count": 10,
51
   "metadata": {},
52
   "outputs": [
53
    {
54
     "name": "stdout",
55
     "output_type": "stream",
56
     "text": [
57
      "DM 0.27433953948483814 -0.17179240013074187\n"
58
     ]
59
    }
60
   ],
61
   "source": [
62
    "Clinical_params = pd.DataFrame({'Group':[], 'SubjectID':[], 'rESS':[], 'cESS':[],\n",
63
    "                                'RV_EDV_ml':[], 'RV_ESV_ml':[], 'RV_EF':[], \n",
64
    "                                'LV_EDV_ml':[], 'LV_ESV_ml':[], 'LV_EF':[], 'LV_mass_g':[], 'Frame':[]})\n",
65
    "\n",
66
    "\n",
67
    "df = pd.read_csv('../private_data/MARTINOS.csv', index_col=0)\n",
68
    "\n",
69
    "Err = []\n",
70
    "Ecc = []\n",
71
    "for subject_index in df.index: \n",
72
    "    group = df.iloc[subject_index].Group\n",
73
    "    sid   = df.iloc[subject_index].SubjectID\n",
74
    "    \n",
75
    "    if group == 'DM':\n",
76
    "        M_nifti = nib.load('../private_data_results/MARTINOS/MSTAT_DM_VOL%d_V1_segmentation.nii'%(sid))\n",
77
    "        u_HF = h5py.File('../private_data_results/MARTINOS/MSTAT_DM_VOL%d_V1_motion.h5'%(sid), 'r')\n",
78
    "    elif sid > 100:\n",
79
    "        M_nifti = nib.load('../private_data_results/MARTINOS/MSTAT_%d_V2_segmentation.nii'%(sid))\n",
80
    "        u_HF = h5py.File('../private_data_results/MARTINOS/MSTAT_%d_V2_motion.h5'%(sid), 'r')\n",
81
    "    else:\n",
82
    "        try:\n",
83
    "            M_nifti = nib.load('../private_data_results/MARTINOS/MSTAT_VOL%d_V1_segmentation.nii'%(sid))\n",
84
    "            u_HF = h5py.File('../private_data_results/MARTINOS/MSTAT_VOL%d_V1_motion.h5'%(sid), 'r')\n",
85
    "        except:\n",
86
    "            M_nifti = nib.load('../private_data_results/MARTINOS/MSTAT_VOL%d_V2_segmentation.nii'%(sid))\n",
87
    "            u_HF = h5py.File('../private_data_results/MARTINOS/MSTAT_VOL%d_V2_motion.h5'%(sid), 'r')\n",
88
    "    \n",
89
    "    \n",
90
    "    M_nifti = nifti_dataset.resample_nifti(M_nifti, in_plane_resolution_mm=1.25, number_of_slices=16)\n",
91
    "    \n",
92
    "    m  = M_nifti.get_fdata()\n",
93
    "    u  = load_HF(u_HF)\n",
94
    "    \n",
95
    "    center = center_of_mass(m[:,:,:,0]==3)\n",
96
    "    \n",
97
    "    u=base_dataset._roll2center_crop(u,center)\n",
98
    "    m=base_dataset._roll2center_crop(m,center)\n",
99
    "    \n",
100
    "    esid = (m[:,:,3:-3]==3).sum(axis=(0,1,2)).argmin()\n",
101
    "    \n",
102
    "    \n",
103
    "    for t in [esid]:\n",
104
    "        # CALCULATE PARAMS AT END-SYSTOLE ONLY\n",
105
    "        \n",
106
    "        E = strain.MyocardialStrain(m[:,:,:,0],u[:,:,:,:,t])\n",
107
    "        E.calculate_strain()\n",
108
    "\n",
109
    "        rESS = E.Err[E.mask_rot==2].mean()\n",
110
    "        cESS = E.Ecc[E.mask_rot==2].mean()\n",
111
    "        \n",
112
    "        print(group, rESS, cESS)\n",
113
    "\n",
114
    "        \n",
115
    "    break"
116
   ]
117
  },
118
  {
119
   "cell_type": "code",
120
   "execution_count": null,
121
   "metadata": {},
122
   "outputs": [],
123
   "source": []
124
  },
125
  {
126
   "cell_type": "code",
127
   "execution_count": 11,
128
   "metadata": {},
129
   "outputs": [],
130
   "source": [
131
    "import numpy as np\n",
132
    "from scipy.ndimage import rotate\n",
133
    "from scipy.ndimage import gaussian_filter\n",
134
    "from scipy.interpolate import interp1d, interp2d\n",
135
    "from scipy.ndimage.measurements import center_of_mass\n",
136
    "\n",
137
    "class MyocardialStrain():\n",
138
    "    \n",
139
    "    def __init__(self, mask, flow):\n",
140
    "                \n",
141
    "        self.mask  = mask\n",
142
    "        self.flow  = flow\n",
143
    "        \n",
144
    "        assert len(mask.shape) == 3\n",
145
    "        assert len(flow.shape) == 4\n",
146
    "        assert mask.shape == flow.shape[:3]\n",
147
    "        assert flow.shape[-1] == 3\n",
148
    "        \n",
149
    "    def calculate_strain(self, dx=1, dy=1, dz=1, lv_label=3):\n",
150
    "        \n",
151
    "        cx, cy, cz = center_of_mass(self.mask==lv_label)\n",
152
    "        nx, ny, nz = self.mask.shape\n",
153
    "        \n",
154
    "        self.flow_rot = _roll_to_center(self.flow, cx, cy)\n",
155
    "        self.mask_rot = _roll_to_center(self.mask, cx, cy)\n",
156
    "\n",
157
    "        ux, uy, uz  = np.array_split(self.flow_rot, 3, -1)\n",
158
    "        Uxx,Uxy,Uxz = np.gradient(np.squeeze(ux),dx,dy,dz)\n",
159
    "        Uyx,Uyy,Uyz = np.gradient(np.squeeze(uy),dx,dy,dz)\n",
160
    "        Uzx,Uzy,Uzz = np.gradient(np.squeeze(uz),dx,dy,dz)\n",
161
    "\n",
162
    "        self.E_cart = np.zeros((nx,ny,nz,3,3))\n",
163
    "        for i in range(nx):\n",
164
    "            for j in range(ny):\n",
165
    "                for k in range(nz):\n",
166
    "                    Ugrad = [[Uxx[i,j,k], Uxy[i,j,k], Uxz[i,j,k]], \n",
167
    "                             [Uyx[i,j,k], Uyy[i,j,k], Uyz[i,j,k]],\n",
168
    "                             [Uzx[i,j,k], Uzy[i,j,k], Uzz[i,j,k]]]\n",
169
    "                    F = np.array(Ugrad) + np.identity(3)\n",
170
    "                    e = 0.5*(np.matmul(F.T, F) - np.identity(3))\n",
171
    "                    self.E_cart[i,j,k] += e\n",
172
    "\n",
173
    "        self.Ezz = self.E_cart[:,:,:,2,2]\n",
174
    "        self.Err, self.Ecc = self._convert_to_polar(self.E_cart[:,:,:,:2,:2])\n",
175
    "\n",
176
    "        \n",
177
    "        \n",
178
    "        \n",
179
    "    def _convert_to_polar(self, E):\n",
180
    "\n",
181
    "        phi = _polar_grid(*E.shape[:2])[0]\n",
182
    "        Err = np.zeros(self.mask.shape)\n",
183
    "        Ecc = np.zeros(self.mask.shape)\n",
184
    "        for k in range(self.mask.shape[-1]):\n",
185
    "            cos = np.cos(np.deg2rad(phi))\n",
186
    "            sin = np.sin(np.deg2rad(phi))\n",
187
    "        \n",
188
    "            Exx, Exy, Eyx, Eyy = E[:,:,k,0,0],E[:,:,k,0,1],E[:,:,k,1,0],E[:,:,k,1,1]\n",
189
    "            Err[:,:,k] +=  cos*( cos*Exx+sin*Exy) +sin*( cos*Eyx+sin*Eyy)\n",
190
    "            Ecc[:,:,k] += -sin*(-sin*Exx+cos*Exy) +cos*(-sin*Eyx+cos*Eyy)\n",
191
    "\n",
192
    "        return Err, Ecc\n",
193
    "    \n",
194
    "   \n",
195
    "\n",
196
    "def _roll(x, rx, ry):\n",
197
    "    x = np.roll(x, rx, axis=0)\n",
198
    "    return np.roll(x, ry, axis=1)\n",
199
    "\n",
200
    "def _roll_to_center(x, cx, cy):\n",
201
    "    nx, ny = x.shape[:2]\n",
202
    "    return _roll(x,  int(nx//2-cx), int(ny//2-cy))\n",
203
    "\n",
204
    "def _polar_grid(nx=128, ny=128):\n",
205
    "    x, y = np.meshgrid(np.linspace(-nx//2, nx//2, nx), np.linspace(-ny//2, ny//2, ny))\n",
206
    "    phi  = (np.rad2deg(np.arctan2(y, x)) + 180).T\n",
207
    "    r    = np.sqrt(x**2+y**2+1e-8)\n",
208
    "    return phi, r"
209
   ]
210
  },
211
  {
212
   "cell_type": "code",
213
   "execution_count": 12,
214
   "metadata": {},
215
   "outputs": [
216
    {
217
     "name": "stdout",
218
     "output_type": "stream",
219
     "text": [
220
      "DM 0.27433953948483814 -0.17179240013074187\n"
221
     ]
222
    }
223
   ],
224
   "source": [
225
    "E = MyocardialStrain(m[:,:,:,0],u[:,:,:,:,t])\n",
226
    "E.calculate_strain()\n",
227
    "\n",
228
    "rESS = E.Err[E.mask_rot==2].mean()\n",
229
    "cESS = E.Ecc[E.mask_rot==2].mean()\n",
230
    "\n",
231
    "print(group, rESS, cESS)"
232
   ]
233
  },
234
  {
235
   "cell_type": "code",
236
   "execution_count": 13,
237
   "metadata": {},
238
   "outputs": [],
239
   "source": [
240
    "mask = m[:,:,:,0]\n",
241
    "flow = u[:,:,:,:,t]"
242
   ]
243
  },
244
  {
245
   "cell_type": "code",
246
   "execution_count": 19,
247
   "metadata": {},
248
   "outputs": [],
249
   "source": [
250
    "lv_label = 3\n",
251
    "\n",
252
    "cx, cy, cz = center_of_mass(mask==lv_label)\n",
253
    "nx, ny, nz = mask.shape\n",
254
    "\n",
255
    "cx, cy, cz\n",
256
    "\n",
257
    "flow_rot = _roll_to_center(flow, cx, cy)\n",
258
    "mask_rot = _roll_to_center(mask, cx, cy)\n",
259
    "\n",
260
    "dx=1; dy=1; dz=1\n",
261
    "\n",
262
    "ux, uy, uz  = np.array_split(flow_rot, 3, -1)\n",
263
    "Uxx,Uxy,Uxz = np.gradient(np.squeeze(ux),dx,dy,dz)\n",
264
    "Uyx,Uyy,Uyz = np.gradient(np.squeeze(uy),dx,dy,dz)\n",
265
    "Uzx,Uzy,Uzz = np.gradient(np.squeeze(uz),dx,dy,dz)"
266
   ]
267
  },
268
  {
269
   "cell_type": "code",
270
   "execution_count": 53,
271
   "metadata": {},
272
   "outputs": [],
273
   "source": [
274
    "def_grad = np.array([[Uxx,Uxy,Uxz],\n",
275
    "                     [Uyx,Uyy,Uyz],\n",
276
    "                     [Uzx,Uzy,Uzz]])"
277
   ]
278
  },
279
  {
280
   "cell_type": "code",
281
   "execution_count": 54,
282
   "metadata": {},
283
   "outputs": [],
284
   "source": [
285
    "I = np.identity(3)[:,:,None,None,None]\n",
286
    "I = np.repeat(I,repeats=128, axis=2)\n",
287
    "I = np.repeat(I,repeats=128, axis=3)\n",
288
    "I = np.repeat(I,repeats=16, axis=4)"
289
   ]
290
  },
291
  {
292
   "cell_type": "code",
293
   "execution_count": 62,
294
   "metadata": {},
295
   "outputs": [],
296
   "source": [
297
    "F = def_grad + I"
298
   ]
299
  },
300
  {
301
   "cell_type": "code",
302
   "execution_count": null,
303
   "metadata": {},
304
   "outputs": [],
305
   "source": []
306
  },
307
  {
308
   "cell_type": "code",
309
   "execution_count": null,
310
   "metadata": {},
311
   "outputs": [],
312
   "source": []
313
  },
314
  {
315
   "cell_type": "code",
316
   "execution_count": 64,
317
   "metadata": {},
318
   "outputs": [],
319
   "source": [
320
    "for i in range(nx):\n",
321
    "    for j in range(ny):\n",
322
    "        for k in range(nz):\n",
323
    "            \n",
324
    "            Ugrad = [[Uxx[i,j,k], Uxy[i,j,k], Uxz[i,j,k]], \n",
325
    "                     [Uyx[i,j,k], Uyy[i,j,k], Uyz[i,j,k]],\n",
326
    "                     [Uzx[i,j,k], Uzy[i,j,k], Uzz[i,j,k]]]\n",
327
    "            \n",
328
    "            Fijk = np.array(Ugrad) + np.identity(3)\n",
329
    "            \n",
330
    "            assert (Fijk == F[:,:,i,j,k]).all()"
331
   ]
332
  },
333
  {
334
   "cell_type": "code",
335
   "execution_count": 58,
336
   "metadata": {},
337
   "outputs": [
338
    {
339
     "data": {
340
      "text/plain": [
341
       "array([[1., 0., 0.],\n",
342
       "       [0., 1., 0.],\n",
343
       "       [0., 0., 1.]])"
344
      ]
345
     },
346
     "execution_count": 58,
347
     "metadata": {},
348
     "output_type": "execute_result"
349
    }
350
   ],
351
   "source": [
352
    "Fijk"
353
   ]
354
  },
355
  {
356
   "cell_type": "code",
357
   "execution_count": 59,
358
   "metadata": {},
359
   "outputs": [
360
    {
361
     "data": {
362
      "text/plain": [
363
       "array([[0., 0., 0.],\n",
364
       "       [0., 0., 0.],\n",
365
       "       [0., 0., 0.]])"
366
      ]
367
     },
368
     "execution_count": 59,
369
     "metadata": {},
370
     "output_type": "execute_result"
371
    }
372
   ],
373
   "source": [
374
    "F[:,:,i,j,k]"
375
   ]
376
  },
377
  {
378
   "cell_type": "code",
379
   "execution_count": null,
380
   "metadata": {},
381
   "outputs": [],
382
   "source": [
383
    "F = np.array(Ugrad) + np.identity(3)\n",
384
    "            e = 0.5*(np.matmul(F.T, F) - np.identity(3))\n",
385
    "            self.E_cart[i,j,k] += e\n"
386
   ]
387
  },
388
  {
389
   "cell_type": "code",
390
   "execution_count": 35,
391
   "metadata": {},
392
   "outputs": [],
393
   "source": [
394
    "np.repeat?"
395
   ]
396
  },
397
  {
398
   "cell_type": "code",
399
   "execution_count": null,
400
   "metadata": {},
401
   "outputs": [],
402
   "source": []
403
  },
404
  {
405
   "cell_type": "code",
406
   "execution_count": 7,
407
   "metadata": {},
408
   "outputs": [],
409
   "source": [
410
    "# Manuel A. Morales (moralesq@mit.edu)\n",
411
    "# Harvard-MIT Department of Health Sciences & Technology  \n",
412
    "# Athinoula A. Martinos Center for Biomedical Imaging\n",
413
    "\n",
414
    "import os\n",
415
    "import h5py\n",
416
    "import glob\n",
417
    "import warnings\n",
418
    "import numpy as np\n",
419
    "import nibabel as nib\n",
420
    "\n",
421
    "from dipy.align.reslice import reslice\n",
422
    "from data.base_dataset import BaseDataset, Transforms\n",
423
    "from data.image_folder import make_dataset\n",
424
    "\n",
425
    "\n",
426
    "class H5PYDataset(BaseDataset):\n",
427
    "\n",
428
    "    def __init__(self, opt):\n",
429
    "        BaseDataset.__init__(self, opt)\n",
430
    "        self.filenames = sorted(make_dataset(opt.dataroot, opt.max_dataset_size, 'H5PY'))\n",
431
    "    \n",
432
    "    def __len__(self):\n",
433
    "        return len(self.filenames)\n",
434
    "                \n",
435
    "    def __getitem__(self, idx):      \n",
436
    "\n",
437
    "        HF = h5py.File(self.filenames[idx], 'r')\n",
438
    "        "
439
   ]
440
  },
441
  {
442
   "cell_type": "code",
443
   "execution_count": 8,
444
   "metadata": {},
445
   "outputs": [],
446
   "source": [
447
    "import h5py\n",
448
    "from utils import strain\n",
449
    "from data import nifti_dataset"
450
   ]
451
  },
452
  {
453
   "cell_type": "code",
454
   "execution_count": 9,
455
   "metadata": {},
456
   "outputs": [],
457
   "source": [
458
    "def load_HF(HF):\n",
459
    "    output = []\n",
460
    "    for frame_id in range(len(HF.keys())):\n",
461
    "        key = 'frame_%d'%(frame_id)\n",
462
    "        for subkey in HF[key].keys():\n",
463
    "            output += [np.array(HF[key][subkey])]\n",
464
    "\n",
465
    "    HF.close()\n",
466
    "    return np.stack(output,-1)"
467
   ]
468
  },
469
  {
470
   "cell_type": "code",
471
   "execution_count": null,
472
   "metadata": {},
473
   "outputs": [],
474
   "source": []
475
  },
476
  {
477
   "cell_type": "code",
478
   "execution_count": null,
479
   "metadata": {},
480
   "outputs": [],
481
   "source": []
482
  }
483
 ],
484
 "metadata": {
485
  "kernelspec": {
486
   "display_name": "Python 3",
487
   "language": "python",
488
   "name": "python3"
489
  },
490
  "language_info": {
491
   "codemirror_mode": {
492
    "name": "ipython",
493
    "version": 3
494
   },
495
   "file_extension": ".py",
496
   "mimetype": "text/x-python",
497
   "name": "python",
498
   "nbconvert_exporter": "python",
499
   "pygments_lexer": "ipython3",
500
   "version": "3.6.9"
501
  }
502
 },
503
 "nbformat": 4,
504
 "nbformat_minor": 2
505
}