[Insight-developers] [ITK + Python] Wrapping classes proposal
Gaetan Lehmann
gaetan.lehmann at jouy.inra.fr
Fri Jun 17 08:38:04 EDT 2005
On Thu, 16 Jun 2005 16:14:01 +0200, Benoit Regrain
<benoit.regrain at creatis.insa-lyon.fr> wrote:
>
>
> ----- Original Message ----- From: "Gaetan Lehmann"
> <gaetan.lehmann at jouy.inra.fr>
> To: "Benoit Regrain" <benoit.regrain at creatis.insa-lyon.fr>;
> <insight-developers at itk.org>
> Sent: Wednesday, June 15, 2005 3:37 PM
> Subject: Re: [Insight-developers] [ITK + Python] Wrapping classes
> proposal
>
>
>>> Some comments :
>>> I import import itkClassParser instead of import everything in it to be
>>> able to keep only interesting attributes (for user) in itk module
>>> I use True and False instead of 1 and 0
>>> I haven't imported ItkBase because the unique method it contains is
>>> used
>>> only in one class. But it may be nice to have one common class for all
>>> itk
>>> classes
>>> I have used ''.join([]) instead of string.join([], '') because
>>> string.join
>>> should be deprecated in future python
>>> I remove ITK_ prefix of types (itk.ITK_US -> itk.US). itk.US seems
>>> enough
>>> to me :-)
> It's a very good idea
>
>
>> ho, and I forgot : items() is now called keys() to be consistent with
>> dict interface :-)
> Good. It's better to have the same use than a dict like you have made (by
> adding some others methods too).
>
>
>
>>>>> Also, I don't really understand the can_instanciate method : some
>>>>> classes can be instanciated without New method, just with () (Ex:
>>>>> itk.Index[2]())
>>>> But some classes found like ImageToImageFilter aren't instanciable
>>>> because
>>>> they haven't the ::New method (in the C++ code). The can_instanciate
>>>> method
>>>> is to differenciate super classes and instanciable classes
>>>
>>> ok
>>> To be consistent with VTK, I have made classes callable (it delegates
>>> to
>>> New if New exists). Doing that unify classes with New method and
>>> classes
>>> without New method.
>>> As there is no way to know in a class is really callable, I'm not sure
>>> this method is really useful, or it should return True (when New
>>> method is
>>> avaible) or Perhaps if New is not avaible ;-)
> I think that can_instanciable isn't working in all cases. So I have
> removed it :-(
>
>
>
>
> I have simplified our solution :
> - Consider the itkVersion that is not a template class.
> We were using a liaison class (ItkClassWithoutType) that
> will offer all static methods accessible in itkVersion, like
> GetITKVersion.
> The wrapping creates the itkVersion_GetITKVersion method too.
> So, I have removed this liaison class.
> Thus, now, when I write itk.Version, I directly have the itkVersion
> class
> - Consider the itkImage that is a template class.
> I'm using the same deduction to remove the ItkClassType and direcly
> call the itk wrapped class.
> For example : itk.Image["US2"] returns the class
> 'itkImage_2D.itkImageUS2'
> - I keep the first level for template classe (ItkClass).
Good !
Why haven't I thought to it before ? ;-)
>
> Removing the ItkClassType class removes :
> - all addons that you have made (while the call of the New method).
> But it's always possible to add them directly to the wrapped itk
> class :-)
I can't live without it ;-) so I keep my modified New() method (see
attached code).
The problem is that I'm not able to do the same with classes without New()
method :
itk.BinaryBallStructuringElement.US2(Radius=3) don't work now :-(, and add
a New method to classes without it to be able to that don't seems to be a
nice idea :-/
Another problem : removing New method like in vtk is a good idea; we don't
need to know in python that we call a static method (New) or the real
constructor. Currently, I'm not able to customize __init__() method to do
that :-(
That's sad, but because of lacks of this feature, I'm still using my
module without improvement from yours.
> - the __name__ and __type__ variables added.
> But it's always possible to add or change them directly to the
> wrapped itk class :-)
> But I'm not sure that's a good idea to change the class name of
> wrapped itk classes.
> Moreover, not all itk classes will be changed (I think of itkXxxPtr
> or itkXxx_Pointer
> classes)
>
> But in an other hand, now, the ITK wrappers must generate the python
> code for the
> only python class ItkClass. That will simplify the realisation of our
> solution.
>
> Now, the last problem is always the mangled name of wrapped itk classes.
> If we had
> some consistent rules concerning the mangled name, our solution may work
> in all cases
> (by adding a variable on wrapped itk classes) :-)
> It will be nice that an ITK worker answers us on this point...
>
As I pointed out in a previous mail, it would be really easy to parse <C
itk::SmartPointer<(itk::MedianImageFilter<(itk::Image<(unsigned
short,3)>,itk::Image<(unsigned short,3)>)>)> instance at
_a088d208_p_itk__SmartPointerTitk__MedianImageFilterTitk__ImageTunsigned_short_3_t_itk__ImageTunsigned_short_3_t_t_t>
or
<C itk::Image<(unsigned short,3)> instance at
_4800c708_p_itk__ImageTunsigned_short_3_t>
but repr(itk.MedianImageFilter.US3US3) returns "<class
'itkMedianImageFilter.itkMedianImageFilterUS3US3'>" and not "<class
itk::MedianImageFilter<(itk::Image<(unsigned
short,3)>,itk::Image<(unsigned short,3)>)>>"
I hope it souldn't be too difficult to modify the __repr__ method :-)
If It's possible, the last problem will be with default type in template.
For example, itk.ImageFileReader[itk.Image[itk.US, 2]] is not
itk::ImageFileReader<(itk::Image<(unsigned short,2)>)>
but
itk::ImageFileReader<(itk::Image<(unsigned
short,2)>,itk::DefaultConvertPixelTraits<(unsigned short)>)>
I done some small changes :
verify that the key is really a template in __getitem__ : before that,
itk.MedianImageFilter['__dict__'] was returning the __dict__ attribute
remove imported modules to really have clean itk attributes. I suppose it
was not working in last code I posted.
rename __manage_digit to __manage_digit__ to really mask it
add a __flatList__ method to be able to use nested types (as it was done
in my module)
And some questions :
Do you really want to keep __call__ in ItkClass ? I was using it before
using the dict interface, but now, I think there is no more interest to
have it
is_template() methode do the same than has_key(). Do we really need it ?
--
Gaetan Lehmann <gaetan.lehmann at jouy.inra.fr>
Tel: +33 1 34 65 29 66
Biologie du Développement et de la Reproduction
INRA de Jouy-en-Josas (France)
Web: http://voxel.jouy.inra.fr
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itk.py
Type: application/octet-stream
Size: 12497 bytes
Desc: not available
Url : http://www.itk.org/mailman/private/insight-developers/attachments/20050617/7cb6a9d7/itk.obj
More information about the Insight-developers
mailing list