Simon Matthews (University of Iceland) & Penny Wieser (Oregon State University)
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.
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!".
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 [ ]:.
Python can do simple maths very easily, for example we can add two numbers together (again click in the cell, shift + return to run:
182.5 + 1649.1
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:
# 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:
np.log(10)
2.302585092994046
Another useful set of functions find the mean and standard deviation of a set of numbers:
# 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.
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:
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:
CO2*1e4 # 1e4 is shorthand for 1 x 10^4
1000.0
This can save us a lot of time if we want to reuse code with a different value for CO$_2$.
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!
Now use your variable when you call the numpy mean
and std
methods:
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.
print("HELLO!")
HELLO!
Hello! $\Delta$ CO$_2$