Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inconsistent results in width_check when calling by keyword argument (python) #1793

Closed
tvt173 opened this issue Jul 19, 2024 · 9 comments · Fixed by #1795
Closed

inconsistent results in width_check when calling by keyword argument (python) #1793

tvt173 opened this issue Jul 19, 2024 · 9 comments · Fixed by #1795
Assignees
Labels
Milestone

Comments

@tvt173
Copy link

tvt173 commented Jul 19, 2024

Hi @klayoutmatthias ,

I found a troublesome bug today when using a keyword argument to set the min_projection in a width_check for a Region object in python. I find that I get consistent results when calling using a positional argument but inconsistent results when using a keyword argument. Do you know why this might be?

Please see the example script below:

import klayout.db as kdb
import numpy as np

def always(value_dbu):
    # min_projection is not set
    return region.width_check(value_dbu)

def never(value_dbu):
    # setting min_projection via a positional argument
    return region.width_check(value_dbu, False, kdb.Metrics.Euclidian, np.deg2rad(90), value_dbu)

def sometimes(value_dbu):
    # setting min_projection via a named argument
    return region.width_check(value_dbu, min_projection=value_dbu)

filename = r"troublesome_taper.oas"
l = kdb.Layout()
value_dbu = 1000
l.read(filename)
layer = l.layer(8, 0)
region = kdb.Region(l.begin_shapes(l.top_cell(), layer=layer))
for func in [always, never, sometimes]:
    results = []
    for i in range(30):
        result = len(func(value_dbu))
        results.append(str(result))
    print(f"{func.__name__}: " + "".join(results))

Expected Output:

always: 111111111111111111111111111111
never: 000000000000000000000000000000
sometimes: 000000000000000000000000000000

Actual Output:

always: 111111111111111111111111111111
never: 000000000000000000000000000000
sometimes: 011000100001001100000100000000

I'm using the standalone klayout python package v0.29.2 on Windows 11.

Sample gds: troublesome_taper.zip

@sebastian-goeldi
Copy link
Contributor

sebastian-goeldi commented Jul 19, 2024

Your never case might be flawed.

The ignore angle should be in degrees not radian.

From the docs:

"ignore_angle" specifies the angle limit of two edges. If two edges form an angle equal or above the given value, they will not contribute in the check. Setting this value to 90 (the default) will exclude edges with an angle of 90 degree or more from the check. Use nil for this value to select the default.

So for proper comparison it should be 90 or even better None

Does this change the output of never?

@tvt173
Copy link
Author

tvt173 commented Jul 19, 2024

nice catch @sebastian-goeldi ! indeed it changes the results for the "never" case, but it does not change the fact that "sometimes" behaves inconsistently

updated script:

import klayout.db as kdb
import numpy as np

def always(value_dbu):
    # min_projection is not set
    return region.width_check(value_dbu, False, kdb.Metrics.Euclidian, 90)

def never(value_dbu):
    # setting min_projection via a positional argument
    return region.width_check(value_dbu, False, kdb.Metrics.Euclidian, 90, value_dbu)

def sometimes(value_dbu):
    # setting min_projection via a named argument
    return region.width_check(value_dbu, min_projection=value_dbu)

filename = r"troublesome_taper.oas"
l = kdb.Layout()
value_dbu = 1000
l.read(filename)
layer = l.layer(8, 0)
region = kdb.Region(l.begin_shapes(l.top_cell(), layer=layer))
for func in [always, never, sometimes]:
    results = []
    for i in range(30):
        result = len(func(value_dbu))
        results.append(str(result))
    print(f"{func.__name__}: " + "".join(results))

Updated results:

always: 111111111111111111111111111111
never: 111111111111111111111111111111
sometimes: 011000000000000000000000000000

@sebastian-goeldi
Copy link
Contributor

sebastian-goeldi commented Jul 19, 2024

hmm, time to upgrade to 0.29.4 (or linux :P); with 0.29.4 I get:

unfixed ignore angle:

python test.py 
always: 111111111111111111111111111111
never: 000000000000000000000000000000
sometimes: 111111111111111111111111111111

fixed to None:

python test.py 
always: 111111111111111111111111111111
never: 111111111111111111111111111111
sometimes: 111111111111111111111111111111

Huh, this is worrisome. On linux with 0.29.2 I also get the correct output.

@klayoutmatthias is the python kwargs binding different on windows than on linux/macos?

@tvt173
Copy link
Author

tvt173 commented Jul 19, 2024

i still get the inconsistent behavior on 0.29.4/windows 😞

interestingly, @sebastian-goeldi and I can both confirm that we get consistent results in the klayout GUI on Windows, but now oddly, we get the wrong result consistently (?). all three results should be the same now, but on windows GUI "sometimes" gives all zeros (only linux appears to show the correct behavior) 🤔

always: 11111111111111111111111111111111111111111111111111
never: 11111111111111111111111111111111111111111111111111
sometimes: 00000000000000000000000000000000000000000000000000

@tvt173
Copy link
Author

tvt173 commented Jul 19, 2024

i can also confirm linux gives the correct results. it appears to be a windows issue
(the output variable names are now really confusing... to be clear, I believe all three cases should return all 1's, and that is the result obtained on linux)

@sebastian-goeldi
Copy link
Contributor

sebastian-goeldi commented Jul 19, 2024

I just tested it in windows 11 VM vs debian, the results are interesting...

image

I suspect the only one being correct is the linux standalone

@klayoutmatthias
Copy link
Collaborator

I am getting an even more interesting one on Ubuntu 22.04:

image

I think that is more or less the same problem.

Time for debugging :(

Matthias

@klayoutmatthias
Copy link
Collaborator

I think I found the issue.

The compiler does not warn if passing a return-by-value to "const &" .. :(

It will be easy to fix. The issue happens only for keyword parameters with an enum- or object-type default value. In this case it was the Metrics type I think.

Matthias

@tvt173
Copy link
Author

tvt173 commented Jul 19, 2024

Great! Thanks for the quick work getting to the bottom of it! A patch release would be appreciated once the fix is in

klayoutmatthias added a commit that referenced this issue Jul 27, 2024
Fixing issue #1793 (problem with default arguments in Python)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants