Coverage for src/jupytalk/talk_examples/pydata2016.py: 4%

90 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-26 01:14 +0200

1""" 

2@file 

3@brief Examples used in talks 

4""" 

5 

6 

7def example_networkx(ax=None, **options): 

8 """ 

9 Example using :epkg:`networkx`. 

10 

11 @param ax axis 

12 @param options look at the code 

13 @return ax 

14 """ 

15 import networkx as nx 

16 import matplotlib.pyplot as plt 

17 

18 G = nx.random_geometric_graph(200, 0.125) 

19 # position is stored as node attribute data for random_geometric_graph 

20 pos = nx.get_node_attributes(G, 'pos') 

21 

22 # find node near center (0.5,0.5) 

23 dmin = 1 

24 ncenter = 0 

25 for n in pos: 

26 x, y = pos[n] 

27 d = (x - 0.5) ** 2 + (y - 0.5) ** 2 

28 if d < dmin: 

29 ncenter = n 

30 dmin = d 

31 

32 # color by path length from node near center 

33 p = nx.single_source_shortest_path_length(G, ncenter) 

34 

35 if ax is None: 

36 _, ax = plt.subplots( 

37 nrows=1, ncols=1, figsize=options.get('figsize', (5, 5))) 

38 

39 nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4, ax=ax) 

40 nx.draw_networkx_nodes(G, pos, nodelist=p.keys(), 

41 node_size=80, ax=ax) 

42 

43 ax.set_xlim(-0.05, 1.05) 

44 ax.set_ylim(-0.05, 1.05) 

45 ax.axis('off') 

46 return ax 

47 

48 

49def example_confidence_interval(ax=None, seaborn=False, **options): 

50 """ 

51 Draws pseudo confidence interval for a regression 

52 in a :epkg:`matplotlib` graph. 

53 

54 @param ax axis 

55 @param seaborn uses :epkg:`seaborn` 

56 instead of :epkg:`matplotlib` 

57 @param options look at the code 

58 @return ax 

59 """ 

60 import matplotlib.pyplot as plt 

61 

62 if ax is None: 

63 _, ax = plt.subplots( 

64 nrows=1, ncols=1, figsize=options.get('figsize', (5, 5))) 

65 

66 import scipy 

67 import numpy 

68 nx, nboot = 22, 400 

69 x = scipy.linspace(0.0, 1.0, nx) # pylint: disable=E1101 

70 data = x + numpy.random.normal(loc=0.0, scale=0.1, size=nx) 

71 yp = scipy.polyfit(x, data, 1) # pylint: disable=E1101 

72 y = scipy.polyval(yp, x) # pylint: disable=E1101 

73 

74 if seaborn: 

75 from seaborn import regplot 

76 return regplot(x=x, y=y, ax=ax) 

77 else: 

78 r = data - y 

79 for _ in range(nboot): 

80 pc = scipy.polyfit( # pylint: disable=E1101 

81 x, y + r[scipy.random.randint(0, nx - 1, nx)], 1) # pylint: disable=E1101 

82 ax.plot(x, scipy.polyval(pc, x), 'k-', # pylint: disable=E1101 

83 linewidth=2, alpha=3.0 / float(nboot)) 

84 ax.plot(x, y, 'k-') 

85 ax.plot(x, data, 'ko') 

86 return ax 

87 

88 

89def example_cartopy(ax=None, **options): 

90 """ 

91 Draws a map of France 

92 with :epkg:`cartopy`. 

93 

94 @param ax axis 

95 @param options look at the code 

96 @return ax 

97 """ 

98 import cartopy.crs as ccrs 

99 import cartopy.feature as cfeature 

100 import matplotlib.pyplot as plt 

101 

102 fig = plt.figure(figsize=(7, 7)) 

103 ax = fig.add_subplot( 

104 1, 1, 1, projection=ccrs.PlateCarree()) # pylint: disable=E0110 

105 ax.set_extent([-5, 10, 42, 52]) 

106 

107 ax.add_feature(cfeature.OCEAN) 

108 ax.add_feature(cfeature.COASTLINE) 

109 ax.add_feature(cfeature.RIVERS) 

110 ax.add_feature(cfeature.LAKES) 

111 ax.add_feature(cfeature.LAND) 

112 ax.add_feature(cfeature.BORDERS, linestyle=':') 

113 ax.plot([2.35, 2.20], [48.85, 48.71], '.') 

114 ax.text(2.35, 48.85, "Paris") 

115 ax.text(2.20, 48.71, "Saclay", ha="right") 

116 ax.set_title('France') 

117 return ax 

118 

119 

120def example_pydy(ax=None, **options): 

121 """ 

122 Example from the documentation of 

123 :epkg:`pydy`. 

124 

125 @param ax matplotlib axis 

126 @parm options extra options 

127 @return ax 

128 """ 

129 # part 1 

130 

131 from sympy import symbols 

132 import sympy.physics.mechanics as me 

133 

134 mass, stiffness, damping, gravity = symbols('m, k, c, g') 

135 

136 position, speed = me.dynamicsymbols('x v') 

137 positiond = me.dynamicsymbols('x', 1) 

138 force = me.dynamicsymbols('F') 

139 

140 ceiling = me.ReferenceFrame('N') 

141 

142 origin = me.Point('origin') 

143 origin.set_vel(ceiling, 0) 

144 

145 center = origin.locatenew('center', position * ceiling.x) 

146 center.set_vel(ceiling, speed * ceiling.x) 

147 

148 block = me.Particle('block', center, mass) 

149 

150 kinematic_equations = [speed - positiond] 

151 

152 force_magnitude = mass * gravity - stiffness * position - damping * speed + force 

153 forces = [(center, force_magnitude * ceiling.x)] 

154 

155 particles = [block] 

156 

157 kane = me.KanesMethod(ceiling, q_ind=[position], u_ind=[speed], 

158 kd_eqs=kinematic_equations) 

159 kane.kanes_equations(forces, particles) 

160 

161 # part 2 

162 

163 from numpy import linspace, sin 

164 from pydy.system import System 

165 

166 sys = System(kane, 

167 constants={mass: 1.0, stiffness: 1.0, 

168 damping: 0.2, gravity: 9.8}, 

169 specifieds={force: lambda x, t: sin(t)}, 

170 initial_conditions={position: 0.1, speed: -1.0}, 

171 times=linspace(0.0, 10.0, 1000)) 

172 

173 y = sys.integrate() 

174 

175 # part 3 

176 

177 import matplotlib.pyplot as plt 

178 if ax is None: 

179 _, ax = plt.subplots( 

180 nrows=1, ncols=1, figsize=options.get('figsize', (5, 5))) 

181 

182 ax.plot(sys.times, y) 

183 ax.legend((str(position), str(speed))) 

184 return ax