[Gate-users] Run GATETOOLS from Python script

Nils Krah nils.krah at creatis.insa-lyon.fr
Thu Jan 30 18:14:20 CET 2020


Hi Daniela,
yes, dicomfiles contains the filenames which match the pattern '*.dcm’, so the dicom files. Up to that point, this has nothing to do with gatetools, by the way. Just a standard way to read in filenames in python. A priori, the filenames are not sorted, but that should not be a problem for you. The read_dicom function in gatetools looks for the SliceLocation dicom attribute and, if present, sorts the slices accordingly.

On to your error: The error looks like the file is not where you think it is. My code works only if you execute it in the folder where the dicom files are. If, for example, you write

dicomfiles = fnmatch.filter(os.listdir(‘/Users/nkrah/dicoms‘), ‘*dcm’)

the list dicomfiles will contain the names in the directory /Users/nkrah/dicoms but not their full path. If I run the script from /Users/nkrah/scripts and feed the dicomfiles list to gt.read_dicom(dicomfiles), python will try to find the files in the current directory, i.e., where the script is executed.

That might explain your problem. Can you share the code you are using or are you using the exact same lines I have provided?

Try using absolute paths:

dicomfiles_names = fnmatch.filter(os.listdir('./'), '*.dcm’)
dicomfiles_absolute = [os.path.abspath(df) for df in dicomfiles_names] # this compact syntax is called ‘list comprehension’ in python.

… and feed this as input to the read_dicom function.

I know these things are a bit frustrating at the beginning, but once you get a hang of how to deal with files and so on in python, it’s quite straight forward.

Cheers,
Nils
On 30 Jan 2020 at 17:40 +0100, Botnariuc, Daniela <daniela.botnariuc.19 at ucl.ac.uk>, wrote:
> 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>, 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
> > 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/a6267627/attachment.html>


More information about the Gate-users mailing list