diff --git a/docs/source/examples/migp.py b/docs/source/examples/migp.py index 372c7ab1f..dd5940839 100644 --- a/docs/source/examples/migp.py +++ b/docs/source/examples/migp.py @@ -1,7 +1,8 @@ +"Example choice variable usage" import numpy as np -from gpkit import * +from gpkit import Variable, Model -x = Variable("x", choices=range(1,4)) +x = Variable("x", choices=range(1, 4)) num = Variable("numerator", np.linspace(0.5, 7, 11)) m = Model(x + num/x) diff --git a/docs/source/examples/performance_modeling.py b/docs/source/examples/performance_modeling.py index a0a50f57a..40f0320e6 100644 --- a/docs/source/examples/performance_modeling.py +++ b/docs/source/examples/performance_modeling.py @@ -250,6 +250,6 @@ def setup(self): sankey # pylint: disable=pointless-statement except (ImportError, ModuleNotFoundError): print("Making Sankey diagrams requires the ipysankeywidget package") - + from gpkit.interactive.references import referencesplot referencesplot(M, openimmediately=False) diff --git a/docs/source/examples/treemap.py b/docs/source/examples/treemap.py new file mode 100644 index 000000000..a17039975 --- /dev/null +++ b/docs/source/examples/treemap.py @@ -0,0 +1,10 @@ +"Treemap example" +import plotly # pylint: disable=unused-import +from gpkit.interactive.plotting import treemap +from performance_modeling import M + +fig = treemap(M) +# plotly.offline.plot(fig, filename="treemap.html") # uncomment to show + +fig = treemap(M, itemize="constraints", sizebycount=True) +# plotly.offline.plot(fig, filename="sizedtreemap.html") # uncomment to show diff --git a/docs/source/examples/treemap_output.txt b/docs/source/examples/treemap_output.txt new file mode 100644 index 000000000..e69de29bb diff --git a/docs/source/figures/sizedconstrainttreemap.png b/docs/source/figures/sizedconstrainttreemap.png new file mode 100644 index 000000000..15a4458bb Binary files /dev/null and b/docs/source/figures/sizedconstrainttreemap.png differ diff --git a/docs/source/figures/treemap.png b/docs/source/figures/treemap.png new file mode 100644 index 000000000..7a9654f36 Binary files /dev/null and b/docs/source/figures/treemap.png differ diff --git a/docs/source/visint.rst b/docs/source/visint.rst index 234bf88dc..1a9173232 100644 --- a/docs/source/visint.rst +++ b/docs/source/visint.rst @@ -1,10 +1,35 @@ Visualization and Interaction ***************************** -Variable Reference Plots +Code in this section uses the `CE solar model `_ except where noted otherwise. + +Model Hierarchy Treemaps ======================== -Code in this section uses the `CE solar model `_. +.. code:: python + + import plotly + from gpkit.interactive.plotting import treemap + from solar.solar import * + Vehicle = Aircraft(Npod=3, sp=True) + M = Mission(Vehicle, latitude=[20]) + fig = treemap(M) + plotly.offline.plot(fig, filename="treemap.html") + +.. figure:: figures/treemap.png + +and, using sizing and counting by constraints instead of variables (the default): + +.. code:: python + + fig = treemap(M, itemize="constraints", sizebycount=True) + plotly.offline.plot(fig, filename="sizedtreemap.html") + +.. figure:: figures/sizedconstrainttreemap.png + + +Variable Reference Plots +======================== .. code:: python @@ -57,8 +82,6 @@ Requirements Example ------- -Code in this section uses the `CE solar model `_ - .. code:: python from solar.solar import * diff --git a/gpkit/constraints/gp.py b/gpkit/constraints/gp.py index b236596e7..8fcfa3a94 100644 --- a/gpkit/constraints/gp.py +++ b/gpkit/constraints/gp.py @@ -323,7 +323,7 @@ def _compile_result(self, solver_out): " `sol[\"choicevariables\"]`." % sorted(self.choicevaridxs.keys()), self.choicevaridxs)]} return SolutionArray(result) - elif self.choicevaridxs: + if self.choicevaridxs: result["warnings"] = {"Freed Choice Variables": [(\ "This model has the discretized choice variables" " %s, but since the '%s' solver doesn't support discretization" diff --git a/gpkit/interactive/plotting.py b/gpkit/interactive/plotting.py index 176cac5fc..e6ca5d142 100644 --- a/gpkit/interactive/plotting.py +++ b/gpkit/interactive/plotting.py @@ -93,17 +93,19 @@ def treemap(model, itemize="variables", sizebycount=False): sizes = [] if itemize == "variables": - items = model.varkeys + lineagestrs = [l.lineagestr() or "Model" for l in model.varkeys] elif itemize == "constraints": - items = model.flat() - lineagestrs = [] - for item in items: - lineagestrs.append(item.lineagestr()) + lineagestrs = [l.lineagestr() or "Model" for l in model.flat()] modelcount = Counter(lineagestrs) for modelname, count in modelcount.items(): modelnames.append(modelname) - parent = modelname.rsplit(".", 1)[0] + if "." in modelname: + parent = modelname.rsplit(".", 1)[0] + elif modelname != "Model": + parent = "Model" + else: + parent = "" parents.append(parent) sizes.append(count) @@ -112,18 +114,17 @@ def treemap(model, itemize="variables", sizebycount=False): modelnames.append(parent) if "." in parent: grandparent = parent.rsplit(".", 1)[0] + elif parent != "Model": + grandparent = "Model" else: grandparent = "" parents.append(grandparent) sizes.append(0) - values = sizes if sizebycount else None - fig = go.Figure(go.Treemap( ids=modelnames, labels=[modelname.split(".")[-1] for modelname in modelnames], parents=parents, - values=values, + values=sizes if sizebycount else None, )) return fig - diff --git a/gpkit/solution_array.py b/gpkit/solution_array.py index 30fe3eda9..5b1f0433b 100644 --- a/gpkit/solution_array.py +++ b/gpkit/solution_array.py @@ -706,7 +706,7 @@ def table(self, showvars=(), if "sensitivities" not in self and ("sensitivities" in table or "constraints" in table): continue - elif table == "cost": + if table == "cost": cost = self["cost"] # pylint: disable=unsubscriptable-object if kwargs.get("latex", None): # cost is not printed for latex continue diff --git a/gpkit/tests/t_examples.py b/gpkit/tests/t_examples.py index c84bb9cb6..bcab2d90f 100644 --- a/gpkit/tests/t_examples.py +++ b/gpkit/tests/t_examples.py @@ -75,6 +75,9 @@ def test_autosweep(self, example): assert_logtol(sol_ac("A"), (A_ac/3)**2, tol2) assert_logtol(sol_ac["cost"], (A_ac/3)**4, tol2) + def test_treemap(self, example): + pass + def test_checking_result_changes(self, example): sol = example.sol self.assertAlmostEqual(sol["cost"], 0.48, 2) diff --git a/gpkit/tests/t_interactive.py b/gpkit/tests/t_interactive.py deleted file mode 100644 index fd20fd536..000000000 --- a/gpkit/tests/t_interactive.py +++ /dev/null @@ -1,21 +0,0 @@ -"""Tests for interactive plotting tools""" -import unittest -from gpkit.interactive.plotting import treemap -import gpkit - - -class TestPlotting(unittest.TestCase): - """TestCase for gpkit.interactive.plotting""" - def test_treemap(self): - # m = Model() TODO - fig = treemap(m) - # fig.show(renderer="browser") - - -TESTS = [TestPlotting] - - -if __name__ == "__main__": # pragma: no cover - # pylint: disable=wrong-import-position - from gpkit.tests.helpers import run_tests - run_tests(TESTS)