Friday 24 August 2012

Setting up Git for writing papers

One more technical post before getting on with more research stuff next week. Writing papers in LaTeX is made far easier with some kind of versioning system. Now I don't have any particular favorites and if there is some kind of institutional system set up then you should probably go with that to save some headaches. However, there are often delays with getting new people from outside the institution into the system so there is some incentive to run your own.

I recommend gitolite especially if you are on some version of linux. All you need is a box which people can access via ssh. The cool part is you don't need to give people an account on your machine you just get them to send you their public keys.

First make an account -  call it git_admin but you can name it anything you wish. As root

>># adduser git_admin
>># passwd git_admin

download and install gitolite, if you are on a major distribution (fedora, ubuntu, etc.) there will be a package

Next generate a public-private key for git_admin -
>> su - git_admin
>> ssh-keygen
>> cp ~/.ssh/id_rsa.pub /tmp/git_admin.pub

make sure to name the file username.pub

login to gitolite - again as root
>># su - gitolite
>>gl-setup /tmp/git_admin.pub

got back to the git-admin account and do
>>git clone gitolite@localhost:git-admin

if you want the admin files on a different machine replace localhost with the appropriate ip address/name

Now to add a repo go to git-admin/conf/gitolite.conf

add the line:

repo repository_name
         RW+    =     username

to add a user simply add the public key file to the keydir directory, do a git add, commit, and push and there you've got a personal git repository.

PS - if you are new to git that means

>> git add username.pub
>> git commit -a -m"some informative message"
>> git push

if git complains about you not having an identity, run
>> git config user.name "my name"
>> git config user.email "some@email.here"

both of these should be set to something.

and finally, once you clone an empty repository make some dummy file such as a readme, then to do
>> git add readme
>> git commit -a -m"some msg"
>> git push origin master


All this information is on the internet already but not in one place...so hopefully it helps.





Monday 20 August 2012

First post - Getting java classes to work in Matlab

First, a little background:

A problem in research is not only the diversity of tools (also lack of benchmarks, etc.) used but also programming languages used. I think this is especially the case in applied and computational topology. There is C++, java, python, matlab, and a few others I am sure - but lets just stick with those...


Now there are a number for great packages for doing computational/appiled topology...assuming they do exactly what you want - thats not to say they are not extensible, rather they are optimized for certain things (like getting work done) - and anyone who has played around with a Rips complex can appreciate this - efficient code is crucial.

On the other side - this makes playing around with new concepts kind of difficult. Brings us to the problem: I need to compute persistent cohomology and need cocycles - and I want access to this in MATLAB. There is code to do this, but not quite the way I want it...so I coded it up in MATLAB...but there is a nasty loop which makes things slow...then in C++, thinking I can compile into a MEX file. Couldnt get the appropriately old compiler to work....ok...well I will just use java....

A caveat....my strongest language is C and C++, then python/java/whatever..my IDE is emacs or much more rarely vim (I suppose for this stuff i should just use eclipse)

I write everything, compile, read the documentation...import into matlab and nothing happens...non-descript error...

I wasnt that far off but here are a few things to help you save a few hours -  and one last thing, I discovered


http://undocumentedmatlab.com/

it is great for random things which are not in the documentation.



1.) Import in MATLAB will look like it worked regardless of whether it did, you will just see an error when you access a class.


2.) In MATLAB do java -version

this will likely be some old version...you could try to update it, it may work, I have no idea. The easier way to do it is when you compile do

javac -target 1.x -source 1.x nameof.java

in my case i used 1.6. java will complain about missing bootstrap files, but it should compile


3.) Use packages. If you are relatively new to java, put all your java in one directory and lets call it MyFiles. then at the top of each file put

package MyFiles;

then you compile ad one directory lower do

jar cvf MyFiles.jar MyFiles/


4.) Actual importing - you need to add the path with the jar file using

javaaddpath('path')

in MATLAB. Here I suggest you use the full path name rather than relative path name, since there may be some subtle wierdness in MATLAB. Then continuing the example from above -

import MyFiles.*;

Now you should have access to your classes as

R = MyFiles.MyClass();


4.) Returning data from java methods: For me the whole point is that I can go back and forth between java and matlab. Here I suggest you make dedicated methods in your java class which return int[][] or double[][]. Then if need be have a second matlab script which does something like

cell2mat(cell(output))

Note there a re faster ways to do this, but this I think is the simplest.

For the output methods...if it is some dynamic structure. I generally make a Vector or something like that then turn it into a static array and copy it over into my int or double array. This is not very efficient but I have limited passing back and forth so its ok...


A full example:
I have a directory testClass/
inside i make the following myClass.java



package testClass;

public class myClass
{
  public double value;

  public myClass()
  {
      value = 0;
  }

  public double add_modp(double v,int p)
  {
      value=(v+value)%p;
      System.out.println("adding mod "+p);
      return (value+v;
  }
}



compile

javac -target 1.6 -source 1.6 testClass/myClass.java


compress

jar cvf testClass.jar testClass/


in Matlab

javaaddpath('/fullpath/testClass.jar');
import testClass.*

now we can call

t = testClass.myClass();
t.add_modp(4,3)

and so on...so there output isnt a problem but I will write up my (inelegant) solution at some point.