Difference between revisions of "EGR 103/Fall 2021/Lab 3"

From PrattWiki
Jump to navigation Jump to search
(3.3 Script Structure)
(2 Connect)
 
(15 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
=== 1 Get the files ===
 
=== 1 Get the files ===
See [[EGR 103/Fall 2021/Beginning of Lab]] and follow those instructions for Lab 3.  Note that there are several starter files for this week in addition to the usual style file and skeleton for the lab report.  You should '''not''' worry about getting Overleaf set up at this time - that can wait until you are ready to work on the individual lab report writeup (i.e. once you are done writing the scripts and creating the graphs for that).  For this week, the Connect, APT, and Individual Gradescope parts will not require anything from Overleaf.
+
See [[EGR 103/Fall 2021/Beginning of Lab]] and follow those instructions for Lab 3.  Note that there are several starter files for this week in addition to the usual style file and skeleton for the lab report.  You should '''not''' worry about getting Overleaf set up at this time - that can wait until you are ready to work on the individual lab report writeup (i.e. once you are done writing the scripts and creating the graphs for that).  For this week, the Connect, APT, and Group Gradescope parts will not require anything from Overleaf.  You will eventually include the ''Individual'' Gradescope codes in your Overleaf project though you do not need the Overleaf project set up to start working on those.
  
 
=== 2 Connect ===
 
=== 2 Connect ===
 +
'''''Important:''''' though the Chapra book on Connect was written for MATLAB, we will be solving all the problems in Python; wherever the problems say "Write MATLAB code to ..." replace MATLAB with Python.
 
==== 2.1 Registration ====
 
==== 2.1 Registration ====
* You will need to have an account on the McGraw-Hill Connect system for this class to do this assignment.  If you already have one, you can move on to the next step.
+
* You will need to have an account on the McGraw-Hill Connect system for this class to do this assignment.  If you already have one, you can move on to the next section.
* If you do not already have an account, you need to go to [https://connect.mheducation.com/class/m-gustafson-egr-103-fall-2021-1] and click '''register now''' to get registered.
+
* If you do not already have an account, you need to go to [https://connect.mheducation.com/class/m-gustafson-egr-103-fall-2021-1 https://connect.mheducation.com/class/m-gustafson-egr-103-fall-2021-1] and click '''register now''' to get registered.
 
* There are several reading assignments and lab assignments in the Connect book; you must have your own account for this.
 
* There are several reading assignments and lab assignments in the Connect book; you must have your own account for this.
 +
 
==== 2.2 Assignments ====
 
==== 2.2 Assignments ====
 
Once you have registered, you should be able to get into the class on Connect.  At the moment, there should be one assignment - "Connect Lab 03."  When you begin an assignment, it will give you assignment details such as how many points are available and when the assignment is due.  I will generally give an extra hour for the Connect assignments just in case something happens at or just before 9PM ET on the day it is due (so the due time will look like 10 PM; that is a hard limit).  You are allowed as many attempts as needed before the due date.  You are also allowed to "Check my work" but please note that checking your work is '''not''' the same as submitting your work!  Be sure to actually submit when you are ready to turn something in.
 
Once you have registered, you should be able to get into the class on Connect.  At the moment, there should be one assignment - "Connect Lab 03."  When you begin an assignment, it will give you assignment details such as how many points are available and when the assignment is due.  I will generally give an extra hour for the Connect assignments just in case something happens at or just before 9PM ET on the day it is due (so the due time will look like 10 PM; that is a hard limit).  You are allowed as many attempts as needed before the due date.  You are also allowed to "Check my work" but please note that checking your work is '''not''' the same as submitting your work!  Be sure to actually submit when you are ready to turn something in.
Line 23: Line 25:
 
* Print the problem, or
 
* Print the problem, or
 
* Look at the references for the problem.   
 
* Look at the references for the problem.   
For this problem, you may be given different numerical values from others in the class.  You are allowed to work on these in small groups; your group should collectively talk about the process by which you are going to answer the question and then individually work on those answers.  Note that for Problem 1.13 you do not actually need to write a program (though you can use Python as a calculator to come up with the answer).  Look at the diagram and note that there are four ways water gets into the body and five ways it leaves; the metabolism arrow is a net intake.  
+
For this and other problems in Connect, you may be given different numerical values from others in the class.  You are allowed to work on these in small groups; your group should collectively talk about the process by which you are going to answer the question and then individually work on those answers.  You are allowed to compare code for these group problems.  Note that for Problem 1.13 you do not actually need to write a program (though you can use Python as a calculator to come up with the answer).  Look at the diagram and note that there are four ways water gets into the body and five ways it leaves; the metabolism arrow is a net intake of water.  
  
Figure out what you think the amount of water you would need to drink to remain at equilibrium, enter that value in the box, and note as you are entering that number that the "Check my work" box lights up. Click that box and you will be told you "only" have 100 times to check your work.  If you believe your work is correct, click "Check my work" - otherwise, click Cancel and re-calculate.  If you get the answer right, remember that later '''you must submit the assignment.'''  If you get the answer wrong, try again until you get it right.  In either case, you will need to "Return to question."
+
Figure out what you think the amount of water you would need to drink to remain at equilibrium is, enter that value in the box, and note as you are entering that number that the "Check my work" box at top right lights up. Click that box and you will be told you "only" have 100 times to check your work.  If you believe your work is correct, click "Check my work" - otherwise, click "Cancel" and re-calculate.  If you get the answer right, remember that later '''you must submit the assignment.'''  If you get the answer wrong, try again until you get it right.  In either case, you will need to "Return to question."
  
Once you get it right, you can click "Submit" at the top right immediately or wait until you have answered more questions.  As mentioned in the lab handout, I will be grading based on your '''best''' answer for '''each''' problem.  Also, when you submit, if you go back to work on other problems, your previous answers will remain in place.  For whatever reason, Connect will tell ''you'' your score based on the average score of all submissions.  That will generally be lower than what I have as a score.  A week or so after a Connect assignment is due, I will upload the grades to Sakai and then you can make sure I have your grade right for your best parts.  For right now, go ahead and make sure you have the first question correct by using "Check your work" and then go ahead and "Submit" the partial assignment. '''''Having at least a 10/50 for the Connect Lab 03 assignment by the end of lab this week will count as a 10 point homework assignment'''''.  When you click Submit, if the assignment is not yet actually completed (i.e. you haven't put answers in for everything), Connect will warn youYou can either Submit to get a partial score registered or you can Cancel and go back and fill more things in.  There is really no harm in submitting multiple times so go ahead and submit your answer to the first question now.
+
Once you get it right, you can click "Submit" at the top right immediately or wait until you have answered more questions.  As mentioned in the lab handout, I will be grading based on your '''best''' answer for '''each''' problem.  Also, when you submit, if you go back to work on other problems, your previous answers will remain in place.  For whatever reason, Connect will tell ''you'' your score based on the average score of all submissions.  That will generally be lower than what I have as a score.  A week or so after a Connect assignment is due, I will upload the grades to Sakai and then you can make sure I have your grade right for your best submissions of each problem.   
  
Once you are done with a question, you can use the navigation section at the bottom center of the screen to change questions.  "Next" will take you to the next question and "Prev" will take you to the previous one. If you click on the 3x3 grid, that will show you all the problems in a particular setFor this assignment, go ahead and click on the grid and note that there is a set of two chain links between Questions 4 and 5 -- that means that they are linked together and that you are expected to answer Question 4 before moving on to Question 5.   If you skip a linked part and Connect does not let you go back, you can simply Submit your answers to that point; when you go back to the assignment, it will let you in to the earlier Questions.  There is more information in the lab handout run-through below regarding each of the other problems in the Connect part of the lab for this week.
+
For right now, go ahead and make sure you have the first question correct by using "Check your work" and then go ahead and "Submit" the partial assignment. '''''Having at least a 10/50 for the Connect Lab 03 assignment by the end of lab this week will count as a 10 point homework assignment'''''When you click Submit, if the assignment is not yet actually completed (i.e. you have not put answers in for everything), Connect will warn you.  You can either Submit to get a partial score registered or you can Cancel and go back and fill more things in.  There is really no harm in submitting multiple times so go ahead and submit your answer to the first question now.
  
=== 3. APT ===
+
Once you are done with a question, you can use the navigation section at the bottom center of the screen to change questions.  "Next" will take you to the next question and "Prev" will take you to the previous one.  If you click on the 3x3 grid, that will show you all the problems in a particular assignment.  For this assignment, go ahead and click on the grid and note that there is a set of two chain links between Questions 4 and 5 -- that means that they are linked together and that you are expected to answer Question 4 before moving on to Question 5.  If you skip a linked part and Connect does not let you go back, you can simply Submit your answers to that point; when you go back to the assignment, it will let you in to the earlier Questions.  There is more information in the lab handout run-through below regarding each of the other problems in the Connect part of the lab for this week.
The Algorithmic Programming Testing assignments are auto-graded exercises designed and vetted by faculty and staff in Duke's Computer Science Department, and they have let me use them for EGR 103, for which I am incredibly thankful. As noted elsewhere, you are allowed to work together in small groups on APTs but you do need to have your own code and you should make sure you completely understand the solution you have come up with.  You will ''not'' be required to submit documentation or your code anywhere other than the APT site, where you will upload your code and have it graded.
+
 
 +
=== 3 APT ===
 +
The Algorithmic Programming Testing assignments are auto-graded exercises designed and vetted by faculty and staff in Duke's Computer Science Department, and they have let me use them for EGR 103, for which I am incredibly thankful! As noted elsewhere, you are allowed to work together in small groups on APTs but you do need to have your own code and you should make sure you completely understand the solution you have come up with.  You will ''not'' be required to submit documentation or your code anywhere other than the APT site, where you will upload your code and have it graded.
  
 
==== 3.1 Assignments ====
 
==== 3.1 Assignments ====
The APT assignments will be posted at the [http://classes.pratt.duke.edu/EGR103F21/ Classes] page, specifically in [http://classes.pratt.duke.edu/EGR103F21/assignments.html Assignments] at the [http://classes.pratt.duke.edu/EGR103F21/APT.html link].  The problems will generally be due at the same time as the lab for that week is due.
+
The APT assignments will be posted at the [http://classes.pratt.duke.edu/EGR103F21/ Classes] page, specifically in [http://classes.pratt.duke.edu/EGR103F21/assignments.html Assignments] at the [http://classes.pratt.duke.edu/EGR103F21/APT.html link].  The problems will generally be due at or near the same time as the lab for that week.  For now, go ahead and open the APT page in a new browser window and click on the link for the <code>Gravity</code> problem so that you can see specific examples of the APT components described below.
  
 
Each assignment will have one or more problems, and each problem will have a link to a description on the CS server.  The problem description will always have the following parts:
 
Each assignment will have one or more problems, and each problem will have a link to a description on the CS server.  The problem description will always have the following parts:
Line 45: Line 49:
 
*: This will give you information about how your function will be tested.  Basically, this puts limits on what you will be asked.
 
*: This will give you information about how your function will be tested.  Basically, this puts limits on what you will be asked.
 
* Examples
 
* Examples
*: These are...examples...showing what the returns should be for different runs of the functions you are supposed to write.  You should generally test your code with all the examples before actually uploading to the APT grading system
+
*: These are...examples...showing what the returns should be for different runs of the functions you are supposed to write.  You should generally test your code with all the examples before actually uploading to the APT grading system.  There will be a demonstration below of how to use these tests.
  
==== 3.2 Testing ====
+
==== 3.2 Uploading and Testing ====
Quick note: the APT system will require you to log in to the Duke system; if you have not logged in from the browser you are using in a while, asking the APT page to run a test will take you to the log-in page and then may display a mostly blank screen with the message: "upload issue on move. If this is the first upload after authenticating with netid, resubmit the same file."  If you get that, just go back two pages in your browser and re-run the test.
+
To check a code, you will need to click the radio button next to the name of the problem you want to solve, then click the "Choose File" button.  Find the appropriate Python file for that problem and select it.  Next, click "test/run."  Quick note: the APT system will require you to log in to the Duke system; if you have not logged in from the browser you are using in a while, asking the APT page to run a test will take you to the log-in page and then may display a mostly blank screen with the message: "upload issue on move. If this is the first upload after authenticating with netid, resubmit the same file."  If you get that, just go back two pages in your browser and click "test/run" again.  If you named your file and your function properly (including capitalization!) the APT system should give you your test results twice - once with simply pass/fail results and once again with more specific information about the tests.
  
 
==== 3.3 Script Structure ====
 
==== 3.3 Script Structure ====
Your scripts should generally include two parts: your function and your tests on the examples.  It turns out there is a specific way you should run your tests.  You could just put your tests in your script as follows:
+
Your scripts should generally include two parts: your function and your tests on the examples.   
 +
It turns out there is a specific way you should code your tests.  You ''could'' just put your tests in your script as follows (though by the end of this section, you will see why you should not):
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
# Define function
 
# Define function
Line 68: Line 73:
 
print(FUN(IN1, IN2))
 
print(FUN(IN1, IN2))
 
</syntaxhighlight>
 
</syntaxhighlight>
and that will ''work'' for APTs but you will get some extra stuff printed on your screen that yo do not need.  Also, it turns out, that will ''not'' work for Gradescope, which is sometimes very picky about any extra print statements that may arise.  To see how the APT system reacts to this kind of script, create and save a file called <code>Gravity.py</code> with the following in it:
+
and that will ''work'' for APTs but you will get some extra stuff printed on your screen that yo do not need.  Also, it turns out, that will ''not'' work for Gradescope, which is sometimes very picky about any extra print statements that may arise from your script.   
 +
 
 +
To see how the APT system reacts to this kind of script, create and save a file called <code>Gravity.py</code> with the following in it:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
# Define function
 
# Define function
Line 97: Line 104:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
The reason for that is that the APT system is actually ''importing'' your script in order to learn a function that it will call <code>Gravity.falling()</code>.  In the process of importing, however, it will also run whatever extra lines of code happen to be in your script.  To see this work,  
 
The reason for that is that the APT system is actually ''importing'' your script in order to learn a function that it will call <code>Gravity.falling()</code>.  In the process of importing, however, it will also run whatever extra lines of code happen to be in your script.  To see this work,  
* Run your <code>Gravity.py</code> script to make sure Python's working directory is the same as where your script lives,
+
* Run your <code>Gravity.py</code> script to make sure Python's working directory is the same as where your script lives, and note that the test code ran when you did that.
* Clear all the variables by clicking on the eraser or trash can icon above and on the left side of the top right (Variable explorer, usually) window (if you hover over the correct icon it will say something like "Remove all variables" - go ahead and do that but note that the working directory has not changed).
+
* Now restart the kernel.  This goes beyond just clearing the variables and basically tells Python to start from scratch: 
* Go to the ''console'' (lower right) window and type <code>import Gravity</code> and you will see that the four print statements in your script will print.  '''''Note:''''' this may only happen the ''first'' time you import Gravity during a session; newer versions of Spyder seem to ignore the non-function codes after the first import, even if variables are cleared!
+
** Find and click the triple-line or gear icon above and on the right of the console window
 +
** Select "Restart kernel" (and yes, you are sure)
 +
** When you restart the kernel, the working directory stays the same so Spyder should still be looking at your working folder for this lab.
 +
* Go to the ''console'' (lower right) window and type <code>import Gravity</code> and you will see that the four print statements in your script will print; clearly, when you import the module, the test code still runs.
 
* In the console, type <code>falling(3, 5)</code> and note that you get an error --  this is because when you ''import'' the script versus ''run'' the script the functions defined in the script have to be called by first saying what module the function was from!  This is similar to having to use something like <code>np.cos()</code> to indicate that the cos function was imported from the numpy module (with a nickname for the latter of np).   
 
* In the console, type <code>falling(3, 5)</code> and note that you get an error --  this is because when you ''import'' the script versus ''run'' the script the functions defined in the script have to be called by first saying what module the function was from!  This is similar to having to use something like <code>np.cos()</code> to indicate that the cos function was imported from the numpy module (with a nickname for the latter of np).   
 
* In the console, type type <code>Gravity.falling(3, 5)</code> and note that now you have a value of 8 that is returned.
 
* In the console, type type <code>Gravity.falling(3, 5)</code> and note that now you have a value of 8 that is returned.
* Once again, clear all the variables by clicking on the eraser or trash can icon above and on the left side of the top right (Variable explorer, usually) window.
+
* Once again, restart the kernel.
* Now type <code>import Gravity as grav</code>; depending on your version of Spyder and whether you imported Gravity previously (even if you cleared it), this may or may not produce the four lines of code on the screen.  Regardless...
+
* Now go to the console and type <code>import Gravity as grav</code>; the test code runs again!
* See if you can figure out how to ask <code>falling</code> to compute the result when $$t$$ is 3 and $$v$$ is 5 now!
+
* See if you can figure out how to ask the <code>falling</code> to compute the result when $$t$$ is 3 and $$v$$ is 5 now!
* For completeness, once again, clear all the variables by clicking on the eraser or trash can icon above and on the left side of the top right (Variable explorer, usually) window.
+
* For completeness, restart the kernel one more time to see another way of bringing the function in, then
* Now type <code>from Gravity import falling</code>; this will actually bring in the function <code>falling</code> under its own name without needing to say the module name, so try <code>falling(3, 5)</code> and make sure you get 8 again.
+
* Type <code>from Gravity import falling</code>; this will actually bring in the function <code>falling</code> under its own name without needing to say the module name.  It will also run the test code.  Type <code>falling(3, 5)</code> in the console and make sure you get 8 again.
  
Given all that, it turns out there is a way to specifically tell Python to only run certain code when you ''run'' a script and to not run it when you ''import'' a script.  When Python is running things, it generates a value for a built-in variable called <code>__name__</code>.  If you ''run'' a script, the variable <code>__name__</code> (which includes two underlines before and after) will have a value of a string containing the characters <name>__main__</code> (which also includes two underlines before and after).  If you ''import'' a script, the variable <code>__name__</code> will contain a string that indicates the name of the file from which things are imported.  To see this in action:
+
Fortunately, there is a way to specifically tell Python to only run certain code when you ''run'' a script and to not run it when you ''import'' a script.  When Python is running things, it generates a value for a built-in variable called <code>__name__</code>.  If you ''run'' a script, the variable <code>__name__</code> (which includes two underlines before and after) will be a string <code>"__main__"</code> (which also includes two underlines before and after).  If you ''import'' a script, the variable <code>__name__</code> will be a string that indicates the name of the file from which things are being imported.  To see this in action:
* Change your <code>Gravity.py</code> file to include a line at the end that prints <code>__name__</code>:<syntaxhighlight lang=python>
+
* Change your <code>Gravity.py</code> file to include a line at the end that prints <code>__name__</code>:<syntaxhighlight lang=python highlight=20>
 
# Define function
 
# Define function
 
def falling(time, velo):
 
def falling(time, velo):
Line 131: Line 141:
 
print(__name__)
 
print(__name__)
 
</syntaxhighlight>
 
</syntaxhighlight>
* To make sure the demonstration I am about to do works for all versions of Spyder and Python, restart the kernel.  This goes beyond just clearing the variables and basically tells Python to start from scratch.   
+
* Restart the kernel, then save and run your <code>Gravity.py</code> script (using F5 or the play button); the last thing on the screen should be <code>__main__</code> indicating that is the value of the variable <code>__name__</code>.
** Find and click the triple-line or gear icon above and on the right of the console window\
+
* Restart the kernel again, then in the console type <code>import Gravity</code>; this time, the value of <code>__name__</code> is <code>Gravity</code>
** Select "Restart kernel" (and yes, you are sure)
+
* Restart the kernel again, then in the console type <code>from Gravity import falling</code>; what happened?
** Save and run your <code>Gravity.py</code> script (using F5 or the play button); the last thing on the screen should be <code>__main__</code> indicating that is the value of the variable <code>__name</code>.
+
What the above exercise shows is that you may be able to use the value of the <code>__name__</code> variable to adjust how Python looks at your script -- specifically, you can have Python run your test code when you are ''running'' your script and you can have Python ignore your test code when you are ''importing'' your script.  To do that, you are going to use an <code>if</code> tree based on comparing <code>__name__</code> to <code>"__main__"</code>.  Specifically, change your <code>Gravity.py</code> to:
 +
<syntaxhighlight lang=python>
 +
# Define function
 +
def falling(time, velo):
 +
    dist = velo + time # this is wrong!
 +
    return dist
 +
 
 +
if __name__ == "__main__":  
 +
    # Run tests
 +
    t = 3
 +
    v = 5
 +
    print(falling(t, v))
 +
    t = 3
 +
    v = 0
 +
    print(falling(t, v))
 +
    t = 3600
 +
    v = 0
 +
    print(falling(t, v))
 +
    t = 86400
 +
    v = 0
 +
    print(falling(t, v))
 +
</syntaxhighlight>
 +
* Now restart the kernel and run the script (play button or F5 key) - the tests should run
 +
* Restart the kernel and type <code>import Gravity</code> into the console - the tests should ''not'' run
 +
 
 +
In general, then, for both the APT and the Gradescope assignments, you should put all your test code under a
 +
<syntaxhighlight lang=python>
 +
if __name__ == "__main__": 
 +
    # Run tests
 +
</syntaxhighlight>
 +
statement to make sure you can see the results of the tests while you are testing your code but neither the APT system nor Gradescope will see your tests while you are getting your code graded. '''''Note:''''' you will generally not need to keep restarting the kernel!  In this exercise, that was just to clearly demonstrate how running or importing a module works the first time.  In some versions of Spyder and Python, the test code always runs on import and in others it only runs the first time.
 +
 
 +
==== 3.4 Fixing Gravity ====
 +
Go ahead and work in a small group to fix the Gravity code; there is really only one line of code that you need to change.  Remember that Python uses the ** operator for powers.  Run your code to make sure all the tests are working correctly, then you can upload and test the code using the APT system.  Once that works, if you go back to the [http://classes.pratt.duke.edu/EGR103F21/APT.html APT page], you can make sure your grade was registered properly by using the "check submissions" link at the top.  This will report your most recent attempt at any APT problems you have done.  Each APT is worth 10 points (and yes, the percent should be multiplied by 100).  You should complete Gravity before the end of lab; the other APTs need to be completed by ''next'' Sunday.
 +
 
 +
=== 4 Group Gradescope ===
 +
This week, there are three programs that you should work on in small groups during lab and then submit to a system called Gradescope. '''''Note:''''' all five Gradescope problems have already been started and those codes are included in the set you got from <code>Lab03Files</code>.  Be sure to use those starter files!
 +
 
 +
==== 4.1 Accessing Gradescope ====
 +
To access Gradescope, go to [https://www.gradescope.com/ gradescope.com] and then click the "Log In" button at top right.  Once there, select the "School Credentials" button near the bottom left of the Log In panel.  Scroll down to Duke NETId and then log in with your Duke credentials.  You should now see an EGR 103 class for Fall 2021. If you go to that class, you will see five different assignments - three for "In Lab" (due in lab but certainly no later than ''this'' Sunday) and two for "Individual" (due ''next'' Sunday).  You should work on the "In Lab" assignments...in lab in small groups this week.
 +
 
 +
==== 4.2 Uploading for Gradescope ====
 +
To turn in an assignment, click the name of the assignment; for this example, imagine that you want to get <code>keep_time.py</code> checked.  Since <code>keep_time.py</code> is already a part of the files you got for this week, you can try to get the version currently available to you graded.  All you need to do is find it and drag it into the "Submit Programming Assignment" panel or click in the "DRAG & DROP" part of the panel and navigate to the code. Once found, click "Upload" and Gradescope will let you know if it got it (or not).  If it got it, it will run several tests on it and give you a grade based on that.  The starter code will actually succeed for the first test since Gradescope checks what happens if you use 0 seconds as an argument.  Every other test will fail! '''Side note:''' I accidentally reversed the words "Expected" and "received" in the test suite responses so swap those in your mind when reading the test results!  You should now have a 0.5/10 for this assignment.  You should work on fixing that and then use the "Resubmit" button. 
 +
 
 +
You can always get back to this testing panel from Gradescope by clicking on the class, then Dashboard, then the assignment.  You can see all the times you have submitted answers with the "Submission History" at the bottom left of the results page.  Near the top, there is a toggle to look at either the results or the code - if something truly strange happens with the autograder, click the "Code" button to make sure you uploaded the right thing.  You can resubmit as many times as you would like before the deadline without penalty. '''''Do not try to hardcode the test answers!'''''  Gradescope randomizes how it tests the code.  There is more information below on how to get this and the other Group Gradescope assignments done.
 +
 
 +
==== 4.3 Gradescope Grades ====
 +
Gradescope grades are not immediately transferred to Sakai; usually the grades will show up in Sakai a few days after the deadline for those assignments.  At the moment, there does not appear to be a way to look at all your grades in Gradescope at once.
 +
 
 +
=== 5 Individual Lab ===
 +
The individual lab consists of both Gradescope-graded work and work that you will document in a manner similar to that of Lab 2.  Note that you will be including just the codes for the \textit{individual} Gradescope assignments in the PDF of your lab report.  Remember that you need to submit both the PDF of your lab report and the compressed file containing the Overleaf source for the lab; that compressed folder will have all your scripts for the problems discussed in the PDF.  It will \textit{not} need to have the Connect or APT scripts in it, nor the group Gradescope scripts, though if those happen to be included in the source file that is fine too.
 +
 
 +
=== 6 Summary of Due Dates ===
 +
* End of lab (or this Sunday 9/12 at 9PM, latest):
 +
** Connect Problem 1.13 from Connect Lab 03 assignment
 +
** APT Gravity Problem
 +
** Gradescope Lab 03 In Lab problems 1-3.
 +
* Sunday 9/19 at 9PM:
 +
** Connect Lab 03 assignment
 +
** APT Assignment 1
 +
** Gradescope Lab 03 Individual problems 1-2
 +
** Individual Lab Report PDF and compressed file uploaded to Sakai
  
 
== Lab Document Supporting Information ==
 
== Lab Document Supporting Information ==
Line 150: Line 221:
 
=== 3.4 Connect Assignments ===
 
=== 3.4 Connect Assignments ===
 
The Connect assignments come from a book that focuses on MATLAB, but you will be completing the problems in Python.
 
The Connect assignments come from a book that focuses on MATLAB, but you will be completing the problems in Python.
 +
 +
==== Chapra 1.13 ====
 +
You do not have to write code for this; just figure out how much water a person needs to drink to balance out the total water intake.
  
 
==== Chapra 2.13 ====
 
==== Chapra 2.13 ====
 
Your program needs to perform the following tasks:
 
Your program needs to perform the following tasks:
* Store the Force values in an array
+
* Store the Force ($$F$$) values in an array
* Store the $$x$$ values in an array
+
* Store the Displacement ($$x$$) values in an array
* Calculate the $$k$$ values for each pair (i.e. you will have five different $$k$$ values)
+
* Calculate the $$k$$ values ''for each pair'' (i.e. you will have five different $$k$$ values)
* Calculate the potential energy for each pair  
+
* Calculate the potential energy for each pair (i.e. you will have five different $$U$$ values)
 
* Use the <code>ARRAY.max()</code> method and print this value
 
* Use the <code>ARRAY.max()</code> method and print this value
 
Notes about the differences between MATLAB and Python when you see the solution code:
 
Notes about the differences between MATLAB and Python when you see the solution code:
Line 165: Line 239:
  
 
==== Chapra 2.14 ====
 
==== Chapra 2.14 ====
Your program should store the $$n$$, $$S$$, $$B$$, and $$H$$ values in an array, then use those to calculate $$U$$.  Note that Connect will accept both floating-point and scientific notation for numbers (i.e. something like 0.3624 or 3.624e-01)
+
Your program should store the $$n$$, $$S$$, $$B$$, and $$H$$ values in an array, then use those to calculate the five $$U$$ values in one equation.  Note that Connect will accept both floating-point and scientific notation for numbers (i.e. something like 0.3624 or 3.624e-01)
  
==== Chapra 2.21 ====
+
==== Chapra 2.21 (a) and (b) ====
Your program should store the $$m$$, $$v_t$$, and $$A$$ values in arrays and the $$\rho$$ and $$g$$ values in floats.  Calculate the seven different $$C_D$$ values using the equations in Case Study 2.7 (one equation has $$c_d$$ as a function of $$m$$, $$g$$, and $$v_t$$ and another equation can be re-written to calculate $$C_D$$ as a function of $$c_d$$, $$\rho$$, and $$A$$). The <code>.min()</code>, <code>.max()</code>, and <code>.mean()</code> methods for arrays will come in handy!
+
Your program should store the $$m$$, $$v_t$$, and $$A$$ values in arrays and the $$\rho$$ and $$g$$ values in floats.  Calculate the seven different $$C_D$$ values using the equations in Case Study 2.7 (one equation has $$c_d$$ as a function of $$m$$, $$g$$, and $$v_t$$ and another equation can be re-written to calculate $$C_D$$ as a function of $$c_d$$, $$\rho$$, and $$A$$). The <code>ARRAY.min()</code>, <code>ARRAY.max()</code>, and <code>ARRAY.mean()</code> methods for arrays will come in handy!
  
 
=== 3.5 APT Problems ===
 
=== 3.5 APT Problems ===
* We will go through these in labs - be sure to take notes!  As we go through them, you will solve the first APT Problem! Here's what we have by the end of lab:
+
* Gravity
<syntaxhighlight lang=python>
+
*: Discussed in detail above!
def falling(time, velo):
+
* Bogsquare
    return velo * time + 0.5 * 9.8 * time ** 2
+
*: See [https://docs.python.org/3/library/functions.html?highlight=len#len len] at [https://docs.python.org/ Python.org]
 
+
* Cone
 
+
*: Remember **
# Alternately
+
* Grayscale
"""
+
*: rgb?  NBD!
def falling(time, velo):
 
    dist = velo*time + 0.5 * 9.8 * time**2
 
    return dist
 
"""
 
 
 
print(__name__)
 
 
 
if __name__ == "__main__":
 
    print(falling(3, 5))
 
    print(falling(0, 0))
 
    print(falling(0, 1))
 
    print(falling(1, 0))
 
 
 
</syntaxhighlight>
 
  
 
=== 3.6 In-Lab Autograded Assignments ===
 
=== 3.6 In-Lab Autograded Assignments ===
Line 204: Line 264:
  
 
==== 3.6.2 Based on P&E 1.31 ====
 
==== 3.6.2 Based on P&E 1.31 ====
* Main thing here is to figure out how to get defaults to work.  Remove the <code>*blah</code> and replace it with three inputs.  Note that two of them need default cases.
+
* Main thing here is to figure out how to get defaults to work.  Remove the <code>*blah, **more_blah</code> and replace it with three inputs.  Note that two of them need default cases.  To put a default case in the parameters of a function, put the local variable name followed by = and a default value.  For instance, if your third parameter in the function definition were <code>secs=17</code> and you called your function with only two arguments, the function would automatically assign 17 to <code>secs</code>.
* The tester does not always have three input parameters when the function gets called.
+
* The test codes do not always have three input parameters when the function gets called, meaning the defaults have to work for the tests to work.
  
 
==== 3.6.3 Saturation Function ====
 
==== 3.6.3 Saturation Function ====
 
* Be sure to include the default cases.
 
* Be sure to include the default cases.
* This can be done all in one calculation!  Note that in Lab 2 we used ARRAY.min() to get the minimum value of an array; that version does not work if you are comparing two numbers (unless you put them in an array).  Use <code>min(Thing1, Thing2)</code> instead.
+
* This can be done all in one calculation! You are allowed to do it in more than one calculation, however. Note that in Lab 2 we used ARRAY.min() to get the minimum value of an array; that version does not work if you are comparing two numbers (unless you put them in an array).  Use <code>min(Thing1, Thing2)</code> instead.
  
 
=== 3.7 Individual Post-Lab Autograded Work ===
 
=== 3.7 Individual Post-Lab Autograded Work ===
Line 215: Line 275:
  
 
==== 3.7.1 Random Integers ====
 
==== 3.7.1 Random Integers ====
* Be sure to include the default cases.
+
* The default cases are already included - make sure you understand how they work.
* Carefully consider how you the human would keep track of rolls and roll counts before trying to code it.
+
* Carefully consider how you the human would keep track of rolls and roll counts before trying to code those processes.
* <code>np.random.seed(INT)</code> is the command you need to initialize the random number generator to a particular state
+
* <code>np.random.seed(INT)</code> is the command needed to initialize the random number generator to a particular state; leave that line of code alone or else Gradescope will not be able to grade properly.
* <code>np.random.randint</code> is your friend!
+
* <code>np.random.randint</code> is your friend! See [https://numpy.org/doc/stable/reference/random/generated/numpy.random.randint.html numpy.random.randint] at [https://numpy.org/ NumPy]
 
* There are a few different ways to organize this - make sure you are clear about what you want to do before coding.
 
* There are a few different ways to organize this - make sure you are clear about what you want to do before coding.
 
* Note that the first output will have as many entries as there are dice and the second will have as many as there are sides.
 
* Note that the first output will have as many entries as there are dice and the second will have as many as there are sides.
 
* The return should be a tuple of two arrays.  If for whatever reason you ended up with a list somewhere, you can always convert a list of numerical values to an array (if the shape of the list is compatible with conversion) using <code>np.array(LIST)</code>
 
* The return should be a tuple of two arrays.  If for whatever reason you ended up with a list somewhere, you can always convert a list of numerical values to an array (if the shape of the list is compatible with conversion) using <code>np.array(LIST)</code>
* You will need a for loop for this to work.
+
* You will need a for loop for the accounting part to work and we will talk about loops in class on Friday!
  
 
==== 3.7.2 Inspired by P&E 1.39 ====
 
==== 3.7.2 Inspired by P&E 1.39 ====
 
* Note the import line at the top.
 
* Note the import line at the top.
 
* The <code>round(NUM, ndigits=DIG)</code> command will round a number NUM so that it has DIG significant digits after the decimal point.  You ''can'' have negative DIG values!  Play with the command to see what that does.  Then make sure your function is returning a value that has been properly rounded.
 
* The <code>round(NUM, ndigits=DIG)</code> command will round a number NUM so that it has DIG significant digits after the decimal point.  You ''can'' have negative DIG values!  Play with the command to see what that does.  Then make sure your function is returning a value that has been properly rounded.
* See [https://en.wikipedia.org/wiki/Passer_rating Passer rating] on Wikipedia and specifically the NFL rating in the [https://en.wikipedia.org/wiki/Passer_rating#NFL_and_CFL_formula NFL and CFL Formula] section to determine how $$a$$, $$b$$, $$c$$, and $$d$$ are computed. Note especially that those four values are bounded by [0, 2.375].  
+
* See [https://en.wikipedia.org/wiki/Passer_rating Passer rating] on Wikipedia and specifically the NFL rating in the [https://en.wikipedia.org/wiki/Passer_rating#NFL_and_CFL_formula NFL and CFL Formula] section to determine how $$a$$, $$b$$, $$c$$, and $$d$$ are computed. Note especially that those four values are bounded by [0, 2.375] - use your <code>bounded</code> function from <code>football_helper</code> to handle that; the import line is already in the code.  
* The link is [https://www.teamrankings.com/nfl/player-stat/qb-rating-nfl NFL Player Stats - QB Rating] from TeamRankings; click on a player and you will get a table with the information you need.
+
* The link to get values to check your code is [https://www.teamrankings.com/nfl/player-stat/qb-rating-nfl NFL Player Stats - QB Rating] from TeamRankings; click on a player and you will get a table with the information you need.  Put some players' values in the testing part of your script to see if it is working.
* When you want to submit this to the autograder, you must also submit your <code>football_helper.py</code> script.
+
* '''''Important:''''' When you want to submit this to the Gradescope autograder, you must also submit your <code>football_helper.py</code> script since your <code>football</code> script imports <code>bounded</code> from it.
  
 
=== 3.8 Individual Lab Report ===
 
=== 3.8 Individual Lab Report ===
Line 244: Line 304:
  
 
==== 3.8.2 Random Numbers ====
 
==== 3.8.2 Random Numbers ====
* Note that you have the code for getting a NetID, converting the NetID to a seed for the random number generator, and calculating the number of bins for the histogram.  You will need to change the <code>nums = </code> line to get input from the user.
+
* Note that you already have the code for getting a NetID, converting the NetID to a seed for the random number generator, and calculating the number of bins for the histogram.  You will need to change the <code>nums = </code> line to get input from the user.
* I also included the code for plotting histograms with a particular number of bins.   
+
* I also included the code for plotting histograms with a particular number of bins.  A histogram will basically provide an accounting of how many values in your data set fall within a particular range.  For example, we could take the birth dates of every person in the class, then bin them by which month in a year someone was born and create a histogram with 12 values: one for each month of the year.  Or we could bin them by season and have four different values.  The starter code automatically calculates how many bins to use based on the base-10 logarithm of the number of values you are looking at.  See [https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html matplotlib.pyplot.hist] from [https://matplotlib.org matplotlib.org] for more information.   
 
* I did not include the code to make the distributions.  They are in the np module, in the random groups, and the actual commands have names that completely make sense for generating uniform or normal distributions, respectively.
 
* I did not include the code to make the distributions.  They are in the np module, in the random groups, and the actual commands have names that completely make sense for generating uniform or normal distributions, respectively.
 
** Oops, I've said too much...
 
** Oops, I've said too much...
 
* I also did not include the lines about printing.  Make sure you use the correct formats!
 
* I also did not include the lines about printing.  Make sure you use the correct formats!
* There are some commands in the script or that need to go in the script that we haven't completely covered in class - we will go over the following in lab:
+
* There are some commands in the script that we haven't completely covered in class - we will go over the following in lab:
** <code>map</code>, <code>ord</code>, <code>m.ceil</code>, <code>m.log10</code>
+
** <code>m.ceil</code>: rounds a single number
*** We are going to use <code>m.ceil()</code> instead of <code>np.ceil()</code> because the Numpy version returns a float instead of an int.
+
*** We are going to use <code>m.ceil()</code> instead of <code>np.ceil()</code> because the NumPy version returns a float instead of an int and the number of bins has to be an integer for histograms to work.
 +
** <code>m.log10</code>: calculates the base 10 logarithm of a single number
  
 
===== On checking your random numbers =====
 
===== On checking your random numbers =====

Latest revision as of 16:24, 7 September 2021

Typographical errors

  • None yet!

Self-Guided Run-through

The following will take you through what you need to do during the lab period today. It will generally follow the order of the lab handout with some exceptions.

1 Get the files

See EGR 103/Fall 2021/Beginning of Lab and follow those instructions for Lab 3. Note that there are several starter files for this week in addition to the usual style file and skeleton for the lab report. You should not worry about getting Overleaf set up at this time - that can wait until you are ready to work on the individual lab report writeup (i.e. once you are done writing the scripts and creating the graphs for that). For this week, the Connect, APT, and Group Gradescope parts will not require anything from Overleaf. You will eventually include the Individual Gradescope codes in your Overleaf project though you do not need the Overleaf project set up to start working on those.

2 Connect

Important: though the Chapra book on Connect was written for MATLAB, we will be solving all the problems in Python; wherever the problems say "Write MATLAB code to ..." replace MATLAB with Python.

2.1 Registration

  • You will need to have an account on the McGraw-Hill Connect system for this class to do this assignment. If you already have one, you can move on to the next section.
  • If you do not already have an account, you need to go to https://connect.mheducation.com/class/m-gustafson-egr-103-fall-2021-1 and click register now to get registered.
  • There are several reading assignments and lab assignments in the Connect book; you must have your own account for this.

2.2 Assignments

Once you have registered, you should be able to get into the class on Connect. At the moment, there should be one assignment - "Connect Lab 03." When you begin an assignment, it will give you assignment details such as how many points are available and when the assignment is due. I will generally give an extra hour for the Connect assignments just in case something happens at or just before 9PM ET on the day it is due (so the due time will look like 10 PM; that is a hard limit). You are allowed as many attempts as needed before the due date. You are also allowed to "Check my work" but please note that checking your work is not the same as submitting your work! Be sure to actually submit when you are ready to turn something in.

When you Continue to the assignment, you will go to the first problem, which for this week is Problem 01.013 (or 1.13). At left, there are icons; you can click them to:

  • See the relevant parts of the Chapra book,
  • Get a hint about the problem,
  • Print the problem, or
  • Look at the references for the problem.

For this and other problems in Connect, you may be given different numerical values from others in the class. You are allowed to work on these in small groups; your group should collectively talk about the process by which you are going to answer the question and then individually work on those answers. You are allowed to compare code for these group problems. Note that for Problem 1.13 you do not actually need to write a program (though you can use Python as a calculator to come up with the answer). Look at the diagram and note that there are four ways water gets into the body and five ways it leaves; the metabolism arrow is a net intake of water.

Figure out what you think the amount of water you would need to drink to remain at equilibrium is, enter that value in the box, and note as you are entering that number that the "Check my work" box at top right lights up. Click that box and you will be told you "only" have 100 times to check your work. If you believe your work is correct, click "Check my work" - otherwise, click "Cancel" and re-calculate. If you get the answer right, remember that later you must submit the assignment. If you get the answer wrong, try again until you get it right. In either case, you will need to "Return to question."

Once you get it right, you can click "Submit" at the top right immediately or wait until you have answered more questions. As mentioned in the lab handout, I will be grading based on your best answer for each problem. Also, when you submit, if you go back to work on other problems, your previous answers will remain in place. For whatever reason, Connect will tell you your score based on the average score of all submissions. That will generally be lower than what I have as a score. A week or so after a Connect assignment is due, I will upload the grades to Sakai and then you can make sure I have your grade right for your best submissions of each problem.

For right now, go ahead and make sure you have the first question correct by using "Check your work" and then go ahead and "Submit" the partial assignment. Having at least a 10/50 for the Connect Lab 03 assignment by the end of lab this week will count as a 10 point homework assignment. When you click Submit, if the assignment is not yet actually completed (i.e. you have not put answers in for everything), Connect will warn you. You can either Submit to get a partial score registered or you can Cancel and go back and fill more things in. There is really no harm in submitting multiple times so go ahead and submit your answer to the first question now.

Once you are done with a question, you can use the navigation section at the bottom center of the screen to change questions. "Next" will take you to the next question and "Prev" will take you to the previous one. If you click on the 3x3 grid, that will show you all the problems in a particular assignment. For this assignment, go ahead and click on the grid and note that there is a set of two chain links between Questions 4 and 5 -- that means that they are linked together and that you are expected to answer Question 4 before moving on to Question 5. If you skip a linked part and Connect does not let you go back, you can simply Submit your answers to that point; when you go back to the assignment, it will let you in to the earlier Questions. There is more information in the lab handout run-through below regarding each of the other problems in the Connect part of the lab for this week.

3 APT

The Algorithmic Programming Testing assignments are auto-graded exercises designed and vetted by faculty and staff in Duke's Computer Science Department, and they have let me use them for EGR 103, for which I am incredibly thankful! As noted elsewhere, you are allowed to work together in small groups on APTs but you do need to have your own code and you should make sure you completely understand the solution you have come up with. You will not be required to submit documentation or your code anywhere other than the APT site, where you will upload your code and have it graded.

3.1 Assignments

The APT assignments will be posted at the Classes page, specifically in Assignments at the link. The problems will generally be due at or near the same time as the lab for that week. For now, go ahead and open the APT page in a new browser window and click on the link for the Gravity problem so that you can see specific examples of the APT components described below.

Each assignment will have one or more problems, and each problem will have a link to a description on the CS server. The problem description will always have the following parts:

  • Problem Statement
    This will be a narrative description of what you are trying to solve. This may include some domain knowledge (for instance, the equation for the distance traveled by an object thrown downward at an initial velocity of $$v$$, under the influence of Earth's gravity, after $$t$$ seconds).
  • Specification
    This will be critical information about the exact name of the file to save (including capitalization) as well as the exact name of the function that needs to be in that file (including capitalization and the names of any local variables). Be sure your file is named exactly the same as the filename given in the Specification and be sure your function definition matches the Specification exactly. You are not required to include the docstring (the triple quotes and the text between them) but you are certainly welcome to.
  • Constraints
    This will give you information about how your function will be tested. Basically, this puts limits on what you will be asked.
  • Examples
    These are...examples...showing what the returns should be for different runs of the functions you are supposed to write. You should generally test your code with all the examples before actually uploading to the APT grading system. There will be a demonstration below of how to use these tests.

3.2 Uploading and Testing

To check a code, you will need to click the radio button next to the name of the problem you want to solve, then click the "Choose File" button. Find the appropriate Python file for that problem and select it. Next, click "test/run." Quick note: the APT system will require you to log in to the Duke system; if you have not logged in from the browser you are using in a while, asking the APT page to run a test will take you to the log-in page and then may display a mostly blank screen with the message: "upload issue on move. If this is the first upload after authenticating with netid, resubmit the same file." If you get that, just go back two pages in your browser and click "test/run" again. If you named your file and your function properly (including capitalization!) the APT system should give you your test results twice - once with simply pass/fail results and once again with more specific information about the tests.

3.3 Script Structure

Your scripts should generally include two parts: your function and your tests on the examples. It turns out there is a specific way you should code your tests. You could just put your tests in your script as follows (though by the end of this section, you will see why you should not):

# Define function
def FUN(VAR1, VAR2, ...):
    CODE
    MORE CODE
    YET MORE CODE
    return THING1, THING2, THING3

# Run tests
IN1 = BLAH1
IN2 = BLAH2
print(FUN(IN1, IN2))
IN1 = BLAH3
IN2 = BLAH4
print(FUN(IN1, IN2))

and that will work for APTs but you will get some extra stuff printed on your screen that yo do not need. Also, it turns out, that will not work for Gradescope, which is sometimes very picky about any extra print statements that may arise from your script.

To see how the APT system reacts to this kind of script, create and save a file called Gravity.py with the following in it:

# Define function
def falling(time, velo):
    dist = velo + time # this is wrong!
    return dist

# Run tests
t = 3
v = 5
print(falling(t, v))
t = 3
v = 0
print(falling(t, v))
t = 3600
v = 0
print(falling(t, v))
t = 86400
v = 0
print(falling(t, v))

If you upload this to the APT system and test it, (a) all the tests will fail and (b) there will be some text between horizontal lines that looks like:

8
3
3600
86400

The reason for that is that the APT system is actually importing your script in order to learn a function that it will call Gravity.falling(). In the process of importing, however, it will also run whatever extra lines of code happen to be in your script. To see this work,

  • Run your Gravity.py script to make sure Python's working directory is the same as where your script lives, and note that the test code ran when you did that.
  • Now restart the kernel. This goes beyond just clearing the variables and basically tells Python to start from scratch:
    • Find and click the triple-line or gear icon above and on the right of the console window
    • Select "Restart kernel" (and yes, you are sure)
    • When you restart the kernel, the working directory stays the same so Spyder should still be looking at your working folder for this lab.
  • Go to the console (lower right) window and type import Gravity and you will see that the four print statements in your script will print; clearly, when you import the module, the test code still runs.
  • In the console, type falling(3, 5) and note that you get an error -- this is because when you import the script versus run the script the functions defined in the script have to be called by first saying what module the function was from! This is similar to having to use something like np.cos() to indicate that the cos function was imported from the numpy module (with a nickname for the latter of np).
  • In the console, type type Gravity.falling(3, 5) and note that now you have a value of 8 that is returned.
  • Once again, restart the kernel.
  • Now go to the console and type import Gravity as grav; the test code runs again!
  • See if you can figure out how to ask the falling to compute the result when $$t$$ is 3 and $$v$$ is 5 now!
  • For completeness, restart the kernel one more time to see another way of bringing the function in, then
  • Type from Gravity import falling; this will actually bring in the function falling under its own name without needing to say the module name. It will also run the test code. Type falling(3, 5) in the console and make sure you get 8 again.

Fortunately, there is a way to specifically tell Python to only run certain code when you run a script and to not run it when you import a script. When Python is running things, it generates a value for a built-in variable called __name__. If you run a script, the variable __name__ (which includes two underlines before and after) will be a string "__main__" (which also includes two underlines before and after). If you import a script, the variable __name__ will be a string that indicates the name of the file from which things are being imported. To see this in action:

  • Change your Gravity.py file to include a line at the end that prints __name__:
    # Define function
    def falling(time, velo):
        dist = velo + time # this is wrong!
        return dist
    
    # Run tests
    t = 3
    v = 5
    print(falling(t, v))
    t = 3
    v = 0
    print(falling(t, v))
    t = 3600
    v = 0
    print(falling(t, v))
    t = 86400
    v = 0
    print(falling(t, v))
    
    print(__name__)
    
  • Restart the kernel, then save and run your Gravity.py script (using F5 or the play button); the last thing on the screen should be __main__ indicating that is the value of the variable __name__.
  • Restart the kernel again, then in the console type import Gravity; this time, the value of __name__ is Gravity
  • Restart the kernel again, then in the console type from Gravity import falling; what happened?

What the above exercise shows is that you may be able to use the value of the __name__ variable to adjust how Python looks at your script -- specifically, you can have Python run your test code when you are running your script and you can have Python ignore your test code when you are importing your script. To do that, you are going to use an if tree based on comparing __name__ to "__main__". Specifically, change your Gravity.py to:

# Define function
def falling(time, velo):
    dist = velo + time # this is wrong!
    return dist

if __name__ == "__main__":  
    # Run tests
    t = 3
    v = 5
    print(falling(t, v))
    t = 3
    v = 0
    print(falling(t, v))
    t = 3600
    v = 0
    print(falling(t, v))
    t = 86400
    v = 0
    print(falling(t, v))
  • Now restart the kernel and run the script (play button or F5 key) - the tests should run
  • Restart the kernel and type import Gravity into the console - the tests should not run

In general, then, for both the APT and the Gradescope assignments, you should put all your test code under a

if __name__ == "__main__":  
    # Run tests

statement to make sure you can see the results of the tests while you are testing your code but neither the APT system nor Gradescope will see your tests while you are getting your code graded. Note: you will generally not need to keep restarting the kernel! In this exercise, that was just to clearly demonstrate how running or importing a module works the first time. In some versions of Spyder and Python, the test code always runs on import and in others it only runs the first time.

3.4 Fixing Gravity

Go ahead and work in a small group to fix the Gravity code; there is really only one line of code that you need to change. Remember that Python uses the ** operator for powers. Run your code to make sure all the tests are working correctly, then you can upload and test the code using the APT system. Once that works, if you go back to the APT page, you can make sure your grade was registered properly by using the "check submissions" link at the top. This will report your most recent attempt at any APT problems you have done. Each APT is worth 10 points (and yes, the percent should be multiplied by 100). You should complete Gravity before the end of lab; the other APTs need to be completed by next Sunday.

4 Group Gradescope

This week, there are three programs that you should work on in small groups during lab and then submit to a system called Gradescope. Note: all five Gradescope problems have already been started and those codes are included in the set you got from Lab03Files. Be sure to use those starter files!

4.1 Accessing Gradescope

To access Gradescope, go to gradescope.com and then click the "Log In" button at top right. Once there, select the "School Credentials" button near the bottom left of the Log In panel. Scroll down to Duke NETId and then log in with your Duke credentials. You should now see an EGR 103 class for Fall 2021. If you go to that class, you will see five different assignments - three for "In Lab" (due in lab but certainly no later than this Sunday) and two for "Individual" (due next Sunday). You should work on the "In Lab" assignments...in lab in small groups this week.

4.2 Uploading for Gradescope

To turn in an assignment, click the name of the assignment; for this example, imagine that you want to get keep_time.py checked. Since keep_time.py is already a part of the files you got for this week, you can try to get the version currently available to you graded. All you need to do is find it and drag it into the "Submit Programming Assignment" panel or click in the "DRAG & DROP" part of the panel and navigate to the code. Once found, click "Upload" and Gradescope will let you know if it got it (or not). If it got it, it will run several tests on it and give you a grade based on that. The starter code will actually succeed for the first test since Gradescope checks what happens if you use 0 seconds as an argument. Every other test will fail! Side note: I accidentally reversed the words "Expected" and "received" in the test suite responses so swap those in your mind when reading the test results! You should now have a 0.5/10 for this assignment. You should work on fixing that and then use the "Resubmit" button.

You can always get back to this testing panel from Gradescope by clicking on the class, then Dashboard, then the assignment. You can see all the times you have submitted answers with the "Submission History" at the bottom left of the results page. Near the top, there is a toggle to look at either the results or the code - if something truly strange happens with the autograder, click the "Code" button to make sure you uploaded the right thing. You can resubmit as many times as you would like before the deadline without penalty. Do not try to hardcode the test answers! Gradescope randomizes how it tests the code. There is more information below on how to get this and the other Group Gradescope assignments done.

4.3 Gradescope Grades

Gradescope grades are not immediately transferred to Sakai; usually the grades will show up in Sakai a few days after the deadline for those assignments. At the moment, there does not appear to be a way to look at all your grades in Gradescope at once.

5 Individual Lab

The individual lab consists of both Gradescope-graded work and work that you will document in a manner similar to that of Lab 2. Note that you will be including just the codes for the \textit{individual} Gradescope assignments in the PDF of your lab report. Remember that you need to submit both the PDF of your lab report and the compressed file containing the Overleaf source for the lab; that compressed folder will have all your scripts for the problems discussed in the PDF. It will \textit{not} need to have the Connect or APT scripts in it, nor the group Gradescope scripts, though if those happen to be included in the source file that is fine too.

6 Summary of Due Dates

  • End of lab (or this Sunday 9/12 at 9PM, latest):
    • Connect Problem 1.13 from Connect Lab 03 assignment
    • APT Gravity Problem
    • Gradescope Lab 03 In Lab problems 1-3.
  • Sunday 9/19 at 9PM:
    • Connect Lab 03 assignment
    • APT Assignment 1
    • Gradescope Lab 03 Individual problems 1-2
    • Individual Lab Report PDF and compressed file uploaded to Sakai

Lab Document Supporting Information

The following parts of this document follow the sections / subsections in the lab handout.

3.1 Introduction

This lab involves functions and random numbers. Every script has been started, but sometimes only with the boilerplate community standard statement. Note - none of the test_ files will be included in your lab report; they are meant to test your code and sometimes produce other files. Those other files will be included in your lab report.

3.2 Resources

See main EGR 103 page for links to these

3.3 Getting Started

3.4 Connect Assignments

The Connect assignments come from a book that focuses on MATLAB, but you will be completing the problems in Python.

Chapra 1.13

You do not have to write code for this; just figure out how much water a person needs to drink to balance out the total water intake.

Chapra 2.13

Your program needs to perform the following tasks:

  • Store the Force ($$F$$) values in an array
  • Store the Displacement ($$x$$) values in an array
  • Calculate the $$k$$ values for each pair (i.e. you will have five different $$k$$ values)
  • Calculate the potential energy for each pair (i.e. you will have five different $$U$$ values)
  • Use the ARRAY.max() method and print this value

Notes about the differences between MATLAB and Python when you see the solution code:

  • MATLAB uses arrays by default, so something like [1 2 3] in MATLAB would be np.array([1, 2, 3]) in Python
  • MATLAB uses matrix multiplication and division by default, so if you want to multiply the elements in corresponding locations together, you have to use .* and ./ in MATLAB (i.e. put a period in front of the operator). Python does element multiplication and division by default so np.array([1, 2, 3]) * np.array([4, 5, 6]) gives an array with [4, 10, 18] in it.
  • MATLAB uses .^ for raising arrays to powers, whereas Python uses ** so np.array([1, 2, 3])**2 is an array with [1, 4, 9] in it.
  • MATLAB and Python both have a built-in max() function, but for Python arrays, it is better to use the .max() method appended to the array.

Chapra 2.14

Your program should store the $$n$$, $$S$$, $$B$$, and $$H$$ values in an array, then use those to calculate the five $$U$$ values in one equation. Note that Connect will accept both floating-point and scientific notation for numbers (i.e. something like 0.3624 or 3.624e-01)

Chapra 2.21 (a) and (b)

Your program should store the $$m$$, $$v_t$$, and $$A$$ values in arrays and the $$\rho$$ and $$g$$ values in floats. Calculate the seven different $$C_D$$ values using the equations in Case Study 2.7 (one equation has $$c_d$$ as a function of $$m$$, $$g$$, and $$v_t$$ and another equation can be re-written to calculate $$C_D$$ as a function of $$c_d$$, $$\rho$$, and $$A$$). The ARRAY.min(), ARRAY.max(), and ARRAY.mean() methods for arrays will come in handy!

3.5 APT Problems

  • Gravity
    Discussed in detail above!
  • Bogsquare
    See len at Python.org
  • Cone
    Remember **
  • Grayscale
    rgb? NBD!

3.6 In-Lab Autograded Assignments

3.6.1 P&E 1.31

  • Integer division is given by // and the values can be ints or floats or a combination thereof; if either or both of the arguments are floats the answer is a float.
  • The remainder operator is given by %.
  • Think about how you need to update the number of seconds after accounting for some with hours or minutes.
  • Remember to return the three items in a tuple in the correct order.
  • Note the starter test code that has been provided for you in the script.

3.6.2 Based on P&E 1.31

  • Main thing here is to figure out how to get defaults to work. Remove the *blah, **more_blah and replace it with three inputs. Note that two of them need default cases. To put a default case in the parameters of a function, put the local variable name followed by = and a default value. For instance, if your third parameter in the function definition were secs=17 and you called your function with only two arguments, the function would automatically assign 17 to secs.
  • The test codes do not always have three input parameters when the function gets called, meaning the defaults have to work for the tests to work.

3.6.3 Saturation Function

  • Be sure to include the default cases.
  • This can be done all in one calculation! You are allowed to do it in more than one calculation, however. Note that in Lab 2 we used ARRAY.min() to get the minimum value of an array; that version does not work if you are comparing two numbers (unless you put them in an array). Use min(Thing1, Thing2) instead.

3.7 Individual Post-Lab Autograded Work

As mentioned, these are individual exercises. You should not look at anyone else's work for this nor should you allow anyone other than a TA or instructor to look at your work. Note that you can submit to the autograder as many times as you would like before the lab is due; your grade will be based on the score earned the last time the autograder ran.

3.7.1 Random Integers

  • The default cases are already included - make sure you understand how they work.
  • Carefully consider how you the human would keep track of rolls and roll counts before trying to code those processes.
  • np.random.seed(INT) is the command needed to initialize the random number generator to a particular state; leave that line of code alone or else Gradescope will not be able to grade properly.
  • np.random.randint is your friend! See numpy.random.randint at NumPy
  • There are a few different ways to organize this - make sure you are clear about what you want to do before coding.
  • Note that the first output will have as many entries as there are dice and the second will have as many as there are sides.
  • The return should be a tuple of two arrays. If for whatever reason you ended up with a list somewhere, you can always convert a list of numerical values to an array (if the shape of the list is compatible with conversion) using np.array(LIST)
  • You will need a for loop for the accounting part to work and we will talk about loops in class on Friday!

3.7.2 Inspired by P&E 1.39

  • Note the import line at the top.
  • The round(NUM, ndigits=DIG) command will round a number NUM so that it has DIG significant digits after the decimal point. You can have negative DIG values! Play with the command to see what that does. Then make sure your function is returning a value that has been properly rounded.
  • See Passer rating on Wikipedia and specifically the NFL rating in the NFL and CFL Formula section to determine how $$a$$, $$b$$, $$c$$, and $$d$$ are computed. Note especially that those four values are bounded by [0, 2.375] - use your bounded function from football_helper to handle that; the import line is already in the code.
  • The link to get values to check your code is NFL Player Stats - QB Rating from TeamRankings; click on a player and you will get a table with the information you need. Put some players' values in the testing part of your script to see if it is working.
  • Important: When you want to submit this to the Gradescope autograder, you must also submit your football_helper.py script since your football script imports bounded from it.

3.8 Individual Lab Report

Once again, these are individual exercises. You should not look at anyone else's work for this nor should you allow anyone other than a TA or instructor to look at your work. These scripts and graphs will go in a written lab report that will be uploaded to Sakai. There is no autograder for these parts.

3.8.1 Based on P&E 1.35

  • Carefully consider what the equations need to look like for the angles before writing them.
  • Test your code with some obvious triangles (hint: once you import numpy as np, you can use np.sqrt()...and you probably know some things about a 1-1-1 triangle and a 1-1-np.sqrt(2) triangle!)
  • The input values for trig functions and the output values for inverse trig function in numpy are in radians.
  • Get the math working before trying the drawing, then spend some times figuring out how to create the line (hint: it takes four points to draw a closed triangle)
  • There should be a default case for whether or not to make the drawing.
  • Make sure your graphics are set to "Automatic" - see Python:Plotting#Python_Settings
  • Do not use the plt.show() or fig.show() command in your code!

3.8.2 Random Numbers

  • Note that you already have the code for getting a NetID, converting the NetID to a seed for the random number generator, and calculating the number of bins for the histogram. You will need to change the nums = line to get input from the user.
  • I also included the code for plotting histograms with a particular number of bins. A histogram will basically provide an accounting of how many values in your data set fall within a particular range. For example, we could take the birth dates of every person in the class, then bin them by which month in a year someone was born and create a histogram with 12 values: one for each month of the year. Or we could bin them by season and have four different values. The starter code automatically calculates how many bins to use based on the base-10 logarithm of the number of values you are looking at. See matplotlib.pyplot.hist from matplotlib.org for more information.
  • I did not include the code to make the distributions. They are in the np module, in the random groups, and the actual commands have names that completely make sense for generating uniform or normal distributions, respectively.
    • Oops, I've said too much...
  • I also did not include the lines about printing. Make sure you use the correct formats!
  • There are some commands in the script that we haven't completely covered in class - we will go over the following in lab:
    • m.ceil: rounds a single number
      • We are going to use m.ceil() instead of np.ceil() because the NumPy version returns a float instead of an int and the number of bins has to be an integer for histograms to work.
    • m.log10: calculates the base 10 logarithm of a single number
On checking your random numbers

You can check your work by using mrg and 5000 for the input responses - you should get the values in the lab handout. However - this assumes you calculated the uniformly distributed numbers first and the normally distributed numbers second! Calculating them this way gives you:

Information for 5000 random numbers for mrg:
Uniform: min: +8.509e-05 avg: +5.020e-01 max: +9.998e-01
Normal:  min: -4.292e+00 avg: -7.668e-04 max: +3.634e+00

as listed in the lab manual. If you calculated them in the other order (normal and then uniform), you would get:

Information for 5000 random numbers for mrg:
Uniform: min: +6.652e-05 avg: +5.001e-01 max: +1.000e+00
Normal:  min: -3.272e+00 avg: -8.981e-03 max: +3.660e+00

and the histograms will be slightly different as well. We will accept either set of answers! Also - this has not been tested on different platforms - if the random number generators behave differently on OSX, Win, and Linux, I will note that here. So far - no evidence of differences.

General Concepts

This section is not in the lab report but rather has some items in it that span multiple problems in the lab. We will cover these during the lab.

  • If your script's main job is to define one or more functions, and you want to put test code in the script, a great way to do that is to put the tester code at the bottom in a section headed by the statement:
if __name__ == "__main__":
That way the test code will only run when you run the script and not when you import the script.
  • Please do not make any changes at all to the test_ file! Also, do not include it in your lab report. Here's some information about how it works, though:
    • The fo=open() command opens a file for writing - this is how you can "print" into a file
    • The scrsave() function will take a string and send it two places - the screen with a print() function and a file with the fo.write() function. This was so I only had to generate the string once to have it show up on the screen and in the file.
    • The seeding will take a string, convert it to the mapping of the ord() function on the letters in the string, then add them all together.
    • The try...except structure will...try to do something. If it can - great! If not, it stores the problem in ex and then prints it. If your code doesn't work, my tester will still run.
    • The fo.close() command closes the file for writing
  • For random numbers, the key commands we need for this week are:
Class Document Protection