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 

21# our Miniconda windows test platform can't print unicode 

22if sys.platform[:3] != "win" and sol is not None: 

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

24 bds = Breakdowns(sol) 

25 

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

27 print("==============") 

28 bds.plot("cost") 

29 

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

31 print("===================") 

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

33 ".AircraftDrag.Poper")] 

34 bds.plot(varkey) 

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

36 

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

38 print("----------------------------------------------") 

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

40 

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

42 print("============================") 

43 bds.plot("model sensitivities") 

44 bds.plot("Aircraft") 

45 

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

47 print("====================================") 

48 # often useful as a reference point when reading traces 

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

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

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

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

53 print("----------------") 

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

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

56 print("----------------") 

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

58 

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

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

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

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

63 # bds.icicle("model sensitivities") 

64 # bds.treemap("cost")