#Documentation: Installation of necessary modules: Cython
Added timeit functionality to check the time required for the function to run ###Code:
from timeit import Timer
t = Timer(lambda: dist_pts_ellipse(ellipse,pts))
print t.timeit(number=5)
created a random 1000 points whose distance need to be calculated from the ellipse.
repeating if 5 times
###Drivers control Example dict
{
'display_name': 'Auto Focus',
'unit': 'input_terminal', <- This is either 'input_terminal' or 'processing_unit' which one can be found in cuvc.pxd
,'control_id': uvc.UVC_CT_FOCUS_AUTO_CONTROL , <- you find this in libuvc.h
'bit_mask': 1<<17, <- you find this in cuvc.pxd
'offset': 0 , <-- found in the attached file look at p81-p100
'data_len': 1 , <- called size in the attache file same pages
'buffer_len': 1, <- usually the same as data-len, except when a control has an offset, then a multiple.
'min_val': 0, <- either defined in the attached file. (If the control supports a GET_MIN call this field is 'None')
'max_val': 1, <- same as above, but GET_MAX
'step':1, <- same as above, but GET_RES
'def_val':None, <- same as above but GET_DEF
'd_type': bool, <- either int, bool or a dict with menu entries.
'doc': 'Enable the Auto Focus' <- short description of what the control does.
}
####Doubts The min_val and max_val is not specified in certain cases. What should be done with them in particular? When the offset is a non zero value what should be the order followed for writing d_type?
Timeit Function is used to find the time required for the functions to evaluate the answer
Dist_pts_ellipse | Sample size 200 | Speedup | Sample size 1000 | Speedup |
---|---|---|---|---|
Time for numpy | 0.0010199546814 | 1 | 0.00103092193604 | 1 |
Time for numexpr | 0.0524370670319 | 0.019 | 0.0629789829254 | 0.0163 |
Time for cython | 0.00037693977356 | 2.706 | 0.000943183898926 | 1.093 |
GetAnglesPolyline | Implemented 10 times | Speedup |
---|---|---|
Time for python | 0.000600099563599 | 1 |
Time for cython | 0.00056004524231 | 1.07 |
prune_quick_combine | Implemented 10 times | Speedup |
---|---|---|
Time for python | 0.00080394744873 | 1 |
Time for cython | 0.00055003166198 | 1.46 |
filter | Implemented 1000 times | Speedup |
---|---|---|
Time for ctypes | 0.989124059677 | 1 |
Time for cython | 0.459467887878 | 2.153 |
square_marker_detect | Implemented 100 times | Speedup |
---|---|---|
Time for python code | 3.27636003494 | 1 |
Time for cython code | 2.98566699028 | 1.097 |
Profiling | Prune_quick_combine | Speedup | Ellipse_eval | Speedup |
---|---|---|---|---|
Original | 7.67% | 1 | 6.48% | 1 |
Using prune cython | 6.08% | 1.26 | 5.47% | 1.185 |
Cython ellipse_eval | 5.32% | 1.44 | 4.80% | 1.35 |
=======
####Errors while writing dist_pts_ellipse
- I had given the data type of the returned value of the function in the function definition itself. That throwed an error
- The dot product of pts and M_rot was modified since initially it showed error. Changed the syntax to this :
python pts = pts.dot(M_rot)
- The implementation of the filter function in cython is in the file filter.pyx .
- Changed the arguments of area function to np.ndarray[np.float32_t, ndim=2] img .
- To speed up the function changed the range() to xrange() in the for loops .
- Also used local variables to store values used in the iterative nested for loop instead for accessing the value every iteration.
####Errors
- The dimensions of the input array were not matching when i used the syntax np.float32_t(:,:). Changed the syntax to :
python np.ndarray[np.float32_t, ndim=2] img
- Exception IndexError: 'index 6834 is out of bounds for axis 0 with size 101' in 'filter.area' ignored. The error was due to incorrect way of pointing to an element of an array. For C we considered a single dimensiom array so we needed to multiply the rows with the column size to go to the next rown. In cython however we are directly using 2D arrays so no need for the multiplication
####Cython code
- Using the -a switch to the cython command line program (or following a link from the Sage notebook) results in an HTML report of Cython code interleaved with the generated C code. 2.Lines are colored according to the level of “typedness” – white lines translate to pure C, while lines that require the Python C-API are yellow (darker as they translate to more C-API interaction). 3.Lines that translate to C code have a plus (+) in front and can be clicked to show the generated code.
####Profiling
- Go to command line and type the following commands
python -m cProfile -o profile.pstats main.py
gprof2dot -f pstats profile.pstats | dot -Tpng -o main.png
- A main.png file will be created in your folder.Open it.
- It has a graph of the profiling
- If graph2dot is not installed use the foloowing command:
sudo pip install gprof2dot
###Results of profiling
- The eye0_cpu_time_original.png corresponds to the profiling results of the program as it is.
- The eye0_cpu_time_prune.png corresponds to the profiling results of the program when cython implementation of Pruning quick combine was used.
- The eye0_cpu_time.png corresponds to the profiling results of the program when cython implementation of ellipse_eval was used.