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

45 statements  

1"An example to show off Breakdowns" 

2import os 

3import sys 

4import pickle 

5import pint 

6from packaging import version 

7from gpkit.breakdowns import Breakdowns 

8 

9dirpath = os.path.dirname(os.path.realpath(__file__)) + os.sep 

10if version.parse(pint.__version__) >= version.parse("0.13"): 

11 sol = pickle.load(open(dirpath+"solar_13.p", "rb")) 

12elif version.parse(pint.__version__) >= version.parse("0.12"): 

13 sol = pickle.load(open(dirpath+"solar_12.p", "rb")) 

14elif version.parse(pint.__version__) >= version.parse("0.10"): 

15 sol = pickle.load(open(dirpath+"solar_10.p", "rb")) 

16elif version.parse(pint.__version__) == version.parse("0.9"): 

17 sol = pickle.load(open(dirpath+"solar.p", "rb")) 

18else: 

19 sol = None 

20 

21if sol is not None: 

22 # the code to create solar.p is in ./breakdowns/solartest.py 

23 bds = Breakdowns(sol) 

24 

25 print("Cost breakdown (as seen in solution tables)") 

26 print("==============") 

27 bds.plot("cost") 

28 

29 print("Variable breakdowns (note the two methods of access)") 

30 print("===================") 

31 varkey, = sol["variables"].keymap[("Mission.FlightSegment.AircraftPerf" 

32 ".AircraftDrag.Poper")] 

33 bds.plot(varkey) 

34 bds.plot("AircraftPerf.AircraftDrag.MotorPerf.Q") 

35 

36 print("Combining the two above by increasing maxwidth") 

37 print("----------------------------------------------") 

38 bds.plot("AircraftPerf.AircraftDrag.Poper", maxwidth=105) 

39 

40 print("Model sensitivity breakdowns (note the two methods of access)") 

41 print("============================") 

42 bds.plot("model sensitivities") 

43 bds.plot("Aircraft") 

44 

45 print("Exhaustive variable breakdown traces (and configuration arguments)") 

46 print("====================================") 

47 # often useful as a reference point when reading traces 

48 bds.plot("AircraftPerf.AircraftDrag.Poper", height=12) 

49 # includes factors, can be useful for reading traces as well 

50 bds.plot("AircraftPerf.AircraftDrag.Poper", showlegend=True) 

51 print("\nPermissivity = 2 (the default)") 

52 print("----------------") 

53 bds.trace("AircraftPerf.AircraftDrag.Poper") 

54 print("\nPermissivity = 1 (stops at Pelec = v·i)") 

55 print("----------------") 

56 bds.trace("AircraftPerf.AircraftDrag.Poper", permissivity=1) 

57 

58 # you can also produce Plotly treemaps/icicle plots of your breakdowns 

59 fig = bds.treemap("model sensitivities", returnfig=True) 

60 fig = bds.icicle("cost", returnfig=True) 

61 # uncommenting any of the below makes and shows the plot directly 

62 # bds.icicle("model sensitivities") 

63 # bds.treemap("cost")