Batchmake tutorial

From KitwarePublic
Jump to navigationJump to search

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.

How Do I Get and Run MatchBake Under Microsoft Windows?

On the Microsoft Windows platform, BatchMake can be easily installed like any other program, and launched from the Microsoft Windows Start menu. The BatchMake sources (or the self-installer) are available for download at http://batchmake.org/download.htm


What Exactly Does BatchMaker Do?

BatchMake is a cross platform tool for batch processing of large amount of data.

For more informations about it, you can visit the BatchMake website at http://batchmake.org/index.htm


What Additional Documentation is Available?

Other documentation is available at http://batchmake.org/documentation.htm


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)

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


dicomPhilips2meta - Conversion tool from dicom Philips to metaImage

   * Windows: Media:DicomPhilips2meta.zip (compiled using MSVC6 on Win2K)
   * Linux: Media:DicomPhilips2meta.tgz (compiled using gcc 3.2 on RH9) 

Usage is:

dicomPhilips2meta -t <PhilipsDicom> <outputmeta.mha/mhd>

MetaCommand Output:

To use MetaCommand you have to include the file metaCommand.h in your code.

#include <metaCommand.h>

Then you need to declare a MetaOutput object, for example:

MetaCommand output;

And you should be put this code, before the parsing :

 output.SetMetaCommand(&command);
 BatchMakeStream bmstream(&command);

Then you could add a field to see a variable in BatchMake. In that example , x is an int variable, and we need to see its value in BatchMake:

 int x = 10;
 output.AddFloatField("Result","Computation Result",x); //Result will be the filed of output
 output.Write();

Now, you can access to the variable "x" via BatchMake with the command line :

Run(output ${example})
Set(result example.Result) 
Echo(x = ${result})

But before that, you need to add this command line at the beginning of your script :

SetAppOption(example.GenerateXMLMetaOutput 1)