/
shell.py
163 lines (137 loc) · 4.42 KB
/
shell.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
from matplotlib.cbook import flatten
import matplotlib.pyplot as plt
from numpy import linspace, append, diff
from collections import namedtuple
from itertools import groupby
import fplot, experiment, curvefit, fec, fcalc, fileIO, refolding
from datatypes import TrapData, FretData
try:
import db
except ImportError:
print "Please install pymongo"
import os
import datetime
_modules_ = ["fplot", "fileIO", "experiment", "curvefit", "refolding",
"fec", "fcalc", "db", "os"]
# from importlib import import_module
# _imported_ = map(import_module, _modules_)
# for modname in _modules_:
# mod = import_module(modname)
# _all_ = mod.__all__
from figure import Figure
from experiment import *
from fec import nm_to_nt, Rips
from date import today, date, to_date
_names_ = experiment.__all__
_date_ = ['today', 'date', 'to_date']
fec_names = ['fec', 'nm_to_nt', 'Rips']
__all__ = _modules_ + _names_ + fec_names + _date_ \
+ ['TrapData', 'FretData'] \
+ [ "fig", "pretty_rip_sizes", "pick_pts", "pick_pt", "pick_line",
"pick_intervals", "Interval", "group_by", "to_date", "transposed",
"savefig", "plot_segmented", "reload_all", "getitems", "flatten_"]
Interval = namedtuple('Interval', 'start end')
def reload_all():
reload(fplot)
reload(fileIO)
reload(experiment)
reload(fec)
reload(db)
def getitems(arr, *keys):
return map(operator.itemgetter(*keys), arr)
def savefig(fname=None, **kwargs):
fplot.save(fname, **kwargs)
def fig(name, title=''):
'''Create and return new Figure window with internal @name
If @title is True, automatically set to @name (default no title)
Set window title to @title if string.
'''
fig_ = Figure(name).new()
fig_.clear()
if title is True:
title = name
if title:
plt.title(title)
return fig_
def hspan(start, stop, color=None, alpha=.2):
color = color or next(fplot.COLOR)
plt.axhspan(start, stop, facecolor=color, alpha=alpha)
def plot_segmented(p, title='', exp=[]):
trap,fret = p.trap,p.fret
ratio = p['sampling_ratio']
exp_time = p['fret.exposurems']/1000.
plt.hold(True)
plt.subplot(211)
plt.title(title)
plt.plot(fret.time, fret.fret, 'k:')
plt.subplot(212)
for _ in exp:
plt.plot(_.trap.ext, _.trap.f, 'k:', linewidth=1)
for start_n in range(len(fret)):
plt.subplot(211)
time,donor,acc,E = fret[start_n:start_n+2]
plt.plot(time, E, 'o', markersize=8)
plt.subplot(212)
x,f,sep = trap[start_n*ratio:(start_n+1)*ratio+1].T
plt.plot(x, f, '-', linewidth=3)
plt.subplot(211)
plt.xlim(0,time+exp_time)
plt.ylim(-0.05,1.1)
plt.xlabel('Time (s)')
plt.ylabel('FRET')
plt.subplot(212)
plt.xlabel('Extension (nm)')
plt.ylabel('Force (pN)')
plt.autoscale(tight=True)
def pick_pt():
return pick_pts(1)[0]
def pick_pts(num=1):
return Figure.fromCurrent().pickPoints(num)
def pick_intervals(num=1):
'''
Return list of Intervals picked from current plot
'''
return map(Interval._make, Figure.fromCurrent().pickRegions(num))
def region_to_str(regions):
return '\n'.join(
'Region {n}: ({:0.1f}, {:0.1f})'.format(
n=n, *region) for n,region in enumerate(regions)
)
def transposed(iterable):
return [[y[n] for y in iterable] for n in range(len(iterable[0]))]
def truncate_floats(iterable, places=2):
fmt_str = '{{:.{}f}}'.format(places)
return map(lambda s: fmt_str.format(s), iterable)
def pretty_rip_sizes(rip_sizes, helices):
rips_nm = truncate_floats(rip_sizes, places=1)
rips_nt = truncate_floats(
map(nm_to_nt, rip_sizes, helices),
places=1)
return ', '.join('{} nt ({} nm)'.format(in_nt, in_nm)
for in_nt,in_nm in zip(rips_nt, rips_nm))
def flatten_(arr):
return list(flatten(arr))
def line_from_two_xy_pts(pts):
assert len(pts) == 2
m,b = linear_fit_from_two_xy_pts(pts)
return lambda x: m*x + b
def linear_fit_from_two_xy_pts(pts):
assert len(pts) == 2
pts = sorted(pts)
x0,y0 = pts[0]
x1,y1 = pts[1]
m = (y1-y0)/(x1-x0)
b = y0 - m*x0
return m,b
def linspace_pts(pts):
assert len(pts) == 2
pts = sorted(pts)
x0,x1 = pts[0][0], pts[1][0]
return linspace(x0, x1)
def pick_line(plot=True):
pts = plt.ginput(2)
ll = line_from_two_xy_pts(pts)
if plot:
X = linspace_pts(pts)
plt.plot(X, ll(X))
return ll