PHYS2320 Computing 2 Lab 3 Worksheet

Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due

PHYS2320 Computing 2 Lab 3 Worksheet

This worksheet can be viewed as a pdf, html page or as a JuPyter Notebook. If you are using the notebook version then you can work through the worksheet and add your code to it.

You will find it in the Semester 1 Workshops->Workshop 3 - Week 5 folder on the PHYS2320 Minerva pages. There are three versions, pdf, html and notebook.

Aims

This lab's aims are


  • Load and Plot numeric data
  • Indexing and manipulating numpy arrays
  • Using numpy functions to find our information about data
  • Interpolating values


Getting Started

This worksheet is best viewed in JuPyter Notebook. If you have installed anaconda python on your own computer, or are using the cluster machine then you probably have JuPyter already install. If you are using Windows, then there should be a menu entry in your start menu, on a Mac you may need to open a terminal window and type:
jupyter notebook
Alternatively you can use Jupyter Lab
jupyter lab

Attendance Records

Do not forget to register your attendance at a Computer Labs session!

https://phys2320.leeds.ac.uk/attendance/register

Tasks

You have two tasks this week centered around the two data files. Your tasks are:

Task 1

Using numpy.genfromtxt() open the data file Fourier Data.csv into a numpy array.

The first column of the data file is a time, the second and subsequent columns are the terms of a Fourier series:

F(t) = a1 sin ωt + a2 sin 2ωt + a3 sin 3ωt + ⋯ + an sin nωt

(i.e. the second column contains values of a1 sin ωt, the third column contains values of a2 sin 2ωt and so on.)

1. Plot each of the first five fourier components against time.
2. Calcualte the sum of the first five components as a function of time and plot this against time. Repeat this for the frist 10 components and then all the components in the file.

Task 2

The second task is related to the mini-assessment that is due in by midnight on the Thursday after the next Computing 2 lab session in two weeks time.

Your overall task is to write a function that will read a comma separated data file with two columns - Time(s) and Signal(V) and find out various information about he data.

1. Find the largest value of Signal(V) in the file - V_max and the corresponding time t_max.

2. Find hte smallest value of Signal(V) in the file V_min and the corresponding time t_min.

3. V_max should be positive and V_min should be negative, and there should be one point where the Signal(V) crosses zero. Find an estimate for the time t_0 where the signal crossed zero. Note that this is likely to be between data points in the file so you will have to interpolate the data to find the zero crossing point.

A tempalte for the function you shiould write for the mini-assessment is provided below.

Detailed Description and Marking Scheme

You can download a sample data set from the module website at https://phys2320.leeds.ac.uk/peak_find/. When you click on the Submit button the site will generate a unique set of data based on your username. The file is comma separated data values with a single row of header on the first line and between 25 and 75 datapoints. The function you write will be tested on a similar (but not identical!) file - the data used to test your function is generated from the same website.

You should use the code below as your starting point:

# When you submit, you will start with the def line below and finish with the re
# You can assume that the auto-grader is doing the following imports
from pprint import pprint
import numpy as np
import scipy.interpolate as interp
# The def line is the first line you submit
def analyse_data(filename):
"""Mini Assessment 2 analysis function.
Args:
filename(str):
The full path and name of the data file to analyse.
Returns:
(dict):
{"V_max": floating point number of maximum value of Signal(V) in fil
"t_max": floating point number of the time corresponding to V_max
"V_min": floating point number of the minimum value of the Signal(V)
"t_min": floating point number of the time at which the V_min point
"t_0": estimated time at which Signal(V) crossed zero. There will be
}
"""
#Do not change anything in the lines above this comment.
#You should out your code here
# Do not change any of the lines below this point other than to replace the
# with the variables where you have calculated your answer
answers={"V_max": None, "t_max": None, "V_min":None, "t_min":None, "t_0":Non
return answers
# The return line is the last line that you submit
# This code will let you test your function
if __name__ == "__main__": # Check we're not importing this code
test_filename='replace-me'
results=nalyse_data(test_filename)
pprint(results)

Marking Scheme

You get one mark for each value in the answers dictionary that is correct. If your code returns answers that are not correct then it will be checked with a second data file and if it returns answers that are different from the first data file (but not correct) then it will get half marks. (i.e. if you make an attempt to get the right answers but don't succeed you get partial credit - but if you simply return fixed numbers then you don't get marks.)

Deadline

You must submit your function via the link below by midnight on Thursday 14th

November (that is the midnight between Thursday and Friday). For students whosubmitted by the deadline, results should come out one week after the deadline.

As with other mini-assessments, there is a 1 mark per day penalty for submitting after the deadline if you do not have an extension. Please make sure you allow enough time to submit your work as the website will strictly enforce that you submit only the function definition starting with the def line. The submission page is linked to the page you download the work from, but you can go directly to it at https://phys2320.leeds.ac.uk/peak_find/record.

If you have mitigating circumstances and require an extension, then please email the Physics Student Support email.

Hints and Tips

Task 1

The first place to start after you have downloaded the file is to open it in a text editor and inspect it to decide what is the best way to read it.

In this case, you know that numpy.genfromtxt is going to be needed - so you what to see if the file has any column headers and what character is used to separate the columns.

You also want to find where the file has been saved (probably your Downloads folder).

You will want to start by importing numpy and matplotlib.pyplot. By convention, we

import them as follows:
import numpy as np
import matplotlib.pyplot as plt

You don't have to use np and plt but it is probably a good idea to stick to the conventions as it makes your code easier for someone else to work with.

Once you have loaded the data from the file, you can check how many dimensions it has, and what shape is is. After that you can use the indexing operations on a numpy array to pull out just the bits that you need to the do the tasks.

# Replace this cell with the genfromtxt code to read the file
data = np.ones((100,4)) # Make an array of 100 rows and 4 columns of 1.0s
data.ndim # number of dimensions of the array (1D vector, 2D matrix or table, 3
data.shape # The shape tells you the size in each dimension
data.dtype # The dtype tells you the type of data in the array

Indexing arrays to get at the bits you want is covered in the first video tutorial in the numpy series. In many ways it is similar to lists:

data[0]

gets the first element of |*data*

You probably want to separate the array you read with np.genfromtxt into two variables - one e.g. time being the first column and a second e.g. signal being the second and subsequent columns.

For the first parts you will want to use a for loop to extract the correct column and then add the plot to a figure (so you have one graph with 5 different lines on it) - you should aim to give each line a suitable label and add a legend to the plot.

For the second part, you will probably want to make use of the np.sum function and understand what the axis parameter does.

Interpolating Data

Before doing the mini-assessment task, we will need to loom at interpolation. Interpolation is the process of estimating the value of some measurement or function between known data points. Imagine we have a set of (x,y) data points as below:


and we want to know what the measurement would have had we looked at x values between the black data points. To do this we need to make some assumptions about how the y values would have varied in the gaps between the black data points. We can't know this for sure, so we are estimating what the result would have been. This is the process of interpolating.

The simplest assumption is that y varied in a straight line - and so we would estimate the intermediate points as below:


This is called linear interpolation.

Of course looking at the data you might expect y to vary smoothly between the data points, and by considering two data points on either side of the position we are interested in, we could fit a cubic function and this would give perhaps a more realistic estimate of the intermediate value:


Interpolation in Python

The scipy package has a set of functions for interpolating data. Here we will look at the simplest of these - interpolating a 1D function. We will start by making a set of data points (x,y) and then interpolate them.
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-5,5,21) # Set of x values between +/- 5y=np.exp(-(x**2))
plt.figure()
plt.plot(x,y,"r+")
plt.show()
Now we will use scipy.interpolate.interp1d to interpolate data points between the red crosses.

What interp1d gives us is something that we can call like a function, passing it any value of x that lies within the range of the data points we have (we can't interpolate outside of this range - this is extrapolation and is even more dodgy that interpolating values...!)

from scipy.interpolate import interp1d
interpolating_func=interp1d(x,y)
new_x=np.linspace(-4.9,4.9,51)
plt.figure()
plt.plot(x,y,'r+')
plt.plot(new_x,interpolating_func(new_x),'g.')
plt.show()

Task 2

The first thing your function needs to do is to read in the data from file that it is given and get the two columns as two variables. This is rather similar to task 1 above. After this you need to identify the maximum and minimum values of the signal. You may want to do some judicious googling of numpy and maximum value to find the appropriate functions.

To find the corresponding times you will first need to find the index pf the maximum and minimum values in the signal - then you can use those indices to find the corresponding times.

The final item t_0 is by far and away the most difficult to compute - there is unlikely to be a data point exactly at Signal(V)=0 so you need to interpolate the data to find exactly where it crosses the y=0 axis. As we will see in the lab session, in Python interpolating data gives you something that you can call with an x value that will return the corresponding y value.

This is still not quite the correct thing for the mini-assessment as you know the y value and want the x value - on the figure below it is where the two dotted lines intersect.

We will see next week that there are function that can do this, but for now an easy thing to do is simply to swap the x and y arrays when doing the interpolation and get time as a function signal. This is not quite straightforward because now there are some values of Signal(V) have two different values of time(s) and the interpolating function struggles tobe sensible. The figure below shows the raw data and the results of trying to simply interpolate all the time(s) values as a function of the Signal(V).


The way to solve this is to do the interpolation only for the part of the data between t_max and t_min - to ensure that there is only one value of time(s) for each value of Singal(V). This is shown as the orange line above and gives a much more sensible value. You should have all the information you need from finding values of t_max and t_min to get the correct sections of the time(s) and Signal(V) to do this interpolation.

Testing your Code

As with the first mini-assessment, submitting code that does not work - for whatever reason - will get you 0 marks. So it is important to check that your code at least runs. In the template code cell in the Task 2 section, the function def and body are surrounded by the code necessary to run and test the function. To do this you should:

1. Replace the replace-me in the test_filename='replace-me' line with thename of your actual data file.
2. Use the kernel menu to restart the kernel
3. Run that cell and only that cell and ensure that it runs and returns the data you expected.

发表评论

电子邮件地址不会被公开。 必填项已用*标注