I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
5. Importing modules
Python lets you define modules which are files consisting of Python code. A module can define functions, classes and variables.
Most Python distributions already include the most popular modules with predefined libraries which make our programmer lives easier. Some well-known libraries are: time, sys, os, numpy, etc.
There are several ways to import a library:
1) Import all the contents of the library: import lib_name
Note: You have to call these methods as part of the library
Can you write Python code for it?

import time
print(time.time())  # returns the current processor time in seconds
time.sleep(2) # suspends execution for the given number of seconds
print(time.time()) # returns the current processor time in seconds again!!!


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
6. User defined functions
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity and a high degree of code reusing.
As we have seen, Python gives us many built-in functions like print(), etc. but you can also create your own functions.
Defining a function:

Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).

Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.

The first statement of a function can be an optional statement - the documentation string of the function or docstring. Docstring blocks start and ed with triple quotes (""").

The code block within every function starts with a colon (:) and is indented.

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
Can you write Python code for it?

# Example of a function:

def add_five(a):
  """This function adds the number five to its input argument."""
  return a + 5

# Let's try it out!
x = int(input('Please enter an integer: '))
print('Our add_five function is super effective at adding fives:')
print(x, '+ 5 =', add_five(x))


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
Exercise 1: Define variables str1 and str2 containing strings "The greatest glory in living lies not in never falling, " and "but in rising every time we fall", respectively. Concatenate both strings to compose a sentence, transform it to capital letters and print the result.
Can you write Python code for it?

# Write your solution here
# <SOL>
str1 = "The greatest glory in living lies not in never falling, "
str2 = "but in rising every time we fall"
str3 = (str1 + str2).upper()
print(str3)
# </SOL>


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
Exercise 2: Define variable odds containing all odd numbers betewen 1 and 9. Also, define variable prime containing all prime numbers between 1 and 9. Now, define variable odd_or_prime as the concatenation of odd and prime. Some numbers are repeated. User method remove to supress elements appearing twice. Print the result.
Can you write Python code for it?

# Write your solution here
# <SOL>
odd = [1, 3, 5, 7, 9]
prime = [2, 3, 5, 7]
odd_or_prime = odd + prime
odd_or_prime.remove(3)
odd_or_prime.remove(5)
odd_or_prime.remove(7)
print(odd_or_prime)
# </SOL>


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
Exercise 3: After the exam review, you teacher has updated your mark in Statistics to 7.0. Also, the scores of "communication theory" have been just published, and you've got 10.0. How would you modify your dictionary with the new marks?
Can you write Python code for it?

# Write your solution here
# <SOL>
my_marks['statistics'] = 7.0
my_marks['communication theory'] = 10.0
# </SOL>
print(my_marks)


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
Exercise 4: Create a list powers2 of all powers of 2 between $2^0$ (2**0) and $2^{16}$ (2**16). Loop over the list to create a new list y with all powers of 2 that are greater than 100 and lower than 1000
Can you write Python code for it?

# Write your solution here
# <SOL>
powers2 = [2**x for x in range(16)]
y = []
for p in powers2:
    if p > 100 and p < 10000:
        y.append(p)
# </SOL>
print(y)


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
Exercise 5: Now try it yourself! Write a function called my_factorial that computes the fatorial of a given number. If you're feeling brave today, you can try writing it recursively since Python obviously supports recursion. Remember that the factorial of 0 is 1 and the factorial of a negative number is undefined.
Can you write Python code for it?

# Write your solution here
# <SOL>
def my_factorial(n):
    if n > 1:
        return n * my_factorial(n - 1)
    else:
        return 1
# </SOL>

# Let's try it out!
from math import factorial
x = int(input('Please enter an integer: '))
if my_factorial(x) == factorial(x):
  print(x, '! = ', my_factorial(x))
  print('It works!')
else:
  print("Something's not working..." )


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
In combination with enumerate(), you can iterate over the elements of the sequence and have a counter over them
Can you write Python code for it?

words = ['cat', 'window', 'open-course']
for (i, w) in enumerate(words):
     print ('element ' + str(i) + ' is ' + w)


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
In order to access a particular value in the dictionary we need to use the corresponding key:
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
The above fragment, allows us also to discuss some important characteristics of the Python language syntaxis:

Unlike other languages, Python does not require to use the 'end' keyword to indicate that a given code fragment finishes. Instead, Python recurs to indentation
Indentation in Python is mandatory, and consists of 4 spaces (for first level indentation)
The condition lines conclude with ':', which are then followed by the indented blocks that will be executed only when the indicated conditions are satisfied.

3.2. Loops
The statement 'for' lets you iterate over the items of any iterable (a list, a string, a dictionary... ), in the order that they appear.
Can you write Python code for it?

words = ['cat', 'window', 'open-course']
for w in words:
     print (w, len(w))

my_dict = {"key1": 10, "key2": 25, "key3": 30}
for key in my_dict.keys():
     print (key, my_dict[key])


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
The first argument is a string containing the filename. The second argument defines the mode in which the file will be used:
'r' : only to be read,
'w' : for only writing (an existing file with the same name would be erased),
'a' : the file is opened for appending; any data written to the file is automatically appended to the end.
'r+': opens the file for both reading and writing.

If the mode argument is not included, 'r' will be assumed.
Use f.write(string) to write  the contents of a string to the file.  When you are done, do not forget to close the file:
Can you write Python code for it?

f.write('This is a test\n with 2 lines')
f.close()


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
The following commands implement some common operations with strings in Python. Take a look at them, and try to deduce what the result of each operation will be. Then, execute the commands and check what the actual results are.
Can you write Python code for it?

print(str1[0:5])

print(str1 + str2)

print(str1.lower())

print(str1.upper())

print(len(str1))

print(str1.replace('h','H'))

str3 = 'This is a question'
str3 = str3.replace('i', 'o')
str3 = str3.lower()
print(str3[0:3])


I am working on the file "P1.Python_intro/P1_Starting_with_Python_professor.ipynb".
The first task is:
You can also read line by line from the file identifier
Can you write Python code for it?

f2 = open('workfile', 'r')
for line in f2:
    print(line)

f2.close()


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
2) Define a short name to use the library: import lib_name as lib
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
2. Working with Python lists and dictionaries
2.1. Lists
Python lists are iterable containers that hold a number of other objects, in a given order. To create a list, just put different comma-separated values between square brackets:
Can you write Python code for it?

# A list of integers
mylist0 = [1, 2, 3, 4, 5]
print(mylist0)

# A list of trings
mylist1 = ['a', 'b', 'c', 'd']
print(mylist1)

# A list of strings, integers and float
mylist2 = ['student', 'teacher', 1997, 2000, 8.9]
print(mylist2)


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
2.1.1. Indexing lists
To check the value of a list element, indicate between brackets the index (or indices) to obtain the value (or values) at that position (positions).
Run the code fragment below, and try to guess what the output of each command will be.
Note: Python indexing starts from 0!!!!
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
3. Flow control (with 'for' and 'if')
As in other programming languages, python offers mechanisms to loop through a piece of code several times, or for conditionally executing a code fragment when certain conditions are satisfied.
3.1. Conditional execution
For conditional execution, you can we use the 'if', 'elif' and 'else' statements.
Try to play with the following example:
Can you write Python code for it?

x = int(input('Please enter an integer: '))
if x < 0:
    x = 0
    print('Negative changed to zero')
elif x == 0:
    print('Zero')
elif x == 1:
    print('Single')
else:
    print('More')


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
4. File input and output operations
First of all, you need to open a file with the open() function (if the file does not exist, the open() function will create it).
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
5. Importing modules
Python lets you define modules which are files consisting of Python code. A module can define functions, classes and variables.
Most Python distributions already include the most popular modules with predefined libraries which make our programmer lives easier. Some well-known libraries are: time, sys, os, numpy, etc.
There are several ways to import a library:
1) Import all the contents of the library: import lib_name
Note: You have to call these methods as part of the library
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
Exercise 1: Define variables str1 and str2 containing strings "The greatest glory in living lies not in never falling, " and "but in rising every time we fall", respectively. Concatenate both strings to compose a sentence, transform it to capital letters and print the result.
Can you write Python code for it?

# Write your solution here
# <SOL>
# </SOL>


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
Exercise 2: Define variable odds containing all odd numbers betewen 1 and 9. Also, define variable prime containing all prime numbers between 1 and 9. Now, define variable odd_or_prime as the concatenation of odd and prime. Some numbers are repeated. User method remove to supress elements appearing twice. Print the result.
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
Exercise 4: Create a list powers2 of all powers of 2 between $2^0$ (2**0) and $2^{16}$ (2**16). Loop over the list to create a new list y with all powers of 2 that are greater than 100 and lower than 1000
Can you write Python code for it?

# Write your solution here
# <SOL>
# </SOL>
print(y)


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
Exercise 5: Now try it yourself! Write a function called my_factorial that computes the fatorial of a given number. If you're feeling brave today, you can try writing it recursively since Python obviously supports recursion. Remember that the factorial of 0 is 1 and the factorial of a negative number is undefined.
Can you write Python code for it?

# Write your solution here
# <SOL>
# </SOL>

# Let's try it out!
from math import factorial
x = int(input('Please enter an integer: '))
if my_factorial(x) == factorial(x):
  print(x, '! = ', my_factorial(x))
  print('It works!')
else:
  print("Something's not working..." )


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
If we want to display all the keys in a dictionary we can use the .keys() method. Likewise we can view all the stored values in the dictionary by using  the .values() method:
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
In combination with enumerate(), you can iterate over the elements of the sequence and have a counter over them
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
In order to access a particular value in the dictionary we need to use the corresponding key:
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
It is interesting to notice the difference in the use of commands 'lower' and 'len'. Python is an object-oriented language, and str1 is an instance of the Python class 'string'. Then, str1.lower() invokes the method lower() of the class string to which object str1 belongs, while len(str1) or type(str1) imply the use of external methods, not belonging to the class string. In any case, we will not pay (much) attention to these issues during the session.
Finally, we remark that there exist special characters that require special consideration. Apart from language-oriented characters or special symbols (e.g., \euro), the following characters are commonly used to denote carriage return and the start of new lines
Can you write Python code for it?

print('This is just a carriage return symbol.\r Second line will start on top of the first line.')

print('If you wish to start a new line,\r\nthe line feed character should also be used.')

print('But note that most applications are tolerant\nto the use of \'line feed\' only.')


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
Other useful functions are:
len(list):    Gives the number of elements in a list.
max(list):    Returns item from the list with max value.
min(list):    Returns item from the list with min value.
Can you write Python code for it?

mylist = [1, 2, 3, 4, 5]
print(len(mylist))
print(max(mylist))
print(min(mylist))


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
Python is very expressive and flexible when it comes to list creation and manipulation. Check out this interesting block of code and try to figure out what it's doing:
Can you write Python code for it?

# Python one-liners are great:
x = [1, 2, 3, 4, 5]
y = [i**2 for i in x]
print('x = ', x, '\ny = ', y)


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
The above fragment, allows us also to discuss some important characteristics of the Python language syntaxis:

Unlike other languages, Python does not require to use the 'end' keyword to indicate that a given code fragment finishes. Instead, Python recurs to indentation
Indentation in Python is mandatory, and consists of 4 spaces (for first level indentation)
The condition lines conclude with ':', which are then followed by the indented blocks that will be executed only when the indicated conditions are satisfied.

3.2. Loops
The statement 'for' lets you iterate over the items of any iterable (a list, a string, a dictionary... ), in the order that they appear.
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
The first argument is a string containing the filename. The second argument defines the mode in which the file will be used:
'r' : only to be read,
'w' : for only writing (an existing file with the same name would be erased),
'a' : the file is opened for appending; any data written to the file is automatically appended to the end.
'r+': opens the file for both reading and writing.

If the mode argument is not included, 'r' will be assumed.
Use f.write(string) to write  the contents of a string to the file.  When you are done, do not forget to close the file:
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
The following commands implement some common operations with strings in Python. Take a look at them, and try to deduce what the result of each operation will be. Then, execute the commands and check what the actual results are.
Can you write Python code for it?


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
To read the content of a file, use the function f.read():
Can you write Python code for it?

f2 = open('workfile', 'r')
text = f2.read()
f2.close()
print(text)


I am working on the file "P1.Python_intro/P1_Starting_with_Python_student.ipynb".
The first task is:
You can also concatenate lists by simple "addition"
Can you write Python code for it?

fruits = ['apple', 'orange']
drinks = ['water',  'juice']
fruits_and_drinks = fruits + drinks
print(fruits_and_drinks)


I am working on the file "P1.ipynb".
The first task is:
At this point, if you were successful you probably have the Hough line segments drawn onto the road, but what about identifying the full extent of the lane and marking it clearly as in the example video (P1_example.mp4)?  Think about defining a line to run the full length of the visible lane based on the line segments you identified with the Hough Transform.  Modify your draw_lines function accordingly and try re-running your pipeline.
Now for the one with the solid yellow lane on the left. This one's more tricky!
Can you write Python code for it?

yellow_output = 'solidYellowLeft-Result.mp4'
clip2 = VideoFileClip('solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(yellow_output))


I am working on the file "P1.ipynb".
The first task is:
Build a Lane Finding Pipeline
Build the pipeline and run your solution on all test_images. Make copies into the test_images directory, and you can use the images in your writeup report.
Try tuning the various parameters, especially the low and high Canny thresholds as well as the Hough lines parameters.
Can you write Python code for it?

# TODO: Build your pipeline that will draw lane lines on the test_images
# then save them to the test_images directory.

#test_img_dir = test_img_dir + "generated/"
print("test image dir: " + test_img_dir)

def process_image(test_img_dir, img):
    # read in original image
    ori_image = mpimg.imread(os.path.join(test_img_dir, img))

    #convert to HSV colorspace for color filtering
    image = cv2.cvtColor(ori_image, cv2.COLOR_RGB2HSV)

    #select white and yellow only get the lane lines
    sensitivity = 50
    lower_white = np.array([0,0,255-sensitivity])
    upper_white = np.array([255,sensitivity,255])
    lower_yellow = np.array([20, 120, 100])
    upper_yellow = np.array([50, 170, 255])
    white = cv2.inRange(image, lower_white, upper_white)
    yellow = cv2.inRange(image, lower_yellow, upper_yellow)
    white_yellow = white + yellow

    # apply the mask to the image
    image = cv2.bitwise_and(image,image, mask= white_yellow)
    image=cv2.cvtColor(image, cv2.COLOR_HSV2RGB)

    # output directory for generated images
    output_dir = "generated/" + iteration + "/"
    if not os.path.exists(test_img_dir + output_dir):
        os.makedirs(test_img_dir + output_dir)

    # convert to grayscale
    gray = grayscale(image)
    plt.imsave(test_img_dir + output_dir + img + "_grayscale_" + iteration + ".png", gray, vmin=None, vmax=None, cmap='gray', format='png', origin=None, dpi=100)

    # apply gaussian blurring
    blurred_img = gaussian_blur(gray, 7)
    plt.imsave(test_img_dir + output_dir + img +  "_blurred_" + iteration + ".png", blurred_img, vmin=None, vmax=None, cmap='gray', format='png', origin=None, dpi=100)

    # canny edge detection
    canny_img = canny(blurred_img, 50, 150)
    plt.imsave(test_img_dir + output_dir +  img + "_canny_" + iteration + ".png", canny_img, vmin=None, vmax=None, cmap='gray', format='png', origin=None, dpi=100)

    # region of interest
    imshape = canny_img.shape
    vertices = np.array([[(.51 *imshape[1], .6*imshape[0]),(.49*imshape[1],...


I am working on the file "P1.ipynb".
The first task is:
Build a Lane Finding Pipeline
Build the pipeline and run your solution on all test_images. Make copies into the test_images_output directory, and you can use the images in your writeup report.
Try tuning the various parameters, especially the low and high Canny thresholds as well as the Hough lines parameters.
Can you write Python code for it?

# TODO: Build your pipeline that will draw lane lines on the test_images
# then save them to the test_images directory.
def showImage(img,cmap=None):
    # create a new figure to show each image
    plt.figure()
    # show image
    plt.imshow(img,cmap=cmap)

def detectLaneLines(image):
    # get image sizes. X=columsns & Y=rows
    x = image.shape[1]
    y = image.shape[0]

    # convert the image to gray scale
    grayImage = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
    # blur the image
    blurImage = gaussian_blur(grayImage,3)
    # perform edge detection
    cannyImage = canny(blurImage,50,150)
    # define the co-ordinates for the interested section of the image.
    # we're only interested in the bottom half where there is road
    vertices = np.array([[(x*0.15,y),(x*0.45,y*0.6),(x*0.55,y*0.6),(x*0.85,y)]],dtype=np.int32)
    # create a masked image of only the interested region
    maskedImage = region_of_interest(cannyImage, vertices)

    # detect & draw lines using hough algo on our masked image
    rho = 1 # distance resolution in pixels of the Hough grid
    theta = (np.pi/180)*1 # angular resolution in radians of the Hough grid
    threshold = 10    # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 3 #minimum number of pixels making up a line
    max_line_gap = 3    # maximum gap in pixels between connectable line segments
    houghImage = hough_lines(maskedImage,rho, theta, threshold, min_line_length, max_line_gap)

    # merge the mask layer onto the original image and return it
    return weighted_img(houghImage,image)

# read a sample image
image = mpimg.imread('test_images/solidWhiteRight.jpg')
# show detected lanes in the sample
showImage(detectLaneLines(image),cmap='gray')


# TODO: Build your pipeline that will draw lane lines on the test_images
# then save them to the test_images directory.
for image_path in test_images:
    image_path_in = os.path.join(TESTIMAGEDIR_IN, image_path)
    image_path_out = os.path.join(TESTIMAGEDIR_OUT, image_path)

    init_list()

    img = mpimg.imread(image_path_in)
    img_gray = grayscale(img)
    # cv2.imwrite(os.path.join(TESTIMAGEDIR_OUT, "grayscale_" + image_path), img_gray)

    kernel_size = 5
    img_gray_blur = gaussian_blur(img_gray, kernel_size)
    # cv2.imwrite(os.path.join(TESTIMAGEDIR_OUT, "grayscale_blur_" + image_path), img_gray_blur)

    low_threshold = 50
    high_threshold = 220
    edges = canny(img_gray_blur, low_threshold, high_threshold)
    # cv2.imwrite(os.path.join(TESTIMAGEDIR_OUT, "edges_" + image_path), edges)

    imshape = img.shape
    vertices = np.array([[(0,imshape[0]),(450, 320), (490, 320), (imshape[1],imshape[0])]], dtype=np.int32)
    edges_masked = region_of_interest(edges, vertices)

    # cv2.imwrite(os.path.join(TESTIMAGEDIR_OUT, "edges_masked_" + image_path), edges_masked)

    rho = 2 # distance resolution in pixels of the Hough grid
    theta = np.pi/180 # angular resolution in radians of the Hough grid
    threshold = 15   # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 40 #minimum number of pixels making up a line
    max_line_gap = 20    # maximum gap in pixels between connectable line segments
    # img_line = np.copy(img)*0 # creating a blank to draw lines on
    img_line = hough_lines(edges_masked, rho, theta, threshold, min_line_length, max_line_gap)
    # cv2.imwrite(os.path.join(TESTIMAGEDIR_OUT, "img_line_" + image_path), img_line)

    img_mixed = weighted_img(img_line, img)
    plt.imshow(img_mixed)
    plt.show()
    cv2.imwrite(image_path_out, img_mixed)


import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2

imagePath = "test_images/"
arrImages = os.listdir("test_images/")
figureIndex = 0
slopeAvgs = [ 0.0, 0.0, 0, 30 ]
lastLines = [ None, None ]

for anImage in arrImages:
  # Read in and grayscale the image
  image = mpimg.imread("test_images/" + anImage)
  gray = grayscale(image)

  # Define a kernel size and apply Gaussian smoothing
  kernel_size = 5
  blur_gray = gaussian_blur( gray, kernel_size )

  # Define our parameters for Canny and apply
  low_threshold = 50
  high_threshold = 150
  edges = canny(blur_gray, low_threshold, high_threshold)

  # Next we'll create a masked edges image using cv2.fillPoly()
  mask = np.zeros_like(edges)
  ignore_mask_color = 255

  # This time we are defining a four sided polygon to mask
  imshape = image.shape
  vertices = np.array([[( 95, imshape[0] ), ( 460, 310 ), ( 480, 310 ), ( 900, imshape[0])]], dtype=np.int32)
  masked_edges = region_of_interest( edges, vertices )

  # Define the Hough transform parameters
  # Make a blank the same size as our image to draw on
  rho = 1 # distance resolution in pixels of the Hough grid
  theta = np.pi/180 # angular resolution in radians of the Hough grid
  threshold = 5     # minimum number of votes (intersections in Hough grid cell)
  min_line_length = 10 #minimum number of pixels making up a line
  max_line_gap = 1    # maximum gap in pixels between connectable line segments
  line_image = np.copy(image)*0 # creating a blank to draw lines on

  # Run Hough on edge detected image
  line_image = hough_lines( masked_edges, rho, theta, threshold, min_line_length, max_line_gap, vertices, True )

  # Create a "color" binary image to combine with line image
  color_edges = np.dstack((edges, edges, edges))
  lines_edges = weighted_img( color_edges, line_image)

  plt.figure(figureIndex)
  plt.imshow(lines_edges)
  plt.figure(figureIndex + 1)
  plt.imshow(image)
  figureIndex = figureIndex + 2


I am working on the file "P1.ipynb".
The first task is:
Ideas for Lane Detection Pipeline
Some OpenCV functions (beyond those introduced in the lesson) that might be useful for this project are:
cv2.inRange() for color selection
cv2.fillPoly() for regions selection
cv2.line() to draw lines on an image given endpoints
cv2.addWeighted() to coadd / overlay two images
cv2.cvtColor() to grayscale or change color
cv2.imwrite() to output images to file
cv2.bitwise_and() to apply a mask to an image
Check out the OpenCV documentation to learn about these and discover even more awesome functionality!
Helper Functions
Below are some helper functions to help get you started. They should look familiar from the lesson!
Can you write Python code for it?

# Parameters for all helper functions
canny_low  = 50
canny_high = 150
kernel_size = 3
rho = 2                 # distance resolution of the Hough grid [pixel]
theta = np.pi/180       # angular resolution of the Hough grid [rad]
threshold = 15          # minimum number of votes (intersections in Hough grid cell)
min_line_len = 40       # minimum number of pixels making up a line
max_line_gap = 20       # maximum gap in pixels between connectable line segments
im_y = 540              # height of image
im_x = 960              # width of image
mask_y  = int(0.6*im_y) # upper y-coordinate of fillPoly trapezoidal mask region
mask_x1 = int(0.48*im_x)# upper left x-coordinate of fillPoly trapezoidal mask region
mask_x2 = int(0.54*im_x)# upper right x-coordinate of fillPoly trapezoidal mask region

left_x_prev  = []       # Holds last valid value of left_x array used in draw_lines()
left_y_prev  = []       # Holds last valid value of left_y array used in draw_lines()
right_x_prev = []       # Holds last valid value of right_x array used in draw_lines()
right_y_prev = []       # Holds last valid value of right_y array used in draw_lines()
img_dir = "test_images/"
img_out_dir="test_images_out/"

def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Or use BGR2GRAY if you read an image with cv2.imread()
    # return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region...


import math
from collections import deque

QUELENGTH = 20
slopelist_left = deque(maxlen=QUELENGTH)
slopelist_right = deque(maxlen=QUELENGTH)
xlist_left = deque(maxlen=QUELENGTH)
xlist_right = deque(maxlen=QUELENGTH)
point_left_up_last = None
point_right_up_last = None
point_left_bottom_last = None
point_right_bottom_last = None

def mean(arr):
    return sum(arr)/max(len(arr), 1.0)

def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Or use BGR2GRAY if you read an image with cv2.imread()
    # return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    #filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image

def init_list():
    global slopelist_left
    global...


import math
from scipy import stats

prev_l_x1 = None
prev_l_x2 = None
prev_r_x1 = None
prev_r_x2 = None

def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Or use BGR2GRAY if you read an image with cv2.imread()
    # return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    #filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image

def draw_lines(img, lines, color=[255, 0, 0], thickness=10):
    """
    1. Separate left and right lanes based on slope
    2. Find best fit line through linear regression
    3. Adjust the best fit line based on line from the previous frame
    4. Draw the lines
    """
    global prev_l_x1, prev_l_x2, prev_r_x1, prev_r_x2
    # fix...


zeros = np.zeros(shape=(10, 10))
nums = np.arange(0, 10)
zeros[1:4, :] = nums
print(zeros)

import math

def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    # Or use BGR2GRAY if you read an image with cv2.imread()
    # return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def create_vertices(img):
    """
    'img' is a canny transform edge image

    Adjust our vertices here to be a trapezoid
    The top of the trapezoid should be where we first detect edges from the center looking bottom-up
    Sides of the trapezoid should extend to edges (plus buffer)
    """

    ysize, xsize = img.shape[0], img.shape[1]
    bottom_ignore = ysize//6
    ybuffer = ysize//30
    xbuffer_top = xsize//50
    xbuffer_bot = xbuffer_top*2
    side_search_buffer = ybuffer//2

    # Let's find the last white pixel's index in the center column.
    # This will give us an idea of where our region should be
    # We ignore a certain portion of the bottom of the screen so we get a better region top
    #   - This is partly because car hoods can obsure the region
    center_white = img[:ysize-bottom_ignore, xsize//2] == 255
    indices = np.arange(0, center_white.shape[0])
    indices[~center_white] = 0
    last_white_ind = np.amax(indices)

    # If our first white pixel is too close to the bottom of the screen, default back to the screen center
    # region_top_y = (last_white_ind if last_white_ind < 4*ysize//5 else ysize//2) + ybuffer
    region_top_y =...


import math

def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Or use BGR2GRAY if you read an image with cv2.imread()
    # return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    #filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image

def draw_lines(img, lines, color=[255, 0, 0], thickness=5):
    """
    NOTE: this is the function you might want to use as a starting point once you want to
    average/extrapolate the line segments you detect to map out the full
    extent of the lane (going from the result shown in raw-lines-example.mp4
    to that shown in P1_example.mp4).

    Think about things like separating line segments by their
    slope...


import math

def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Or use BGR2GRAY if you read an image with cv2.imread()
    # return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    #filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image

def draw_lines(img, lines, draw_lane_ceil=.4, color=[255, 0, 0], thickness=4):
    # aim: getting extrapolated average lane lines from lane fragments we got through hough transform
    # -> get intercepts of the extrapolated hough lines

    lane_y_region=1-draw_lane_ceil # using the complement as parameter as it is more understandable (40% from the bottom up are "drawable")

    left_bottom_intercepts = []
   ...


import math
from scipy import stats

def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    #return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Or use BGR2GRAY if you read an image with cv2.imread()
    return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    #filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image

# to store the slopes & intercepts from previous frame
previous_lslope = 1
previous_lintercept = 0
previous_rslope = 1
previous_rintercept = 0

def draw_lines(img, lines, color=[255, 0, 0], thickness=8):
    """
    NOTE: this is the function you might want to use as a starting point once you want to
    average/extrapolate the line segments you detect to map out the full
    extent of the lane (going...


I am working on the file "P1.ipynb".
The first task is:
Improve the draw_lines() function
At this point, if you were successful with making the pipeline and tuning parameters, you probably have the Hough line segments drawn onto the road, but what about identifying the full extent of the lane and marking it clearly as in the example video (P1_example.mp4)?  Think about defining a line to run the full length of the visible lane based on the line segments you identified with the Hough Transform. As mentioned previously, try to average and/or extrapolate the line segments you've detected to map out the full extent of the lane lines. You can see an example of the result you're going for in the video "P1_example.mp4".
Go back and modify your draw_lines function accordingly and try re-running your pipeline. The new output should draw a single, solid line over the left lane line and a single, solid line over the right lane line. The lines should start from the bottom of the image and extend out to the top of the region of interest.
Now for the one with the solid yellow lane on the left. This one's more tricky!
Can you write Python code for it?

yellow_output = './videos/output/yellow.mp4'
clip2 = VideoFileClip('./videos/solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(yellow_output))


yellow_output = 'test_videos_output/solidYellowLeft.mp4'
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip2 = VideoFileClip('test_videos/solidYellowLeft.mp4').subclip(0,5)
clip2 = VideoFileClip('test_videos/solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(yellow_output))


fileName = 'solidYellowLeft.mp4'
yellow_output = 'test_videos_output/' + fileName
slopeAvgs = [ 0.0, 0.0, 0, 30 ]
lastLines = [ None, None ]
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip2 = VideoFileClip('test_videos/solidYellowLeft.mp4').subclip(0,5)
clip2 = VideoFileClip('test_videos/' + fileName)
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(yellow_output))


yellow_output = 'yellow.mp4'
clip2 = VideoFileClip('solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(yellow_output))


I am working on the file "P1.ipynb".
The first task is:
Let's try the one with the solid white lane on the right first ...
Can you write Python code for it?

white_output = './videos/output/white.mp4'
clip1 = VideoFileClip("./videos/solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)


white_output = 'test_videos_output/solidWhiteRight.mp4'
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip1 = VideoFileClip("test_videos/solidWhiteRight.mp4").subclip(0,5)
clip1 = VideoFileClip("test_videos/solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)


previous_lines = []
white_output = 'test_videos_output/solidWhiteRight.mp4'
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip1 = VideoFileClip("test_videos/solidWhiteRight.mp4").subclip(0,5)
clip1 = VideoFileClip("test_videos/solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)


fileName = 'solidWhiteRight.mp4'
white_output = 'test_videos_output/' + fileName
slopeAvgs = [ 0.0, 0.0, 0, 30 ]
lastLines = [ None, None ]
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip1 = VideoFileClip("test_videos/solidWhiteRight.mp4").subclip(0,5)
clip1 = VideoFileClip("test_videos/" + fileName )
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)


white_output = 'white.mp4'
clip1 = VideoFileClip("solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)


white_output = 'test_videos_output/solidWhiteRight.mp4'
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip1 = VideoFileClip("test_videos/solidWhiteRight.mp4").subclip(0,5)
init_list()
clip1 = VideoFileClip("test_videos/solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)


white_output = 'solidWhiteRight-Result.mp4'
clip1 = VideoFileClip("solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)


I am working on the file "P1.ipynb".
The first task is:
Play the video inline, or if you prefer find the video in your filesystem (should be in the same directory) and play it in your video player of choice.
Can you write Python code for it?

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(white_output))


I am working on the file "P1.ipynb".
The first task is:
Questão 6
(valor: 1.5)
Alice, Bob e Carol inventaram o seguinte jogo inspirado no par ou ímpar:

Cada um deles escolhe um número inteiro.
Se a soma dos números for um múltiplo de 3, Alice ganha o jogo.
Se a soma dos números tiver resto 1 se dividido por 3, Bob ganha o jogo
Se a soma dos números tiver resto 2 se dividido por 3, Carol ganha o jogo

O seu programa deve:
- Pedir o número de Alice
- Pedir o número de Bob
- Pedir o número de Carol
- Dizer quem ganhou
Can you write Python code for it?

alice = int(input('Insira seu número, Alice: '))
bob = int(input('Insira seu número, Bob: '))
carol = int(input('Insira seu número, Carol: '))

soma = alice+bob+carol

if soma % 3 == 0:
    print('Alice ganhou!!!')
elif soma % 3 == 1:
    print('Bob ganhou!!!')
elif soma % 3 == 2:
    print('Carol ganhou!!!')


I am working on the file "P1.ipynb".
The first task is:
Read in an Image
Can you write Python code for it?

#reading in an image
image = mpimg.imread('images/solidWhiteRight.jpg')

#printing out some stats and plotting
print('This image is:', type(image), 'with dimesions:', image.shape)
plt.imshow(image)  # if you wanted to show a single color channel image called 'gray', for example, call as plt.imshow(gray, cmap='gray')


#reading in an image
image = mpimg.imread('test_images/solidWhiteRight.jpg')

#printing out some stats and plotting
print('This image is:', type(image), 'with dimensions:', image.shape)
plt.imshow(image)  # if you wanted to show a single color channel image called 'gray', for example, call as plt.imshow(gray, cmap='gray')


#reading in an image
image = mpimg.imread('test_images/solidWhiteRight.jpg')

#printing out some stats and plotting
print('This image is:', type(image), 'with dimesions:', image.shape)
plt.imshow(image)  # if you wanted to show a single color channel image called 'gray', for example, call as plt.imshow(gray, cmap='gray')


I am working on the file "P1.ipynb".
The first task is:
Some OpenCV functions (beyond those introduced in the lesson) that might be useful for this project are:
cv2.inRange() for color selection
cv2.fillPoly() for regions selection
cv2.line() to draw lines on an image given endpoints
cv2.addWeighted() to coadd / overlay two images
cv2.cvtColor() to grayscale or change color
cv2.imwrite() to output images to file
cv2.bitwise_and() to apply a mask to an image
Check out the OpenCV documentation to learn about these and discover even more awesome functionality!
Below are some helper functions to help get you started. They should look familiar from the lesson!
Can you write Python code for it?

import math
import code
from itertools import cycle

class Line_with_info:
    def __init__(self, x1, y1,x2,y2):
        self.x1 = x1
        self.y1 = y1
        self.x2=x2
        self.y2=y2
        self.slope=(self.y2-self.y1)/(self.x2-self.x1)
        self.slope_angle=math.atan(self.slope)
    def get_line(self):
        points_data=[[self.x1,self.y1,self.x2,self.y2]];
        #code.interact(local=locals())
        #Use raise SystemExit
        return points_data
    def get_slope(self):
        return self.slope
    def get_slope_angle(self):
        return self.slope_angle

def get_distance(x1,y1,x2,y2):
    return math.sqrt(math.pow((x2-x1),2)+math.pow((y2-y1,2)))
def get_slope(x1,y1,x2,y2):
    return abs(math.atan(abs((y2-y1))/abs((x2-x1))))
def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Or use BGR2GRAY if you read an image with cv2.imread()
    # return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
   ...


I am working on the file "P1.ipynb".
The first task is:
Test Images
Build your pipeline to work on the images in the directory "test_images"
You should make sure your pipeline works well on these images before you try the videos.
Can you write Python code for it?


import os
image_names = [name for name in os.listdir("./images") if '.' in name]
image_names.sort()
print(image_names)
images = [mpimg.imread('./images/{0}'.format(name)) for name in image_names]


import os
test_img_dir = ("test_images/")

test_images = os.listdir(test_img_dir)
print(test_images)


I am working on the file "P1.ipynb".
The first task is:
Test on Images
Now you should build your pipeline to work on the images in the directory "test_images"
You should make sure your pipeline works well on these images before you try the videos.
Can you write Python code for it?


I am working on the file "P1.ipynb".
The first task is:
Test on Videos
You know what's cooler than drawing lanes over images? Drawing lanes over video!
We can test our solution on two provided videos:
solidWhiteRight.mp4
solidYellowLeft.mp4
Note: if you get an import error when you run the next cell, try changing your kernel (select the Kernel menu above --> Change Kernel).  Still have problems?  Try relaunching Jupyter Notebook from the terminal prompt. Also, check out this forum post for more troubleshooting tips.
If you get an error that looks like this:
NeedDownloadError: Need ffmpeg exe.
You can download it by calling:
imageio.plugins.ffmpeg.download()
Follow the instructions in the error message and check out this forum post for more troubleshooting tips across operating systems.
Can you write Python code for it?

# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

def process_image(image):
    # NOTE: The output you return should be a color image (3 channel) for processing video below
    # TODO: put your pipeline here,
    # you should return the final output (image where lines are drawn on lanes)

    line_image = extract_lines(image, image_vertices(image))
    lines_added = weighted_img(line_image, image, α=0.95, β=1., λ=0.)

    return lines_added


# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

def process_image(ori_image):
    image = cv2.cvtColor(ori_image, cv2.COLOR_RGB2HSV)
    image = gaussian_blur(image, 3)
    plt.imshow(image, cmap='gray')

    #select white and yellow only get the lane lines
    sensitivity = 50
    lower_white = np.array([0,0,255-sensitivity])
    upper_white = np.array([255,sensitivity,255])
    lower_yellow = np.array([20, 120, 100])
    upper_yellow = np.array([50, 170, 255])
    white = cv2.inRange(image, lower_white, upper_white)
    yellow = cv2.inRange(image, lower_yellow, upper_yellow)
    white_yellow = white + yellow

    image = cv2.bitwise_and(image,image, mask= white_yellow)
    image=cv2.cvtColor(image, cv2.COLOR_HSV2RGB)
    # convert to grayscale
    gray = grayscale(image)

    # apply gaussian blurring
    blurred_img = gaussian_blur(gray, 7)

    # canny edge detection
    canny_img = canny(blurred_img, 50, 150)

    # region of interest
    imshape = canny_img.shape
    #vertices = np.array([[(0,imshape[0]),(450, 320), (490, 320), (imshape[1],imshape[0])]], dtype=np.int32)
    vertices = np.array([[(.51 *imshape[1], .6*imshape[0]),(.49*imshape[1], .6*imshape[0]), (0, imshape[0]), (imshape[1],imshape[0])]], dtype=np.int32)
    masked_img = region_of_interest(canny_img, vertices)

    # hugh lines
    #hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap)
    rho = 1
    theta = 1 * np.pi/180
    threshold = 30 #40
    min_line_len = 5 #150
    max_line_gap = 3  #130
    hough_img = hough_lines(masked_img, rho, theta, threshold, min_line_len, max_line_gap)

    #def weighted_img(img, initial_img, α=0.8, β=1., λ=0.)
    result = weighted_img(hough_img, ori_image, α=0.8, β=1., λ=0.)
    return result

def process_image2(image):

    # convert to grayscale
    gray = grayscale(image)
    #plt.imsave(test_img_dir + output_dir + img + "_grayscale_" + iteration + ".png", gray, vmin=None,...


# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

## process_image() has been defined in the Helper Functions cell of this notebook for code reuse with testing images and video.


I am working on the file "P1.ipynb".
The first task is:
Test on Videos
You know what's cooler than drawing lanes over images? Drawing lanes over video!
We can test our solution on two provided videos:
solidWhiteRight.mp4
solidYellowLeft.mp4
Note: if you get an import error when you run the next cell, try changing your kernel (select the Kernel menu above --> Change Kernel). Still have problems? Try relaunching Jupyter Notebook from the terminal prompt. Also, consult the forums for more troubleshooting tips.
If you get an error that looks like this:
NeedDownloadError: Need ffmpeg exe.
You can download it by calling:
imageio.plugins.ffmpeg.download()
Follow the instructions in the error message and check out this forum post for more troubleshooting tips across operating systems.
Can you write Python code for it?

# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

def process_image(image):
    # NOTE: The output you return should be a color image (3 channel) for processing video below
    # TODO: put your pipeline here,
    # you should return the final output (image where lines are drawn on lanes)

    return detectLaneLines(image)


# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

def process_image(img):
    # NOTE: The output you return should be a color image (3 channel) for processing video below
    # TODO: put your pipeline here,
    # you should return the final output (image where lines are drawn on lanes)

    img = cv2.resize(img, (960, 546))
    img_gray = grayscale(img)

    kernel_size = 5
    img_gray_blur = gaussian_blur(img_gray, kernel_size)

    low_threshold = 50
    high_threshold = 220
    edges = canny(img_gray_blur, low_threshold, high_threshold)

    imshape = img.shape
    vertices = np.array([[(50,imshape[0]),(450, 320), (490, 320), (imshape[1]-50,imshape[0])]], dtype=np.int32)
    edges_masked = region_of_interest(edges, vertices)

    rho = 2 # distance resolution in pixels of the Hough grid
    theta = np.pi/180 # angular resolution in radians of the Hough grid
    threshold = 15   # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 40 #minimum number of pixels making up a line
    max_line_gap = 20    # maximum gap in pixels between connectable line segments
    img_line = hough_lines(edges_masked, rho, theta, threshold, min_line_length, max_line_gap)

    result = weighted_img(img_line, img)
    return result


# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML


# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2

def process_image(image):
  gray = grayscale(image)

  # Define a kernel size and apply Gaussian smoothing
  kernel_size = 5
  blur_gray = gaussian_blur( gray, kernel_size )

  # Define our parameters for Canny and apply
  low_threshold = 50
  high_threshold = 150
  edges = canny(blur_gray, low_threshold, high_threshold)

  # Next we'll create a masked edges image using cv2.fillPoly()
  mask = np.zeros_like(edges)
  ignore_mask_color = 255

  # This time we are defining a four sided polygon to mask
  imshape = image.shape
# Regular vid ROIs
  vertices = np.array([[( 125, imshape[0] ), ( 400, 340 ), ( 520, 340 ), ( 900, imshape[0])]], dtype=np.int32)
# Challenge ROI
#  vertices = np.array([[( 155 + 80, imshape[0] - 60 ), ( 590, 450 ), ( 750, 450 ), ( 1200 - 60, imshape[0] - 60)]], dtype=np.int32)
  masked_edges = region_of_interest( edges, vertices )

  # Define the Hough transform parameters
  # Make a blank the same size as our image to draw on
  rho = 1 # distance resolution in pixels of the Hough grid
  theta = np.pi/180 # angular resolution in radians of the Hough grid
  threshold = 10    # minimum number of votes (intersections in Hough grid cell)
  min_line_length = 15 #minimum number of pixels making up a line
  max_line_gap = 1    # maximum gap in pixels between connectable line segments
  line_image = np.copy(image)*0 # creating a blank to draw lines on

  # Run Hough on edge detected image
  line_image = hough_lines( masked_edges, rho, theta, threshold, min_line_length, max_line_gap, vertices, False )

  # Create a "color" binary image to combine with line image
  color_edges = np.dstack((edges, edges, edges))
  lines_edges = weighted_img( image, line_image)

  return lines_edges


I am working on the file "P1.ipynb".
The first task is:
Writeup and Submission
If you're satisfied with your video outputs, it's time to make the report writeup in a pdf or markdown file. Once you have this Ipython notebook ready along with the writeup, it's time to submit for review! Here is a link to the writeup template file.
Optional Challenge
Try your lane finding pipeline on the video below.  Does it still work?  Can you figure out a way to make it more robust?  If you're up for the challenge, modify your pipeline so it works with this video and submit it along with the rest of your project!
Can you write Python code for it?

challenge_output = 'test_videos_output/challenge.mp4'
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip3 = VideoFileClip('test_videos/challenge.mp4').subclip(0,5)
clip3 = VideoFileClip('test_videos/challenge.mp4')
challenge_clip = clip3.fl_image(process_image)
%time challenge_clip.write_videofile(challenge_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(challenge_output))


challenge_output = 'extra.mp4'
clip2 = VideoFileClip('challenge.mp4')
challenge_clip = clip2.fl_image(process_image)
%time challenge_clip.write_videofile(challenge_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(challenge_output))


challenge_output = 'test_videos_output/challenge.mp4'
## To speed up the testing process you may want to try your pipeline on a shorter subclip of the video
## To do so add .subclip(start_second,end_second) to the end of the line below
## Where start_second and end_second are integer values representing the start and end of the subclip
## You may also uncomment the following line for a subclip of the first 5 seconds
##clip3 = VideoFileClip('test_videos/challenge.mp4').subclip(0,5)
init_list()
clip3 = VideoFileClip('test_videos/challenge.mp4')
challenge_clip = clip3.fl_image(process_image)
%time challenge_clip.write_videofile(challenge_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(challenge_output))


I am working on the file "P1.ipynb".
The first task is:
run your solution on all test_images and make copies into the test_images directory).
Can you write Python code for it?

import math
# TODO: Build your pipeline that will draw lane lines on the test_images
# then save them to the test_images directory.
#Step1: Graying
image = mpimg.imread('test_images/solidWhiteRight.jpg')
image_gray_unfiltered=grayscale(image)
#Step2: Filteration
imshape=image_gray_unfiltered.shape
kernel_size = 5
blur_gray = gaussian_blur(image_gray_unfiltered,kernel_size)
#Step3: Edge detection
low_threshold = 50
high_threshold = 120
edges = canny(blur_gray, low_threshold, high_threshold)   #returns array edges same size of image_gray

#Step4: Region masking
low_y_limit=320
vertices = np.array([[(50,imshape[0]),(450, low_y_limit), (500, low_y_limit), (imshape[1],imshape[0])]], dtype=np.int32)
masked_edges=region_of_interest(edges,vertices)

#Step5: Line detection
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi/180 # angular resolution in radians of the Hough grid
threshold = 15     # minimum number of votes (intersections in Hough grid cell)
min_line_length = 40 #minimum number of pixels making up a line
max_line_gap = 10   # maximum gap in pixels between connectable line segments
line_image=hough_lines(masked_edges,rho, theta, threshold,min_line_length,max_line_gap,low_y_limit,imshape[0])

#Displaying
color_edges = np.dstack((edges, edges, edges))
lines_edges = weighted_img(line_image, color_edges, 0.8, 1, 0)
f, axarr = plt.subplots(2,figsize=(15,15))
axarr[0].imshow(image, cmap='gray')
axarr[1].imshow(lines_edges)


I am working on the file "P1/P1.ipynb".
The first task is:
<hr />
4. Provide one or two visualizations that show the distribution of the sample data. Write one or two sentences noting what you observe about the plot or plots.
Observation: the distribution for both are non-symmetric with a longer tail to the right
Can you write Python code for it?


I am working on the file "P1/P1.ipynb".
The first task is:
<hr />
5. Now, perform the statistical test and report your results. What is your confidence level and your critical statistic value? Do you reject the null hypothesis or fail to reject it? Come to a conclusion in terms of the experiment task. Did the results match up with your expectations?
Can you write Python code for it?

diff = (df["Incongruent"] - df["Congruent"]).describe()
print diff

std_error = diff["std"]/(diff["count"])**0.5
t_score = (diff["mean"] - 0)/std_error
print t_score


I am working on the file "P1/P1.ipynb".
The first task is:
As show in the calculation above, the t-score is 8.0207
At the statistic significance of 0.01 (or confidence level of 99%), the critial static value (df = 23) is 2.5.
Because $t = 8.02 > t_{\alpha=0.01, df=23, one-tail}=2.5, $, we reject the null hypothesis and conclude that it takes significantly longer time to conplete task with incongruent words. It is consistent with my expectation
Can you write Python code for it?

# using scipy.stats to do checking
from scipy.stats import ttest_rel
ttest_rel(df['Incongruent'], df["Congruent"])


I am working on the file "P1/P1_Cassio.ipynb".
The first task is:
4. Provide one or two visualizations that show the distribution of the sample data. Write one or two sentences noting what you observe about the plot or plots.
Can you write Python code for it?

plt.hist(
    x=[initialData[CONGRUENT], initialData[INCONGRUENT]],
    normed=False,
    range=(min(initialData[CONGRUENT]), max(initialData[INCONGRUENT])),
    bins=10,
    label='Time to name'
)

plt.hist(
    x=initialData[CONGRUENT],
    normed=False,
    range=(min(initialData[CONGRUENT]), max(initialData[CONGRUENT])),
    bins=10,
    label='Time to name'
)

plt.hist(
    x=initialData[INCONGRUENT],
    normed=False,
    range=(min(initialData[INCONGRUENT]), max(initialData[INCONGRUENT])),
    bins=10,
    label='Time to name',
    color='Green'
)

plt.hist(
    x=dataDifference,
    normed=False,
    range=(min(dataDifference), max(dataDifference)),
    bins=10,
    label='Time to name',
    color='Red'
)


I am working on the file "P1/P1_Cassio.ipynb".
The first task is:
From analyzing the histograms of both the Congruent and Incongruent datasets we can visualy see that the Incongruent dataset contains a greater number of higher time-to-name values than the Congruent datasets.
This is evident from looking at the values of the mean values of both datasets, previously calculated (14.051125 and 22.0159166667 for Congruent and Incongruent datasets, respectively)
5. Now, perform the statistical test and report your results. What is your confidence level and your critical statistic value? Do you reject the null hypothesis or fail to reject it? Come to a conclusion in terms of the experiment task. Did the results match up with your expectations?
Can you write Python code for it?

degreesOfFreedom = len(initialData[CONGRUENT]) - 1

def standardError(standardDeviation, sampleSize):
    return standardDeviation / math.sqrt(sampleSize)

def getTValue(mean, se):
    return mean / se

se = standardError(standardDeviation(variance(valuesToPower(valuesMinusMean(dataDifference), 2))), len(dataDifference))
tValue = getTValue(differenceMean, se)

def marginOfError(t, standardError):
    return t * standardError

def getConfidenceInterval(mean, t, standardError):
    return (mean - marginOfError(t, standardError), mean + marginOfError(t, standardError))

print('Degrees of Freedom:', degreesOfFreedom)
print('Standard Error:', se)
print('T Value:', tValue)
print('T Critical Regions: Less than', -TCRITICAL, 'and Greater than', TCRITICAL)
print('Is the T Value inside of the critical region?', tValue >= TCRITICAL or tValue < TCRITICAL)
print('Is p < 0.005?', tValue >= TCRITICAL or tValue < TCRITICAL)
print('Confidence Interval:', getConfidenceInterval(differenceMean, TCRITICAL, se))


I am working on the file "P10-CoupledMasses.ipynb".
The first task is:
Project 10 (option A)
Complete the analyss of three masses connected by springs of equal spring constant.
Find the eigenvalues and eigenvectors for three masses.
Use the RK4 approach as shown above to validate that the eigenvalues you compute are correct.
Can you write Python code for it?

from scipy.optimize import curve_fit

def cosFit(t, A, omega, phi):
    """
    Function def for a cosine fit
    """
    return A*np.cos(omega*t+phi)

x1a=np.array(x1list)
ta=np.array(tlist)
popt, pcov = curve_fit(cosFit, ta, x1a, p0=(0.707, np.sqrt(10.0), 0.0))

A=popt[0]
omega=popt[1]
phi=popt[2]

print("A =>", A)
print("omega**2 =>", omega**2)
print("phi =>", phi)

pl.title('Fit to find frequency.')
pl.xlabel('t')
pl.ylabel('x1')
pl.plot(ta, cosFit(ta, A, omega, phi), 'b-', label="fit")
pl.plot(ta, x1a, 'r.', label='data')
pl.legend()


I am working on the file "P10-CoupledMasses.ipynb".
The first task is:
We can also sort out what's happening using the matrix formulation developed in the slides. The eigenvalue problem:
$$\begin{bmatrix} \frac{(k_b+k_s)}{m} & -\frac{k_s}{m} \ -\frac{k_s}{m} & \frac{(k_b + k_s)}{m} \end{bmatrix} \begin{bmatrix} x_1 \ x_2 \end{bmatrix}  = \omega^2 \begin{bmatrix} x_1 \ x_2 \end{bmatrix} $$
Has solutions:
$$\omega^2 = \frac{k_b}{m},\frac{k_b+2k_s}{m}$$
Which corrspond to eigenvectors
$$\begin{bmatrix} x_1 \ x_2 \end{bmatrix} = \mathinner|+\rangle =  \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \ 1 \end{bmatrix}$$
or
$$\begin{bmatrix} x_1 \ x_2 \end{bmatrix} = \mathinner|-\rangle = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \ -1 \end{bmatrix}$$
The neat thing is that we can use the "eig" function in pylab to automaticaly find the eigenvectors and eigenvalues automatically.
See the cell below for an example!
Can you write Python code for it?

Mat = np.array([[(kb+ks)/m, -ks/m],
            [-ks/m, (kb+ks)/m]])

vals, vecs = np.linalg.eig(Mat)

print("values:", vals)
print("vec(0):", vecs[:,0])
print("vec(1):", vecs[:,1])

if not USE_VISUAL:
    pl.figure()
    ax = pl.axes()
    ax.set_aspect('equal')
    ax.arrow(0, 0, vecs[0,0], vecs[1,0], head_width=0.04, fc='k', ec='k')
    ax.arrow(0, 0, vecs[0,1], vecs[1,1], head_width=0.04, fc='k', ec='k')
    pl.xlim([-1,1])
    pl.ylim([-1,1])
    pl.xlabel("x1")
    pl.ylabel("x2")
    pl.grid()

if not USE_VISUAL:
    pl.figure()
    ax = pl.axes()
    ax.set_aspect('equal')
    ax.arrow(0, 0, vecs[0,0], vecs[1,0], head_width=0.04, fc='k', ec='k')
    ax.arrow(0, 0, vecs[0,1], vecs[1,1], head_width=0.04, fc='k', ec='k')
    pl.xlim([-0.7,1.5])
    pl.ylim([-1,1.5])
    pl.grid()

    x0 = np.array([.4,1])
    c0 = vecs[:,0].dot(x0)
    c1 = vecs[:,1].dot(x0)
    ax.arrow(0, 0, c0*vecs[0,0], c0*vecs[1,0], head_width=0.04,fc='r', ec='r')
    ax.arrow(0, 0, c1*vecs[0,1], c1*vecs[1,1], head_width=0.04,fc='b', ec='b')
    ax.arrow(0, 0, x0[0], x0[1], head_width=0.04, fc='g', ec='g')
    print("c1 = ", c1)
    print("c0 = ", c0)

if not USE_VISUAL:
    Tf = 15*2*np.pi/np.sqrt(vals[0]) # take 5 periods of the high freq
    t = np.linspace(0,Tf,500)
    x = c0*np.array([vecs[:,0]]).T*np.cos(np.sqrt(vals[0])*t) + c1*np.array([vecs[:,1]]).T*np.cos(np.sqrt(vals[1])*t)
    pl.subplot(211)
    pl.ylabel("x1")
    pl.title("motion of coupled masses")
    pl.plot(t,x[0,:],label="x1")
    pl.grid()
    pl.subplot(212)
    pl.ylabel("x2")
    pl.xlabel("t")
    pl.plot(t,x[1,:],label="x2")
    pl.grid()


I am working on the file "P10Decorators.ipynb".
The first task is:
Functions as Arguments
Now lets see how we can pass functions as arguments into other functions:
Can you write Python code for it?

def hello():
    return 'Hi Jose!'

def other(func):
    print 'Other code would go here'
    print func()

other(hello)


I am working on the file "P10Decorators.ipynb".
The first task is:
Functions within functions
Great! So we've seen how we can treat functions as objects, now lets see how we can define functions inside of other functions:
Can you write Python code for it?

def hello(name='Jose'):
    print 'The hello() function has been executed'

    def greet():
        return '\t This is inside the greet() function'

    def welcome():
        return "\t This is inside the welcome() function"

    print greet()
    print welcome()
    print "Now we are back inside the hello() function"

hello()

welcome()


I am working on the file "P10Decorators.ipynb".
The first task is:
Great! Note how we can pass the functions as objects and then use them within other functions. Now we can get started with writing our first decorator:
Creating a Decorator
In the previous example we actually manually created a Decorator. Here we will modify it to make its use case clear:
Can you write Python code for it?

def new_decorator(func):

    def wrap_func():
        print "Code would be here, before executing the func"

        func()

        print "Code here will execute after the func()"

    return wrap_func

def func_needs_decorator():
    print "This function is in need of a Decorator"

func_needs_decorator()

# Reassign func_needs_decorator
func_needs_decorator = new_decorator(func_needs_decorator)

func_needs_decorator()


I am working on the file "P10Decorators.ipynb".
The first task is:
Great! Now we can see how x is pointing to the greet function inside of the hello function.
Can you write Python code for it?


I am working on the file "P10Decorators.ipynb".
The first task is:
Here we get back a dictionary of all the global variables, many of them are predefined in Python. So let's go ahead and look at the keys:
Can you write Python code for it?


I am working on the file "P10Decorators.ipynb".
The first task is:
Lets take a quick look at the code again.
In the if/else clause we are returning greet and welcome, not greet() and welcome().
This is because when you put a pair of parentheses after it, the function gets executed; whereas if you don’t put parenthesis after it, then it can be passed around and can be assigned to other variables without executing it.
When we write x = hello(), hello() gets executed and because the name is Jose by default, the function greet is returned. If we change the statement to x = hello(name = "Sam") then the welcome function will be returned. We can also do print hello()() which outputs now you are in the greet() function.
Can you write Python code for it?


I am working on the file "P10Decorators.ipynb".
The first task is:
Note how due to scope, the welcome() function is not defined outside of the hello() function. Now lets learn about returning functions from within functions:
Returning Functions
Can you write Python code for it?

def hello(name='Jose'):

    def greet():
        return '\t This is inside the greet() function'

    def welcome():
        return "\t This is inside the welcome() function"

    if name == 'Jose':
        return greet
    else:
        return welcome

x = hello()


I am working on the file "P10Decorators.ipynb".
The first task is:
Note how s is there, the Global Variable we defined as a string:
Can you write Python code for it?


I am working on the file "P10Decorators.ipynb".
The first task is:
Now lets run our function to check for any local variables in the func() (there shouldn't be any)
Can you write Python code for it?


I am working on the file "P10Decorators.ipynb".
The first task is:
Now lets see what function is returned if we set x = hello(), note how the closed parenthesis means that name ahs been defined as Jose.
Can you write Python code for it?


I am working on the file "P10Decorators.ipynb".
The first task is:
Remember that Python functions create a new scope, meaning the function has its own namespace to find variable names when they are mentioned within the function. We can check for local variables and global variables with the local() and globals() functions. For example:
Can you write Python code for it?


I am working on the file "P10Decorators.ipynb".
The first task is:
Scope Review
Remember from the nested statements lecture that Python uses Scope to know what a label is referring to. For example:
Can you write Python code for it?

s = 'Global Variable'

def func():
    print locals()


I am working on the file "P10Decorators.ipynb".
The first task is:
This assignment is not attached to the original function:
Can you write Python code for it?


I am working on the file "P11-FourierSeries.ipynb".
The first task is:
Fourier Analysis of a Plucked String
Let's assume we're studying a plucked string with a shape like this:
\begin{eqnarray}
f(x) & = & 2 A \frac{x}{L}  &  (x<L/2) \\
\\
f(x) & = & 2 A \left(\frac{L-x}{L}\right) & (x >= L/2)
\end{eqnarray}
Let's graph that and see what it looks like:
Can you write Python code for it?

L=1.0
N=500  # make sure N is even for simpson's rule
A=1.0

def fLeft(x):
    return 2*A*x/L

def fRight(x):
    return 2*A*(L-x)/L

def fa_vec(x):
    """
    vector version
    'where(cond, A, B)', returns A when cond is true and B when cond is false.
    """
    return np.where(x<L/2, fLeft(x), fRight(x))

x=np.linspace(0,L,N)  # define the 'x' array
h=x[1]-x[0]        # get x spacing
y=fa_vec(x)
plt.title("Vertical Displacement of a Plucked String at t=0")
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x,y)
plt.grid()


I am working on the file "P11-FourierSeries.ipynb".
The first task is:
If we guess that we can express $f(x)$ as a superposition of $b_n(x)$ then we have:
\begin{equation}
f(x) = c_1 b_1(x) + c_2 b_2(x) + c_3 b_3(x) + \cdots = \sum_{n=1}^\infty c_n b_n(x)
\end{equation}
What happens if we multiply $f(x)$ with $b_m(x)$ and then integrate from $x=0$ to $x=L$?
\begin{equation}
\int_0^L f(x) b_m(x) dx = c_1 \int_0^L b_m(x) b_1(x)dx + c_2 \int_0^L b_m(x) b_2(x)dx + c_3 \int_0^L b_m(x) b_3(x)dx + \cdots
\end{equation}
or more compactly in dirac notation:
\begin{equation}
\langle m|f\rangle = c_1 \langle m|1\rangle + c_2 \langle m|2\rangle + c_3 \langle m|3\rangle + \cdots
\end{equation}
or equivalently:
\begin{equation}
\langle m|f\rangle = \sum_{n=1}^\infty c_n \langle m|n\rangle
\end{equation}
Remember that the $b_n(x)$ are orthonormal so that means:
$$\langle n|m \rangle = \int_0^L b_n(x) b_m(x)dx = \delta_{nm}$$
where $\delta_{nm}$ is defined as 0 if $n<>m$ and 1 if $n=m$.
So:
$$\sum_{n=1}^\infty c_n \langle m|n\rangle = c_m$$
or in other words:
$$c_m = \langle m|f\rangle = \int_{0}^{L} b_m(x) f(x)  dx $$
Yeah! Let's do that integral for this case. Note that the function $f(x)$ is symmetric about the midpoint, just like $b_m$ when $m$ is odd. When $m$ is even, the integral is zero. SO, for odd m we can write:
$$ c_m = \int_{0}^{L} b_m(x) f(x)  dx = 2 \int_{0}^{L/2} b_m(x) f(x)  dx $$
(when $m$ is odd) or:
$$ c_m = 2 \int_0^{L/2}  \sqrt{\frac{2}{L}} \sin(\frac{m\pi x}{L}) 2A \frac{x}{L} dx $$
$$ c_m = \frac{4A}{L} \sqrt{\frac{2}{L}} \int_0^{L/2} x\sin(\frac{m\pi x}{L}) dx $$
$$ c_m = \frac{4A}{L} \sqrt{\frac{2}{L}} \frac{L^2}{\pi^2 m^2} (-1)^{\frac{m-1}{2}}$$
Or simplifying:
$$ c_m = \frac{4A \sqrt{2L}}{\pi^2 m^2} (-1)^{\frac{m-1}{2}}$$
Can you write Python code for it?

def simpson_array(f, h):
    """

    Use Simpson's Rule to estimate an integral of an array of
    function samples

    f: function samples (already in an array format)
    h: spacing in "x" between sample points

    The array is assumed to have an even number of elements.

    """
    if len(f)%2 != 0:
        raise ValueError("Sorry, f must be an array with an even number of elements.")

    evens =  f[2:-2:2]
    odds = f[1:-1:2]
    return (f[0] + f[-1] + 2*odds.sum() + 4*evens.sum())*h/3.0

def braket(n):
    """
    Evaluate <n|f>
    """
    return simpson_array(basis(x,n)*fa_vec(x),h)

M=20
coefs = [0]
coefs_th = [0]
ys = [[]]
sup = np.zeros(N)
for n in range(1,M):
    coefs.append(braket(n))   # do numerical integral

    if n%2==0:
        coefs_th.append(0.0)
    else:
        coefs_th.append(4*A*np.sqrt(2*L)*(-1)**((n-1)/2.0)/(np.pi**2*n**2))  # compare theory

    ys.append(coefs[n]*basis(x,n))
    sup += ys[n]
    plt.plot(x,sup)

print("%10s\t%10s\t%10s" % ('n', 'coef','coef(theory)'))
print("%10s\t%10s\t%10s" % ('---','-----','------------'))

for n in range(1,M):
    print("%10d\t%10.5f\t%10.5f" % (n, coefs[n],coefs_th[n]))


I am working on the file "P11Iterators and Generators Homework.ipynb".
The first task is:
Problem 4
Explain a use case for a generator using a yield statement where you would not want to use a normal function with a return statement.
loop over a large matrix or data frame
Extra Credit!
Can you explain what gencomp is in the code below? (Note: We never covered this in lecture! You will have to do some googling/Stack Overflowing!)
Can you write Python code for it?

my_list = [1,2,3,4,5]

gencomp = (item for item in my_list if item > 3)

for item in gencomp:
    print item


I am working on the file "P12-MachineLearning.ipynb".
The first task is:
Calculating Error and updating weights
Let's look at a simple implementation of updating weights and calculating error.
Suppose we have an array w that contains the weights and we wish to compute the sum from Eq 1 for the jth row fo the 2D array x. It's easy using the dot method of linear algebra:
y_out = x[j].dot(w)

This will give us a scaler value. If the scaler is greater than, or equal to 0 we want to register that as a "yes", and if it's below zero it's a "no". The easiest way to track error is to use a numerical representation of "yes" and "no". For this exercise let's use +1 for "yes" and -1 for "no" (just like the y array).
There's a paradigm in python, similar to the c trick: cond ? a:b in python that's:
cond and a or b

Note that python only evaluates the second operand when evaluating an and operation if the first operand is True. Also, python always evaluates the second operand in an or operation if the first operand is False. So the statement
cond and a or b

will return a if cond is true and b if cond is false. So, we can get what we want using:
y_out = (x[j].dot(w)>=0.0) and +1 or -1

This compares x[j].dot(w) to zero, and returns +1 or -1 if the result is above or below zero respectively.
So, below find three functions defined, the workhorse calc_error which evaluates the error and (optionally) updates the weights as well as test and train which call calc_error to test and train the system, respectively.
The only non-trivial part of calc_error is the updating of the weights which looks like this:
w += eta*err*x[j]

x[j] is the jth input. err is the calculated error (+2 if the output is low, -2 if the output is high, and 0 if the output matches the input). Note that if the output is low and a particular input is positive, this will increase the weight for that input thereby increasing the output for this input in the future. On the other hand if the input is negative this will decrease the weight of that input, also increasing the output for this input in the future. So the sign of the...
Can you write Python code for it?

def calc_error(y_arr, x_arr, w, train=False, eta=0.1):
    """
    calc_error computes the error for each input (+1 if it's wrong, 0 if it's correct)

    y_arr is the 1D array of known outputs
    x_arr is the 2D array of inputs (N rows (number of samples), m+1 columns (number of inputs/sample))
    w is the 1D array of weights (threshold + m weights)
    train is True if training (updating of weights) is to occur as a side effect.

    Returns: The total number of errors found in iterating over all the samples
    """
    err_sum=0.0
    for j in range(len(y_arr)):
        y_out = (x_arr[j].dot(w)>=0.0) and 1 or -1  # make a prediction, +1 (versi) or -1 (vir)
        err = y_arr[j]-y_out                        # compare to known species
        err_sum += abs(err) and 1 or 0              # accumulate the total error
        if train:
            w += eta*err*x_arr[j]                   # adjust the weights during training only
    return err_sum

def train(y, x, w, eta=0.1):
    return calc_error(y, x, w, train=True, eta=eta)

def test(y, x, w, eta=0.1):
    return calc_error(y, x, w, eta=eta)

eta=0.1 # how much should we "nudge" the weights each time?
w = np.random.rand(len(x[0]))*.05  # random array of "small" weights

err_test=[]
err_train=[]
train_iters=50
for i in range(train_iters):
    err_train.append(train(y[:6], x[:6], w, eta=eta))
    err_test.append(test(y[6:], x[6:], w, eta=eta))

plt.title("Training and testing error.")
plt.plot(range(train_iters),err_train,'b', label="Train")
plt.plot(range(train_iters),err_test,'r', label="Test")
plt.xlabel("Iteration")
plt.ylabel("Error")
plt.legend()
plt.grid()

xx,yy = np.meshgrid(np.linspace(min(x[:,1])-1,max(x[:,1]+1),100),
                    np.linspace(min(x[:,2])-1,max(x[:,2]+1),100))

z=w[0]+xx*w[1] + yy*w[2]

plt.contourf(xx,yy,z,levels=[-10,0,+10])

plt.title("Fake data! Perceptron output with data")
plt.xlabel("Do you like Salt?")
plt.ylabel("How hungry are...


I am working on the file "P12-MachineLearning.ipynb".
The first task is:
Fake Data
Almost like fake news, fake data let's us make up things and try out ideas. It doesn't matter if it's really right or not, since it's all just fake! Suppose we're trying to predict if someone will want fries with their lunch. We ask "how much do you like salt?" and "how hungry are you?" The answers to these questions, on a 3 pt scale are collected, along with whether they ordered fries with their meeal:
Person | Likes Salt? | Hunger Level? | Ordered Fries?
:---:|:---:|:---:|:---:
1 | 1 | 1 | No
2 | 1 | 2 | No
3 | 2 | 1 | No
4 | 2 | 2 | Yes
5 | 2 | 3 | Yes
6 | 3 | 2 | Yes
7 | 0.3 | 0.5 | No
8 | 2.5 | 2.5 | Yes
The input data is used for the x array, and the output data is converted to +1 and -1
for the output array.
Can you write Python code for it?

x = np.array([  # x is an Nx3 (x[0] is always 1, for the threshold, x[1], and x[2]
    [1,1,1], # are the "real" inputs that correspond to actual data.
    [1,1,2], # For example x[1] could be
    [1,2,1],
    [1,2,2],
    [1,2,3],
    [1,3,2],
    [1,0.3,0.5],
    [1,2.5,2.5],
])

y = np.array([-1,-1,-1,1,1,1,-1,1])  # did they order fries? +1 -> yes, and -1 -> no.

plt.xlim(0,4)                 # plot x from 0 to 4
plt.ylim(0,4)                 # plot y from 0 to 4

ix_fries = (y==1)              # boolean, do you like fries?
ix_no_fries = np.invert(ix_fries) # boolean, inverse of ix_fries?

plt.title("Fake data, show inputs and outputs")
plt.xlabel("Do you like Salt?")
plt.ylabel("How hungry are you?")
plt.plot(x[ix_fries,1],x[ix_fries,2],'b.',label="fries")
plt.plot(x[ix_no_fries,1],x[ix_no_fries,2],'r.',label="no fries")
plt.legend()
plt.grid()


I am working on the file "P12-MachineLearning.ipynb".
The first task is:
Let's pull out only two species and take 80% for our training set and leave the rest for testing.
Can you write Python code for it?

sub_frame = df[(df['Species']=='Iris-versicolor')|(df['Species']=='Iris-virginica')] # only two species
train_ix = np.random.rand(len(sub_frame))<0.8   # an array of random booleans, 80% true
train_df = sub_frame[train_ix]        # choose a random "training" set using the random index
test_df = sub_frame[np.invert(train_ix)] # use the rest for "testing".

#
# x is a 5xN (~80 samples with 4 traits + offset in column 1)
#
x=np.array([
    np.ones(len(train_df)),
    train_df.PetalLengthCm.values,
    train_df.PetalWidthCm.values,
    train_df.SepalLengthCm.values,
    train_df.SepalWidthCm.values,]).T  # transpose to get in the right shape

#
# y is an Nx1, +1 if species == versi and -1 otherwise
#

y=np.where(train_df.Species=='Iris-versicolor',1,-1)

#
# build testing set arrays, just like training set, but using the remaining indexes
#

xt=np.array([
    np.ones(len(test_df)),
    test_df.PetalLengthCm.values,
    test_df.PetalWidthCm.values,
    test_df.SepalLengthCm.values,
    test_df.SepalWidthCm.values,]).T

yt=np.where(test_df.Species=='Iris-versicolor',1,-1)

#
# Now plot all the data to visualize what we're seeing
#

versi_df=sub_frame[sub_frame.Species=='Iris-versicolor'] # get all the versi
virg_df=sub_frame[sub_frame.Species=='Iris-virginica']   # get all the virg

plt.plot(versi_df.PetalLengthCm.values, versi_df.PetalWidthCm.values, 'r.', label="Versicolor")
plt.plot(virg_df.PetalLengthCm.values, virg_df.PetalWidthCm.values, 'b.', label="Virginica")

plt.xlabel("Petal Length")
plt.ylabel("Petal Width")
plt.title("Petal Length and Width for two Species of Iris: All Data")
plt.legend(loc=2)
plt.grid()

w = np.random.rand(len(x[0]))*.05  # random array of "small" weights
eta=.01

err_test=[]
err_train=[]
train_iters=2000
for i in range(train_iters):
    err_train.append(train(y, x, w, eta=eta))
    err_test.append(test(yt, xt, w, eta=eta))

plt.title("Training and Testing...


I am working on the file "P12Advanced Python Objects - Test.ipynb".
The first task is:
Advanced Dictionaries
Problem 7: Create this dictionary:
{0: 0, 1: 1, 2: 8, 3: 27, 4: 64}
 using dictionary comprehension.
Can you write Python code for it?


I am working on the file "P12Advanced Python Objects - Test.ipynb".
The first task is:
Advanced Sets
Problem 5: Find the elements in set1 that are not in set2:
Can you write Python code for it?


I am working on the file "P12Advanced Python Objects - Test.ipynb".
The first task is:
Advanced Strings
Problem 3: Check if every letter in the string s is lower case
Can you write Python code for it?

s = 'hello how are you Mary, are you feeling okay?'

retVal = 1
for word in s.split():
    print word
    for item in word:
        # print item
        if not item.islower():
            # print item
            print 'The string has Uppercase characters'
            retVal = 0
            break
print retVal

s.islower()


I am working on the file "P12Advanced Python Objects - Test.ipynb".
The first task is:
Problem 4: How many times does the letter 'w' show up in the string below?
Can you write Python code for it?

s = 'twywywtwywbwhsjhwuwshshwuwwwjdjdid'
s.count('w')


I am working on the file "P12Advanced Python Objects - Test.ipynb".
The first task is:
Problem 6: Find all elements that are in either set:
Can you write Python code for it?


I am working on the file "P13Interactive Widgets.ipynb".
The first task is:
From Portilla's notes
This examples clarifies how interact process its keyword arguments:

If the keyword argument is a Widget instance with a value attribute, that widget is used. Any widget with a value attribute can be used, even custom ones.
Otherwise, the value is treated as a widget abbreviation that is converted to a widget before it is used.

The following table gives an overview of different widget abbreviations:

Keyword argument	Widget

`True` or `False`	Checkbox

`'Hi there'`	Text

`value` or `(min,max)` or `(min,max,step)` if integers are passed	IntSlider

`value` or `(min,max)` or `(min,max,step)` if floats are passed	FloatSlider

`('orange','apple')` or `{'one':1,'two':2}`	Dropdown

Can you write Python code for it?

# Min,Max slider with Tuples
interact(f, x=(0,4));

# (min, max, step)
interact(f, x=(0,8,2));

interact(f, x=(0.0,10.0));

interact(f, x=(0.0,10.0,0.01));

@interact(x=(0.0,20.0,0.5))
def h(x=5.5):
    return x

interact(f, x=('apples','oranges'));

interact(f, x={'one': 10, 'two': 20});


I am working on the file "P13Interactive Widgets.ipynb".
The first task is:
Function Annotations
Can you write Python code for it?

def f(x:True): # python 3 only
    return x

from IPython.utils.py3compat import annotate

@annotate(x=True)
def f(x):
    return x

interact(f);

def f(a, b):
    return a+b

w = interactive(f, a=10, b=20)

type(w)

w.children

from IPython.display import display
display(w)

w.kwargs

w.result

from ipywidgets import *

IntSlider()

from IPython.display import display
w = IntSlider()
display(w)

display(w)


I am working on the file "P13Interactive Widgets.ipynb".
The first task is:
Note the semicolon
Can you write Python code for it?

# Generate a slider to interact with
interact(f, x=10);

interact(f, x=10,);


I am working on the file "P13Interactive Widgets.ipynb".
The first task is:
There are client-server nuances!
Can you write Python code for it?

# NO LAG VERSION
caption = widgets.Latex(value = 'The values of range1 and range2 are synchronized')

range1 = widgets.IntSlider(description='Range 1')
range2 =  widgets.IntSlider(description='Range 2')

l = widgets.jslink((range1, 'value'), (range2, 'value'))
display(caption, range1, range2)

# NO LAG VERSION
caption = widgets.Latex(value = 'Changes in source_range values are reflected in target_range1')

source_range = widgets.IntSlider(description='Source range')
target_range1 = widgets.IntSlider(description='Target range ')

dl = widgets.jsdlink((source_range, 'value'), (target_range1, 'value'))
display(caption, source_range, target_range1)

l.unlink()
dl.unlink()

import ipywidgets as widgets

# Show all available widgets!
widgets.Widget.widget_types.values()

widgets.FloatSlider(
    value=7.5,
    min=5.0,
    max=10.0,
    step=0.1,
    description='Test:',
)

widgets.FloatSlider(
    value=7.5,
    min=5.0,
    max=10.0,
    step=0.1,
    description='Test',
    orientation='vertical',
)

widgets.FloatProgress(
    value=7.5,
    min=5.0,
    max=10.0,
    step=0.1,
    description='Loading:',
)

widgets.BoundedFloatText(
    value=7.5,
    min=5.0,
    max=10.0,
    description='Text:',
)

widgets.FloatText(
    value=7.5,
    description='Any:',
)

widgets.ToggleButton(
    description='Click me',
    value=False,
)

widgets.Checkbox(
    description='Check me',
    value=True,
)

widgets.Valid(
    value=True,
)

from IPython.display import display

w = widgets.Dropdown(
    options=['1', '2', '3'],
    value='2',
    description='Number:',
)
display(w)

# Show value
w.value

w = widgets.Dropdown(
    options={'One': 1, 'Two': 2, 'Three': 3},
    value=2,
    description='Number:')

display(w)

w.value

widgets.RadioButtons(
    description='Pizza topping:',
    options=['pepperoni', 'pineapple', 'anchovies'],
)

widgets.Select(
    description='OS:',
    options=['Linux', 'Windows', 'OSX'],
)

widgets.ToggleButtons(
    description='Speed:',
    options=['Slow', 'Regular', 'Fast'],
)

w =...