-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notebooks and my notes for defensive programming
- Loading branch information
0 parents
commit 81db389
Showing
9 changed files
with
3,944 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.ipynb_checkpoints |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
" # Defensive programming (1)\n", | ||
" \n", | ||
" How much time do you spend writing software? How much time do you spend \n", | ||
" debugging that software? It turns out that it is very easy to spend lots\n", | ||
" of time fixing bugs and less time than you would like writing new software\n", | ||
" to do new science. This is a problem that is fairly well understood by\n", | ||
" the software engineering community, but many scientists don't take advantage \n", | ||
" of this knowledge. This afternoon we will take a brief look at some of the \n", | ||
" tools and technique to make your debugging less painful.\n", | ||
" \n", | ||
" We'll also think a bit about how you may know if your programmes are correct. \n", | ||
" This is a much harder but important problem. Even minor errors in research \n", | ||
" code can lead to the retraction of papers, as happened to Geoffrey Chang \n", | ||
" in 2006 (see http://dx.doi.org/10.1126/science.314.5807.1856). Chang did\n", | ||
" nothing malicious and committed no fraud, but because of a minor software \n", | ||
" error had two retract five papers just before Christmas." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"__NB: This notebook is designed for teaching about exceptions and error testing. It includes deliberate errors. There are probably accidental errors too.__" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Mean cell volume \n", | ||
"First, we will look at how one programme can produce \n", | ||
"the wrong answer, and how we can avoid this happening\n", | ||
"when we use it." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def cell_volume(X, Y, Z):\n", | ||
" # Return the volume of a unit cell \n", | ||
" # described by lattice vectors X, Y and Z\n", | ||
" # The volume is given by the determinant of\n", | ||
" # the matrix formed by sticking the three \n", | ||
" # vectors together. i.e.\n", | ||
" #\n", | ||
" # | X[0] Y[0] Z[0] |\n", | ||
" # V = | X[1] Y[1] Z[1] |\n", | ||
" # | X[2] Y[2] Z[2] |\n", | ||
" #\n", | ||
" # V = X[0].Y[1].Z[2] + Y[0].Z[1].X[2] \n", | ||
" # + X[2].Y[0].Z[1] - Z[0].Y[1].X[2]\n", | ||
" # - Y[0].X[1].Z[2] - X[0].Z[1].Y[2]\n", | ||
" \n", | ||
" volume = (X[0]*Y[1]*Z[2] + Y[0]*Z[1]*X[2] + X[2]*Y[0]*Z[1] \n", | ||
" - Z[0]*Y[1]*X[2] - Y[0]*X[1]*Z[2] - X[0]*Z[1]*Y[2])\n", | ||
" \n", | ||
" return volume" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def mean_cell_volume(cell_list):\n", | ||
" # Return the avarage volume of a list \n", | ||
" # of unit cells. Each element of cell_list\n", | ||
" # should be a list of three lattice vectors, \n", | ||
" # each with three components. The volume of\n", | ||
" # each cell is calculated and summed before \n", | ||
" # being devided by the number of cells to give\n", | ||
" # the mean volume.\n", | ||
" \n", | ||
" num_cells = 0\n", | ||
" sum_volume = 0.0\n", | ||
" for cell in cell_list:\n", | ||
" X = cell[0]\n", | ||
" Y = cell[1]\n", | ||
" Z = cell[2]\n", | ||
" sum_volume = sum_volume + cell_volume(X, Y, Z)\n", | ||
" num_cells = num_cells + 1\n", | ||
" \n", | ||
" mean_volume = sum_volume/num_cells\n", | ||
" \n", | ||
" return mean_volume\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 2", | ||
"language": "python", | ||
"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.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
" # Defensive programming (2)\n", | ||
" We have seen the basic idea that we can insert\n", | ||
" assert statments into code, to check that the \n", | ||
" results are what we expect, but how can we test\n", | ||
" software more fully? Can doing this help us \n", | ||
" avoid bugs in the first place?\n", | ||
" \n", | ||
" One possible approach is **test driven development**.\n", | ||
" Many people think this reduces the number of bugs in \n", | ||
" software as it is written, but evidence for this in the \n", | ||
" sciences is somewhat limited as it is not always easy \n", | ||
" to say what the right answer should be before writing the\n", | ||
" software. Having said that, the tests involved in test\n", | ||
" driven development are certanly useful even if some of\n", | ||
" them are written after the software.\n", | ||
" \n", | ||
" We will look at a new (and quite difficult) problem, \n", | ||
" finding the overlap between ranges of numbers. For \n", | ||
" example, these could be the dates that different \n", | ||
" sensors were running, and you need to find the \n", | ||
" date ranges where all sensors recorded data before\n", | ||
" running further analysis." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<img src=\"python-overlapping-ranges.svg\">" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Start off by imagining you have a working function `range_overlap` that takes\n", | ||
"a list of tuples. Write some assert statments that would check if the answer from this\n", | ||
"function is correct. Put these in a function. Think of different cases and \n", | ||
"about edge cases (which may show a subtle bug)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def test_range_overlap():\n", | ||
" " | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 2", | ||
"language": "python", | ||
"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.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Oops, something went wrong.