Activity 1.4.2 Objects and Methods: 1. Form Pairs As Directed by Your Teacher. Meet or Greet Each Other To Practice
Activity 1.4.2 Objects and Methods: 1. Form Pairs As Directed by Your Teacher. Meet or Greet Each Other To Practice
Materials
Procedure
1. Form pairs as directed by your teacher. Meet or greet each other to practice
professional skills. Set team norms.
2014 Project Lead The Way, Inc.
Computer Science and Software Engineering Activity 1.4.2 Objects and Methods Page 1
2. To open an image in a program, you will need a way to find the file using the
programming language. You can use a files absolute filename. Most operating
systems and programming languages remember one location in the file system as
your "working directory," and a file can be described relative to that location: a
relative filename. First we deal with absolute filenames.
Most file systems are hierarchical, forming a tree that begins with a root
directory. An absolute filename specifies where the file is stored from the root,
which is typically indicated by / in UNIX and Max Operating Systems and by the
startup drive letter in Windows, such as C:\.
Files and directories are nodes, each branching from one parent in the tree,
with the root considered the top of the tree. The absolute filename of admin (in
the green box below) is C:/Users/admin. What is the absolute filename of
nice.jpg (in the red circle below)?
If we were currently working in the admin directory, what would be the relative
filename for nice.jpg?
4. The table below lists the commands from UNIX for navigating the tree. Even
when you run a Python environment on Windows or another operating system,
Python will recognize these UNIX commands.
Command
Purpose
pwd
cd
cd ..
cd dirname
ls
Try to navigate up the tree and back down into a different directory using these
commands in the iPython shell. The iPython session below is an example. Your
output will be different, and you will have to tailor your input to match the output
in order to navigate up the filesystem.
In []: pwd
Out[]: uC:\\Windows\\system
In []: cd ..
C:\Windows
In []: ls
Volume in drive C is Win7Disk
Volume Serial Number is 1A7E-10ED
2014 Project Lead The Way, Inc.
Computer Science and Software Engineering Activity 1.4.2 Objects and Methods Page 3
Directory of C:\Windows
05/07/2013
10:36 AM
<DIR>
05/07/2013
10:36 AM
<DIR>
..
11/20/2010
04:29 PM
07/13/2009
11:52 PM
65,024 bfsvc.exe
<DIR>
Cursors
In []: cd Cursors
C:\Windows\Cursors
5. You may have noticed the double backslashes in the output shown above from
pwd. The double backslashes are an escape character. Escape characters are
multi-character codes that allow you type single characters that would be invisible
or would have other effects. Quotation marks can be included inside a quoted
string, for example:
'This entire quote \' has a single quotation mark in the
middle but no backslash!'
A few common escape characters are shown in the following table:
Escape sequence
\t
\n
\\
\'
\"
Character
Tab
Newline
Backslash
Single quotation mark
Double quotation mark
6. Launch Canopy. Open an editor window. Set the working directory to your folder.
Create a new Python file. Save the file as JDoe_JSmith_1_4_2.py.
7. Lines 12 and 14 of the code below create an absolute filename for an image by
assuming that the image is in the same folder as your Python script. Use
Windows Explorer to place files woman.jpg and 'cat-1a.gif' in the folder
where you are saving Python scripts.
Earlier we used the code editor to define functions that we still executed in the
iPython session. Coding in the code editor will execute directly with the play
button if it is not in a function definition. Execute the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'''
JDoe_JSmith_1_4_2: Read and show an image.
'''
import matplotlib.pyplot as plt
import os.path
import numpy as np
abbreviations
You should see a new window displaying the image of a woman, perhaps hidden
behind other windows. You can use Alt-tab to cycle through windows.
The figures created by matplotlib are interactive graphical
user interfaces (GUIs). The GUI shows the coordinates of
the mouse pointer, as shown at right. These coordinates are
the image coordinates, more or less. (That's not quite true,
since the coordinates shown by the GUI can be between
integers, unlike the image coordinates. Also, we could have
placed the image with its upper left corner somewhere other
than (0, 0).)
a. What is the (x, y) coordinate pair of the womans nose
in the image coordinate system?
b. Change the code so that it shows the cat. What are the
image coordinates at the tip of the cat's nose?
The particular Figure object is being stored in a new variable, fig. The
particular AxesSubplot object is being stored in the variable ax.
That was a lot of information! fig and ax are both objects. What class is each
of them in?
fig is an instance of the class _________________
ax is an instance of the class __________________
Similarly, in line 22, the method _______ is being called on the object
________. That method is being given ___ arguments. That method is a
method of the class _________.
The method subplots can also be used to create a grid of AxesSubplots, as shown
below. In this case subplots (1, n) will return
where ndarray is an n-d array, short for an "n-dimensional array." You can access
the elements of an ndarray with an index in square brackets:
17
18
19
20
21
22
23
24
a. Modify lines 18 and 20 of your code to match what is shown above. You will
have to re-execute the code to see the effect. Practice using object-oriented
syntax by describing line 22: the method _______ is being called on the
object ________.
i.
ii.
10. Methods and other functions often can be called with additional arguments. If
you don't provide an optional argument when you call the function, the default
value of that argument is used.
The imshow() method was called on both of the AxesSuplots shown below. For
the axes on the left, the method was called with interpolation='none',
2014 Project Lead The Way, Inc.
Computer Science and Software Engineering Activity 1.4.2 Objects and Methods Page 10
whereas the axes on the right used the default value of the interpolation
argument.
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ax[0].set_ylim(470, 420)
ax[1].set_xlim(135, 165)
ax[1].set_ylim(470, 420)
# Show the figure on the screen
fig.show()
The keywords of a function are often important ideas from the library's
subject matter. Interpolating is an important idea in math. Describe the
connections among interpolation between data points, the interpolation
argument, the image above, and the code above.
Some methods of
Description
plt.SubplotAxes
axis('on' | 'off')
set_ylim(ymin,ymax)
cla()
Clear axes
imshow(img)
Computer Science and Software Engineering Activity 1.4.2 Objects and Methods Page 12
minorticks_on()
minorticks_off()
set_xlabel(string)
set_ylabel(str)
set_xticks(list)
set_title(string)
In []: ax[0].imshow(img)
In []: fig.canvas.draw()
Reminder: You can use the up arrow to avoid having to retype the draw()
command each time.
Share your work for this step as directed by your instructor, perhaps by
showing the teacher your image on screen and by submitting a ZIP file of:
12. The ability to wade through documentation full of unknown terms is an important
skill. Go to the documentation at
http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.imshow. This
documentation is for the class Axes, which includes the AxesSubplot subclass.
The link provided here points to the documentation for the imshow() method.
From the documentation, identify one additional method of an AxesSubplot .
Describe at least one of the optional arguments of that method and state its
default value.
13. The class AxesSubplot has many methods for displaying data, including the
plot() method.
plot(x, y, 'ro') places red circles (coded by 'ro') at all points (xi, yi) where
x and y are lists of the xi and yi coordinates, respectively. In an image containing
a few faces, mark the eyes with red circles using plot().
Share your work for this step as directed by your instructor, perhaps by showing
the teacher your image on screen and by submitting a ZIP file of:
Conclusion
1
2 What is an object?
3 Objects have methods and properties. What are methods and properties?