-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPC_traj.py
More file actions
145 lines (99 loc) · 4.71 KB
/
PC_traj.py
File metadata and controls
145 lines (99 loc) · 4.71 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import os
import re
def PC_trajGraded(PCmean, PCsem, PCsToPlot, rep, saveDir, file_type, scaling, start_end, cum_var, legend):
""" New figure style to plot the PC trajectories with a gradated colorscheme according to scaling
Input:
PCmean - dataframe of the PCmeans calculated after doing PCA and transforming into PC space
PCsem - sem of PCs
PCsToPlot = principal components to plot
rep - if replicated specify
saveDir - directory into which the figure should be saved
file_type - filetype eg .png, .svg, .eps
scaling - how to scale and grade the data
start_end - whether to write on start and end of trajectory
cum_var - array of cumulative variance explained by each PC
legend - 'on' or 'off'
animation - whether to animate the plot (Boolean)
Output:
Figure
"""
PC1 = PCsToPlot[0]
PC2 = PCsToPlot[1]
xscale = 1/(PCmean.max()[PC1] - PCmean.min()[PC1])
yscale = 1/(PCmean.max()[PC2] - PCmean.min()[PC2])
allDrugs= np.unique(PCmean['drug'])
plt.figure()
for drug in allDrugs:
cscale = np.arange(1, np.unique(PCmean[PCmean['drug']==drug][scaling]).shape[0]+1,1)
plt.errorbar(x= PCmean[PCmean['drug']==drug][PC1]*xscale,\
y = PCmean[PCmean['drug']==drug][PC2]*yscale,\
xerr = PCsem[PCsem['drug']==drug][PC1]*xscale,\
yerr = PCsem[PCsem['drug']==drug][PC2]*yscale, \
color = [0.8, 0.8, 0.8], zorder = -0.5, elinewidth = 2, label = None)
plt.pause(0.1)
plt.scatter(x = PCmean[PCmean['drug']==drug][PC1]*xscale,\
y=PCmean[PCmean['drug']==drug][PC2]*yscale,\
cmap = plt.get_cmap(drug),c=cscale , vmin = -1,\
vmax = cscale[-1]+3, label = drug, alpha =0.9)
plt.axis('scaled')
plt.xlim (-1,1)
plt.ylim (-1,1)
plt.title(scaling)
try:
plt.xlabel(PC1 + '_' + str(cum_var[0]*100) + '%')
plt.ylabel(PC2 + '_'+ str((cum_var[1] - cum_var[0])*100) + '%')
except TypeError:
print ('no cumulate variance input')
if legend == 'on':
plt.legend(loc='upper left', bbox_to_anchor=(1.1,1.05) ,ncol = 1, frameon= True)
plt.tight_layout()
plt.savefig(os.path.join(saveDir, '{}_{}_PC_errorbar{}'.format(rep, PCsToPlot, file_type)), dpi = 200)
plt.show()
# if animation:
# Writer = animation.writers['ffmpeg']
# writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)
def PC_trajGradedNoScale(PCmean, PCsem, rep, saveDir, file_type, scaling, start_end, cum_var, legend):
""" New figure style to plot the PC trajectories with a gradated colorscheme according to scaling
Input:
PCmean - dataframe of the PCmeans calculated after doing PCA and transforming into PC space
PCsem - sem of PCs
rep - if replicated specify
saveDir - directory into which the figure should be saved
file_type - filetype eg .png, .svg, .eps
scaling - how to scale and grade the data
start_end - whether to write on start and end of trajectory
cum_var - array of cumulative variance explained by each PC
legend - 'on' or 'off'
Output:
Figure
"""
allDrugs= np.unique(PCmean['drug'])
plt.figure()
for drug in allDrugs:
cscale = np.arange(1, np.unique(PCmean[PCmean['drug']==drug][scaling]).shape[0]+1,1)
plt.errorbar(x= PCmean[PCmean['drug']==drug]['PC_1'],\
y = PCmean[PCmean['drug']==drug]['PC_2'],\
xerr = PCsem[PCsem['drug']==drug]['PC_1'],\
yerr = PCsem[PCsem['drug']==drug]['PC_2'], \
color = [0.8, 0.8, 0.8], zorder = -0.5, elinewidth = 2, label = None)
plt.pause(0.1)
plt.scatter(x = PCmean[PCmean['drug']==drug]['PC_1'],\
y=PCmean[PCmean['drug']==drug]['PC_2'],\
cmap = plt.get_cmap(drug),c=cscale , vmin = -1,\
vmax = cscale[-1]+3, label = drug, alpha =0.9)
plt.axis('scaled')
plt.title(scaling)
plt.xlabel('PC_1 ' + str(cum_var[0]*100) + '%')
plt.ylabel('PC_2 ' + str((cum_var[1] - cum_var[0])*100) + '%')
if legend == 'on':
plt.legend(loc='upper left', bbox_to_anchor=(1.1,1.05) ,ncol = 1, frameon= True)
plt.tight_layout()
plt.savefig(os.path.join(saveDir, str(rep) + 'PC12_errorbarNS' + file_type), dpi = 200)
plt.show()