User talk:Andy

From KitwarePublic
Jump to navigationJump to search

VIM is a great editor

Ok, so I use VIM. I used to use Emacs, but I guess the starting time was bothering me. Also, for small sysadmin edits I used VIM already and I was mixing key strokes from Emacs and VIM.

My VIM Settings

set nocompatible " We use a vim
set noautoindent
set tabstop=2       " Tabs are two characters
set shiftwidth=2    " Indents are two charactes too
set expandtab       " Do not use tabs

syntax on           " Turn on syntax hilighting

set hlsearch        " Hilight the search results
set incsearch       " Incrementally search. Like Emacs

set autochdir       " Automatically chdir to directory of the buffer
set matchpairs+=<:>
"set matchpairs+=<:> 
set showmatch
set laststatus=2
set guioptions-=T
set guioptions-=m
set guioptions+=f
"set guifont=Fixed\ 11
"set guifont=Fixed\ 11
set viminfo+=n$HOME/.vim/viminfo
"set guifont=-misc-fixed-*-*-*-*-10-*-*-*-*-*-*-*
"set guifont=Andale\ Mono\ 8

colorscheme darkblue

amenu F&ont.&5x7            :set guifont=5x7<CR><C-L>
amenu F&ont.&6x10           :set guifont=6x10<CR><C-L>
amenu F&ont.6x13            :set guifont=6x13<CR><C-L>
amenu F&ont.&7x13           :set guifont=7x13<CR><C-L>
amenu F&ont.&8x13           :set guifont=8x13<CR><C-L>
amenu F&ont.&9x15           :set guifont=9x15<CR><C-L>
amenu F&ont.&10x20          :set guifont=10x20<CR><C-L>
amenu F&ont.&12x24          :set guifont=12x24<CR><C-L>
amenu F&ont.&heabfix        :set guifont=-*-haebfix-medium-r-normal-*-15-*-*-*-*-*-*-*<CR><C-L>
amenu F&ont.&lucida         :set guifont=-*-lucidatypewriter-medium-r-*-*-14-*-*-*-*-*-*-*<CR><C-L>

if exists("loaded_vimspell")
  set spell_auto_type="tex,mail,text,html,sgml,otl"

set wildmode=longest " Make tabcompletion behave correctly
set selection=exclusive "  Only select up to not including last character
set ignorecase " Ignore case when searching lowercase
set smartcase " Ignore case when searching lowercase

set cinoptions={1s,:0,l1,g0,c0,(0,(s,m1
highlight SpellErrors guibg=Red guifg=Black
:autocmd BufRead,BufNewFile [Mm]akefile :set noexpandtab
highlight SpellErrors  guibg=Red guifg=Black

Random things that need to be put somewhere

Random Python Tricks

How do I get a current line number or file name from the script?

import inspect

f = inspect.currentframe()
print "Line number: %d" % f.f_lineno
print "File name: %s" % f.f_code.co_filename

Also, you can go up the stack and do the same thing:

lineno = inspect.currentframe().f_back.f_lineno

CVS branch control

Note: this script accesses the Entries file directly, it has already pointed out that it should be upgraded to use the cvs status command to be independent of Entries file format changes in the future.


# This variable contains the list of branches to lock - update this list as per
# your requirement

# This variable contains the email-ids of people to bug if the checkin fails
# You would probably want to list the ids of the Release Engineers and the CVS admins

ErrorExit () {
   echo " "
   echo "! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !WARNING ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! "
   echo "! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !COMMIT FAILED ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! "
   echo " "
   echo "${Branch} is closed for commits"
   echo "Please contact $people_to_bug"
   echo " "
   echo "! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !COMMIT FAILED ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! "
   echo " "
   exit 1

# this script would get two argument - full path to the
# directory where you are doing the checkin and the file you are checking in

# This makes $1 point to the file I am checking in

for user in $allow_users
  [ "x$CVS_USER" = "x$user" ] && exit 0

# Grab the last field - that contains the branch name
Branch=`grep "^/$1/" CVS/Entries | awk -F/ '{print $NF}' | cut -c2-`

if [[ ${Branch} != "" ]]
        for locked_branch in $locked_branches
            echo ${Branch} | grep -q $locked_branch && ErrorExit ${Branch}

ITK Filter

The first ITK filter you may want to look at is the UnaryFunctorImageFilter.

It is in


This filter takes one image as input and produces another image as output. The processing is done pixel by pixel, meaning that the pixel at the output is computed only from the corresponding (i,j) pixel in the input.

The filter uses the input and output images types as template parameters, and a third template parameter is a "Functor" class that indicates what operation must be performed in the input pixel in order to compute the output pixel.

The functor is usually written in about 10 lines of code, and with them you can instantiate a specialization of the UnaryFunctorFilter.

An example on how to instantiate this filter is in the Sinus Image Filter, ( a filter that computes

           OutputPixel = sin( InputPixel)

The code is in


In 30 lines of code, it implements the full filter by deriving from an instantiation of the Unary Functor filter.


  Create a pixel wise filter that will set to 0 all the
  pixels of an image with values below 100.


    Do not look at the BinaryThresholdImageFilter.

The second filter you want to look at is the BinaryFunctorImageFilter.

This filter is very similar to the UnaryFunctorImageFilter of Lesson 1, but it accepts two input images and produces one output image. The Functor mechanism is also used here, so a new filter is created by first writing its corresponding Functor that tells how to compute the output pixel value from the pixel values of the two input image pixels.

    OuputValue = Functor(  InputPixelA , InputPixelB )

For an example of an instantiation of this filter you can look at the AddImageFilter

The code is in



   Create a MaskImageFilter using the BinaryFunctorImageFilter


   Do not look at the MaskImageFilter

You can now look a the neighborhood filters. Those are filters that compute the output pixel value based on a neighborhood of the input pixel value.

The value is computed by applying an "Operator" to the input neighborhood. The basic filter for this purpose is the NeighborhoodOperatorImageFilter

This filter is used internally in the DerivativeImageFilter

Note that it is instantiated internally and used for delegating the computation of the output image.

Very close to this filter is the MeanImageFilter

It is a good filter to look at the use of the Neighborhood iterators.


   Write a filter that computes the value of the output pixel
   by voting among the input pixels. The votes are for value 0
   if the input pixel in the neigborhood is < a threshod, and
   the votes will be for 255 if the input pixel in the neighborhood
   is >= (the same threshold).


   Don't look at the BinaryMedianImageFilter

The Insight Consortium declines any reponsability for stimulating your addiction to coffee.

Playing with math

Modify string in a bunch of files

For instance, replace the word windows by linux in all text files:

perl -i.bak -pe 's/windows/linux/g' *.txt

Once you checked that everything is OK, you can remove the .txt.bak files

Note: it is not recursive

perl -i.bak -pe 's/windows/linux/g' `find ./ -type f -name "*.txt"`

To restore the files:

for i in *.php3; do mv $i `basename $i php3`php; done
for i in *.h.bak; do mv $i `basename $i h.bak`h; done

Invoking Java from C++

I need to invoke Java code from C++. Here is a hello world program that does that:

#include <iostream>
#include <jni.h>

int main(int argc, char* argv[])
  JavaVM* jvm;
  JNIEnv* env;
  JavaVMInitArgs args;
  JavaVMOption options[1];

  /* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
  args.version = JNI_VERSION_1_2;
  args.nOptions = 0;
  //options[0].optionString = "-Djava.class.path=...";
  args.options = options;
  //args.ignoreUnrecognized = JNI_FALSE;

  if ( JNI_CreateJavaVM(&jvm, (void **)&env, &args) != 0 )
    std::cerr << "Cannot create VM" << std::endl;

  std::cout << "Have VM: " << (void*)env << std::endl;
  jclass java_lang_System;
  jmethodID mainMethod;
  jobjectArray applicationArgs;
  jstring applicationArg0;

  java_lang_System = env->FindClass("java/lang/System");
  std::cout << "Found class: " << (void*)java_lang_System << std::endl;

  jfieldID outField = env->GetStaticFieldID(java_lang_System, "out", "Ljava/io/PrintStream;");
  std::cout << "Found field: " << (void*)outField << std::endl;

  jobject java_lang_System_out = env->GetStaticObjectField(java_lang_System, outField);
  std::cout << "Found object: " << (void*)java_lang_System_out << std::endl;

  jclass java_io_PrintStream = env->GetObjectClass(java_lang_System_out);
  std::cout << "Found class: " << (void*)java_io_PrintStream << std::endl;

  jmethodID java_io_PrintStream_print = env->GetMethodID(java_io_PrintStream, "println", "([C)V");

  const char* message = "Hello World";
  const size_t len = strlen(message);
  jcharArray array = env->NewCharArray(len);
  jchar *jarray = env->GetCharArrayElements(array,NULL);
  size_t cc;
  for ( cc = 0; cc < len; ++ cc )
    jarray[cc] = message[cc];
  env->CallVoidMethod(java_lang_System_out, java_io_PrintStream_print, array);


  return 0;