Batchmake tutorial

From KitwarePublic
Revision as of 20:08, 6 February 2009 by Finetjul (talk | contribs) (Add Testing tutorial)
Jump to navigationJump to search

< Main BatchMake

Tutorial: Getting Started with BatchMake

Introduction

This section explains the purpose of the BatchMake tool, and describes the screen layout. This section is organized as a series of questions and answers about the general capabilities and structure of the BatchMake tool.

Running an application with BatchMake

For begining, we ar going to do a very simple example. It consists to list all files of a directory. First, you have to indicate what is the directory that you want to list. You can do it with the cammand 'Set'. The first argument is the name of the variable,and the second is the path of your directory.

Set(directory 'C:\Matthieu\BatchMake\Examples')

After that, BatchMake knows your directory, and so, it can list all files.

ListFileInDir(Files ${directory})

The last thing to do, is to print the names of the files. It is easy with the commad 'echo' You can print the list which is "direcory", or you can print each file.

foreach(file ${Files})
echo(${file})
endforeach(file)
echo(${Files})

If you want to print each file, you need to do a loop with the instruction : 'foreach...endeach'

Complete Example : Converter

Converter.cxx

It consists to run an application which convert a tif image into an mha one.

First you have to use MetaCommad class in order to receive parameters from the commad line. If you don't know how to use MetaCommand, you can find some help at the end of this tutorial.

We have this code :

MetaCommand command;
command.SetName("Converter");
command.SetVersion("1.0");
command.SetAuthor("Kitware Inc");
command.SetDescription("Convert tif->mha");
command.AddField("InputImage","InputImage",MetaCommand::STRING);
command.AddField("OutputImage","OutputImage",MetaCommand::STRING);
if(!command.Parse(argc,argv))
{
return 1;
}

std::string InputImage = command.GetValueAsString("InputImage"); 
std::string OutputImage = command.GetValueAsString("OutputImage"); 

reader->SetFileName(InputImage);
writer->SetFileName(OutputImage);

writer->SetInput(reader->GetOutput());
writer->UseCompressionOn();
writer->Update();

return 1;
}

At the beginning of our code, we fill insome fields like the name, the version etc... The most important fields to add, are the input image field and the output image one. The first parameter is the name of the field, the second its "tag", and the last one, its type.

Therefore, our application have one input (InputImage), and one output (OutputImage) which will be the two arguments of the program.

Now, we can run our program with BatchMake.


Converter.bms

Before beginning to write your BatchMake code, we need to specify a new application in the wraper

Wrapper3.JPG

Create a new application with the button "New" :

Wrapper.JPG


Use the button "Browse and Run" to select your application (Converter.exe), and you will see some information about it, like metacommand filelds.

Wrapper1.JPG

At the bottom of the image, you can see the Command line of the application. The two <string> indicate you have to specify two arguments to run Converter => the input image and the ouput one. Now you can click "OK"

Wrapper2.JPG

"Converter" application is now known by BatchMake.


Here is the code of our BatchMake file :

SetApp(converter @Converter)       #"converter" is the variable which defines the application

Wrapper4.JPG

When you type @, BatchMake shows you all the applications it knows.

Set(outputdir 'C:/Example/TifImages')       #Here, we define the output directory where new images will be stored
MakeDirectory(${outputdir})                 #If the output directory doesn't exist, it will be created
Set(inputdir 'C:/Example/MhaImages')      #Here, we define the intput directory where tiff images are
ListFileInDir(files ${inputdir})          #Then we list all files which are in that directory
foreach(i $(files))        #For each file of the directory inputdir

Set(inputimage ${inputdir}/${i})                      #We create a new variable which contains the inputimage file.
SetAppOption(converter.InputImage ${inputimage})      #We sepcify what is the input image for the first field

GetFilename(j ${i} NAME_WITHOUT_EXTENSION)
Set(outputimage ${outputdir}/${j}.mha)                 #We create a new variable which contains the outputimage file.
SetAppOption(converter.OutputImage ${outputimage})     #And the output image for the second field of Metacommand

echo(${converter})          #We can see the line command with echo
Run(output ${converter})    #Running the application...

endforeach(image)

Tutorial: Sending Data to the central DataBase

Sending results:

Firstly, we are going to send the name of our inputimage and our ouputimage.

To send data to the central DataBase of BatchMake, you have to register you on the website :

http://insight-journal.org/batchmake/index.php


When it's done, log you on the DataBase, go to "My Profile", and click on the button "Generate Key" and remember this one.

Now, you can add this code at the begining of your script :

DashboardHost(http://www.insight-journal.org/batchmake)
DashboardUser('Your_FirstName Your_LastName')
DashboardKey('Your_Key')
#The name of your project must be created by a super-admin of BatchMake : Ask him if you want to create a new project
CreateExperiment(exp 'Your project name' 'Title of your new experiment' 'This is a description for this experiment')
# We add a method 
CreateMethod(ConvertMeth exp 'Converter' 'Convert a mha image into a mha compress')


The following cammand should be add after that the variables are seted :

# We add two inputs that will be displayed in the DataBase
AddMethodInput(InputImage ConvertMeth 'InputImage Name') #InputImage is the same variable that we used before.
AddMethodInput(InputImage ConvertMeth 'OutputImage Name') #OutputImage is the same variable that we used before.
# All the path will be displayed, but if you prefre display only the imagename => replace InputImage and OutputImage by i.

The entries of the methods are now created, you can send your new method. with the command :

DashboardSend(ConvertMeth)

Dash01.jpg

The Dashboard has another option that is very interisting : You can display an image in the DataBase.

The only condition is that your image is a png.

But maybe, you would like to display a slice from a 3D image (like a mha) : you can do this too with BatchMake. You just have to write the following command :

 Set(InputLiver 'C:/Matthieu/Tests/Liver.mha')
 #We take the first slice after the middle of the image (2 = axial axe)
 ExtractSlice('${InputLiver}' '${InputLiver}1.png' 2 1 FROM_MIDDLE) 

Liver.bms

The following lines will display diferent slices of the liver image in the database.

Set(InputLiver 'C:/Matthieu/Tests/Liver.mha')
sequence(seq 0 20 5)
foreach(x ${seq})
ExtractSlice('${InputLiver}' '${InputLiver}${x}.png' 2 ${x} FROM_MIDDLE) 
SetIdealOutput(slice '${InputLiver}${x}.png')
AddMethodInput(x ConvertMeth 'Slice #')
AddMethodIdealOutput(slice ConvertMeth 'Slice' png)
DashboardSend(ConvertMeth)
endforeach()

Dashboard02.JPG

Tutorial: Using BatchMake inside a C++ application

BatchMake is a standalone application. It can be run as a command line but also has a front-end GUI. But BatchMake is also a collection of C++ classes that can be used directly inside your C++ applications. This tutorial shows how run a script from your application and how to access the values of your script variable. You first have to include the BatchMake parser. Make sure you link the BatchMake libraries with your project.

#include "bmScriptParser.h"

Then you should instantiate a ScriptParser object

bm::ScriptParser m_Parser;

Optionally set the wrapped application directory or/and the application path

m_Parser.LoadWrappedApplication( "c:/my_apps/" );
m_Parser.SetBatchMakeBinaryPath( "c:/my_apps/" ); 

And finally compile and run the script. Execute() returns true if it successfully compiled and run with no errors, false otherwise.

m_Parser.Execute( "my_script.bms" );

After the script is executed, you can retrieve the values of your script variables.

std::vector<MString> value = m_Parser.GetScriptActionManager()->GetVariable( "my_variable" );

Tutorial: Add a test for CTest

First you have to create some test .bms files. Then you need an executable that takes your .bms files as argument and process them. This is done in Testing/Code/bmActionsTest.cxx. In your testing directory, open your CMakeLists.txt file and add the lines:

SET(BATCHMAKE_TESTS ${CXX_TEST_PATH}/BatchMakeTests)
SET(TEST_DATA ${WhereYourBMSFilesAre} )
ADD_TEST(MyActionsTest   ${BATCHMAKE_TESTS} ${WhereActionTestIs}/ActionsTest ${TEST_DATA}/MyActionsTest.bms)
ADD_TEST(MyActionsTest2  ${BATCHMAKE_TESTS} ${WhereActionTestIs}/ActionsTest ${TEST_DATA}/MyActionsTest2.bms)
...

You can give a look to the Testing/Code/CMakeLists.txt file; some BatchMake tests are there.