[Gate-users] Run GATETOOLS from Python script

Botnariuc, Daniela daniela.botnariuc.19 at ucl.ac.uk
Thu Jan 30 17:40:41 CET 2020


Dear Nils,

Thank you very much for the quick reply, I really appreciate it.
I tried to run the code you suggested but I still get an error.

According to the code, the ‘dicomfiles’ variable  is an array which lists all the files in the folder: [‘dicom1.dcm’, ‘dicom2.dcm’, ‘dicom3.dcm’, ….]  - it actually lists the files in a random order.

When I run the code, I get the following error:
no such file or directory: ‘dicom 17.dcm'  (being ‘dicom17.dcm’ the first file appearing in my list)

I appreciate any help with this issue.

Thank you
Daniela

From: Nils Krah <nils.krah at creatis.insa-lyon.fr>
Sent: 30 January 2020 14:10
To: gate-users at lists.opengatecollaboration.org; Botnariuc, Daniela <daniela.botnariuc.19 at ucl.ac.uk>
Subject: Re: [Gate-users] Run GATETOOLS from Python script

Hi Daniela,
within a python script, you need to call the python function image_convert. It takes as input an ITK image.
So, say you have a series of dicom files representing a 3D image and you want to convert it to float, you could do:

import os
import fnmatch
import itk
import gatetools as gt


# get file in current folder and filter out those ending on .dcm
dicomfiles = fnmatch.filter(os.listdir('./'), '*.dcm’)
# read them in and get them as ITK image
itk_image = gt.read_dicom(dicomfiles)
# pass the ITK image to the convert function and specify to which type you want to convert
converted_image = gt.image_convert(itk_image, pixeltype=‘float’)
# you get again an ITK image and yu can continue manipulating it as you wish.
# You may dump it with:
itk.imwrite(converted_image, ‘converted_image.mhd’)


(I haven’t tested this bit of code, but it should work.)


Technical remark for python newcomers:
When writing gt. preceding the function name, you are referring to the namespace of the imported module. The module’s name gatetools has been aliased to gt for brevity. That is not mandatory, but often practical.

Hope that helps.

Best,
Nils
On 30 Jan 2020 at 12:56 +0100, Botnariuc, Daniela <daniela.botnariuc.19 at ucl.ac.uk<mailto:daniela.botnariuc.19 at ucl.ac.uk>>, wrote:

Dear GATE users,

I have been using the gatetools (thank you for this great contribution!) from the command line but I would like to run them from a python script. I am not as familiar with python as I should be to understand the code in detail. I tried to transform the bash scripts that run the tools from the command line to python files but I am having problems with the type of input/output the functions require.

For example, for the gt_image_convert, how should I write my input (path to the dicom folder) and output in python to run the function?
I thought it would be something like: (but I am missing something)

gt_image_convert('/home/gate/Documents/dicom_folder','output.mhd','float')

Thank you. I look forward to hearing from you.
Kind regards,
Daniela

_______________________________________________
Gate-users mailing list
Gate-users at lists.opengatecollaboration.org<mailto:Gate-users at lists.opengatecollaboration.org>
http://lists.opengatecollaboration.org/mailman/listinfo/gate-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opengatecollaboration.org/pipermail/gate-users/attachments/20200130/d5e73f6f/attachment-0001.html>


More information about the Gate-users mailing list