Computing for Scientists Visualization (Oct . 28, 2010 Nov. 09, 2010) Jie Zhang Copyright CDS 130 - 003 Fall, 2010 Content 1. Introduction

2. Computer Visualization Pipeline 3. 2-D Data Visualization Height-plot method Color map method References: (1) Data Visualization: Principles and Practice, by Alexandru C. Telea, A. K. Peters Ltd, ISBN-13: 978-1-56881-306-6, 2008 (2) CDS 301 Scientific Information and Data Visualization, Fall 2009 at

http://solar.gmu.edu/teaching/2009_CDS301/index.html 1. Introduction What is visualization? Why visualization? Picture A Picture Is Worth a Thousand Words Cave Painting: the dawn of civilization

Population Graph: Height-Plot Year Numeric output of the Predator-Prey Model A graph is worth one thousand numbers Height plot of the

Predator-Prey Model Graph: 3-D Height-Plot Numeric output: a 2-Dimension data array, which is the same as Graph: 3-D Height-Plot Height plot A graph is worth one million numbers

Visualization - Cognition Visualization is a cognitive process performed by humans in forming a mental image of a domain space Human Cognition Most effective way human takes in information, digest information, making decision, and being creative.

The most important among the five senses What is Scientific Visualization Scientific visualization is an interdisciplinary branch of science primarily concerned with the visualization of three dimensional phenomena (meteorological, medical, biological etc) where the emphasis is on realistic rendering of volumes, surfaces, illumination sources with a dynamic (time) component.

Friendly (2008), also http://en.wikipedia.org/wiki/Scientific_visualization At Information Age Goal of Scientific Visualization Provide scientific insights

Effective communication Example: Scalar Data Height plot of a 2-D data, and the contour lines In topology, the data are of 2-D: a curved surface The data are mapped into 3-D geometric space in computer memory The data are then rendered onto a 2-D visual plane on a screen Example: Vector Data

Stream tubes: show how water flows through a box with an inlet at the top-right and an outlet at the lower-left of the box; the data are (1) 3-D volume, (2) vector Example: Unstructured Data Sampled data Rendered data Scattered Point Cloud and Surface Reconstruction. The data are a set of sampled data points in 3-D space, and the distribution of data is unstructured; need to reconstruct the surface from the scattered points

Example: Volume Visualization 3-D data of human head. Instead of showing a subset (e.g, a slice as in CT images), the whole 3-D data are shown at once using the technique of Opacity Transfer Function Example: Information Visu. The file system of FFmpeg, a popular software package for encoding audio and video data into digital format Information, such as name and address, can not be interpolated; the visualization focuses on the relations.

An attempt at visualizing the Fourth Dimension: Take a point, stretch it into a line, curl it into a circle, twist it into a sphere, and punch through the sphere. --Albert Einstein-- 2. Computer Visualization Pipeline How to do it by a computer? We illustrate the process using the well known 2-D Gaussian function:

f( x, y ) e ( x2 y2 ) Gaussian Function in 1-D It is straightforward to visualize the 1-D Gaussian function f( x) e ( x2 ) (1) Choose a data domain, e.g., x in [-3.0, 3.0] (2) Choose a set of regular points in x, e.g., step size 0.5:

x1=-3.0, x2=-2.5, x3=-2.0, x4=-2.5.X13=3.0 (3) Calculate the corresponding functional value f(xi) (4) Draw the 13 data points (xi, f(xi)) for i=1:13 (5) Connecting these points to form a curve, which shows the Gaussian function Gaussian Function in 1-D f( x) e ( x2 ) x=-3.0:0.1:3.0; %declare the domain space and define the grid % x: one dimensional data array

y=exp(-power(x,2)); %calculate the discrete functional value % y: also one dimensional data array plot(x,y,'*') hold on plot(x,y) xlabel('x') ylabel('y') title('Gaussian 1D') print -dpng 'Gaussian_1d.png' X: 1-D data array interval or domain: from -3.0 to 3.0

sub-interval: 0.1 value=[-3.0, -2.9, -2.8,2.9, 3.0] number of elements: N=61 Gaussian Function in 1-D f( x) e ( x2 ) Gaussian Function in 2-D f( x, y ) e ( x2 y2 )

We know the result. In a height-plot format, It should look like something below. But how to do it through the computer? Created by PARAVIEW, a power visualization package A 3-D height plot showing the Gaussian

Surface The Grid on the domain Pipeline Step 1: Data Acquisition Step 1: data acquisition. For any input data (experiment, simulation etc) , convert the data into a discrete dataset over a regular grid Gaussian Example: Choose a uniform grid in the domain

space Nx=30 (i=1:30) Ny=30 (j=1:30) Calculate the functional value at these grid points This creates a set of 30 X 30, or 900 in total discrete 3D data points [x,y,z] Pipeline Step 2: Data Mapping Computer graphics only handles a 3-D scene Gaussian Example: Step 2: Mapping the

Form a set of fourdiscrete dataset onto a vertex polygons or 3-D scene. Neighboring quadrilateral in 3-D discrete data points are space grouped to form graphic For each quadrilateral, primitive shapes that a the position of the four computer can handle vertices are known, and efficiently, e.g., point,

its normal direction is line, triangle, calculated (in order to quadrilateral, and determine its luminance) tetrahedron Pipeline Step 2: Data Mapping Quadrilateral shape Pipeline Step 3: Rendering

Step 3: Computer graphics renders the 3-D scene (a set of primitive shapes) onto a 2D projected place, by 1. Choose the view angle 2. Choose the light source 3. Choose the shading Gaussian Example: Viewed from top-front

From a point light source in the front Smooth shading Shading Flat Shading Gouraud Shading Shading Flat shading: Given a polygon surface (e.g., quadrilateral), flat

shading applies the lighting model once for the complete polygon, e.g., for the center point The whole polygon surface has the same light intensity The least expensive But visual artifact of the faceted surface Gouraud shading (or smooth shading): Apply lighting model at every vertex of the polygon The intensity between the vertices are calculated using interpolation, thus yielding smooth variation Visualization Pipeline f( x, y ) e

( x2 y2 ) Continuous data 1. Data Acquisition float data[N_x,N_y] Discrete dataset 2. Data Mapping Quadrilateral in 3D

Geometric object 3. Rendering Displayed image Visualization Pipeline: Summary Step 1: data acquisition, which is to convert any input data into a discrete dataset within a domain. For Gaussian function, the input is a continuous Gaussian function, the output is a discrete dataset Step 2: data mapping: which is to map the discrete

dataset from step one (input) onto a 3-D scene (output) in computer memory. For Gaussian function, the output is a set of quadrilaterals in 3-D space. Each quadrilateral has four vertexes. Step 3: rendering, which is to display the 3-D scene created in step 2 (input) onto a projected 2-D image (output). 3. 2-D data Visualization(Nov. 1, 2010) Visualization with Matlab Watch Matlab tutorial video on arrays, including indexing and find function:

http://www.mathworks.com/demos/matlab/ visualizing-data-overview-matlab-videodemonstration.html 2-D Data Visualization Method 1: Height plot Create 2-D data array in Matlab Nested For loop in Matlab surf in Matlab Method 2: Colormap RGB color system image in Matlab Height-Plot Method

Height Plot Population Evolution Over the Period of 40 years 120 100 Population 80 60

40 20 0 0 5 10 15

20 Years 25 30 35 Population plot from Natalia Lattanzio 40

Gaussian Function f( x) e ( x2 ) (1) Choose a data domain, e.g., x in [-3.0, 3.0] (2) Choose a set of regular points in x with a subinterval of 0.1: x1=-3.0, x2=-2.9, x3=-2.8, x4=-2.7.X13=3.0 (3) Calculate the corresponding functional value f(xi) (4) Draw the 61 data points (xi, f(xi)) for i=1:61 (5) Connecting these points to form a curve, which shows the Gaussian function

Gaussian Function in 1-D f( x) e ( x2 ) %find number of intervals x_start=-3.0 x_end=3.0 x_sub=0.1 N=(x_end-x_start)/x_sub %number of iteration N+1 %for loop to find discrete x and y values for i=[1:N+1]

x(i)=x_start+(i-1)*x_sub; y(i)=exp(-power(x(i),2)); end plot(x,y,'*') hold on plot(x,y) X: 1-D data array interval or domain: from -3.0 to 3.0 sub-interval: 0.1 value=[-3.0, -2.9, -2.8,2.9, 3.0] number of elements: N=61

Gaussian Function in 1-D f( x) e ( x2 ) sine Function f( x) sin x Class activity (1) Choose a data domain, x in [-6.28, 6.28] or [-2,2] (2) Choose a set of regular points in x with a subinterval

of 0.1: x1=-6.28, x2=-6.18, x3=-6.08.8, x4=-2.7x..=6.28 (3) Calculate the corresponding functional value f(xi) (4) Connecting these points to form a curve, which shows the sine function y=sin(x) % sine function in Matlab Height-Plot of 2-D Gaussian f( x, y ) e ( x2 y2 ) 2-D Array

A large amount of scientific data are in the format of 2-D array, e.g., images Watch Matlab tutorial video on arrays, including indexing and find function: http://www.mathworks.com/demos/matlab/ working-with-arrays-matlab-video-tutorial.html 2-D Array Create 1-D array >> a=[1,2,3,4,5]

>> a=[1 2 3 4 5] >> a=1:5 >> a=[1:5] %indexing and assigning values using for loops %.e.g., assign x^2 for i=[1:5] x(i)=power(i,2) end %what are x? Index is used to access the array

12345 >>x(3) 2-D Array Create 2-D array >> a=[1,2,3;4,5,6;7,8,9] %3 x 3 array a= 1 4 7

2 5 8 3 6 9 2-D Array Index of 2-D array (1,1)

(2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2,3) (3,3) >>a(1,1) 1

>>a(2,3) 6 >>a(i,j) Index of a 2-D array has two subscripts First subscript, often noted by i, indicate the row number Second subscript, often noted by j, indicate the column number

(continued) (Nov. 4, 2010) 2-D Array Visualize the index and the value of a 2-D array >> a=[1,2,3;4,5,6;7,8,9] %3 x 3 array a= 1 2 3 4 5 6 7 8 9

>>surf(a) 2-D Array Re-assign the value of the elements >> a=(2,2) ans = 5 >>a(2,2)=10 a= 1 2 3 4 10 6 7

8 9 2-D Array Group exercise: (1) Create the following 2-D array (2) Visualize it using surf (3) Change the element at (2,2) to 20 (4) Visualize the new 2-D array a= 2 3 6

2 4 6 2 5 6 Nested FOR loops Create a 2-D array using nested FOR loops for i=[1:3]

for j=[1:3] a(i,j)=j+(i-1)*3; end end a= 1 4 7

2 5 8 3 6 9 Double FOR loop is a quick way to go through every data elements in the 2-D data array Assign the value Check the value

Change the value Nested FOR loops Find the values of the following double FOR loops: for i=[1:3] for j=[1:3] a(i,j)=i+j end End a= ? ?

? ? ? ? ? ? ? 2-D height plot f( x, y ) e

( x2 y2 ) Draw a 2-D function in the following domain space: (1) x between [-3, 3], with step size of 0.1 (2) y between [-3,3] , with step size of 0.1 2-D height plot x_start=-3.0 x_end=3.0 x_sub=0.1 Nx=(x_end-x_start)/x_sub %number of points along x y_start=-3.0 y_end=3.0

y_sub=0.1 Ny=(y_end-y_start)/y_sub %number of points along y for i=[1:Nx] for j=[1:Ny] x(i)=x_start+i*x_sub; y(j)=y_start+j*y_sub; f(i,j)=exp(-(power(x(i),2)+power(y(j),2))) end end surf(f) 2-D height plot f( x, y ) e

( x2 y2 ) 2-D height plot Group exercise: Draw the following 2-D function in the following domain space: (1) x between [-3, 3], with step size of 0.1 (2) y between [-3,3] , with step size of 0.1 f( x, y ) x 2 y 2 2-D height plot

f( x, y ) x 2 y 2 Image method and Colormap (Nov. 4, 2010) Color Question: Do scientific data have intrinsic color? For example, temperature reading around the globe. For example, the values of 2-D Gaussian function

Color Answer: Most scientific data concern about value, strength, or intensity. They have no color Color image of scientific data are called pseudocolor image, which uses color as a cue to indicate the value of the data. Colormap Gray-scale Map Rainbow Colormap

Computer Color A special type of vector with dimension c=3 The value of a color = (R, G, B) RGB system: convenient for computer R: red G: green B: blue HSV system: intuitive for human H: Hue (the color, from red to blue) S: Saturation (purity of the color, or how much is mixed with white V: Value (the brightness)

Computer Color A pixel of a LCD monitor contains three sub-pixels: R, G, B Each sub-pixel displays the intensity of one primary color Modern LCD monitor uses 1 Byte or 8 bits to determine the intensity of the primary color Each primary color has 255 intensity levels The number of colors of a LCD monitor = 255 (red) x 255 (green) x 255 (blue) = 16,581,375 Or 16 million different colors The monitor shows true color Human eyes discern 10 million color

(continued) (Nov. 9, 2010) RGB Cube R yellow magenta G B

Cyan RGB System Every color is represented as a mix of pure red, green and blue colors in different amount Equal amounts of the three colors give gray shades RGB cube main diagonal line connecting the points (0,0,0) and (1,1,1) is the locus of all the grayscale value Color (0,0,0): dark Color (1,1,1): white Color (0.5,0.5,0.5): gray level between dark and white Color (1,0,0): Red

Color (0,1,0): Green Color (1,1,0): ? (1,0,1): ? (0,1,1): ? Colormap in Matlab To select a colormap interactively: Use the Colormap menu in the Figure Properties pane of the Plot Tools GUI

Matlab built-in Colormap Example: Re-usable function function gauss = Gaussian_2d () x_start=-3.0 x_end=3.0 x_sub=0.1 Nx=(x_end-x_start)/x_sub %number of points along x y_start=-3.0 y_end=3.0 y_sub=0.1 Ny=(y_end-y_start)/y_sub %number of points along y

for i=[1:Nx] for j=[1:Ny] x(i)=x_start+i*x_sub; y(j)=y_start+j*y_sub; f(i,j)=exp(-(power(x(i),2)+power(y(j),2))) end end gauss=f; endfunction f( x, y ) e ( x2 y2 )

Return value Colormap in Matalb Call the function you created and visualize it >z=Gaussian_2d >surf(z) %default color map jet >colormap(gray) >colormap(jet) colormap(jet) Use Colormap to

change color colormap(gray) colormap Group exercise: Create the following 2-D array in Matlab/Octave >a=(1,2,3;4,5,6;7,8,9) Visualize it using surf function, the surface function or 3-D height-plot provided by Matalb Change to colormap to gray, jet, spring, hot et al. Colormap in Matalb

How does a colormap work? map your data value on each pixel (e.g., row i, column j) into a color based on the colormap A data value of 5.0 will Re-scale is needed >m=gray >size(m) ans = 64 3 >m 0.000 0.000

0.015 0.015 0.031 0.031 0.047 0.047 0.063 0.063 . 0.968 0.968 0.984 0.984 1.000 1.000 0.000 0.015 0.031

0.047 0.063 0.968 0.984 1.000 Row 1 Row 2 Row 3 Row 4 Row 5 ----Row 62 Row 63

Row 64 correspond to color (0.063, 0.063, 0.063), and computer understands this color and will display it correctly A data value of 0 will correspond to color (0, 0, 0),

or dark pixel A data value of 64 will correspond to color (1, 1, 1) or a white pixel Your data need to be rescaled between 0 and 64 surf do re-scale for you Colormap: re-scale you use a colormap of 64 colors to visualize a 2-D data, e.g., 2-D Gaussian What happens in computer is that, the data range of the 2D Gaussian function from 0 to 1, is mapped to the color range from 1 to 64.

0 in function value corresponds to color number 1 1 in function value corresponds to color number 64 Any value in between in function is re-scaled to a value between 1 and 64 0.5 in function corresponds to color number 32 colormap Group exercise: Do you understand the changes? Create the following 2-D array in Matlab/Octave >a=(1,2,3;4,5,6;7,8,9)

Visualize it using surf function, the surface function or 3-D height-plot provided by Matalb Change the size of the colormap >surf(a) >colormap(gray(64)) % gray colormap with 64 colors >colormap(gray(9)) % gray colormap with 9 colors >colormap(gray(2)) % gray colormap with 2 colors >m=colormap(gray(2)) image method in Matlab For many applications, you want to show your data as a flat image, when your data are given as a 2-D matrix. image does the trick for you

image does not scale the data for you. >z=Gaussian_2d %get the 2-D data >colormap(gray) %choose gray colormap >image(z) % show the image % but it does not work % it is dark It does not work! >image(z*64) It works! Why?

>image(z*256) Why it saturates at the center? image method in Matlab 2-D Gaussian image in gray colormap 2-D Gaussian image in jet

colormap colormap Group exercise: visualize the following 2-D array using image method >for i=[1:64] >for j=[1:64] >f(i,j)=i >end >end >colormap(gray(64)) >image(a) %repeat the process, but change f(i,j)=j

%Do you fully understand what you get? %what if colormap(gray(32)) %what if colormap(gray(128)) >m=colormap(jet(10)) % check out the color array Question: what visualization techniques are used in this image? The End