forked from AFM-SPM/TopoStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTFOplot.py
138 lines (111 loc) · 4.51 KB
/
TFOplot.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
import os
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
from cycler import cycler
# Set seaborn to override matplotlib for plot output
sns.set()
sns.set_style("white")
# The four preset contexts, in order of relative size, are paper, notebook, talk, and poster.
# The notebook style is the default
sns.set_context("poster", font_scale=1.1)
def importfromjson(path, name):
filename = os.path.join(path, name + '.json')
importeddata = pd.read_json(filename)
return importeddata
def plotkde(df, directory, name, plotextension, grouparg, plotarg):
print 'Plotting kde of %s' % plotarg
# Create a saving name format/directory
savedir = os.path.join(directory, 'Plots')
if not os.path.exists(savedir):
os.makedirs(savedir)
# Plot and save figures
savename = os.path.join(savedir, name + plotarg + plotextension)
fig, ax = plt.subplots(figsize=(10, 7))
df.groupby(grouparg)[plotarg].plot.kde(ax=ax, legend=True, alpha=0.7)
plt.savefig(savename)
def plot_mean_SD(df, directory, name, plotextension, plotarg, lengths):
print 'Plotting mean and SD for %s' % plotarg
# Create a saving name format/directory
savedir = os.path.join(directory, 'Plots')
if not os.path.exists(savedir):
os.makedirs(savedir)
# Plot and save figures
savename = os.path.join(savedir, name + plotarg + '_mean_std' + plotextension)
# Determine mean and std for each measurement and plot on bar chart
TFO = df
TFOmean = dict()
TFOstd = dict()
fig = plt.figure(figsize=(10, 7))
plt.ylabel(' ')
color = iter(['blue', 'limegreen', 'mediumvioletred', 'darkturquoise'])
for i in sorted(lengths, reverse=False):
TFOmean[i] = i
TFOstd[i] = i
x = TFO.query('Obj == @i')[plotarg]
a = x.mean()
b = x.std()
TFOmean[i] = a
print a
TFOstd[i] = b
print b
c = next(color)
plt.bar(i, TFOmean[i], yerr=TFOstd[i], alpha=0.7, color=c)
plt.savefig(savename)
def plothist(df, arg1, grouparg, directory, extension):
print 'Plotting graph of %s' % (arg1)
# Create a saving name format/directory
savedir = os.path.join(directory, 'Plots')
savename = os.path.join(savedir, os.path.splitext(os.path.basename(directory))[0])
if not os.path.exists(savedir):
os.makedirs(savedir)
# Plot arg1 using MatPlotLib separated by the grouparg
# Plot with figure with stacking sorted by grouparg
# Plot histogram
bins = np.arange(0, 130, 3)
fig = plt.figure(figsize=(10, 7))
new_prop_cycle = cycler('color', ['blue', 'limegreen', 'mediumvioletred', 'darkturquoise'])
plt.rc('axes', prop_cycle=new_prop_cycle)
df.groupby(grouparg)[arg1].plot.hist(legend=True, alpha=0.7, bins=bins)
plt.ylabel(' ')
# Save plot
plt.savefig(savename + '_' + arg1 + '_a' + extension)
def plotviolin(df, directory, name, plotextension, grouparg, plotarg):
print 'Plotting violin of %s' % plotarg
# Create a saving name format/directory
savedir = os.path.join(directory, 'Plots')
if not os.path.exists(savedir):
os.makedirs(savedir)
# Plot and save figures
savename = os.path.join(savedir, name + plotarg + '_violin' + plotextension)
fig, ax = plt.subplots(figsize=(10, 7))
# Plot violinplot
ax = sns.violinplot(x=grouparg, y=plotarg, data=df)
ax.invert_xaxis()
# plt.xlim(0, 1)
plt.xlabel(' ')
plt.ylabel(' ')
plt.savefig(savename)
# Set the file path, i.e. the directory where the files are here'
path = '/Users/alicepyne/Dropbox/UCL/DNA MiniCircles/Minicircle Data Edited/TFO/TFOlengthanalysis'
# Set the name of the json file to import here
name = 'TFOlengthanalysis'
plotextension = '.pdf'
bins = 8
allbins = 20
# # import data from the csv file specified as a dataframe
# TFO = pd.read_csv(os.path.join(path, '339LINTFOlengthanalysis.csv'))
# import data from a JSON file
TFO = importfromjson(path, name)
# rename columns to sensible names
TFO['Obj'] = TFO['Obj'].apply(str)
TFO['Obj'] = TFO['Obj'].replace({'1': 'full length', '2': 'short side', '3': 'long side', '4': 'TFO'})
# Get details of different length types
lengths = TFO['Obj'].unique()
lengths = sorted(lengths)
plot_mean_SD(TFO, path, name, plotextension, 'length (nm)', lengths)
plothist(TFO, 'length (nm)', 'Obj', path, plotextension)
plotviolin(TFO, path, name, plotextension, 'Obj', 'length (nm)')
plothist(TFO, 'length (nm)', 'Obj', path, plotextension)
# plotkde(TFO, path, name, plotextension, 'Obj', 'length (nm)')