Skip to content

Commit

Permalink
Use rules_python to provide pip dependencies
Browse files Browse the repository at this point in the history
Setup a hermetic Python 3.10 interpreter and pip environment with numpy,
matplotlib, pandas, and tornado.

This commit defines a `//scripts:plot_data` py_binary that uses the
provided interpreter and pip environment:

 bazel run //scripts:plot_data -- <abs-path-csvfile>

Due to the Bazel execution root, the absolute path to the csvfile must
be provided.

Change-Id: I039c77b81a37ae4abd4f2b1bb2fd2f8613c05ad9
  • Loading branch information
oliverlee committed Jul 27, 2023
1 parent 72af018 commit f9348d9
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 7 deletions.
35 changes: 35 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,38 @@ http_archive(
load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")

hedron_compile_commands_setup()

http_archive(
name = "rules_python",
sha256 = "0a8003b044294d7840ac7d9d73eef05d6ceb682d7516781a4ec62eeb34702578",
strip_prefix = "rules_python-0.24.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.24.0/rules_python-0.24.0.tar.gz",
)

#load("@rules_python//python:repositories.bzl", "py_repositories")
#
#py_repositories()

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python3_10",
python_version = "3.10",
)

load("@rules_python//python/pip_install:repositories.bzl", "pip_install_dependencies")

pip_install_dependencies()

load("@rules_python//python:pip.bzl", "pip_parse")
load("@python3_10//:defs.bzl", "interpreter")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements = "//python:requirements.txt",
)

load("@pip//:requirements.bzl", "install_deps")

install_deps()
4 changes: 4 additions & 0 deletions python/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

# https://github.com/bazelbuild/rules_python/blob/1722988cc407b08a4e7770295452076706823f9d/docs/pip.md#compile_pip_requirements
compile_pip_requirements(name = "requirements")
4 changes: 4 additions & 0 deletions python/requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
numpy
matplotlib
pandas
tornado
355 changes: 355 additions & 0 deletions python/requirements.txt

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions python/setup.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 Toyota Motor Corporation. All rights reserved.
# Copyright 2022-2023 Woven Planet. All rights reserved.
"""
Sets up Python environment with Pip dependencies for users of @bazel_tooling.
"""

load("@python3_10//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")
load("//python:requirements.bzl", "install_deps")

def tooling_python_setup():
pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements = "//python:requirements.txt",
)

install_deps()
13 changes: 13 additions & 0 deletions scripts/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_python//python:defs.bzl", "py_binary")
load("@pip//:requirements.bzl", "requirement")

py_binary(
name = "plot_data",
srcs = ["plot_data.py"],
deps = [
requirement("numpy"),
requirement("matplotlib"),
requirement("pandas"),
requirement("tornado"),
],
)
15 changes: 8 additions & 7 deletions scripts/plot_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import argparse

import matplotlib.pyplot as plt
plt.switch_backend('WebAgg')

import numpy as np
import pandas as pd


def plot(csvfile):
"""Returns a matplotlib axes with data in ``csvfile`` plotted.
Expand All @@ -34,19 +35,19 @@ def plot(csvfile):
"""

df = pd.read_csv(csvfile, index_col='time')
df = pd.read_csv(csvfile, index_col='time', sep = ';')

fig, axes = plt.subplots(5, 1, sharex=True)

df[['ay', 'az']].plot(ax=axes[0])

df['wz'].pot(ax=axes[1])
df['wx'].plot(ax=axes[1])

df[['theta_a', 'theta_af']].apply(np.rad2deg).plot(axes[2])
df[['theta_a', 'theta_af']].apply(np.rad2deg).plot(ax=axes[2])

df[['theta_g', 'theta_gf']].apply(np.rad2deg).plot(axes[3])
df[['theta_g', 'theta_gf']].apply(np.rad2deg).plot(ax=axes[3])

df['theta'].apply(np.rad2deg).plot(axes[4])
df['theta'].apply(np.rad2deg).plot(ax=axes[4])

return axes

Expand All @@ -57,5 +58,5 @@ def plot(csvfile):
parser.add_argument('csvfile')
args = parser.parse_args()

plot(args.csvfile)
axis = plot(args.csvfile)
plt.show()

0 comments on commit f9348d9

Please sign in to comment.