{"cells": [{"metadata": {"_cell_guid": "f8df935b-0fe0-4398-9f04-f08cbd42db48", "_uuid": "143c2ccdbcb27c00cc63441f6b85fcc11941aaa9"}, "source": "# Saving to vtr file\nThe aim of this notebook is to show how to save .a3d files to vtr for visualizing them with paraview.  \nhttps://www.paraview.org/  \nhttps://pypi.python.org/pypi/PyEVTK", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": true, "trusted": false, "_cell_guid": "205fa5a2-5f16-44df-b63e-e79f8194d29f", "_uuid": "1037b086413bcf531b6b037e57f7a71b81e06005"}, "source": "import numpy as np\nimport os\nimport matplotlib\nfrom pyevtk.hl import gridToVTK", "outputs": [], "cell_type": "code", "execution_count": 1}, {"metadata": {"_cell_guid": "abced121-25bc-40f1-a66e-a9aba0a1c3aa", "_uuid": "f402748ab90ee5b947ce0f4b74c20330e0014067"}, "source": "The following functions have been copied from:  \nhttps://www.kaggle.com/wcukierski/reading-images", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": true, "trusted": false, "_cell_guid": "4d76d70b-8f22-4470-b58d-64429cf320f1", "_uuid": "e3ad93f687d047d8c1fe7e431d4154bc0632576a"}, "source": "def read_header(infile):\n    \"\"\"Read image header (first 512 bytes)\n    \"\"\"\n    h = dict()\n    fid = open(infile, 'r+b')\n    h['filename'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 20))\n    h['parent_filename'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 20))\n    h['comments1'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 80))\n    h['comments2'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 80))\n    h['energy_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['config_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['file_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['trans_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['scan_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['data_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['date_modified'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 16))\n    h['frequency'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['mat_velocity'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['num_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n    h['num_polarization_channels'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['spare00'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['adc_min_voltage'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['adc_max_voltage'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['band_width'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['spare01'] = np.fromfile(fid, dtype = np.int16, count = 5)\n    h['polarization_type'] = np.fromfile(fid, dtype = np.int16, count = 4)\n    h['record_header_size'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['word_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['word_precision'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['min_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['max_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['avg_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['data_scale_factor'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['data_units'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['surf_removal'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n    h['edge_weighting'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n    h['x_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n    h['y_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n    h['z_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n    h['t_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n    h['spare02'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['x_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['y_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['z_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['scan_orientation'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['scan_direction'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['data_storage_order'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['scanner_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['x_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['y_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['z_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['t_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['num_x_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n    h['num_y_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n    h['num_z_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n    h['num_t_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n    h['x_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['y_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['z_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['x_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['y_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['z_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['x_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['y_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['z_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['x_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['y_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['z_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['date_processed'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 8))\n    h['time_processed'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 8))\n    h['depth_recon'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['x_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['y_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['elevation_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['roll_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['z_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['azimuth_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['adc_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['spare06'] = np.fromfile(fid, dtype = np.int16, count = 1)\n    h['scanner_radius'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['x_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['y_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['z_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['t_delay'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['range_gate_start'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['range_gate_end'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['ahis_software_version'] = np.fromfile(fid, dtype = np.float32, count = 1)\n    h['spare_end'] = np.fromfile(fid, dtype = np.float32, count = 10)\n    return h", "outputs": [], "cell_type": "code", "execution_count": 2}, {"metadata": {"collapsed": true, "trusted": false, "_cell_guid": "283c55fb-20cf-46ab-bc5d-7b27ce162184", "_uuid": "092af83cd6c7e3268549e53439b747a27b4a60d4"}, "source": "def read_data(infile):\n    \"\"\"Read any of the 4 types of image files, returns a numpy array of the image contents\n    \"\"\"\n    extension = os.path.splitext(infile)[1]\n    h = read_header(infile)\n    nx = int(h['num_x_pts'])\n    ny = int(h['num_y_pts'])\n    nt = int(h['num_t_pts'])\n    fid = open(infile, 'rb')\n    fid.seek(512) #skip header\n    if extension == '.aps' or extension == '.a3daps':\n        if(h['word_type']==7): #float32\n            data = np.fromfile(fid, dtype = np.float32, count = nx * ny * nt)\n        elif(h['word_type']==4): #uint16\n            data = np.fromfile(fid, dtype = np.uint16, count = nx * ny * nt)\n        data = data * h['data_scale_factor'] #scaling factor\n        data = data.reshape(nx, ny, nt, order='F').copy() #make N-d image\n    elif extension == '.a3d':\n        if(h['word_type']==7): #float32\n            data = np.fromfile(fid, dtype = np.float32, count = nx * ny * nt)\n        elif(h['word_type']==4): #uint16\n            data = np.fromfile(fid, dtype = np.uint16, count = nx * ny * nt)\n        data = data * h['data_scale_factor'] #scaling factor\n        data = data.reshape(nx, nt, ny, order='F').copy() #make N-d image\n    elif extension == '.ahi':\n        data = np.fromfile(fid, dtype = np.float32, count = 2* nx * ny * nt)\n        data = data.reshape(2, ny, nx, nt, order='F').copy()\n        real = data[0,:,:,:].copy()\n        imag = data[1,:,:,:].copy()\n    fid.close()\n    if extension != '.ahi':\n        return data\n    else:\n        return real, imag", "outputs": [], "cell_type": "code", "execution_count": 3}, {"metadata": {"_cell_guid": "aa9c288f-915e-4f85-96ca-9a9e0d607491", "_uuid": "76a8e8de6ec9d57cac220b33f54349892b6f30cf"}, "source": "This simple function allows to save the 3d data to a .vtk file. \nYou may have to modify the pyevtk source code because there are some comparisons *==None* that need to be replaced by *is None*", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": true, "trusted": false, "_cell_guid": "011e8202-8a66-4e16-be78-e26112f4d0fa", "_uuid": "f45c3ba31ed2ae30d94af37f759ac77204e29a08"}, "source": "def save_to_vtk(data, filepath):\n    \"\"\"\n    save the 3d data to a .vtk file. \n    \n    Parameters\n    ------------\n    data : 3d np.array\n        3d matrix that we want to visualize\n    filepath : str\n        where to save the vtk model, do not include vtk extension, it does automatically\n    \"\"\"\n    x = np.arange(data.shape[0]+1)\n    y = np.arange(data.shape[1]+1)\n    z = np.arange(data.shape[2]+1)\n    gridToVTK(filepath, x, y, z, cellData={'data':data.copy()})", "outputs": [], "cell_type": "code", "execution_count": 4}, {"metadata": {"_cell_guid": "39461a9e-9465-4b9c-b0f4-e6f175576ca2", "_uuid": "9b1dac8fccd7cfcfc1b5b0ddc62cc99bf8419793"}, "source": "Load the file and normalize for easier visualization.", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"trusted": false, "_cell_guid": "cc128c90-8285-490f-81b7-35a6fea76960", "_uuid": "fa42bfed1326da5b8220ba32bee0ef0fc7be21d1"}, "source": "filepath = '/media/guillermo/Data/Kaggle/DHS/data/sample/0043db5e8c819bffc15261b1f1ac5e42.a3d'\ndata = read_data(filepath)\ndata /= np.max(data)\ndata.shape", "outputs": [], "cell_type": "code", "execution_count": 7}, {"metadata": {"_cell_guid": "a8b9c6ba-a70d-472a-945d-8ad682adf7f4", "_uuid": "1170eff34526716bded9a49b5fee65d2b2102909"}, "source": "Now we have a 3d array on data, save to file using the previous function.", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": true, "trusted": false, "_cell_guid": "e38ce0d3-af22-4137-ac2a-28fbc286dd66", "_uuid": "477186be6c9a82ada8bd0415c54cc340ba60314e"}, "source": "save_to_vtk(data, '/media/guillermo/Data/Kaggle/DHS/data_mods/forum/0043db5e8c819bffc15261b1f1ac5e42')", "outputs": [], "cell_type": "code", "execution_count": 6}, {"metadata": {"_cell_guid": "129e590a-556d-4671-921e-f05acc97b266", "_uuid": "3f496d98524b0868c3fb71695689d6f26a26c967"}, "source": "Now we can open the file using Paraview  \nhttps://www.youtube.com/watch?v=o7420QY5BN0", "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"collapsed": true, "trusted": false, "_cell_guid": "82091d35-fa75-4f11-8684-271fda1a7a13", "_uuid": "4f918a53f1988935ac36cc4c2b7ffc5796fde920"}, "source": [], "outputs": [], "cell_type": "code", "execution_count": null}], "metadata": {"kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}, "language_info": {"version": "3.6.1", "mimetype": "text/x-python", "codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "file_extension": ".py", "pygments_lexer": "ipython3", "name": "python"}}, "nbformat": 4, "nbformat_minor": 2}