-
Notifications
You must be signed in to change notification settings - Fork 0
/
SENSITIVITY-TEST.py
executable file
·334 lines (309 loc) · 15.2 KB
/
SENSITIVITY-TEST.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 16 10:10:05 2020
@author: jocelynreahl
"""
# Import packages:
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
from sklearn import preprocessing
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Rectangle
import matplotlib.transforms as transforms
# Import ALLDATA.csv
lower = pd.read_csv('Data_CSV/ALLDATA_NARTISS-LOWER.csv')
middle = pd.read_csv('Data_CSV/ALLDATA_NARTISS-MIDDLE.csv')
upper = pd.read_csv('Data_CSV/ALLDATA_NARTISS-UPPER.csv')
bounds = [lower, middle, upper]
# Define microtexture sets to use for PCA:
# tex_allpossible = ['af', 'as', 'bb', 'cf', 'ff', 'ls', 'saf', 'slf', 'up', # Polygenetic
# 'er', 'vc', # Percussion
# 'crg', 'cg', 'dt', 'sg', # High-stress
# 'de', 'pf', # Chemicals
# 'low', 'med', 'high'] # Relief
tex_allauthors = ['as', 'cf', 'cg', 'er', 'ls', 'pf', 'saf', 'slf', 'vc',
'low', 'med', 'high']
tex_mechanical = ['as', 'cf', 'cg', 'er', 'ls', 'saf', 'slf', 'vc', 'low',
'med', 'high']
def confidence_ellipse(x, y, ax, n_std=3.0, facecolor='none', **kwargs):
"""
Create a plot of the covariance confidence ellipse of *x* and *y*.
https://matplotlib.org/devdocs/gallery/statistics/confidence_ellipse.html
Parameters
----------
x, y : array-like, shape (n, )
Input data.
ax : matplotlib.axes.Axes
The axes object to draw the ellipse into.
n_std : float
The number of standard deviations to determine the ellipse's radiuses.
**kwargs
Forwarded to `~matplotlib.patches.Ellipse`
Returns
---------
matplotlib.patches.Ellipse
"""
if x.size != y.size:
raise ValueError("x and y must be the same size")
cov = np.cov(x, y)
pearson = cov[0, 1]/np.sqrt(cov[0, 0] * cov[1, 1])
# Using a special case to obtain the eigenvalues of this
# two-dimensionl dataset.
ell_radius_x = np.sqrt(1 + pearson)
ell_radius_y = np.sqrt(1 - pearson)
ellipse = Ellipse((0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2,
facecolor=facecolor, **kwargs)
# Calculating the stdandard deviation of x from
# the squareroot of the variance and multiplying
# with the given number of standard deviations.
scale_x = np.sqrt(cov[0, 0]) * n_std
mean_x = np.mean(x)
# calculating the stdandard deviation of y ...
scale_y = np.sqrt(cov[1, 1]) * n_std
mean_y = np.mean(y)
transf = transforms.Affine2D() \
.rotate_deg(45) \
.scale(scale_x, scale_y) \
.translate(mean_x, mean_y)
ellipse.set_transform(transf + ax.transData)
return ax.add_patch(ellipse)
def run_PCA_fit_transform(dataset, tex):
'''
Run PCA fit-transform on dataset (fits the PCA model to the dataset and
apply dimensionality reduction to dataset).
----------
dataset = pandas dataframe
tex = list of microtexture abbreviations to use for PCA ordination
Returns
----------
pca_df = pandas dataframe with coordinates for each sample on each PC axis
pca = pca model object
'''
data = dataset.loc[:, tex]
scaled_data = preprocessing.scale(data)
pca = PCA()
pca_fit_transform = pca.fit_transform(scaled_data)
per_var = np.round(pca.explained_variance_ratio_*100, decimals=2)
components = ['PC' + str(x) for x in range(1, len(per_var)+1)]
pca_df = pd.DataFrame(pca_fit_transform, columns=components)
pca_df['transport'] = list(dataset['transport'])
pca_df['author'] = list(dataset['author'])
pca_df['marker'] = list(dataset['marker'])
pca_df['color'] = list(map(lambda s: s.replace('\ufeff', ''),
dataset['transportcolor']))
return pca_df, pca
def run_PCA_transform(dataset, tex, pca):
'''
Run PCA transform on dataset (applies existing dimensionality reduction
from initial pca.fit_transform() to new dataset).
----------
dataset = pandas dataframe
tex = list of microtexture abbreviations to use for PCA ordination
pca = pca model object from run_PCA_fit_transform()
Returns
----------
pca_df = pandas dataframe with coordinates for each sample on each PC axis
'''
if 'pf' in set(tex):
dataset = dataset[dataset['author'] != 'Sweet_2010']
data = dataset.loc[:, tex]
else:
data = dataset.loc[:, tex]
scaled_data = preprocessing.scale(data)
pca_transform = pca.transform(scaled_data)
per_var = np.round(np.round(pca.explained_variance_ratio_*100,
decimals=1))
components = ['PC' + str(x) for x in range(1, len(per_var)+1)]
pca_df = pd.DataFrame(pca_transform, columns=components)
pca_df['transport'] = list(dataset['transport'])
pca_df['author'] = list(dataset['author'])
pca_df['marker'] = list(dataset['marker'])
pca_df['color'] = list(map(lambda s: s.replace('\ufeff', ''),
dataset['transportcolor']))
return pca_df
def Sensitivityplot(datalist, tex, label):
'''
Plot lower (column 1), middle (column 2), and upper (column 3) bounds of
Nartišs and Kalińska-Nartiša (2017) ancient data when transformed into
modern PCA space.
----------
datalist = list of pandas Dataframes; use list of lower, middle, and upper
Nartišs and Kalińska-Nartiša (2017) data
tex = list of microtexture abbreviations to use for PCA ordination
label = str; file name with no extension; directs to Figures folder
'''
# Define "reference" and "sample" datasets and perform pca.fit_transform()
# and pca.transform() on the reference (modern) and sample (ancient)
# datasets, respectively.
modern_list = [] # empty list to store modern fit-transformed PCA data
pca_list = [] # empty list to store PCA model objects
ancient_list = [] # empty list to store ancient transformed PCA data
for d in datalist:
reference = d[d['relage'] == 'Active']
sample = d[d['relage'] != 'Active']
pca_df_m, pca = run_PCA_fit_transform(reference, tex)
pca_df_a = run_PCA_transform(sample, tex, pca)
modern_list.append(pca_df_m)
pca_list.append(pca)
ancient_list.append(pca_df_a)
# Plot PCA ordination w/reference first, then sample
fig, ax = plt.subplots(3, 3, figsize=(20, 20)) # Set up axes
ls = 24
for i in range(3):
for j in range(3):
ax[i, j].tick_params(axis='both', direction='in', which='major',
top=True, labeltop=False, right=True,
labelright=False, left=True, bottom=True,
labelsize=ls)
ax[i, j].set_xlim(-5, 6)
ax[i, j].set_ylim(-5, 6)
data_m = modern_list[j]
data_a = ancient_list[j]
if i == 0:
ax[i, j].set_xlabel('PC1', size=ls)
ax[i, j].text(-4.5, 4.5, 'A'+str(j+1), size=40)
for k in range(len(data_m)):
fk = data_m['color'].loc[k]
mk = data_m['marker'].loc[k]
xk = data_m['PC1'].loc[k]
yk = data_m['PC2'].loc[k]
ax[i, j].scatter(xk, yk, marker=mk, facecolors=fk,
edgecolors=fk, s=200, alpha=0.5,
linewidths=1)
colors = ['#D55E00', '#0072B2', '#F0E442']
for c in colors:
trans_group = data_m[data_m['color'] == c]
confidence_ellipse(trans_group['PC1'],
trans_group['PC2'], ax[i, j],
n_std=2, facecolor='none',
edgecolor=c, alpha=1, lw=2)
for k in range(len(data_a)):
if data_a['author'].loc[k] == 'Nartiss_2017':
fk = data_a['color'].loc[k]
mk = data_a['marker'].loc[k]
xk = data_a['PC1'].loc[k]
yk = data_a['PC2'].loc[k]
# if data_a['author'].loc[k] == 'Nartiss_2017':
# sk = 1000
# elif data_a['author'].loc[k] != 'Nartiss_2017':
# sk = 200
ax[i, j].scatter(xk, yk, marker=mk, facecolors=fk,
edgecolors='k', s=500, alpha=1,
linewidths=1)
if j == 0:
ax[i, j].set_ylabel('PC2', size=ls)
ax[i, j].add_patch(Rectangle((-5, 6+0.5), 11, 1,
clip_on=False, fill=True,
facecolor='#648FFF',
edgecolor='w'))
ax[i, j].text(0.5, 7, 'Lower Bound', size=ls, c='w',
horizontalalignment='center',
verticalalignment='center', weight='bold')
ax[i, j].add_patch(Rectangle((-5-3.1, -5), 1, 11,
clip_on=False, fill=True,
facecolor='#648FFF',
edgecolor='w'))
ax[i, j].text(-7.5, 0.5, 'PC1 v. PC2', size=ls, c='w',
horizontalalignment='center',
verticalalignment='center', weight='bold',
rotation=90)
if j == 1:
ax[i, j].add_patch(Rectangle((-5, 6+0.5), 11, 1,
clip_on=False, fill=True,
facecolor='#648FFF',
edgecolor='w'))
ax[i, j].text(0.5, 7, 'Middle Bound', size=ls, c='w',
horizontalalignment='center',
verticalalignment='center', weight='bold')
if j == 2:
ax[i, j].add_patch(Rectangle((-5, 6+0.5), 11, 1,
clip_on=False, fill=True,
facecolor='#648FFF',
edgecolor='w'))
ax[i, j].text(0.5, 7, 'Upper Bound', size=ls, c='w',
horizontalalignment='center',
verticalalignment='center', weight='bold')
if i == 1:
ax[i, j].set_xlabel('PC1', size=ls)
ax[i, j].text(-4.5, 4.5, 'B'+str(j+1), size=40)
for k in range(len(data_m)):
fk = data_m['color'].loc[k]
mk = data_m['marker'].loc[k]
xk = data_m['PC1'].loc[k]
yk = data_m['PC3'].loc[k]
ax[i, j].scatter(xk, yk, marker=mk, facecolors=fk,
edgecolors=fk, s=200, alpha=0.5,
linewidths=1)
colors = ['#D55E00', '#0072B2', '#F0E442']
for c in colors:
trans_group = data_m[data_m['color'] == c]
confidence_ellipse(trans_group['PC1'], trans_group['PC3'],
ax[i, j], n_std=2, facecolor='none',
edgecolor=c, alpha=1, lw=2)
for k in range(len(data_a)):
if data_a['author'].loc[k] == 'Nartiss_2017':
fk = data_a['color'].loc[k]
mk = data_a['marker'].loc[k]
xk = data_a['PC1'].loc[k]
yk = data_a['PC3'].loc[k]
# if data_a['author'].loc[k] == 'Nartiss_2017':
# sk = 1000
# elif data_a['author'].loc[k] != 'Nartiss_2017':
# sk = 200
ax[i, j].scatter(xk, yk, marker=mk, facecolors=fk,
edgecolors='k', s=500, alpha=1,
linewidths=1)
if j == 0:
ax[i, j].set_ylabel('PC3', size=ls)
ax[i, j].add_patch(Rectangle((-5-3.1, -5), 1, 11,
clip_on=False, fill=True,
facecolor='#648FFF',
edgecolor='w'))
ax[i, j].text(-7.5, 0.5, 'PC1 v. PC3', size=ls, c='w',
horizontalalignment='center',
verticalalignment='center', weight='bold',
rotation=90)
if i == 2:
ax[i, j].set_xlabel('PC2', size=ls)
ax[i, j].text(-4.5, 4.5, 'C'+str(j+1), size=40)
for k in range(len(data_m)):
fk = data_m['color'].loc[k]
mk = data_m['marker'].loc[k]
xk = data_m['PC2'].loc[k]
yk = data_m['PC3'].loc[k]
ax[i, j].scatter(xk, yk, marker=mk, facecolors=fk,
edgecolors=fk, s=200, alpha=0.5,
linewidths=1)
colors = ['#D55E00', '#0072B2', '#F0E442']
for c in colors:
trans_group = data_m[data_m['color'] == c]
confidence_ellipse(trans_group['PC2'], trans_group['PC3'],
ax[i, j], n_std=2, facecolor='none',
edgecolor=c, alpha=1, lw=2)
for k in range(len(data_a)):
if data_a['author'].loc[k] == 'Nartiss_2017':
fk = data_a['color'].loc[k]
mk = data_a['marker'].loc[k]
xk = data_a['PC2'].loc[k]
yk = data_a['PC3'].loc[k]
# if data_a['author'].loc[k] == 'Nartiss_2017':
# sk = 1000
# elif data_a['author'].loc[k] != 'Nartiss_2017':
# sk = 200
ax[i, j].scatter(xk, yk, marker=mk, facecolors=fk,
edgecolors='k', s=500, alpha=1,
linewidths=1)
if j == 0:
ax[i, j].set_ylabel('PC3', size=ls)
ax[i, j].add_patch(Rectangle((-5-3.1, -5), 1, 11,
clip_on=False, fill=True,
facecolor='#648FFF',
edgecolor='w'))
ax[i, j].text(-7.5, 0.5, 'PC2 v. PC3', size=ls, c='w',
horizontalalignment='center',
verticalalignment='center', weight='bold',
rotation=90)
plt.savefig('Figures/SENSITIVITY-TEST-' + label.upper() + '.jpg', dpi=300)
plt.show()