Saturday, 6 September 2014

Install SciPy on Mac OS


Way to many versions of python on Mac OS, but this seems to work.

Intstall homebrew
install gfortran
     brew install gfortran
     or
     brew install gcc
sudo easy_install scipy

References
  1. http://urinieto.com/2012/02/install-python-2-7-x-under-snow-leopard-lion-64-bits/
  2. http://stackoverflow.com/questions/11517164/scipy-numpy-matplotlib-troubles-on-osx
  3. http://brew.sh/
  4. http://docs.scipy.org/doc/numpy/user/install.html


Thursday, 14 August 2014

Spelling and Grammar Checking for Latex

One of the hurdles of starting to use Latex for document creation after getting used to Word is "is there spelling AND grammar checking?" I am happy to say there is a solution to this problem.

There are a few Latex editors already that have support for spell checking but grammar checking is another issue all together. In this post I am going to give a quick guide on a great Latex editor and how you can add grammar checking as well.

TexStudio is becoming a very popular latex editor. It works across many platforms including Windows, Linux and Mac. TexStudio also has built in support for being able to compile and display the document being worked on.

LangaugeTool is a very interesting tool that can be used as stand alone grammar checker. It works as a server on your computer and any system can send HTTP requests to it to ask if there are any grammar issues.

Here are the steps to get everything setup:

  1. Download the version of TexStudio that will work for your operating system.
  2. Install TexStudio
  3. Download LangaugeTool. It uses java so you will need Java available on you computer already.
  4. Unpack Language tool into some directory
  5. In TexStudio Configure TXS first, by using Options > Configure TexStudio… > Grammar like this:
    http://languagetool.wdfiles.com/local--files/checking-la-tex-with-languagetool/texstudio262.png
  6. If TexStudio is configured to use inline grammar checking (which is the default, see Options > Configure TexStudio… > Editor > Inline Checking > Grammar), you should see grammar checks immediately:
  7. After setting this up you should automatically start seeing grammer highlighting in your documents.



As a side note you can easily grab more spelling dictionaries for TexStudio.

Best of Luck.

References
  1. http://wiki.languagetool.org/checking-la-tex-with-languagetool#toc4

Tuesday, 3 September 2013

Generating Latex table of Matlab/Octave Matrix


I created some code in Octave/Matlab to generate latex table from matrix data. I created this because in my case I was tuning some data and algorithms for a paper I was writing and I loathed entering my data into the latex over and over in large tables.

The use just needs to supply a file descriptor, names for the table rows, column, the data matrix and if desired a wrapper to wrap the data entries from the matric in any specific formatting.


Example wrapper functions



function outstr = numWrapper(value)

outstr= strcat("\\num{" , num2str (value), "}");

endfunction
function outstr = ccolWrapper(value)
outstr= strcat("\\ccol{" , num2str (value), "}");
endfunction
 
 


This is the code used to generate the latex
 


%
% creates a latex style table from matrix data.
% wrapper is a function used to wrap the data in each cell
% can be used to apply more formatting in latex
%
% fid is the file pointer to the file to write to
% matrix is the data matrix that will be placed starting at row 1 column 1
% rowNames is a column vector of the string names to be inserted as the names for the rows
% size(rowNames,1) == size(columnNames,1)+1, i.e. the
% columnNames will be used to label each of the column in the data
% wrapper is a special function that will take your input data and wrap it with latex formatting
%
% Will look like
% latex table formatting
% columnNames[0] | columnNames[1] | columnNames[2] | columnNames[2] | ...
% rowNames[0] | matrix(0,0) | matrix(0,1) | matrix(0,2) | ...
% rowNames[1] | matrix(1,0) | matrix(1,1) | matrix(1,2) | ...
% .
% .
% .
%
%
%
%
% latex table formating
function fid = matrixToLatexTable(fid, matrix, rowNames, columnNames, wrapper )
fprintf(fid,'\\documentclass{article}\n');
fprintf(fid,'\\begin{document}\n');
fprintf(fid,' \\begin{tabular}{ |');
for col=1:size(matrix,2)+1
fprintf(fid,'c | ');
end
fprintf(fid,'}\n\\hline\n');
for col=1:size(columnNames,1)
if col==size(columnNames,1)
fprintf(fid,' %s', columnNames(col,:))
else
fprintf(fid,' %s & ', columnNames(col,:))
end
end
fprintf(fid,'\n \\\\ \\hline\n');
% now write the elements of the matrix
for r=1:size(matrix,1)
fprintf(fid,' %s & ', rowNames(r,:))
for c=1:size(matrix,2)
if c==size(matrix,2)
if true
fprintf(fid,'%s ',wrapper(matrix(r,c)));
else
fprintf(fid,'%f ', matrix(r,c));
end
else
if true
fprintf(fid,'%s & ',wrapper(matrix(r,c)));
else
fprintf(fid,'%f & ', matrix(r,c));
end
end
end
fprintf(fid,' \\\\ \\hline \n');
end
fprintf(fid,'\\end{tabular}\n');
fprintf(fid,'\\end{document}\n');
fclose(fid);
endfunction
 
 
Best of luck
- Glen 

Sunday, 19 May 2013

psycopg2 instering an array of polygons

This post is an example of how to insert an array of polygons into the Postgres database using Python and psycopg2.

The most important and problematic part of inserting a polygon array is the string formatting for the polygon[].

The first thing I tried was to just make a string for the exact syntax the Postgresql expects. This was not easy in my case. Probibly mostly because I was reading all of my data from a file and python would wrap qoutes around many of the changes I made in the file.

Next I found that psycopg2 will automatically turn a Python list into the proper syntax for ARRAY[] insertion. This would work great but some of the type checking between Python and psycopg2 lead to an error that the column you are trying to insert into is of type polygon not type text[].

Last I stumbled upon what worked for me. I had to turn the array of polygons into a python list just I could encapsulate the string in double quotes and lastly in the whole thing in single quotes.

Example:

Data:
    This is a list of axis aligned box polygons that I am inserting separated by ';'
((-7,-7),(-7,-5.6),(-5.6,-5.6),(-5.6,-7),(-7,-7));((-7,-2.8),(-7,-1.4),(-5.6,-1.4),(-5.6,-2.8),(-7,-2.8));((-7,-0),(-7,1.4),(-5.6,1.4),(-5.6,-0),(-7,-0));((-7,2.8),(-7,4.2),(-5.6,4.2),(-5.6,2.8),(-7,2.8));((-7,5.6),(-7,7),(-5.6,7),(-5.6,5.6),(-7,5.6));((-5.6,-5.6),(-5.6,-4.2),(-4.2,-4.2),(-4.2,-5.6),(-5.6,-5.6));((-5.6,-4.2),(-5.6,-2.8),(-4.2,-2.8),(-4.2,-4.2),(-5.6,-4.2));((-4.2,-5.6),(-4.2,-4.2),(-2.8,-4.2),(-2.8,-5.6),(-4.2,-5.6));((-4.2,-4.2),(-4.2,-2.8),(-2.8,-2.8),(-2.8,-4.2),(-4.2,-4.2));((-4.2,-2.8),(-4.2,-1.4),(-2.8,-1.4),(-2.8,-2.8),(-4.2,-2.8));((-4.2,4.2),(-4.2,5.6),(-2.8,5.6),(-2.8,4.2),(-4.2,4.2));((-4.2,5.6),(-4.2,7),(-2.8,7),(-2.8,5.6),(-4.2,5.6));((-2.8,5.6),(-2.8,7),(-1.4,7),(-1.4,5.6),(-2.8,5.6));((-1.4,-7),(-1.4,-5.6),(-0,-5.6),(-0,-7),(-1.4,-7));((-0,-7),(-0,-5.6),(1.4,-5.6),(1.4,-7),(-0,-7));((1.4,4.2),(1.4,5.6),(2.8,5.6),(2.8,4.2),(1.4,4.2));((2.8,-7),(2.8,-5.6),(4.2,-5.6),(4.2,-7),(2.8,-7));((4.2,5.6),(4.2,7),(5.6,7),(5.6,5.6),(4.2,5.6));((5.6,-5.6),(5.6,-4.2),(7,-4.2),(7,-5.6),(5.6,-5.6));((5.6,2.8),(5.6,4.2),(7,4.2),(7,2.8),(5.6,2.8))

After reading this from a file into a single string I used the following code to format it to psycopg2's liking.


            obstacles=valuesDict['box_obstacles'].split(";")
            obstacles_string = "{"
            for obstacle in obstacles:
                obstacles_string = obstacles_string + "\"" + str(obstacle) + "\","
               
            obstacles_string = obstacles_string[:-1] + "}"
            self._box_obstacles=obstacles_string

© 2013 Glen Berseth | www.fracturedplane.com

Postgresql: Calculate the number of polygon islands or overlapping groups

This post is about some work I did on how to calculate the number of number of groups of intersecting polygons in an array of polygons in Postgresql


First lets be a little more strict about what the definition of an island is. For the purposes of this algorithm an island consists of a set polygons where at least one polygon in the set overlaps another in the set.


I created a function to calculate the number of islands attached is the plpgsql code for the function.


CREATE OR REPLACE FUNCTION getNumIslands(polys polygon[]) RETURNS INT AS
$BODY$
DECLARE
islandList polygon[]; -- holds the set of obstacles that make up the current island.
islandCount INT; -- holds the count of the number of islands.
poly polygon; -- holds the polygon to start an island search
polys_copy polygon[]; -- A copy of the input list
inddex INT; -- keeps track of the current index in the polys_copy search

BEGIN
    
islandCount := 0;

islandList := polys;
polys_copy := polys;
inddex := 1;

WHILE array_length(polys_copy,1) > 0
LOOP
    -- RAISE NOTICE 'Poly_list length is: %', array_length(polys_copy, 1);
    -- grab the first poly
    poly := polys_copy[1];
    islandList := array_append(ARRAY[]::polygon[], poly);
    -- RAISE NOTICE 'Current island is %', islandList;
    -- remove the first poly because we dont care about it overlaping itself.
    polys_copy := polys_copy[2:array_length(polys_copy, 1)];
    
    -- Make a list of all the polys left that overlap with poly
    WHILE inddex <= array_length(polys_copy, 1)
    LOOP
        -- If the poly_copy overlaps the poly then add its index to the list
        -- of polys to be removed
        FOREACH poly IN ARRAY islandList
        LOOP
            IF polys_copy[inddex] && poly
            THEN
                islandList := array_append(islandList, polys_copy[inddex]);
                --
                IF array_length(polys_copy,1) = 1
                THEN
                    polys_copy := null;
                ELSE
                    polys_copy := polys_copy[1:inddex-1] || polys_copy[inddex+1:array_length(polys_copy, 1)];
                END IF;
                -- RAISE NOTICE 'Intersects with poly index: %', inddex;
                -- When a new intersecting poly is found it needs to be compared
                -- to every poly not just the polys left in the list
                inddex := 0;
                EXIT;
            END IF;
        END LOOP;
        -- RAISE NOTICE 'Current island is %', islandList;
        inddex := inddex + 1;
    END LOOP;
    
    islandCount := islandCount + 1;
    inddex := 1;
END LOOP;

RETURN islandCount;
                
END
                
$BODY$ LANGUAGE plpgsql;
© 2013 Glen Berseth | www.fracturedplane.com

Sunday, 21 April 2013

Install Twiki on Ubuntu 12.04


This is short guide on how to install Twiki ~5.13 on Ubuntu 12.04. This guide has a fix or two that was not present in any other guide I found.

  1. Install all of the relevant dependencies.

    sudo apt-get install apache2 libgdal-perl libcgi-session-perl libhtml-tree-perl liberror-perl libfreezethaw-perl libgd2-xpm rcs libapache2-mod-perl2-doc libapache2-mod-perl2 libapache2-mod-php5 latex2html
  2.  Download Twiki

    If installing in the default directory for Apache (/var/www) place the downloaded zip/tar.gz file in /var/www (http://twiki.org/cgi-bin/view/Codev/DownloadTWiki).
  3. Install Twiki

    cp /var/www/twiki/bin/LocalLib.cfg.txt /srv/www/ducklington.org/twiki/bin/LocalLib.cfg
    chown -R www-data:www-data /var/www/twiki
  4. Configure Twiki
    Go to this link and have the page generate the configuration needed for your apache and perl configuration.
    http://twiki.org/cgi-bin/view/TWiki/ApacheConfigGenerator

    save the generated configuration to /etc/apache2/conf.d/twiki.conf
  5. Restart Apache2
    sudo /etc/init.d/apache2 reload
  6. Allow configuration file to know where the path to the configuration should be put.

    Edit /var/www/twiki/bin/LocalLib.cfg:
    $twikiLibPath = "/var/www/twiki/lib";
  7. Run Twiki config.
    Configure your twiki: go to http://localhost/twiki/bin/configure,  setup admin password and paths.


  8. Edit /var/www/twiki/bin/LocalLib.cfg:
    $twikiLibPath = "/var/www/twiki/lib";

Wednesday, 17 April 2013

Putting a steering algorithm into SteerSuite



This is a guide I created which I hope to turn into a Wiki some day on how to put your steering algorithm in SteerSuite.

  1. First things first
    1. Your agent needs to extend the SteerLib::AgentInterface class so that is can be used properly with the gspacialDatabase
    2. If your algorithm keeps collections of obstacle types or other agents the types should be changed to SteerLib::AgentInterface or other agents and SteerLib::BoxObstacle
    3. Next you will need to start converting geometries
      1. If you are lucky the algorithm that is being ported to SteerSuite will differentiate between Points and Vecotors otherwise you will need to go through every operation and check if it produces a Point or Vectory because SteerSuite differentiate between them.
    4. The *AIModule should not include the Agent type
      1. This is to avoid circular includes
      2. Not should the agent include the module type only the interfaces provided from SteerSuite
    5. Try not to change any of the math in the algorithm
      1. Best is to alter the initial conditions to fit the algorithm best and then convert the reusult computed by the algorithm back into SteerSuite coordinates.