Skip to content

Commit

Permalink
sankey working on individual SP GPs, as_posyslts returns copy (#1283)
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd authored Mar 21, 2018
1 parent 1fbc1c9 commit 71478ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion gpkit/interactive/sankey.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"implements Sankey"
from collections import defaultdict
from ipysankeywidget import SankeyWidget # pylint: disable=import-error
from ipywidgets import Layout
from gpkit import ConstraintSet, Model
from gpkit import GeometricProgram, SequentialGeometricProgram
from gpkit.nomials.math import MonomialEquality
Expand Down Expand Up @@ -70,6 +71,8 @@ def varlinks(self, constrset, key, target=None, printing=True):
+ key.unitstr(into=" [%s]", dimless=" [-]"))
self.nodes.append({"id": source, "title": shortname})
self.links[target, source] += value
# make sure to show connections
self.links[target, source] = max(self.links[target, source], 1e-15)
if key in self.gp.result["sensitivities"]["cost"]:
cost_senss = self.gp.result["sensitivities"]["cost"]
value = -cost_senss[key] # sensitivites flow _from_ cost
Expand Down Expand Up @@ -139,6 +142,9 @@ def diagram(self, variables=None, flowright=False, width=900, height=400,
# use inverted circled numbers to id the variables...
node["title"] += " " + unichr(0x2776+lookup[node["id"]])
elif "passthrough" in node:
for nodes in self.links:
if node["id"] in nodes: # make sure to show connections
self.links[nodes] = max(self.links[nodes], 1e-15)
cn = node.pop("passthrough")
l_idx = lookup[str(cn.left.hmap.keys()[0].keys()[0])]
r_idx = lookup[str(cn.right.hmap.keys()[0].keys()[0])]
Expand All @@ -157,7 +163,9 @@ def diagram(self, variables=None, flowright=False, width=900, height=400,
"value": abs(value) or 1e-30,
"color": getcolor(value)})
return SankeyWidget(nodes=self.nodes, links=links,
margins=margins, width=width, height=height)
layout=Layout(width=str(width),
height=str(height)),
margins=margins)

@property
def variable_properties(self):
Expand Down
4 changes: 4 additions & 0 deletions gpkit/keydict.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def __setitem__(self, key, value):
value = np.array([clean_value(key, v) for v in value])
if getattr(value, "shape", False) and dict.__contains__(self, key):
goodvals = ~isnan(value)
if self[key].dtype != value.dtype:
# e.g., we're replacing a number with a linked function
dict.__setitem__(self, key, np.array(self[key],
dtype=value.dtype))
self[key][goodvals] = value[goodvals]
else:
dict.__setitem__(self, key, value)
Expand Down
2 changes: 1 addition & 1 deletion gpkit/nomials/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def subval(posy):

def as_gpconstr(self, x0, substitutions): # pylint: disable=unused-argument
"The GP version of a Posynomial constraint is itself"
return self
return self.__class__(self.left, self.oper, self.right) # a copy

class MonomialEquality(PosynomialInequality):
"A Constraint of the form Monomial == Monomial."
Expand Down

0 comments on commit 71478ca

Please sign in to comment.