Commit 39198c7c authored by Emilio Ambite's avatar Emilio Ambite

last version

parent c8ae748f
......@@ -97,7 +97,7 @@
"def normalize_rectangle(rect):\n",
" '''Normalizes a rectangle so that it is at the origin and 1.0 units long on its longest axis.'''\n",
" assert len(rect) == 4, 'Rectangles must contain 4 coordinates'\n",
" llx, llyy, urx, ury = rect\n",
" llx, lly, urx, ury = rect\n",
" assert llx < urx, 'Invalid X coordinates'\n",
" assert lly < ury, 'Invalid Y coordinates'\n",
"\n",
......@@ -141,6 +141,27 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Post-conditions in lines 17, 18 check well formed output:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -244,20 +265,6 @@
"# Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -286,12 +293,17 @@
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
}
],
......
......@@ -504,6 +504,13 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -555,38 +562,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Oh wait... maybe I am creating to many variables... let's check:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes we can avoid the use of some variables:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -668,31 +643,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-info\">\n",
"<div class=\"panel-heading\">\n",
"<h3 class=\"panel-title\">\n",
"IPython Magic!\n",
"</h3>\n",
"</div>\n",
"<div class=\"panel-body\">\n",
"The % indicates an IPython magic function - a funciton that is valid only within the notebook environment.\n",
"</div>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -741,15 +691,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -884,7 +825,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.7.4"
}
},
"nbformat": 4,
......
......@@ -100,7 +100,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"What about Farh to Celsius? One of the main proposes of functions is reusability. So let's reuse one of them to build some \"neat\" code:"
"What about Fahr to Celsius? One of the main proposes of functions is reusability. So let's reuse one of them to build some \"neat\" code:"
]
},
{
......@@ -125,14 +125,16 @@
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {},
"source": [
"1) 7 3\n",
"2) 3 7\n",
"3) 3 3\n",
"4) 7 7\n",
"1. ) 7 3\n",
"2. ) 3 7\n",
"3. ) 3 3\n",
"4. ) 7 7\n",
"\n",
"\n",
"```\n",
"a = 3\n",
"b = 7\n",
"\n",
......@@ -143,7 +145,8 @@
"\n",
"swap(a, b)\n",
"\n",
"print(a, b)\n"
"print(a, b)\n",
"```"
]
},
{
......@@ -528,20 +531,23 @@
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"def numbers(one, two=2, three, four=4):\n",
" n = str(one) + str(two) + str(three) + str(four)\n",
" return n\n",
"\n",
"print(numbers(1, three=3))\n",
"```\n",
"\n",
"what do you expect will be printed? What is actually printed? What rule do you think Python is following?\n",
"\n",
"1) 1234\n",
"2) one2three4\n",
"3) 1239\n",
"4) SyntaxError"
"1. ) 1234\n",
"2. ) one2three4\n",
"3. ) 1239\n",
"4. ) SyntaxError"
]
},
{
......@@ -593,10 +599,34 @@
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"def s(p):\n",
" a = 0\n",
" for v in p:\n",
" a += v\n",
" m = a / len(p)\n",
" d = 0\n",
" for v in p:\n",
" d += (v - m) * (v - m)\n",
" return numpy.sqrt(d / (len(p) - 1))\n",
" \n",
"#It is not very to say what it does...though it does something very well known. Check this one:\n",
"\n",
"def std_deviation(sample):\n",
" sample_sum = 0\n",
" for value in sample:\n",
" sample_sum += value\n",
" \n",
" sample_mean = sample_sum / len(sample)\n",
" sum_squared_devs = 0\n",
" for value in sample:\n",
" sum_squared_devs += (value - sample_mean) * (value - sample_mean)\n",
" return numpy.sqrt(sum_squared_devs / (len(sample) - 1))\n",
"\n",
"#Much more easy to read!"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
......@@ -607,7 +637,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises"
"## Additional Exercises"
]
},
{
......@@ -739,22 +769,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Readable code"
]
},
{
"cell_type": "code",
"execution_count": null,
......
......@@ -102,7 +102,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Example: Products in the shelves of a shop. We can print the results of the index operations in the images:"
"Example: Products in the shelves of a shop. "
]
},
{
......@@ -114,15 +114,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -309,7 +300,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.7.4"
}
},
"nbformat": 4,
......
......@@ -11,7 +11,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We found some suspicious features in our firs dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
"We found some suspicious features in our first dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
]
},
{
......@@ -133,22 +133,6 @@
"<li>etc.."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 3. This loop overrides the value of a external variable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......
......@@ -6,7 +6,7 @@
"source": [
"# Making choices\n",
"\n",
"In last section we discovered something suspicios in our inflammation data by drawing some plots. How can we use Python\n",
"In last section we discovered something suspicious in our inflammation data by drawing some plots. How can we use Python\n",
"to automatically detect the anomalies we found and take an action for each of them?"
]
},
......@@ -38,7 +38,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause:"
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause. If there are multiple choices we make use of the <code>elif</code>:"
]
},
{
......@@ -54,25 +54,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes there is no need to use a else, it depends on the logic of the problem:"
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes the flow of the decision is more complex. There are still multiple choices available \n",
"when the first condition is discarded. <br>\n",
"As an example imagine we want to know if a given number is positive negative or zero. We make use of the <code>elif</code> clause:"
"Sometimes there is no need to use a else, it depends on the logic of the problem:"
]
},
{
......@@ -84,13 +73,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -198,6 +180,20 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -213,15 +209,6 @@
"First two files max inflammation seems to rise linearly. So let's write a code that check this behavior and show warning:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -344,7 +331,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.7.4"
}
},
"nbformat": 4,
......
......@@ -287,10 +287,8 @@
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def test_range_overlap():\n",
......@@ -362,27 +360,6 @@
"# Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# a possible pre-condition:\n",
"assert len(input) > 0, #'List length must be non-zero'\n",
"# a possible post-condition:\n",
"assert numpy.min(input) <= average <= numpy.max(input), #'Average should be between min and max of input values'"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -394,10 +371,8 @@
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy\n",
......@@ -409,6 +384,7 @@
" return None\n",
" lowest, highest = ranges[0]\n",
" for (low, high) in ranges[1:]:\n",
" assert low <= high, 'first element in range should be the lower one'\n",
" lowest = max(lowest, low)\n",
" highest = min(highest, high)\n",
" if lowest >= highest: # no overlap\n",
......@@ -419,23 +395,33 @@
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"test_range_overlap()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": []
"source": [
"# a possible pre-condition:\n",
"assert len(input) > 0, #'List length must be non-zero'\n",
"# a possible post-condition:\n",
"assert numpy.min(input) <= average <= numpy.max(input), #'Average should be between min and max of input values'"
]
}
],
"metadata": {
......
......@@ -165,7 +165,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<li>Remember: Both SyntaxError and IntentationError indicate a problem with the syntax of the program"
"<li>Remember: Both SyntaxError and IndentationError indicate a problem with the syntax of the program"
]
},
{
......
......@@ -331,9 +331,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"import numpy"
......@@ -568,7 +566,7 @@
}
],
"source": [
"print(data[5:10, 0:10]) # Selecting rows 0,1,2,3 and cols from 0 to 9. Important! notice upper limits are not \n",
"print(data[5:10, 0:10]) # Selecting rows 5,6,7,8 and cols from 0 to 9. Important! notice upper limits are not \n",
" # included in the counting\n",
" # You don't have to start slices at 0"
]
......@@ -591,7 +589,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 5,
"metadata": {},
"outputs": [
{
......@@ -599,9 +597,9 @@
"output_type": "stream",
"text": [
"small is: \n",
"[[ 2. 3. 0. 0.]\n",
" [ 1. 1. 0. 1.]\n",
" [ 2. 2. 1. 1.]]\n"
"[[2. 3. 0. 0.]\n",
" [1. 1. 0. 1.]\n",
" [2. 2. 1. 1.]]\n"
]
}
],
......@@ -650,7 +648,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 6,
"metadata": {},
"outputs": [
{
......@@ -658,22 +656,22 @@
"output_type": "stream",
"text": [
"original:\n",
"[[ 2. 3. 0. 0.]\n",
" [ 1. 1. 0. 1.]\n",
" [ 2. 2. 1. 1.]]\n",
"doubledata: \n",
"[[ 4. 6. 0. 0.]\n",
" [ 2. 2. 0. 2.]\n",
" [ 4. 4. 2. 2.]]\n"
"[[2. 3. 0. 0.]\n",
" [1. 1. 0. 1.]\n",
" [2. 2. 1. 1.]]\n",
"doublesmall: \n",
"[[4. 6. 0. 0.]\n",
" [2. 2. 0. 2.]\n",
" [4. 4. 2. 2.]]\n"
]
}
],
"source": [
"doubledata = 2 * data # The x2 operation is made in every element of the array\n",
"doublesmall = 2 * small # The x2 operation is made in every element of the array\n",
"print('original:')\n",
"print(data[:3,36:])\n",
"print('doubledata: ')\n",
"print(doubledata[:3,36:])"
"print(small)\n",
"print('doublesmall: ')\n",
"print(doublesmall)"
]
},
{
......@@ -685,7 +683,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 7,
"metadata": {},
"outputs": [
{
......@@ -693,16 +691,16 @@
"output_type": "stream",
"text": [
"tripledata:\n",
"[[ 6. 9. 0. 0.]\n",
" [ 3. 3. 0. 3.]\n",
" [ 6. 6. 3. 3.]]\n"
"[[6. 9. 0. 0.]\n",
" [3. 3. 0. 3.]\n",
" [6. 6. 3. 3.]]\n"
]
}
],
"source": [
"tripledata = doubledata + data #Array-Array operations are made on each corresponding element\n",
"triplesmall = doublesmall + small #Array-Array operations are made on each corresponding element\n",
"print('tripledata:')\n",
"print(tripledata[:3,36:])"
"print(triplesmall)"
]
},
{
......@@ -1001,33 +999,6 @@
" # You may get a warning here about rendering fonts. If we re-run it not htere anymore."
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-info\">\n",
"<div class=\"panel-heading\">\n",
"<h3 class=\"panel-title\">\n",
"IPython Magic!\n",
"</h3>\n",
"</div>\n",
"<div class=\"panel-body\">\n",
"The % indicates an IPython magic function - a funciton that is valid only within the notebook environment.\n",
"</div>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
......
......@@ -19,7 +19,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [
{
......@@ -156,7 +156,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Example: Products in the shelves of a shop. We can print the results of the index operations in the images:"
"Example: Products in the shelves of a shop. "
]
},
{
......@@ -170,23 +170,6 @@