Last active
December 18, 2015 17:39
-
-
Save slarson/5820077 to your computer and use it in GitHub Desktop.
Loading WormBehavior database file in Python without Matlab (iPython Notebook)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "Playing with WormBehavior Database" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "First, we need to grab a .mat file from the server:" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "import shutil\nimport mimetypes\nimport os\nimport urllib2\nimport urlparse\n\ndef download_file_locally(url, dest):\n req, filename, content_type = download_file(url) \n if dest.endswith('/'):\n dest = os.path.join(dest, filename)\n with open(dest, 'wb') as f:\n shutil.copyfileobj(req, f)\n req.close()\n\ndef filename_from_url(url):\n return os.path.basename(urlparse.urlsplit(url)[2])\n\ndef download_file(url):\n \"\"\"Create an urllib2 request and return the request plus some useful info\"\"\"\n name = filename_from_url(url)\n r = urllib2.urlopen(urllib2.Request(url))\n info = r.info()\n if 'Content-Disposition' in info:\n # If the response has Content-Disposition, we take filename from it\n name = info['Content-Disposition'].split('filename=')[1]\n if name[0] == '\"' or name[0] == \"'\":\n name = name[1:-1]\n elif r.geturl() != url:\n # if we were redirected, take the filename from the final url\n name = filename_from_url(r.geturl())\n content_type = None\n if 'Content-Type' in info:\n content_type = info['Content-Type'].split(';')[0]\n # Try to guess missing info\n if not name and not content_type:\n name = 'unknown'\n elif not name:\n name = 'unknown' + mimetypes.guess_extension(content_type) or ''\n elif not content_type:\n content_type = mimetypes.guess_type(name)[0]\n return r, name, content_type\n \nurl = \"ftp://[email protected]/pub/tjucikas/wormdatabase/results-12-06-08/Laura%20Grundy/unc-8/n491n1192/MT2611/on_food/XX/30m_wait/L/tracker_2/2010-03-19___09_14_57/unc-8%20(rev)%20on%20food%20R_2010_03_19__09_14_57___2___2_features.mat\"\n\ndownload_file_locally(url, \"./\")", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "Then, we need to open the file up. Since this is a Matlab file from version greater than 7.3, we need to turn to the [h5py](http://www.h5py.org/docs/intro/quick.html#what-is-hdf5) library to open these .mat files without using Matlab.\n\nLet's confirm we have the right tools:" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "import h5py\nf = h5py.File('unc-8%20(rev)%20on%20food%20R_2010_03_19__09_14_57___2___2_features.mat')\nprint f", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "<HDF5 file \"unc-8%20(rev)%20on%20food%20R_2010_03_19__09_14_57___2___2_features.mat\" (mode r+)>" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n" | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "Check!" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "import h5py\nf = h5py.File('unc-8%20(rev)%20on%20food%20R_2010_03_19__09_14_57___2___2_features.mat')\n\nprint list(f)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "[u'#refs#', u'info', u'worm']\n" | |
} | |
], | |
"prompt_number": 12 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment