Coverage for docs/source/examples/breakdowns.py: 91%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"An example to show off Breakdowns"
2import os
3import pickle
4import pint
5from packaging import version
6from gpkit.breakdowns import Breakdowns
8dirpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
9if version.parse(pint.__version__) >= version.parse("0.13"):
10 sol = pickle.load(open(dirpath+"solar_13.p", "rb"))
11elif version.parse(pint.__version__) >= version.parse("0.12"):
12 sol = pickle.load(open(dirpath+"solar_12.p", "rb"))
13elif version.parse(pint.__version__) >= version.parse("0.10"):
14 sol = pickle.load(open(dirpath+"solar_10.p", "rb"))
15elif version.parse(pint.__version__) == version.parse("0.9"):
16 sol = pickle.load(open(dirpath+"solar.p", "rb"))
17else:
18 sol = None
20if sol is not None:
21 # the code to create solar.p is in ./breakdowns/solartest.py
22 bds = Breakdowns(sol)
24 print("Cost breakdown (as seen in solution tables)")
25 print("==============")
26 bds.plot("cost")
28 print("Variable breakdowns (note the two methods of access)")
29 print("===================")
30 varkey, = sol["variables"].keymap[("Mission.FlightSegment.AircraftPerf"
31 ".AircraftDrag.Poper")]
32 bds.plot(varkey)
33 bds.plot("AircraftPerf.AircraftDrag.MotorPerf.Q")
35 print("Combining the two above by increasing maxwidth")
36 print("----------------------------------------------")
37 bds.plot("AircraftPerf.AircraftDrag.Poper", maxwidth=105)
39 print("Model sensitivity breakdowns (note the two methods of access)")
40 print("============================")
41 bds.plot("model sensitivities")
42 bds.plot("Aircraft")
44 print("Exhaustive variable breakdown traces (and configuration arguments)")
45 print("====================================")
46 # often useful as a reference point when reading traces
47 bds.plot("AircraftPerf.AircraftDrag.Poper", height=12)
48 # includes factors, can be useful for reading traces as well
49 bds.plot("AircraftPerf.AircraftDrag.Poper", showlegend=True)
50 print("\nPermissivity = 2 (the default)")
51 print("----------------")
52 bds.trace("AircraftPerf.AircraftDrag.Poper")
53 print("\nPermissivity = 1 (stops at Pelec = v·i)")
54 print("----------------")
55 bds.trace("AircraftPerf.AircraftDrag.Poper", permissivity=1)
57 # you can also produce Plotly treemaps/icicle plots of your breakdowns
58 fig = bds.treemap("model sensitivities", returnfig=True)
59 fig = bds.icicle("cost", returnfig=True)
60 # uncommenting any of the below makes and shows the plot directly
61 # bds.icicle("model sensitivities")
62 # bds.treemap("cost")