The following images show regular 5x5 grids with a nearest neighbor, ring or complete topology:
I recently found out that drawing these kind of images is easily possible using Graphviz and Python. A small script that automates creation of these images with arbitrary number of rows and columns can be downloaded here. The script creates a .svg file, which can be conveniently postprocessed with Inkscape. Usage of the script is as follows:
python ./graphviz_grid.py <num_rows> <num_cols> <ring|nn|compl> <outfilename.svg>

This blog is supposed to be a collection of random, unrelated, little ideas, thoughts, and discoveries, which I assume to be helpful to a negligible part of the world's population and wish to share out of pure altruism. If posts appear really weird, maybe you have the wrong kind of humor. Many of the posts are science/technology related. If you are opposed to that, stop reading here! Comments, criticism, corrections, amendments, questions are always welcome.
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
2015-02-23
2013-06-13
Octave vs Python
"Don't do loops in Octave." is a well known truth. But sometimes loops are just too handy or cannot be avoided at all. I was curious whether there is a difference in execution time of loops in Python and Octave since both are interpreted languages.
user@machine:~/scripts/python_vs_octave$ octave loops.m
...
39.48
...
user@machine:~/scripts/python_vs_octave$ python loops.py
3.10390710831
Even in this simple example, the Octave script takes more than 10 times as long as the equivalent Python script. The difference becomes even bigger, if the amount of computation inside the loops is increased. Since Python also comes with sophisticated matrix processing capabilities (NumPy, SciPy) and if severe performance degradation for more sophisticated numerical analysis is not acceptable, the proverb above can be simply shortened to "Don't do Octave."
PS.: Another popular data analysis software is R. On the same machine using R version 2.14.1, the execution time of the equivalent script was 18.35s, which is in between the execution time of the Python and Octave scripts.
The following two scripts do nothing, but executing 3 nested loops with 100 iterations each and doing some useless computation within the loops. One script is in Python and one is in Octave. The Octave script is also listed above. Octave version 3.6.4 and Python version 2.7.3 was used for the comparison. The results are devastating.tic; for i=1:100 for j=1:100 for k=1:100 vain1 = i^2+j^2+k^2; vain2 = i^2+j^2+k^2; ... vain10 = i^2+j^2+k^2; endfor endfor endfor t = toc; disp(num2str(t));
user@machine:~/scripts/python_vs_octave$ octave loops.m
...
39.48
...
user@machine:~/scripts/python_vs_octave$ python loops.py
3.10390710831
Even in this simple example, the Octave script takes more than 10 times as long as the equivalent Python script. The difference becomes even bigger, if the amount of computation inside the loops is increased. Since Python also comes with sophisticated matrix processing capabilities (NumPy, SciPy) and if severe performance degradation for more sophisticated numerical analysis is not acceptable, the proverb above can be simply shortened to "Don't do Octave."
PS.: Another popular data analysis software is R. On the same machine using R version 2.14.1, the execution time of the equivalent script was 18.35s, which is in between the execution time of the Python and Octave scripts.
2013-01-27
Typesetting Word-By-Word Translations
Recently I was asked how to typeset documents with a word-by-word translation like the following:
As I have learned, linguists have the fancy word Interlinear Gloss for this. There are several Latex packages available for this purpose. Among them is gb4e which I decided to use.
For simplicity it is assumed that the text to be 'glossed' is provided as a plain text file with sentences delimited by '. ', '? ' or '! ' (2 spaces) and words separated by individual spaces. The implementation of a small script that creates a document with nicely aligned words is very straight forward. The dictionary needs to be provided as a .csv file.
Unfortunately the task can not be fully automated. Breaking text into sentences requires some knowledge about a specific language. So does breaking sentences into words. Ideally the dictionary should also have some capability to detect flections, etc. The script just generates a Latex file that can be modified manually.
The script can be downloaded here.
Welche | Farbe | hat | der | gelbe | Bus? |
---|---|---|---|---|---|
Which | color | has | the | yellow | bus? |
As I have learned, linguists have the fancy word Interlinear Gloss for this. There are several Latex packages available for this purpose. Among them is gb4e which I decided to use.
For simplicity it is assumed that the text to be 'glossed' is provided as a plain text file with sentences delimited by '. ', '? ' or '! ' (2 spaces) and words separated by individual spaces. The implementation of a small script that creates a document with nicely aligned words is very straight forward. The dictionary needs to be provided as a .csv file.
Unfortunately the task can not be fully automated. Breaking text into sentences requires some knowledge about a specific language. So does breaking sentences into words. Ideally the dictionary should also have some capability to detect flections, etc. The script just generates a Latex file that can be modified manually.
The script can be downloaded here.
Labels:
interlinear gloss,
latex,
Python,
word-by-word translation
2012-10-24
OpenCV Background Subtraction in Python
Recently, I tried finding an example of Background Subtraction being done in OpenCV and Python without success. The code is not complicated or special in any way. However, I guess, the example might be helpful, if you want to get started with Background Subtraction quickly.
The script reads a video file and writes out a mask for every frame.
Unfortunately there seem to be no Python bindings for BackgroundSubtractorMOG2 at the time of this writing.
The script reads a video file and writes out a mask for every frame.
#!/usr/bin/env python #-*- encoding: utf-8 -*- import cv2 import numpy bgs = cv2.BackgroundSubtractorMOG(24*60, 1, 0.9, 0.01) capture = cv2.VideoCapture("00185.MTS.mp4") cv2.namedWindow("input") a = 0 while(True): f, img = capture.read() fgmask = bgs.apply(img) #cv2.imshow("input", fgmask) #cv2.waitKey(1) cv2.imwrite("./pngs/image-"+str(a).zfill(5)+".png", fgmask) a = a + 1 print(a)
Unfortunately there seem to be no Python bindings for BackgroundSubtractorMOG2 at the time of this writing.
2012-07-09
Creating Timelapse Videos with a Webcam
Last week I had some fun creating timelapse videos using just an inexpensive webcam and a laptop. Timelapse videos are a nice way to visualize processes which are otherwise to slow to observe directly (eg. the movement of clouds or the growth of plants). Technically the the solution to all the world's problems seem to be little Python scripts, which can be
downloaded here. ;)
For simplicity a lot of things are hardcoded in these scripts, so you most certainly have to make changes. The scripts should be easy to understand, as they are quite short and simple. However if anything is unclear, just drop me a comment.
For simplicity a lot of things are hardcoded in these scripts, so you most certainly have to make changes. The scripts should be easy to understand, as they are quite short and simple. However if anything is unclear, just drop me a comment.
timelapse.py, timelapse_copy.py
timelapse.py uses ffmpeg for capturing images from the webcam (support for v4l assumed). You do not need this script, if you have any other means of acquiring the individual frames. The filenames of the acquired images contain a consecutive nr. and the time and date when the image was taken. Unfortunately it turned out later that times and dates in the filename confuse ffmpeg and have to stripped away to join the individual frames to a single video file. Besides the nr. needs to have leading zeros. The timelapse_copy.py script was made to compensate for that.timelapse_filter.py
As mentioned before, an inexpensive webcam was used for simplicity. Therefore a Gimp plugin is used to sharpen and denoise the pictures a little. The script uses Gimp and the package gimp-plugin-registry (for the wavelet denoise). You can skip this step if you have a good camera or are not willing to wait, because the filtering can take quite some time, depending on which filtering operations you want to run on the individual frames. Do not forget to copy this script to ~/.gimp-2.6/plug-ins and to make it executable.timelapse_ffmpeg.py
A third script creates a video from the filtered images again using ffmpeg.2011-08-23
Mnemosyne Print Flash Cards on Dead Wood
Do you know Mnemosyne? It's an excellent tool for learning flash cards. The only problem is that the computer is an evil distraction machine. So you might decide one day, that you want you print your flashcards to learn them the old school way, which means double sided printing questions and answers out on paper.
Unfortunately Mnemosyne does not have the ability to print flash cards with the question on the front and the answer on the back in double sided print. However flash cards can be exported to xml which can be converted to printable html with the following script. The Python script is a bit shaky and you might have to adapt it for your own needs, but it did the job for me.
Unfortunately Mnemosyne does not have the ability to print flash cards with the question on the front and the answer on the back in double sided print. However flash cards can be exported to xml which can be converted to printable html with the following script. The Python script is a bit shaky and you might have to adapt it for your own needs, but it did the job for me.
2011-08-20
Python Constant Madness
Somehow Python does and does not have constants. The following examples, which can be tried out on the interactive interpreter, are of not much practical use, but interesting however:
Even the built-in constants True and False can be overwritten with arbitrary content. This flexibility is probably rarely required and if this happens by accident, the logic of the following program is most likely screwed.
Type "help", "copyright", "credits" or "license" for more information. >>> temp_true = True >>> temp_false = False >>> True = temp_false >>> False = temp_true >>> True False >>> False True
Even the built-in constants True and False can be overwritten with arbitrary content. This flexibility is probably rarely required and if this happens by accident, the logic of the following program is most likely screwed.
>>> True = "hello world" >>> True 'hello world'Ruby does have a little advantage here, since it does not allow assigning new values to true and false.
Subscribe to:
Posts (Atom)