This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
phylabs:lab_courses:phys-211-wiki-home:inactive:introductory-lab-python-tutorial:start [2022/09/27 17:18] mccowan |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Python Tutorial ====== | ||
| - | <div pythoncomic> | ||
| - | </ | ||
| - | http:// | ||
| - | < | ||
| - | < | ||
| - | .wrap_pythoncomic { | ||
| - | transition: | ||
| - | background: url(" | ||
| - | width: 600px; | ||
| - | height: 700px; | ||
| - | display: block; | ||
| - | margin: auto; | ||
| - | background-position: | ||
| - | background-size: | ||
| - | } | ||
| - | |||
| - | .wrap_pythoncomic: | ||
| - | transition: | ||
| - | transition-delay: | ||
| - | background: url(" | ||
| - | background-size: | ||
| - | |||
| - | } | ||
| - | </ | ||
| - | </ | ||
| - | |||
| - | |||
| - | ====== 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:// | ||
| - | * 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 // | ||
| - | * 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 " | ||
| - | * Read the page [[phylabs: | ||
| - | * This will orient you and give you examples of some some basic syntax and techniques. | ||
| - | |||
| - | <WRAP help> | ||
| - | < | ||
| - | < | ||
| - | |||
| - | 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' | ||
| - | |||
| - | **Online Python Resources** | ||
| - | |||
| - | **Introduction to Python for Data Scientists** | ||
| - | Online course with a series of video tutorials, designed to introduce you to Python in about 4 hours. | ||
| - | |||
| - | **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. | ||
| - | |||
| - | **A Crash Course in Python for Scientists** | ||
| - | |||
| - | Written by Rick Muller at Sandia National Laboratories, | ||
| - | |||
| - | **Lectures on scientific computing with Python** | ||
| - | |||
| - | Basically an online textbook, written by Robert Johansson at the iTHES Research Group. Also written primarily for physicists. | ||
| - | |||
| - | **Interactive Online Course from the University of Waterloo / Python From Scratch Video Lectures** | ||
| - | |||
| - | The first site has tools to learn & practice Python on their website, the second has a focused set of lectures on Python. Note that this is being taught with a computer science emphasis, so their focus will differ from other tutorials. | ||
| - | |||
| - | **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. | ||
| - | |||
| - | **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. | ||
| - | |||
| - | **Cracking Codes with Python / Invent Your Own Computer Games with Python / 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. | ||
| - | </ | ||
| - | </ | ||
| - | |||
| - | ====== 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 " | ||
| - | * 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: | ||
| - | </ | ||
| - | ====== Assignments ====== | ||
| - | |||
| - | ---- | ||
| - | ===== Example 1 ===== | ||
| - | |||
| - | Files needed for Example 1: | ||
| - | |||
| - | <WRAP download> | ||
| - | {{ : | ||
| - | </ | ||
| - | |||
| - | 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. | ||
| - | |||
| - | < | ||
| - | <!-- | ||
| - | 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 // | ||
| - | * In the bottom subplot, plot the // | ||
| - | * 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/ | ||
| - | * 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. | ||
| - | --> | ||
| - | </ | ||
| - | ==== More Plotting Help ==== | ||
| - | |||
| - | If you'd like a broader introduction to what Matplotlib can do, visit J.R.Johansson' | ||
| - | [[https:// | ||
| - | |||
| - | 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 // | ||
| - | |||
| - | It sometimes is necessary to also specify an extra amount of space to be added by using the " | ||
| - | |||
| - | <code python> | ||
| - | plt.tight_layout(pad = 2) | ||
| - | plt.savefig(' | ||
| - | plt.show() | ||
| - | </ | ||
| - | |||
| - | {{: | ||
| - | |||
| - | ===== Example 2 ===== | ||
| - | |||
| - | Files needed for Example 2: | ||
| - | |||
| - | <WRAP download> | ||
| - | {{phylabs: | ||
| - | |||
| - | {{ : | ||
| - | </ | ||
| - | |||
| - | 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: | ||
| - | |||
| - | 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 ==== | ||
| - | |||
| - | < | ||
| - | <!-- | ||
| - | 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 your two fits appear in two different colors and add a legend. | ||
| - | |||
| - | --> | ||
| - | </ | ||
| - | |||
| - | 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: | ||
| - | |||
| - | {{ : | ||
| - | </ | ||
| - | |||
| - | 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. | ||
| - | |||
| - | < | ||
| - | <!-- | ||
| - | We now want to modify the Example 3 script to handle data of the sort you collected in the first experiment, [[phylabs: | ||
| - | |||
| - | Consider this actual lab notebook page detailing a set of measurements from the gamma cross sections experiment: | ||
| - | |||
| - | {{: | ||
| - | |||
| - | |||
| - | 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' | ||
| - | * 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. | ||
| - | |||
| - | {{: | ||
| - | |||
| - | 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! | ||
| - | --> | ||
| - | </ | ||
| - | |||
| - | 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: | ||
| - | |||
| - | {{ : | ||
| - | </ | ||
| - | |||
| - | In Example 4, we again import data from an external file (Example4_Data.tsv), | ||
| - | |||
| - | 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. | ||