Friday, 5 January 2018

Visualizing Mandelbrot Set in Oracle BI using D3

Target Audience

Oracle BI Professionals - You can learn building new visualizations using JavaScript library.
Other BI Professionals - You may want to try this out in your BI tool (Cognos, PowerBI, Tableau, QlikView, etc.)
Others 1 - Those who want to learn a mathematical concept.
Others 2 - Those who love intricate / artistic patterns. You can just download the file and enjoy the beautiful patterns.
Others 3 - Those who want to test the processing power of their computer.

Background

For a long time, I was thinking about bringing together mathematics (my favorite subject since childhood) and Oracle BI (my passion and skillset). While browsing the internet for various math topics, I came across Mandelbrot Set which comes under the family of fractals. The thing that amazed me was that a small mathematical formula can result in very visually appealing patterns. In this blog, we will see what is a Mandelbrot Set and how to create Oracle BI report to visualize the set.

What is Mandelbrot Set?

It is a set of numbers "c" in the complex plane for which the absolute value of the quadratic function remains bounded when iterated from "z = 0". Below is the function -

zn+1 = zn2 + c

It appears very difficult to understand but actually it's quite simple when you know the terminology used. Below image breaks down the definition-

Image 1  -   Terminologies in the Mandelbrot Set definition


When we plot the points in Mandelbrot Set on the complex plane, the resulting image is an intricate pattern which repeats itself over-and-over again when we zoom in. The points in the green color belong to the Mandelbrot Set and points in other colors have become un-bounded after certain range of iterations.

Image 2  -   Mandelbrot Set diagram


Preparing the Dataset

To visualize Mandelbrot Set in Oracle BI, we need to have the dataset. So let’s see the detail steps and calculations involved in identifying the points in Mandelbrot Set. The first step is to generate a grid of points and we are specifically interested in the points that lie in the range -2 and 2 on the real axis and -2i and 2i on the imaginary axis. This is because it is known that any number outside this grid will definitely break one of the criteria (bounded) and the absolute value will go to infinity when iterated for the quadratic equation.

Image 3  -   Complex Planes with different density


It is fairly easy to generate the grid of points by nesting two loops in a PL-SQL procedure. As each point on the grid represents a complex number, there are two associated values – real part and imaginary part. For example, the complex number -1.3 + 0.7i has the real part as -1.3 and imaginary part as 0.7. As we have the grid of points, we can proceed with the calculations. The formula has 3 complex numbers – output number, input number and constant number. Let’s represent these numbers –

  Complex Number   Real Part   Imaginary Part
  Output Number, zn+1   R3   I3
  Input Number, zn   R2   I2
  Constant / Grid Point, c   R1   I1

zn+1 = zn2 + c      
R3 + I3 * i = (R2 + I2 * i)2 + (R1 + I1 * i)      .....   Expressing in terms of Real and Imaginary parts
R3 + I3 * i = (R22 + 2 * R2 * I2 * i + I22 * i2) + (R1 + I1 * i)      .....   Expanding square expression
R3 + I3 * i = (R22 + 2 * R2 * I2 * i - I22) + (R1 + I1 * i)      .....   Replacing i2 by -1
R3 + I3 * i = (R22 - I22 + R1) + (2 * R2 * I2 + I1) * i      .....   Re-arranging the parts

This gives us the formula for real and imaginary parts of the output number as –
R3 = R22 - I22 + R1
I3 = 2 * R2 * I2 + I1
Absolute Value = √(R32 + I32)

Let’s try out two complex numbers on the grid and see if they qualify to be part of the Mandelbrot Set.

z1 = z02 + c      →      z2 = z12 + c      →      z3 = z22 + c      →      z4 = z32 + c      →      z5 = z42 + c

  Grid Point (c) = -1.996 + 0.01 * i     Grid Point (c) = 0.5 - 0.85 * i
  Seq   Complex Number   Abs. Value     Seq   Complex Number   Abs. Value
  z0   0 + 0 * i   0     z0   0 + 0 * i   0
  z1   - 1.996 + 0.01 * i   1.99602505     z1   0.5 – 0.85 * i   0.986154146
  z2   1.987916 – 0.02992 * i   1.988141149     z2   0.0275 – 1.7 * i   1.700222412
  z3   1.954914817 – 0.108956893 * i   1.957948811     z3   - 2.38924375 - 0.9435 * i   2.568789199
  z4   1.813820336 – 0.416002891 * i   1.860914457     z4   No need for further calculations as value greater than 2, hence unbounded.
  z5   1.120885805 – 1.499109006 * i   1.871820718     z5

And the winner after 5 iterations → -1.996 + 0.01 * i
Here we have stopped after doing 5 iterations, but this complex number may not remain bounded if we do more iterations. Here is another challenge for you -

Challenge #2 - How many iterations can you do with your computer?

Let’s insert this information in the database table.
  REAL_PART   IMG_PART   BOUNDED   ITERATIONS
  - 1.996   0.01   YES   5
  0.5   - 0.85   NO   3

Modelling in Oracle BI

In the Oracle BI Repository,
Physical Layer      →      Import the table from database and create two aliases - dimension and fact. A join can be created on any column or even dummy column (1=1) to complete the formality of building Star Schema.
Logical Layer      →      Next, move this model to logical layer. No need to create hierarchies or metrics here.
Presentation Layer      →      Bring only one table to this layer - dimension or fact, anyone is fine.

In the Oracle BI Answers,
For visualizing this data set, we will use Scatter Plot. The points will be displayed in different colors based on the number of iterations. For example, points with 5 iterations can be displayed in green color, 4 iterations as blue, 3 iterations as yellow and so on.

Image 4  -   Mandelbrot Set diagram with standard graph in Oracle BI


This is how it looks with standard chart option in Oracle BI, but it does not look similar to actual diagram. We need something that will give us complete control on the formatting like point size, color, axis, interaction, etc. There are lot of JavaScript libraries available as open-source that can be integrated with Oracle BI and be utilized for advanced or very interactive visualizations. Here, we will use D3 (Data Driven Documents). The pre-requisite for this implementation is basic understanding of HTML, CSS and Javascript, this would help in writing and modifying the code to suit our visualization requirements.

The integration of D3 library requires following actions –
1. Download the library (https://d3js.org/).
     →  It is just one file with .js extension. The file size is less than 1 MB.
     →  Please note the version, there are syntax differences while coding.
2. Deploy the downloaded library to the Oracle BI Server.
     →  It can be copied to Managed Server path (tmp\_WL_user\analytics_11.1.1\7dezjl\war\res \b_mozilla).
     →  It can also be "deployed as an application" from the Admin Console.

Now we are all set to start visualizing Mandelbrot Set as an Oracle BI report using D3 libraries, we need to use Narrative View for this purpose. The process involves referencing D3 library (prefix section), binding data (narrative section) and manipulating the dataset (postfix section). The first two parts are pretty easy, however the last part involves writing lot of javascript code for drawing x-y axes, plotting points, scaling, coloring, etc.

Prefix Section
     →  Reference the D3 library using <script> tag.
     →  Declare an empty array. Example - mbset = [];

Narrative Section
     →  Bind the data to array using push method.
     →  Example - mbset.push({BOUNDED:@1, REAL:@2, IMG:@3});
     →  Please note @1, @2 and @3 are column sequence in the Criteria tab of the report.

Postfix Section
     →  Define the axes using axisBottom() and axisLeft() functions.
     →  To utilize more screen space, scale-up the axes using scaleLinear() function.
     →  To plot and format(color, size and position) grid points, use SVG elements.

Image 5  -   JavaScript Coding in the Narrative View


It is now time to see the final result -

Image 6  -   Mandelbrot Set diagram with D3 Visualization in Oracle BI


About The Challenges

I have tried different combinations of grid density and iterations and below are the performances on my personal laptop -

  Grid Density   Unit Step   Iterations   Execution Time
  400 x 400   0.0100   40   13.34 seconds
  800 x 800   0.0050   40   62.744 seconds
  800 x 800   0.0050   80   76.22 seconds
  2000 x 2000   0.0020   200   708.73 seconds
  4000 x 4000   0.0010   100   1994.74 seconds
  8000 x 8000   0.0005   100   8711.18 seconds

If you want to test performance / limits of your computer, you can go for denser grid like 100k x 100k, 1 million x 1 million, 2 million x 2 million, etc. These challenges may also help you out to check efficiency of PL-SQL code. There is absolutely no limitation to how far you can go with respect to grid density or iterations, you can search and check out YouTube videos to understand this.

Download

At the below link, there is a zip file which contains -
→     HTML file with JavaScript code
→     CSV file with Mandelbrot Set data
→     Close-up images of different parts of Mandelbrot Set

      Download Here    →  

The HTML file can be modified to see how different aspects of the code work, you can play with the chart area by modifying iteration ranges or color codes.

Note : I have tested this in Firefox 57.0.4 and it works fine. However, there could be browser or version specific issues. Also, it takes a while to render the image in the browser.