Skip to content

Commit 723c503

Browse files
authored
Add sphinx doc in PyBind file. Provide more documentation (#292)
* Add the support to write documentation in the pybind directly. * Provide more detail of the API documentations.
1 parent b0a4fdd commit 723c503

File tree

3 files changed

+96
-14
lines changed

3 files changed

+96
-14
lines changed

docs/api/visualdl.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22
API
33
==========
44

5+
Python API
6+
----------
7+
58
.. automodule:: visualdl
6-
:members: LogReader, LogWriter
9+
:members: LogReader, LogWriter
10+
11+
PyBind Wrapper Classes
12+
-----------------------
13+
14+
.. automodule:: visualdl.core

visualdl/logic/pybind.cc

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,20 @@ namespace cp = visualdl::components;
2929
CODE(double);
3030

3131
PYBIND11_MODULE(core, m) {
32-
m.doc() = "C++ core of VisualDL";
32+
m.doc() = R"pbdoc(
33+
34+
VisualDL uses PyBind to operate with the c++ framework. Users should use LogWriter to instantiate scalar/histogram/image writer
35+
36+
.. autoclass:: ScalarWriter__float
37+
:members:
38+
39+
.. autoclass:: HistogramWriter__float
40+
:members:
41+
42+
.. autoclass:: ImageWriter
43+
:members:
44+
45+
)pbdoc";
3346

3447
py::class_<vs::LogReader>(m, "LogReader")
3548
.def(py::init(
@@ -115,22 +128,63 @@ PYBIND11_MODULE(core, m) {
115128
#undef ADD_SCALAR_READER
116129

117130
#define ADD_SCALAR_WRITER(T) \
118-
py::class_<cp::Scalar<T>>(m, "ScalarWriter__" #T) \
131+
py::class_<cp::Scalar<T>>(m, "ScalarWriter__" #T, R"pbdoc(
132+
PyBind class. Must instantiate through the LogWriter.
133+
)pbdoc") \
119134
.def("set_caption", &cp::Scalar<T>::SetCaption) \
120-
.def("add_record", &cp::Scalar<T>::AddRecord);
135+
.def("add_record", &cp::Scalar<T>::AddRecord, R"pbdoc(
136+
add a record with the step and value
137+
138+
:param step: This value appears at this step in the run.
139+
:type step: integer
140+
:param value: The scalar value to be recorded.
141+
:type value: float
142+
)pbdoc");
121143
ADD_SCALAR_WRITER(int);
122144
ADD_SCALAR_WRITER(float);
123145
ADD_SCALAR_WRITER(double);
124146
#undef ADD_SCALAR_WRITER
125147

126148
// clang-format on
127-
py::class_<cp::Image>(m, "ImageWriter")
128-
.def("set_caption", &cp::Image::SetCaption)
129-
.def("start_sampling", &cp::Image::StartSampling)
130-
.def("is_sample_taken", &cp::Image::IsSampleTaken)
131-
.def("finish_sampling", &cp::Image::FinishSampling)
132-
.def("set_sample", &cp::Image::SetSample)
133-
.def("add_sample", &cp::Image::AddSample);
149+
py::class_<cp::Image>(m, "ImageWriter", R"pbdoc(
150+
PyBind class. Must instantiate through the LogWriter.
151+
)pbdoc")
152+
.def("set_caption", &cp::Image::SetCaption, R"pbdoc(
153+
PyBind class. Must instantiate through the LogWriter.
154+
)pbdoc")
155+
.def("start_sampling", &cp::Image::StartSampling, R"pbdoc(
156+
Start a sampling period, this interface will start a new reservoir sampling phase.
157+
)pbdoc")
158+
.def("is_sample_taken", &cp::Image::IsSampleTaken, R"pbdoc(
159+
Will this sample be taken, this interface is introduced to reduce the cost
160+
of copy image data, by testing whether this image will be sampled, and only
161+
copy data when it should be sampled. In that way, most of un-sampled image
162+
data need not be copied or processed at all.
163+
164+
:return: Index
165+
:rtype: integer
166+
)pbdoc")
167+
.def("finish_sampling", &cp::Image::FinishSampling, R"pbdoc(
168+
End a sampling period, it will clear all states for reservoir sampling.
169+
)pbdoc")
170+
.def("set_sample", &cp::Image::SetSample, R"pbdoc(
171+
Store the image shape with the flatten image data.
172+
173+
:param index:
174+
:type index: integer
175+
:param image_shape: Image size
176+
:type image_shape: tuple
177+
:param image_data: Flatten image data
178+
:type image_data: list
179+
)pbdoc")
180+
.def("add_sample", &cp::Image::AddSample, R"pbdoc(
181+
A combined interface for is_sample_taken and set_sample, simpler but is less efficient.
182+
183+
:param image_shape: Image size
184+
:type image_shape: tuple
185+
:param image_data: Flatten image data
186+
:type image_data: list
187+
)pbdoc");
134188

135189
py::class_<cp::ImageReader::ImageRecord>(m, "ImageRecord")
136190
// TODO(ChunweiYan) make these copyless.
@@ -147,9 +201,19 @@ PYBIND11_MODULE(core, m) {
147201
.def("record", &cp::ImageReader::record)
148202
.def("timestamp", &cp::ImageReader::timestamp);
149203

150-
#define ADD_HISTOGRAM_WRITER(T) \
151-
py::class_<cp::Histogram<T>>(m, "HistogramWriter__" #T) \
152-
.def("add_record", &cp::Histogram<T>::AddRecord);
204+
#define ADD_HISTOGRAM_WRITER(T) \
205+
py::class_<cp::Histogram<T>>(m, "HistogramWriter__" #T, R"pbdoc(
206+
PyBind class. Must instantiate through the LogWriter.
207+
)pbdoc") \
208+
.def("add_record", &cp::Histogram<T>::AddRecord, R"pbdoc(
209+
add a record with the step and histogram_value
210+
211+
:param step: This value appears at this step in the run.
212+
:type step: integer
213+
:param histogram_value: A flatten list of the distribution value. EX: [1, 2, 100, 2, 3, 200] will draw a histogram where
214+
the value between 1~2 is 100 and the value between 2~3 is 200
215+
:type histogram_value: list
216+
)pbdoc");
153217
ADD_FULL_TYPE_IMPL(ADD_HISTOGRAM_WRITER)
154218
#undef ADD_HISTOGRAM_WRITER
155219

visualdl/python/storage.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ def scalar(self, tag, type='float'):
168168
169169
:param tag: The scalar writer will label the data with tag
170170
:type tag: basestring
171+
:return: A scalar writer to handle step and value records
172+
:rtype: ScalarWriter
171173
"""
172174
check_tag_name_valid(tag)
173175
type2scalar = {
@@ -183,6 +185,12 @@ def image(self, tag, num_samples, step_cycle=1):
183185
184186
:param tag: The image writer will label the image with tag
185187
:type tag: basestring
188+
:param num_samples: how many samples to take in a step.
189+
:type num_samples: integer
190+
:param step_cycle: store every `step_cycle` as a record.
191+
:type step_cycle: integer
192+
:return: A image writer to sample images
193+
:rtype: ImageWriter
186194
"""
187195
check_tag_name_valid(tag)
188196
return self.writer.new_image(tag, num_samples, step_cycle)
@@ -194,6 +202,8 @@ def histogram(self, tag, num_buckets, type='float'):
194202
195203
:param tag: The histogram writer will label the data with tag
196204
:type tag: basestring
205+
:return: A histogram writer to record distribution
206+
:rtype: HistogramWriter
197207
"""
198208
check_tag_name_valid(tag)
199209
types = {

0 commit comments

Comments
 (0)