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

Numpy frame #739

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Numpy frame #739

wants to merge 5 commits into from

Conversation

giboul
Copy link

@giboul giboul commented Dec 5, 2024

Hi everyone!

I spent too much time on the error below that showed up because I gave the frame argument of pyclaw.solution.Solution.__init__ a numpy integer instead of a pure python integer... So I thought it could be made a bit more flexible.

type(frame) = <class 'numpy.int64'>
Traceback (most recent call last):
  File "/home/giboul/Desktop/TriftGeoClaw/TSUL/read.py", line 114, in <module>
    read_cut(times[i])
  File "/home/giboul/Desktop/TriftGeoClaw/TSUL/read.py", line 71, in read_cut
    frame_sol = solution.Solution(frame, path=projdir/"AVAC"/"_output5", file_format=AVAC["out_format"])
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/giboul/Desktop/clawpack/pyclaw/src/pyclaw/solution.py", line 140, in __init__
    raise Exception('Invalid pyclaw.Solution object initialization')
Exception: Invalid pyclaw.Solution object initialization

With the suggested changes, the error message would look like this for an erroneous input:

Traceback (most recent call last):
  File "/home/giboul/Desktop/TriftGeoClaw/TSUL/read.py", line 114, in <module>
    read_cut(times[i])
  File "/home/giboul/Desktop/TriftGeoClaw/TSUL/read.py", line 71, in read_cut
    frame_sol = solution.Solution(frame, path=projdir/"AVAC"/"_output5", file_format=AVAC["out_format"])
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/giboul/Desktop/clawpack/pyclaw/src/pyclaw/solution.py", line 141, in __init__
    raise Exception(
Exception: Invalid pyclaw.Solution object initialization: '...' is not an int.

@coveralls
Copy link

coveralls commented Dec 5, 2024

Coverage Status

coverage: 26.77% (-19.8%) from 46.556%
when pulling 1f0105a on giboul:NumpyFrame
into 37658ee on clawpack:master.

@mandli
Copy link
Member

mandli commented Dec 5, 2024

Yikes, that is annoying. I have run into the numpy integer problem a bunch lately as well.

My one thought now looking at this is that maybe we would rather simply cast the frame argument as an int(). This should raise a fairly straightforward exception and will allow other types to be converted if sane, *e.g. * int('3').

@giboul
Copy link
Author

giboul commented Dec 6, 2024

It would also accept floats, is that fine?

@ketch
Copy link
Member

ketch commented Dec 6, 2024

Thanks for taking the time to report and fix this, @giboul . I think it would be fine to merge this but even better to change the code to attempt to cast the input to int as @mandli says. It's hard to imagine a serious problem that that would cause.

@giboul
Copy link
Author

giboul commented Dec 6, 2024

Cool ! I just noticed I had not treated the case where frame is passed as a keyword.

@@ -136,8 +136,7 @@ def get_clawpack_dot_xxx(modname): return modname.rpartition('.')[0]
if len(arg) == 1:
# Load frame
frame = arg[0]
if not isinstance(frame,int):
raise Exception('Invalid pyclaw.Solution object initialization')
frame = int(frame)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to just do frame = int(arg[0])?

Copy link
Author

@giboul giboul Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just that the error traceback is slighly more clear by explicitly saying frame is the issue:

    frame = int(frame)
            ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '_1_'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point. Not sure what is better I guess then.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... 'frame' still appears in frame = int(arg[0]) so I commited that change. Funny how it takes me 5 commits to change two lines.

@mandli
Copy link
Member

mandli commented Dec 6, 2024

The tests are failing as the Riemann commit does not include the fwave dictionary. Running it locally works though.

@giboul
Copy link
Author

giboul commented Dec 6, 2024

Is there something I can do about the tests failing?

@mandli
Copy link
Member

mandli commented Dec 8, 2024

We have to update the super-repository for clawpack with the updated commit point that includes the f-wave solver feature.

@mandli
Copy link
Member

mandli commented Dec 8, 2024

I think clawpack/clawpack#259 should take care of this although the interface has changed since last I did a PR like this. Once that get merged the testing should work.

Since `frame` would appear in the error anyway, I guess this is cleaner
@mandli
Copy link
Member

mandli commented Dec 26, 2024

This looks fine to me and works locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants