forked from AFM-SPM/TopoStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAngles.py
129 lines (102 loc) · 4.07 KB
/
Angles.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
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 savestats(path, name, dataframetosave):
print 'Saving JSON file for: ' + str(name)
savename = os.path.splitext(file)[0]
if not os.path.exists(path):
os.makedirs(path)
dataframetosave.to_json(savename + '.json')
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 plotscatter(df, directory, name, plotextension):
print 'Plotting scatter'
# 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 + plotextension)
sns.pairplot(x_vars=["Method"], y_vars=["Angle"], data=df, hue="Type", size=5)
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)
# Set the file path, i.e. the directory where the files are here'
path = '/Users/alice/Dropbox/UCL/DNA MiniCircles/Paper/Pyne et al/SI Angles'
# Set the name of the json file to import here
name = 'AnglesV2'
plotextension = '.pdf'
bins = 8
allbins = 20
# # import data from the csv file specified as a dataframe
# angles = pd.read_csv(os.path.join(path, 'AnglesV2short.csv'))
angleslong = pd.read_csv(os.path.join(path, 'AnglesV2.csv'))
df = angleslong.dropna()
details = df.groupby('Type').describe()
plotscatter(df, path, name, plotextension)