VESIcal at vVMSG 2022

Simon Matthews (University of Iceland) & Penny Wieser (Oregon State University)

01 - Introduction to Python, Jupyterlab, and the ENKI Server

Welcome to the VESIcal workshop. Before we get started on VESIcal calculations, we will introduce the Jupyterlab interface, and how to get started with python

Every Jupyter notebook is made up of cells. Each cell might contain typset text (like the one you are reading now) or code (could be python, or other programming languages) with its output immediately below. The Jupyter notebook you are reading now is most likely running on an instance of the ENKI server. The ENKI server provides powerful tools for petrological thermodynamic calculations, and is the software upon which VESIcal is written. The VESIcal workshop server will only be available for the duration of the workshop. If you want to continue using VESIcal, you can get access to the a public instance of the ENKI server here, or read the VESIcal documentation here for other ways of using VESIcal. It is also possible to install the ENKI software on your own computer- see here for more info.

In this notebook we will give you a very brief introduction to python. However, you don't actually need to understand any of this to use VESIcal. There are many example notebooks for you to use with VESIcal, including the ones you will use today, and you just need to know where to adjust numbers or filenames to make it work with your data.

If you get stuck at any point in the workshop, write a message in the zoom chat. Penny or I will get back to you.

Has the notebook crashed? Try pressing the circular arrow at the top of the page.

Exercise 01.01 - Edit and run cells

To edit a Jupyter notebook cell double click on it. Try it with this cell now!

Hopefully you should now be looking at 'typewriter' style text. To return the cell to being nicely formatted press shift and enter/return at the same time. In the cell below I have placed an image showing where these keys are on my keyboard.

Now we can try running some python code in a cell. Click in the cell below (a single click is fine for a code cell), then press shift and return. Hopefully an output will appear below the cell saying "Hello VMSG!".

In [2]:
print("Hello VMSG!")
Hello VMSG!

Notice there is a set of square brackets with a number next to the cell. This means the cell has been run. A cell that is still working will show [*]:, and a cell that hasn't done anything yet will show [ ]:.

Exercise 01.02 - Using python to do maths

Python can do simple maths very easily, for example we can add two numbers together (again click in the cell, shift + return to run:

In [3]:
182.5 + 1649.1
Out[3]:
1831.6

We can do more advanced maths by using a set of methods defined in the NumPy python library. To tell python we will be using this we have to import it:

In [4]:
# This code imports the numpy package and makes it available to us with the name np
import numpy as np

The first line in that cell started with a #, this tells python to ignore what is written on the rest of line, so that we can write annotations there. I will do this throughout the notebooks to describe what each line of code does- but you don't need to pay attention to it.

One useful numpy function returns the natural logarithm of a number:

In [5]:
np.log(10)
Out[5]:
2.302585092994046

Another useful set of functions find the mean and standard deviation of a set of numbers:

In [6]:
# Find and print the mean
print(np.mean([5.0, 6.3, 2.9, 1.8]))
# Find and print the standard deviation
print(np.std([5.0, 6.3, 2.9, 1.8]))
4.0
1.7564168070250297

If I hadn't used the print command there, only the final line in the cell would return an output. Notice the numbers were surrounded by square brackets and separated by commas. This tells python we are providing it with a list of numbers to work with.

Try clicking in the cell and changing the numbers, then rerun the calculation.

Exercise 01.03 - Using variables

In python we can assign names to numbers, plots, calculations, or pretty much anything. For example, I could store the CO$_2$ content of a melt inclusion:

In [7]:
CO2 = 0.1

Then in place of writing 0.1 in every calculation I want to do using the CO$_2$ content, I can just write CO2. For example, if I wanted to calculate what the CO2 content is in ppmw:

In [8]:
CO2*1e4 # 1e4 is shorthand for 1 x 10^4
Out[8]:
1000.0

This can save us a lot of time if we want to reuse code with a different value for CO$_2$.

Exercise 01.04 - Time to try writing some code!

Now we will have a short break during which you could try writing some code yourself (or go make some coffee!).

In one of the cells above we calculated the mean and standard deviation of a list of numbers, but we repeated that list twice when calling each method.

In the cell below define a variable that stores this list of numbers. Remember to surround your list with the square brackets!

In [ ]:
 

Now use your variable when you call the numpy mean and std methods:

In [ ]:
 

Summary and more info

In the rest of the workshop we will use Jupyter notebooks to run VESIcal calculations. The code will always be set up for you, just requiring you switch numbers. However, there will also be opportunities to do slightly more advanced things for those who are interested!

Jupyter notebooks are extremely useful for documenting and sharing computational work, particularly because you can embed lots of different types of content. You can even get it to typset equations using $\LaTeX$, and tables etc using markdown. It is very easy to convert them into other formats for sharing, for example pdfs, web pages (see the tutorials at vesical.readthedocs.io), or even whole publications which can be user-interactive via myBinder: VESIcal manuscript at myBinder.

To add new cells click the + symbol at the top of the page. To change a cells type, once you are clicked into it find the dropdown box at the top, "Markdown" gives you the typeset text cell.

In [ ]:
 
In [9]:
print("HELLO!")
HELLO!

Hello! $\Delta$ CO$_2$

In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: