Skip to content

Commit

Permalink
Merge pull request #146 from jwebst/HW8_branch
Browse files Browse the repository at this point in the history
Hw8 branch
  • Loading branch information
jakevdp committed Dec 2, 2014
2 parents b98711c + 98e2faf commit 24d1c04
Show file tree
Hide file tree
Showing 3 changed files with 1,295 additions and 0 deletions.
639 changes: 639 additions & 0 deletions jwebst/HW6.ipynb

Large diffs are not rendered by default.

493 changes: 493 additions & 0 deletions jwebst/HW7.ipynb

Large diffs are not rendered by default.

163 changes: 163 additions & 0 deletions jwebst/HW8.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"metadata": {
"name": "",
"signature": "sha256:c067349766aec8ef547a36630beebf15c1d37676de8152c20bc150ee680cdb00"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"ASTR599: HW8 Debugging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jason Webster 2014-11-24"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Homework\n",
"Below is a script taken from [scipy lectures](https://scipy-lectures.github.io/advanced/debugging/)\n",
"\n",
"It is meant to compare the performance of several root-finding algorithms within the ``scipy.optimize``\n",
"package, but it breaks. Use one or more of the above tools to figure out what's going on and to fix\n",
"the script.\n",
"\n",
"When you turn in this homework (via github pull request, of course), please **write a one to two paragraph summary**\n",
"of the process you used to debug this, including any dead ends (it may help to take notes as you go)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"\"\"\n",
"A script to compare different root-finding algorithms.\n",
"\n",
"This version of the script is buggy and does not execute. It is your task\n",
"to find an fix these bugs.\n",
"\n",
"The output of the script sould look like:\n",
"\n",
" Benching 1D root-finder optimizers from scipy.optimize:\n",
" brenth: 604678 total function calls\n",
" brentq: 594454 total function calls\n",
" ridder: 778394 total function calls\n",
" bisect: 2148380 total function calls\n",
"\"\"\"\n",
"from itertools import product\n",
"\n",
"import numpy as np\n",
"from scipy import optimize\n",
"\n",
"FUNCTIONS = (np.tan, # Dilating map\n",
" np.tanh, # Contracting map\n",
" lambda x: x**3 + 1e-4*x, # Almost null gradient at the root\n",
" lambda x: x+np.sin(2*x), # Non monotonous function\n",
" lambda x: 1.1*x+np.sin(4*x), # Fonction with several local maxima\n",
" )\n",
"\n",
"OPTIMIZERS = (optimize.brenth, optimize.brentq,\n",
" optimize.ridder, optimize.bisect)\n",
"\n",
"\n",
"def apply_optimizer(optimizer, func, a, b):\n",
" \"\"\" Return the number of function calls given an root-finding optimizer, \n",
" a function and upper and lower bounds.\n",
" \"\"\"\n",
" return optimizer(func, a, b, full_output=True)[1].function_calls\n",
"\n",
"\n",
"def bench_optimizer(optimizer, param_grid):\n",
" \"\"\" Find roots for all the functions, and upper and lower bounds\n",
" given and return the total number of function calls.\n",
" \"\"\"\n",
" return sum(apply_optimizer(optimizer,func,a,b) for func, a, b in param_grid)\n",
"\n",
"\n",
"def compare_optimizers(optimizers):\n",
" \"\"\" Compare all the optimizers given on a grid of a few different\n",
" functions all admitting a signle root in zero and a upper and\n",
" lower bounds.\n",
" \"\"\"\n",
" random_a = -1.3 + np.random.random(size=100)\n",
" random_b = .3 + np.random.random(size=100)\n",
" print(\"Benching 1D root-finder optimizers from scipy.optimize:\")\n",
" for optimizer in OPTIMIZERS:\n",
" param_grid = product(FUNCTIONS, random_a, random_b)\n",
" ncalls = bench_optimizer(optimizer, param_grid)\n",
" print('{:>18}: {:8} total function calls'.format(optimizer.__name__, ncalls))\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" compare_optimizers(OPTIMIZERS)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Benching 1D root-finder optimizers from scipy.optimize:\n",
" brenth: 604728 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" brentq: 594624 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" ridder: 775404 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" bisect: 2149520 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I didn't use any debugging tools to resolve the issues. I noticed the code didn't run, so I looked for something that would make it text and removed the ticks. Then I read through the code an looked for obvious errors. I reformatted it to make it a little easier to read. It was crashing with an error about int/tupple in a complex command line. I tried exploring the list comprehensions with the product, but that didn't seem to be the issue. I eventually noticed that there was a comma in the return value of the function it was calling, which was creating the tupple. When this was removed there were no more runtime errors, but the last three function were reporting no calls. It took some time to figure this out, but I tested calling the functions independently and noticed that the product won't run in a list more than once. I moved the definition in the loop, which produced the remaining values. They weren't exactly the same but close. I reran it several times and each time was different but similar results. \n",
"The formatting was still wrong, and I tried several places to add the formatting to the string, but couldn't find a way to do this with the variable names. They didn't seem useful, so I took them out and replaced them with the formats for the variables"
]
}
],
"metadata": {}
}
]
}

0 comments on commit 24d1c04

Please sign in to comment.