piegy 2.1.0__cp310-cp310-win32.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- piegy/C_core/piegyc.cp310-win32.pyd +0 -0
- piegy/__init__.py +56 -0
- piegy/__version__.py +31 -0
- piegy/analysis.py +222 -0
- piegy/data_tools.py +127 -0
- piegy/figures.py +491 -0
- piegy/simulation.py +500 -0
- piegy/simulation_py.py +817 -0
- piegy/test_var.py +583 -0
- piegy/tools/__init__.py +15 -0
- piegy/tools/figure_tools.py +238 -0
- piegy/tools/file_tools.py +29 -0
- piegy/videos.py +303 -0
- piegy-2.1.0.dist-info/METADATA +113 -0
- piegy-2.1.0.dist-info/RECORD +18 -0
- piegy-2.1.0.dist-info/WHEEL +5 -0
- piegy-2.1.0.dist-info/licenses/LICENSE.txt +28 -0
- piegy-2.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,238 @@
|
|
1
|
+
'''
|
2
|
+
Helper functions for making figures.
|
3
|
+
|
4
|
+
Functions:
|
5
|
+
- heatmap: Make a heatmap based on input data. Sets title, text ... as well
|
6
|
+
- bar: Make a barplot. Sets title, text ... as well.
|
7
|
+
- scatter: Make a scatter plot. Sets title, text ... as well.
|
8
|
+
- gen_title: Generates a title when the plot is about an interval of time.
|
9
|
+
- gen_text: Generates a text about standard deviation info.
|
10
|
+
- scale_interval: scale interval if mod's data was already reduced.
|
11
|
+
- ave_interval: Calculates average value of data over a time interval.
|
12
|
+
- ave_interval_1D: Return in a 1D format.
|
13
|
+
- config_mpl: Configure Matplotlib parameters in a nice format
|
14
|
+
'''
|
15
|
+
|
16
|
+
|
17
|
+
import matplotlib.pyplot as plt
|
18
|
+
import numpy as np
|
19
|
+
import seaborn as sns
|
20
|
+
|
21
|
+
# move ax a bit left if add text
|
22
|
+
# default value is [0.125, 0.11, 0.9, 0.88]
|
23
|
+
|
24
|
+
|
25
|
+
def heatmap(data, ax = None, cmap = "Greens", annot = False, fmt = '.3g', title = None, text = None):
|
26
|
+
'''
|
27
|
+
Helper function for making heatmaps.
|
28
|
+
|
29
|
+
Inputs:
|
30
|
+
data: 1D data for which you want to make a heatmap.
|
31
|
+
ax: matplotlib ax to plot on.
|
32
|
+
cmap: Color of heatmap. Uses matplotlib color maps
|
33
|
+
annot: Whether to show numbers of every block.
|
34
|
+
fmt: Number format for annotations. How many digits you want to keep.
|
35
|
+
title: The title you want to add. None means no title.
|
36
|
+
text: Adds some text in a text block at the top-right corner.
|
37
|
+
|
38
|
+
Returns:
|
39
|
+
fig: Seaborn heatmap.
|
40
|
+
'''
|
41
|
+
|
42
|
+
if ax == None:
|
43
|
+
_, ax = plt.subplots()
|
44
|
+
|
45
|
+
if text != None:
|
46
|
+
ax.text(0.8, 1.025, text, size = 10, linespacing = 1.5, transform = ax.transAxes)
|
47
|
+
|
48
|
+
ax = sns.heatmap(data, ax = ax, cmap = cmap, annot = annot, fmt = fmt)
|
49
|
+
ax.set_title(title, x = 0.5, y = 1)
|
50
|
+
|
51
|
+
return ax
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def bar(data, ax = None, color = "green", xlabel = None, ylabel = None, title = None, text = None):
|
56
|
+
'''
|
57
|
+
Helper Function for making barplots.
|
58
|
+
|
59
|
+
Inputs:
|
60
|
+
data: 2D data to make barplot.
|
61
|
+
ax: matplotlib ax to plot on.
|
62
|
+
color: Uses Matplotlib colors.
|
63
|
+
xlabel, y_label:
|
64
|
+
Label for axes.
|
65
|
+
title: Title for the barplot.
|
66
|
+
text: Adds some text in a text block at the top-right corner.
|
67
|
+
|
68
|
+
Returns:
|
69
|
+
fig: A Matplotlib barplot.
|
70
|
+
'''
|
71
|
+
|
72
|
+
N = np.array(data).size
|
73
|
+
xaxis = np.array([i for i in range(N)])
|
74
|
+
|
75
|
+
if ax == None:
|
76
|
+
if N > 60:
|
77
|
+
# make figure larger if has more data points
|
78
|
+
_, ax = plt.subplots(figsize = (min(N * 0.107, 6.4), 4.8))
|
79
|
+
else:
|
80
|
+
_, ax = plt.subplots()
|
81
|
+
|
82
|
+
if text != None:
|
83
|
+
ax.text(0.7, 1.025, text, size = 10, linespacing = 1.5, transform = ax.transAxes)
|
84
|
+
|
85
|
+
ax.bar(x = xaxis, height = data, color = color)
|
86
|
+
ax.set_xlabel(xlabel)
|
87
|
+
ax.set_ylabel(ylabel)
|
88
|
+
ax.set_title(title, x = 0.5, y = 1)
|
89
|
+
|
90
|
+
return ax
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
def scatter(X, Y, ax = None, color = "orange", alpha = 0.25, xlabel = "x", ylabel = "y", title = None):
|
95
|
+
'''
|
96
|
+
Helper function for makeing scatter plots.
|
97
|
+
|
98
|
+
Inputs:
|
99
|
+
X: x-coordinates of points.
|
100
|
+
Y: y-coordinates of points.
|
101
|
+
ax: matplotlib ax to plot on.
|
102
|
+
Note color is Matplotlib colors.
|
103
|
+
|
104
|
+
Returns:
|
105
|
+
fig: A Matplotlib scatter plot.
|
106
|
+
'''
|
107
|
+
|
108
|
+
if ax == None:
|
109
|
+
_, ax = plt.subplots(figsize = (7.2, 5.4))
|
110
|
+
ax.scatter(X, Y, color = color, alpha = alpha)
|
111
|
+
|
112
|
+
ax.set_xlabel(xlabel)
|
113
|
+
ax.set_ylabel(ylabel)
|
114
|
+
ax.set_title(title)
|
115
|
+
|
116
|
+
return ax
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
def gen_title(title, start, end):
|
121
|
+
'''
|
122
|
+
Generate a title for plot when it's about an interval of time.
|
123
|
+
'''
|
124
|
+
title += ", " + str(round(start * 100, 1)) + "~" + str(round(end * 100, 1)) + "%"
|
125
|
+
return title
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def gen_text(ave, std):
|
130
|
+
'''
|
131
|
+
Generate text about standard deviation info.
|
132
|
+
'''
|
133
|
+
text = "ave=" + str(round(ave, 2)) + ", std=" + str(round(std, 2))
|
134
|
+
return text
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
def ave_interval(data, start_index, end_index):
|
139
|
+
'''
|
140
|
+
Calculate average value of data over an interval. Return a 2D np.array
|
141
|
+
Assume data is 3D with shape N x M x K, then takes average on the 3rd axis.
|
142
|
+
|
143
|
+
Input:
|
144
|
+
data: 3D np.array or list. Will take average on the 3rd axis.
|
145
|
+
start_index, end_index:
|
146
|
+
over what interval to take average.
|
147
|
+
|
148
|
+
Returns:
|
149
|
+
data_ave: 2D np.array with shape N x M, contains average value of data.
|
150
|
+
'''
|
151
|
+
|
152
|
+
N = len(data)
|
153
|
+
M = len(data[0])
|
154
|
+
|
155
|
+
# plot a particular record
|
156
|
+
if start_index == end_index:
|
157
|
+
start_index = end_index - 1
|
158
|
+
|
159
|
+
data_ave = np.zeros((N, M))
|
160
|
+
|
161
|
+
for i in range(N):
|
162
|
+
for j in range(M):
|
163
|
+
for k in range(start_index, end_index):
|
164
|
+
data_ave[i][j] += data[i][j][k]
|
165
|
+
data_ave[i][j] /= (end_index - start_index)
|
166
|
+
|
167
|
+
return data_ave
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
def ave_interval_1D(data, start_index, end_index):
|
172
|
+
'''
|
173
|
+
Calculate average value of data over an interval. Return a 1D np.array.
|
174
|
+
Assume data is 3D and has shape (1 x M x K) or (N x 1 x K). Then implicitly 'compress' that 1 and takes average on the 3rd axis.
|
175
|
+
|
176
|
+
Input:
|
177
|
+
data: 3D np.array or list. One of its dimensions must have size 1. Will take average on the 3rd axis.
|
178
|
+
start_index, end_index:
|
179
|
+
over what interval to take average.
|
180
|
+
|
181
|
+
Returns:
|
182
|
+
data_ave: 1D np.array with len N * M, contains average value of data.
|
183
|
+
'''
|
184
|
+
|
185
|
+
N = len(data)
|
186
|
+
M = len(data[0])
|
187
|
+
|
188
|
+
if start_index == end_index:
|
189
|
+
start_index = end_index - 1
|
190
|
+
|
191
|
+
data_ave = np.zeros(N * M)
|
192
|
+
|
193
|
+
for i in range(N):
|
194
|
+
for j in range(M):
|
195
|
+
for k in range(start_index, end_index):
|
196
|
+
data_ave[i * M + j] += data[i][j][k]
|
197
|
+
data_ave[i * M + j] /= (end_index - start_index)
|
198
|
+
|
199
|
+
return data_ave
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
def scale_interval(interval, compress_itv):
|
204
|
+
# scale interval if mod's data was already reduced.
|
205
|
+
if compress_itv < 1:
|
206
|
+
raise ValueError('figures.scale_interval has compress_itv < 1:', compress_itv)
|
207
|
+
|
208
|
+
interval = int(interval / compress_itv)
|
209
|
+
if interval == 0:
|
210
|
+
print('Warning: data already smoothed by an interval: mod.compress_itv =', compress_itv, 'which is coarser than your', interval)
|
211
|
+
interval = 1
|
212
|
+
|
213
|
+
return interval
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
def config_mpl(mpl):
|
218
|
+
'''
|
219
|
+
Configure Matplotlib figures
|
220
|
+
'''
|
221
|
+
mpl.rcParams['savefig.dpi'] = 300
|
222
|
+
|
223
|
+
mpl.rcParams['font.family'] = 'serif'
|
224
|
+
mpl.rcParams['font.serif'] = plt.rcParams['font.serif']
|
225
|
+
mpl.rcParams['lines.linewidth'] = 1.75
|
226
|
+
mpl.rcParams['font.size'] = 11
|
227
|
+
mpl.rcParams['axes.labelsize'] = 12
|
228
|
+
mpl.rcParams['xtick.major.size'] = 10
|
229
|
+
mpl.rcParams['ytick.major.size'] = 9
|
230
|
+
mpl.rcParams['xtick.minor.size'] = 4
|
231
|
+
mpl.rcParams['ytick.minor.size'] = 4
|
232
|
+
|
233
|
+
mpl.rcParams['xtick.major.width'] = 1.5
|
234
|
+
mpl.rcParams['ytick.major.width'] = 1.5
|
235
|
+
mpl.rcParams['xtick.minor.width'] = 1.5
|
236
|
+
mpl.rcParams['ytick.minor.width'] = 1.5
|
237
|
+
|
238
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
'''
|
2
|
+
File-processing tools.
|
3
|
+
|
4
|
+
Functions:
|
5
|
+
- del_dirs: Delete everything in a directory, as well as the directory itself.
|
6
|
+
'''
|
7
|
+
|
8
|
+
import os
|
9
|
+
|
10
|
+
|
11
|
+
def del_dirs(dirs):
|
12
|
+
# Delete everything in a directory.
|
13
|
+
|
14
|
+
subdirs_list = []
|
15
|
+
|
16
|
+
for subdirs, dirs_, files in os.walk(dirs):
|
17
|
+
if subdirs not in subdirs_list:
|
18
|
+
subdirs_list.append(subdirs)
|
19
|
+
|
20
|
+
for file in files:
|
21
|
+
path = os.path.join(subdirs, file)
|
22
|
+
if os.path.isfile(path):
|
23
|
+
os.remove(path)
|
24
|
+
|
25
|
+
len_s = len(subdirs_list)
|
26
|
+
|
27
|
+
for i in range(len_s):
|
28
|
+
os.rmdir(subdirs_list[len_s - i - 1])
|
29
|
+
|
piegy/videos.py
ADDED
@@ -0,0 +1,303 @@
|
|
1
|
+
'''
|
2
|
+
Make mp4 videos for simulation results.
|
3
|
+
|
4
|
+
Videos are made by:
|
5
|
+
make every frame by figures.py functions, then put frames together into a video.
|
6
|
+
|
7
|
+
Public Function:
|
8
|
+
- make_video: make video based simulation results.
|
9
|
+
|
10
|
+
Private Functions
|
11
|
+
- get_max_lim: Get the max lim (interval) over many lims, and then expand it a bit for better accommodation.
|
12
|
+
Essentially takes union of those intervals.
|
13
|
+
- video_lim: Find a large enough xlim and ylim for video.
|
14
|
+
- make_mp4: Put frames together into a mp4.
|
15
|
+
others not documented here.
|
16
|
+
|
17
|
+
'''
|
18
|
+
|
19
|
+
|
20
|
+
from . import figures
|
21
|
+
from .tools import file_tools as file_t
|
22
|
+
from .tools import figure_tools as figure_t
|
23
|
+
|
24
|
+
import matplotlib as mpl
|
25
|
+
import matplotlib.pyplot as plt
|
26
|
+
import numpy as np
|
27
|
+
import os
|
28
|
+
from moviepy import ImageSequenceClip
|
29
|
+
|
30
|
+
|
31
|
+
# a list of supported figures
|
32
|
+
SUPPORTED_FIGURES = ['UV_heatmap', 'pi_heatmap', 'UV_bar', 'pi_bar', 'UV_hist', 'pi_hist', 'UV_pi']
|
33
|
+
|
34
|
+
|
35
|
+
# map function name to functios in figures.py
|
36
|
+
# functions not in this dictionary is not supported for videos.
|
37
|
+
FUNC_DICT = {'UV_heatmap': figures.UV_heatmap, 'UV_bar': figures.UV_bar, 'UV_hist': figures.UV_hist,
|
38
|
+
'pi_heatmap': figures.pi_heatmap, 'pi_bar': figures.pi_bar, 'pi_hist': figures.pi_hist, 'UV_pi': figures.UV_pi}
|
39
|
+
|
40
|
+
|
41
|
+
# Map some color maps to regular colors, used to change colors when an invalid color name is given
|
42
|
+
SNS_PLT_COLOR_DICT = {'Greens': 'green', 'Purples': 'purple', 'BuPu': 'violet', 'YlGn': 'yellowgreen'}
|
43
|
+
# Map regular colors to color maps
|
44
|
+
PLT_SNS_COLOR_DICT = {'green': 'Greens', 'purple': 'Purples', 'violet': 'BuPu', 'yellowgreen': 'YlGn'}
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
def convert_color(func_name, U_color, V_color):
|
50
|
+
'''
|
51
|
+
Converts some invalid colors.
|
52
|
+
If making heatmap videos but gave single colors, map to color maps.
|
53
|
+
If making barplot or histogram videos but gave single colors, map to Matplotlib
|
54
|
+
'''
|
55
|
+
|
56
|
+
if 'heatmap' in func_name:
|
57
|
+
# if making heatmaps but give regular colors
|
58
|
+
if U_color in PLT_SNS_COLOR_DICT.keys():
|
59
|
+
print('Making heatmaps, changed \'' + U_color + '\' to \'' + PLT_SNS_COLOR_DICT[U_color] + '\'')
|
60
|
+
U_color = PLT_SNS_COLOR_DICT[U_color]
|
61
|
+
if V_color in PLT_SNS_COLOR_DICT.keys():
|
62
|
+
print('Making heatmaps, changed \'' + V_color + '\' to \'' + PLT_SNS_COLOR_DICT[V_color] + '\'')
|
63
|
+
V_color = PLT_SNS_COLOR_DICT[V_color]
|
64
|
+
|
65
|
+
return U_color, V_color
|
66
|
+
|
67
|
+
elif 'heatmap' not in func_name:
|
68
|
+
# if making barplots or histogram
|
69
|
+
if U_color in SNS_PLT_COLOR_DICT.keys():
|
70
|
+
print('Not making heatmaps, changed \'' + U_color + '\' to \'' + SNS_PLT_COLOR_DICT[U_color] + '\'')
|
71
|
+
U_color = SNS_PLT_COLOR_DICT[U_color]
|
72
|
+
if V_color in SNS_PLT_COLOR_DICT.keys():
|
73
|
+
print('Not making heatmaps, changed \'' + V_color + '\' to \'' + SNS_PLT_COLOR_DICT[V_color] + '\'')
|
74
|
+
V_color = SNS_PLT_COLOR_DICT[V_color]
|
75
|
+
|
76
|
+
return U_color, V_color
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
def get_max_lim(lims):
|
81
|
+
'''
|
82
|
+
Get the max lim over many lims, i.e., the lowest lower bound and highest upper bound.
|
83
|
+
And then expand it a bit for better accommodation.
|
84
|
+
|
85
|
+
Input:
|
86
|
+
lim: list or np.array, has form [lim1, lim2, ...]
|
87
|
+
|
88
|
+
Returns:
|
89
|
+
A max lim which contains all lims.
|
90
|
+
'''
|
91
|
+
|
92
|
+
lims = np.array(lims)
|
93
|
+
|
94
|
+
lim_min = np.min(lims[:, 0]) # min of min
|
95
|
+
lim_max = np.max(lims[:, 1]) # max of max
|
96
|
+
r = lim_max - lim_min
|
97
|
+
|
98
|
+
if lim_min != 0:
|
99
|
+
# negative values are reached
|
100
|
+
# extend both upper bound and lower bound
|
101
|
+
return [lim_min - r * 0.05, lim_max + r * 0.05]
|
102
|
+
else:
|
103
|
+
# only extend upper bound
|
104
|
+
return [0, lim_max + r * 0.05]
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def frame_lim(mod, func, frames):
|
110
|
+
'''
|
111
|
+
Find a large enough xlim and ylim for frames, if not heatmaps.
|
112
|
+
|
113
|
+
Inputs:
|
114
|
+
mod: A simulation.model object, the simulation results.
|
115
|
+
frames: How many frame to make for the video.
|
116
|
+
|
117
|
+
Returns:
|
118
|
+
xlim and ylim for U and V, 4 in total.
|
119
|
+
'''
|
120
|
+
|
121
|
+
# take 10 samples and store their lims in list
|
122
|
+
U_xlist = []
|
123
|
+
U_ylist = []
|
124
|
+
V_xlist = []
|
125
|
+
V_ylist = []
|
126
|
+
|
127
|
+
for i in range(10):
|
128
|
+
fig_U, ax_U = plt.subplots()
|
129
|
+
fig_V, ax_V = plt.subplots()
|
130
|
+
ax_U, ax_V = func(mod, ax_U = ax_U, ax_V = ax_V, start = i / 10, end = (i / 10 + 1 / frames))
|
131
|
+
|
132
|
+
U_xlist.append(ax_U.get_xlim())
|
133
|
+
U_ylist.append(ax_U.get_ylim())
|
134
|
+
V_xlist.append(ax_V.get_xlim())
|
135
|
+
V_ylist.append(ax_V.get_ylim())
|
136
|
+
|
137
|
+
plt.close(fig_U)
|
138
|
+
plt.close(fig_V)
|
139
|
+
|
140
|
+
# get the largest 'range' based on the lists
|
141
|
+
U_xlim = get_max_lim(U_xlist)
|
142
|
+
U_ylim = get_max_lim(U_ylist)
|
143
|
+
V_xlim = get_max_lim(V_xlist)
|
144
|
+
V_ylim = get_max_lim(V_ylist)
|
145
|
+
|
146
|
+
return U_xlim, U_ylim, V_xlim, V_ylim
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def frame_heatmap_lim(mod, func, frames):
|
152
|
+
'''
|
153
|
+
Find a large enough color bar lim for frames, if heatmaps.
|
154
|
+
|
155
|
+
Inputs:
|
156
|
+
mod: A simulation.model object, the simulation results.
|
157
|
+
frames: How many frame to make for the video.
|
158
|
+
|
159
|
+
Returns:
|
160
|
+
clim for U and V
|
161
|
+
'''
|
162
|
+
|
163
|
+
U_list = []
|
164
|
+
V_list = []
|
165
|
+
|
166
|
+
for i in range(10):
|
167
|
+
fig_U, ax_U = plt.subplots()
|
168
|
+
fig_V, ax_V = plt.subplots()
|
169
|
+
ax_U, ax_V = func(mod, ax_U = ax_U, ax_V = ax_V, start = i / 10, end = (i / 10 + 1 / frames))
|
170
|
+
|
171
|
+
U_list.append(ax_U.collections[0].get_clim())
|
172
|
+
V_list.append(ax_V.collections[0].get_clim())
|
173
|
+
|
174
|
+
plt.close(fig_U)
|
175
|
+
plt.close(fig_V)
|
176
|
+
|
177
|
+
U_clim = get_max_lim(U_list)
|
178
|
+
V_clim = get_max_lim(V_list)
|
179
|
+
|
180
|
+
return U_clim, V_clim
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
def make_mp4(video_dir, frame_dir, fps):
|
186
|
+
'''
|
187
|
+
Read .png from the frames folder and make into a mp4
|
188
|
+
Inputs:
|
189
|
+
video_dir: where to save the video
|
190
|
+
frame_dirs: where to read frames from
|
191
|
+
fps: frames per second
|
192
|
+
'''
|
193
|
+
|
194
|
+
frame_paths_incomplete = os.listdir(frame_dir)
|
195
|
+
frame_paths_incomplete.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
|
196
|
+
frame_paths = []
|
197
|
+
for path in frame_paths_incomplete:
|
198
|
+
if (path[-4:] == '.png') and ('frame' in path):
|
199
|
+
frame_paths.append(frame_dir + '/' + path)
|
200
|
+
clip = ImageSequenceClip(frame_paths, fps = fps)
|
201
|
+
clip.write_videofile(video_dir, logger = None)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
def make_video(mod, func_name = 'UV_heatmap', frames = 100, dpi = 200, fps = 30, U_color = 'Greens', V_color = 'Purples', annot = False, fmt = '.3g', del_frames = False, dirs = 'videos'):
|
207
|
+
'''
|
208
|
+
Make a mp4 video based on simulation results.
|
209
|
+
|
210
|
+
Inputs:
|
211
|
+
- mod: a simulation.model object, the simulation results.
|
212
|
+
- func_name: what function to use to make the frames. Should be one of the functions in figures.py
|
213
|
+
- frames: how many frames to make. Use more frames for more smooth evolutions.
|
214
|
+
- dpi: dots per inch.
|
215
|
+
- fps: frames per second.
|
216
|
+
- U_color: color for U's videos. Color maps or regular colors, based on what function you use.
|
217
|
+
- V_color: color for V's videos.
|
218
|
+
- annot: used by heatmaps. Whether to show numbers.
|
219
|
+
- fmt: number format
|
220
|
+
- del_frames: whether to delete frames after making video.
|
221
|
+
- dirs: where to store the frames and videos.
|
222
|
+
'''
|
223
|
+
|
224
|
+
if func_name not in FUNC_DICT.keys():
|
225
|
+
raise ValueError(func_name + ' not supported for videos.')
|
226
|
+
func = FUNC_DICT[func_name]
|
227
|
+
|
228
|
+
# convert color if invalid colors are given
|
229
|
+
U_color, V_color = convert_color(func_name, U_color, V_color)
|
230
|
+
|
231
|
+
# print progress
|
232
|
+
one_progress = frames / 100
|
233
|
+
current_progress = one_progress
|
234
|
+
|
235
|
+
if 'heatmap' in func_name:
|
236
|
+
# make sure a fixed color bar for all frames
|
237
|
+
U_clim, V_clim = frame_heatmap_lim(mod, func, frames)
|
238
|
+
else:
|
239
|
+
# make sure y axis not changing if not making heatmaps
|
240
|
+
U_xlim, U_ylim, V_xlim, V_ylim = frame_lim(mod, func, frames)
|
241
|
+
|
242
|
+
|
243
|
+
U_frame_dirs = dirs + '/U-' + func_name
|
244
|
+
V_frame_dirs = dirs + '/V-' + func_name
|
245
|
+
|
246
|
+
if os.path.exists(U_frame_dirs):
|
247
|
+
file_t.del_dirs(U_frame_dirs)
|
248
|
+
os.makedirs(U_frame_dirs)
|
249
|
+
if os.path.exists(V_frame_dirs):
|
250
|
+
file_t.del_dirs(V_frame_dirs)
|
251
|
+
os.makedirs(V_frame_dirs)
|
252
|
+
|
253
|
+
#### for loop ####
|
254
|
+
|
255
|
+
for i in range(frames):
|
256
|
+
if i > current_progress:
|
257
|
+
print('making frames', round(i / frames * 100), '%', end = '\r')
|
258
|
+
current_progress += one_progress
|
259
|
+
|
260
|
+
fig_U, ax_U = plt.subplots(figsize = (6.4, 4.8))#, constrained_layout = True)
|
261
|
+
fig_V, ax_V = plt.subplots(figsize = (6.4, 4.8))#, constrained_layout = True)
|
262
|
+
|
263
|
+
if 'heatmap' in func_name:
|
264
|
+
ax_U, ax_V = func(mod, ax_U = ax_U, ax_V = ax_V, U_color = U_color, V_color = V_color, start = i / frames, end = (i + 1) / frames, annot = annot, fmt = fmt)
|
265
|
+
else:
|
266
|
+
ax_U, ax_V = func(mod, ax_U = ax_U, ax_V = ax_V, U_color = U_color, V_color = V_color, start = i / frames, end = (i + 1) / frames)
|
267
|
+
|
268
|
+
if 'heatmap' in func_name:
|
269
|
+
ax_U.collections[0].set_clim(U_clim)
|
270
|
+
ax_V.collections[0].set_clim(V_clim)
|
271
|
+
else:
|
272
|
+
# make sure y axis not changing if not heatmap and not UV_pi
|
273
|
+
ax_U.set_ylim(U_ylim)
|
274
|
+
ax_V.set_ylim(V_ylim)
|
275
|
+
if ('hist' in func_name) or (func_name == 'UV_pi'):
|
276
|
+
# need to set xlim as well for UV_pi and histograms
|
277
|
+
ax_V.set_xlim(U_xlim)
|
278
|
+
ax_V.set_xlim(V_xlim)
|
279
|
+
|
280
|
+
fig_U.savefig(U_frame_dirs + '/' + 'U_frame_' + str(i) + '.png', pad_inches = 0.25, dpi = dpi)
|
281
|
+
fig_V.savefig(V_frame_dirs + '/' + 'V_frame_' + str(i) + '.png', pad_inches = 0.25, dpi = dpi)
|
282
|
+
|
283
|
+
plt.close(fig_U)
|
284
|
+
plt.close(fig_V)
|
285
|
+
|
286
|
+
#### for loop ends ####
|
287
|
+
|
288
|
+
# frames done
|
289
|
+
print('making mp4... ', end = '\r')
|
290
|
+
|
291
|
+
# make videos based on frames
|
292
|
+
make_mp4(dirs + '/U-' + func_name + '.mp4', U_frame_dirs, fps)
|
293
|
+
make_mp4(dirs + '/V-' + func_name + '.mp4', V_frame_dirs, fps)
|
294
|
+
|
295
|
+
if del_frames:
|
296
|
+
file_t.del_dirs(U_frame_dirs)
|
297
|
+
file_t.del_dirs(V_frame_dirs)
|
298
|
+
print('video saved: ' + dirs + ', frames deleted')
|
299
|
+
else:
|
300
|
+
print('video saved: ' + dirs + ' ')
|
301
|
+
|
302
|
+
|
303
|
+
|
@@ -0,0 +1,113 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: piegy
|
3
|
+
Version: 2.1.0
|
4
|
+
Summary: Payoff-Driven Stochastic Spatial Model for Evolutionary Game Theory
|
5
|
+
Author-email: Chenning Xu <cxu7@caltech.edu>
|
6
|
+
License: BSD 3-Clause License
|
7
|
+
|
8
|
+
Copyright (c) 2025, Chenning Xu
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions are met:
|
12
|
+
|
13
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
14
|
+
list of conditions and the following disclaimer.
|
15
|
+
|
16
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
17
|
+
this list of conditions and the following disclaimer in the documentation
|
18
|
+
and/or other materials provided with the distribution.
|
19
|
+
|
20
|
+
3. Neither the name of the copyright holder nor the names of its
|
21
|
+
contributors may be used to endorse or promote products derived from
|
22
|
+
this software without specific prior written permission.
|
23
|
+
|
24
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
25
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
26
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
27
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
28
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
29
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
30
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
31
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
32
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
33
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
|
35
|
+
Project-URL: Source, https://github.com/Chenning04/piegy.git
|
36
|
+
Project-URL: Documentation, https://piegy.readthedocs.io/en/
|
37
|
+
Keywords: Game Theory,Evolutionary Game Theory,Spatial Model,Stochastic Model,Payoff Driven
|
38
|
+
Classifier: Development Status :: 4 - Beta
|
39
|
+
Classifier: Intended Audience :: Developers
|
40
|
+
Classifier: Intended Audience :: Science/Research
|
41
|
+
Classifier: Intended Audience :: Education
|
42
|
+
Classifier: License :: OSI Approved :: BSD License
|
43
|
+
Classifier: Programming Language :: Python :: 3
|
44
|
+
Classifier: Programming Language :: Python :: 3.10
|
45
|
+
Classifier: Programming Language :: Python :: 3.11
|
46
|
+
Classifier: Operating System :: OS Independent
|
47
|
+
Requires-Python: >=3.8
|
48
|
+
Description-Content-Type: text/markdown
|
49
|
+
License-File: LICENSE.txt
|
50
|
+
Requires-Dist: numpy
|
51
|
+
Requires-Dist: matplotlib
|
52
|
+
Requires-Dist: moviepy>=2.1.1
|
53
|
+
Requires-Dist: seaborn>=0.13.2
|
54
|
+
Dynamic: license-file
|
55
|
+
|
56
|
+
# piegy
|
57
|
+
|
58
|
+
The package full name is: Payoff-Driven Stochastic Spatial Model for Evolutionary Game Theory. "pi" refers to "payoff, and "egy" is taken from "Evolutionary Game Theory".
|
59
|
+
|
60
|
+
Provides a stochastic spatial model for simulating the interaction and evolution of two species in either 1D or 2D space, as well as analytic tools.
|
61
|
+
|
62
|
+
## Installation
|
63
|
+
|
64
|
+
To install *piegy*, run the following in terminal:
|
65
|
+
|
66
|
+
```bash
|
67
|
+
pip install piegy
|
68
|
+
```
|
69
|
+
|
70
|
+
## Documentation and Source
|
71
|
+
|
72
|
+
See source code at: [piegy GitHub-repo](https://github.com/Chenning04/piegy.git).
|
73
|
+
The *piegy* documentation at: [piegy Documentation](https://piegy.readthedocs.io/en/).
|
74
|
+
|
75
|
+
## How the Model Works
|
76
|
+
|
77
|
+
Our model can be summarized as "classical evolutionary game theory endowed with spatial structure and payoff-driven migration rules". Consider two species, predators and preys (denoted by *U* and *V*), in a rectangular region. We divide the region into N by M patches and simulate their interaction within a patch by classical game theory (i.e., payoff matrices and carrying capacity). Interactions across patches are simulated by payoff-driven migration rules. An individual migrates to a neighboring patch with probability weighted by payoff in the neighbors.
|
78
|
+
|
79
|
+
We use the Gillepie algorithm as the fundamental event-selection algorithm. At each time step, one event is selected and let happen; and the step size is continuous, dependent on the current state in the space. Data are recorded every some specified time interval.
|
80
|
+
|
81
|
+
## Analytic Tools
|
82
|
+
|
83
|
+
The *piegy* package also provides a wide range of analytic and supportive tools alongside the main model, such as plotting, numerical tools, data saving & reading, etc. We also provide the *piegy.videos* module for more direct visualizations such as how population distribution change over time.
|
84
|
+
|
85
|
+
## C Core
|
86
|
+
|
87
|
+
From version 2 on, the *piegy* simulations are now equipped with a C core, which makes it significantly faster than previous versions.
|
88
|
+
|
89
|
+
## Examples
|
90
|
+
|
91
|
+
To get started, simply get our demo model and run simulation:
|
92
|
+
|
93
|
+
```python
|
94
|
+
from piegy import simulation, figures
|
95
|
+
import matplotlib.pyplot as plt
|
96
|
+
|
97
|
+
mod = simulation.demo_model()
|
98
|
+
simulation.run(mod)
|
99
|
+
|
100
|
+
fig1, ax1 = plt.subplots()
|
101
|
+
figures.UV_dyna(mod, ax1)
|
102
|
+
fig2, ax2 = plt.subplots(1, 2, figsize = (12.8, 4.8))
|
103
|
+
figures.UV_heatmap(mod, ax2[0], ax2[1])
|
104
|
+
```
|
105
|
+
|
106
|
+
The figures reveal population dynamics and steady state population distribution.
|
107
|
+
|
108
|
+
|
109
|
+
## Acknowledgments
|
110
|
+
|
111
|
+
- Thanks Professor Daniel Cooney at University of Illinois Urbana-Champaign. This package is developed alongside a project with Prof. Cooney and received enormous help from him.
|
112
|
+
- Special thanks to the open-source community for making this package possible.
|
113
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
piegy/__init__.py,sha256=3voZtYSFLmgQCRQsz2bTrhVSI_lJ-UXc78IY92ajWJM,2138
|
2
|
+
piegy/__version__.py,sha256=XKaDCdQoaHEBFn0RdDb0Gcjwotejw9Xi7M87I6KNPW4,1576
|
3
|
+
piegy/analysis.py,sha256=tJhJTSFJsK_Zr34s78Cy7Im1NtONctkdNmg0R0teTeU,8941
|
4
|
+
piegy/data_tools.py,sha256=uYEoCYO7ZVtKBsNDgco7kERvBlOFFK7wpTuqcCwCBEU,3573
|
5
|
+
piegy/figures.py,sha256=QAbCoIopa0FRkReXBG0ExFvG-wNevI8hK8Ar-Ph14II,18427
|
6
|
+
piegy/simulation.py,sha256=KV_fXQXXG6QI-kkSDQqC3F8moFd0m_orBWA69magk9Q,20405
|
7
|
+
piegy/simulation_py.py,sha256=s9cSdcIXc-n_njK5Y9KKolejOVrVtcy3Ou1UT7NmVdY,30340
|
8
|
+
piegy/test_var.py,sha256=weIaC-3jL9A3tLa_kpqPVLR0NQHwzzhaMtP8P0sFlA4,24145
|
9
|
+
piegy/videos.py,sha256=meG3kNCnmAsEc2LtLGk2VixQZ9Nx4dRYwK7DB17_LFk,10521
|
10
|
+
piegy/C_core/piegyc.cp310-win32.pyd,sha256=vOo036kcu9_z1yVjTY5HzvQ0S7WghRErndKMgf7B7s8,74801
|
11
|
+
piegy/tools/__init__.py,sha256=Zcr2OExiwY_Zeh_vVwDjbuHtTaI_eHOe8xwzgsVdja0,315
|
12
|
+
piegy/tools/figure_tools.py,sha256=pz7Y2L1S1rVpbjwUwlsp1v1LRgi8IJq8ybLDn3woSNg,7213
|
13
|
+
piegy/tools/file_tools.py,sha256=ThXsc2gBX8SbJWW07afolz1oHH0dJQfMvn9D3b-y7TE,659
|
14
|
+
piegy-2.1.0.dist-info/licenses/LICENSE.txt,sha256=1ihCy5mwfvcRiquE82AHAMhd4iVUzDfgL7eG8_tPwQ0,1526
|
15
|
+
piegy-2.1.0.dist-info/METADATA,sha256=3q6TtVWzreHWW46evHQ7rCBeUwNsFW-DdGhY1eWVVxk,5548
|
16
|
+
piegy-2.1.0.dist-info/WHEEL,sha256=GWZF0cboiU4MhsG0baPl8rrtCaXFSLW25384gp3vddM,97
|
17
|
+
piegy-2.1.0.dist-info/top_level.txt,sha256=k4QLYL8PqdqDuy95-4NZD_FVLqJDsmq67tpKkBn4vMw,6
|
18
|
+
piegy-2.1.0.dist-info/RECORD,,
|