1377 views
# Hands-On Lattice and Longitudinal Calculations During the CAS 2019 in Vysoke-Tatry (Slovakia), we will use Python as scripting language for the Hands-On Lattice and Longitudinal Calculations. We kindly ask you to make some homework before coming to Vysoke-Tatry to prepare yourself (and your laptop) for the course. **We strongly suggest to use Python for the course**. A basic knowledge of Python is assumed, therefore if you are not familiar with it you can find, in the following sections, few resources to fill the gap. During the course we will use **Python3** in a **Jupyter notebook** and, mostly, the numpy, matplotlib, pandas, seaborn and sympy packages. We will explain in the following sections how to install this software on your laptops. After a short introduction where we provided some useful links to get familiar with Python, we will focus on the software setup. To get a better idea of the level of the Python knoledge needed for the course you can browse the [Primer of the Hands-on exercises](https://www.overleaf.com/read/yccphjdyfmrz). Do not worry about the theory for the moment (it will be discussed in details during the school) but focus on the Python syntax and data types (tuples, lists,...). # A (very) short introduction to Python ## Test Python on a web page If you are not familiar with Python and you have not it installed on your laptop, you can start playing with simple python snippets on the web: without installing any special software you can connect, e.g., to [jupyterLab](https://gke.mybinder.org/v2/gh/jupyterlab/jupyterlab-demo/try.jupyter.org?urlpath=lab) and test the following commands ```python= import numpy as np # Matrix definition Omega=np.array([[0, 1],[-1,0]]) M=np.array([[1, 0],[1,1]]) # Sum and multiplication of matrices Omega - M.T @ Omega @ M # M.T means the "traspose of M". # Function definition def Q(f=1): return np.array([[1, 0],[-1/f,1]]) #Eigenvalues and eigenvectors np.linalg.eig(M) ``` You can compare and check your output with the ones [here](https://cernbox.cern.ch/index.php/s/xipyXzX7V9KBJbI). ## The *numpy* package To get familiar with the *numpy* package have a look at the following [summary poster](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf). ![](https://codimd.web.cern.ch/uploads/upload_6ffb4d07b1ebb895528f2a34aae41ec6.png) You can google many other resources, but the one presented of the poster covers the set of instructions you should familiar with. ## The *linalg* module To get familiar with the Linear Algebra (linalg) module have a look at the following [summary poster]( https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_SciPy_Cheat_Sheet_Linear_Algebra.pdf). ![](https://codimd.web.cern.ch/uploads/upload_15561fc12184bb0ae3f9cf7b1850317a.png) ## The *pandas* package (optional) To get familiar with the *pandas* package have a look at the following [summary poster]( https://s3.amazonaws.com/assets.datacamp.com/blog_assets/PandasPythonForDataScience.pdf). ![](https://codimd.web.cern.ch/uploads/upload_90383c01e29d29fb6a5516c613e22c4d.png) ## The *seaborn* package (optional) To get familiar with the *seaborn* package have a look at the following [summary poster]( https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_Seaborn_Cheat_Sheet.pdf). ![](https://codimd.web.cern.ch/uploads/upload_9a3c3f5ca48bbd567a0662df20dbd16f.png) ## JupyterLab JupyterLab is a user-friendly environment to work with Python. You can find an overview on JupyterLab [here]( https://jupyterlab.readthedocs.io/en/stable/). In the following section we will explain how to install a Python on your laptop. ## Python installation Please install the Anaconda distribution from https://www.anaconda.com/distribution/ ::: warning Install the **Python 3.7** distribution. ::: Please test the following code to check that all packages are correctly installed. Lanch Jupyter Lab from a terminal ```bash jupyter lab ``` You should end-up on your default browser with a page similar to the following: ![](https://codimd.web.cern.ch/uploads/upload_5b0618b75e4f4df0facf2a609b9354b5.png) Open a Python 3 notebook and execute the following test: ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt import seaborn as sns import sympy as sy # Matrix definition Omega=np.array([[0, 1],[-1,0]]) M=np.array([[1, 0],[1,1]]) # Sum and multiplication of matrices Omega - M.T @ Omega @ M # M.T means the "traspose of M". # Function definition def Q(f=1): return np.array([[1, 0],[-1/f,1]]) #Eigenvalues and eigenvectors np.linalg.eig(M) ``` Or you can test a simple plot: ```python # check a simple plot plt.plot([0,10],[0,10],'ob-') plt.xlabel('My x-label [arb. units]') plt.ylabel('My y-label [arb. units]') plt.title('My title') ``` ![](https://codimd.web.cern.ch/uploads/upload_b7610a37c41729a79bc4b0a5d863594b.png) Or something fancier: ```python # an sns plot sns.set(style="ticks") rs = np.random.RandomState(11) x = rs.normal(size=1000) y = rs.normal(size=1000) sns.jointplot(x, y, kind="hex") ``` ![](https://codimd.web.cern.ch/uploads/upload_1c7eaa74ee5422c62408cc9a57f7f0de.png) Or you can import from the internet some information in a pandas dataframe: ```python # a simple pandas dataframe (GDP world statistics) myDF=pd.read_csv('https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GDP.../OECD?contentType=csv&detail=code&separator=comma&csv-lang=en') myDF[(myDF['TIME']==2018) & (myDF['MEASURE']=='MLN_USD')] myDF.head() ``` that gives ![](https://codimd.web.cern.ch/uploads/upload_90f1a933f3f7092f2a4d4b50725e61ae.png) **Just for fun**, you can start googling additional packages that can boost your working flow or curiosity. Like *geopandas*, you can install it with ```bash conda install geopandas ``` and test it: ```python import geopandas world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres')) # Plot population estimates with an accurate legend import matplotlib.pyplot as plt plt.figure(figsize=(20,10)) world.plot(column='pop_est', ax=plt.gca(), legend=True); ``` ![](https://codimd.web.cern.ch/uploads/upload_ed2932a0996674dc7e64983a46d7e7c2.png) Looking forward to see you in Vysoke-Tatry!