{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# VESIcal at vVMSG 2022\n", "Simon Matthews (University of Iceland) & Penny Wieser (Oregon State University)\n", "## 01 - Introduction to Python, Jupyterlab, and the ENKI Server\n", "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\n", "\n", "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](server.enki-portal.org), or read the VESIcal documentation [here](https://vesical.readthedocs.io/en/latest/about.html#many-ways-to-use-vesical) for other ways of using VESIcal. It is also possible to install the ENKI software on your own computer- see [here](https://gitlab.com/ENKI-portal/ThermoEngine) for more info.\n", "\n", "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.\n", "\n", "**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.**\n", "\n", "Has the notebook crashed? Try pressing the circular arrow at the top of the page." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 01.01 - Edit and run cells\n", "To edit a Jupyter notebook cell double click on it. Try it with this cell now!\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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!\"." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello VMSG!\n" ] } ], "source": [ "print(\"Hello VMSG!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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 [ ]:." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 01.02 - Using python to do maths\n", "Python can do simple maths very easily, for example we can add two numbers together (again click in the cell, *shift* + *return* to run:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1831.6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "182.5 + 1649.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# This code imports the numpy package and makes it available to us with the name np\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "One useful numpy function returns the natural logarithm of a number:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.302585092994046" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.log(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another useful set of functions find the mean and standard deviation of a set of numbers:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.0\n", "1.7564168070250297\n" ] } ], "source": [ "# Find and print the mean\n", "print(np.mean([5.0, 6.3, 2.9, 1.8]))\n", "# Find and print the standard deviation\n", "print(np.std([5.0, 6.3, 2.9, 1.8]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "Try clicking in the cell and changing the numbers, then rerun the calculation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 01.03 - Using variables\n", "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:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "CO2 = 0.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "CO2*1e4 # 1e4 is shorthand for 1 x 10^4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can save us a lot of time if we want to reuse code with a different value for CO$_2$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 01.04 - Time to try writing some code!\n", "Now we will have a short break during which you could try writing some code yourself (or go make some coffee!).\n", "\n", "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.\n", "\n", "In the cell below define a variable that stores this list of numbers. Remember to surround your list with the square brackets!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now use your variable when you call the numpy `mean` and `std` methods:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Summary and more info\n", "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!\n", "\n", "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](vesical.readthedocs.io)), or even whole publications which can be user-interactive via myBinder: [VESIcal manuscript at myBinder](https://mybinder.org/v2/gh/kaylai/vesical-binder/HEAD?filepath=Manuscript.ipynb).\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HELLO!\n" ] } ], "source": [ "print(\"HELLO!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hello! $\\Delta$ CO$_2$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }