1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
|
%global _empty_manifest_terminate_build 0
Name: python-lygadgets
Version: 0.1.35
Release: 1
Summary: Tools to make klayout, the standalone, and python environments work better together
License: MIT
URL: https://pypi.org/project/lygadgets/
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/18/28/a23e85710396549b5c9c0acee82c1bf1d9729312484a4460d36aa922f14f/lygadgets-0.1.35.tar.gz
BuildArch: noarch
Requires: python3-future
Requires: python3-xmltodict
%description
[](https://travis-ci.org/atait/klayout-gadgets)
[](https://pepy.tech/project/lygadgets)
# klayout-gadgets
Tools 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.

- [Installation](#installation)
- [Vocabulary](#definition-of-terms)
- [Features](#features)
* [Scope linking](#linking-all-sorts-of-things-into-klayout-scope)
* [Cell translation](#cell-translation)
* [Environment](#environment)
- [Upcoming features and ideas](#upcoming-features-and-ideas)
## Installation
### Step 1: install the python package
From PyPI
```bash
pip install lygadgets
```
or from source
```bash
git clone git@github.com:atait/klayout-gadgets.git
pip install klayout-gadgets
```
### Step 2: link to klayout
You have a new terminal script that can link all kinds of stuff to the proper places in the klayout configuration directories.
```bash
lygadgets_link lygadgets
```
*Warning for OSX Catalina*: `lygadgets` requires 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 terms
**lypackage (or salt package)**: handled by the klayout Package Manager. It includes a `grain.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 type `python xxx.py` in the command line.
**system namespace**: the modules that are visisble to system python
**GSI (generic scripting interface)**: an interpreter than can run python and Ruby within the special namespace available to klayout.
**GSI/klayout namespace** modules visible in GSI. This includes regular python builtins, Pypackages located in salt packages. And *sometimes* the 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.
## Features
### Linking all sorts of things into klayout scope
See the "examples" directory for more detailed discussion and demonstration.
`lygadgets_link` symlinks 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 administrator*
These things can be linked
- lypackages (i.e. klayout_dot_config)
- python packages
- python modules
- klayout technologies, either .lyt file or enclosing tech directory
- macros: ruby or python
`lygadgets_link` has flags `--force` to 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 `--copy` flag lets you make static copies instead of links.
#### Linking python packages
Right after `pip install mypack`, run `lygadgets_link mypack`, and it will show up to klayout's GSI. This creates a symlink from source files to the `~/.klayout/python` directory.
**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:
```python
# setup.py
setup(name='lygadgets',
...
install_requires=['future', 'xmltodict'],
)
```
Now, we *also* put them in `__init__.py`.
``` python
# lygadgets/__init__.py
__version__ = '0.1.25'
__lygadget_link__ = ['future', 'xmltodict']
```
Some packages that require lygadgets linking:
- [lyipc](https://github.com/atait/klayout-ipc)
- [lytest](https://github.com/atait/lytest)
- [lymask](https://github.com/atait/lymask)
#### Linking technologies
This usually goes like
```bash
lygadgets_link SOEN-PDK/tech/OLMAC
```
and then you will be able to access all the features of that technology (layers, pcells, properties, ...) within system python or GSI like this
```python
from lygadgets import pya
the_tech = pya.Technology.technology_by_name('OLMAC')
```
### Cell translation
See "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<sup>\*</sup>.
The solution offered here is to take advantage of the one universal layout specification: GDS. An example call would be
```python
# Setup a klayout cell
pya_layout = pya.Layout()
pya_cell = pya_layout.create_cell('newname')
# Make a phidl device
phidl_device = some_device(10, 20)
# Translate
lygadgets.anyCell_to_anyCell(init_device, pya_cell)
```
Under the hood, lygadgets is taking the phidl Device, writing it to GDS, loading that GDS into `pya_cell`, then deleting the GDS. In the future, I hope to add support for IPKISS, nazca, gdspy, and maybe others on request.
<sup>\*</sup>Geometric regression testing is useful. See [lytest](https://github.com/atait/lytest) for 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 translation
This is handled in `lygadgets.autolibrary.WrappedPCell`. Example call:
```python
from lygadgets.autolibrary import WrappedPCell
from olmac_pcells import wg_to_snspd
pya_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 parameters
wg_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 layout
We can now convert any PCell into a klayout PCell. The klayout GUI has a decent interface for inserting PCells interactively.
```python
# Macro: klayout_Library.lym. Find it in SOEN-PDK
from olmac_pcells import wg_to_snspd, htron
class OLMAC_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).
### Environment
See the "examples" directory for more detailed discussion and demonstration.
Detects the interpreter in which code is being run. Provides a `pya` that is safe to import. In system interpreter, this will break
```python
import pya
```
but this will never break:
```python
from lygadgets import pya
```
however, of course, you then cannot try to use the GUI features of `pya`. 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 of `pya` in batch mode. In GSI mode, lygadgets gives the GSI pya so as not disrupt things.
#### More aggressive: `patch_environment`
GSI pya contains more than the standalone klayout.db. Your exciting python scripts that were run using `klayout -r script.py` and all of its dependencies that auto-run a whole bunch of stuff in their `__init__.py`s -- all of that stuff contains references to pya and GUI features of pya (Example: SiEPIC_Tools). So you definitely cannot do `python script.py`.
The command `lygadgets.patch_environment()` solves this problem by hijacking the way python finds packages. Just put it at the very top of `script.py`, after importing lygadgets. Now, you no longer need to do `from 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 of `patch_environment`. If it detects the GSI, it will do nothing.
## Upcoming features and ideas
- detect version of lypackages and pypackages to determine whether or not to force link update
- Port translation
## Things that lygadgets does not do
- no 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/illustration
- no calling `subprocess.call('klayout -r foo.py')`
- no phidl in the implementation.
- `klayout.db` is allowed if it speeds it up, but it cannot be required
#### Authors: Alex Tait, Adam McCaughan, Sonia Buckley, Jeff Chiles, Jeff Shainline, Rich Mirin, Sae Woo Nam
#### National Institute of Standards and Technology, Boulder, CO, USA
%package -n python3-lygadgets
Summary: Tools to make klayout, the standalone, and python environments work better together
Provides: python-lygadgets
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-lygadgets
[](https://travis-ci.org/atait/klayout-gadgets)
[](https://pepy.tech/project/lygadgets)
# klayout-gadgets
Tools 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.

- [Installation](#installation)
- [Vocabulary](#definition-of-terms)
- [Features](#features)
* [Scope linking](#linking-all-sorts-of-things-into-klayout-scope)
* [Cell translation](#cell-translation)
* [Environment](#environment)
- [Upcoming features and ideas](#upcoming-features-and-ideas)
## Installation
### Step 1: install the python package
From PyPI
```bash
pip install lygadgets
```
or from source
```bash
git clone git@github.com:atait/klayout-gadgets.git
pip install klayout-gadgets
```
### Step 2: link to klayout
You have a new terminal script that can link all kinds of stuff to the proper places in the klayout configuration directories.
```bash
lygadgets_link lygadgets
```
*Warning for OSX Catalina*: `lygadgets` requires 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 terms
**lypackage (or salt package)**: handled by the klayout Package Manager. It includes a `grain.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 type `python xxx.py` in the command line.
**system namespace**: the modules that are visisble to system python
**GSI (generic scripting interface)**: an interpreter than can run python and Ruby within the special namespace available to klayout.
**GSI/klayout namespace** modules visible in GSI. This includes regular python builtins, Pypackages located in salt packages. And *sometimes* the 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.
## Features
### Linking all sorts of things into klayout scope
See the "examples" directory for more detailed discussion and demonstration.
`lygadgets_link` symlinks 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 administrator*
These things can be linked
- lypackages (i.e. klayout_dot_config)
- python packages
- python modules
- klayout technologies, either .lyt file or enclosing tech directory
- macros: ruby or python
`lygadgets_link` has flags `--force` to 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 `--copy` flag lets you make static copies instead of links.
#### Linking python packages
Right after `pip install mypack`, run `lygadgets_link mypack`, and it will show up to klayout's GSI. This creates a symlink from source files to the `~/.klayout/python` directory.
**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:
```python
# setup.py
setup(name='lygadgets',
...
install_requires=['future', 'xmltodict'],
)
```
Now, we *also* put them in `__init__.py`.
``` python
# lygadgets/__init__.py
__version__ = '0.1.25'
__lygadget_link__ = ['future', 'xmltodict']
```
Some packages that require lygadgets linking:
- [lyipc](https://github.com/atait/klayout-ipc)
- [lytest](https://github.com/atait/lytest)
- [lymask](https://github.com/atait/lymask)
#### Linking technologies
This usually goes like
```bash
lygadgets_link SOEN-PDK/tech/OLMAC
```
and then you will be able to access all the features of that technology (layers, pcells, properties, ...) within system python or GSI like this
```python
from lygadgets import pya
the_tech = pya.Technology.technology_by_name('OLMAC')
```
### Cell translation
See "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<sup>\*</sup>.
The solution offered here is to take advantage of the one universal layout specification: GDS. An example call would be
```python
# Setup a klayout cell
pya_layout = pya.Layout()
pya_cell = pya_layout.create_cell('newname')
# Make a phidl device
phidl_device = some_device(10, 20)
# Translate
lygadgets.anyCell_to_anyCell(init_device, pya_cell)
```
Under the hood, lygadgets is taking the phidl Device, writing it to GDS, loading that GDS into `pya_cell`, then deleting the GDS. In the future, I hope to add support for IPKISS, nazca, gdspy, and maybe others on request.
<sup>\*</sup>Geometric regression testing is useful. See [lytest](https://github.com/atait/lytest) for 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 translation
This is handled in `lygadgets.autolibrary.WrappedPCell`. Example call:
```python
from lygadgets.autolibrary import WrappedPCell
from olmac_pcells import wg_to_snspd
pya_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 parameters
wg_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 layout
We can now convert any PCell into a klayout PCell. The klayout GUI has a decent interface for inserting PCells interactively.
```python
# Macro: klayout_Library.lym. Find it in SOEN-PDK
from olmac_pcells import wg_to_snspd, htron
class OLMAC_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).
### Environment
See the "examples" directory for more detailed discussion and demonstration.
Detects the interpreter in which code is being run. Provides a `pya` that is safe to import. In system interpreter, this will break
```python
import pya
```
but this will never break:
```python
from lygadgets import pya
```
however, of course, you then cannot try to use the GUI features of `pya`. 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 of `pya` in batch mode. In GSI mode, lygadgets gives the GSI pya so as not disrupt things.
#### More aggressive: `patch_environment`
GSI pya contains more than the standalone klayout.db. Your exciting python scripts that were run using `klayout -r script.py` and all of its dependencies that auto-run a whole bunch of stuff in their `__init__.py`s -- all of that stuff contains references to pya and GUI features of pya (Example: SiEPIC_Tools). So you definitely cannot do `python script.py`.
The command `lygadgets.patch_environment()` solves this problem by hijacking the way python finds packages. Just put it at the very top of `script.py`, after importing lygadgets. Now, you no longer need to do `from 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 of `patch_environment`. If it detects the GSI, it will do nothing.
## Upcoming features and ideas
- detect version of lypackages and pypackages to determine whether or not to force link update
- Port translation
## Things that lygadgets does not do
- no 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/illustration
- no calling `subprocess.call('klayout -r foo.py')`
- no phidl in the implementation.
- `klayout.db` is allowed if it speeds it up, but it cannot be required
#### Authors: Alex Tait, Adam McCaughan, Sonia Buckley, Jeff Chiles, Jeff Shainline, Rich Mirin, Sae Woo Nam
#### National Institute of Standards and Technology, Boulder, CO, USA
%package help
Summary: Development documents and examples for lygadgets
Provides: python3-lygadgets-doc
%description help
[](https://travis-ci.org/atait/klayout-gadgets)
[](https://pepy.tech/project/lygadgets)
# klayout-gadgets
Tools 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.

- [Installation](#installation)
- [Vocabulary](#definition-of-terms)
- [Features](#features)
* [Scope linking](#linking-all-sorts-of-things-into-klayout-scope)
* [Cell translation](#cell-translation)
* [Environment](#environment)
- [Upcoming features and ideas](#upcoming-features-and-ideas)
## Installation
### Step 1: install the python package
From PyPI
```bash
pip install lygadgets
```
or from source
```bash
git clone git@github.com:atait/klayout-gadgets.git
pip install klayout-gadgets
```
### Step 2: link to klayout
You have a new terminal script that can link all kinds of stuff to the proper places in the klayout configuration directories.
```bash
lygadgets_link lygadgets
```
*Warning for OSX Catalina*: `lygadgets` requires 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 terms
**lypackage (or salt package)**: handled by the klayout Package Manager. It includes a `grain.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 type `python xxx.py` in the command line.
**system namespace**: the modules that are visisble to system python
**GSI (generic scripting interface)**: an interpreter than can run python and Ruby within the special namespace available to klayout.
**GSI/klayout namespace** modules visible in GSI. This includes regular python builtins, Pypackages located in salt packages. And *sometimes* the 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.
## Features
### Linking all sorts of things into klayout scope
See the "examples" directory for more detailed discussion and demonstration.
`lygadgets_link` symlinks 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 administrator*
These things can be linked
- lypackages (i.e. klayout_dot_config)
- python packages
- python modules
- klayout technologies, either .lyt file or enclosing tech directory
- macros: ruby or python
`lygadgets_link` has flags `--force` to 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 `--copy` flag lets you make static copies instead of links.
#### Linking python packages
Right after `pip install mypack`, run `lygadgets_link mypack`, and it will show up to klayout's GSI. This creates a symlink from source files to the `~/.klayout/python` directory.
**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:
```python
# setup.py
setup(name='lygadgets',
...
install_requires=['future', 'xmltodict'],
)
```
Now, we *also* put them in `__init__.py`.
``` python
# lygadgets/__init__.py
__version__ = '0.1.25'
__lygadget_link__ = ['future', 'xmltodict']
```
Some packages that require lygadgets linking:
- [lyipc](https://github.com/atait/klayout-ipc)
- [lytest](https://github.com/atait/lytest)
- [lymask](https://github.com/atait/lymask)
#### Linking technologies
This usually goes like
```bash
lygadgets_link SOEN-PDK/tech/OLMAC
```
and then you will be able to access all the features of that technology (layers, pcells, properties, ...) within system python or GSI like this
```python
from lygadgets import pya
the_tech = pya.Technology.technology_by_name('OLMAC')
```
### Cell translation
See "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<sup>\*</sup>.
The solution offered here is to take advantage of the one universal layout specification: GDS. An example call would be
```python
# Setup a klayout cell
pya_layout = pya.Layout()
pya_cell = pya_layout.create_cell('newname')
# Make a phidl device
phidl_device = some_device(10, 20)
# Translate
lygadgets.anyCell_to_anyCell(init_device, pya_cell)
```
Under the hood, lygadgets is taking the phidl Device, writing it to GDS, loading that GDS into `pya_cell`, then deleting the GDS. In the future, I hope to add support for IPKISS, nazca, gdspy, and maybe others on request.
<sup>\*</sup>Geometric regression testing is useful. See [lytest](https://github.com/atait/lytest) for 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 translation
This is handled in `lygadgets.autolibrary.WrappedPCell`. Example call:
```python
from lygadgets.autolibrary import WrappedPCell
from olmac_pcells import wg_to_snspd
pya_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 parameters
wg_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 layout
We can now convert any PCell into a klayout PCell. The klayout GUI has a decent interface for inserting PCells interactively.
```python
# Macro: klayout_Library.lym. Find it in SOEN-PDK
from olmac_pcells import wg_to_snspd, htron
class OLMAC_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).
### Environment
See the "examples" directory for more detailed discussion and demonstration.
Detects the interpreter in which code is being run. Provides a `pya` that is safe to import. In system interpreter, this will break
```python
import pya
```
but this will never break:
```python
from lygadgets import pya
```
however, of course, you then cannot try to use the GUI features of `pya`. 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 of `pya` in batch mode. In GSI mode, lygadgets gives the GSI pya so as not disrupt things.
#### More aggressive: `patch_environment`
GSI pya contains more than the standalone klayout.db. Your exciting python scripts that were run using `klayout -r script.py` and all of its dependencies that auto-run a whole bunch of stuff in their `__init__.py`s -- all of that stuff contains references to pya and GUI features of pya (Example: SiEPIC_Tools). So you definitely cannot do `python script.py`.
The command `lygadgets.patch_environment()` solves this problem by hijacking the way python finds packages. Just put it at the very top of `script.py`, after importing lygadgets. Now, you no longer need to do `from 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 of `patch_environment`. If it detects the GSI, it will do nothing.
## Upcoming features and ideas
- detect version of lypackages and pypackages to determine whether or not to force link update
- Port translation
## Things that lygadgets does not do
- no 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/illustration
- no calling `subprocess.call('klayout -r foo.py')`
- no phidl in the implementation.
- `klayout.db` is allowed if it speeds it up, but it cannot be required
#### Authors: Alex Tait, Adam McCaughan, Sonia Buckley, Jeff Chiles, Jeff Shainline, Rich Mirin, Sae Woo Nam
#### National Institute of Standards and Technology, Boulder, CO, USA
%prep
%autosetup -n lygadgets-0.1.35
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-lygadgets -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 0.1.35-1
- Package Spec generated
|