The DataClass module

alt text




Introduction

In previous activities we've learned how to use Python to simulate physical systems and to fit datasets to physical models. Rather than hosting another activity, this page will host and describe a generic two-dimensional data fitting module that can be used to produce quality fits and plots to a wide range of datasets and models.





Download the data file(s)

There are several example data files. You should download at least one of them. Once your script is working, it should be simple to switch out one data file for another. Along with each data file, there is a corresponding fitting_function that needs to be coded up in your Python script.

  1. Data 1   --   \( y = \exp(-x/a) \left[ 1 - (x/b)^2 + (x/c)^3 \right] \)

  2. Data 2   --   \( y = x^2 \sin(b x + c) + d \)

  3. Data 3   --   \( y = a \sin(b x + c) + d \)

  4. Data 4   --   \( y = a \sin(b x) + c \cos(d x) \)

Make sure to download at least one of these files and save it to the same folder you saved the DataClass.py file.





Exploring DataClass

After you've downloaded the DataClass file, open up a new, blank, Python file. This will be your script with which you load in and fit the data. Before you start coding, change the "Working Directory" in Canopy to the folder where you saved the DataClass.py file. Once you've changed your working directory, complete the following steps to finish your program:

  1. Import the DataClass module and the Numpy library using an import statement. For example,

    import DataClass
    import numpy as np

  2. Import any other libraries you might need.
  3. Next define a function that will be your fitting function. You can name it whatever you like. An example of a fitting function is,

    def func(x,a,b,c):
        return a*x*x + b*x + c

    This is a general quadratic equation with three parameters. Your fitting function will depend on which data file you downloaded.
  4. In the lines below your fitting function definition will be the commands to instantiate a Data object that holds your data.

  5. Set a variable data equal to an instance of the Data class. You must pass the file name of the data file as an argument to Data(). For example,

    data = DataClass.Data(filename,func)

  6. On the next line call the statistics method of data. This will print out some simple statistics such as mean and range of the data.

  7. Finally, call the fit_data method of data. This will fit the data to your fitting function and plot the results. Check the definition of fit_data in the DataClass.py file to check for any arguments it might take.

Now you can execute the script. If all goes well you should see a plot pop up displaying the data and the fit to the data. An example of what you should see is,

alt text




Playing with options

Now that you have a working data fitter, try passing different keyword arguments to data.fit_data() to change properties of the plot. Some examples of what you can change are:

  1. fontsize. This controls the size of the font used in the labels.
  2. fmt. This controls the shape and color of the points used in plotting the data. It defaults to blue circles.
  3. markersize. This controls size of the data points.
  4. color. This sets the color of the best fit line.
  5. linestyle. This sets the style of the best fit line. It can be solid, '-'; dashed, '--'; or dashed-dot,'-.'.
  6. linewidth. This controls how thick the best fit line is.
  7. grid_off. If set to True, this turns of the grid in the plots.
  8. There are many other style choices that can be found in the documentation for the plot function.