Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
phylabs:lab_courses:phys-211-wiki-home:inactive:introductory-lab-python-tutorial:start [2024/06/11 16:34]
kevinv ↷ Page moved from phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:start to phylabs:lab_courses:phys-211-wiki-home:inactive:introductory-lab-python-tutorial:start
— (current)
Line 1: Line 1:
-====== Python Tutorial ====== 
  
-<div pythoncomic> 
-</div> 
-http://xkcd.com/353/ by Randall Munroe. Please don't sample anything in the cabinets. 
-<HTML> 
-<style> 
-.wrap_pythoncomic { 
-transition:all 1s ease; 
-background: url("https://imgs.xkcd.com/comics/python.png") no-repeat; 
-width: 600px; 
-height: 700px; 
-display: block; 
-margin: auto; 
-background-position: center; 
-background-size: 100%; 
-} 
- 
-.wrap_pythoncomic:hover{ 
-transition:all 1s ease; 
-transition-delay:4s; 
-background: url("https://thejenkinscomic.files.wordpress.com/2020/04/hey-i-discovered-how-to-write-hidden-mouse-over-text.png") no-repeat; 
-background-size: 600px; 
- 
-} 
-</style> 
-</HTML> 
- 
- 
-====== Before you begin... ====== 
- 
- 
----- 
- 
-Before you begin, make sure that you have done the following things: 
- 
-  * Download and install the Anaconda python distribution so that you can run Jupyter notebook. 
-    * Go to https://www.anaconda.com/download/ and select the appropriate Python 3.x installation for your operating system. (Do not download a Python 2.x version.) 
-  * Familiarize yourself with the Jupyter environment by making sure you can open and run files. 
-    * Download one of the tutorials below, and save it to a folder on your computer.  
-    * Open //Jupyter Notebook// (or alternately open //Anaconda Navigator// and within that program, launch //Jupyter Notebook//). A new tab on your internet browser should open up displaying a directory of your computer. 
-    * Navigate that directory to the place where that file was downloaded. You should be able to double-click on the notebook from within the Jupyter directory, and the file should open in a separate tab. 
-    * Try to execute the first few cells by selecting the cell with your mouse and hitting "shift+enter" on your keyboard. 
-  * Read the page [[phylabs:lab_courses:supplemental-material:programming:an_introduction_to_python:start|An Introduction to Python]].  
-    * This will orient you and give you examples of some some basic syntax and techniques. 
- 
-<WRAP help> 
-<details> 
-<summary>Completely new to programming?</summary> 
- 
-These Python tutorials assume that you have some background knowledge of common programming terminology (e.g. variables, strings, for loops) and at least novice coding experience in some programming language. 
- 
-If you haven't done any programming previously, you will probably need some additional support in getting started. Thus, we've curated a list of resources for learning the basics of programming in Python. 
- 
-**Online Python Resources** 
- 
-[[https://pll.harvard.edu/course/introduction-data-science-python|Introduction to Python for Data Scientists]] 
-Online course with a series of video tutorials, designed to introduce you to Python in about 4 hours. 
- 
-[[https://runestone.academy/ns/books/published/thinkcspy/index.html | How to Think Like a Computer Scientist / Interactive Version]] 
- 
-This online book assumes no previous programming knowledge; working through to chapter 12 should be plenty sufficient to do anything asked of you in the course. The interactive version embeds the programming tasks in your web browser, if you'd prefer to work that way. 
- 
-[[https://notebook.community/r31415smith/intro_python/Crash%20Course%20v0.62 | A Crash Course in Python for Scientists]] 
- 
-Written by Rick Muller at Sandia National Laboratories, this tutorial has a heavy focus on using Python for physics.  Heavily based on using examples. 
- 
-[[https://github.com/jrjohansson/scientific-python-lectures | Lectures on scientific computing with Python]] 
-Basically an online textbook, written by Robert Johansson at the iTHES Research Group.  Also written primarily for physicists. 
- 
-[[https://open.cs.uwaterloo.ca/python-from-scratch/ | Interactive Online Course from the University of Waterloo]] 
- 
-Tools to learn & practice Python online, with a computer science emphasis.  Their focus will be different from other tutorials, but the change in perspective might be useful. 
- 
-[[http://illustratedtheoryofnumbers.com/prog.html | Introduction to Python Programming for Number Theoretic Applications]] 
- 
-While this introduction quickly veers into math-specific topics, the first 3 exercises serve as a good introduction to working in Jupyter notebooks. 
- 
-[[https://automatetheboringstuff.com/#toc | Automate the Boring Stuff with Python]] 
- 
-Another book that assumes no programming experience. Focuses on accomplishing tasks like sorting / filtering information or automating file organization. 
- 
-[[https://inventwithpython.com/cracking/ | Cracking Codes with Python]] 
-[[https://inventwithpython.com/invent4thed/ | Invent Your Own Computer Games with Python]] 
-[[https://inventwithpython.com/pygame/ |  Making Games with Python & Pygame]] 
- 
-These three books are by the previous author, for anyone who would rather work with some more tangential topics. The end result is still transferable skills, so learn whatever way works best for you. 
-</details> 
-</WRAP> 
- 
-====== Goals and procedures ====== 
- 
----- 
- 
-To complete this tutorial, set up a folder on your computer where you will download the files and save all your output. (If you will be using Google Colab instead, upload all the files to your Google Colab folder on Google Drive.) As you work through each example you will do the following: 
- 
-  * read through working example code and comments to understand how certain tasks are done; 
-  * run and modify the example program to see the output; 
-  * create new code (usually by copying and modifying existing code) in the labeled "Task" sections; and 
-  * create one key plot for each example which will be turned in. 
- 
-These tutorial scripts are meant to give you familiarity with all the features of Python that you will use this year in lab. If you have any trouble completing the tutorial or understanding features of the code, please ask for help and have patience. 
- 
-Programing well takes practice, but we are using coding here as a tool, not as the goal in itself. If at any point in the year you find yourself struggling with the programming more than the physics, stop and ask a TA for help; we do not want Python to be a major (or even big) part of your time commitment for this class. 
- 
-<WRAP info> 
-For some of the most common issues we've seen in this course, we've set up a [[phylabs:lab_courses:supplemental-material:programming:python_faqs_and_common_problems:start|Python FAQs and Common Problems]] page. 
-</WRAP> 
-====== Assignments ====== 
- 
----- 
-===== Example 1 ===== 
- 
-Files needed for Example 1: 
- 
-<WRAP download> 
- 
-{{ phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:git:exercise1_plotting_and_functions.ipynb |}} 
-</WRAP> 
- 
-In Example 1, we introduce ways to generate and plot data by making two different figures. 
-==== Exercise ==== 
- 
-Work your way through the notebook, and ask for help if you need it! 
- 
-After completing the notebook, be sure to save the final plot from **Task 5**. This will be the **key plot** you turn in for Example 1. 
- 
-<HTML> 
-<!-- 
-Now, save a copy of the file with a different file name or create a new blank file. You will edit this file to create a new figure (shown below). It is a **single figure** composed of **two subplots**. 
- 
-  * In the top subplot, plot the //Exponential// distribution$f(x) = 2 e^{-2x}$ over the interval 0 < //x// < 3 in blue.   
-  * In the bottom subplot, plot the //Gaussian// distribution  $g(t) = \frac{1}{2\pi} e^{-\frac{({t-1})^2}{2}}$  over the interval -3 < //t// < 3 in green.     
-  * Label the figure “Two examples of probability distributions”. 
-  * Include a legend for each subplot where the label gives the name of the distribution illustrated. 
-  * Label the x-axis of the each subplot with the appropriate variable. 
-  * Label the y-axis of each subplot with the appropriate mathematical equation. ([[display/phylabs/LaTeX+Formatting |LaTeX Formatting]] is optional.) 
-  * For the bottom subplot, move the legend to the top left and set the y-axis scale to have an upper limit of 0.20. 
---> 
-</HTML> 
-==== More Plotting Help ==== 
- 
-If you'd like a broader introduction to what Matplotlib can do, visit J.R.Johansson's tutorial: 
-[[https://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb]] 
- 
-When making subplots like this, python sometimes allows axes labels, titles or other text to overlap in ugly ways. To avoid this, you can use the matplotlib function //[[https://matplotlib.org/users/tight_layout_guide.html|tight_layout()]]// which will adjust the spacing so that features don't overlap. Insert this function just before saving your file and using the function //show()//. 
- 
-It sometimes is necessary to also specify an extra amount of space to be added by using the "pad" option – for example, //tight_layout(pad = 2)// – where the amount of extra padding is in units of the font size. For example, in your code, the last few lines might read as follows: 
- 
-<code python> 
-plt.tight_layout(pad = 2) 
-plt.savefig('wicked_awesome_plot.png') 
-plt.show() 
-</code> 
- 
-{{phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:example1_final.png?400|}} 
- 
-===== Example 2 ===== 
- 
-Files needed for Example 2: 
- 
-<WRAP download> 
-{{phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:git:exercise2_fitting_data.ipynb}} 
- 
-{{ phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:git:example2_data.tsv |}} 
-</WRAP> 
- 
-In example 2, we import data from an external file (example2_data.tsv) instead of generating it inside the program. We then fit that data to simple functions and plot both the data and fit on a single plot. See the page on  
-[[phylabs:lab_courses:supplemental-material:physics-and-mathematics-references:least_squares_fitting:start| least-squares fitting]] to understand the notation used in the program. 
- 
-Open the data file to see how the data is stored, then look through the script file to understand each step. We introduce many new features here; in particular, note how we do fits as this will be a big part of your python work this quarter. 
- 
-==== Exercise ==== 
- 
-<HTML> 
-<!-- 
-Now, save a copy of Example 2 with a different file name. You will edit this file (not the original). We will not use the manual computation of the least-squares fit for this exercise, so you should remove it from your code before you begin. 
- 
-In this exercise, we will use a new text file with different points and uncertainties. We will fit the data to two different functions and use the reduced-chi-squared to judge which fit better models the data. It will be useful to work through this slowly and stop to run your program after each major bullet point rather than attempting to code everything at once. 
- 
-  * Import NEW data from the file “Example2_Data2.tsv”. 
-    * The file includes three columns – //x//, //y//, and uncertainty in //y//. 
-    * Plot the data and look at the quality of the linear fit. 
-  * Next, we will introduce a second fit function, $g(x) = a + bx + cx^2$ .     
-  * Modify your code to include a second fit function for the quadratic form. Do not erase your first fit function; we will eventually use both. 
-    * Include a second weighted residual using the new quadratic fit function and adjust your array of initial parameter guesses. 
-    * Run the program using only the quadratic fit function. You may want to adjust the plot to show the quadratic fit against the data, but don’t worry too much about modifying output or plot annotations. At this point, we’re just checking to make sure the program runs without any errors. 
-  * Next, we will perform both fits in the script at the same time. This means some of the code will have to be copied so it appears twice – once for each fit. 
-    * Work from the top of the code to the bottom and decide what needs to be duplicated. Rename variables as needed to keep the two fits distinct. For example, you previously had an array of initial parameter guesses p0. Perhaps now you have two arrays, p01 and p02. Likewise, you had one fit produce an output array pf. Now perhaps you have two fits making two arrays, pf1 and pf2. 
-    * Again, work one step at a time. Don’t worry about how the plot looks until you’re sure that the fits are working. (It may help to remove the plot annotations altogether for this part.) 
-    * Take your time and ask for help if you get stuck. 
-  * Finally, modify the plot aesthetics. 
-    * Let’s say the //x//-data is time (in seconds) and the //y//-data is height (in meters). 
-    * Give your plot a new title. 
-    * Add a second set of annotations for the second fit. 
-    * On the annotations, make sure that you display the correct number of [[:phylabs:lab_courses:supplemental-material:programming:python_faqs_and_common_problems#how_do_i_control_the_number_of_significant_figures_displayed_on_text_annotations|significant figures]] 
-    * Make sure your two fits appear in two different colors and add a legend. 
- 
---> 
-</HTML> 
- 
-After completing the whole notebook, be sure to save the final plot from **Task 5**. 
- 
-===== Example 3 ===== 
- 
-Files needed for Example 3: 
- 
-<WRAP download> 
-{{phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:git:exercise3_fitting_exponential_data.ipynb}} 
- 
-{{ phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:git:example3_data.txt |}} 
-</WRAP> 
- 
-In Example 3, we again perform a fit, but this time to a decaying exponential.  
-==== Exercise ==== 
- 
-In almost every experiment this year, you will have to do some version of the following exercise: 
- 
-  * record data in your lab notebook; 
-  * translate this handwritten data into a digital form; 
-  * manipulate the data; 
-  * plot the data; and 
-  * fit the data. 
- 
-This notebook will step you through not only how to do this, but also how to format your output to look nicer. 
- 
-<HTML> 
-<!-- 
-We now want to modify the Example 3 script to handle data of the sort you collected in the first experiment, [[phylabs:lab_courses:phys-211-wiki-home:introductory-lab-gamma-cross-sections|Gamma Cross Sections]]. There, you recorded raw net and gross counts, collection times, and absorber thicknesses. We want to build a program which can take this data as input, compute rates and uncertainties, fit the data to an appropriate model, and plot the results.  
- 
-Consider this actual lab notebook page detailing a set of measurements from the gamma cross sections experiment: 
- 
-{{:phylabs:lab_courses:phys-211-wiki-home:introductory-lab_-python-tutorial:notebook_data.png?400|}} 
- 
- 
-Save a copy of both the script and input text file from Example 3 under new file names. You will create **two separate plots**, one showing the exponential decay of the count rate at 511 keV and one for the count rate at 1275 keV. (They can be separate files; they do not need to be two subplots within a single plot.) 
- 
-Edit these new files (not the originals) by doing the following. As with the last example, move slowly and stop after each bullet point to run and check the program rather trying to program everything at once. 
- 
-  * Take the notebook data shown in the picture above and record it in your new text file. 
-    * Note that in the example, there were three columns – t, A and dA. Our data is a little different, so the columns you choose to use will be different as well. 
-    * Edit the script to read in this new file and save the columns under appropriate variable names. 
-  * After importing the data, insert lines of code which compute the rate and the uncertainty in the rate. 
-    * Note that since you have counts for two energy peaks, you will end up with two rates. Name them appropriately (e.g. R1 and R2). 
-    * Since the uncertainty in time is small, the uncertainty in the rate can be computed as $\frac{{dR}}{R} = \frac{{dN}}{N}$       
-    * If the net counts //N// represent gross counts //G// minus background counts //B//, then ${dN}^2 = {dB}^2 +{dG}^2$  where $dB = \sqrt{B}$  and E $dG = \sqrt{G}$ .      
-    * Putting all this together, we find $dR = \frac{R \sqrt{{dB}^2 + {dG}^2}}{N}$ .       
-      * Why not just use dN? 
-        * We expect the data to decay as an exponential. Change the fit function to the form R(x) = R_0e^{-\lambda x} + C$ .     
-        * Note that we've added a constant background$C$  to the fit. This helps account for any constant background rate which might not have been fully subtracted out.     
-        * As the fit function now has an additional parameter, the initial guess array will need to be lengthened by one. 
-    * Give appropriate guesses to the fit parameter for one of the rates and attempt a fit for that rate only. 
-    * Repeat, making modifications if necessary, until the fit converges. 
-    * Note that the plot annotation text and all the labels will be incorrect. Don't fix them yet; try to get the fit to look and plot OK before worrying about that. 
-  * Once the first fit looks ok, copy the appropriate code and attempt a fit on the second rate. 
-    * Unlike in the last example, we want to plot these fits in different figures, so make sure you duplicate that code as well. 
-    * Again, give appropriate initial guesses and make modifications if it doesn't succeed on the first try. 
-  * Finally, work on the plot aesthetics. 
-    * Modify the title and axes labels. 
-    * Correct the plot annotations. Check significant figures and units. 
-    * Adjust axes ranges if necessary. In particular, make sure that the _x-_axis extends a little bit to the left and right of your first and last points (maybe half a unit?) so that no data is cut off by the border of the plot.  
- 
-{{:phylabs:lab_courses:phys-211-wiki-home:introductory-lab_-python-tutorial:example3_final.png?800|}} 
- 
-Whoa! Take a breath! These plots are EXACTLY like what you need to do with your actual experimental data. You now have a working script that can be used on your first report and which can be modified as needed for use on the rest of your reports this year. That's quite the accomplishment! 
---> 
-</HTML> 
- 
-After completing the whole notebook, be sure to save the final plot from **Task 6**. This will be the **key plot** you turn in for Example 3. 
- 
-===== Example 4 ===== 
- 
-Files needed for Example 4: 
-<WRAP download> 
-{{phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:git:exercise4_pha_spectrum.ipynb}} 
- 
-{{ phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:git:example4_data.tsv |}} 
-</WRAP> 
- 
-In Example 4, we again import data from an external file (Example4_Data.tsv), but this time it is more a more elaborate PHA spectrum. The new element here is that we will plot all the data, but only fit a portion of it. In addition, we will use a more elaborate fit function this time with several parameters and more complex background. 
- 
-Look through the file. Notice how we treat this data and how we write the fit function for a Gaussian. In particular, note the code used to apply our fit only to a portion of the data. Run the file and understand the output. 
- 
-==== Exercise ==== 
- 
-After completing the whole notebook, be sure to save the final plot from **Task 3**. This will be the **key plot** you turn in for Example 4. 
- 
-===== What to turn in ===== 
- 
-Once you complete all four exercises, please put the four final plots together into a single document, save it as a PDF, and upload it to Canvas. (Make sure your name appears in the text of the document somewhere.) We do not need to see (//nor do we want to see//) your raw python code. 
- 
-If you'd like to do this using LaTeX, we have a barebones template here: {{phylabs:lab_courses:phys-211-wiki-home:introductory-lab-python-tutorial:python_tutorial_plots.zip | Python Plots Template}} 
- 
-To use this file with Overleaf, click on the ''New Project'' button and select ''Upload Project'' Upload the zip file, and then use the upload file button (just below the menu in the top left) to upload your plots.  Be sure to change the name to your name! Finally, navigate to each ''{figure}'' section, find the ''\includegraphics'' command, and change the filename in curly brackets to one of your figures.