In [1]:
# Math libraries
import numpy as np
import math
from math import sqrt

# Matplotlib plotting libraries
import matplotlib.pyplot as plt
%matplotlib inline

# Bokeh plotting libraries
import bokeh.plotting as blt
blt.output_notebook()


BokehJS successfully loaded.

In [2]:
class JuliaSet(object):
    def __init__(self, c, n = 100):
        self.c = c
        self.n = n
        self._d = .001
        self.set = []
    
    def juliamap(self, z):
        return z ** 2 + self.c
    
    def iterate(self, z):
        for m in np.arange(1, self.n+1):
            z = self.juliamap(z)
            if abs(z) > 2:
                return m
        return 0
    
    
    def setcomplexplane(self, _d):
        x, y = -2, -2
        self._complexplane = []
        while (x <= 2):
            y=-2
            while(y<=2):
                z = complex(x,y)
                self._complexplane.append(z)
                y = self._d + y
            x = self._d + x
        
    def set_spacing(self, d):
        self._d = d
        self.setcomplexplane(d)
        
    
    def generate(self):
        self.set = []
        for i in self._complexplane:
            self.set.append(self.iterate(i))
        return self.set

In [3]:
class JuliaSetPlot(JuliaSet):
    """Extend JuliaSet to add plotting functionality"""
    
    def __init__(self, *args, **kwargs):
        # Invoke constructor for JuliaSet first, unaltered
        super(JuliaSetPlot, self).__init__(*args, **kwargs)
        # Add one more attribute: a rendered image array
        self.img = np.array([])
    
    def get_dim(self):
        # get what should be an attribute
        return int(math.sqrt(self.img.size))
    
    def render(self):
        if not self.set: self.generate()
        # Convert inefficient list to efficient numpy array
        self.img = np.array(self.set)
        dim = int(math.sqrt(self.img.size))
        # Reshape array into a 2d complex plane
        self.img = np.reshape(self.img, (dim,dim)).T
        
    def show(self):
        if not self.img.size: self.render()
        # Specify complex plane axes efficiently
        xy = np.linspace(-2,2,self.get_dim())
        # Use matplotlib to plot image as an efficient mesh
        plt.figure(1, figsize=(12,9))
        plt.pcolormesh(xy,xy,self.img, cmap=plt.cm.hot)
        plt.colorbar()
        plt.show()
        
    def interact(self):
        from matplotlib.colors import rgb2hex
        if not self.img.size: self.render()
        # Mimic matplotlib "hot" color palette
        colormap = plt.cm.get_cmap("hot")
        bokehpalette = [rgb2hex(m) for m in colormap(np.arange(colormap.N))]
        # Use bokeh to plot an interactive image
        f = blt.figure(x_range=[-2,2], y_range=[-2,2], plot_width=600, plot_height=600)
        f.image(image=[j.img], x=[-2,2], y=[-2,2], dw=[4], dh=[4], palette=bokehpalette)
        blt.show(f)

In [4]:
j = JuliaSetPlot(-1.037 + 0.17j)
%time j.set_spacing(0.006)
%time j.show()


CPU times: user 312 ms, sys: 8 ms, total: 320 ms
Wall time: 330 ms
CPU times: user 6.12 s, sys: 212 ms, total: 6.34 s
Wall time: 11 s

In [5]:
j = JuliaSetPlot(-0.624 + 0.435j)
%time j.set_spacing(0.006)
%time j.interact()


ERROR:/projects/sage/sage-6.9/local/lib/python2.7/site-packages/bokeh/validation/check.pyc:E-1000 (COLUMN_LENGTHS): ColumnDataSource column lengths are not all the same: ColumnDataSource, ViewModel:ColumnDataSource, ref _id: e582ca47-81b6-42bc-abb0-e857a092f948
CPU times: user 344 ms, sys: 44 ms, total: 388 ms
Wall time: 570 ms
CPU times: user 5.91 s, sys: 120 ms, total: 6.03 s
Wall time: 8.84 s

In [50]:
%prun j.generate()


 

In [51]:
%load_ext line_profiler
%lprun -f j.generate j.generate()


The line_profiler extension is already loaded. To reload it, use:
  %reload_ext line_profiler

In [ ]: