The goal of this lab is to learn more about making classes, and to use classes to organize code. The particular application for this assignment is creating plots of data read from a file. An important part of this project will be process and coding style. Comment your code appropriately, and try to make it easy to read.
For this lab, you will create two classes. One class will be called Datum, and each Datum object will hold three values: x, y, and z. In addition to holding the data, the Datum class will act like a Zelle graphics object, but also contains fields that represent data. The second class will be called DataCollection, and will contain a list of Datum objects.
The purpose for creating the two classes is to permit you to analyze and graph data in a flexible way. The Datum class handles the data, input/output, and drawing tasks for individual data points. The DataCollection class handles things like graphing and data analysis tasks that require access to all of the data. When working correctly, you will be able to treat graphs of data like graphics objects that can be created, manipulated, and moved around.
- Download the datumskeleton.py file and change its name to datum.py. You will need the Zelle graphics.py file as well. To avoid problems later, you may want to re-indent the code in your editor before you start working with the file. Note that you cn run the datum.py file from the start. It won't do anything, of course, until you start to define some of the methods.
- Start with the set method, which should create and set x, y, and z fields in the object to the values in tx, ty, and tz, and create and set the fields x0, y0, dr, and color to the input values drawX, drawY, drawR, and drawColor. Then have the __init__ method call the set method to avoid code duplication. The __init__ method should also create a field called circle and set its value to the keyword None. This field will be used to hold a Circle in the draw method.
- Next, write the accessor functions X, Y, and Z. These functions should simply return the values of the x, y, and z fields, respectively. (Accessor functions are usually very simple.)
- The draw function should check if the circle field is equal to None. It if is, the method should create a circle at (self.x0, self.y0) with radius self.dr, fill it with the color self.color and then draw it into the given window. If the circle field is not null, then it needs to be undrawn, moved to the new location, and then drawn again into the window. The (x0, y0) fields of self are the new location. The last place the circle was drawn will be center point of the Circle object referenced by self.circle.
- The move, undraw, and setFill functions should first check to see if the circle field is None. If the circle field is not equal to None, then the methods should call the appropriate method on the circle object. Note that the move function should not affect the underlying (x, y, z) data. Both the move and setFill functions should update the appropriate drawing fields (x0, y0, color).
- The read and write functions need to read/write a single line of the file. Each line will hold three values (x, y, z). An example of a complete data file is testdata.txt. The read function will read a single line of the file and extract the three values. The string split method is a good way to divide the line into three parts. The write function needs to write a single line of the file, which should be three numbers and a newline character.
- Download or copy the DataCollection skeleton. Your task is to complete the three methods write, add, and move. The add method should just append the datum object to the list of dataum objects held by the DataCollection.
- The write method should open the file specified by filename and have all the Datum objects in the data list write themselves to the file.
- The move method should call the move method on all of the Datum objects in the data list and on the background rectangle held in the bkg field.
- Once the above functions are written, you can run the DataCollectionMain function. Note that you may want to simplify the function at first to get things working.
- Your code should be well-commented. The skeleton file contains comments for each function. You should add in additional comments for anything you write that is non-obvious.
Below are some suggested extensions.
- Add functionality to the DataCollection class. Have it calculate standard deviations, means, or linear regresions (line fits) of the data.
- Find some real data from a real project and make some plots of the data.
- Finish making the Datum class look like a Zelle graphics object (like the Peace symbol).
- Finish making the DataCollection class look like a Zelle graphics object.
- Write a function that uses the DataCollection class, but gives it an interface that lets the user enter a filename and perhaps the characteristics of the plot.
- Write a Python program to generate data according to some function or distribution and plot the data.
Create a web page for your writeup. Your writeup should include the following.
- A brief description of the task (in your own words).
- An image showing your Datum class working.
- An image showing your Data Collection class working.
- How are fields (data) in an object created? Give an example from your code.
- How are fields in an object accessed inside a method? Given an example from your code.
- A description of and pictures for any extensions you did.
When your writeup is ready for viewing and linked to your index page, send me an email.
To hand in your python code, place it on the Academics fileserver in the private folder. Do not post it on the web. You may include snippets from your code in your writeup to demonstrate how certain aspects of the function work, but please do not include the whole thing.
To place the code on the fileserver, go to the server fileserver1 and select the Academics volume. Within that there ought to be a CS151 folder, and within that there should be a folder with your username, and within that ought to be a folder labeled private. You can read and write files to the private directory and I can read them, but no one else can access them. Put your python files in that directory to hand them in. Please organize your files into folders by project.