Tradutor

domingo, 24 de janeiro de 2016

How to parallelize loops

In image processing, we frequently apply the same algorithm on a large batch of images.
In this paragraph, we propose to use joblib to parallelize loops. Here is an example of
such repetitive tasks:

from skimage import data, color, util
from skimage.restoration import denoise_tv_chambolle
from skimage.feature import hog

def task(image):
    """
    Apply some functions and return an image.
    """
    image = denoise_tv_chambolle(image[0][0], weight=0.1, multichannel=True)
    fd, hog_image = hog(color.rgb2gray(image), orientations=8,
                        pixels_per_cell=(16, 16), cells_per_block=(1, 1),
                        visualise=True)
    return hog_image


# Prepare images
hubble = data.hubble_deep_field()
width = 10
pics = util.view_as_windows(hubble, (width, hubble.shape[1], hubble.shape[2]), step=width)

To call the function task on each element of the list pics, it is usual to write a for loop.
To measure the execution time of this loop, you can use ipython and measure the execution time
with %timeit.

def classic_loop():
    for image in pics:
        task(image)

%timeit classic_loop()

Another equivalent way to code this loop is to use a comprehension list which has the same efficiency.

def comprehension_loop():
    [task(image) for image in pics]

%timeit comprehension_loop()

joblib is a library providing an easy way to parallelize for loops once we have a comprehension list.
The number of jobs can be specified.

from joblib import Parallel, delayed

def joblib_loop():
    Parallel(n_jobs=4)(delayed(task)(i) for i in pics)

%timeit joblib_loop()

quinta-feira, 21 de janeiro de 2016

# Numpy Hints

" usr/bin/env python"
"-*- coding:utf-8 -*-"
# This simple code shows how to create an
 array and an image with theses arrays

# importing numpy module as np
import numpy as np

#from matplotlib module importing pyplot submodule as plt
from matplotlib import pyplot as plt

# creating an array using np reference
one_array = np.array([1,15])

# creating images
image0 = np.linspace(0, 27, 10000).reshape(100,100)

# atributting data-type uint8, commonly used in image processing 
image1 = np.linspace(0, 255, 10000).reshape(100,100).astype(np.uint8)

# showing the data types, min and max
print("Image0:", image0.dtype, image0.min() , image0.max())
print("Image1:", image1.dtype, image1.min() , image1.max())

# creating a figure object
fig,  (ax0, ax1) = plt.subplots(1,2)

# add image0 to axes 0
ax0.imshow(imagem0, cmap='gray')

# add image1 to axes 1 
ax1.imshow(image1, cmap='winter')

# shows images
plt.show()

domingo, 20 de setembro de 2015

Python programming language for science: Scipy, Numpy and Matplotlib

Python


Python is a high-level programming language very used in many areas of computer science. I'm writing  this blog for myself, and to improve my skills in English, as well as to make public the advantages on usage of Python for science.  So, in this blog i will show to you some "tutorials" which i found searching on Web. This tutorials are, initially, just for Debian-based systems like Ubuntu 14.04 LTS. 

Comparison 



Unlike MATLAB and others commercial programming languages and numerical computing enviroment, Python is totally free. 

In MATLAB, the standard library does'nt contain as much generic programming functionality, but does include matrix algebra and an extensive library for data processing and plotting, this can be solved buying $$ the toolkits for MATLAB. In academia many many professors are using MATLAB, and encourage your students to get the "student license" (very limited), and therefore they end up opting to use a crack in order to get all toolboxes. Beyond this, MATLAB have some problems with portabillity and many others that you can see in [1]

In Python, exist three basics(but strongest) libraries for science, scipy, numpy and matplotlib, all these libraries have subpackages, but due Python be an open source all packages and subpackages are written with support python community, however this is not a big problem since Python have a numerous documentation, bloggers, tutorials on youtube and more, you can learn code with python.


Features of Python



  • Free.
  • Beautiful programming language;
  • Powerful;
  • Namespaces; 
  • Introspection;
  • String manipulation;
  • Portability; 
  • Class and function definitions. 

See you in next post... 


Darleison Rodrigues
Graduating in Energy Engineering
Communication Coordinator at CAENE
University of International Integration of Lusofonia Afro-Brazilian


--

References

[1] http://www.pyzo.org/python_vs_matlab.html