Difference between revisions of "Slicer3:Slicer Daemon"

From NAMIC Wiki
Jump to: navigation, search
m (Text replacement - "http://www.slicer.org/slicerWiki/index.php/" to "https://www.slicer.org/wiki/")
 
(38 intermediate revisions by 3 users not shown)
Line 1: Line 1:
−
= Goals and Functionality =
+
<big>'''Note:''' We are migrating this content to the slicer.org domain - <font color="orange">The newer page is [https://www.slicer.org/wiki/Slicer3:Slicer_Daemon  here]</font></big>
−
 
 
−
The Slicer Daemon refers to a network protocol that can be used to connect to a running instance of slicer to read and write data in the MRML scene and execute other commands.  The name is based on the unix system convention of naming network services '[http://en.wikipedia.org/wiki/Daemon_%28computer_software%29 daemons]'.
 
−
 
 
−
= Server Implementation =
 
−
 
 
−
The file
 
−
[http://www.na-mic.org/ViewVC/index.cgi/trunk/Modules/SlicerDaemon/Tcl/slicerd.tcl?view=log slicerd.tcl] implements the server side functionality.
 
−
 
 
−
By default it listens for connections on port 18943.
 
−
 
 
−
= Clients =
 
−
 
 
−
== Tcl ==
 
−
 
 
−
Two utilities are provided:
 
−
 
 
−
* [http://www.na-mic.org/ViewVC/index.cgi/trunk/Modules/SlicerDaemon/Tcl/slicerget.tcl?view=log slicerget.tcl] is used to read volumes out of slicer.  The volume is written to the stdout of the slicerget command in nrrd format.
 
−
 
 
−
* [http://www.na-mic.org/ViewVC/index.cgi/trunk/Modules/SlicerDaemon/Tcl/slicerput.tcl?view=log slicerput.tcl] is used to write volumes into slicer.  The volume is read in nrrd format from stdin of slicerput and loaded into the mrml scene.
 
−
 
 
−
Some sample commands (assumes your PATH is correctly set to include unu, slicerget and slicerput):
 
−
 
 
−
# a noop -- just copy image onto itself
 
−
slicerget.tcl 1 | slicerput.tcl noop
 
−
 
 
−
# put external data into slicer
 
−
unu 1op abs -i d:/data/bunny-small.nrrd | slicerput.tcl
 
−
 
 
−
# run an external command and put the data back into slicer
 
−
slicerget.tcl 1 | unu 1op abs -i - slicerput.tcl abs
 
−
 
 
−
== Python ==
 
−
 
 
−
A [http://www.na-mic.org/ViewVC/index.cgi/trunk/Modules/SlicerDaemon/Python/ Python based set of code] for interacting with the Slicer Daemon is provided.
 
−
 
 
−
For example, the [http://www.na-mic.org/ViewVC/index.cgi/trunk/Modules/SlicerDaemon/Python/mathExample.py?view=log following code] reads a volume and creates a new volume where each voxel is the square of the corresponding voxel of the input image.  The new image is then sent back to slicer.
 
−
 
 
−
<pre>
 
−
import slicerd
 
−
import numpy
 
−
 
 
−
s = slicerd.slicerd()
 
−
 
 
−
n = s.get(0)
 
−
 
 
−
im = n.getImage()
 
−
n.setImage( im * im )
 
−
 
 
−
s.put(n, 'newImage')
 
−
</pre>
 
−
 
 
−
For example, the [http://www.na-mic.org/ViewVC/index.cgi/trunk/Modules/SlicerDaemon/Python/sliceExample.py?view=log following code] reads a volume and extracts a slice of it for plotting using the matplotlib code (see [http://www.scipy.org the SciPy website] for more info on Python numerics and plotting).
 
−
 
 
−
<pre>
 
−
import slicerd
 
−
import pylab
 
−
 
 
−
s = slicerd.slicerd()
 
−
 
 
−
n = s.get(0)
 
−
 
 
−
slice = n.getImage()[16,:,:]
 
−
pylab.imshow(slice)
 
−
pylab.show()
 
−
</pre>
 
−
 
 
−
== Matlab ==
 
−
 
 
−
[http://www.na-mic.org/ViewVC/index.cgi/trunk/Modules/SlicerDaemon/matlab/ Matlab based] versions of Slicer Daemon client code is also under development.
 
−
 
 
−
<pre>
 
−
function volume = getSlicerVolume( id_or_name )
 
−
 
 
−
% This is an example script that shows how to establish a reading pipe to a
 
−
% running slicer daemon (start Slicer3 with option --daemon).
 
−
% These steps have to be done to adapt the script to your environment:
 
−
% - matlab extentions "popenr" and "popenw" have to be compiled for your
 
−
%  machine: cd into $SLICER_HOME/Modules/SlicerDaemon/matlab/popen , and
 
−
%  do "mex popenr.c" and "mex popenw.c" in matlab.
 
−
% - make sure you add the path to popen
 
−
% - make sure to add the path to the matlab scripts in
 
−
%  $SLICER_HOME/Modules/SlicerDaemon/Tcl
 
−
%
 
−
% returns a struct with image data from slicer volume with id "id" or
 
−
% name "name" and image information according to the nrrd format.
 
−
 
 
−
% add path for popen
 
−
cpath = pwd;
 
−
cd('popen');
 
−
pName = pwd;
 
−
addpath (pName);
 
−
 
 
−
% find slicerget.tcl script
 
−
cd( cpath );
 
−
cd('../Tcl');
 
−
pScript = pwd;
 
−
cd( cpath );
 
−
 
 
−
if (isa(id_or_name,'numeric'))
 
−
  % fprintf('The id is: %d.\n',id_or_name);
 
−
    cmd_r = sprintf('%s/slicerget.tcl %d',pScript, id_or_name);
 
−
elseif (isa(id_or_name,'char'))
 
−
  % fprintf('The name is: %s.\n',id_or_name);
 
−
    cmd_r = sprintf('%s/slicerget.tcl %s',pScript, id_or_name);
 
−
else
 
−
    fprintf('Usage: getSlicerVolume(id) or getSlicerVolume(name).\n');
 
−
    exit;
 
−
end
 
−
 
 
−
p_r = popenr(cmd_r);
 
−
if p_r < 0
 
−
    error(['Error running popenr(',cmd_r,')']);
 
−
end
 
−
 
 
−
volume = preadNrrd(p_r);
 
−
 
 
−
% close pipe
 
−
popenr(p_r,-1)
 
−
</pre>
 

Latest revision as of 17:35, 10 July 2017

Home < Slicer3:Slicer Daemon

Note: We are migrating this content to the slicer.org domain - The newer page is here