Skip to content

Commit

Permalink
document, lint, fix treemap
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Mar 21, 2021
1 parent bc0231e commit 6e9a319
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 40 deletions.
5 changes: 3 additions & 2 deletions docs/source/examples/migp.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/performance_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 10 additions & 0 deletions docs/source/examples/treemap.py
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Binary file added docs/source/figures/sizedconstrainttreemap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/figures/treemap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 27 additions & 4 deletions docs/source/visint.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
Visualization and Interaction
*****************************

Variable Reference Plots
Code in this section uses the `CE solar model <https://github.com/convexengineering/solar/tree/gpkitdocs>`_ except where noted otherwise.

Model Hierarchy Treemaps
========================

Code in this section uses the `CE solar model <https://github.com/convexengineering/solar/tree/gpkitdocs>`_.
.. 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
Expand Down Expand Up @@ -57,8 +82,6 @@ Requirements
Example
-------

Code in this section uses the `CE solar model <https://github.com/convexengineering/solar/tree/gpkitdocs>`_

.. code:: python
from solar.solar import *
Expand Down
2 changes: 1 addition & 1 deletion gpkit/constraints/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 11 additions & 10 deletions gpkit/interactive/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

2 changes: 1 addition & 1 deletion gpkit/solution_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions gpkit/tests/t_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 0 additions & 21 deletions gpkit/tests/t_interactive.py

This file was deleted.

0 comments on commit 6e9a319

Please sign in to comment.