SixTrack / sixtracklib

Library for single charged particle simulations in accelerators
GNU Lesser General Public License v2.1
12 stars 16 forks source link

Error when printing out SpaceCharge #120

Closed aoeftiger closed 4 years ago

aoeftiger commented 4 years ago

When trying to print out the existing elements in a beam_elements.Elements instance including SpaceChargeBunched instances, then a TypeError is thrown. This does not happen for the same line without SpaceChargeBunched instances in the layout (here, elements.get_elements() just returns a list as expected).

The same applies to printing out SpaceChargeBunched elements...

>>> type(elements.get_elements()[0])
sixtracklib.beam_elements.SpaceChargeBunched

>>> elements.get_elements()[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    400                         if cls is not object \
    401                                 and callable(cls.__dict__.get('__repr__')):
--> 402                             return _repr_pprint(obj, self, cycle)
    403 
    404             return _default_pprint(obj, self, cycle)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    695     """A pprint that just redirects to the normal repr function."""
    696     # Find newlines and replace them with p.break_()
--> 697     output = repr(obj)
    698     for idx,output_line in enumerate(output.splitlines()):
    699         if idx:

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cobject.py in __repr__(self)
    198         out = [f"<{self.__class__.__name__} at {self._offset}"]
    199         for nn, ff in self._fields():
--> 200             out.append(f"  {nn}:{getattr(self,nn)}")
    201         out.append(">")
    202         return "\n".join(out)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cfield.py in __get__(self, obj, cls)
     50             return self
     51         else:
---> 52             return self._field_getter(obj)
     53     def __set__(self,obj,value):
     54         return self._field_setter(obj,value)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cfield.py in _field_getter(self, obj)
     35         fsize=obj._fsizes[self.index]
     36         length=obj._flength[self.index]
---> 37         return obj._buffer.get_field(offset,ftype,fsize,length)
     38     def _field_setter(self,obj,value):
     39         const = obj._fconst[self.index]

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cbuffer.py in get_field(self, offset, ftype, fsize, length)
    319                         for ii in range(length)]
    320             else:
--> 321                 return self._data[offset:offset+fsize].view(ftype)
    322 
    323     def set_field(self, offset, ftype, fsize, length, value):

TypeError: slice indices must be integers or None or have an __index__ method

or for the entire Elements list:

>>> elements
<sixtracklib.beam_elements.Elements at 0x7f9e1c22d310>

>>> elements.get_elements()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    383                 if cls in self.type_pprinters:
    384                     # printer registered in self.type_pprinters
--> 385                     return self.type_pprinters[cls](obj, self, cycle)
    386                 else:
    387                     # deferred printer

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in inner(obj, p, cycle)
    561                 p.text(',')
    562                 p.breakable()
--> 563             p.pretty(x)
    564         if len(obj) == 1 and type(obj) is tuple:
    565             # Special case for 1-item tuples.

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    400                         if cls is not object \
    401                                 and callable(cls.__dict__.get('__repr__')):
--> 402                             return _repr_pprint(obj, self, cycle)
    403 
    404             return _default_pprint(obj, self, cycle)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    695     """A pprint that just redirects to the normal repr function."""
    696     # Find newlines and replace them with p.break_()
--> 697     output = repr(obj)
    698     for idx,output_line in enumerate(output.splitlines()):
    699         if idx:

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cobject.py in __repr__(self)
    198         out = [f"<{self.__class__.__name__} at {self._offset}"]
    199         for nn, ff in self._fields():
--> 200             out.append(f"  {nn}:{getattr(self,nn)}")
    201         out.append(">")
    202         return "\n".join(out)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cfield.py in __get__(self, obj, cls)
     50             return self
     51         else:
---> 52             return self._field_getter(obj)
     53     def __set__(self,obj,value):
     54         return self._field_setter(obj,value)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cfield.py in _field_getter(self, obj)
     35         fsize=obj._fsizes[self.index]
     36         length=obj._flength[self.index]
---> 37         return obj._buffer.get_field(offset,ftype,fsize,length)
     38     def _field_setter(self,obj,value):
     39         const = obj._fconst[self.index]

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cbuffer.py in get_field(self, offset, ftype, fsize, length)
    319                         for ii in range(length)]
    320             else:
--> 321                 return self._data[offset:offset+fsize].view(ftype)
    322 
    323     def set_field(self, offset, ftype, fsize, length, value):

TypeError: slice indices must be integers or None or have an __index__ method
hannes-bartosik commented 4 years ago

Hi Adrian,

are you running the latest version of sixtracklib (from the official repository). we encountered a similar problem some time ago and fixed it …

cheers, Hannes

On 06 Jan 2020, at 15:03, Adrian Oeftiger notifications@github.com<mailto:notifications@github.com> wrote:

When trying to print out the existing elements in a beam_elements.Elements instance including SpaceChargeBunched instances, then a TypeError is thrown. This does not happen for the same line without SpaceChargeBunched instances in the layout (here, elements.get_elements() just returns a list as expected).

The same applies to printing out SpaceChargeBunched elements...

type(elements.get_elements()[0]) sixtracklib.beam_elements.SpaceChargeBunched

elements.get_elements()[0]

TypeError Traceback (most recent call last) ~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/core/formatters.py in call(self, obj) 700 type_pprinters=self.type_printers, 701 deferred_pprinters=self.deferred_printers) --> 702 printer.pretty(obj) 703 printer.flush() 704 return stream.getvalue()

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj) 400 if cls is not object \ 401 and callable(cls.dict.get('repr')): --> 402 return _repr_pprint(obj, self, cycle) 403 404 return _default_pprint(obj, self, cycle)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in _reprpprint(obj, p, cycle) 695 """A pprint that just redirects to the normal repr function.""" 696 # Find newlines and replace them with p.break() --> 697 output = repr(obj) 698 for idx,output_line in enumerate(output.splitlines()): 699 if idx:

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cobject.py in repr(self) 198 out = [f"<{self.class.name} at {self._offset}"] 199 for nn, ff in self._fields(): --> 200 out.append(f" {nn}:{getattr(self,nn)}") 201 out.append(">") 202 return "\n".join(out)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cfield.py in get(self, obj, cls) 50 return self 51 else: ---> 52 return self._field_getter(obj) 53 def set(self,obj,value): 54 return self._field_setter(obj,value)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cfield.py in _field_getter(self, obj) 35 fsize=obj._fsizes[self.index] 36 length=obj._flength[self.index] ---> 37 return obj._buffer.get_field(offset,ftype,fsize,length) 38 def _field_setter(self,obj,value): 39 const = obj._fconst[self.index]

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cbuffer.py in get_field(self, offset, ftype, fsize, length) 319 for ii in range(length)] 320 else: --> 321 return self._data[offset:offset+fsize].view(ftype) 322 323 def set_field(self, offset, ftype, fsize, length, value):

TypeError: slice indices must be integers or None or have an index method

or for the entire Elements list:

elements <sixtracklib.beam_elements.Elements at 0x7f9e1c22d310>

elements.get_elements()

TypeError Traceback (most recent call last) ~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/core/formatters.py in call(self, obj) 700 type_pprinters=self.type_printers, 701 deferred_pprinters=self.deferred_printers) --> 702 printer.pretty(obj) 703 printer.flush() 704 return stream.getvalue()

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj) 383 if cls in self.type_pprinters: 384 # printer registered in self.type_pprinters --> 385 return self.type_pprinters[cls](obj, self, cycle) 386 else: 387 # deferred printer

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in inner(obj, p, cycle) 561 p.text(',') 562 p.breakable() --> 563 p.pretty(x) 564 if len(obj) == 1 and type(obj) is tuple: 565 # Special case for 1-item tuples.

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj) 400 if cls is not object \ 401 and callable(cls.dict.get('repr')): --> 402 return _repr_pprint(obj, self, cycle) 403 404 return _default_pprint(obj, self, cycle)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/IPython/lib/pretty.py in _reprpprint(obj, p, cycle) 695 """A pprint that just redirects to the normal repr function.""" 696 # Find newlines and replace them with p.break() --> 697 output = repr(obj) 698 for idx,output_line in enumerate(output.splitlines()): 699 if idx:

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cobject.py in repr(self) 198 out = [f"<{self.class.name} at {self._offset}"] 199 for nn, ff in self._fields(): --> 200 out.append(f" {nn}:{getattr(self,nn)}") 201 out.append(">") 202 return "\n".join(out)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cfield.py in get(self, obj, cls) 50 return self 51 else: ---> 52 return self._field_getter(obj) 53 def set(self,obj,value): 54 return self._field_setter(obj,value)

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cfield.py in _field_getter(self, obj) 35 fsize=obj._fsizes[self.index] 36 length=obj._flength[self.index] ---> 37 return obj._buffer.get_field(offset,ftype,fsize,length) 38 def _field_setter(self,obj,value): 39 const = obj._fconst[self.index]

~/anaconda3/envs/stl_martin/lib/python3.7/site-packages/cobjects/cbuffer.py in get_field(self, offset, ftype, fsize, length) 319 for ii in range(length)] 320 else: --> 321 return self._data[offset:offset+fsize].view(ftype) 322 323 def set_field(self, offset, ftype, fsize, length, value):

TypeError: slice indices must be integers or None or have an index method

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/SixTrack/sixtracklib/issues/120?email_source=notifications&email_token=ABSXBZ7GJUJV55QGPWIGIG3Q4M22RA5CNFSM4KDEYSW2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IEHMBEA, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABSXBZ5BNEMDW3PBQ7BSKH3Q4M22RANCNFSM4KDEYSWQ.

aoeftiger commented 4 years ago

I'm running on the latest version of this repository, yes..

rdemaria commented 4 years ago

also cobjects ?

aoeftiger commented 4 years ago

as per pip install yes..?

~$ pip install cobjects
Requirement already satisfied: cobjects in ./anaconda3/lib/python3.7/site-packages (0.0.9)
Requirement already satisfied: cffi>=1.0.0 in ./anaconda3/lib/python3.7/site-packages (from cobjects) (1.12.3)
Requirement already satisfied: numpy in ./anaconda3/lib/python3.7/site-packages (from cobjects) (1.16.4)
Requirement already satisfied: pycparser in ./anaconda3/lib/python3.7/site-packages (from cffi>=1.0.0->cobjects) (2.19)
aoeftiger commented 4 years ago

I just see that v0.0.9 is not the latest version on pypi, github has a newer commit from Oct 30.. do you want to update the cobjects version on pypi?

aoeftiger commented 4 years ago

And indeed, the github version fixes this error. Thanks ;)