itk-snap distributed segmentation service documentation
TRANSCRIPT
ITK-SNAP Distributed SegmentationService Documentation
Release 100
Paul Yushkevich
Dec 20 2021
Contents
1 DSS Userrsquos Quick Start Guide 111 Prerequisites 112 DSS Overview 113 Running DSS from ITK-SNAP 214 Using DSS from the Command Line 7
2 DSS Service Developerrsquos Quick Start Guide 921 Prerequisites 922 DSS Architecture Overview 923 Running DSS locally 1024 Writing Your Own Service 12
3 ITK-SNAP DSS Reference Manual 2931 servicejson Files 2932 DSS REST API 30
4 What is ITK-SNAP DSS 35
i
ii
CHAPTER 1
DSS Userrsquos Quick Start Guide
This quick start guide describes how to use the new Distributed Segmentation Services (DSS) functionality in ITK-SNAP 38 or later
11 Prerequisites
bull ITK-SNAP 38 or later
bull A Google account (for authentication)
bull Familiarity with the command line (for the second part of tutorial)
12 DSS Overview
DSS allows you to send image datasets directly from ITK-SNAP to external ldquoservice providersrdquo who apply advancedimage processing algorithms to your data For example some segmentation algorithms require a Linux cluster ora high-end graphics card (GPU) With DSS you can use these algorithms simply by making a few mouse clicks inITK-SNAP
When you use DSS you communicate with a web-based application called the ldquomiddleware serverrdquo The main mid-dleware server for ITK-SNAP is httpsdssitksnaporg (you can visit this link and see the status of available services)Other local servers may also be set up in the future by various organizations The DSS middleware server distributesyour image datasets to service providers who use their advanced computer hardware to perform processing on yourdata
1
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning At the present DSS is intended for processing of small batches of datasets None of the providers havethe capacity to process hundreds or thousands of datasets for individual users In the future we envision extendingthis system to use cloud-based computing resources with the ability to pass the computational costs to end-users
13 Running DSS from ITK-SNAP
In this tutorial we will segment hippocampal subfields in a multi-modality MRI dataset of the human brain Segmen-tation will be perfomed using the ASHS algorithm
131 Download and Open Sample Workspace
A zip file containing an ITK-SNAP workspace with a high-resolution T2-weighted MRI scan of the hippocampalregion and a whole-brain T1-weighted MRI scan can be obtained from the ITK-SNAP download page Or simply usethe direct link below to download
bull httpswwwnitrcorgfrsdownloadphp10983ashs_testzip
Decomress the file ashs_testzip and double-click the workspace file ashs_testitksnap This shouldlaunch ITK-SNAP and load a pair of MRI scans as shown below
2 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
In this workspace a high-resolution T2-weighted is loaded as the ldquomain imagerdquo and a whole-brain T1-weightedMRI as an additional secondary image The ASHS algorithm will segment the subfields of the hippocampus andparahippocampal gyrus using a multi-atlas algorithm as described in (Yushkevich et al 2015) These structures playimportant roles in creation of memories and are affected early in Alzheimerrsquos disease
Fig 1 A close-up view of the hippocampus and parahippocampal gyrus from the sample dataset
132 Connect to DSS and Register
From the main ITK-SNAP menu select Tools-gtDistributed Segmentation Service This opens the ldquoDSS dialogrdquowhich we will use throughout this tutorial The dialog has three tabs ldquoConnectrdquo ldquoSubmitrdquo and ldquoResultsrdquo
13 Running DSS from ITK-SNAP 3
ITK-SNAP Distributed Segmentation Service Documentation Release 100
To connect to the DSS system press the Get Token button This will launch a web browser that will take you to thepage below
Press the Sign in with Google button and follow the prompts You will be asked to accept the terms of use after whichyou will get to a page shown below This page contains a single-use login credential called a ldquotokenrdquo
Copy and paste the 40-character token into the ldquoLogin Tokenrdquo field in ITK-SNAP The server status should change toldquoConnectedrdquo momentarily
133 Submit a Ticket to the DSS
The DSS uses the concept of tickets to track image processing tasks When you send a dataset to DSS a new ticketis created and assigned a unique number As the dataset is being processed you can track the status of your ticket oryou can request the processing to be cancelled Tickets are autonomous and you can close ITK-SNAP and open it
4 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
later (even on another computer) to access the results for your ticket You can submit another ticket without waitingfor your first ticket to finish processing
Go to the submit page in the DSS dialog The drop-down box called ldquoServicesrdquo contains a list of all services currentlyavailable on the DSS system
For this tutorial select the service ASHS-PMC 100 The submit dialog will look like this
The main part of the dialog consists of a table of tags Tags are labels assigned to different images in the workspacethat help the service provider identify which image is which (eg which is the T2-weighted MRI and which is theT1-weighted MRI) For this tutorial dataset the tags should already correctly match to the images as shown above soyou do not need to do any tag assignment
Note In the future we will extend the tag system to allow landmarks and manual segmentations to be tagged so thatyou can provide additional input and hints to algorithms that need them For now only images can be tagged
Simply press the Submit button The ticket will begin to upload
13 Running DSS from ITK-SNAP 5
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
Contents
1 DSS Userrsquos Quick Start Guide 111 Prerequisites 112 DSS Overview 113 Running DSS from ITK-SNAP 214 Using DSS from the Command Line 7
2 DSS Service Developerrsquos Quick Start Guide 921 Prerequisites 922 DSS Architecture Overview 923 Running DSS locally 1024 Writing Your Own Service 12
3 ITK-SNAP DSS Reference Manual 2931 servicejson Files 2932 DSS REST API 30
4 What is ITK-SNAP DSS 35
i
ii
CHAPTER 1
DSS Userrsquos Quick Start Guide
This quick start guide describes how to use the new Distributed Segmentation Services (DSS) functionality in ITK-SNAP 38 or later
11 Prerequisites
bull ITK-SNAP 38 or later
bull A Google account (for authentication)
bull Familiarity with the command line (for the second part of tutorial)
12 DSS Overview
DSS allows you to send image datasets directly from ITK-SNAP to external ldquoservice providersrdquo who apply advancedimage processing algorithms to your data For example some segmentation algorithms require a Linux cluster ora high-end graphics card (GPU) With DSS you can use these algorithms simply by making a few mouse clicks inITK-SNAP
When you use DSS you communicate with a web-based application called the ldquomiddleware serverrdquo The main mid-dleware server for ITK-SNAP is httpsdssitksnaporg (you can visit this link and see the status of available services)Other local servers may also be set up in the future by various organizations The DSS middleware server distributesyour image datasets to service providers who use their advanced computer hardware to perform processing on yourdata
1
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning At the present DSS is intended for processing of small batches of datasets None of the providers havethe capacity to process hundreds or thousands of datasets for individual users In the future we envision extendingthis system to use cloud-based computing resources with the ability to pass the computational costs to end-users
13 Running DSS from ITK-SNAP
In this tutorial we will segment hippocampal subfields in a multi-modality MRI dataset of the human brain Segmen-tation will be perfomed using the ASHS algorithm
131 Download and Open Sample Workspace
A zip file containing an ITK-SNAP workspace with a high-resolution T2-weighted MRI scan of the hippocampalregion and a whole-brain T1-weighted MRI scan can be obtained from the ITK-SNAP download page Or simply usethe direct link below to download
bull httpswwwnitrcorgfrsdownloadphp10983ashs_testzip
Decomress the file ashs_testzip and double-click the workspace file ashs_testitksnap This shouldlaunch ITK-SNAP and load a pair of MRI scans as shown below
2 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
In this workspace a high-resolution T2-weighted is loaded as the ldquomain imagerdquo and a whole-brain T1-weightedMRI as an additional secondary image The ASHS algorithm will segment the subfields of the hippocampus andparahippocampal gyrus using a multi-atlas algorithm as described in (Yushkevich et al 2015) These structures playimportant roles in creation of memories and are affected early in Alzheimerrsquos disease
Fig 1 A close-up view of the hippocampus and parahippocampal gyrus from the sample dataset
132 Connect to DSS and Register
From the main ITK-SNAP menu select Tools-gtDistributed Segmentation Service This opens the ldquoDSS dialogrdquowhich we will use throughout this tutorial The dialog has three tabs ldquoConnectrdquo ldquoSubmitrdquo and ldquoResultsrdquo
13 Running DSS from ITK-SNAP 3
ITK-SNAP Distributed Segmentation Service Documentation Release 100
To connect to the DSS system press the Get Token button This will launch a web browser that will take you to thepage below
Press the Sign in with Google button and follow the prompts You will be asked to accept the terms of use after whichyou will get to a page shown below This page contains a single-use login credential called a ldquotokenrdquo
Copy and paste the 40-character token into the ldquoLogin Tokenrdquo field in ITK-SNAP The server status should change toldquoConnectedrdquo momentarily
133 Submit a Ticket to the DSS
The DSS uses the concept of tickets to track image processing tasks When you send a dataset to DSS a new ticketis created and assigned a unique number As the dataset is being processed you can track the status of your ticket oryou can request the processing to be cancelled Tickets are autonomous and you can close ITK-SNAP and open it
4 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
later (even on another computer) to access the results for your ticket You can submit another ticket without waitingfor your first ticket to finish processing
Go to the submit page in the DSS dialog The drop-down box called ldquoServicesrdquo contains a list of all services currentlyavailable on the DSS system
For this tutorial select the service ASHS-PMC 100 The submit dialog will look like this
The main part of the dialog consists of a table of tags Tags are labels assigned to different images in the workspacethat help the service provider identify which image is which (eg which is the T2-weighted MRI and which is theT1-weighted MRI) For this tutorial dataset the tags should already correctly match to the images as shown above soyou do not need to do any tag assignment
Note In the future we will extend the tag system to allow landmarks and manual segmentations to be tagged so thatyou can provide additional input and hints to algorithms that need them For now only images can be tagged
Simply press the Submit button The ticket will begin to upload
13 Running DSS from ITK-SNAP 5
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ii
CHAPTER 1
DSS Userrsquos Quick Start Guide
This quick start guide describes how to use the new Distributed Segmentation Services (DSS) functionality in ITK-SNAP 38 or later
11 Prerequisites
bull ITK-SNAP 38 or later
bull A Google account (for authentication)
bull Familiarity with the command line (for the second part of tutorial)
12 DSS Overview
DSS allows you to send image datasets directly from ITK-SNAP to external ldquoservice providersrdquo who apply advancedimage processing algorithms to your data For example some segmentation algorithms require a Linux cluster ora high-end graphics card (GPU) With DSS you can use these algorithms simply by making a few mouse clicks inITK-SNAP
When you use DSS you communicate with a web-based application called the ldquomiddleware serverrdquo The main mid-dleware server for ITK-SNAP is httpsdssitksnaporg (you can visit this link and see the status of available services)Other local servers may also be set up in the future by various organizations The DSS middleware server distributesyour image datasets to service providers who use their advanced computer hardware to perform processing on yourdata
1
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning At the present DSS is intended for processing of small batches of datasets None of the providers havethe capacity to process hundreds or thousands of datasets for individual users In the future we envision extendingthis system to use cloud-based computing resources with the ability to pass the computational costs to end-users
13 Running DSS from ITK-SNAP
In this tutorial we will segment hippocampal subfields in a multi-modality MRI dataset of the human brain Segmen-tation will be perfomed using the ASHS algorithm
131 Download and Open Sample Workspace
A zip file containing an ITK-SNAP workspace with a high-resolution T2-weighted MRI scan of the hippocampalregion and a whole-brain T1-weighted MRI scan can be obtained from the ITK-SNAP download page Or simply usethe direct link below to download
bull httpswwwnitrcorgfrsdownloadphp10983ashs_testzip
Decomress the file ashs_testzip and double-click the workspace file ashs_testitksnap This shouldlaunch ITK-SNAP and load a pair of MRI scans as shown below
2 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
In this workspace a high-resolution T2-weighted is loaded as the ldquomain imagerdquo and a whole-brain T1-weightedMRI as an additional secondary image The ASHS algorithm will segment the subfields of the hippocampus andparahippocampal gyrus using a multi-atlas algorithm as described in (Yushkevich et al 2015) These structures playimportant roles in creation of memories and are affected early in Alzheimerrsquos disease
Fig 1 A close-up view of the hippocampus and parahippocampal gyrus from the sample dataset
132 Connect to DSS and Register
From the main ITK-SNAP menu select Tools-gtDistributed Segmentation Service This opens the ldquoDSS dialogrdquowhich we will use throughout this tutorial The dialog has three tabs ldquoConnectrdquo ldquoSubmitrdquo and ldquoResultsrdquo
13 Running DSS from ITK-SNAP 3
ITK-SNAP Distributed Segmentation Service Documentation Release 100
To connect to the DSS system press the Get Token button This will launch a web browser that will take you to thepage below
Press the Sign in with Google button and follow the prompts You will be asked to accept the terms of use after whichyou will get to a page shown below This page contains a single-use login credential called a ldquotokenrdquo
Copy and paste the 40-character token into the ldquoLogin Tokenrdquo field in ITK-SNAP The server status should change toldquoConnectedrdquo momentarily
133 Submit a Ticket to the DSS
The DSS uses the concept of tickets to track image processing tasks When you send a dataset to DSS a new ticketis created and assigned a unique number As the dataset is being processed you can track the status of your ticket oryou can request the processing to be cancelled Tickets are autonomous and you can close ITK-SNAP and open it
4 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
later (even on another computer) to access the results for your ticket You can submit another ticket without waitingfor your first ticket to finish processing
Go to the submit page in the DSS dialog The drop-down box called ldquoServicesrdquo contains a list of all services currentlyavailable on the DSS system
For this tutorial select the service ASHS-PMC 100 The submit dialog will look like this
The main part of the dialog consists of a table of tags Tags are labels assigned to different images in the workspacethat help the service provider identify which image is which (eg which is the T2-weighted MRI and which is theT1-weighted MRI) For this tutorial dataset the tags should already correctly match to the images as shown above soyou do not need to do any tag assignment
Note In the future we will extend the tag system to allow landmarks and manual segmentations to be tagged so thatyou can provide additional input and hints to algorithms that need them For now only images can be tagged
Simply press the Submit button The ticket will begin to upload
13 Running DSS from ITK-SNAP 5
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
CHAPTER 1
DSS Userrsquos Quick Start Guide
This quick start guide describes how to use the new Distributed Segmentation Services (DSS) functionality in ITK-SNAP 38 or later
11 Prerequisites
bull ITK-SNAP 38 or later
bull A Google account (for authentication)
bull Familiarity with the command line (for the second part of tutorial)
12 DSS Overview
DSS allows you to send image datasets directly from ITK-SNAP to external ldquoservice providersrdquo who apply advancedimage processing algorithms to your data For example some segmentation algorithms require a Linux cluster ora high-end graphics card (GPU) With DSS you can use these algorithms simply by making a few mouse clicks inITK-SNAP
When you use DSS you communicate with a web-based application called the ldquomiddleware serverrdquo The main mid-dleware server for ITK-SNAP is httpsdssitksnaporg (you can visit this link and see the status of available services)Other local servers may also be set up in the future by various organizations The DSS middleware server distributesyour image datasets to service providers who use their advanced computer hardware to perform processing on yourdata
1
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning At the present DSS is intended for processing of small batches of datasets None of the providers havethe capacity to process hundreds or thousands of datasets for individual users In the future we envision extendingthis system to use cloud-based computing resources with the ability to pass the computational costs to end-users
13 Running DSS from ITK-SNAP
In this tutorial we will segment hippocampal subfields in a multi-modality MRI dataset of the human brain Segmen-tation will be perfomed using the ASHS algorithm
131 Download and Open Sample Workspace
A zip file containing an ITK-SNAP workspace with a high-resolution T2-weighted MRI scan of the hippocampalregion and a whole-brain T1-weighted MRI scan can be obtained from the ITK-SNAP download page Or simply usethe direct link below to download
bull httpswwwnitrcorgfrsdownloadphp10983ashs_testzip
Decomress the file ashs_testzip and double-click the workspace file ashs_testitksnap This shouldlaunch ITK-SNAP and load a pair of MRI scans as shown below
2 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
In this workspace a high-resolution T2-weighted is loaded as the ldquomain imagerdquo and a whole-brain T1-weightedMRI as an additional secondary image The ASHS algorithm will segment the subfields of the hippocampus andparahippocampal gyrus using a multi-atlas algorithm as described in (Yushkevich et al 2015) These structures playimportant roles in creation of memories and are affected early in Alzheimerrsquos disease
Fig 1 A close-up view of the hippocampus and parahippocampal gyrus from the sample dataset
132 Connect to DSS and Register
From the main ITK-SNAP menu select Tools-gtDistributed Segmentation Service This opens the ldquoDSS dialogrdquowhich we will use throughout this tutorial The dialog has three tabs ldquoConnectrdquo ldquoSubmitrdquo and ldquoResultsrdquo
13 Running DSS from ITK-SNAP 3
ITK-SNAP Distributed Segmentation Service Documentation Release 100
To connect to the DSS system press the Get Token button This will launch a web browser that will take you to thepage below
Press the Sign in with Google button and follow the prompts You will be asked to accept the terms of use after whichyou will get to a page shown below This page contains a single-use login credential called a ldquotokenrdquo
Copy and paste the 40-character token into the ldquoLogin Tokenrdquo field in ITK-SNAP The server status should change toldquoConnectedrdquo momentarily
133 Submit a Ticket to the DSS
The DSS uses the concept of tickets to track image processing tasks When you send a dataset to DSS a new ticketis created and assigned a unique number As the dataset is being processed you can track the status of your ticket oryou can request the processing to be cancelled Tickets are autonomous and you can close ITK-SNAP and open it
4 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
later (even on another computer) to access the results for your ticket You can submit another ticket without waitingfor your first ticket to finish processing
Go to the submit page in the DSS dialog The drop-down box called ldquoServicesrdquo contains a list of all services currentlyavailable on the DSS system
For this tutorial select the service ASHS-PMC 100 The submit dialog will look like this
The main part of the dialog consists of a table of tags Tags are labels assigned to different images in the workspacethat help the service provider identify which image is which (eg which is the T2-weighted MRI and which is theT1-weighted MRI) For this tutorial dataset the tags should already correctly match to the images as shown above soyou do not need to do any tag assignment
Note In the future we will extend the tag system to allow landmarks and manual segmentations to be tagged so thatyou can provide additional input and hints to algorithms that need them For now only images can be tagged
Simply press the Submit button The ticket will begin to upload
13 Running DSS from ITK-SNAP 5
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning At the present DSS is intended for processing of small batches of datasets None of the providers havethe capacity to process hundreds or thousands of datasets for individual users In the future we envision extendingthis system to use cloud-based computing resources with the ability to pass the computational costs to end-users
13 Running DSS from ITK-SNAP
In this tutorial we will segment hippocampal subfields in a multi-modality MRI dataset of the human brain Segmen-tation will be perfomed using the ASHS algorithm
131 Download and Open Sample Workspace
A zip file containing an ITK-SNAP workspace with a high-resolution T2-weighted MRI scan of the hippocampalregion and a whole-brain T1-weighted MRI scan can be obtained from the ITK-SNAP download page Or simply usethe direct link below to download
bull httpswwwnitrcorgfrsdownloadphp10983ashs_testzip
Decomress the file ashs_testzip and double-click the workspace file ashs_testitksnap This shouldlaunch ITK-SNAP and load a pair of MRI scans as shown below
2 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
In this workspace a high-resolution T2-weighted is loaded as the ldquomain imagerdquo and a whole-brain T1-weightedMRI as an additional secondary image The ASHS algorithm will segment the subfields of the hippocampus andparahippocampal gyrus using a multi-atlas algorithm as described in (Yushkevich et al 2015) These structures playimportant roles in creation of memories and are affected early in Alzheimerrsquos disease
Fig 1 A close-up view of the hippocampus and parahippocampal gyrus from the sample dataset
132 Connect to DSS and Register
From the main ITK-SNAP menu select Tools-gtDistributed Segmentation Service This opens the ldquoDSS dialogrdquowhich we will use throughout this tutorial The dialog has three tabs ldquoConnectrdquo ldquoSubmitrdquo and ldquoResultsrdquo
13 Running DSS from ITK-SNAP 3
ITK-SNAP Distributed Segmentation Service Documentation Release 100
To connect to the DSS system press the Get Token button This will launch a web browser that will take you to thepage below
Press the Sign in with Google button and follow the prompts You will be asked to accept the terms of use after whichyou will get to a page shown below This page contains a single-use login credential called a ldquotokenrdquo
Copy and paste the 40-character token into the ldquoLogin Tokenrdquo field in ITK-SNAP The server status should change toldquoConnectedrdquo momentarily
133 Submit a Ticket to the DSS
The DSS uses the concept of tickets to track image processing tasks When you send a dataset to DSS a new ticketis created and assigned a unique number As the dataset is being processed you can track the status of your ticket oryou can request the processing to be cancelled Tickets are autonomous and you can close ITK-SNAP and open it
4 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
later (even on another computer) to access the results for your ticket You can submit another ticket without waitingfor your first ticket to finish processing
Go to the submit page in the DSS dialog The drop-down box called ldquoServicesrdquo contains a list of all services currentlyavailable on the DSS system
For this tutorial select the service ASHS-PMC 100 The submit dialog will look like this
The main part of the dialog consists of a table of tags Tags are labels assigned to different images in the workspacethat help the service provider identify which image is which (eg which is the T2-weighted MRI and which is theT1-weighted MRI) For this tutorial dataset the tags should already correctly match to the images as shown above soyou do not need to do any tag assignment
Note In the future we will extend the tag system to allow landmarks and manual segmentations to be tagged so thatyou can provide additional input and hints to algorithms that need them For now only images can be tagged
Simply press the Submit button The ticket will begin to upload
13 Running DSS from ITK-SNAP 5
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
In this workspace a high-resolution T2-weighted is loaded as the ldquomain imagerdquo and a whole-brain T1-weightedMRI as an additional secondary image The ASHS algorithm will segment the subfields of the hippocampus andparahippocampal gyrus using a multi-atlas algorithm as described in (Yushkevich et al 2015) These structures playimportant roles in creation of memories and are affected early in Alzheimerrsquos disease
Fig 1 A close-up view of the hippocampus and parahippocampal gyrus from the sample dataset
132 Connect to DSS and Register
From the main ITK-SNAP menu select Tools-gtDistributed Segmentation Service This opens the ldquoDSS dialogrdquowhich we will use throughout this tutorial The dialog has three tabs ldquoConnectrdquo ldquoSubmitrdquo and ldquoResultsrdquo
13 Running DSS from ITK-SNAP 3
ITK-SNAP Distributed Segmentation Service Documentation Release 100
To connect to the DSS system press the Get Token button This will launch a web browser that will take you to thepage below
Press the Sign in with Google button and follow the prompts You will be asked to accept the terms of use after whichyou will get to a page shown below This page contains a single-use login credential called a ldquotokenrdquo
Copy and paste the 40-character token into the ldquoLogin Tokenrdquo field in ITK-SNAP The server status should change toldquoConnectedrdquo momentarily
133 Submit a Ticket to the DSS
The DSS uses the concept of tickets to track image processing tasks When you send a dataset to DSS a new ticketis created and assigned a unique number As the dataset is being processed you can track the status of your ticket oryou can request the processing to be cancelled Tickets are autonomous and you can close ITK-SNAP and open it
4 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
later (even on another computer) to access the results for your ticket You can submit another ticket without waitingfor your first ticket to finish processing
Go to the submit page in the DSS dialog The drop-down box called ldquoServicesrdquo contains a list of all services currentlyavailable on the DSS system
For this tutorial select the service ASHS-PMC 100 The submit dialog will look like this
The main part of the dialog consists of a table of tags Tags are labels assigned to different images in the workspacethat help the service provider identify which image is which (eg which is the T2-weighted MRI and which is theT1-weighted MRI) For this tutorial dataset the tags should already correctly match to the images as shown above soyou do not need to do any tag assignment
Note In the future we will extend the tag system to allow landmarks and manual segmentations to be tagged so thatyou can provide additional input and hints to algorithms that need them For now only images can be tagged
Simply press the Submit button The ticket will begin to upload
13 Running DSS from ITK-SNAP 5
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
To connect to the DSS system press the Get Token button This will launch a web browser that will take you to thepage below
Press the Sign in with Google button and follow the prompts You will be asked to accept the terms of use after whichyou will get to a page shown below This page contains a single-use login credential called a ldquotokenrdquo
Copy and paste the 40-character token into the ldquoLogin Tokenrdquo field in ITK-SNAP The server status should change toldquoConnectedrdquo momentarily
133 Submit a Ticket to the DSS
The DSS uses the concept of tickets to track image processing tasks When you send a dataset to DSS a new ticketis created and assigned a unique number As the dataset is being processed you can track the status of your ticket oryou can request the processing to be cancelled Tickets are autonomous and you can close ITK-SNAP and open it
4 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
later (even on another computer) to access the results for your ticket You can submit another ticket without waitingfor your first ticket to finish processing
Go to the submit page in the DSS dialog The drop-down box called ldquoServicesrdquo contains a list of all services currentlyavailable on the DSS system
For this tutorial select the service ASHS-PMC 100 The submit dialog will look like this
The main part of the dialog consists of a table of tags Tags are labels assigned to different images in the workspacethat help the service provider identify which image is which (eg which is the T2-weighted MRI and which is theT1-weighted MRI) For this tutorial dataset the tags should already correctly match to the images as shown above soyou do not need to do any tag assignment
Note In the future we will extend the tag system to allow landmarks and manual segmentations to be tagged so thatyou can provide additional input and hints to algorithms that need them For now only images can be tagged
Simply press the Submit button The ticket will begin to upload
13 Running DSS from ITK-SNAP 5
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
later (even on another computer) to access the results for your ticket You can submit another ticket without waitingfor your first ticket to finish processing
Go to the submit page in the DSS dialog The drop-down box called ldquoServicesrdquo contains a list of all services currentlyavailable on the DSS system
For this tutorial select the service ASHS-PMC 100 The submit dialog will look like this
The main part of the dialog consists of a table of tags Tags are labels assigned to different images in the workspacethat help the service provider identify which image is which (eg which is the T2-weighted MRI and which is theT1-weighted MRI) For this tutorial dataset the tags should already correctly match to the images as shown above soyou do not need to do any tag assignment
Note In the future we will extend the tag system to allow landmarks and manual segmentations to be tagged so thatyou can provide additional input and hints to algorithms that need them For now only images can be tagged
Simply press the Submit button The ticket will begin to upload
13 Running DSS from ITK-SNAP 5
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
134 Tracking your Ticket
Once your ticket is uploaded the DSS dialog will switch to the ldquoResultsrdquo page and your newly created ticket will beselected At first your ticket will show up in ldquoreadyrdquo state which means it is sitting in a queue waiting to be pickedup by a service provider Your ticketrsquos position in the queue is shown
Eventually your ticketrsquos status will change to ldquoclaimedrdquo which means that a service provider has picked up your ticketand began processing it You will receive regular status updates including log messages and attachments (indicatedby paper clips)
Note Attachments are images or text files that are sent by the provider to illustrate the progress of the algorithmThey can be helpful for detecting problems during processing and can give you a preview of your final result Whenyou click an attachment in ITK-SNAP a browser window will open showing you the attachment
When the ticket finishes processing it should be marked ldquosuccessrdquo You will now be able to download the ticket Youwill be asked where to save the workspace containing the ticket results This workspace will automatically open inITK-SNAP
Note ITK-SNAP 38 supports multiple segmentation layers Algorithms like ASHS may return multiple segmenta-tion layers (corresponding to different parameters or options in the algorithm) You can switch between the differentsegmentation layers using the keys and or by selecting Tools-gtLayer Inspector
6 Chapter 1 DSS Userrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
14 Using DSS from the Command Line
Please follow the tutorial in ASHS documentation
14 Using DSS from the Command Line 7
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
8 Chapter 1 DSS Userrsquos Quick Start Guide
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
CHAPTER 2
DSS Service Developerrsquos Quick Start Guide
This quick start guide walks you through creating a new service from scratch We will first create a local DSSinstallation using Docker and then create our own segmentation service
21 Prerequisites
bull ITK-SNAP 38 or later
bull Docker
bull Familiarity with Bash scripting
bull A GitHub account and basic Git familiarity
22 DSS Architecture Overview
DSS architecture consists of three layers
9
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
client A GUI or command-line tool that communicates with DSS over the web Existing DSS clients are the ITK-SNAP GUI (after version 38) and the command-line tool itksnap-wt which is bundled with ITK-SNAP
middleware The middleware layer is a web application written in Python that orchestrates communication betweenmultiple service providers and multiple clients The main production DSS middleware is running at httpsdssitksnaporg However users can also run their own local copies of the middleware layer eg for testing
service Algorithm developers provide their tools as DSS services For example an algorithm for segmenting hip-pocampal subfields called ASHS is currently provided as a service at httpsdssitksnaporg This means thatany ITK-SNAP user can take advantage of this service to perform hippocampal subfield segmentation on theirMRI data Services communicate with the DSS middleware layer using itksnap-wt
This tutorial describes how to create your own service and hook it up to DSS
23 Running DSS locally
The first step to creating your own DSS service is to launch a local DSS middleware layer This will make it possibleto test your service
231 Set up DSS middleware
Clone the DSS middlware Git repository
git clone -b sandbox httpsgithubcompyushkevichalfabis_servercd alfabis_server
The following command will create three Docker containers one containing the SQL database for the middlewarelayer another running the middleware web application and the third running an example DSS service (a simplealgorithm that crops out the neck in 3D MRI scans)
docker-compose up
After running the command you will see a lot of output in the terminal colored by the container producing this outputTo test if the container is working connect to it using the web browser using the URL httplocalhost8080
Note Port 8080 must be available on your host machine If it is not edit the file docker-composeyml and change thefirst number under ports to the number of the port that is available to you
When you visit httplocalhost8080 you should see a functioning web page like the one captured below The mid-dleware server is set up in ldquoridiculously unsecurerdquo test mode where everyone accessing the server has administratoraccess This is intended for testing the system on your own machine You should never use this server in a produc-tion setting
10 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
232 Connect to DSS with ITK-SNAP GUI
Next connect to the server using ITK-SNAP Open ITK-SNAP and in the main menu select Tools-gtDistributedSegmentation Service In the Connect tab press the button Manage and paste the URL of your middleware server(httplocalhost8080)
Now press the Get Token button which will open your web browser on a page showing a 40-digit authenticationtoken Normally you would need to login using OAuth2 and accept terms of service but in the ridiculously unsecuretest mode you are automatically logged in as user testexamplecom and so the token is shown to you right away
Copy and paste the token into the corresponding text box in ITK-SNAP The server status should indicate successfulconnection as shown below
Switch over to the Submit page You should see a single service under the list of services called ldquoMRI NeckCut100rdquo This service is running in one of your Docker containers Given a T1-weighted MRI scan of an adultrsquos headthis service will find the top of the head and crop out an 18cm region of interest from the top of the head downie cropping out excessive neck tissue This is useful as a preprocessing step for registering MRI scans betweenindividuals
Try out the service on any T1-weighted brain dataset (but not one that has already been skull-stripped) For exampleyou can download sample datasets from ADNI The result of running the pipeline should look something like whatyou see below
23 Running DSS locally 11
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Fig 1 Result of running the example service on a T1 head scan The neck is cropped away and a segmentation of theheadneck is created
24 Writing Your Own Service
In this section of the tutorial you will create your own service This consists of two parts creating a public Gitrepository that describes the service and writing the back-end script that executes the service
Note If instead of a step-by-step tutorial you would like to see the complete code for a simple service see testingexample_service in this repository
241 Our Example Service
We will implement an example service that performs deformable registration between two images and maps the labelsfrom one image onto the other This may be useful for tracking the change in the size of lesions over time etc In theirITK-SNAP workspace the users will need to tag the source image (eg the first time point) and the target image (thesecond time point) Labels will be transformed from the source image to the target image
242 Creating a Service Description
In this tutorial the directory structure for your service looks like this
my_servicedesc The Git repository describing the service
(continues on next page)
12 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
daemon The directory where the scripts running the service arerarr˓located
work The directory where processing data for tickets is stored
Create this structure enter the desc directory and use your favorite text editor to edit the file servicejson
cd descnano servicejson
The file servicejson is used to explain to the users what kind of input the service requires and what type ofresults it generates The contents of servicejson should be as follows Of course you can modify any of thedescriptive values like ldquokeywordsrdquo or ldquolongdescrdquo
name RegistrationExampleversion 010keywords [registrationlongitudinal
]shortdesc DSS tutorial service (deformable registration)longdesc Given a target image and a source image with labels perform
rarr˓registration and map labels from the source image to the target imagetags [
name Targettype MainImagehint The anatomical image onto which you want to warp the segmentationrequired true
name Sourcetype OverlayImagehint The anatomical image from which you want to warp the segmentationrequired true
name SourceSegtype OverlayImagehint The segmentation of the source imagerequired true
]
The first part of the servicejson file describes the service at various levels of detail Additional keywords youcan include in a real service are citation and url A very important part of the file is the tags directive Itspecifies what kinds of images must be passed in to your pipeline The tags directive in this example specifiesthat the ITK-SNAP workspace passed in to your pipeline must contain an anatomical image (eg an MRI) and asegmentation image Later in your script you will use these tag names to extract these images from the workspace
Note See the reference manual for details on servicejson Files
Before proceeding make sure your Json syntax is correct Use an online validator such as httpsjsonformatterorg orhttpsjsonlintcom to check for errors
24 Writing Your Own Service 13
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Warning If your servicejson file contains invalid syntax you will receive errors in later stages of thetutorial
243 Creating a Git Repository
Next we will create a public Git repository to hold your service descriptor First letrsquos initialize a local Git repositoryMaking sure you are still in your desc directory enter the following commands
git initgit add servicejsongit commit -m Initial commit of servicejson
Now go to httpsgithubcom sign in and create a new public repository Letrsquos say the name of your repository ismy_little_dss_test Make sure your repository is public and make sure not to initialize the repository with aREADME
14 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Now push your local repository to the remote public repository
git remote add origin httpsgithubcomyour_github_useridmy_little_dss_testgitgit push -u origin master
Visit github again to make sure that your servicejson appears there It should appear at the URL httpsgithubcomyour_github_useridmy_little_dss_test
244 Authenticate with DSS
Now letrsquos tell your local DSS server about your new service We will need to authenticate and use DSS administrativecommands in itksnap-wt to do this Make sure that itksnap-wt is in your path
Note itksnap-wt is the command-line workspace tool included with ITK-SNAP 38 and later On Mac and
24 Writing Your Own Service 15
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Windows from the ITK-SNAP main menu select Help-gtInstall Command-Line Tools to install it and other tools
First authenticate with your local DSS middleware server
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token
Follow the link httplocalhost8080token obtain the token from that link and paste it into the prompt
gtgt itksnap-wt -dss-auth httplocalhost8080Paste this link into your browser to obtain a token httplocalhost8080token
Enter the token e851fa1804f4f9256fb937d904e420ed56e98d89d3ccd618Success logged in as testexamplecom
The authentication credentials are stored in the $HOMEalfabis server allowing subsequent calls toitksnap-wt to access the server without having to authenticate
245 Register Your Service with DSS
Before registering our service we need to create a provider entry A provider represents a group of users who offer a setof services via DSS For example a company unit or a research lab We will create a new provider called testlab
itksnap-wt -dssa-providers-add testlab
Note Commands starting with -dssa are administrative commands They are available to you on your local DSSmiddleware server after you login as testexamplecom which has administrator access
Next we will provide access to the provider testlab to the user testexamplecom
itksnap-wt -dssa-providers-users-add testlab testexamplecom
Finally we will register our service with DSS
itksnap-wt -dssa-providers-services-add testlab httpsgithubcomyour_github_useridrarr˓my_little_dss_test master
The above command points DSS to the Github repository where you placed servicejson It also tells DSS to usethe latest commit in the master branch of this repository You can provide any branch tag or commit here Lastlyit tells DSS to associate the service with provider testlab
Verify that the above commands were successful by listing all services available to you as the provider
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
Note Commands starting with -dssp are service provider commands They do not require administrator access
You should now see your service listed
bull When you navigate to httplocalhost8080services in your browser
bull In the ITK-SNAP GUI on the ldquoSubmitrdquo tab of the DSS window
16 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
246 Submitting a Ticket for the New Service
We are almost ready to code our service Normally we would organize our service as a Bash or Python scriptHowever in this example we will walk through every step of the service interactively showing you the output of eachcommand used to communicate with the middleware server The life-cycle of a service is shown in the flowchartbelow
First go ahead and submit a ticket for your new service We have provided a sample dataset called bav_examplezip for this tutorial on the ITK-SNAP sample data download page Decompress the zip file and open the workspacebav_exampleitksnap in ITK-SNAP It should look like the screenshot below The dataset consists of two timeframes in a 3D TEE ultrasound dataset of the bicuspid aortic valve Frame 14 is the lsquotargetrsquo frame which we aimto segment and frame 25 is the lsquosourcersquo frame for which we have the manual segmentation Notice that the manualsegmentation is loaded as an ldquooverlayrdquo in ITK-SNAP rather than a segmentation image This is because our pipelineis going to support sourcetarget pairs that have different image dimensions and in ITK-SNAP the segmentation imagehas to have the same dimensions as the main image
24 Writing Your Own Service 17
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Next go ahead and submit the service On the ldquosubmitrdquo tab of the DSS window assign the tags you defined inservicejson to the different layers in your workspace as shown below Then press Submit Your ticket shouldshow up on the ldquoResultsrdquo tab in ldquoreadyrdquo state
18 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
247 Scripting the Service
We are now going to simulate what the service would do First make sure you are authenticated with the DSS middle-ware server
gtgt itksnap-wt -dss-auth httplocalhost8080
We will need to know the 40-character Git commit hash of the service we registered with DSS Obtain it using
gtgt itksnap-wt -P -dssp-services-listMRI-NeckCut 100 e0a316038e9cbe6a000e07c82758532a8863f51f testRegistrationExample 010 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlab
In this case the hash we will use is b7392368dc5dcec910bb8b87006ae38fd1f2cb32
Claiming a Ticket
Now we will issue a command to claim the next available ticket for our service When a ticket is claimed it is nolonger visible to other providers of the service After claiming a ticket you are expected to process it right awayTo claim the ticket we must provide the service hash code the name of the provider (testlab) and an additionalldquoinstance idrdquo which is an arbitrary code used when a provider has multiple ldquoinstancesrdquo running in parallel Use thefollowing command to claim the ticket (except your hash code will be different)
gtgt itksnap-wt -dssp-services-claim b7392368dc5dcec910bb8b87006ae38fd1f2cb32 testlabrarr˓instance_1POST httplocalhost8080apiproservicesclaims VALUESrarr˓services=8e6886c303260e30e62d67b3347f8ceb234c6d18ampprovider=testlabampcode=instance_11gt 1 b7392368dc5dcec910bb8b87006ae38fd1f2cb32 claimed
The ldquousefulrdquo output of this command is on the line that starts with 1gt In general itksnap-wt prefixes the outputof each command with its number while various informational messages are not labeled The -P flag used previouslytells itksnap-wt not to print this prefix Above the output of the command consists of the ticket id (1) the servicehash code and the status of the ticket (claimed)
Note You can call -dssp-services-claim with a comma-separated list of service hash codes In this case theDSS server will return the highest-priority ticket across all these services You can use the second field in the outputof -dssp-services-claim to figure out which service that ticket belongs to
24 Writing Your Own Service 19
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
Downloading Ticket Workspace
Now that the ticket has been claimed we should download its data Let us create a temporary work directory for ourticket
gtgt TICKET_ID=1gtgt WORKDIR=tmpdss_workticket_$TICKET_IDgtgt mkdir -p $WORKDIR
And now download the ticket
gtgt itksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR1gt tmpdss_workticket_1layer_000_73f86306f91fdcac7a84159b3a916e21niigz1gt tmpdss_workticket_1layer_001_a8533a499615e024637587466b574689niigz1gt tmpdss_workticket_1layer_002_c3577ae4df83ba30ab4b014ebb577c62niigz1gt tmpdss_workticket_1ticket_00000001itksnap
The output of the command consists of all of the filenames that have been downloaded from the DSS server Note thatthe names of the files have been changed this is part of the anonymization process
We can now send a message to the user letting them know that we have downloaded the ticket successfully (this is notnecessary but there is no harm in it either)
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloadedPOST httplocalhost8080apiprotickets1info VALUES message=Ticket successfullyrarr˓downloaded
If you look at the ldquoResultsrdquo tab in ITK-SNAP this message should be visible
Next we will extract the layers identified by the different tags (target source source segmentation) from theworkspace We can list all of the layers with tags like this
gtgt INPUT_WSP=tmpdss_workticket_1ticket_00000001itksnapgtgt itksnap-wt -i $INPUT_WSP -layers-list2gt Layer Role Nickname Filenamerarr˓ Tags2gt 000 Main Target Frame (frame 14) tmpdss_workticket_1layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target2gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_1layer_001_rarr˓a8533a499615e024637587466b574689niigz Source2gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_1layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg
To extract the filenames of the specific layers
gtgt TARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)gtgt SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)gtgt SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Perform Image Registration
Next we will use greedy a tool included with ITK-SNAP to perform registration We will perform affine registrationfollowed by deformable registration In some cases the user may have already performed some registration betweenthe two images using the interactive registration mode in ITK-SNAP We can check this as follows
20 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt Pauls-MBP-2tmp pauly$ itksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-rarr˓get-transformINFO picked layer LayersLayer[001]3gt 1 0 -0 -03gt 0 1 -0 -03gt -0 -0 1 03gt 0 0 0 1
The above command first lsquopicks outrsquo the layer with tag lsquoSourcersquo and then prints its affine transform relative to the mainimage The transform is a 4x4 identity matrix in our example In the script below we actually store the transform anduse it to initialize affine registration but here we will not do that since we know the transform is identity Instead wewill go ahead and just perform affine registration
gtgt AFFINE_MAT=$WORKDIRaffinematgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -rarr˓ia-identityExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 022
N=12 NUMBER OF CORRECTIONS=5 INITIAL VALUES F= -630105 GNORM= 44293
I NFN FUNC GNORM STEPLENGTH1 3 -6389660 87491 00012 5 -6418671 86297 5000
17 20 -2386336 14465 031618 21 -2386542 7294 1000
Final RAS Transform100004 -0000740917 -0000537392 00773808000135426 0998996 000297984 -0111559000132127 -0000743018 100355 -03408510 0 0 1
Affine registration should take about a minute Please see greedy documentation for an explanation of the parameters
Now we can inform the user that affine registration completed that the job is 40 done and even show them the affinetransform matrix
gtgt itksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplaingtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Check your ldquoResultsrdquo page in the ITK-SNAP DSS dialog and you will see this updated information Click on thepaperclip icon and you will see the matrix
Now we can perform deformable registration
gtgt WARP=$WORKDIRwarpniigzgtgt greedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -orarr˓$WARPExecuting with the default number of threads 8Noise on image 0 component 0 fixed = 0224 moving = 021866LEVEL 1 of 3
Smoothing sigmas [429654 417696 369327] [175405 170524 150777]Level 0 Iter 0 Energy = 06955Level 0 Iter 1 Energy = 07112Level 2 Iter 18 Energy = 03482
(continues on next page)
24 Writing Your Own Service 21
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Level 2 Iter 19 Energy = 03487END OF LEVEL 2 DetJac Range 00523 to 63114
Avg Gradient Time 06519s 4143Avg Gaussian Time 07485s 4757Avg Iteration Time 15734s
Again update the progress
gtgt itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulgtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
The next step is to compute a warp that combines the deformable and affine transformations and to apply the warp tothe source image and its segmentation This is accomplished using the following greedy commands
gtgt RESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzgtgt RESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzgtgt COMBINED_WARP=$WORKDIRwarp_combinedniigzgtgt greedy -d 3 -rf $TARGET_IMG
-rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Executing with the default number of threads 8
Package and Upload Results
Finally we add these newly generated images to the ITK-SNAP workspace and save it as a new workspace file
gtgt RESULT_WSP=$WORKDIRresultitksnapgtgt itksnap-wt -i $INPUT_WSP
-layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
9gt Layer Role Nickname Filenamerarr˓ Tags9gt 000 Main Target Frame (frame 14) tmpdss_workticket_3layer_000_rarr˓73f86306f91fdcac7a84159b3a916e21niigz Target9gt 001 Overlay Source Frame (frame 25) tmpdss_workticket_3layer_001_rarr˓a8533a499615e024637587466b574689niigz Source9gt 002 Overlay Source ManSeg (frame 25) tmpdss_workticket_3layer_002_rarr˓c3577ae4df83ba30ab4b014ebb577c62niigz SourceSeg9gt 003 Overlay Resliced Source Image tmpdss_workticket_3reslice_rarr˓sourceniigz9gt 004 Overlay Deformation Field tmpdss_workticket_3warp_rarr˓combinedniigz9gt 005 Segmentation Resliced Segmentation tmpdss_workticket_3reslice_rarr˓source_segniigz
Whatrsquos left is to upload this workspace to the DSS middleware server In the process of uploading the workspacewill be archived into a temporary directory and names of the layers will be replaced by hashes just like in the inputworkspace
22 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
gtgt itksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_IDExported workspace to tmpalfabis_NMYoqnticket_00000001_resultsitksnapUpload tmpalfabis_NMYoqnlayer_004_9c0895a61850b6a03458081ab400f029niigz (889 Mbrarr˓in 69 s)Upload tmpalfabis_NMYoqnlayer_000_73f86306f91fdcac7a84159b3a916e21niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_001_a8533a499615e024637587466b574689niigz (34 Mbrarr˓in 01 s)Upload tmpalfabis_NMYoqnlayer_002_c3577ae4df83ba30ab4b014ebb577c62niigz (00 Mbrarr˓in 00 s)Upload tmpalfabis_NMYoqnticket_00000003_resultsitksnap (00 Mb in 00 s)Upload tmpalfabis_NMYoqnlayer_003_577f9723a54d1bc8bbfb02369d91ed2fniigz (102 Mbrarr˓in 03 s)Upload tmpalfabis_NMYoqnlayer_005_ebaaaed7ad802e73b94e5d254a7a64c0niigz (00 Mbrarr˓in 00 s)
The very final step is to set progress to 100 and mark the ticket as a success
gtgt itksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1gtgt itksnap-wt -dssp-tickets-success $TICKET_ID
If you return to ITK-SNAP the ticket should be marked as successful and the download button will be enabled
Download the result workspace If everything worked well you should see a decent segmentation of your target imagelike this
24 Writing Your Own Service 23
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
This is pretty much it We have now walked through all the steps involved in creating a service A complete scriptimplementing this tutorial service is listed below
Complete Service Script
Below is the code for a functional script that runs this service It relies on Unix tools grep awk and sed to capturethe output of the various itksnap-wt commands It also includes a bit of error checking
binbashset -x
Look up the git commit hash of the service RegistrationExample (third word inrarr˓output)GITHASH=$(itksnap-wt -P -dssp-services-list | grep RegistrationExample | awk printrarr˓$3)
Temporary directory for this scriptif [[ $TMPDIR ]] thenTMPDIR=tmpservice_$GITHASHmkdir -p $TMPDIR
fi
Run in an infinite loopwhile [[ true ]] do
(continues on next page)
24 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Claim a ticketitksnap-wt -dssp-services-claim $GITHASH testlab instance_1 gt $TMPDIRclaimtxt
If return code is non-zero we sleep and continueif [[ $ -ne 0 ]] thensleep 10continue
fi
Get the ticket id (second word in line starting with 1gt)TICKET_ID=$(cat $TMPDIRclaimtxt | grep ^1gt | awk print $2)
Create work dirWORKDIR=$TMPDIRdss_workticket_$TICKET_IDmkdir -p $WORKDIR
Download the ticketitksnap-wt -dssp-tickets-download $TICKET_ID $WORKDIR gt $TMPDIRdownloadtxt
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to download ticketcontinue
fi
Inform the useritksnap-wt -dssp-tickets-log $TICKET_ID info Ticket successfully downloaded
Find the workspace file in the downloadINPUT_WSP=$(cat $TMPDIRdownloadtxt | grep ^1gtitksnap$ | sed -e s^1gt )
Get the necessary layersTARGET_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Target)SOURCE_IMG=$(itksnap-wt -P -i $INPUT_WSP -llf Source)SOURCE_SEG=$(itksnap-wt -P -i $INPUT_WSP -llf SourceSeg)
Extract the affine transformation from the input workspaceINIT_AFFINE=$WORKDIRinit_affinematitksnap-wt -i $INPUT_WSP -layers-pick-by-tag Source -props-get-transform | grep ^3gt | sed -e s^3gt gt $INIT_AFFINE
Perform affine registrationAFFINE_MAT=$WORKDIRaffinematAFFINE_OUTPUT=$WORKDIRaffine_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -a -i $TARGET_IMG $SOURCE_IMG -o $AFFINE_MAT -ia $INIT_AFFINE gt $AFFINE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID AffineReg output $AFFINE_OUTPUT text
rarr˓plain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Affine registration failedcontinue
fi(continues on next page)
24 Writing Your Own Service 25
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
Attach affine result to the log and send updateitksnap-wt -dssp-tickets-attach $TICKET_ID Affine matrix $AFFINE_MAT textplainitksnap-wt -dssp-tickets-log $TICKET_ID info Affine registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 04
Perform deformable registrationWARP=$WORKDIRwarpniigzDEFORMABLE_OUTPUT=$WORKDIRdeformable_reg_outputtxtgreedy -d 3 -m NCC 8x8x8 -n 40x40x20 -i $TARGET_IMG $SOURCE_IMG -it $AFFINE_MAT -o
rarr˓$WARP gt $DEFORMABLE_OUTPUT 2gtamp1
Attach the output to the logitksnap-wt -dssp-tickets-attach $TICKET_ID Deformable output $DEFORMABLE_OUTPUT
rarr˓textplain
Check that registration succeededif [[ -f $AFFINE_MAT ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Deformable registration failedcontinue
fi
itksnap-wt -dssp-tickets-log $TICKET_ID info Deformable registration successfulitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 08
Perform the final reslice stepRESLICE_SOURCE_IMG=$WORKDIRreslice_source_imgniigzRESLICE_SOURCE_SEG=$WORKDIRreslice_source_segniigzCOMBINED_WARP=$WORKDIRwarp_combinedniigzgreedy -d 3 -rf $TARGET_IMG -rm $SOURCE_IMG $RESLICE_SOURCE_IMG -ri LABEL 02vox -rm $SOURCE_SEG $RESLICE_SOURCE_SEG -rc $COMBINED_WARP -r $WARP $AFFINE_MAT
Package up the workspaceRESULT_WSP=$WORKDIRresultitksnapitksnap-wt -i $INPUT_WSP -layers-add-anat $RESLICE_SOURCE_IMG -props-set-nickname Resliced Source Image -layers-add-anat $COMBINED_WARP -props-set-nickname Deformation Field -props-
rarr˓set-colormap jet -layers-add-seg $RESLICE_SOURCE_SEG -props-set-nickname Resliced Segmentation -layers-list -o $RESULT_WSP
Upload the workspaceitksnap-wt -i $RESULT_WSP -dssp-tickets-upload $TICKET_ID
If return code is non-zero we mark the ticket as failedif [[ $ -ne 0 ]] thenitksnap-wt -dssp-tickets-fail $TICKET_ID Failed to upload ticketcontinue
fi
Set progress and mark ticket as successitksnap-wt -dssp-tickets-set-progress $TICKET_ID 0 1 1itksnap-wt -dssp-tickets-success $TICKET_ID
(continues on next page)
26 Chapter 2 DSS Service Developerrsquos Quick Start Guide
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
(continued from previous page)
done
24 Writing Your Own Service 27
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
28 Chapter 2 DSS Service Developerrsquos Quick Start Guide
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
CHAPTER 3
ITK-SNAP DSS Reference Manual
31 servicejson Files
These files are used by ITK-SNAP and the middleware server web interface to describe the service to the user Theycontain descriptive information such as the explanation of what the service does as well as codify what kinds of inputmust be provided to the service
See an example servicejson file httpsgithubcompyushkevichalfabis-svc-ashs-pmcblobmasterservicejson
311 Top-Level Fields
name The official name of the service May not contain spaces
version The semantic version of the service The version refers to the iteration of the service itself rather than to theversion of the underlying tool that it is running For example you may have multple versions of a service thatcalls MyTool 20 (say version 100 of your service called MyTool with the wrong parameters so you createdversion 101 to fix the parameters)
shortdesc The short description of the service This is shown in the ITK-SNAP dropdown used to select services Donot include the name and version of the service in the short description
longdesc A paragraph describing the service in more detail This is shown in ITK-SNAP in a small font once theservice has been selected from a dropdown
citation optional A URL pointing to the paper that you wish to be cited for your service
url optional A URL pointing a webpage describing your service
author optionalnotused Name of the author
keywords optionalnotused A list of keywords that can be used to help find the service
tags A list of tags that point to various ldquoobjectsrdquo in the ITK-SNAP workspace that is passed to your service Seebelow
29
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
parameters optionalnotused A list of parameters that the user must provide for running your service Thishas not yet been implemented
312 Tags Directive
The tags directive includes a list of tags each representing a separate object in the ITK-SNAP workspace Currentlyonly image layers can be tagged but in the near future it will be possible to tag annotations (eg landmarks) andindividual segmentation labels An example tags section look like this
tags [
name T2-MRItype MainImagehint A high-resolution (eg 04x04x20mm^3) T2-weighted MRI scan with
rarr˓oblique coronal orientation parallel to hippocampal main axis This scan must berarr˓the main image in the workspace
required true
name T1-MRItype OverlayImagehint Roughtly isotropic (eg 1x1x1mm^3) T1-weighted MRI scan of the whole
rarr˓brainrequired true
name ACtype PointLandmarkhint Anterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
name PCtype PointLandmarkhint Posterior commissure point can be optionally specified to help align
rarr˓the MRI scan to the brain templaterequired false
]
name The unique name of the tag which may not contain spaces
type Describes the type of the ITK-SNAP object to which the tags refers Currently implemented objects areMainImage AnatomicImage OverlayImage Segmentation In the future we will implementLabel and PointLandmark
hint A text string shown to the user when assigning tags to items in the ITK-SNAP workspace
required A boolean indicating whether the tag must be assigned before the service can be executed
32 DSS REST API
This section describes the RESTful API for the DSS middleware server
30 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
321 apilogin
GET Check login status
POST Login
token 40-character login token (from httpsprotectT1textdollarURLtoken)
322 apioauth2cb
OAuthCallbackAPI
323 apitoken
TokenAPI
324 apiservices
ServicesAPI
325 apiservices([a-f0-9]+)detail
ServicesDetailAPI
326 apiservices([a-f0-9]+)stats
ServicesStatsAPI
327 apitickets
TicketsAPI
328 apitickets(d+)files(input|results)
TicketFilesAPI
329 apitickets(d+)files(input|results)(d+)
TicketFileDownloadAPI
3210 apitickets(d+)status
TicketStatusAPI
3211 apitickets(d+)log
TicketLogAPI
32 DSS REST API 31
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3212 apitickets(d+)progress
TicketProgressAPI
3213 apitickets(d+)queuepos
TicketQueuePositionAPI
3214 apitickets(d+)detail
TicketDetailAPI
3215 apitickets(d+)delete
TicketDeleteAPI
3216 apitickets(d+)retry
TicketRetryAPI
3217 apiticketslogs(d+)attachments
TicketLogAttachmentAPI
3218 apiproservices
ProviderServicesAPI
3219 apiproservices([w-]+)tickets
ProviderServiceTicketsAPI
3220 apiproservices([a-f0-9]+)claims
ProviderServiceClaimsAPI
3221 apiproservicesclaims
ProviderMultipleServiceClaimsAPI
3222 apiprotickets(d+)files(input|results)
ProviderTicketFilesAPI
32 Chapter 3 ITK-SNAP DSS Reference Manual
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3223 apiprotickets(d+)files(input|results)(d+)
ProviderTicketFileDownloadAPI
3224 apiprotickets(d+)status
ProviderTicketStatusAPI
3225 apiprotickets(d+)(error|warning|info|log)
ProviderTicketLogAPI
3226 apiprotickets(d+)attachments
ProviderTicketAttachmentAPI
3227 apiprotickets(d+)progress
ProviderTicketProgressAPI
3228 apiadminproviders
AdminProvidersAPI
3229 apiadminproviders([w-]+)delete
AdminProviderDeleteAPIrdquo
3230 apiadminproviders([w-]+)users
AdminProviderUsersAPIrdquo
3231 apiadminproviders([w-]+)users(d+)delete
AdminProviderUsersDeleteAPIrdquo
3232 apiadminproviders([w-]+)services
AdminProviderServicesAPIrdquo
3233 apiadminproviders([w-]+)services([a-f0-9]+)delete
AdminProviderServicesDeleteAPIrdquo
32 DSS REST API 33
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
ITK-SNAP Distributed Segmentation Service Documentation Release 100
3234 apiadminticketspurge(completed|all)
AdminPurgeTicketsAPIrdquo
3235 apiadmintickets
AdminTicketsAPIrdquo
3236 blobs([a-f0-9]8)
DirectDownloadAPI
3237 blobs([a-f0-9]32)
DirectDownloadAP
34 Chapter 3 ITK-SNAP DSS Reference Manual
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-
CHAPTER 4
What is ITK-SNAP DSS
DSS stands for Distributed Segmentation Services It is an architecture that allows medical image segmentation tasksto be implemented as services on the Internet These services can be easily accessed via interactive software likeITK-SNAP and using a thin command-line client This documentation is focused on algorithm developers interestedin making their tools available through the service
35
- DSS Userrsquos Quick Start Guide
-
- Prerequisites
- DSS Overview
- Running DSS from ITK-SNAP
- Using DSS from the Command Line
-
- DSS Service Developerrsquos Quick Start Guide
-
- Prerequisites
- DSS Architecture Overview
- Running DSS locally
- Writing Your Own Service
-
- ITK-SNAP DSS Reference Manual
-
- servicejson Files
- DSS REST API
-
- What is ITK-SNAP DSS
-