gdsfactory / kfactory

gdsfactory with a klayout backend
https://gdsfactory.github.io/kfactory/
MIT License
28 stars 10 forks source link

need `Trans.from_s` when loading port metadata from GDS #333

Closed gdspaul closed 1 month ago

gdspaul commented 1 month ago

Not sure if this is user error but I'll jot it down. I'm loading a GDS file with v1 metadata in kfactory 0.13.3, like this:

kf.config.meta_format = "v1"

l = kf.KCLayout("Layout")
l.read("something.gds", update_kcl_meta_data="overwrite")

for cell in l.each_cell():
    l[cell.name]

(Sebastian, I can send you the GDS if you like; it was written with kfactory 0.9.3.)

When I get down to l[cell.name] it will crash because somewhere inside there is a Trans being passed around as a string instead of as a Trans object. Around line 1839 of kcell.py, I can make the error go away:

                    if trans:
                        _port.trans = trans # kdb.Trans.from_s(trans) instead will fix it

and I believe this has to be corrected in 4 places in the module to cover all the bases (not sure).

Here's the stack trace I get.

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File [~/Documents/Projects/axon/venv/lib/python3.12/site-packages/kfactory/kcell.py:3490](http://localhost:8888/lab/tree/Projects/axon/~/Documents/Projects/axon/venv/lib/python3.12/site-packages/kfactory/kcell.py#line=3489), in KCLayout.__getitem__(self, obj)
   3489 try:
-> 3490     return self.kcells[self.layout.cell(obj).cell_index()]
   3491 except KeyError:

KeyError: 0

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[6], line 7
      4 l.read("ch4_pch.gds", update_kcl_meta_data="overwrite")
      6 for cell in l.each_cell():
----> 7     l[cell.name]
      9 # l is now loaded.

File [~/Documents/Projects/axon/venv/lib/python3.12/site-packages/kfactory/kcell.py:3494](http://localhost:8888/lab/tree/Projects/axon/~/Documents/Projects/axon/venv/lib/python3.12/site-packages/kfactory/kcell.py#line=3493), in KCLayout.__getitem__(self, obj)
   3492     kdb_c = self.layout.cell(obj)
   3493     c = KCell(name=kdb_c.name, kcl=self, kdb_cell=self.layout.cell(obj))
-> 3494     c.get_meta_data()
   3495     return c
   3496 except Exception as exc:

File [~/Documents/Projects/axon/venv/lib/python3.12/site-packages/kfactory/kcell.py:1840](http://localhost:8888/lab/tree/Projects/axon/~/Documents/Projects/axon/venv/lib/python3.12/site-packages/kfactory/kcell.py#line=1839), in KCell.get_meta_data(self, meta_format)
   1830 _port = Port(
   1831     name=name,
   1832     width=width,
   (...)
   1837     info=_d.get("info", {}),
   1838 )
   1839 if trans:
-> 1840     _port.trans = trans #kdb.Trans.from_s(trans)
   1841 elif dcplx_trans:
   1842     _port.dcplx_trans = dcplx_trans

File [~/Documents/Projects/axon/venv/lib/python3.12/site-packages/kfactory/kcell.py:5101](http://localhost:8888/lab/tree/Projects/axon/~/Documents/Projects/axon/venv/lib/python3.12/site-packages/kfactory/kcell.py#line=5100), in Port.trans(self, value)
   5099 @trans.setter
   5100 def trans(self, value: kdb.Trans) -> None:
-> 5101     self._trans = value.dup()
   5102     self._dcplx_trans = None

AttributeError: 'str' object has no attribute 'dup'
sebastian-goeldi commented 1 month ago

Hi Paul.

Thanks for the bug report. I believe I know what the issue is (I wrote this v1/v2 switch after migrating to v2 and only noticed later that there might be new gds file which can't be loaded again due to there being no way to indicate that we want to load an old format, so I likely really just forgot to make the proper switch in the loader)

I'll write a fix for it and ping you if I either implemented it or I need the old gds ^^