-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
233 lines (179 loc) · 8.27 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import argparse
import nbformat as nbf
from nbformat.v4 import new_notebook, new_code_cell, new_markdown_cell
from nbconvert.preprocessors import ExecutePreprocessor
from dramas import dramas
from pprint import pformat
def generate(data):
nb = new_notebook()
nb['cells'] = []
nb['cells'].append(new_code_cell("""\
from collections import defaultdict, Counter
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'"""))
nb['cells'].append(new_code_cell("""\
data = \\
%s""" % data))
nb['cells'].append(new_markdown_cell("## Матрица"))
nb['cells'].append(new_code_cell("""\
drama_heroes = [hero for act in data for scene in act for hero in scene]
drama_heroes = list(sorted(set(drama_heroes)))
matrices = [np.array([[1 if hero in scene else 0 for scene in act] for hero in drama_heroes]) for act in data]"""))
nb['cells'].append(new_code_cell("""\
def rome(dec):
return {1: 'I', 2: 'II', 3: 'III', 4: 'IV', 5: 'V', 6: 'VI'}[dec]
def col(act, scene):
return rome(act) + '.' + str(scene)
def cols():
return [col(n_act+1, n_scene+1) for n_act, act in enumerate(matrices) for n_scene in range(len(act[0]))]
matrix = np.hstack(matrices)
pd.DataFrame(matrix, index=drama_heroes, columns=cols())"""))
nb['cells'].append(new_markdown_cell("## Мобильность"))
nb['cells'].append(new_code_cell("""\
def mob(mat):
return (np.sum(np.abs(mat[:, 1:] - mat[:, :-1]), axis=1) - 1) / (len(mat[0])-1)
index = list(range(1, len(matrices) + 1)) + ['пьеса']
mobility = list(map(mob, matrices + [matrix]))
mobility = pd.DataFrame(mobility, index=index, columns=drama_heroes).round(2).transpose()
mobility[mobility < 0] = ''
mobility"""))
nb['cells'].append(new_markdown_cell("## Плотность"))
nb['cells'].append(new_code_cell("""\
def dens(matrix):
# print(np.count_nonzero(matrix), '/', matrix.size, sep='', end=', ')
return np.count_nonzero(matrix)/matrix.size"""))
nb['cells'].append(new_code_cell("""\
print('Плотность всей пьесы:', round(dens(matrix), 2))
act_d = list(map(dens, matrices))
act_d_ = np.array(act_d).reshape(-1, 1)
scene_d = [[dens(c) for c in m.T] for m in matrices]
scene_d_ = pd.DataFrame(scene_d).values # to fill with nans
columns = list(range(1, len(scene_d_[0]) + 1)) + ['действия']
index = list(range(1, len(data)+1))
density = pd.DataFrame(np.hstack((scene_d_, act_d_)), columns=columns, index=index)
density.round(2).fillna('')"""))
nb['cells'].append(new_code_cell("""\
from itertools import accumulate
plt.subplots(figsize=(16, 5));
flat = [tup for act in scene_d for tup in enumerate(act, start=1)]
ticks, values = zip(*flat)
plt.plot(values);
plt.xticks(range(len(ticks)), ticks);
borders = np.array(list(accumulate(map(len, [[]] + scene_d))))
plt.scatter(borders[:-1]-0.5, act_d, c='red');
for x in borders[:-1]:
plt.axvline(x-0.5, c='gray', ls='--');
plt.xlabel('явление');
plt.ylabel('плотность');
plt.grid();"""))
nb['cells'].append(new_code_cell("""\
plt.plot(act_d)
plt.grid();
plt.xlabel('действие');
plt.ylabel('плотность');
plt.xticks(range(len(act_d)+1), range(1, len(act_d)+1));
plt.xlim([0, len(act_d)-1]);"""))
nb['cells'].append(new_markdown_cell("## Расстояние"))
nb['cells'].append(new_code_cell("""\
def dist(mat):
n = len(drama_heroes)
dist = np.zeros((n, n))*np.nan
for i in range(n-1):
for j in range(i+1, n):
if np.sum(mat[i]) != 0 and np.sum(mat[j]) != 0:
dist[i][j] = dist[j][i] = np.sum(np.abs(mat[i] - mat[j]))
return dist/len(mat[0])"""))
nb['cells'].append(new_code_cell("""\
act_d = [dist(m) for m in matrices]
drama_d = dist(matrix)"""))
nb['cells'].append(new_code_cell("""\
distances = [pd.DataFrame(d, index=drama_heroes, columns=drama_heroes).stack() for d in act_d + [drama_d]]
distances = pd.concat(distances, axis=1)
distances.columns = list(range(1, len(act_d) + 1)) + ['пьеса']
distances.round(2).fillna('')"""))
nb['cells'].append(new_code_cell("""\
plt.subplots(figsize=(14, 10))
sns.heatmap(drama_d, vmin=0, vmax=1, annot=True, xticklabels=drama_heroes, yticklabels=drama_heroes, fmt='.2f')
plt.title('по всей пьесе');"""))
nb['cells'].append(new_markdown_cell("## Абстрактные типы отношений (= соп., <> альт., > или < дом.)"))
nb['cells'].append(new_code_cell("""\
for i, j in zip(*np.where(drama_d == 0)):
if i != j and i < j:
p = np.sum(matrix[i])
q = np.sum(matrix[j])
s = '=' if p == q else ('>' if p > q else '<')
print(drama_heroes[i], s, drama_heroes[j])"""))
nb['cells'].append(new_markdown_cell("## Ранг"))
nb['cells'].append(new_code_cell("""\
rank = [np.sum(np.sum(matrix, axis=0)[np.where(matrix[i] != 0)[0]] - 1) for i in range(len(drama_heroes))]
df = pd.DataFrame(dict(zip(drama_heroes, rank)), index=['ранг']).sort_values(by='ранг', axis=1, ascending=False)"""))
nb['cells'].append(new_code_cell("""\
plt.scatter(range(len(drama_heroes)), df.values);
plt.xticks(range(len(drama_heroes)), df.columns, rotation=90);
plt.grid();"""))
nb['cells'].append(new_code_cell("""\
df"""))
nb['cells'].append(new_markdown_cell("## Абсолютные относительные частоты"))
nb['cells'].append(new_code_cell("""\
def freq(mat):
return np.sum(mat, axis=1)/len(mat[0])
index = list(range(1, len(matrices) + 1)) + ['пьеса']
freq_abs = list(map(freq, matrices + [matrix]))
freq_abs = pd.DataFrame(freq_abs, index=index, columns=drama_heroes).round(2).transpose()
freq_abs[freq_abs == 0] = ''
freq_abs.sort_values(by='пьеса', ascending=False)"""))
nb['cells'].append(new_markdown_cell("## Условные относительные частоты"))
nb['cells'].append(new_code_cell("""\
def rel_freq(mat):
busyness = np.sum(mat, axis=1)
freq_rel = np.zeros((len(drama_heroes), len(drama_heroes)))
for i in range(len(mat)):
for j in range(len(mat)):
if busyness[j] == 0 or i == j:
freq_rel[i, j] = np.nan
else:
freq_rel[i, j] = np.count_nonzero(mat[i] + mat[j] - 2 == 0)/busyness[j]
return freq_rel
for i, m in enumerate(matrices):
print('Действие', i+1)
pd.DataFrame(rel_freq(m), index=drama_heroes, columns=drama_heroes).round(2).fillna('')
print('По всей пьесе')
pd.DataFrame(rel_freq(matrix), index=drama_heroes, columns=drama_heroes).round(2).fillna('')"""))
nb['cells'].append(new_code_cell("""\
for i, m in enumerate(matrices):
plt.subplots(figsize=(14, 10))
sns.heatmap(rel_freq(m), vmin=0, vmax=1, annot=True, xticklabels=drama_heroes, yticklabels=drama_heroes, fmt='.2f')
plt.title(r"$P_{1,2}$ в действии " + str(i+1));
plt.subplots(figsize=(14, 10))
sns.heatmap(rel_freq(matrix), vmin=0, vmax=1, annot=True, xticklabels=drama_heroes, yticklabels=drama_heroes, fmt='.2f')
plt.title(r"$P_{1,2}$ по всей пьесе");"""))
nb['cells'].append(new_code_cell("""\
freq_abs = freq(matrix).reshape((-1, 1))
freq_rel = rel_freq(matrix)
freq_ = freq_abs - freq_rel
freq_[np.diag_indices(len(freq_[0]))] = freq_abs.ravel()
plt.subplots(figsize=(14, 10))
sns.heatmap(freq_, annot=True, xticklabels=drama_heroes, yticklabels=drama_heroes, fmt='.2f', vmin=-1, vmax=1)
plt.title(r"$P_1 – P_{1,2}$, на диагонали абсолютные частоты");"""))
return nb
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--name", required=True, help="title of the drama")
ap.add_argument("-e", "--execute", help="execute created notebook", action="store_true")
args = vars(ap.parse_args())
name = args['name']
dramas = dict([(drama['name'], drama['data']) for drama in dramas])
if name not in dramas:
raise ValueError('title not found in database')
notebook = generate(pformat(dramas[name]))
nbf.write(notebook, './notebooks/%s.ipynb' % name)
if args['execute']:
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.preprocess(notebook, {'metadata': {'path': 'notebooks/'}})
nbf.write(notebook, './notebooks/%s.ipynb' % name)