Skip to content

Commit

Permalink
dvc: rename plot to plots
Browse files Browse the repository at this point in the history
Follows `dvc params/metrics` convention and is needed in preparation for new
commands and some refactoring.

Part of #3409
  • Loading branch information
efiop committed May 14, 2020
1 parent ff4fcc9 commit ef9ba3e
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 70 deletions.
4 changes: 2 additions & 2 deletions dvc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
move,
params,
pipeline,
plot,
plots,
remote,
remove,
repro,
Expand Down Expand Up @@ -74,7 +74,7 @@
version,
update,
git_hook,
plot,
plots,
]


Expand Down
104 changes: 56 additions & 48 deletions dvc/command/plot.py → dvc/command/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from dvc.command.base import CmdBase, append_doc_link, fix_subparsers
from dvc.exceptions import DvcException
from dvc.repo.plot.data import WORKSPACE_REVISION_NAME
from dvc.repo.plots.data import WORKSPACE_REVISION_NAME

logger = logging.getLogger(__name__)


class CmdPLot(CmdBase):
class CmdPlots(CmdBase):
def _revisions(self):
raise NotImplementedError

Expand Down Expand Up @@ -79,12 +79,12 @@ def run(self):
return 0


class CmdPlotShow(CmdPLot):
class CmdPlotsShow(CmdPlots):
def _revisions(self):
return None


class CmdPlotDiff(CmdPLot):
class CmdPlotsDiff(CmdPlots):
def _revisions(self):
revisions = self.args.revisions or []
if len(revisions) <= 1:
Expand All @@ -95,143 +95,151 @@ def _revisions(self):


def add_parser(subparsers, parent_parser):
PLOT_HELP = (
PLOTS_HELP = (
"Generating plots for metrics stored in structured files "
"(JSON, CSV, TSV)."
)

plot_parser = subparsers.add_parser(
"plot",
plots_parser = subparsers.add_parser(
"plots",
parents=[parent_parser],
description=append_doc_link(PLOT_HELP, "plot"),
help=PLOT_HELP,
description=append_doc_link(PLOTS_HELP, "plots"),
help=PLOTS_HELP,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
plot_subparsers = plot_parser.add_subparsers(
plots_subparsers = plots_parser.add_subparsers(
dest="cmd",
help="Use `dvc plot CMD --help` to display command-specific help.",
help="Use `dvc plots CMD --help` to display command-specific help.",
)

fix_subparsers(plot_subparsers)
fix_subparsers(plots_subparsers)

SHOW_HELP = "Generate a plot image file from a metrics file."
plot_show_parser = plot_subparsers.add_parser(
SHOW_HELP = "Generate a plots image file from a metrics file."
plots_show_parser = plots_subparsers.add_parser(
"show",
parents=[parent_parser],
description=append_doc_link(SHOW_HELP, "plot/show"),
description=append_doc_link(SHOW_HELP, "plots/show"),
help=SHOW_HELP,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
plot_show_parser.add_argument(
plots_show_parser.add_argument(
"-t",
"--template",
nargs="?",
default=None,
help="File to be injected with data.",
)
plot_show_parser.add_argument(
plots_show_parser.add_argument(
"-f", "--file", default=None, help="Name of the generated file."
)
plot_show_parser.add_argument(
plots_show_parser.add_argument(
"-s",
"--select",
default=None,
help="Choose which field(s) or JSONPath to include in the plot.",
help="Choose which field(s) or JSONPath to include in the plots.",
)
plot_show_parser.add_argument(
plots_show_parser.add_argument(
"-x", default=None, help="Field name for x axis."
)
plot_show_parser.add_argument(
plots_show_parser.add_argument(
"-y", default=None, help="Field name for y axis."
)
plot_show_parser.add_argument(
plots_show_parser.add_argument(
"--stdout",
action="store_true",
default=False,
help="Print plot specification to stdout.",
help="Print plots specification to stdout.",
)
plot_show_parser.add_argument(
plots_show_parser.add_argument(
"--no-csv-header",
action="store_true",
default=False,
help="Required when CSV or TSV datafile does not have a header.",
)
plot_show_parser.add_argument(
plots_show_parser.add_argument(
"--no-html",
action="store_true",
default=False,
help="Do not wrap Vega plot JSON with HTML.",
)
plot_show_parser.add_argument("--title", default=None, help="Plot title.")
plot_show_parser.add_argument("--xlab", default=None, help="X axis title.")
plot_show_parser.add_argument("--ylab", default=None, help="Y axis title.")
plot_show_parser.add_argument(
plots_show_parser.add_argument("--title", default=None, help="Plot title.")
plots_show_parser.add_argument(
"--xlab", default=None, help="X axis title."
)
plots_show_parser.add_argument(
"--ylab", default=None, help="Y axis title."
)
plots_show_parser.add_argument(
"datafile", nargs="?", default=None, help="Metrics file to visualize",
)
plot_show_parser.set_defaults(func=CmdPlotShow)
plots_show_parser.set_defaults(func=CmdPlotsShow)

PLOT_DIFF_HELP = (
PLOTS_DIFF_HELP = (
"Plot differences in metrics between commits in the DVC "
"repository, or between the last commit and the workspace."
)
plot_diff_parser = plot_subparsers.add_parser(
plots_diff_parser = plots_subparsers.add_parser(
"diff",
parents=[parent_parser],
description=append_doc_link(PLOT_DIFF_HELP, "plot/diff"),
help=PLOT_DIFF_HELP,
description=append_doc_link(PLOTS_DIFF_HELP, "plots/diff"),
help=PLOTS_DIFF_HELP,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"-t",
"--template",
nargs="?",
default=None,
help="File to be injected with data.",
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"-d",
"--datafile",
nargs="?",
default=None,
help="Metrics file to visualize",
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"-f", "--file", default=None, help="Name of the generated file."
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"-s",
"--select",
default=None,
help="Choose which field(s) or JSONPath to include in the plot.",
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"-x", default=None, help="Field name for x axis."
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"-y", default=None, help="Field name for y axis."
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"--stdout",
action="store_true",
default=False,
help="Print plot specification to stdout.",
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"--no-csv-header",
action="store_true",
default=False,
help="Provided CSV ot TSV datafile does not have a header.",
)
plot_diff_parser.add_argument(
plots_diff_parser.add_argument(
"--no-html",
action="store_true",
default=False,
help="Do not wrap Vega plot JSON with HTML.",
)
plot_diff_parser.add_argument("--title", default=None, help="Plot title.")
plot_diff_parser.add_argument("--xlab", default=None, help="X axis title.")
plot_diff_parser.add_argument("--ylab", default=None, help="Y axis title.")
plot_diff_parser.add_argument(
plots_diff_parser.add_argument("--title", default=None, help="Plot title.")
plots_diff_parser.add_argument(
"--xlab", default=None, help="X axis title."
)
plots_diff_parser.add_argument(
"--ylab", default=None, help="Y axis title."
)
plots_diff_parser.add_argument(
"revisions", nargs="*", default=None, help="Git commits to plot from",
)
plot_diff_parser.set_defaults(func=CmdPlotDiff)
plots_diff_parser.set_defaults(func=CmdPlotsDiff)
4 changes: 2 additions & 2 deletions dvc/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Repo(object):
from dvc.repo.get import get
from dvc.repo.get_url import get_url
from dvc.repo.update import update
from dvc.repo.plot import plot
from dvc.repo.plots import plot

def __init__(self, root_dir=None):
from dvc.state import State
Expand Down Expand Up @@ -426,7 +426,7 @@ def stages(self):

@cached_property
def plot_templates(self):
from dvc.repo.plot.template import PlotTemplates
from .plots.template import PlotTemplates

return PlotTemplates(self.dvc_dir)

Expand Down
9 changes: 5 additions & 4 deletions dvc/repo/plot/__init__.py → dvc/repo/plots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

from dvc.exceptions import DvcException
from dvc.repo import locked
from dvc.repo.plot.data import PlotData
from dvc.repo.plot.template import NoDataForTemplateError, Template

from .data import PlotData
from .template import NoDataForTemplateError, Template

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -82,7 +83,7 @@ def fill_template(

template_data = {}
for template_datafile in template_datafiles:
from dvc.repo.plot.data import _load_from_revisions
from .data import _load_from_revisions

plot_datas = _load_from_revisions(repo, template_datafile, revisions)
tmp_data = []
Expand Down Expand Up @@ -126,7 +127,7 @@ def plot(
repo, datafile=None, template=None, revisions=None, embed=False, **kwargs
):
if revisions is None:
from dvc.repo.plot.data import WORKSPACE_REVISION_NAME
from .data import WORKSPACE_REVISION_NAME

revisions = [WORKSPACE_REVISION_NAME]

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion dvc/repo/plot/template.py → dvc/repo/plots/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class DefaultScatterTemplate(Template):


class PlotTemplates:
TEMPLATES_DIR = "plot"
TEMPLATES_DIR = "plots"
TEMPLATES = [
DefaultLinearTemplate,
DefaultConfusionTemplate,
Expand Down
Empty file added tests/func/plots/__init__.py
Empty file.
12 changes: 6 additions & 6 deletions tests/func/test_plot.py → tests/func/plots/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from bs4 import BeautifulSoup
from funcy import first

from dvc.repo.plot import NoDataOrTemplateProvided
from dvc.repo.plot.data import (
from dvc.repo.plots import NoDataOrTemplateProvided
from dvc.repo.plots.data import (
NoMetricInHistoryError,
PlotData,
PlotMetricTypeError,
)
from dvc.repo.plot.template import (
from dvc.repo.plots.template import (
NoDataForTemplateError,
NoFieldInDataError,
TemplateNotFoundError,
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_plot_multiple_revs_default(tmp_dir, scm, dvc):


def test_plot_multiple_revs(tmp_dir, scm, dvc):
shutil.copy(tmp_dir / ".dvc" / "plot" / "default.json", "template.json")
shutil.copy(tmp_dir / ".dvc" / "plots" / "default.json", "template.json")

metric_1 = [{"y": 2}, {"y": 3}]
_write_json(tmp_dir, metric_1, "metric.json")
Expand Down Expand Up @@ -324,7 +324,7 @@ def test_throw_on_no_metric_at_all(tmp_dir, scm, dvc, caplog):
def custom_template(tmp_dir, dvc):
custom_template = tmp_dir / "custom_template.json"
shutil.copy(
tmp_dir / ".dvc" / "plot" / "default.json", custom_template,
tmp_dir / ".dvc" / "plots" / "default.json", custom_template,
)
return custom_template

Expand Down Expand Up @@ -380,7 +380,7 @@ def test_custom_template_with_specified_data(

def test_plot_override_specified_data_source(tmp_dir, scm, dvc):
shutil.copy(
tmp_dir / ".dvc" / "plot" / "default.json",
tmp_dir / ".dvc" / "plots" / "default.json",
tmp_dir / "newtemplate.json",
)
_replace(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import pytest

from dvc.cli import parse_args
from dvc.command.plot import CmdPlotDiff, CmdPlotShow
from dvc.command.plots import CmdPlotsDiff, CmdPlotsShow


def test_metrics_diff(mocker):
cli_args = parse_args(
[
"plot",
"plots",
"diff",
"--file",
"result.extension",
Expand All @@ -34,7 +34,7 @@ def test_metrics_diff(mocker):
"tag2",
]
)
assert cli_args.func == CmdPlotDiff
assert cli_args.func == CmdPlotsDiff

cmd = cli_args.func(cli_args)

Expand Down Expand Up @@ -63,7 +63,7 @@ def test_metrics_diff(mocker):
def test_metrics_show(mocker):
cli_args = parse_args(
[
"plot",
"plots",
"show",
"-f",
"result.extension",
Expand All @@ -77,7 +77,7 @@ def test_metrics_show(mocker):
"datafile",
]
)
assert cli_args.func == CmdPlotShow
assert cli_args.func == CmdPlotsShow

cmd = cli_args.func(cli_args)

Expand Down Expand Up @@ -115,7 +115,7 @@ def test_metrics_show(mocker):
def test_revisions(mocker, arg_revisions, is_dirty, expected_revisions):
args = mocker.MagicMock()

cmd = CmdPlotDiff(args)
cmd = CmdPlotsDiff(args)
mocker.patch.object(args, "revisions", arg_revisions)
mocker.patch.object(cmd.repo.scm, "is_dirty", return_value=is_dirty)

Expand Down
Empty file.

0 comments on commit ef9ba3e

Please sign in to comment.