CSI Lab 11

Tuesday, April 1st


Another lab where you can work with a partner

For this week's lab you are STRONGLY encouraged to work with a partner. 

The advantage of working with a partner is that two heads are better than one, and you can benefit from discussing the problems with a partner. 

Before you begin, decide between yourselves who will be Partner A and who will be Partner B.  Please notice as the lab goes on that you will change roles several times.  Sometimes you will be the "coder" - the person in charge of the keyboard - and sometimes you will be the "supervisor" - the person in charge of telling the coder what to write.  In order to make this activity work, it is important that you change these roles a couple of times during the class.

You may feel free to turn in ONE signed form for the team, just include the names of BOTH team members on the form.


Introduction

No fooling!

This week's lab is designed to allow you to work with several of the design issues we have talked about in class the last couple of lectures.  You will be working with existing python file (lab11.py) which contains a bunch of code already.  Most of this lab will involve you fixing this code to work correctly or even just "better".

To begin with, download the following

And extract this to your p: drive.


Activity A : Fixing/Cleaning Andy Warhol

One of your classmates created a VERY nice image with his andyWarhol() method for PA05.

Just to confirm that everything is working properly type:

 

 >>> setMediaPath()  # set this to the folder you just extracted
 >>> output = andyWarhol()
 >>> show(output)

At that point you should see:

 

As Borat would say "Very Nice!"

Now open up the code for the method and see how it works.  I don't think this will be too difficult, because the Original Author (OA) did a nice job of laying things out and using some meaningful helper method names. 

[Q1]  In your own words, describe the steps the OA uses to create the finished image.

 

While I think this code is pretty good, I see two areas where we could clean this up and make it even MORE readable and useful.

 

Change #1 - Replace the 9 helper methods with one helper method  [Partner A codes]

Towards the bottom of the andyWarhol() method the OA makes 9 calls to 9 different helper methods.  These helper methods are really pretty good because they DO improve the readability of his code.  You know pretty quickly what is going on.

However, look at two of those different helper methods:

def yellowPurple(picture):
   for x in range(107,213):
     for y in range(1,161):
       pixel = getPixel(picture,x,y)
       if distance(black, getColor(pixel)) < 255:
         setColor(pixel,yellow)
       else:
         setColor(pixel,makeColor(110,15,240))

def blueRed(picture):
   for x in range(212,319):
     for y in range(1,161):
       pixel = getPixel(picture,x,y)
       if distance(black, getColor(pixel)) < 255:
         setColor(pixel,blue)
       else:
         setColor(pixel,red)

 

If you look carefully you will see that there is not much difference between these two methods.   A couple of numbers and a couple of colors.

Replace these 9 helper methods with a single helper method of your naming.  This method should take in any information that separates on version from the other.  Once you have done this, you will need to replace the calls to the 9 different methods with the different calls to this one method. 

This change will not actually make the andyWarhol() method any smaller.  It WILL however, make the entire lab11.py file smaller (not to mention, easier to change).

 

Change #2 - Add more loops to eliminate "repeated" code [ [Partner B codes]

The other change I would like you to make isn't nearly as obvious at first, but it does make the code a little cleaner over all.

Notice that in the original andyWarhol() method there are 9 calls to setColor(). 

  for x in range(1,107):
    for y in range(1,161):
      andyPixel=getPixel(andy,x,y)    
      setColor(getPixel(canvas,106+x,y) ,getColor(andyPixel))
      setColor(getPixel(canvas,212+x,y) ,getColor(andyPixel))
      setColor(getPixel(canvas,318+x,y),getColor(andyPixel))
      setColor(getPixel(canvas,106+x,160+y) ,getColor(andyPixel))
      setColor(getPixel(canvas,212+x,160+y),getColor(andyPixel))
      setColor(getPixel(canvas,318+x,160+y) ,getColor(andyPixel))
      setColor(getPixel(canvas,106+x,320+y)  ,getColor(andyPixel))
      setColor(getPixel(canvas,212+x,320+y),getColor(andyPixel))
      setColor(getPixel(canvas,318+x,320+y),getColor(andyPixel))

Notice that there is a pattern to the values provided to these 9 calls to setColor().  There is a distinct realtionship between the calls.

For this change, see how you can use additional loops to replace the 9 calls to setColor() with only one call to setColor() that uses values from those loops.  If you get stuck, you might check out this hint, but see what you can do on your own first.

 

[SIG1]  When you have andyWarhold() "improved", show your code to a TA.

 


Activity B : Fixing/Cleaning Art Collage [Partner A codes]

A different classmate created a VERY nice image with his artCollage() method for PA05.

Just to confirm that everything is working properly type:

 >>> setMediaPath()  # set this to the folder you just extracted
 >>> output = artCollage()
 >>> show(output)

At that point you should see:

 

Another sweet graphic produced.

Look at the code that produced this.You will see one, BIG, LONG method.

The instructions for this our simple.  Break the long method down into a shorter one by creating a series of well named helper methods and simply using the helper methods.  This will produce an artCollage() method that is only ~20 lines long but will produce a reasonable number of helper methods. 

I think a first pass at cleaning up this code would produce about 10 or 11 helper methods.  However, notice that, like Activity A that originally had 9 helper methods, several of these are simple variations of the same helper method.  In the end I think you can do this with only a couple of helper methods (the exact number depends on you but is probably 4 or 5?)

 

[SIG2]  When you have artCollage() "improved", show your code to a TA.


Activity C : Fixing Lake Effect [Partner B codes]

The final chunk of existing code in my starter code is a non-correct version of lakeEffect()  In the end, of those students who tried this, only one person actually had code that worked correctly. 

The code that I provided you in Lab11.py is an example of some non-working code.

Look carefully at the provided code for lakeEffect() and my original description from PA05.

I actually see several places where this code can be improved.  Some are places where the code needs to be fixed so that it works (for example, this code produces several blank rows/columns which shouldn't happen).  Others are places where the code "works" but does things it doesn't need to do.

Work on "fixing" the lakeEffect() so that it works and is as efficient as possible.

[SIG3]  When you feel you have lakeEffect() working, show your code to a TA.

 


Activity D : Doing some testing [Based on time.  You pick who codes]

If you finish Activity A, B, and C and there is still time remaining (some of you will finish quickly while others will struggle at times) then I would like you to attempt the following activity.

Create a method called letterGrade().  Letter grade should take in a number which represents a score on an exam. 

The method letterGrade() should expect a score and print an appropriate message based on the score.  Something like

 

 >>> letterGrade(95)
  Congratulations.  A 95 is an A!
 >>> letterGrade(82)
  Wow.  82 is a B!
 >>> letterGrade(79.4)
  79.4 is a C.  Try a bit harder next time.
 ...
  

[SIG4] When you feel you have letterGrade() working, show this to a TA.