Pre-class Assignment: Python Packages#

Day 4#

CMSE 202#

✅ Put your name here

#

Goals for today’s pre-class assignment#

  1. Remind yourself how to download indiviual files from the internet via the command line

  2. Practice installing publicly-available Python packages

Assignment instructions#

This assignment is due by 11:59 p.m. the day before class and should be uploaded into the appropriate “Pre-class assignments” submission folder in the Desire2Learn website.


1. Downloading individual files from the internet (revisting the curl command)#

In the in-class assignment you may need to download a few files from the internet, a quick and reliable way to do this is using curl on the command line!

Reflect back on what you learned previously in class or use the internet to answer the questions below:

Question 1: What does the curl command do?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 2: There is a file called US_Unemployment_Oct2012.csv that can be accessed at the following location:

https://raw.githubusercontent.com/wrobstory/vincent/master/examples/data/US_Unemployment_Oct2012.csv

How would you use curl to download this file to your computer?

Important: Remember that by default curl will simply output the contents of the file to your screen. What command flag should you use to ensure that the remote file is actually saved as a file on your computer? If you don’t remember, you might need to leverage the internet to remind yourself.

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 3: Are there any other other command line commands can be used to download files onto your computer from a remote location? There is another very common command out there besides curl, but your computer might not have it. Can you figure out what that command is? The MSU JupyerHub server has this command available. If you can figure out what it is, test it out with the file from the previous question!

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)


2. Installing Third Party Packages#

There is a lot of awesome code out there that is publicly available and accessible on the internet (you started to witness a bit of this when you were exploring repositories on GitHub). Part of this ecosystem of publicly available software tools is a bunch of really useful Python packages and libraries that we might not already have installed as part of the Anaconda Python distribution.

So, how do we go about installing new third party packages that aren’t already a part of our Python installation?

If the package you want to use is available as a “conda” package or from the Python Package Index (PyPI), installation is generally pretty straight forward.

Let’s test out installing a package using conda and a package using pip, which installs packages from PyPI.

2.1 Pip installation#

There is a great package called cmocean (available here) that contains several colormaps that are commonly using for plotting oceanography data. One of the awesome things about the colormaps is that they are designed to be color-blind friendly, meaning that even if you’re colorblind you can still extract information from the colormap.

✅ Do This:

If you’re doing this on your computer locally, try installing the package by doing the following on the command line:

pip install "cmocean[plots]"

If you’re doing this on the MSU JupyterHub, you’ll need to modify the command a bit in order to make sure you don’t run into permission issues with the installation process. If you’re not installing pip packages on your local machine or a machine you have root privileges for, you need to use the --user flag, like so:

pip install --user "cmocean[plots]" # Make sure you include the quotation marks (") in your command

This not only installs the main cmocean module, but also installs the “plots” submodule, which is necessary to get some of the examples from the cmocean documentation page to work. When you run the command, you’ll see that pip finds the packages and also checks if it should install or update any other packages at the same time. Once you’ve installed the package, you should be able to import it. Test it out in this notebook! Try making a plot using one of the new colormaps you have available.

You also have to include import cmocean.plots rather than just using import cmocean if you try to run any of the examples that use “cmocean.plots”.

# Test out importing cmocean here.
# If you can, see if you can make a plot using one of the new colormaps.
# An easy option is to use some of the code provided on the cmocean website

2.2 Conda installation#

While pip is one handy way to install Python packages when you’re working within an Anaconda environment, you can also make use of the conda command. However, the caveat to this is that you need to have permission to writing new files into your Python installation when using conda to install the package, which you do not have permission to on the MSU JupyterHub server. If you’ve been only using JupyterHub for class at this point, that’s totally OK, but you should still read about how to use conda for installing packages and then you can try to install the same pacakge with pip instead.

A package for printing nicer looking tables#

Occasionally there are times when you’re looking at simple datasets in Python and it would be handy to be able to manipulate and visualize the data in simple ASCII formats without dealing with all of the Pandas dataframe overhead. Or perhaps you need to output the a data table in an HTML format so that you can render the table on the web.

✅ Do This: To achieve these sorts of tasks, you can install a package called prettytable using conda like so:

conda install -c conda-forge prettytable

This uses conda to install the package by locating it on conda-forge, which is a community maintained set of conda packages. When you run the command, you’ll see that conda finds the packages and also checks if it should update any other packages at the same time and will let you know everything it plans to do before you confirm that you want it to happen. Once you’ve installed the package, you should be able to import it. Some documentation for prettytable can be found here: jazzband/prettytable

If you were able to get prettytable installed, test it out and make sure it’s working by trying to run some of the examples from the README file on the GitHub page linked above.

Important note: If you’re trying to do this on JupyterHub, rather than on your personal computer, you will get a error that you don’t have permissions to install new packages. Luckily, you can install prettytable with pip as well!

To do so, try running this command:

pip install --user prettytable

Try out one of the examples from the link above!

# Put your prettytable testing code here

2.3 Updating a package you already have#

✅ Do This: Run the following command, either in this notebook or in a terminal:

conda list

This should show you a list of all (or most of) the Python packages available to you. Scroll through the list and look for NumPy. The version you have is likely not complete updated to date with the current version! You can check the current releases here. If your version is fully up to date, you can move on to the next section.

To update a package, we can again use pip, specifically:

pip install --upgrade matplotlib

✅ Do This: Run the code above, either in this notebook or a terminal. At the end of this, you should have version the most recent version.

2.3.1 Going back to older versions of packages#

In rare, though not impossible, circumstances, you may need to install an older version of a package. This can happen when a package requires an older version of another package. (Again, rare, but not crazy rare). In this case, we still use pip, but now we specify the version, like so:

pip install matplotlib==3.7.1

✅ Do This: Run the code above, either in this notebook or a terminal. At the end of this, you should have version 3.7.1.

2.3.2 Checking for outdated packages#

Now that you’ve practiced updating a package, you can also check to see if any of your packages are out of date. To do this, you can use the following command:

pip list --outdated

This can be useful if you want to make sure you’re always using the most up to date version of a package. Or, if you’re working on a project with a collaborator, you can make sure you’re both using the same version of a package, which can help avoid some headaches.

✅ Do This: Run the code above, either in this notebook or a terminal. At the end of this, you should have a list of packages that are out of date.

2.4 Using examples from new packages#

The most important skill you can take away from this pre-class assignment is reading and using examples provided in the documentation. For this part, let’s work with the scikit-image package. You can find the details for this page on its webpage.

✅ Do This:

  1. Navigate to the Gallery page. NOTE: Not all packages will have Galleries. For those packages, use the documentation.

  2. Go to the example RGB to grayscale.

  3. Copy and paste the code provided in this example into the cell below and run it.

#Copy and paste the example code here

The reason why we did this is that we now have an example to build on.

2.4.1: Go back to the Gallery. You should see that some of the other examples use images other than the astronaut we just worked with. Using code from one of the other examples, modify the RGB to grayscale code so that it uses another image (I.e., not the astronaut image).

#Put your code here

2.4.2: Select another example from the scikit-image package. Copy, paste, and run the code below.

#Write your new example code here.

Follow-up Questions#

Copy and paste the following questions into the appropriate box in the assignment survey include below and answer them there. (Note: You’ll have to fill out the section number and the assignment number and go to the “NEXT” section of the survey to paste in these questions.)

  1. Were you able to figure out another command line command for downloading files? If so, what was it?

  2. Did you run into any issues with using pip or conda to install packages?

  3. Have you already had to install new Python packages previously? If so, what package(s) and what did they allow you to do?


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://cmse.msu.edu/cmse202-pc-survey" 
	width="800px" 
	height="600px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, you’re done with your pre-class assignment!#

Now, you just need to submit this assignment by uploading it to the course Desire2Learn web page for this assignment’s dropbox (Don’t forget to add your names in the first cell).

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