Homework Assignment 1#

Git practice, debugging practice, and new Python packages, and Python classes#

✅ Put your name here.

#

✅ Put your GitHub username here.

#

Goals for this homework assignment#

By the end of this assignment, you should be able to:

  • Use Git to create a repository, track changes to the files within the repository, and push those changes to a remote repository.

  • Debug some basic Python code that involves Pandas.

  • Read documentation and example code to use a new Python package

  • Modify and use a simple Python class

Work through the following assignment, making sure to follow all of the directions and answer all of the questions.

There are 54 points possible on this assignment. Point values for each part are included in the section headers and question prompts.

This assignment is due at 11:59 pm on Friday, September 27. It should be uploaded into the “Homework Assignments” submission folder for Homework #1 on D2L. Submission instructions can be found at the end of the notebook. You must also fill out a survey regarding this assignment. The link to this survey can also be found at the end of the notebook.

Table of contents#

  1. Part 0: Office Hours and Help Room (6 points)

  2. Part 1: Git and CLI (10 points)

  3. Part 2: Pandas (11 points)

  4. Part 3: Using documentation to use a new Python package (8 points)

  5. Part 4: Practice with using Python classes (12 points)

  6. Part 5: Finishing (4 points)

(3 points for periodic commits)


Back to ToC

Part 0: Visiting Office Hours or Help Room (6 points)#

Going to Office Hours or Help Room#

Why are we doing this?#

We want to make sure that everyone knows how to access the resources available to you. One of the best resources you have at your disposal is office hours and help room.

What will you do?#

(At minimum) Go to one office hour or help room session ​(it doesn’t matter which one you go to). Come with one question that you would like to talk about. It can be big or small. It can be about the homework, but it doesn’t have to be. It can be anything about the course or about computational modeling and data analysis in general.

Once you get to office hours or help room, ask your question. All of the instructors for CMSE 202 (Professors, TAs, and LAs) will be adding to a running list of folks that we see during office hours; as long as your name appears on the list, you’ll get credit for this part of Homework 1.

NOTE: The day when the homework is due (Friday, September 27 at 11:59pm) will be the busiest time for folks to go to office hours or help room. You are STRONGLY encouraged to go to office hours or help room before Friday to get credit for this part of this assignment. (You should still feel free to go to office hours or helproom on Friday for help, though!)

You can find the office hours calendar on the course website.

FINAL NOTE: If you are unable to attend office hours or help room, please contact the instructor to make alternative arrangements and explain why you are unable to attend.

Question 0.1 (6 points)

Type below the question you asked and who you asked it to (make sure you know who you’re talking to!). Make sure you double-check that the instructor made note of this.

If you did not attend office hours or help room, please explain why.

Put your question here.

Put the instructor name here of the person you spoke with


Back to ToC

Part 1: CLI and Git (10 points)#

Setting up a new folder in your Git repository and adding your HW1#

git is a very important professional tool and we want you to get plenty of practice using it. The following set of questions check your understanding of using Git and the command line by having you add, commit, and push your homework file in your cmse202-f24-turnin repository. You will share this repo with your course lead instructor and TA so that they can pull your completed assignments for grading. Additionally, please verify that your repository is setup as a private repository rather than a public repository.

Question 1.1 (2 points):

  1. Navigate to your turnin repository on Github and add your instructor and TA as a collaborator for the repository. This step is very important since we will need access to your repository to check the status of commits for homework assignments throughout the semester. To show you’ve done this, write the Github usernames of the instructor and TA below.

### Write the Github usernames here ###

Question 1.2 (2 points):

  1. If you haven’t already. Clone your cmse202-f24-turnin repository onto JupyterHub or your computer and paste the code you used to clone the repository below. Even if you already have it cloned, paste the code you would use to clone the repository below.

# Put the command you used to clone the repository here

Question 1.3 (1 point): Open a terminal. Then using the command line interface, move inside the repository folder

What command did you use to enter into the folder?

# Put the command to move into a new directory here.

Question 1.4 (1 point): Once inside the cmse202-f24-turnin repository, create a new folder called hw-01.

What is the command to create the new folder ?

# Put the command to create the folder/directory here

Question 1.5 (1 point): Move this notebook into that new directory in your repository. Move the file using the command line. Paste the code you used to move the file below.

This is an important step: you’ll want to make sure you save and close the notebook before you do this step and then re-open it once you’ve added it to your repository. If you don’t do this, you could end up working on the wrong version of the notebook! Once you’ve moved the notebook correctly, re-open it and continue working on it.

# Put the command you used to move your homework 1 file into the hw-01 folder. 

Question 1.6 (3 points): Now that you have the file moved into your repository in hw-01. Add, commit, and push your updates to GitHub. Record all the code you used to add, commit, and push your changes below.

# Put your code here. 

Before moving on…#

Important: Make sure you’ve added your Professor and your TA as collaborators to your new “turnin” respository with “Read” access so that they can see your assignment.

Double-check the following: Make sure that the version of this notebook that you are working on is the same one that you just added to your repository! If you are working on a different copy of the notebook, none of your changes will be tracked.

If everything went as intended, the file should now show up on your GitHub account in the “cmse202-f24-turnin” repository inside the hw-01 directory that you just created. Periodically, you’ll be asked to commit your changes to the repository. By the end of the assignment you should have multiple commits that correspond to your completion of each section (as specified below). Of course, you can always commit your changes more often than that, if you wish. It can be good to get into a habit of committing your changes any time you make a significant modification, or when you stop working on the project for a bit.


Back to ToC

Part 2: Using Pandas to Explore Data (11 points)#

Reading in a data set with Pandas and using Pandas to explore the data.#

In this section, you will practice reading in a data set using with Pandas and use pandas functions to explore the dataset. You will explore the auto_price.csv file, which is a commonly used data set in the field of machine learning and data science which has data about many different cars and their prices.

Import Pandas before moving on!#

# Import Pandas and matplotlib
import pandas as pd 

Questions 2.1 (2 points): Use Pandas to load in the auto_price.csv file as a DataFrame and then display the first 10 lines of the file. You can download the file using curl with https://raw.githubusercontent.com/hoolagans/CMSE202_FS24/main/auto_price.csv.

# Put code here

Questions 2.2 (2 points): Use the Pandas describe function to describe the data. Once the data is described, record the max length, mean width, and standard deviation of the curb-weight in the cell below.

# Put code here

Record your observations here

max length=

mean width=

standard deviation curb-weight=

Questions 2.3 (2 points): Now create a new DataFrame using just the length and horsepower columns of your current DataFrame. Call this DataFrame new_df. Confirm that the new DataFrame contains just those two columns by displaying the first 4 rows.

# Put code here

Questions 2.4 (2 points): Using the DataFrame created in the previous question. Create a scatter plot showing length vs horsepower with length as the x-axis and horsepower as the y-axis. Hint Look up the documentation for Pandas plot function.

# Put code here

Questions 2.5 (1 points): Do you notice any pattern in the plot from the previous question? Briefly describe any pattern you notice in the cell below.

Record your observations here

Questions 2.6 (2 points): Now using the new_df DataFrame, filter for just cases where horsepower is greater than 160. Show the code you used to filter the DataFrame and then record the number of cases in the DataFrame that passed the filter.

# Put code here

The number of cases with horsepower greater than 160 is [put number here]


🛑 STOP#

Pause to add and commit your changes to your Git repository!

Take a moment to save your notebook, commit the changes to your Git repository using the commit message “Committing Part 2”, no need to push the changes to GitHub, but you can if you want. Remember to add the ‘auto_price.csv’ file if you haven’t already. (1 point)


Back to ToC

Part 3: Working with a less familiar Python package (8 points)#

In this part of the assignment, you will install a Python package that you probably have not used before, and use it to perform simple tasks. Specifically, you will use a package called Pillow, which is capable of performing imaging tasks. From the Pillow documentation:

The Python Imaging Library adds image processing capabilities to your Python interpreter.

This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.

The core image library is designed for fast access to data stored in a few basic pixel formats. It should provide a solid foundation for a general image processing tool.

The documentation for Pillow can be found at https://pillow.readthedocs.io/en/stable/. In particular, the Pillow tutorial section will be very helpful for this part: https://pillow.readthedocs.io/en/stable/handbook/tutorial.html

Question 3.1 (1 point): If you don’t already have the pillow package installed, what command could you use to install it? (you should run this command on the command line, if you need to).

If you do already have it installed, what command did you use to install it?

# Put code here

Once Pillow is installed, running the following cell should not result in an error message. You might need to restart your Jupyter kernel after installing the package for this to work. (Once everything works, it should give you no output at all.)

import PIL

Question 3.2 (2 point): In the Pillow tutorial, there is an example showing how to use Pillow to load in an image.

Do this: Modify that example to load in the ‘logo.png’ file. You do not need to display the image, just load it in and save it to a variable. To access the logo.png image you can download it with curl from https://raw.githubusercontent.com/hoolagans/CMSE202_FS24/main/logo.png

### Put your code here

Question 3.3 (2 points): Now following the tutorial, create a new image called im_rotate that is rotated 180 degrees using the ‘rotate’ function in Pillow.

### Put your code here

Question 3.4 (2 points): Now using the ‘save’ function, save your rotated image to a new file called ‘logo_rotate.png’. Once your file is saved you can open the file to verify that the image is rotated 180 degrees.

### Put your code here

Question 3.5 (1 points): Now that you’ve created the new image and saved it in the file ‘logo_rotate.png’ go ahead and open the file to confirm the image has been rotated. Once you confirm it has been rotated, add, commit, and push the new image into your Github repository in your HW1 folder. If you haven’t already done it, you can also add, commit, and push the original image. Write the code below that you used to add, commit and push the new image file.

### Put your code here

🛑 STOP#

Pause to commit your changes to your Git repository!

Take a moment to save your notebook, commit the changes to your Git repository using the commit message “Committing Part 3”, no need to push the changes to GitHub yet, but you can if you want. (1 point)


Back to ToC

Part 4: Practice with using Python classes (12 points)#

For this part of the assignment, you’re going to work on fleshing out a partially constructed Python class and then experiment with using it to see if it works as intended.

The background#

The following code is a partially written Python class for tracking client details at a pet store. The goal of the class is to track pet and client details when a person drops off and picks up their pet for grooming at the pet store. The class has the __init__ function completed which initializes all of the needed variables. person_name is intended to store the name of the pet owner, pet_name stores the name of the pet being dropped off, pet_weight stores the weight of the pet when it is dropped off, review stores the rating given for the grooming service and should be set when checking out (scores range from 1-10), cost records the total cost of the service, drop_time record the time the client dropped off their pet, and pick_time record the time that the client picked up their pet. The checkin, checkout, and receipt functions are incomplete and will require you to complete them in later questions in this section.

# DO NOT EDIT THIS VERSION, this is the reference version of the code, do not edit this version.
class PetStoreTransaction:

    def __init__():
        self.person_name = ""
        self.pet_name = ""
        self.pet_weight=None
        self.review=None
        self.cost=None
        self.drop_time=None
        self.pick_time=None
        
    def checkin(person_name, pet_name, pet_weight):
        self.person_name = person_name
        self.pet_name = pet_name
        
    def checkout():
        pass
    
    def receipt():
        pass

Modifying the class to alter its behavior and add new functionality#

In the code cell below, you’re provided with a second copy of this new Python class, PetStoreTransaction. For the remainder of this section of the assignment, you will be modifying this version of the class to add new functionality and alter its behavior. You will then be provided with snippets of code designed to test your modifications and confirm that you’ve implemented them as intended.

When you make edits to the class provided below, make sure to run the cell to save your changes before running the later parts that use the class!

# EDIT THIS VERSION OF THE PYTHON CLASS
# This should help to ensure that you can always fall back to the original version provided above, should you need to.


class PetStoreTransaction:

    def __init__(self):
        self.person_name = ""
        self.pet_name = ""
        self.pet_weight=None
        self.review=None
        self.cost=None
        self.drop_time=None
        self.pick_time=None
        
    def checkin(self, person_name, pet_name,):
        self.person_name = person_name
        self.pet_name = pet_name
        
        print(self.person_name, "checked in with ", self.pet_name, "at ", self.drop_time, ". The pet weighs ",self.pet_weight, "at dropoff")
        
    def checkout():
        pass
    
    def receipt():
        print(" Name: ", self.person_name,
              "\n Pet: ", self.pet_name,
              "\n Weight: ", self.pet_weight,
              "\n Cost: ", self.cost,
              "\n Drop-off time: ", self.drop_time,
              "\n Pick-up time: ", self.pick_time, 
              "\n Review: ", self.review)

Questions 4.1 (2 points): Complete the checkin function in the PetStoreTransaction class, then run the following code to see what the output is. DO NOT MODIFY THIS CODE CELL. The checkin function should take as arguments the person_name, pet_name, pet_weight, and drop_time and should store all of those values to the initialized variables. Once the function is completed without errors it should print “Bob checked in with R2D2 at 3PM . The pet weighs 500 lbs at dropoff.”

## DO NOT CHANGE THIS CELL ##

# This is an example usage of the class "PetStoreTransaction" If it doesn't work, you need to make changes to the class.
test1 = PetStoreTransaction()
test1.checkin("Bob", "R2D2", "500lbs", "3PM")

If you need to write any code to test the checkin function, place that here.

# Put exploratory code here, if needed

Question 4.2 (4 points): Now, complete a the class method named checkout. It should take as input and record the cost, pick_time, and review. To conclude it should print some message that summarizes the checkout. Minimally the printed summary, should address the pet_name, pick_time, cost, and review. Once it is complete, you should run the following code and it should print “R2D2 was successfully groomed and picked up at 4PM . The total cost was $499 and the service was rated 8 /10”. Make sure to rerun the test code in Question 4.1 first since this uses the test1 object

# Put your code here

Question 4.3 (2 points): There is currently a bug in the receipt function, which prints a summary of the pet store grooming service. Fix the bug and then write below what you had to do to fix the bug. If the bug is fixed, the code in the test cell below should run without error.

## DO NOT CHANGE THIS CELL ##

# receipt test code

test1.receipt()

Write what you fixed here

Using your new ‘PetStoreTransaction’ class#

Now that you completed and fixed the PetStoreTransaction class, let put it to use.

Question 4.4 (4 points): Create an instance of your class and save it into a variable called transaction1. Once you’ve created the new instance, use the checkin function to check in a client. Choose any details you would like for the names, time, and weight as long as they are different from the details in the demo. Next, use the checkout function, again with unique details from the example. Finally, ensure everything is working by running the receipt function.

### Put your code here

🛑 STOP#

Pause to commit your changes to your Git repository!

Take a moment to save your notebook, commit the changes to your Git repository using the commit message “Committing Part 4”, no need to push the changes to GitHub yet, but you can if you want. (1 point)


Back to ToC

Part 5: Finishing (4 points)#

Question 5.1 (2 points): Have you put your name and GitHub username at the top of your notebook?

Question 5.2 (2 points): Finally, push your repository to GitHub so that all of the commits that you have been making along the way show up on GitHub.

# Put the command you used to push to GitHub here

NOTE: The grader will be able to see your commit messages and whether you pushed the repo at this stage, if everything has gone as planned. Double-check that things look correct on GitHub before you submit this notebook to D2L.


Assignment wrap-up#

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://forms.office.com/Pages/ResponsePage.aspx?id=MHEXIi9k2UGSEXQjetVofbihPqVa-WtNjOGYhCwpOgRURVVEQjZJVkVTWFM5Q1hYNkhWUjhXUkJUTS4u" 
	width="800px" 
	height="600px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, you’re done!#

Submit this assignment by uploading it to the course Desire2Learn web page. Go to the “Homework Assignments” folder, find the dropbox link for Homework #1, and upload it there.

© Copyright 2024, Department of Computational Mathematics, Science and Engineering at Michigan State University