2010-03-25

Mathematica Run and spaces

On Windows I have been trying to interface to another program.  This is due to the PlayerPro not supporting the .NET interface so you can’t do COM object manipulation.  So I am putting any of that code into an exe built by  Autoit and then want Mathematica to call it with variable parameters.

However  I have had real trouble using the Run[] command to run a progam which has a command and a parameter with a filename with spaces in it.

Run[“\”F:\\A dir\\test.exe\" “\”F:\\A dir\\data\"”]

so my work around is to put the command in a batch file Run.bat and then run:

Run[“\”F:\\A dir\\run.bat\"”]

Which is irritating but works.

I used the following to build the command file:

batchRun[workingDir_, command_] := Module[{fn, str},
  fn = workingDir <> "Run.bat";
  If[FileExistsQ[fn], DeleteFile[fn]];
  str = OpenWrite[fn];
  WriteString[str, command];
  Close[str];
  Run["\"" <> fn <> "\""]
  ]

2010-03-23

Mathematica Player Pro deployment process

Starting with a working notebook and assuming that you are working in workbench.  The aim is then to first convert this into very simple notebook with all the gui code in a gui package.  Then to combine all these elements into an installable exe file using InnoSetup for deployment on a machine with PlayerPro installed or just copy them across to your destination.

1) Convert the Gui cells to initialisation cells.  This then saves the code to .m package.

2) Supposing you want to keep the template separate then Rename the saved notebook to something else eg _template.  Rename the saved .m file as smethingGUI.m and the template.

3)Edit somethingGUI.m into a package file with a single function runGUI[]. 

a) Whereas before you might have explicit dependencies in the code eg Needs{moduleX`]  this can now be moved into the begin package dependency list:

BeginPackage["somethng`",  {"moduleX`"}]

b) Any references to global variables will now need to be prefixed with Global` now that they are embedded in a private part of the package.

4) Create a new notebook with the single cell and interface

If[Fold[Or, False,
   StringMatchQ[$Path, ___ ~~ "MathematicaPlayerPro" ~~ ___]],
  Needs[# <> "`", NotebookDirectory[] <> # <> ".mx"] &,
  Needs[# <> "`"] &] /@ {"moduleX”, 
  "somethingGUI"}; somethingGUI`runGUI[]

5) Open this notebook with the Mathematica front end  and run it.  It should work, eg display front end and then allow you to interact with the front end.

6) Edit the Gui code for the button and modify  like this

Dynamic@Button[Style["Run report",FontFamily->"Helvetica"],
      (

If[Fold[Or, False,
   StringMatchQ[$Path, ___ ~~ "MathematicaPlayerPro" ~~ ___]],
  Needs[# <> "`", NotebookDirectory[] <> # <> ".mx"] &,
  Needs[# <> "`"] &] /@ {"moduleX”, 
  "somethingGUI"};

        something`createReport[thisDate];
      ),Enabled->fileExists
    ]

(The fileExists is a dynamic variable that depends on a file test depdning on what the user puts in.  If the file doesn’t exsit the buton is not enabled) (This is a bit of kludge but seems to work.  When you save a result it sees to save partial but not all linked files so then using a Needs in the button procedure loads the path but not quite quickly enough so you need to use complete references.  you need to open the file in the front end and execute the code then make code option not open and then save the notebook.  You now get a blank notebook that opens and you can press buttons. on it.

7)  Build a build and deploy module that actually compresses the files and then deploys them to the test machine.

8) When it runs I still have the run dynamic content and run the initialistation bxes to click. but then it does start up

The next stage is to put this all together as a set of sample files.

2010-03-09

Mathematica Time Series

I have been working on a general data type for the manipulation of time series data.  It still needs some work on it although I have used it for odd jobs.  The time series based on vectors is much faster than the general lists.

you can download the Dates.m file here

There is also a simple test framework which I will rewrite in the future now that I am familiar with the mUnit test framework in Workbench which I like a lot.

This is loosely based on a sample from  Copyright 2005, Kim C. Border Package 0.8.  He has allowed me to release the derived work.

2010-03-08

Mathematica testing notebook reports

I am just coming to grips with creating notebooks (reports) in the kernel and for testing the report.  When testing with MUnit there is no front end so it seems that all the Notebook manipulation commands such as:

    nb = CreateDocument[]

    nb = NotebookPut[]

don’t work.  So my work around is to test my own notebook.  This can be turned into an actual workbook easily on a FrontEnd eg:

In[3]:= nb = Notebook[{}]

Out[3]= Notebook[{}]

In[6]:= AppendNotebook[nb_, expr_] :=
Notebook[Append[nb[[1]], expr]]; nb =
AppendNotebook[nb, Cell["Hello", "Subsection"]]

Out[6]= Notebook[{Cell["Hello", "Subsection"]}]

In[8]:= NotebookPut[nb]

Then you can add a grid box – I have adapted a version here which produces a box even if all the tables are not quite the right length as I would rather have a grid when the data is wonky than not:

writeNotebookGrid[nb_, body_, headerList_] :=
Module[{headings, itemsBody, cols, useBody, result},
  result = AppendNotebook[nb, Cell["Table", "Subsection"]];
  If[Depth[body] < 3, useBody = {{"Body parameter not deep enough"}},
   useBody = body];
  cols = Max[Length[headerList], Length[#] & /@ useBody];
  headings =
   Map[StyleBox[#, FontFamily -> "Times", FontWeight -> "Bold",
      FontColor -> RGBColor[0.5, 0, 0.3]] &, {PadRight[headerList,
      cols, ""]}, {2}];
  itemsBody = Join[headings, PadRight[#, cols, ""] & /@ useBody];
  result =
   AppendNotebook[result,
    Cell[BoxData[
      GridBox[itemsBody, GridFrame -> True, RowLines -> True,
       ColumnLines -> True]], "Text"]];
  Return[result]
  ]

nb = writeNotebookGrid[nb, {{1, 2}, {2, 3}}, {A, B, C}]

Which gives you a little table in your report like this:

Table

Adding graphs to your plot is done like this:

ab = AppendNotebook[nb,
  Cell[BoxData[ToBoxes[Plot[Tan[x], {x, 0, \[Pi]}]], ""],
   "Text"]]; NotebookPut[ab]

The syntax is a bit fidly but will build you a notebook like this:

GraphAndTable

The great thing is that once you have these primitives you can quickly iterate through large data sets and produce detailed reports.  Especially if you spend some time on the graphs with annotations and labels.  Mathematica 7 has improved legends over Mathematica 6.

2010-03-05

Mathematica debugger line by line

I find that it seems that the Mathematica debugger on the workbench gets confused about stepping line  by line (F6 Step over) once you have stepped into (F5 Step Into) another function.  My work around is to set another breakpoint in the second function you are debugging.

2010-03-04

Mathematica package hell

I accidentally created a circular dependency with a Package A Needs Package B which Needs Package C which needs Package A.

Having done that Package A couldn’t access Package B routines even though it correctly need them.  The full form did work eg PackageB`function[].

It is something to be careful of but there is no particular help from the compiler or workbench.  It should be possible to write a package dependency tree program quite easily.