\n",
"\n",
"
Learning Objectives
\n",
"\n",
"\n",
"- Use a library function to get a list of filenames that match a simple wildcard pattern.
\n",
"- Use a for loop to process multiple files.
\n",
"
\n",
"\n",
"\n",
"We now have almost everything we need to process all our data files. The only thing that’s missing is a library with a rather unpleasant name:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import glob"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `glob` library contains a single function, also called `glob`, that finds files whose names match a pattern. We provide those patterns as strings: the character `*` matches zero or more characters, while `?` matches any one character. We can use this to get the names of all the HTML files in the current directory:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(glob.glob('data/inflammation*.csv'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As these examples show, glob.glob’s result is a list of strings, which means we can loop over it to do something with each filename in turn. In our case, the “something” we want to do is generate a set of plots for each file in our inflammation dataset.\n",
"\n",
"```python\n",
"import numpy\n",
"import matplotlib.pyplot\n",
"\n",
"filenames = glob.glob('data/inflammation*.csv')\n",
"filenames = filenames[0:3]\n",
"for f in filenames:\n",
" print(f)\n",
"\n",
" data = numpy.loadtxt(fname=f, delimiter=',')\n",
"\n",
" fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))\n",
"\n",
" axes1 = fig.add_subplot(1, 3, 1)\n",
" axes2 = fig.add_subplot(1, 3, 2)\n",
" axes3 = fig.add_subplot(1, 3, 3)\n",
"\n",
" axes1.set_ylabel('average')\n",
" axes1.plot(data.mean(axis=0))\n",
"\n",
" axes2.set_ylabel('max')\n",
" axes2.plot(data.max(axis=0))\n",
"\n",
" axes3.set_ylabel('min')\n",
" axes3.plot(data.min(axis=0))\n",
"\n",
" fig.tight_layout()\n",
" matplotlib.pyplot.show(fig)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Sure enough, the maxima of the first two data sets show exactly the same ramp as the first, and their minima show the same staircase structure; a different situation has been revealed in the third dataset, where the maxima are a bit less regular, but the minima are consistently zero."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python2",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}