Skip to content

Difftest

difftest

GDS regression test. Inspired by lytest.

diff

diff(
    ref_file: PathType,
    run_file: PathType,
    xor: bool = True,
    test_name: str = "",
    ignore_sliver_differences: bool = False,
    ignore_cell_name_differences: bool = False,
    ignore_label_differences: bool = False,
    show: bool = False,
    stagger: bool = True,
) -> bool

Returns True if files are different.

Prints differences and shows them in klayout.

Parameters:

Name Type Description Default
ref_file PathType

reference (old) file.

required
run_file PathType

run (new) file.

required
xor bool

runs xor on every layer between old and run files.

True
test_name str

prefix for the new cell.

''
ignore_sliver_differences bool

if True, ignores any sliver differences in the XOR result.

False
ignore_cell_name_differences bool

if True, ignores any cell name differences.

False
ignore_label_differences bool

if True, ignores any label differences when run in XOR mode.

False
show bool

shows diff in klayout.

False
stagger bool

if True, staggers the old/new/xor views. If False, all three are overlaid.

True
Source code in kfactory/utils/difftest.py
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
def diff(
    ref_file: PathType,
    run_file: PathType,
    xor: bool = True,
    test_name: str = "",
    ignore_sliver_differences: bool = False,
    ignore_cell_name_differences: bool = False,
    ignore_label_differences: bool = False,
    show: bool = False,
    stagger: bool = True,
) -> bool:
    """Returns True if files are different.

    Prints differences and shows them in klayout.

    Args:
        ref_file: reference (old) file.
        run_file: run (new) file.
        xor: runs xor on every layer between old and run files.
        test_name: prefix for the new cell.
        ignore_sliver_differences: if True, ignores any sliver differences in the
            XOR result.
        ignore_cell_name_differences: if True, ignores any cell name differences.
        ignore_label_differences: if True, ignores any label differences when run in
            XOR mode.
        show: shows diff in klayout.
        stagger: if True, staggers the old/new/xor views. If False, all three are
            overlaid.
    """
    old = read_top_cell(pathlib.Path(ref_file))
    new = read_top_cell(pathlib.Path(run_file))

    if old.kcl.dbu != new.kcl.dbu:
        raise ValueError(
            f"dbu is different in old {old.kcl.dbu} {ref_file!r} "
            f"and new {new.kcl.dbu} {run_file!r} files"
        )

    equivalent = True
    ld = kdb.LayoutDiff()

    a_regions: dict[int, kdb.Region] = {}
    a_texts: dict[int, kdb.Texts] = {}
    b_regions: dict[int, kdb.Region] = {}
    b_texts: dict[int, kdb.Texts] = {}

    def get_region(key: int, regions: dict[int, kdb.Region]) -> kdb.Region:
        if key not in regions:
            reg = kdb.Region()
            regions[key] = reg
            return reg
        return regions[key]

    def get_texts(key: int, texts_dict: dict[int, kdb.Texts]) -> kdb.Texts:
        if key not in texts_dict:
            texts = kdb.Texts()
            texts_dict[key] = texts
            return texts
        return texts_dict[key]

    def polygon_diff_a(anotb: kdb.Polygon, prop_id: int) -> None:
        get_region(ld.layer_index_a(), a_regions).insert(anotb)

    def polygon_diff_b(bnota: kdb.Polygon, prop_id: int) -> None:
        get_region(ld.layer_index_b(), b_regions).insert(bnota)

    def cell_diff_a(cell: kdb.Cell) -> None:
        nonlocal equivalent
        print(f"{cell.name} only in old")
        if not ignore_cell_name_differences:
            equivalent = False

    def cell_diff_b(cell: kdb.Cell) -> None:
        nonlocal equivalent
        print(f"{cell.name} only in new")
        if not ignore_cell_name_differences:
            equivalent = False

    def text_diff_a(anotb: kdb.Text, prop_id: int) -> None:
        print("Text only in old")
        get_texts(ld.layer_index_a(), a_texts).insert(anotb)

    def text_diff_b(bnota: kdb.Text, prop_id: int) -> None:
        print("Text only in new")
        get_texts(ld.layer_index_b(), b_texts).insert(bnota)

    ld.on_cell_in_a_only = cell_diff_a  # ty:ignore[invalid-assignment]
    ld.on_cell_in_b_only = cell_diff_b  # ty:ignore[invalid-assignment]
    ld.on_text_in_a_only = text_diff_a  # ty:ignore[invalid-assignment]
    ld.on_text_in_b_only = text_diff_b  # ty:ignore[invalid-assignment]

    ld.on_polygon_in_a_only = polygon_diff_a  # type: ignore[assignment]  # ty:ignore[invalid-assignment]
    ld.on_polygon_in_b_only = polygon_diff_b  # type: ignore[assignment]  # ty:ignore[invalid-assignment]

    if ignore_cell_name_differences:
        ld.on_cell_name_differs = lambda anotb: print(f"cell name differs {anotb.name}")  # ty:ignore[invalid-assignment]
        equal = ld.compare(
            old.kdb_cell,
            new.kdb_cell,
            kdb.LayoutDiff.SmartCellMapping | kdb.LayoutDiff.Verbose,
            1,
        )
    else:
        equal = ld.compare(old.kdb_cell, new.kdb_cell, kdb.LayoutDiff.Verbose, 1)

    if not ignore_label_differences and (a_texts or b_texts):
        equivalent = False

    if not equal:
        c = DKCell(name=f"{test_name}_difftest")
        ref = old
        run = new

        old = DKCell(name=f"{test_name}_old")
        new = DKCell(name=f"{test_name}_new")

        old.copy_tree(ref.kdb_cell)
        new.copy_tree(run.kdb_cell)

        old.name = f"{test_name}_old"
        new.name = f"{test_name}_new"

        old_ref = c << old
        new_ref = c << new

        dy = 10
        if stagger:
            old_ref.movey(+old.ysize + dy)
            new_ref.movey(-old.ysize - dy)

        layer_label = kf.kcl.layout.layer(1, 0)
        c.shapes(layer_label).insert(kf.kdb.DText("old", old_ref.dtrans))
        c.shapes(layer_label).insert(kf.kdb.DText("new", new_ref.dtrans))
        c.shapes(layer_label).insert(
            kf.kdb.DText(
                "xor", kf.kdb.DTrans(new_ref.xmin, old_ref.ymax - old_ref.ysize - dy)
            )
        )

        if xor:
            print("Running XOR on differences...")
            # assume equivalence until we find XOR differences,
            # determined significant by the settings
            diff = DKCell(name=f"{test_name}_xor")

            for layer in c.kcl.layer_infos():
                # exists in both
                if (
                    new.kcl.layout.layer(layer) is not None
                    and old.kcl.layout.layer(layer) is not None
                ):
                    layer_ref = old.layer(layer)
                    layer_run = new.layer(layer)

                    region_run = kdb.Region(new.begin_shapes_rec(layer_run))
                    region_ref = kdb.Region(old.begin_shapes_rec(layer_ref))
                    region_diff = region_run ^ region_ref

                    if not region_diff.is_empty():
                        layer_id = c.layer(layer)
                        region_xor = region_ref ^ region_run
                        diff.shapes(layer_id).insert(region_xor)
                        xor_w_tolerance = region_xor.sized(-1)
                        is_sliver = xor_w_tolerance.is_empty()
                        message = f"{test_name}: XOR difference on layer {layer}"
                        if is_sliver:
                            message += " (sliver)"
                            if not ignore_sliver_differences:
                                equivalent = False
                        else:
                            equivalent = False
                        print(message)
                # only in new
                elif new.kcl.layout.layer(layer) is not None:
                    layer_id = new.layer(layer)
                    region = kdb.Region(new.begin_shapes_rec(layer_id))
                    diff.shapes(c.kcl.layer(layer)).insert(region)
                    print(f"{test_name}: layer {layer} only exists in updated cell")
                    equivalent = False

                # only in old
                elif old.kcl.layout.layer(layer) is not None:
                    layer_id = old.layer(layer)
                    region = kdb.Region(old.begin_shapes_rec(layer_id))
                    diff.shapes(c.kcl.layer(layer)).insert(region)
                    print(f"{test_name}: layer {layer} missing from updated cell")
                    equivalent = False

            _ = c << diff
            if equivalent:
                print("No significant XOR differences between layouts!")
        else:
            # if no additional xor verification, the two files are not equivalent
            equivalent = False

        if show and not equivalent:
            c.show()
        return not equivalent
    return False

difftest

difftest(
    component: TKCell,
    dirpath: Path,
    dirpath_run: Path,
    test_name: str | None = None,
    xor: bool = True,
    ignore_sliver_differences: bool = False,
    ignore_cell_name_differences: bool = False,
    ignore_label_differences: bool = False,
) -> None

Avoids GDS regressions tests on the GeometryDifferenceError.

If files are the same it returns None. If files are different runs XOR between new component and the GDS reference stored in dirpath and raises GeometryDifferenceError if there are differences and show differences in KLayout.

If it runs for the fist time it just stores the GDS reference.

Parameters:

Name Type Description Default
component TKCell

to test if it has changed.

required
test_name str | None

used to store the GDS file.

None
dirpath Path

directory where reference files are stored.

required
xor bool

runs XOR.

True
dirpath_run Path

directory to store gds file generated by the test.

required
ignore_sliver_differences bool

if True, ignores any sliver differences in the XOR result.

False
ignore_cell_name_differences bool

if True, ignores any cell name differences.

False
ignore_label_differences bool

if True, ignores any label differences when run in XOR mode.

False
Source code in kfactory/utils/difftest.py
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
def difftest(
    component: TKCell,
    dirpath: pathlib.Path,
    dirpath_run: pathlib.Path,
    test_name: str | None = None,
    xor: bool = True,
    ignore_sliver_differences: bool = False,
    ignore_cell_name_differences: bool = False,
    ignore_label_differences: bool = False,
) -> None:
    """Avoids GDS regressions tests on the GeometryDifferenceError.

    If files are the same it returns None. If files are different runs XOR
    between new component and the GDS reference stored in dirpath and
    raises GeometryDifferenceError if there are differences and show differences
    in KLayout.

    If it runs for the fist time it just stores the GDS reference.

    Args:
        component: to test if it has changed.
        test_name: used to store the GDS file.
        dirpath: directory where reference files are stored.
        xor: runs XOR.
        dirpath_run: directory to store gds file generated by the test.
        ignore_sliver_differences: if True, ignores any sliver differences
            in the XOR result.
        ignore_cell_name_differences: if True, ignores any cell name differences.
        ignore_label_differences: if True, ignores any label differences when run
            in XOR mode.

    """
    test_name = test_name or (
        f"{component.function_name}_{component.name}"
        if hasattr(component, "function_name")
        and component.name != component.function_name
        else f"{component.name}"
    )
    dirpath_ref = dirpath
    dirpath_ref.mkdir(exist_ok=True, parents=True)
    dirpath_run.mkdir(exist_ok=True, parents=True)

    ref_file = dirpath_ref / f"{test_name}.gds"
    run_file = dirpath_run / f"{test_name}.gds"

    component.write(run_file)

    if not ref_file.exists():
        shutil.copy(run_file, ref_file)
        raise AssertionError(
            f"Reference GDS file for {test_name!r} not found. Writing to {ref_file!r}"
        )

    if filecmp.cmp(ref_file, run_file, shallow=False):
        return

    if diff(
        ref_file=ref_file,
        run_file=run_file,
        xor=xor,
        test_name=test_name,
        ignore_sliver_differences=ignore_sliver_differences,
        ignore_cell_name_differences=ignore_cell_name_differences,
        ignore_label_differences=ignore_label_differences,
        show=True,
    ):
        print(
            f"\ngds_run {test_name!r} changed from gds_ref {str(ref_file)!r}\n"
            "You can check the differences in Klayout GUI or run XOR with\n"
            f"gf gds-diff --xor {ref_file} {run_file}\n"
        )
        try:
            overwrite(ref_file, run_file)
        except OSError as exc:
            raise GeometryDifferenceError(
                "\n"
                f"{test_name!r} changed from reference {str(ref_file)!r}. "
                "Run `pytest -s` to step and check differences in klayout GUI."
            ) from exc

xor

xor(
    old: KCLayout,
    new: KCLayout,
    test_name: str = "",
    ignore_sliver_differences: bool = False,
    ignore_cell_name_differences: bool = False,
    ignore_label_differences: bool = False,
    stagger: bool = True,
) -> DKCell

Returns XOR of two layouts.

Parameters:

Name Type Description Default
old KCLayout

reference layout.

required
new KCLayout

run layout.

required
test_name str

prefix for the new cell.

''
ignore_sliver_differences bool

if True, ignores any sliver differences in the XOR result.

False
ignore_cell_name_differences bool

if True, ignores any cell name differences.

False
ignore_label_differences bool

if True, ignores any label differences when run in XOR mode.

False
stagger bool

if True, staggers the old/new/xor views. If False, all three are overlaid.

True
Source code in kfactory/utils/difftest.py
 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
def xor(
    old: KCLayout,
    new: KCLayout,
    test_name: str = "",
    ignore_sliver_differences: bool = False,
    ignore_cell_name_differences: bool = False,
    ignore_label_differences: bool = False,
    stagger: bool = True,
) -> DKCell:
    """Returns XOR of two layouts.

    Args:
        old: reference layout.
        new: run layout.
        test_name: prefix for the new cell.
        ignore_sliver_differences: if True, ignores any sliver differences in the
            XOR result.
        ignore_cell_name_differences: if True, ignores any cell name differences.
        ignore_label_differences: if True, ignores any label differences when run in
            XOR mode.
        stagger: if True, staggers the old/new/xor views.
            If False, all three are overlaid.
    """
    if old.kcl.dbu != new.kcl.dbu:
        raise ValueError(
            f"dbu is different in old {old.kcl.dbu} and new {new.kcl.dbu} cells"
        )

    equivalent = True
    ld = kdb.LayoutDiff()

    a_regions: dict[int, kdb.Region] = {}
    a_texts: dict[int, kdb.Texts] = {}
    b_regions: dict[int, kdb.Region] = {}
    b_texts: dict[int, kdb.Texts] = {}

    def get_region(key: int, regions: dict[int, kdb.Region]) -> kdb.Region:
        if key not in regions:
            reg = kdb.Region()
            regions[key] = reg
            return reg
        return regions[key]

    def get_texts(key: int, texts_dict: dict[int, kdb.Texts]) -> kdb.Texts:
        if key not in texts_dict:
            texts = kdb.Texts()
            texts_dict[key] = texts
            return texts
        return texts_dict[key]

    def polygon_diff_a(anotb: kdb.Polygon, prop_id: int) -> None:
        get_region(ld.layer_index_a(), a_regions).insert(anotb)

    def polygon_diff_b(bnota: kdb.Polygon, prop_id: int) -> None:
        get_region(ld.layer_index_b(), b_regions).insert(bnota)

    def cell_diff_a(cell: kdb.Cell) -> None:
        nonlocal equivalent
        print(f"{cell.name} only in old")
        if not ignore_cell_name_differences:
            equivalent = False

    def cell_diff_b(cell: kdb.Cell) -> None:
        nonlocal equivalent
        print(f"{cell.name} only in new")
        if not ignore_cell_name_differences:
            equivalent = False

    def text_diff_a(anotb: kdb.Text, prop_id: int) -> None:
        print("Text only in old")
        get_texts(ld.layer_index_a(), a_texts).insert(anotb)

    def text_diff_b(bnota: kdb.Text, prop_id: int) -> None:
        print("Text only in new")
        get_texts(ld.layer_index_b(), b_texts).insert(bnota)

    ld.on_cell_in_a_only = cell_diff_a  # ty:ignore[invalid-assignment]
    ld.on_cell_in_b_only = cell_diff_b  # ty:ignore[invalid-assignment]
    ld.on_text_in_a_only = text_diff_a  # ty:ignore[invalid-assignment]
    ld.on_text_in_b_only = text_diff_b  # ty:ignore[invalid-assignment]

    ld.on_polygon_in_a_only = polygon_diff_a  # ty:ignore[invalid-assignment]
    ld.on_polygon_in_b_only = polygon_diff_b  # ty:ignore[invalid-assignment]

    if ignore_cell_name_differences:
        ld.on_cell_name_differs = lambda anotb: print(f"cell name differs {anotb.name}")  # ty:ignore[invalid-assignment]
        equal = ld.compare(
            old.kdb_cell,
            new.kdb_cell,
            kdb.LayoutDiff.SmartCellMapping | kdb.LayoutDiff.Verbose,
            1,
        )
    else:
        equal = ld.compare(old.kdb_cell, new.kdb_cell, kdb.LayoutDiff.Verbose, 1)

    if not ignore_label_differences and (a_texts or b_texts):
        equivalent = False
    if equal:
        return DKCell(name="xor_empty")
    c = DKCell(name=f"{test_name}_difftest")
    ref = old
    run = new

    old_kcell = DKCell(name=f"{test_name}_old")
    new_kcell = DKCell(name=f"{test_name}_new")

    old_kcell.copy_tree(ref.kdb_cell)
    new_kcell.copy_tree(run.kdb_cell)

    old_kcell.name = f"{test_name}_old"
    new_kcell.name = f"{test_name}_new"

    old_ref = c << old_kcell
    new_ref = c << new_kcell

    dy = 10
    if stagger:
        old_ref.movey(+old_kcell.ysize + dy)
        new_ref.movey(-old_kcell.ysize - dy)

    layer_label = kf.kcl.layout.layer(1, 0)
    c.shapes(layer_label).insert(kf.kdb.DText("old", old_ref.dtrans))
    c.shapes(layer_label).insert(kf.kdb.DText("new", new_ref.dtrans))
    c.shapes(layer_label).insert(
        kf.kdb.DText(
            "xor", kf.kdb.DTrans(new_ref.xmin, old_ref.ymax - old_ref.ysize - dy)
        )
    )

    print("Running XOR on differences...")
    # assume equivalence until we find XOR differences,
    # determined significant by the settings
    diff = DKCell(name=f"{test_name}_xor")

    for layer in c.kcl.layer_infos():
        # exists in both
        if (
            new_kcell.kcl.layout.layer(layer) is not None
            and old_kcell.kcl.layout.layer(layer) is not None
        ):
            layer_ref = old_kcell.layer(layer)
            layer_run = new_kcell.layer(layer)

            region_run = kdb.Region(new_kcell.begin_shapes_rec(layer_run))
            region_ref = kdb.Region(old_kcell.begin_shapes_rec(layer_ref))
            region_diff = region_run ^ region_ref

            if not region_diff.is_empty():
                layer_id = c.layer(layer)
                region_xor = region_ref ^ region_run
                diff.shapes(layer_id).insert(region_xor)
                xor_w_tolerance = region_xor.sized(-1)
                is_sliver = xor_w_tolerance.is_empty()
                message = f"{test_name}: XOR difference on layer {layer}"
                if is_sliver:
                    message += " (sliver)"
                    if not ignore_sliver_differences:
                        equivalent = False
                else:
                    equivalent = False
                print(message)
        # only in new
        elif new_kcell.kcl.layout.layer(layer) is not None:
            layer_id = new_kcell.layer(layer)
            region = kdb.Region(new_kcell.begin_shapes_rec(layer_id))
            diff.shapes(c.kcl.layer(layer)).insert(region)
            print(f"{test_name}: layer {layer} only exists in updated cell")
            equivalent = False

        # only in old
        elif old_kcell.kcl.layout.layer(layer) is not None:
            layer_id = old_kcell.layer(layer)
            region = kdb.Region(old_kcell.begin_shapes_rec(layer_id))
            diff.shapes(c.kcl.layer(layer)).insert(region)
            print(f"{test_name}: layer {layer} missing from updated cell")
            equivalent = False

        _ = c << diff
    return c