JupyterLab

Workshop 2 – Interactive classroom

JupyterLab

It is time that we start introducing the coding environment we will use for programming during this course.

Terminal
cd $SCIPRO
conda activate scipro2023
jupyter-lab

This series of terminal commands will open the coding environment JupyterLab for you. It is a web-based application that starts a new internet browser window. While JupyterLab is running out of your terminal, you won’t be able to use the terminal anymore for any other tasks. That is ok, though, because you will have plenty of options to work with using the JupyterLab graphical user interface in your browser.

We will now slowly start to get familiar with the capabilities that JupyterLab offers us by doing some exercises.

The terminal within JupyterLab

Recap of exercise #01-02 ‘conda’
  1. Create a new conda environment testenv without specifying a python version for it, and also install the packages ipython and numpy into the environment. Do not yet activate it.
  2. Run conda --help in the terminal and examine the available conda commands.
  3. Run conda info --help and read the documentation.
  4. Use conda info to display a list of all environments currently installed on your computer.
    • How many environments are there?
    • Where are they installed?
  5. Activate testenv.
  6. Find out which version of python was installed in testenv. Use conda --help to find the correct command!
  7. Deactivate testenv. Make sure that no environment is activated anymore (i.e., no parentheses with an environment name are displayed before your command line).
  8. Remove testenv by running conda remove -n testenv --all.

Writing and executing a python program with JupyterLab

Open a new python file by clicking on the blue button with a + on the top left and then select Python File.

  1. Rename the file using the GUI (graphical user interface) of JupyterLab: solar_elev_angle_latitudes.py.
  2. Create a new directory (also using the GUI) and call it 02_workshop.
  3. Move the python file into the new directory.

Now, we want to make use of a special feature that JupyterLab offers us. We can connect the python file with a python interpreter. To do so, right-click on the file tab and select Create Console for Editor.

A new popup window opens. You can just go ahead with the suggestion made by the window and start a Python 3 kernel.

In the following screenshot, I have copy-pasted our 3rd version program of the solar elevation angle into the python file:

Executing select lines and blocks of code

Move your cursor to the first line that imports numpy. Then press [Shift+Enter]. The line gets executed by the python console below!

Similarly, if you highlight several lines of code and hit [Shift+Enter], the highlighted block of code gets executed.

In addition to executing commands from your python file, you can also use the prompt at the python console to execute commands. Analogously, use [Shift+Enter] to execute a command.

Recap of exercise #01-03 ‘solar elevation angle for different latitudes’

Write a program that computes and plots the solar elevation angle for Sept 16, 2023 at noon true local time (i.e., tau = 0) for all latitudes from the equator to the north pole in 5\(^\circ\) intervals. Use version 3 of our second program (in the slide deck 01C_first_steps.html) to guide you.

Execute your program line-by-line using the python interpreter (python console) in JupyterLab. Then execute your program from the terminal in JupyterLab.

Showing a figure instead of printing it to a PNG file

We will get to advanced plotting of visuals in more detail later in the course. For now, though, try using the command plt.show() instead of plt.savefig() whenever you don’t want to save a PNG file, but when you want to display the figure within the python console in JupyterLab.

Exercises

#02-01: Test yourself!

Take the quizzes for python syntax, comments, variables, data types, numbers, strings, booleans, operators, and ‘if .. else’. There are a few questions that we didn’t cover yet, e.g. some additional data types. Ignore those for now. Please write down any questions that you have and we will discuss those together later.

#02-02: Classification

Write a Python program that classifies energy consumption based on the user’s input. Ask the user to enter the daily energy consumption in kilowatt-hours (kWh), and then categorize it into one of the following categories:

  • “Low Consumption” for consumption less than 8 kWh
  • “Moderate Consumption” for consumption between 8 kWh and 15 kWh
  • “High Consumption” for consumption greater than 15 kWh

Since this program requires interactive user input, I suggest you execute it from the terminal in JupyterLab.

Once the above program runs, think about how to make the program more robust. What happens if the user responds with a word, such as ‘ten’? Modify the program, so that it displays a meaningful message that tells the user that the input could not be processed and that the program expects an integer or float as input. After this message, the program should exit without a classification result.

#02-03: Classification of numpy array

Create a copy of your previous Python program that classifies energy consumption. Modify it like that:

  • We are now interested in the energy consumption of each month of the year. Instead of asking the user for input, we use numpy’s capabilities to generate random numbers. Have a look at the documentation of the function numpy.random.normal (either use the python console, or the Internet). Can you find out how to generate 12 random numbers (one for each month) from a normal distribution with a mean of 30*12 kWh and a standard deviation of 30*4 kWh? (We approximate each month with 30 days.) Call the array consumption_data.
  • We now want to classify the average monthly energy consumption of our random households. Use the function numpy.mean to do the computation.
  • Adjust your classification thresholds to monthly consumption instead of daily consumption.
  • The random numbers we generate can potentially include negative values. Let’s only compute a result if no value in consumption_data is negative. If there is one or multiple negative values in there, exit the program with a statement “You really used negative energy in some months? I don’t believe you!”. To implement that, think about two different strategies. One strategy should use the function numpy.any, the other strategy numpy.all.

This program does not require user input anymore. I suggest you execute it through the python console in JupyterLab. How many times do you have to execute the program (i.e., generate new random numbers) until the program exits with the funny “error” message? Do we have to adjust the standard deviation of the random number generator?

If you really struggle with the documentation for the random number generator function, then use my tip and apply the function like that:

np.random.normal(mean, std, n_months)

Learning checklist

  • I know how to use Jupyter-lab as coding environment
  • I can access the terminal within Jupyter-lab, create a python file, and attach an ipython console to the current script. This allows me to interactively write and execute python code.