-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMB_Measurements.py
367 lines (322 loc) · 13.1 KB
/
TMB_Measurements.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import math
import random
import matplotlib.pyplot as mplpy
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
import numpy
import TMB_Classes
DATA_TYPES = ["individual", "range", "mean", "mean/sd", "mean/se", "classcount", "mean/sd/min/max", "mean/se/min/max"]
def sort_measurement_data(data: list) -> dict:
"""
sort the measurement data into species and into male, female, etc.
"""
species = set()
for d in data:
species.add(d.species)
species_data = {}
for s in species:
sdata = TMB_Classes.SpeciesMeasurements()
for dtype in DATA_TYPES:
tall = []
tother = []
tmale = []
tfemale = []
for d in data:
if d.species == s:
if d.type == dtype:
tall.append(d)
if d.sex == "male":
tmale.append(d)
elif d.sex == "female":
tfemale.append(d)
else:
tother.append(d)
if len(tall) > 0:
sdata.all[dtype] = tall
if len(tother) > 0:
sdata.other[dtype] = tother
if len(tmale) > 0:
sdata.male[dtype] = tmale
if len(tfemale) > 0:
sdata.female[dtype] = tfemale
species_data[s] = sdata
return species_data
def se_to_sd(se, n):
return se * math.sqrt(n)
def combine_measurement_data(data):
cdata = []
if "individual" in data:
n = 1
else:
n = 0
subset = [t for t in DATA_TYPES if t != "individual"]
for dtype in subset:
if dtype in data:
dat = data[dtype]
for d in dat:
n = max(n, d.n)
if n > 0:
maxr = 1000
while n > maxr:
maxr *= 10
pern = round(maxr / n)
if "individual" in data:
dat = data["individual"]
for d in dat:
for r in range(pern):
cdata.append(d.value)
if "range" in data:
dat = data["range"]
for d in dat:
for r in range(pern):
cdata.append(d.value.max_val)
cdata.append(d.value.min_val)
if d.n > 2:
sd = (d.value.max_val - d.value.min_val)/4
mp = d.value.midpoint()
for r in range(pern*(d.n - 2)):
v = random.gauss(mp, sd)
# do not allow simulated widths to exceed observed range
while (v < d.value.min_val) or (v > d.value.max_val):
v = random.gauss(mp, sd)
cdata.append(v)
if "mean/sd" in data:
dat = data["mean/sd"]
for d in dat:
for r in range(pern*d.n):
cdata.append(random.gauss(d.value.mean, d.value.sd))
if "mean/se" in data:
dat = data["mean/se"]
for d in dat:
sd = se_to_sd(d.value.se, d.n)
for r in range(pern*d.n):
cdata.append(random.gauss(d.value.mean, sd))
if "mean" in data:
dat = data["mean"]
for d in dat:
# this essentially treats the mean as having variance of zero, which may be giving it too much power
for r in range(pern*d.n):
cdata.append(d.value.mean)
if "mean/sd/min/max" in data:
dat = data["mean/sd/min/max"]
for d in dat:
for r in range(pern):
cdata.append(d.value.max_val)
cdata.append(d.value.min_val)
if d.n > 2:
for r in range(pern*(d.n - 2)):
v = random.gauss(d.value.mean, d.value.sd)
# don't allow simulated widths to exceed observed range
while (v < d.value.min_val) or (v > d.value.max_val):
v = random.gauss(d.value.mean, d.value.sd)
cdata.append(v)
if "mean/se/min/max" in data:
dat = data["mean/se/min/max"]
for d in dat:
for r in range(pern):
cdata.append(d.value.max_val)
cdata.append(d.value.min_val)
if d.n > 2:
sd = se_to_sd(d.value.se, d.n)
for r in range(pern*(d.n - 2)):
v = random.gauss(d.value.mean, sd)
# don't allow simulated widths to exceed observed range
while (v < d.value.min_val) or (v > d.value.max_val):
v = random.gauss(d.value.mean, sd)
cdata.append(v)
if "classcount" in data:
dat = data["classcount"]
for d in dat:
for r in range(int(round(pern*d.n, 0))):
cdata.append(d.value.midpoint())
return cdata
def plot_individuals(faxes, data, yv, color):
if "individual" in data:
idata = data["individual"]
x = []
for d in idata:
x.append(d.value)
y = [yv + 0.75*random.random() for _ in idata]
faxes.scatter(x, y, color=color, edgecolors="black", linewidths=0.25)
yv += 1
return yv
def plot_ranges(faxes, data, yv, color):
if "range" in data:
rdata = data["range"]
x = []
y = []
for i, d in enumerate(rdata):
x.append([d.value.min_val, d.value.max_val])
# y.append(yv-i*1.5/len(rdata))
y.append(yv)
yv += 1
parts = faxes.violinplot(x, y, points=10, vert=False, widths=0.5, showextrema=True, showmedians=False,
showmeans=False)
for pc in parts["bodies"]:
pc.set_alpha(0)
for p in ["cmins", "cmaxes", "cbars"]:
parts[p].set_color(color)
parts[p].set_linewidths(0.5)
return yv
def plot_means(faxes, data, yv, color):
if "mean" in data:
mdata = data["mean"]
x = []
for d in mdata:
x.append(d.value.mean)
y = [yv for _ in mdata]
faxes.scatter(x, y, color=color, edgecolors="black", linewidths=0.25, marker="d")
yv += 1
return yv
def plot_means_sd(faxes, data, yv, color):
if "mean/sd" in data:
mdata = data["mean/sd"]
x = []
e = []
for d in mdata:
x.append(d.value.mean)
e.append(d.value.sd*1.96)
y = [yv + i for i in range(len(mdata))]
faxes.errorbar(x, y, xerr=e, color=color, marker="d", markeredgecolor="black", markeredgewidth=0.25, ls="none")
yv += len(mdata)
return yv
def plot_means_se(faxes, data, yv, color):
if "mean/se" in data:
mdata = data["mean/se"]
x = []
e = []
for d in mdata:
sd = se_to_sd(d.value.se, d.n)
x.append(d.value.mean)
e.append(sd*1.96)
y = [yv + i for i in range(len(mdata))]
faxes.errorbar(x, y, xerr=e, color=color, marker="d", markeredgecolor="black", markeredgewidth=0.25, ls="none")
yv += len(mdata)
return yv
def plot_classcount(faxes, data, yv, color):
if "classcount" in data:
classes = set()
cdata = data["classcount"]
boxes = []
for d in cdata:
classes.add(d.class_id)
for c in classes:
current_class = []
maxn = 0
for d in cdata:
if d.class_id == c:
current_class.append(d)
maxn = max(maxn, d.n)
for d in current_class:
rect = Rectangle((d.value.min_val, yv), d.value.max_val-d.value.min_val, d.n/maxn)
boxes.append(rect)
yv += 1.25
pc = PatchCollection(boxes, facecolor=color, edgecolor="black", linewidths=0.25, alpha=0.5)
faxes.add_collection(pc)
return yv
def plot_means_sd_min_max(faxes, data, yv, color):
if "mean/sd/min/max" in data:
mdata = data["mean/sd/min/max"]
mx = []
e = []
x = []
for i, d in enumerate(mdata):
mx.append(d.value.mean)
e.append(d.value.sd*1.96)
x.append([d.value.min_val, d.value.max_val])
y = [yv + i for i in range(len(mdata))]
parts = faxes.violinplot(x, y, points=10, vert=False, widths=0.5, showextrema=True, showmedians=False,
showmeans=False)
for pc in parts["bodies"]:
pc.set_alpha(0)
for p in ["cmins", "cmaxes", "cbars"]:
parts[p].set_color(color)
parts[p].set_linewidths(0.5)
faxes.errorbar(mx, y, xerr=e, color=color, marker="d", markeredgecolor="black", markeredgewidth=0.25, ls="none")
yv += len(mdata)
return yv
def plot_means_se_min_max(faxes, data, yv, color):
if "mean/se/min/max" in data:
mdata = data["mean/se/min/max"]
mx = []
e = []
x = []
for i, d in enumerate(mdata):
sd = se_to_sd(d.value.se, d.n)
mx.append(d.value.mean)
e.append(sd*1.96)
x.append([d.value.min_val, d.value.max_val])
y = [yv + i for i in range(len(mdata))]
parts = faxes.violinplot(x, y, points=10, vert=False, widths=0.5, showextrema=True, showmedians=False,
showmeans=False)
for pc in parts["bodies"]:
pc.set_alpha(0)
for p in ["cmins", "cmaxes", "cbars"]:
parts[p].set_color(color)
parts[p].set_linewidths(0.5)
faxes.errorbar(mx, y, xerr=e, color=color, marker="d", markeredgecolor="black", markeredgewidth=0.25, ls="none")
yv += len(mdata)
return yv
def plot_combined_data(faxes, combined_data, yv, color):
if len(combined_data) > 0:
quartile1, median, quartile3 = numpy.percentile(combined_data, [25, 50, 75])
mean = numpy.mean(combined_data)
parts = faxes.violinplot(combined_data, [yv], widths=2, points=1000, vert=False)
for pc in parts["bodies"]:
pc.set_color(color)
for p in ["cmins", "cmaxes", "cbars"]:
parts[p].set_color(color)
# parts[p].set_linewidths(0.5)
faxes.hlines(yv, quartile1, quartile3, color=color, linestyle='-', lw=5, alpha=0.5)
faxes.scatter(median, yv, marker="o", color="white", edgecolor=color, zorder=3)
faxes.scatter(mean, yv, marker="d", color="white", edgecolor=color, zorder=3)
yv += 2.5
return yv
def plot_measurement_data(species_dat, combined_data, comb_male_data, comb_female_data, filename):
fig, faxes = mplpy.subplots(figsize=[6, 6])
faxes.spines["right"].set_visible(False)
faxes.spines["top"].set_visible(False)
faxes.spines["left"].set_visible(False)
faxes.get_yaxis().set_visible(False)
mplpy.xlabel("carapace breadth (mm)")
y = 1
# plot individuals
y = plot_individuals(faxes, species_dat.female, y, "red")
y = plot_individuals(faxes, species_dat.male, y, "blue")
y = plot_individuals(faxes, species_dat.other, y, "black")
# plot ranges
y = plot_ranges(faxes, species_dat.female, y, "red")
y = plot_ranges(faxes, species_dat.male, y, "blue")
y = plot_ranges(faxes, species_dat.other, y, "black")
# plot means
y = plot_means(faxes, species_dat.female, y, "red")
y = plot_means(faxes, species_dat.male, y, "blue")
y = plot_means(faxes, species_dat.other, y, "black")
# plot means w/sd and se
y = plot_means_sd(faxes, species_dat.female, y, "red")
y = plot_means_se(faxes, species_dat.female, y, "red")
y = plot_means_sd(faxes, species_dat.male, y, "blue")
y = plot_means_se(faxes, species_dat.male, y, "blue")
y = plot_means_sd(faxes, species_dat.other, y, "black")
y = plot_means_se(faxes, species_dat.other, y, "black")
y = plot_means_sd_min_max(faxes, species_dat.female, y, "red")
y = plot_means_sd_min_max(faxes, species_dat.male, y, "blue")
y = plot_means_sd_min_max(faxes, species_dat.other, y, "black")
y = plot_means_se_min_max(faxes, species_dat.female, y, "red")
y = plot_means_se_min_max(faxes, species_dat.male, y, "blue")
y = plot_means_se_min_max(faxes, species_dat.other, y, "black")
# plot classcounts
y = plot_classcount(faxes, species_dat.female, y, "red")
y = plot_classcount(faxes, species_dat.male, y, "blue")
y = plot_classcount(faxes, species_dat.other, y, "black")
y += 1
y = plot_combined_data(faxes, comb_female_data, y, "red")
y = plot_combined_data(faxes, comb_male_data, y, "blue")
plot_combined_data(faxes, combined_data, y, "black")
custom_lines = [Line2D([0], [0], color="black", lw=4),
Line2D([0], [0], color="blue", lw=4),
Line2D([0], [0], color="red", lw=4)]
faxes.legend(custom_lines, ["All", "Males", "Females"], ncol=3, loc="lower center", bbox_to_anchor=(0.5, 1.01))
mplpy.savefig(filename, format="png", dpi=600)
mplpy.close("all")