Skip to content

Commit

Permalink
[ci] Enable yapf and isort on example files
Browse files Browse the repository at this point in the history
Note we explicitly exclude running pylint on them as it requires a bunch
of manual fixes first.
  • Loading branch information
ailzhang committed Jun 13, 2022
1 parent 573f89e commit 2df0417
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 127 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autoupdate_schedule: quarterly
autoupdate_commit_msg: '[misc] Update pre-commit hooks'

exclude: ^((tests/python/test_exception|.*/examples/.*)\.py$|external/)
exclude: ^((tests/python/test_exception)\.py$|external/)
repos:
- repo: https://github.com/google/yapf
rev: v0.32.0
Expand Down Expand Up @@ -33,3 +33,4 @@ repos:
- id: pylint
args: ['-rn', '-sn']
files: ^python/taichi/
exclude: ^python/taichi/examples/.*.py
3 changes: 2 additions & 1 deletion python/taichi/examples/algorithm/mciso_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ def main(self):
(0, 1))
if gui.is_pressed(gui.SPACE):
num = ret.shape[0]
writer = ti.tools.PLYWriter(num_vertices=num * 3, num_faces=num)
writer = ti.tools.PLYWriter(num_vertices=num * 3,
num_faces=num)
vertices = ret.reshape(num * 3, 3) * 2 - 1
writer.add_vertex_pos(vertices[:, 0], vertices[:, 1],
vertices[:, 2])
Expand Down
268 changes: 178 additions & 90 deletions python/taichi/examples/autodiff/diff_sph/diff_sph.py

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion python/taichi/examples/features/io/export_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@
b = np.random.rand(20)
alpha = np.random.rand(20)
# re-fill
writer = ti.tools.PLYWriter(num_vertices=20, num_faces=12, face_type="quad")
writer = ti.tools.PLYWriter(num_vertices=20,
num_faces=12,
face_type="quad")
writer.add_vertex_pos(x, y, z)
writer.add_faces(indices)
writer.add_vertex_channel("vdata1", "double", vdata)
Expand Down
8 changes: 6 additions & 2 deletions python/taichi/examples/ggui_examples/mass_spring_3d_ggui.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

bending_springs = False


@ti.kernel
def initialize_mass_points():
random_offset = ti.Vector([ti.random() - 0.5, ti.random() - 0.5]) * 0.1
Expand Down Expand Up @@ -58,6 +59,7 @@ def initialize_mesh_indices():
else:
colors[i * n + j] = (1, 0.334, 0.52)


initialize_mesh_indices()

spring_offsets = []
Expand All @@ -73,6 +75,7 @@ def initialize_mesh_indices():
if (i, j) != (0, 0) and abs(i) + abs(j) <= 2:
spring_offsets.append(ti.Vector([i, j]))


@ti.kernel
def substep():
for i in ti.grouped(x):
Expand Down Expand Up @@ -105,7 +108,6 @@ def substep():
x[i] += dt * v[i]



@ti.kernel
def update_vertices():
for i, j in ti.ndrange(n, n):
Expand Down Expand Up @@ -145,7 +147,9 @@ def update_vertices():
two_sided=True)

# Draw a smaller ball to avoid visual penetration
scene.particles(ball_center, radius=ball_radius * 0.95, color=(0.5, 0.42, 0.8))
scene.particles(ball_center,
radius=ball_radius * 0.95,
color=(0.5, 0.42, 0.8))
canvas.scene(scene)
window.show()

Expand Down
30 changes: 21 additions & 9 deletions python/taichi/examples/graph/mpm88_graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import argparse

import numpy as np

import taichi as ti

ti.init(arch=ti.vulkan)
Expand All @@ -17,7 +19,6 @@
N_ITER = 500 # Use 500 to make speed diff more obvious



@ti.kernel
def substep_reset_grid(grid_v: ti.any_arr(field_dim=2),
grid_m: ti.any_arr(field_dim=2)):
Expand Down Expand Up @@ -95,6 +96,7 @@ def init_particles(x: ti.any_arr(field_dim=1), v: ti.any_arr(field_dim=1),
v[i] = [0, -1]
J[i] = 1


x = ti.Vector.ndarray(2, ti.f32, shape=(n_particles))
v = ti.Vector.ndarray(2, ti.f32, shape=(n_particles))

Expand All @@ -105,19 +107,29 @@ def init_particles(x: ti.any_arr(field_dim=1), v: ti.any_arr(field_dim=1),

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--baseline',
action='store_true')
parser.add_argument('--baseline', action='store_true')
args, unknown = parser.parse_known_args()

if not args.baseline:
print('running in graph mode')
# Build graph
sym_x = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'x', ti.f32, element_shape=(2, ))
sym_v = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'v', ti.f32, element_shape=(2, ))
sym_C = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'C', ti.f32, element_shape=(2, 2))
sym_x = ti.graph.Arg(ti.graph.ArgKind.NDARRAY,
'x',
ti.f32,
element_shape=(2, ))
sym_v = ti.graph.Arg(ti.graph.ArgKind.NDARRAY,
'v',
ti.f32,
element_shape=(2, ))
sym_C = ti.graph.Arg(ti.graph.ArgKind.NDARRAY,
'C',
ti.f32,
element_shape=(2, 2))
sym_J = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'J', ti.f32)
sym_grid_v = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'grid_v', ti.f32, element_shape=(2, ))
sym_grid_v = ti.graph.Arg(ti.graph.ArgKind.NDARRAY,
'grid_v',
ti.f32,
element_shape=(2, ))
sym_grid_m = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'grid_m', ti.f32)
g_init_builder = ti.graph.GraphBuilder()
g_init_builder.dispatch(init_particles, sym_x, sym_v, sym_J)
Expand All @@ -127,7 +139,7 @@ def init_particles(x: ti.any_arr(field_dim=1), v: ti.any_arr(field_dim=1),

substep.dispatch(substep_reset_grid, sym_grid_v, sym_grid_m)
substep.dispatch(substep_p2g, sym_x, sym_v, sym_C, sym_J, sym_grid_v,
sym_grid_m)
sym_grid_m)
substep.dispatch(substep_update_grid_v, sym_grid_v, sym_grid_m)
substep.dispatch(substep_g2p, sym_x, sym_v, sym_C, sym_J, sym_grid_v)

Expand Down
48 changes: 27 additions & 21 deletions python/taichi/examples/graph/stable_fluid_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse

import numpy as np

import taichi as ti

ti.init(arch=ti.vulkan)
Expand Down Expand Up @@ -214,11 +215,10 @@ def reset():
pressures_pair.cur.fill(0)
dyes_pair.cur.fill(0)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--baseline',
action='store_true')
parser.add_argument('--baseline', action='store_true')
args, unknown = parser.parse_known_args()

gui = ti.GUI('Stable Fluid', (res, res))
Expand Down Expand Up @@ -261,38 +261,44 @@ def reset():
'pressures_pair_nxt', ti.f32)
velocity_divs = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'velocity_divs',
ti.f32)
mouse_data = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'mouse_data', ti.f32)
mouse_data = ti.graph.Arg(ti.graph.ArgKind.NDARRAY, 'mouse_data',
ti.f32)

g1_builder = ti.graph.GraphBuilder()
g1_builder.dispatch(advect, velocities_pair_cur, velocities_pair_cur,
velocities_pair_nxt)
g1_builder.dispatch(advect, velocities_pair_cur, dyes_pair_cur, dyes_pair_nxt)
g1_builder.dispatch(apply_impulse, velocities_pair_nxt, dyes_pair_nxt, mouse_data)
velocities_pair_nxt)
g1_builder.dispatch(advect, velocities_pair_cur, dyes_pair_cur,
dyes_pair_nxt)
g1_builder.dispatch(apply_impulse, velocities_pair_nxt, dyes_pair_nxt,
mouse_data)
g1_builder.dispatch(divergence, velocities_pair_nxt, velocity_divs)
# swap is unrolled in the loop so we only need p_jacobi_iters // 2 iterations.
for _ in range(p_jacobi_iters // 2):
g1_builder.dispatch(pressure_jacobi, pressures_pair_cur, pressures_pair_nxt,
velocity_divs)
g1_builder.dispatch(pressure_jacobi, pressures_pair_nxt, pressures_pair_cur,
velocity_divs)
g1_builder.dispatch(subtract_gradient, velocities_pair_nxt, pressures_pair_cur)
g1_builder.dispatch(pressure_jacobi, pressures_pair_cur,
pressures_pair_nxt, velocity_divs)
g1_builder.dispatch(pressure_jacobi, pressures_pair_nxt,
pressures_pair_cur, velocity_divs)
g1_builder.dispatch(subtract_gradient, velocities_pair_nxt,
pressures_pair_cur)
g1 = g1_builder.compile()

g2_builder = ti.graph.GraphBuilder()
g2_builder.dispatch(advect, velocities_pair_nxt, velocities_pair_nxt,
velocities_pair_cur)
g2_builder.dispatch(advect, velocities_pair_nxt, dyes_pair_nxt, dyes_pair_cur)
g2_builder.dispatch(apply_impulse, velocities_pair_cur, dyes_pair_cur, mouse_data)
velocities_pair_cur)
g2_builder.dispatch(advect, velocities_pair_nxt, dyes_pair_nxt,
dyes_pair_cur)
g2_builder.dispatch(apply_impulse, velocities_pair_cur, dyes_pair_cur,
mouse_data)
g2_builder.dispatch(divergence, velocities_pair_cur, velocity_divs)
for _ in range(p_jacobi_iters // 2):
g2_builder.dispatch(pressure_jacobi, pressures_pair_cur, pressures_pair_nxt,
velocity_divs)
g2_builder.dispatch(pressure_jacobi, pressures_pair_nxt, pressures_pair_cur,
velocity_divs)
g2_builder.dispatch(subtract_gradient, velocities_pair_cur, pressures_pair_cur)
g2_builder.dispatch(pressure_jacobi, pressures_pair_cur,
pressures_pair_nxt, velocity_divs)
g2_builder.dispatch(pressure_jacobi, pressures_pair_nxt,
pressures_pair_cur, velocity_divs)
g2_builder.dispatch(subtract_gradient, velocities_pair_cur,
pressures_pair_cur)
g2 = g2_builder.compile()


swap = True

while gui.running:
Expand Down
1 change: 0 additions & 1 deletion tests/python/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

1 change: 0 additions & 1 deletion tests/python/examples/autodiff/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

0 comments on commit 2df0417

Please sign in to comment.