{"cells":[{"metadata":{"_uuid":"8486fdd6aa12fd468a67cdce5bb027a5a616ce52"},"cell_type":"markdown","source":"# Overview\nShow the results from the preprocessing step as images and animated gifs to make sure the data were processed correctly."},{"metadata":{"trusted":true,"_uuid":"9bbb12914a57854bf1d3c38d5f750ba729d19892"},"cell_type":"code","source":"%matplotlib inline\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom cv2 import imread, createCLAHE # read and equalize images\nfrom glob import glob\nimport matplotlib.pyplot as plt\nimport h5py","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1670cf154329c72de27e05532f39b1ce55a20bd8"},"cell_type":"code","source":"data_path = os.path.join('..', 'input', 'mri-heart-processing', 'train_mri_128_128.h5')\n# show what is inside\nwith h5py.File(data_path, 'r') as h5_data:\n    for c_key in h5_data.keys():\n        print(c_key, h5_data[c_key].shape, h5_data[c_key].dtype)\n    cur_images = h5_data['image'][0:10]","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"4a62ae3fc03d0f7ad0a0f6b7892d0e15d924ac61"},"cell_type":"code","source":"import matplotlib.animation as animation\nWriter = animation.writers['imagemagick']\nwriter = Writer(fps=10, metadata=dict(artist='Me'), bitrate=1800)","execution_count":null,"outputs":[]},{"metadata":{"scrolled":false,"trusted":true,"_uuid":"6d26500c529c42de0ff7d4071f494ab4de21765f"},"cell_type":"code","source":"from tqdm import tqdm\nfor ind in tqdm(range(cur_images.shape[0])):\n    ims = []\n    temp_stack = cur_images[ind,:,:,:]\n    plt.close('all')\n    fig, ax1 = plt.subplots(1,1, figsize = (8, 8))\n    c_aximg = ax1.imshow(temp_stack[0], cmap='bone', interpolation='lanczos', animated = True)\n    ax1.axis('off')\n    plt.tight_layout()\n    def update_image(frame):\n        c_aximg.set_array(temp_stack[frame])\n        return c_aximg,\n    im_ani = animation.FuncAnimation(fig, update_image, \n                                     frames = range(temp_stack.shape[0]),\n                                     interval=50, repeat_delay=300,\n                                    blit=True)\n    im_ani.save('hr_%03d.gif' % (ind), writer=writer)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"7cf79f58383df9550144e4d8c5ecac4a7522ae53"},"cell_type":"code","source":"from skimage.segmentation import slic\ndef tslic(tstack, numSegments = 200):\n    return slic(tstack, \n                n_segments = numSegments,\n                compactness = 5e-4,  \n                spacing = (1,1,0.1), \n                enforce_connectivity = True,  \n                multichannel = False,\n                sigma = (0.5,0.5,1))","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"d49bbfad74da4f63050aa5bdce6a1f9850551169"},"cell_type":"code","source":"n_stck = tslic(temp_stack)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80dfa3760abd557fab068bbe75f09d232dee8f58"},"cell_type":"code","source":"plt.close('all')\nfig, ax1 = plt.subplots(1,1, figsize = (8, 8))\nc_aximg = ax1.imshow(np.sum(n_stck==10, 0), cmap='bone_r', interpolation='lanczos', animated = True)\nax1.axis('off')\nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"ac62ef52ea5f39d3d469daccff569320ca8d2f14"},"cell_type":"code","source":"def update_image(frame):\n    c_aximg.set_array(n_stck[frame]==10)\n    return c_aximg,\nim_ani = animation.FuncAnimation(fig, update_image, \n                                 frames = range(n_stck.shape[0]),\n                                 interval=50, repeat_delay=300,\n                                blit=True)\nim_ani.save('slic_%03d.gif' % (ind), writer=writer)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"23872a746f54a7ed921354ff5cc957017779d5e7"},"cell_type":"markdown","source":" Guess this wont work"},{"metadata":{"trusted":true,"_uuid":"86061322bf427fec6275a3687b78d2fced518e51"},"cell_type":"code","source":"from skimage.viewer import ImageViewer\niv = ImageViewer(n_stck[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"7640a949d3eb0c0995e12d1d5049e38d420541ef"},"cell_type":"code","source":"iv.show()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":false,"_uuid":"cfdd09460f4c608499ebfc48fdaec799b8d90015"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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.5.2"}},"nbformat":4,"nbformat_minor":1}