-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
DOC: Updating f2py docs to python 3 and fixing some typos #15303
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
Conversation
…ck for string bug here.
…2py-docs-python3 * 'f2py-docs-python3' of github.com:melissawm/numpy: Fixed typo. Fixed typos. Remove future imports from f2py example. Finished update of usage.rst PEP8 fix for equal sign and added note about building extension modules. WIP: updating usage.rst Finished update of python-usage.rst WIP: updated sections "Common blocks" and "F90 module data" from python-usage.rst WIP: updated "Callback arguments" section in python-usage.rst WIP: updated "Array arguments" section of python-usage.rst WIP: updated "String arguments" section of python-usage.rst. TODO check for string bug here. WIP: updated "Scalar Arguments" session of python-usage.rst WIP: updating f2py docs to python3: intro to python-usage done. Updated f2py "Getting Started" doc to python3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks much better now. I made a few small comments
@@ -14,7 +15,9 @@ foo - Function signature: | |||
] | |||
>>> allocarr.mod.b # b is Fortran-contiguous | |||
array([[ 1., 2., 3.], | |||
[ 4., 5., 6.]],'f') | |||
[ 4., 5., 6.]], dtype=float32) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
float32
-> np.float32
and add import numpy as np
to the top of the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the comments! However this is the actual output from the allocarr module, so I can't change that. Maybe fixing this should be an issue for f2py?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The output of print(allocarr.mod.__doc__)
is wrong. I haven't look into the rest of the changes but this indicates some bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is more of a numpy issue how the array is displayed in repr mode.
@@ -3,4 +3,4 @@ | |||
array([ 0., 1., 4., 9., 16.]) | |||
>>> import math | |||
>>> foo.calculate(range(5), math.exp) | |||
array([ 1. , 2.71828175, 7.38905621, 20.08553696, 54.59814835]) | |||
array([ 1. , 2.71828183, 7.3890561, 20.08553692, 54.59815003]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any idea why the results changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea, it surprised me too!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like using float32 changed to float64 or vice-versa in the underlying code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be due to defining real*8 func
. Before the type of func
was real
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, func
was not defined in the .f
file so this example was not working. See #14919 . In fact, declaring func
as real gives this output:
>>> import foo
>>> foo.calculate(range(5), lambda x: x*x)
array([0., 0., 0., 0., 0.])
>>> import math
>>> foo.calculate(range(5), math.exp)
array([ 0.00000000e+00, -2.85695233e-32, -1.01502388e-04, -9.36294913e-01,
3.80232235e-11])
>>>
This example only works if we declare func
as real*8
@@ -18,4 +18,4 @@ COMMON blocks: | |||
>>> ftype.foo(24) | |||
IN FOO: N= 24 A= 3. X=[ 1. 45. 3.] | |||
>>> ftype.data.x | |||
array([ 1., 45., 3.],'f') | |||
array([ 1., 45., 3.], dtype=float32) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
float32 ->
np.float32, and
import numpy as np` at the top of the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same case as above, it's the output from the module generated by f2py, so I can't change that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @mattip is wrong here, and misread what this code was.
@@ -20,4 +21,6 @@ foo - Function signature: | |||
Setting a(1,2)=a(1,2)+3 | |||
>>> moddata.mod.a # a is Fortran-contiguous | |||
array([[ 1., 5., 3.], | |||
[ 4., 5., 6.]],'f') | |||
[ 4., 5., 6.]], dtype=float32) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, this is output from the f2py generated module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or more generally, this is just now arrayprint.array_repr
works.
doc/source/f2py/string_session.dat
Outdated
A=123 | ||
B=123 | ||
B=123�� |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this now need a PEP-263 line:
# -*- coding: utf-8 -*-
at the top of the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is an open issue with f2py, because this is the output that we get from the generated module. See #4519 . I'm not sure if I should leave the docs like and include maybe a note about this being open, or write the expected output (but then the docs would be misleading). Any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattip, this isn't a python file, so that won't help.
It's possible that github is making assumption based on the .dat
file extension - renaming to txt
might help, as might adding a .gitattributes
entry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result for B
is actually expected: foo's second argument is expected to have 5 character input but the user gives less. As a quick fix, try b = array('123\0')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but the user gives less
Shouldn't this be an error? Or if not, should python pad with nulls?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, f2py should trigger a ValueError when the input has a smaller byte-size than expected by array argument.
@melissawm , can you create another issue on this? To resolve the given example issue, use b = array('123\0\0')
so that it will not break when the bug will be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pearu using b = array('123\0\0')
gives me the following:
>>> from numpy import array
>>> a = array('123')
>>> b = array('123\0\0')
>>> c = array('123')
>>> d = array('123')
>>> mystring.foo(a, b, c, d)
A=1
B=1
C=1
D=1
CHANGE A,B,C,D
A=A
B=B
C=C
D=D
>>> a.tostring(), b.tostring(), c.tostring(), d.tostring()
(b'1\x00\x00\x002\x00\x00\x003\x00\x00\x00', b'B \x00', b'1\x00\x00\x002\x00\x00\x003\x00\x00\x00', b'D \x00')
I can open another issue for the lack of ValueError when the array is too small, but I think this is related to #4519
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
a = array(b'123\0\0')
b = array(b'123\0\0')
c = array(b'123')
d = array(b'123')
f2py might be unicode ignorant..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That solves it. The issue about not raising the ValueError is here #15311
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may as well remove .tostring()
(which is really ndarray.tobytes()
) - if you want just the item in the array, you can use a[()], b[()], c[()], d[()]
, which will also hide the trailing nulls for you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left some comments. Overall looks good.
I am worried about the fact that the generated documentation strings of common blocks and module data do not contain the names of the items. These are needed! This indicates a bug that has been introduced to f2py in the past.
doc/source/f2py/moddata_session.dat
Outdated
foo - Function signature: | ||
foo() | ||
>>> print(moddata.mod.__doc__) | ||
'i'-scalar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The names of module data should be restored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, depends on #15325
doc/source/f2py/string_session.dat
Outdated
A=123 | ||
B=123 | ||
B=123�� |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result for B
is actually expected: foo's second argument is expected to have 5 character input but the user gives less. As a quick fix, try b = array('123\0')
doc/source/f2py/usage.rst
Outdated
@@ -21,7 +22,7 @@ Command ``f2py`` | |||
================= | |||
|
|||
When used as a command line tool, ``f2py`` has three major modes, | |||
distinguished by the usage of ``-c`` and ``-h`` switches: | |||
distinguished by the usage of ``-c``, ``-m`` and ``-h`` switches: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-m
does not define a mode. It just specifies the name of the generated module. Suggest undo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, when -m
is not used, then it is equivalent to usage -m untitled
@@ -14,7 +15,9 @@ foo - Function signature: | |||
] | |||
>>> allocarr.mod.b # b is Fortran-contiguous | |||
array([[ 1., 2., 3.], | |||
[ 4., 5., 6.]],'f') | |||
[ 4., 5., 6.]], dtype=float32) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is more of a numpy issue how the array is displayed in repr mode.
@@ -3,4 +3,4 @@ | |||
array([ 0., 1., 4., 9., 16.]) | |||
>>> import math | |||
>>> foo.calculate(range(5), math.exp) | |||
array([ 1. , 2.71828175, 7.38905621, 20.08553696, 54.59814835]) | |||
array([ 1. , 2.71828183, 7.3890561, 20.08553692, 54.59815003]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be due to defining real*8 func
. Before the type of func
was real
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I addressed all comments, let me know if any other changes are necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added just one minor request, otherwise LGTM.
I'm not sure I understand why we're changing the terminal sessions to indicate what we want to happen - shouldn't they indicate what actually happens, even if it turns out to be a bug? |
documenting buggy behavior sounds odd and is not what we normally do. CI is green, so there's no reason not to document the correct behavior, which will match actual behavior again once the now identified regression is fixed. right? |
Has it been fixed? |
No, it has not been fixed, and that is what makes me uneasy about this patch.
Well, CI used to be green for all documentation until @mattip turned on refguide. Just because CI isn't currently forcing us to make our docs accurate doesn't mean we shouldn't. |
Just because CI isn't currently forcing us to make our docs accurate
doesn't mean we shouldn't.
In this case, the code is faulty, not the docs. Already this fact should
force us to fix the code instead of making the docs faulty as well.
(This is a good example where accurate behavior can be a wrong behavior).
|
OK, let's put this in and make sure there is an issue for the fix. There are a ton of open issues for f2py that we should triage. |
Thanks Melissa. |
Hi all,
This PR fixes problems with the f2py documentation. I added some comments from the discussion here to the docs to make it clearer - feel free to remove if you think it's not necessary.
Closes #14865
Closes #14862
Closes #14812
Closes #14960