Dec 27, 2011

xrange with float steps - Python

The great thing about this code, is that the definition its a generator. (You can easily identify the keyword yield)

def drange(start, stop, step):
    r = start
    while r < stop:
        yield r
        r += step

>>>i0=drange(0.0, 1.0, 0.1)
>>>["%g" % x for x in i0]
['0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1']
>>>

source

Dec 14, 2011

Region Of Interest Ascii - IDL

An IDL little snippet to read Regions of Interest from an ascii file

function load_roi, file_name
    ; Open file in read-only mode.
    openr, fd, file_name, /GET_LUN
    
    ; Read total pixel from 1 ROI.
    size = 0
    readf, fd, size
    
    ; Read the indices of the pixels in the ROI
    result = intarr(size)
    readf, fd, result
    
    ; Close the file.
    close, fd
    
    return, result
end

Coded by Sil!

Dec 1, 2011

Timeit function

Here i present a little function (with an example below) to time some bunch of code using timeit.

# -*- coding: utf-8 *-*
import timeit


class Chronoit():
    """
    A little class to handle timeit output.
    The output is in seconds.
    """
    def __init__(self, main_statement, setup_statement, total_iterations):
        self.main_stmt = main_statement
        self.setup_stmt = setup_statement
        self.total_iter = total_iterations

        t = timeit.Timer(self.main_stmt, self.setup_stmt)
        self.show_results(t.timeit(number=self.total_iter))

    def show_results(self, time_elapsed):
        time_per_pass = time_elapsed / self.total_iter
        if time_per_pass > 60.0:
            minutes_per_pass = time_per_pass / 60.0
            secs_per_pass = time_per_pass % 60.0
            print "#" * 50
            print "Timing"
            print "setup stmt: \"" + self.setup_stmt.split("\n")[1] + " [...]\""
            print "main stmt: \"" + self.main_stmt.split("\n")[1] + " [...]\""

            print "\t total time: %d secs" % time_elapsed
            print "\t iterations: %d " % self.total_iter
            print '%.2f mins/pass %.2f secs/pass' % \
                      (minutes_per_pass, secs_per_pass)
        else:
            secs_per_pass = time_per_pass
            print "_" * 50
            print "Timing"
            print "setup stmt: \"" + self.setup_stmt.split("\n")[1] + " [...]\""
            print "main stmt: \"" + self.main_stmt.split("\n")[1] + " [...]\""

            print "\t total time: %d secs " % time_elapsed
            print "\t iterations: %d " % self.total_iter
            print '\t %.2f secs/pass' % secs_per_pass

        print "_" * 50

if __name__ == "__main__":
    MAX_ITERATIONS = 10
    N, M = 100, 1000
################################################################################

setup_stmt = \
"""
import numpy
matrix = numpy.random.randint(0,100,(%d * %d))
#""" % (N, M)

main_stmt = \
"""
for i in xrange(%d * %d):
    pixel = matrix[i]
    pixel = pixel * 20
    matrix[i] = pixel
#""" % (N, M)
t_obj = Chronoit(main_stmt, setup_stmt, MAX_ITERATIONS)

My intention also is to extend this idea making a class of it, and do some profilling.


Modem Zyxel p-660R-T1 v3s Arnet

  • User: admin
  • Password: alvlgeddl