-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathC_barcharts.py
148 lines (134 loc) · 5.46 KB
/
C_barcharts.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
from lingpy import *
from collections import defaultdict
from matplotlib import pyplot as plt
import colorsys
from sys import argv
ref = 'scaid' if 'sca' in argv else 'lexstatid'
wl = Wordlist('D_subset-300-22.tsv-cognates.tsv')
langs = csv2list('D_languages.tsv')
# make dictionary to get the groups quickly from a language name
lang2group = {k[4]: k[2] for k in langs[1:]}
patterns = {l: [] for l in lang2group}
allpats = defaultdict(list)
etd = wl.get_etymdict(ref=ref)
for k, vals in etd.items():
idxs = [v[0] for v in vals if v and wl[v[0], 'doculect'] in lang2group]
lngs = [wl[idx, 'doculect'] for idx in idxs]
groups = defaultdict(list)
for idx, lng in zip(idxs, lngs):
groups[lang2group[lng]] += [lng]
gstruc = ' '.join(['{0}:{1}'.format(y, len(groups[y])) for y in
sorted(groups)])
for idx, lng in zip(idxs, lngs):
patterns[lng] += [(gstruc, idx)]
allpats[gstruc] += [k]
bars = [0, 0, 0, 0, 0, 0]
bars2 = [0, 0, 0, 0, 0, 0]
bars3 = [0, 0, 0, 0, 0, 0]
bars4 = [0, 0, 0, 0, 0, 0]
bars5 = [0, 0, 0, 0, 0, 0]
with open('O_patterns-{0}.tsv'.format(ref), 'w') as f:
f.write('PATTERN\tAtlantic\tBangime\tDogon\tMande\tSonghai\tExamples\tCOGID\n')
for k, v in allpats.items():
nums = ['0', '0', '0', '0', '0']
grps = ['Atlantic', 'Bangime', 'Dogon', 'Mande', 'Songhai']
cncs = [wl[[y[0] for y in etd[cogid] if y][0], 'concept'] for cogid in
v]
for key in k.split():
a, b = key.split(':')
kidx = grps.index(a)
nums[kidx] = b
f.write('{0}\t{1}\t{2}\t{3}\t{4}\n'.format(k, '\t'.join(nums),
len(v), ' '.join([str(x) for x in
v]), ', '.join(cncs)))
if 'Bangime' in k:
nums = [int(x) for x in nums]
if nums.count(0) == 3:
for i, (a, b) in enumerate(zip(grps, nums)):
if b > 0 and a != 'Bangime':
this_lng = a
bars[i] += len(v)
elif nums.count(0) == 4:
bars[1] += len(v)
else:
bars[-1] += len(v)
if 'Atlantic' in k:
nums = [int(x) for x in nums]
if nums.count(0) == 3:
for i, (a, b) in enumerate(zip(grps, nums)):
if b > 0 and a != 'Atlantic':
this_lng = a
bars2[i] += len(v)
elif nums.count(0) == 4:
bars2[0] += len(v)
else:
bars2[-1] += len(v)
if 'Dogon' in k:
nums = [int(x) for x in nums]
if nums.count(0) == 3:
for i, (a, b) in enumerate(zip(grps, nums)):
if b > 0 and a != 'Dogon':
this_lng = a
bars3[i] += len(v)
elif nums.count(0) == 4:
bars3[2] += len(v)
else:
bars3[-1] += len(v)
if 'Mande' in k:
nums = [int(x) for x in nums]
if nums.count(0) == 3:
for i, (a, b) in enumerate(zip(grps, nums)):
if b > 0 and a != 'Mande':
this_lng = a
bars4[i] += len(v)
elif nums.count(0) == 4:
bars4[3] += len(v)
else:
bars4[-1] += len(v)
if 'Songhai' in k:
nums = [int(x) for x in nums]
if nums.count(0) == 3:
for i, (a, b) in enumerate(zip(grps, nums)):
if b > 0 and a != 'Mande':
this_lng = a
bars5[i] += len(v)
elif nums.count(0) == 4:
bars5[4] += len(v)
else:
bars5[-1] += len(v)
plt.clf()
labels = grps + ['?']
explode = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'lightgray', 'orange']
plt.pie(bars, explode=explode, labels=labels, colors=colors, shadow=True,
startangle=140, autopct='%1.1f%%')
plt.axis('equal')
plt.savefig('O_bangime-{0}.pdf'.format(ref))
plt.clf()
#explode = [0.2, 0.0, 0.0, 0.2, 0.2, 0.2]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'lightgray', 'orange']
plt.pie(bars2, explode=explode, labels=labels, colors=colors, shadow=True,
startangle=140, autopct='%1.1f%%')
plt.axis('equal')
plt.savefig('O_atlantic-{0}.pdf'.format(ref))
plt.clf()
#explode = [0.2, 0.0, 0.0, 0.2, 0.2, 0.2]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'lightgray', 'orange']
plt.pie(bars3, explode=explode, labels=labels, colors=colors, shadow=True,
startangle=140, autopct='%1.1f%%')
plt.axis('equal')
plt.savefig('O_dogon-{0}.pdf'.format(ref))
plt.clf()
#explode = [0.2, 0.0, 0.0, 0.2, 0.2, 0.2]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'lightgray', 'orange']
plt.pie(bars4, explode=explode, labels=labels, colors=colors, shadow=True,
startangle=140, autopct='%1.1f%%')
plt.axis('equal')
plt.savefig('O_mande-{0}.pdf'.format(ref))
plt.clf()
#explode = [0.2, 0.0, 0.0, 0.2, 0.2, 0.2]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'lightgray', 'orange']
plt.pie(bars5, explode=explode, labels=labels, colors=colors, shadow=True,
startangle=140, autopct='%1.1f%%')
plt.axis('equal')
plt.savefig('O_songhai-{0}.pdf'.format(ref))