Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Aug 10, 2020
1 parent 7b34702 commit 107c47e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 66 deletions.
3 changes: 2 additions & 1 deletion docs/source/examples/performance_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def setup(self):

# this will only make an image when run in jupyter notebook
from gpkit.interactive.sankey import Sankey
variablesankey = Sankey(sol, M).diagram(AC.wing.A)
sankey = Sankey(sol, M).diagram(left=40, width=950, right=150)
sankey.auto_save_svg("performance_modeling.svg")
sankey
sankey # pylint: disable=pointless-statement
74 changes: 23 additions & 51 deletions gpkit/interactive/sankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

def isnamedmodel(constraint):
"Checks if a constraint is a named model"
return isinstance(constraint, Model) and type(constraint) is not Model
return (isinstance(constraint, Model)
and constraint.__class__.__name__ != "Model")

def getcolor(value):
"color scheme for sensitivities"
Expand All @@ -34,9 +35,10 @@ def __init__(self, solution, constraintset, leftlabel=None):
self.links = defaultdict(float)
self.nodes = []

def constrlinks(self, constrset, target, i=0, depth=0, named=False):
# pylint: disable=too-many-branches
def link(self, constrset, target, var=None, i=0, depth=0, named=False):
"adds links of a given constraint set to self.links"
total_sens = 0
total_sens = None
switchedtarget = False
if not named and isnamedmodel(constrset) and 0 < depth <= self.maxdepth:
switchedtarget = target
Expand All @@ -50,66 +52,36 @@ def constrlinks(self, constrset, target, i=0, depth=0, named=False):
constrset = {k: constrset[i]
for k, i in constrset.idxlookup.items()}
if isinstance(constrset, dict):
for i, (label, constr) in enumerate(constrset.items()):
for i, (label, c) in enumerate(constrset.items()): # pylint: disable=redefined-argument-from-local
if depth > self.maxdepth or not self.constraintlabels:
subtotal_sens = self.constrlinks(constr, target, i, depth)
subtotal_sens = self.link(c, target, var, i, depth)
else:
source = "%s.%03i.%s" % (target, i, label)
self.nodes.append({"id": source, "title": label})
subtotal_sens = self.constrlinks(constr, source, i, depth+1,
named=isnamedmodel(constr))
self.links[source, target] += subtotal_sens
total_sens += subtotal_sens
elif isinstance(constrset, Iterable):
for i, constr in enumerate(constrset):
total_sens += self.constrlinks(constr, target, i, depth)
elif constrset in self.csenss:
total_sens = -abs(self.csenss[constrset])
if switchedtarget:
self.links[target, switchedtarget] += total_sens
return total_sens

def varlinks(self, constrset, target, key, i=0, depth=1):
"adds links of a given constraint set to self.links"
total_sens = None
switchedtarget = False
if (isinstance(constrset, Model) and type(constrset) is not Model
and depth <= self.maxdepth):
switchedtarget = target
name, _ = constrset.lineage[-1]
target = "%s.%03i.%s" % (switchedtarget, i, name)
self.nodes.append({"id": target, "title": name})
depth += 1
if getattr(constrset, "idxlookup", None):
constrset = {k: constrset[i]
for k, i in constrset.idxlookup.items()}
if isinstance(constrset, dict):
for i, (label, constr) in enumerate(constrset.items()):
if depth <= self.maxdepth:
source = "%s.%03i.%s" % (target, i, label)
self.nodes.append({"id": source, "title": label})
subtotal_sens = self.varlinks(constr, source, key, i, depth+1)
subtotal_sens = self.link(c, source, var, i, depth+1,
named=isnamedmodel(c))
if subtotal_sens is not None:
self.links[source, target] += subtotal_sens
else:
subtotal_sens = self.varlinks(constr, target, key, i, depth)
if subtotal_sens is not None:
if total_sens is None:
total_sens = 0
total_sens += subtotal_sens
elif isinstance(constrset, Iterable):
for i, constr in enumerate(constrset):
subtotal_sens = self.varlinks(constr, target, key, i, depth)
for i, c in enumerate(constrset): # pylint: disable=redefined-argument-from-local
subtotal_sens = self.link(c, target, var, i, depth)
if subtotal_sens is not None:
if total_sens is None:
total_sens = 0
total_sens += subtotal_sens
elif key in constrset.v_ss:
total_sens = constrset.v_ss[key]
elif var is None and constrset in self.csenss:
total_sens = -abs(self.csenss[constrset])
elif var is not None and var.key in constrset.v_ss:
total_sens = constrset.v_ss[var.key]
if switchedtarget and total_sens is not None:
self.links[target, switchedtarget] += total_sens
self.links[target, switchedtarget] += total_sens
return total_sens

# pylint: disable=too-many-locals
def diagram(self, variable=None, *, flowright=False, width=900, height=400,
top=0, bottom=0, left=120, right=55, maxdepth=None,
constraintlabels=None):
Expand All @@ -121,13 +93,13 @@ def diagram(self, variable=None, *, flowright=False, width=900, height=400,
self.maxdepth = maxdepth # NOTE: side effects
if constraintlabels is not None:
self.constraintlabels = constraintlabels # NOTE: side effects
if variable is None:
self.constrlinks(self.cset, self.leftlabel)
else:
self.links[self.leftlabel, str(variable)] = \
self.varlinks(self.cset, self.leftlabel, variable.key)
links = []

total_sens = self.link(self.cset, self.leftlabel, variable)
if variable is not None:
self.links[self.leftlabel, str(variable)] = total_sens

maxflow = np.abs(list(self.links.values())).max()
links = []
for (source, target), value in self.links.items():
if not flowright: # reverse by default, sigh
source, target = target, source
Expand Down
22 changes: 8 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@
import os
from distutils.core import setup

LONG_DESCRIPTION = """
GPkit is a Python package for defining and manipulating
geometric programming models,
abstracting away the backend solver.
Supported solvers are
`MOSEK <http://mosek.com>`_
and `CVXopt <http://cvxopt.org/>`_.
`Documentation <http://gpkit.rtfd.org/>`_
`Citing GPkit <http://gpkit.rtfd.org/en/latest/citinggpkit.html>`_
"""

LICENSE = """The MIT License (MIT)
Expand All @@ -38,13 +26,18 @@
SOFTWARE."""

# create blank settings file to replace anything cached
settings = os.sep.join([os.path.dirname(__file__), "gpkit", "env", "settings"])
THIS_DIR = os.path.dirname(__file__)
try:
with open(settings, "w") as f:
with open(os.sep.join([THIS_DIR, "gpkit", "env", "settings"]), "w") as f:
f.write("installed_solvers : ")
except IOError:
pass

# read the README file
with open(os.path.join(THIS_DIR, "README.md"), encoding="utf-8") as f:
LONG_DESCRIPTION = f.read()


setup(
name="gpkit",
description="Package for defining and manipulating geometric "
Expand All @@ -61,4 +54,5 @@
package_data={"gpkit": ["env/settings"]},
license=LICENSE,
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
)

0 comments on commit 107c47e

Please sign in to comment.