Package: lygadgets
klayout-gadgetsTools to make klayout, the standalone, and python environments work better together.Particular focus on script-based layout of integrated circuits. There are some python-driven scripters and others that use the klayout language. Both can use tools to get certain simple information about klayout. The problem is that their environments and even execution engines are extremely different.The purpose of this package is to provide simple klayout-related tools in a way that is highly robust to the environment in which it is being interpreted.No import errors when you are trying to test/debug a pure python aspect of an otherwise klayout GSI script.No inconsistencies between GSI and system namespace.InstallationVocabularyFeaturesScope linkingCell translationEnvironmentUpcoming features and ideasInstallationStep 1: install the python packageFrom PyPIpipinstalllygadgetsor from sourcegitclonegit@github.com:atait/klayout-gadgets.git
pipinstallklayout-gadgetsStep 2: link to klayoutYou have a new terminal script that can link all kinds of stuff to the proper places in the klayout configuration directories.lygadgets_linklygadgetsWarning for OSX Catalina:lygadgetsrequires a klayout environment with python >3.6. A suitable klayout binary that works with OSX Catalina and python 3 has not been released as of 10/25/19.Definition of termslypackage (or salt package): handled by the klayout Package Manager. It includes agrain.xml. These can define macros, DRC, python packages, etc.salt package: A lypackage that is published and available through the klayout Package Manager.pypackage: python package included as part of a lypackage.system python: the interpreter that runs when you typepython xxx.pyin the command line.system namespace: the modules that are visisble to system pythonGSI (generic scripting interface): an interpreter than can run python and Ruby within the special namespace available to klayout.GSI/klayout namespacemodules visible in GSI. This includes regular python builtins, Pypackages located in salt packages. Andsometimesthe system namespace, but only if you launch klayout from the command line.GUI mode: running scripts when there an open application window. In this mode, GSI is the primary interpreter.FeaturesLinking all sorts of things into klayout scopeSee the "examples" directory for more detailed discussion and demonstration.lygadgets_linksymlinks stuff (pypackages, technologies, salt packages) to the right places, just as if you had downloaded them through salt. Symlinking is especially useful if you have git project that changes, and you want changes to be immediately reflected in the klayout application.Note on Windows: symlinking requires running as administratorThese things can be linkedlypackages (i.e. klayout_dot_config)python packagespython modulesklayout technologies, either .lyt file or enclosing tech directorymacros: ruby or pythonlygadgets_linkhas flags--forceto overwrite anything that is currently there. However, you won't have to do this often because links are symbolic, meaning they update as you update the file/directory. The--copyflag lets you make static copies instead of links.Linking python packagesRight afterpip install mypack, runlygadgets_link mypack, and it will show up to klayout's GSI. This creates a symlink from source files to the~/.klayout/pythondirectory.As of v0.1.25, this command can also trigger linking of other dependencies. The trigger list is a package attribute called__lygadget_link__, defined in__init__. Here is where you put pip dependencies:# setup.pysetup(name='lygadgets',...install_requires=['future','xmltodict'],)Now, wealsoput them in__init__.py.# lygadgets/__init__.py__version__='0.1.25'__lygadget_link__=['future','xmltodict']Some packages that require lygadgets linking:lyipclytestlymaskLinking technologiesThis usually goes likelygadgets_linkSOEN-PDK/tech/OLMACand then you will be able to access all the features of that technology (layers, pcells, properties, ...) within system python or GSI like thisfromlygadgetsimportpyathe_tech=pya.Technology.technology_by_name('OLMAC')Cell translationSee "tests/translation_test.py" for demonstrations.There are at least ten python-based layout languages targeted for photonics. I have no idea how many there are for electronics. None of them are compatible. What some folks do is translation by-hand followed by rigorous regression testing. This might work for a PDK with 10's of really simple PCells. But imagine an actual, real codebase that has 1,000's of PCells, sometimes embedded within a hierarchy of other PCells. The permutations of parameters is staggering. Regression testing will not work*.The solution offered here is to take advantage of the one universal layout specification: GDS. An example call would be# Setup a klayout cellpya_layout=pya.Layout()pya_cell=pya_layout.create_cell('newname')# Make a phidl devicephidl_device=some_device(10,20)# Translatelygadgets.anyCell_to_anyCell(init_device,pya_cell)Under the hood, lygadgets is taking the phidl Device, writing it to GDS, loading that GDS intopya_cell, then deleting the GDS. In the future, I hope to add support for IPKISS, nazca, gdspy, and maybe others on request.*Geometric regression testing is useful. Seelytestfor that. It's practically necessary for large codebases being modified by multiple people. "Multiple people" can include you and yourself in the future who will have forgotten everything.PCell translationThis is handled inlygadgets.autolibrary.WrappedPCell. Example call:fromlygadgets.autolibraryimportWrappedPCellfromolmac_pcellsimportwg_to_snspdpya_layout=pya.Layout()WgToSNSPD=WrappedPCell(wg_to_snspd)# You now have a pya PCell. You can put it in your layout multiple times with different parameterswg_cell1,wg_ports1=WgToSNSPD('My_SNSPD').pcell(pya_layout,params={'wgnw_length':200})wg_cell2,wg_ports2=WgToSNSPD('My_SNSPD').pcell(pya_layout,params={'wgnw_length':400})This translation can also go from klayout -> phidl.Make your non-klayout PCells available for GUI layoutWe can now convert any PCell into a klayout PCell. The klayout GUI has a decent interface for inserting PCells interactively.# Macro: klayout_Library.lym. Find it in SOEN-PDKfromolmac_pcellsimportwg_to_snspd,htronclassOLMAC_Library(lygadgets.WrappedLibrary):tech_name="OLMAC"all_funcs_to_wrap=[wg_to_snspd,htron]description="NIST SOEN"OLMAC_Library()# This registers it with the GUI and the GSI(This is still a little buggy).EnvironmentSee the "examples" directory for more detailed discussion and demonstration.Detects the interpreter in which code is being run. Provides apyathat is safe to import. In system interpreter, this will breakimportpyabut this will never break:fromlygadgetsimportpyahowever, of course, you then cannot try to use the GUI features ofpya. You can't use it at all if you do not have the klayout standalone:pip install klayout.If you have the klayout python standalone, that is what you will get as "pya". Then, its layout database features will be available, just like the regular GSI version ofpyain batch mode. In GSI mode, lygadgets gives the GSI pya so as not disrupt things.More aggressive:patch_environmentGSI pya contains more than the standalone klayout.db. Your exciting python scripts that were run usingklayout -r script.pyand all of its dependencies that auto-run a whole bunch of stuff in their__init__.pys -- all of that stuff contains references to pya and GUI features of pya (Example: SiEPIC_Tools). So you definitely cannot dopython script.py.The commandlygadgets.patch_environment()solves this problem by hijacking the way python finds packages. Just put it at the very top ofscript.py, after importing lygadgets. Now, you no longer need to dofrom lygadgets import pya. GUI calls will be stifled. The downside is that this aggressive approach is less likely to work cross-platform. It is still being debugged on Windows and Anaconda.Backwards compatibility with GSI is the top priority ofpatch_environment. If it detects the GSI, it will do nothing.Upcoming features and ideasdetect version of lypackages and pypackages to determine whether or not to force link updatePort translationThings that lygadgets does not dono non-standard required dependencies (only future and xmltodict)no reference to particular technologies (e.g. OLMAC) or specific types of properties (e.g. "WAVEGUIDES.xml"), except for the sake of documentaion/illustrationno callingsubprocess.call('klayout -r foo.py')no phidl in the implementation.klayout.dbis allowed if it speeds it up, but it cannot be requiredAuthors: Alex Tait, Adam McCaughan, Sonia Buckley, Jeff Chiles, Jeff Shainline, Rich Mirin, Sae Woo NamNational Institute of Standards and Technology, Boulder, CO, USA