Example of post processing plugin with template update

From Dynamo
Revision as of 11:55, 5 October 2016 by Daniel Castaño (talk | contribs)
Jump to navigation Jump to search

Here we show a piece of code that illustrates how to create a simple post processing plugin. In this case, we show an example that operates a minimum of functionality of a plugin: operating on the average produced by one iteration, allowing Dynamo to use a transform of the average as template for the next iteration. This particular example implements a transform consisting in applying a C2 transfom along all x,y,z axis of the template.

The plugin is callable through Matlab execution of the project.


Plugin code

A plugin function is a script called dynamo_plugin_post_<Some identification>. For this example you can just choose any folder in your file system, say ~/myPlugins and edit there a script file called dynamo_plugin_post_example.m.

You can for instance write in the Matlab prompt

 edit ~/myPlugins/dynamo_plugin_post_example.m

and paste to the matlab command

%
% Simple example of post processing plugin.
% Just applies a symmetry operation on the attained volume. 
%
% The input is the cardfile generated by Dynamo
%
function dynamo_plugin_post_example(cardFile);

% just to ensure
disp('Hello! the plugin is being executed!');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 1: identification of the pipeline stage
%

% reads the cardfile
scard=dynamo_read(cardFile);

% Now the plugin locates:
%   1 from which iteration it is being executed:
ite = scard.database_ite;

%   2 from which project it is being invoked
name_project = scard.name_project;

% We read the corresponding virtual project
vpr = dynamo_vpr_load(name_project);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 2: operation
%

%  reads the average that has been set to be used
average = dynamo_database_read('average',vpr,'ite',ite);

% creates a new average

% in this particular example the transformation consists in a particular symmetrization procedure
transformedAverage  = dynamo_sym(average,'c2');
transformedAverage  = drot(dynamo_sym(drot(transformedAverage,[90,90,0]),'c2'), [0,-90,-90]);
transformedAverage  = drot(dynamo_sym(drot(transformedAverage,[0,90,0]),'c2'), [0,-90,0]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 3: Write results back into the project
%

% determines the location where we need to write the file in order 
% to integrate it inside 
targetFile = dynamo_database_locate('starting_reference',vpr,'ite',ite+1);
dynamo_write(transformedAverage,targetFile);

Activating the plugin inside a project

After writing the script, you need to make your project aware of the fact that during runtime a project will be invoked:

dvput ptest plugin_post_r1 1

This activates the plugin for all the iterations inside round 1 (_r1).

Then, you need to pass the syntax of the plugin script. We don't pass the dynamo_plugin_post part.

dvput ptest plugin_post_order_r1 'example();'

Note that the (); part informs Dynamo that the plugin is a Matlab script.

Running a project that calls a plugin

After binding the plugin into the project, you need to make certain that Matlab will find it in runtime while executing the project. To this end, you need to add the path to the plugin script into the Matlab execution path:

addpath('~/myPlugins');