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

[xdoctest] reformat example code with google style in No.1-No.5 #55804

Merged
merged 6 commits into from
Aug 1, 2023

Conversation

longranger2
Copy link
Contributor

@longranger2 longranger2 commented Jul 29, 2023

PR types

Others

PR changes

Others

Description

修改如下文件的示例代码为新的格式,并通过 xdoctest 检查:

  • python/paddle/batch.py
  • python/paddle/dataset/image.py
  • python/paddle/fft.py
  • python/paddle/metric/metrics.py
  • python/paddle/onnx/export.py

@sunzhongkai588 @SigureMo @megemini

@paddle-bot
Copy link

paddle-bot bot commented Jul 29, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Jul 29, 2023
Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

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

@SigureMo 对于 image 这种,CI 环境里没有吧?跳过测试?

Comment on lines 45 to 52
... print(data)
...
>>> # Output is
>>> # [0, 1]
>>> # [2, 3]
>>> # [4, 5]
>>> # [6, 7]
>>> # [8, 9]
Copy link
Contributor

Choose a reason for hiding this comment

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

>>> # Output is 去掉,后面输出前面的 # 也去掉,这样xdoctest就可以比对输出的正确性了~

>>> for data in batch_reader():
...     print(data)
[0, 1]
[2, 3]
[4, 5]
[6, 7]
[8, 9]

Comment on lines +126 to +128
>>> with open('cat.jpg') as f:
... im = load_image_bytes(f.read())
...
Copy link
Contributor

Choose a reason for hiding this comment

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

这个 cat.jpg CI 环境中应该没有吧?如果 CI 这里出错了,可以用 # doctest: +SKIP 把这部分示例跳过~

>>> # doctest: +SKIP
>>> with open('cat.jpg') as f:
...     im = load_image_bytes(f.read())
...

Copy link
Member

Choose a reason for hiding this comment

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

@SigureMo 对于 image 这种,CI 环境里没有吧?跳过测试?

先不 skip 试试吧,如果没有的话之前也是过不了的,当时只能豁免,如果真的没有再 skip

Copy link
Member

Choose a reason for hiding this comment

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

另外确认一下,我们也是支持在 SKIP 后面加 skip reason 的吧

image

如果支持的话,还是建议加一下 reason,不然得翻 PR 才知道这段代码为啥 skip 了

Copy link
Contributor

Choose a reason for hiding this comment

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

如果支持的话,还是建议加一下 reason

必须支持!不过,当时在写 convert_directive 的 regex pattern 的时候,写的太严了,导致不能兼容带有描述的 skip~

#55811 已修改~

@@ -148,7 +148,7 @@ def load_image(file, is_color=True):

.. code-block:: python

im = load_image('cat.jpg')
>>> im = load_image('cat.jpg')
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

Comment on lines +181 to +182
>>> im = load_image('cat.jpg')
>>> im = resize_short(im, 256)
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

Comment on lines +211 to +213
>>> im = load_image('cat.jpg')
>>> im = resize_short(im, 256)
>>> im = to_chw(im)
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

Comment on lines 1406 to 1407
>>> # Tensor(shape=[5], dtype=float32, place=Place(gpu:0), stop_gradient=True,
>>> # [ 1.33333337, -1.33333337, -0.66666669, 0. , 0.66666669])
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

>>> correct = m.compute(x, y)
>>> m.update(correct)
>>> res = m.accumulate()
>>> print(res) # 0.75
Copy link
Contributor

Choose a reason for hiding this comment

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

这里确少输出

>>> m = paddle.metric.Precision()
>>> m.update(x, y)
>>> res = m.accumulate()
>>> print(res) # 1.0
Copy link
Contributor

Choose a reason for hiding this comment

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

缺少输出

>>> m = paddle.metric.Recall()
>>> m.update(x, y)
>>> res = m.accumulate()
>>> print(res) # 2.0 / 3.0
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

>>> predictions = paddle.to_tensor([[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32')
>>> label = paddle.to_tensor([[2], [0]], dtype="int64")
>>> result = paddle.metric.accuracy(input=predictions, label=label, k=1)
>>> # 0.5
Copy link
Contributor

Choose a reason for hiding this comment

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

print 显性的输出结果~

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

有一些应该是因为格式上对不上导致的错误,需要修正一下,有使用 convert-doctest doctest 进行本地测试吗?

秒修了啊,那我溜了

@longranger2
Copy link
Contributor Author

longranger2 commented Jul 31, 2023

有一些应该是因为格式上对不上导致的错误,需要修正一下,有使用 convert-doctest doctest 进行本地测试吗?

秒修了啊,那我溜了

哈哈有的,本地是OK的,但看了CI的报错,好像是把空格也进行了识别

本地运行结果图:
image

CI的报错:
image

@megemini
Copy link
Contributor

哈哈有的,本地是OK的,但看了CI的报错,好像是把空格也进行了识别

空格不影响的,这里只是日志打印的时候显示原始字符串~

看日志应该是 空行 的原因~ xdoctest 识别到空行会认为输出已经结束,所以只比对了 tensor 的一部分,并抛出错误~

Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

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

对齐一下缩进吧~ 没用的空格删一下~

另外,

- im = center_crop(im, 224)
+ >>> im = center_crop(im, 224)

是不是应该先初始化一下

>>> im = load_image('cat.jpg')
>>> im = center_crop(im, 224)

>>> x = paddle.to_tensor([0.0, 1.0, 0.0, 0.0])
>>> print(paddle.fft.rfft(x))
Tensor(shape=[3], dtype=complex64, place=Place(cpu), stop_gradient=True,
[ (1+0j), -1j , (-1+0j)])
Copy link
Contributor

Choose a reason for hiding this comment

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

这里缩进对齐一下吧

Tensor(shape=[3], dtype=complex64, place=Place(cpu), stop_gradient=True,
[ (1+0j), -1j    , (-1+0j)])

Comment on lines 378 to 379
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[0., 1., 0., 0.])
Copy link
Contributor

Choose a reason for hiding this comment

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

同上,对齐一下缩进~

Comment on lines 420 to 421
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[0., 0., 0., 4.])
Copy link
Contributor

Choose a reason for hiding this comment

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

对齐缩进~

Comment on lines 468 to 476
Tensor(shape=[6], dtype=complex64, place=Place(cpu), stop_gradient=True,
[(-0.1666666716337204+0j), (1-0j) ,
(2.3333334922790527-0j), (3.5+0j) ,
(2.3333334922790527+0j), (1+0j) ])

>>> print(paddle.fft.ihfft(spectrum))
Tensor(shape = [4], dtype = complex64, place = Place(cpu), stop_gradient = True,
[(-0.1666666716337204+0j), (1-0j),
(2.3333334922790527-0j), (3.5+0j)])
Copy link
Contributor

Choose a reason for hiding this comment

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

对齐缩进,另外,没必要的空格也删一下吧~

Tensor(shape=[6], dtype=complex64, place=Place(cpu), stop_gradient=True,
[(-0.1666666716337204+0j), (1-0j),
(2.3333334922790527-0j), (3.5+0j),
(2.3333334922790527+0j), (1+0j)])

Comment on lines 525 to 541
Tensor(shape=[4, 4, 4], dtype=complex128, place=Place(cpu), stop_gradient=True,
[[[(24+0j), 0j , 0j , -0j ],
[(-8+8j), 0j , 0j , -0j ],
[(-8+0j), 0j , 0j , -0j ],
[(-8-8j), 0j , 0j , -0j ]],
[[(24+0j), 0j , 0j , -0j ],
[(-8+8j), 0j , 0j , -0j ],
[(-8+0j), 0j , 0j , -0j ],
[(-8-8j), 0j , 0j , -0j ]],
[[(24+0j), 0j , 0j , -0j ],
[(-8+8j), 0j , 0j , -0j ],
[(-8+0j), 0j , 0j , -0j ],
[(-8-8j), 0j , 0j , -0j ]],
[[(24+0j), 0j , 0j , -0j ],
[(-8+8j), 0j , 0j , -0j ],
[(-8+0j), 0j , 0j , -0j ],
[(-8-8j), 0j , 0j , -0j ]]])
Copy link
Contributor

Choose a reason for hiding this comment

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

对齐一下缩进~

Comment on lines 979 to 981
Tensor(shape=[2, 2], dtype=complex128, place=Place(cpu), stop_gradient=True,
[[ (0.5+0j), 0j ],
[(-0.5+0j), 0j ]])
Copy link
Contributor

Choose a reason for hiding this comment

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

对齐缩进

Comment on lines 1099 to 1101
Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 2.37500000, -1.12500000, 0.37500000, 0.87500000],
[ 0.12500000, 0.12500000, 0.12500000, 0.12500000]])
Copy link
Contributor

Choose a reason for hiding this comment

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

对齐缩进

Comment on lines 1148 to 1150
Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[19., 7., 3., -9.],
[ 1., 1., 1., 1.]])
Copy link
Contributor

Choose a reason for hiding this comment

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

对齐缩进

Comment on lines 1200 to 1205
Tensor(shape=[5, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
[[0., 0., 0., 0., 0.],
[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4.]])
Copy link
Contributor

Choose a reason for hiding this comment

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

对齐缩进

>>> fftfreq_xp = paddle.fft.fftfreq(5, d=scalar_temp)
>>> print(fftfreq_xp)
Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 0. , 0.40000001, 0.80000001, -0.80000001, -0.40000001])
Copy link
Contributor

Choose a reason for hiding this comment

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

对齐缩进

Copy link
Contributor

Choose a reason for hiding this comment

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

后面的相应也都对齐一下吧~

@longranger2
Copy link
Contributor Author

@megemini @SigureMo 修改好了,辛苦review下~

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

括号对齐一下会好看些

[[(24+0j), 0j, 0j, -0j],
[(-8+8j), 0j, 0j, -0j],
[(-8+0j), 0j, 0j, -0j],
[(-8-8j), 0j, 0j, -0j]]])
Copy link
Member

@SigureMo SigureMo Aug 1, 2023

Choose a reason for hiding this comment

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

- [[[1, 2],
- [3, 4]]]
+ [[[1, 2],
+   [3, 4]]]

括号对齐一下吧

[0j, 0j, 0j]],
[[0j, 0j, 0j],
[0j, 0j, 0j],
[0j, 0j, 0j]]])
Copy link
Member

Choose a reason for hiding this comment

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

同上

Copy link
Member

Choose a reason for hiding this comment

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

下面还有一些

@longranger2
Copy link
Contributor Author

括号对齐一下会好看些

好的,我修改下~

@megemini
Copy link
Contributor

megemini commented Aug 1, 2023

  • xdoctest
  • doc preview(抽看了几个,copy from 的代码没问题)

LGTM :)

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTM~

@luotao1 luotao1 merged commit 9084a03 into PaddlePaddle:develop Aug 1, 2023
@longranger2 longranger2 deleted the xdoctest1 branch August 1, 2023 07:56
@luotao1 luotao1 added HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 and removed HappyOpenSource 快乐开源活动issue与PR labels Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants