generate_stubs.py 4.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""
This script makes the stubs for implicitron in docs/modules.
"""

from pathlib import Path

ROOT_DIR = Path(__file__).resolve().parent.parent


def paths_to_modules(paths):
    """
    Given an iterable of paths, return equivalent list of modules.
    """
David Novotny's avatar
David Novotny committed
21
22
23
24
25
    return [
        str(i.relative_to(ROOT_DIR))[:-3].replace("/", ".")
        for i in paths
        if "__pycache__" not in str(i)
    ]
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


def create_one_file(title, description, sources, dest_file):
    with open(dest_file, "w") as f:
        print(title, file=f)
        print("=" * len(title), file=f)
        print(file=f)
        print(description, file=f)
        for source in sources:
            if source.find("._") != -1:
                # ignore internal modules including __init__.py
                continue
            print(f"\n.. automodule:: {source}", file=f)
            print("    :members:", file=f)
            print("    :undoc-members:", file=f)
            print("    :show-inheritance:", file=f)


def iterate_directory(directory_path, dest):
    """
    Create a file for each module in the given path
    """
    toc = []
    if not dest.exists():
        dest.mkdir()
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
51
    for file in sorted(directory_path.glob("*.py")):
52
53
54
55
56
57
58
59
60
61
62
        if file.stem.startswith("_"):
            continue
        module = paths_to_modules([file])
        create_one_file(module[0], file.stem, module, dest / f"{file.stem}.rst")
        toc.append(file.stem)

    for subdir in directory_path.iterdir():
        if not subdir.is_dir():
            continue
        if subdir.name == "fb":
            continue
David Novotny's avatar
David Novotny committed
63
64
        if subdir.name.startswith("_"):
            continue
65
66
67
        iterate_directory(subdir, dest / (subdir.name))
        toc.append(f"{subdir.name}/index")

David Novotny's avatar
David Novotny committed
68
69
70
71
72
    paths_to_modules_ = paths_to_modules([directory_path.with_suffix(".XX")])
    if len(paths_to_modules_) == 0:
        return
    title = paths_to_modules_[0]

73
74
75
76
77
78
79
80
    with open(dest / "index.rst", "w") as f:
        print(title, file=f)
        print("=" * len(title), file=f)
        print("\n.. toctree::\n", file=f)
        for item in toc:
            print(f"    {item}", file=f)


David Novotny's avatar
David Novotny committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def make_directory_index(title: str, directory_path: Path):
    index_file = directory_path / "index.rst"
    directory_rsts = sorted(directory_path.glob("*.rst"))
    subdirs = sorted([f for f in directory_path.iterdir() if f.is_dir()])
    with open(index_file, "w") as f:
        print(title, file=f)
        print("=" * len(title), file=f)
        print("\n.. toctree::\n", file=f)
        for subdir in subdirs:
            print(f"    {subdir.stem}/index.rst", file=f)
        for rst in directory_rsts:
            if rst.stem == "index":
                continue
            print(f"    {rst.stem}", file=f)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def do_implicitron():
    DEST_DIR = Path(__file__).resolve().parent / "modules/implicitron"

    iterate_directory(ROOT_DIR / "pytorch3d/implicitron/models", DEST_DIR / "models")

    unwanted_tools = ["configurable", "depth_cleanup", "utils"]
    tools_sources = sorted(ROOT_DIR.glob("pytorch3d/implicitron/tools/*.py"))
    tools_modules = [
        str(i.relative_to(ROOT_DIR))[:-3].replace("/", ".")
        for i in tools_sources
        if i.stem not in unwanted_tools
    ]
    create_one_file(
        "pytorch3d.implicitron.tools",
        "Tools for implicitron",
        tools_modules,
        DEST_DIR / "tools.rst",
    )

    dataset_files = sorted(ROOT_DIR.glob("pytorch3d/implicitron/dataset/*.py"))
    basic_dataset = [
        "dataset_base",
        "dataset_map_provider",
        "data_loader_map_provider",
        "data_source",
        "scene_batch_sampler",
    ]
    basic_dataset_modules = [
        f"pytorch3d.implicitron.dataset.{i}" for i in basic_dataset
    ]
    create_one_file(
        "pytorch3d.implicitron.dataset in general",
        "Basics of data for implicitron",
        basic_dataset_modules,
        DEST_DIR / "data_basics.rst",
    )

    specific_dataset_files = [
        i for i in dataset_files if i.stem.find("_dataset_map_provider") != -1
    ]
    create_one_file(
        "pytorch3d.implicitron.dataset specific datasets",
        "specific datasets",
        paths_to_modules(specific_dataset_files),
        DEST_DIR / "datasets.rst",
    )

    evaluation_files = sorted(ROOT_DIR.glob("pytorch3d/implicitron/evaluation/*.py"))
    create_one_file(
        "pytorch3d.implicitron.evaluation",
        "evaluation",
        paths_to_modules(evaluation_files),
        DEST_DIR / "evaluation.rst",
    )

    make_directory_index("pytorch3d.implicitron", DEST_DIR)


def iterate_toplevel_module(name: str) -> None:
    dest_dir = Path(__file__).resolve().parent / "modules" / name
    iterate_directory(ROOT_DIR / "pytorch3d" / name, dest_dir)


do_implicitron()
iterate_toplevel_module("renderer")
iterate_toplevel_module("vis")