-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbc_histogram.py
1440 lines (1221 loc) · 50.8 KB
/
rbc_histogram.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Module for plotting histograms of generator data.
"""
from rbc_current import *
import numpy
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.inset_locator import inset_axes, zoomed_inset_axes
from mpl_toolkits.axes_grid.inset_locator import mark_inset
import matplotlib.colors as colors
import timer
slash = '/'
def dict_of_cell_dirs( cell_dirs ):
"""
cell_dirs -- list of directories (cells) containing cell data.
"""
# handles last entry in list returned by split
if cell_dirs[0].endswith( slash ):
last = -2
else:
last = -1
prefix = slash.join( cell_dirs[0].split( slash )[:last] )
prefix += slash
print prefix
cellnames = [ c.split( slash )[last] for c in cell_dirs ]
print cellnames
cells = dict.fromkeys( cellnames )
for k in cells.keys():
cells[k] = dir_list( prefix + k + slash )
return cells
# def load_birth_times( fname ):
# with open( fname ) as fh:
# return pkl.load( fh )
def concat_histograms( ts, width=10 ):
"""
ts -- dictionary of time series. See concat_timeseries() below.
"""
all_hist = []
# set common bin width
dmax = 0
for d in ts.itervalues():
dmax = max
for i, data in enumerate( ts.itervalues() ):
thebins = numpy.arange( 0, data.max(), width )
hist, bins = numpy.histogram( data, bins=thebins )
all_hist.append( hist )
return all_hist
def cell_max( fdir, dim=1 ):
"""
For normalization purposes, find the maximum height over all
frames for a given cell.
"""
if not fdir.endswith( slash ): fdir += slash
frames = os.listdir( fdir )
ending = str( dim ) + '.txt'
frames = [ fdir+f for f in frames if f.endswith( ending ) ]
the_max = 0
for frame in frames:
# from rbc_current
x = get_Max( frame )
if x > the_max:
the_max = x
return the_max
def all_maxes( cell_list ):
"""
"""
max_list = [ ( cell, int( cell_max( cell ) ) )
for cell in cell_list ]
return max_list
def get_birth_times( cell, eps1, eps2=150, normed=True, first_bt=False ):
"""
cell -- path to directory containing Perseus generator
file (one for each frame).
eps1 -- minimum lifespan
eps2 -- maximum lifespan
(So [eps1,eps2], stretched along the diagonal, is the band that we
store generators from.)
For each frame in cell==cell_dir: find generator lifespans. Find first
occurence, \tau, of a midrange generator ( use get_gens_between()
for this, then peel off the birth time from the first
(birth,death) pair to get birth time) store \tau.
NOTE: Depending on eps1, very rarely a list of gens will be
empty. We treat this as missing data and continue to loop over the
frames.
"""
frames = dir_list( cell )
birth_times = []
for frame in frames:
if normed:
gens = get_gens_between_normed( frame, eps1, eps2 )
else:
gens = get_gens_between( frame, eps1, eps2 )
if not gens:
continue
if first_bt:
birth_times.append( gens[0][0] )
else:
birth_times.append( gens )
return birth_times
def get_midrange_gens( cell, eps1, eps2=350, normed=False):
"""
cell -- path to directory containing Perseus generator
file (one for each frame).
eps1 -- minimum lifespan
eps2 -- maximum lifespan
(So [eps1,eps2], stretched along the diagonal, is the band that we
store generators from.)
For each frame in cell==cell_dir: find generator lifespans. Find first
occurence, \tau, of a midrange generator ( use get_gens_between()
for this, then peel off the birth time from the first
(birth,death) pair to get birth time) store \tau.
NOTE: Depending on eps1, very rarely a list of gens will be
empty. We treat this as missing data and continue to loop over the
frames.
"""
frames = dir_list( cell )
gen_stats = []
for frame in frames:
# get the midrange gen stats for frame (normed or not)
if normed:
gstats = get_gens_between_normed( frame, eps1, eps2, means=True )
else:
gstats = get_gens_between( frame, eps1, eps2 )
# if not gstats:
# continue
gen_stats.append( gstats )
return gen_stats
def plot_boxplot( data, vert=1, pa=True, transparent=True ):
"""
data -- a vector or list of vectors. Can be of various
lengths. Originally created for vectors consisting of lag 1 norms
of persistence distances.
vert -- vert==1 ==> vertical orientation;
vert==0 ==> horizontal orientation
(follows boxplot() convention)
pa -- patch_artist: set to True for solid boxes.
Produces a boxplot (see pylab doc).
"""
fig = plt.figure()
if transparent:
fig.patch.set_alpha( 0.0 )
ax = fig.gca()
ax.boxplot( data, vert=vert, patch_artist=pa )
if vert == 1:
ax.set_xlabel( r'Cell type', fontsize=24 )
ax.set_ylabel( r'Lag 1 norm', fontsize=24 )
# rename the xlabels
ax.set_xticklabels( ['Young', 'Old'], fontsize=20 )
# make the ylabels bigger
yticks = ax.get_yticks()
ax.set_yticklabels( [str(int(y)) for y in yticks], fontsize=20 )
# vert better equal 0
else:
ax.set_ylabel( r'Cell type', fontsize=24 )
ax.set_xlabel( r'Lag 1 norm', fontsize=24 )
# rename the xlabels
ax.set_yticklabels( ['Young', 'Old'], fontsize=20 )
# make the ylabels bigger
yticks = ax.get_xticks()
ax.set_xticklabels( [str(int(y)) for y in yticks], fontsize=20 )
fig.show()
return fig
def boxplotter( fname='/Users/jberwald/github/local/caja-matematica/pyRBC/data/lag1_all.pkl',
**kwargs ):
"""
fname -- Pickled dictionary with lag k distance vector norms.
"""
with open( '/Users/jberwald/github/local/caja-matematica/pyRBC/data/lag1_all.pkl' ) as fh:
all_norms = pkl.load( fh )
old = [ all_norms[k] for k in all_norms if 'o' in k ]
new = [ all_norms[k] for k in all_norms if 'n' in k ]
norm_vecs = [ new, old ]
fig = plot_boxplot( norm_vecs, **kwargs )
return fig
def plot_hist_birth_times( bt_old=None, bt_new=None,
stacked=None,
normalize=False, bins=50,
transparent=True, log=False, **kwargs ):
"""
Plot histograms of birth times. Provide both to plot both new and
old on one histogram.
bt_* -- list of birth times for each cell.
"""
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
fig = plt.figure( figsize=(10,8) )
if transparent:
fig.patch.set_alpha( 0.0 )
ax = fig.gca()
if bt_old is not None:
# concatenate data if bt_old is not an array
if not hasattr( bt_old, "mean" ):
old = numpy.hstack( bt_old )
else:
old = bt_old
oldhist, bins, patches = ax.hist( old, bins=bins, hatch='//',
color='r', alpha=0.75, log=log,
normed=normalize, label="Old cells" )
mean_old = old.mean()
median_old = numpy.median( old )
std_old = old.std()
ax.axvline( mean_old,
color='k', linestyle='dashed', lw=1.5 )
ax.axvspan( mean_old - std_old,
mean_old + std_old, facecolor='r', alpha=0.3 )
if bt_new is not None:
if not hasattr( bt_new, "mean" ):
new = numpy.hstack( bt_new )
else:
new = bt_new
newhist, bins, patches = ax.hist( new, bins=bins,
color='b', alpha=0.75, log=log,
normed=normalize, label="New cells" )
mean_new = new.mean()
median_new = numpy.median( new )
std_new = new.std()
ax.axvline( mean_new, #mean_new,
color='k', linestyle='dashed', lw=1.5 )
ax.axvspan( mean_new - std_new,
mean_new + std_new, facecolor='b', alpha=0.3 )
else:
# this logic isn't right for the if-else block. But we'll just move on for now...
if not bt_old and not bt_new:
print "Must provide at least one set of birth times."
return None
# ax.set_xlabel( r'$\tau$ (normalized)', fontsize=16 )
ax.set_xlabel( r'First birth time', fontsize=16 )
ax.set_ylabel( r'Number of frames', fontsize=16 )
# ax.set_yticklabels( [str( int(y) ) for y in yticks
# if y!=0 else ], fontsize=20 )
# This part adds the arrows defined above.
# collection = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
# ax.add_collection(collection)
# set axes limits to align with top of vlines for aesthetic
# reasons
themin = min( old.min(), new.min() )
themax = max( old.max(), new.max() )
ax.set_xlim( themin, themax ) #max( old_max, new_max ) )
xticks = ax.get_xticks()
yticks = ax.get_yticks()
ax.set_xticklabels( [str( x ) for x in xticks], fontsize=14 )
ylabels = []
for y in yticks:
if y!=0:
ylabels.append( str( int(y) ) )
else:
ylabels.append( ' ' ) # empty 0 on y axis
ax.set_yticklabels( ylabels, fontsize=14 )
plt.legend()
fig.show()
return fig, new, old
def plot_simple_hist( ts, nbins=1000, color='b' ):
"""
ts -- single time series of values to bin.
nbins -- number of bins to use
Returns Figure object.
"""
fig = plt.figure( figsize=(12,8) )
fig.patch.set_alpha( 0.0 )
ax = fig.gca()
n, bins, patches = ax.hist( ts, bins=nbins, color=color, log=True,
edgecolor='none' )
ax.set_xlabel( 'Lifespan', fontsize=20 )
ax.set_ylabel( 'Number of generators', fontsize=20 )
ax.tick_params( axis='both', which='major', labelsize=16 )
#ax.tick_params(axis='both', which='minor', labelsize=8)
ax.set_ylim( bottom=0.5 )
return fig
def plot_hist_all( ts, nbins=50, transparent=True, norm_it=False, **kwargs ):
"""
ts -- dictionary with values as lifespan timeseries.
kwargs -- see pylab hist() function
Returns interpolation functions as well as histogram triple and figure instance.
"""
from pylab import log1p
from matplotlib.mlab import stineman_interp
#data = ts.values()
data = ts
fig = plt.figure( figsize=(12,8) )
if transparent:
fig.patch.set_alpha( 0.0 )
# ax = fig.add_subplot( 121 )
# ax2 = fig.add_subplot( 122 )
ax2 = fig.add_subplot( 111 )
# now plot a single cell's histogram on the first axis
#ax.hist( data, **kwargs )
xmax = max( [d.max() for d in data] )
thebins = numpy.linspace(0, xmax, nbins )
kwargs['bins'] = thebins
# holds bins counts (n) for each histogram
all_ny = []
for d in data:
n, bins, patches = ax2.hist( d, **kwargs )
all_ny.append( n )
# all_bins.append( bins )
# convert bins counts to a single array to find min
arr = numpy.array( all_ny ).ravel()
wy = arr[ numpy.where( arr != 0 )[0] ]
min_y = min( wy )
# for plotting average -- these are already log values if
# log==True in kwargs
yhist = numpy.array( all_ny, dtype=numpy.float64 )#.ravel()
avg = yhist.mean( axis=0 )
err = yhist.std( axis=0 )
upper = avg + err
lower = avg - err
# print "yhist", yhist
# print "avg", avg
# print ""
# print err
# print ""
# print lower
# print upper
# print ""
# average value for each histogram bin
for i, x in enumerate( avg ):
if x == 0.0:
avg[i] = 1.0
# label the axes
ax2.set_xlabel( 'Generator lifespan', fontsize=20 )
ax2.set_ylabel( 'Number of generators (log)', fontsize=20 )
xticks = ax2.get_xticks()
yticks = ax2.get_yticks()
ax2.set_xticklabels( [str(int(x)) for x in xticks], fontsize=20 )
ax2.set_yticklabels( [str(int(y)) for y in yticks], fontsize=20 )
# now plot the interpolated average distribution here so it is on
# top of the other stuff
yp = None
xi = numpy.linspace( 0, bins[-1],200)
yi = stineman_interp( xi, bins[:-1], avg, yp )
# interpolate upper and lower error bars to get envelope
# COMMENTED OUT BELOW
upper_yi = stineman_interp( xi, bins[:-1], upper, yp )
lower_yi = stineman_interp( xi, bins[:-1], lower, yp )
# make sure lower does not go negative since this makes no sense.
for i,v in enumerate( lower_yi ):
if v < 1.0:
lower_yi[i] = 1.0
# make sure that the plot doesn't get messed up by small values
# (esp. New cells)
masked_yi = numpy.ma.masked_less( yi, 1 )
# plot the interpolation of the avg and the envelope
ax2.plot( xi, masked_yi, 'r-', lw=3 )
# ax2.fill_between( xi, lower_yi, upper_yi, #where=masked_yi
# color='r', alpha=0.5, zorder=10 )
fig.show()
if norm_it:
y_max = masked_yi.max()
print "y_max", y_max
masked_yi /= y_max
return xi, masked_yi, lower_yi, upper_yi, fig, (n, bins, patches)
def plot_hist_figure( ts, persfile=None, single=1, norm_it=True,
nbins=100, cell_type='new', vline=None ):
"""
Plot the histogram figure for the RBC paper.
ts -- List of arrays of times series of generator lifespans for
each cell.
persfile -- full path to a single persistence file (i.e. fsingle frame of single cell)
single -- Cell to choose from list to compute histogram statistics
on. Should be the same cell used in <persfile>. (Default=1,
corresponds to new11 in ordered list (see below)).
norm_it -- Toggle whether to return a normalized histogram.
nbins -- number of bins.
new -- New or Old cells.
vline -- x-axis location of vertical dotted line. If None, no line is drawn.
Note: Values used in RBC paper:
new_hist_ts.pkl
old_hist_ts.pkl
new_110125-concatenated-ASCII_2000_1.txt
old_120125-concatenated-ASCII_2000_1.txt
"""
# for text object $\tau^*$ below
from matplotlib.text import Text
if cell_type == 'new':
color = 'blue'
ctype = 'new'
elif cell_type == 'old':
color = 'red'
ctype = 'old'
else:
color = 'green'
ctype = 'all'
# compute stats for all cells
out_all = plot_hist_all( ts, norm_it=norm_it )
allx = out_all[0]
ally = out_all[1]
# compute stats for chosen single cell
if single is not False:
out = plot_hist_all( [ts[single]], norm_it=norm_it )
nx = out[0]
ny = out[1]
pdf_ny = pdf( nx, ny )
# now normalize everything by dividing by total area ( y --> PDF )
pdf_ally = pdf( allx, ally )
# output some stats
# print "\int { pdf_ally } = ", ((nx[1:]-nx[:-1]) * pdf_ally[:-1]).sum()
# print "\int { pdf_ny } = ", ((nx[1:]-nx[:-1]) *pdf_ny[:-1]).sum()
fig = plt.figure()
ax = fig.gca()
# ax.set_xscale( 'log' )
ax.set_yscale( 'log' )
#ax.set_aspect( 1 )
if single is not False:
ax.plot( nx, pdf_ally, lw=3, c='g', label='Mean, all '+ctype+' cells' )
ax.plot( nx, pdf_ny, lw=3, c='m', marker='^', ms=8,
label='Mean, single '+ctype+' cell' )
if vline:
ax.axvline( vline, linestyle=':', color='k' )
# add a histogram for a single frame
if persfile:
ts = numpy.asarray( get_ts( persfile ), dtype=numpy.int )
n, bins = numpy.histogram( ts, bins=nbins, range=(nx.min(),nx.max()) )
ts_pdf = pdf( bins[:-1], n )
#print 'ts_pdf', ((bins[1:] - bins[:-1])*ts_pdf).sum()
width = bins[1]-bins[0] #nx[1]-nx[0]
ax.bar( bins[:-1], ts_pdf, width=width, color=color, alpha=0.5,
label='Single frame distribution' )
#ax.plot( bins[:-1], ts_pdf, marker='o', ms=6, lw=3, label='Single frame' )
#ax.set_xticklabels( [str(int(x)) for x in xticks], fontsize=20 )
plt.ylim( 0.00001, 0.1 )
plt.xlim( right=150 )
if vline:
# add a \tau^* i nthe right spot
tks, labels = plt.xticks()
tks = list( tks )
tks.append( vline )
tks.sort()
# find index of new vline tick
loc = tks.index( vline )
tau = Text( text='$\tau^{*}$' )
new_labs = []
# insert \tau into correct spot (Text( '\tau' ) doesn't seem to
# work)
for x in tks:
if x == vline:
if ctype == 'new':
L_text = r'$L_{new}$'
elif ctype == 'old':
L_text = r'$L_{old}$'
else:
L_text = r'$L_{all}$'
new_labs.append( L_text )
else:
new_labs.append( str( int(x) ) )
ax.set_xticks( tks )
ax.set_xticklabels( new_labs, fontsize=12 )
# now back to normal labeling and stuff
plt.xlabel( 'Lifespan', fontsize=16 )
plt.ylabel( 'Normalized distribution', fontsize=16 )
plt.legend()
fig.show()
return fig, bins, ts_pdf #, n, bins
def pdf( xi, yi ):
"""
Normalize f(x) = y in terms of probability distribution
functions. Thus, it should return f such that
\int_{min(xi)}^{max(xi)} {f(x_i)*(x_{i+1}-x_{i}) = 1
"""
dx = xi[1:] - xi[:-1]
integ = numpy.sum( yi[:-1] * dx )
thepdf = yi/integ
return thepdf
def concat_timeseries( cells, ts_max=-1, skip=1, normed=False ):
"""
cells -- list of cell names (full paths to directories).
ts_max -- number of frames (so max length of ts). [default=-1, eg. all frames]
skip -- keep every <skip> frames
Returns dicitonary, values times series of generator data, keyed
by cell names
"""
all_ts = {}
for c in cells:
# list of lifespans for each frame in cell
# concatenate all of the diagrams for cell lifespans into one
# timeseries
ts = [ get_ts ( frame )
for frame in cells[c][:ts_max:skip] ]
ts = numpy.hstack( ts )
# convert and normalize, then append to the list of time series
if normed:
ts = numpy.asarray( ts, dtype=numpy.float )
ts /= ts.max()
else:
ts = numpy.asarray( ts, dtype=numpy.int )
all_ts[c]= ts
return all_ts
def plot_scatter( ts, log=False, cutoff=0.2 ):
"""
ts -- dictionary of time series data. keys are cell names and
values are 1D numpy arrays.
"""
fig = plt.figure()
ax = fig.gca()
#axins = inset_axes( ax, width='70%', height=1., loc=10 )
zoom = 2.5
axins = zoomed_inset_axes( ax, zoom, 1 ) # location == upper right
# create a color instance
rcolors = numpy.random.random( (len(cells),3) )
cc = colors.ColorConverter()
cmap = [ cc.to_rgb( c ) for c in rcolors ]
nx_max = ny_max = 0
for i, data in enumerate( ts.itervalues() ):
thebins = numpy.arange( 0, data.max(), 4 )
hist, bins = numpy.histogram( data, bins=thebins )
# store for later use
nx_max = max( thebins.max(), nx_max )
if log:
hist = numpy.log1p( hist )
ny_max = max( hist.max(), ny_max )
ax.plot( bins[:-1], hist, 'o-', ms=5, color=cmap[i] )
axins.plot( bins[:-1], hist, 'o-', ms=6, color=cmap[i] )
ax.set_xlabel( r'Generator lifespan', fontsize=20 )
if log:
ax.set_ylabel( r'Number of generators ($\log_{10}$)', fontsize=20 )
else:
ax.set_ylabel( r'Number of generators', fontsize=20 )
# find axis max over all time series
# ny_max = max( [ y.max() for y in ts.values() ] )
# sub region of the original image
yscale = ny_max * (0.4)
# set the zoom cutoff as a percentage of the max
xmax = cutoff * nx_max
x1, x2, y1, y2 = 10, xmax, 0, yscale
print x1, x2, y1, y2
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
axins.set_aspect( xmax/yscale )
axins.set_xticks([])
axins.set_yticks([])
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
fig.show()
return fig
def plot_hist( fname, nbins=None, scale=1.0, color='blue',
gaussian=False, sigma=1.0, normed=False, fontsize=14 ):
"""
Plot a histogram of generator lifespan along the diagonal.
fname -- full path to perseus output file.
"""
# for normpdf() function
import matplotlib.mlab as mlab
ts = get_ts ( fname )
# the (almost) infinite generator overwhelms the plot
ts = ts[:-1]
print ts
if scale:
ts = numpy.asarray( ts, dtype=numpy.float )
ts /= scale
# plot the histogram
fig = plt.figure()
ax = fig.gca()
# the histogram of the data
if not nbins:
nbins = ts.max()-ts.min()
n, bins, patches = ax.hist( ts, bins=nbins, normed=normed,
facecolor=color, alpha=0.75)
if gaussian:
mu = 0
bincenters = 0.5 * ( bins[1:] + bins[:-1] )
# add a 'best fit' line for the normal PDF
y = mlab.normpdf( bincenters, mu, sigma)
l = ax.plot( bincenters, y, 'r--', linewidth=2 )
# xticks = [ int( tk ) for tk in ax.get_xticks() ]
# yticks = [ int( tk ) for tk in ax.get_yticks() ]
# ax.set_xticklabels( xticks, fontsize=fontsize )
# ax.set_yticklabels( yticks, fontsize=fontsize )
#ax.set_title( r'Distribution of generator lifespans along diagonal' )
ax.set_xlabel( r"Lifespan", fontsize=fontsize )
ax.set_ylabel( r"Number of generators ($\beta_1$)", fontsize=fontsize )
#ax.grid( True )
ax.set_xlim( 0,21 )
plt.show()
return fig, ts, bins
def dir_list( fdir, betti=1 ):
"""
Returns a list of Perseus output files for given betti #.
"""
dlist = os.listdir( fdir )
theFiles = [ fdir+f for f in dlist if f.endswith( '_'+str(betti)+'.txt' ) ]
theFiles.sort( key=natural_key )
return theFiles
def plot_hist_colors( cell, color='blue',
normed=False, fontsize=20,
threshold=50, cell_type='New',
show_plot=False, log=True):
"""
Plot a histogram of generator lifespan along the diagonal. This
allows more control over bin color.
cell -- full path to perseus output file.
"""
from matplotlib.ticker import ScalarFormatter
fig = plt.figure( dpi=80)
ax = fig.gca()
# cell is a list of frames
if hasattr( cell, 'pop' ):
# create an array with first entry in cell, then extend in
# loop
ts = get_ts ( cell[0] )
#ts = ts[:-1]
for f in cell[1:]:
new_ts = get_ts ( f )
# the (almost) infinite generator overwhelms the plot
#new_ts = new_ts[:-1]
# extend last ts of generators by new_ts
ts = numpy.hstack( ( ts, new_ts ) )
# the histogram of the data
#vals = vals[1:]
# create two identical histograms
n, bins, patches = ax.hist( ts, bins=(ts.max()-ts.min()),\
normed=normed, facecolor=color,\
alpha=0.75, histtype='bar', log=log )
# or cell is just a single frame
else:
ts = get_ts ( cell )
# the (almost) infinite generator overwhelms the plot
ts = ts[:-1]
# the histogram of the data
n, bins, patches = ax.hist( ts, bins=ts.max()-ts.min(),\
normed=normed, facecolor=color,\
alpha=0.75 )# histtype='step')
# # we need to normalize the data to 0..1 for the full
# # range of the colormap
# fracs = N.astype(float)/N.max()
# norm = colors.normalize(fracs.min(), fracs.max())
theColors = [ 'b', 'r' ]
for num_gens, patch in zip( bins[:-1], patches ):
if num_gens > threshold:
color = theColors[ 1 ]
# patch.set_width( 1.0 )
else:
color = theColors[ 0 ]
#patch.set_width( 1.0 )
patch.set_facecolor( color )
# set some axis attributes
xticks = [ int( tk ) for tk in ax.get_xticks() ]
yticks = [ int( tk ) for tk in ax.get_yticks() ]
ax.set_xticklabels( xticks, fontsize=fontsize )
ax.set_yticklabels( yticks, fontsize=fontsize )
#ax.set_title( r''+str( cell_type ) + ' cell', fontsize=24 )
ax.set_xlabel( r"Lifespan (death-birth)", fontsize=fontsize )
ax.set_ylabel( r"$\beta_1$", fontsize=fontsize )
#ax.ticklabel_format( style='sci', axis='y' ) # scientific notation
sf = ScalarFormatter()
sf.set_scientific( True )
ax.grid( True )
if show_plot:
plt.show()
return fig, ts
def plot_midrange_ts( new_file, old_file, skip=10, fontsize=20,
lines=None, plot_mean=False, means=False ):
"""
Plots two times series of midrange generators.
"""
if type( new_file ) == str and type( old_file ) == str:
ts_new = numpy.loadtxt( new_file )
ts_old = numpy.loadtxt( old_file )
else:
ts_new = new_file
ts_old = old_file
fig = plt.figure( dpi=160, figsize=([10,4]) )
ax = fig.gca()
ax.plot( ts_new[::skip], 'b-', linewidth=2 )
ax.plot( ts_old[::skip], 'r-', linewidth=2 )
# plot vertical lines to point out location of sublevel sets in ts
if lines:
vmin, vmax = ax.get_ylim()
smidge = 0.1
print vmin, vmax
for line in lines:
v = int( line/float(skip) )
ax.vlines( v, vmin+smidge, vmax, linestyle='dashed', linewidth=2 )
if means:
ax.axhline( ts_new.mean(), color='k', linestyle='--', lw=2, label="New cell mean" )
ax.axhline( ts_old.mean(), color='k', linestyle='--', lw=2, label="Old cell mean" )
# set some axis attributes; account for skip when setting tick marks
xticks = [ skip*int( tk ) for tk in ax.get_xticks() ]
yticks = [ int( tk ) for tk in ax.get_yticks() ]
ax.set_xticklabels( xticks, fontsize=fontsize )
ax.set_yticklabels( yticks, fontsize=fontsize )
ax.set_xlabel( r"Frame Number", fontsize=fontsize )
ax.set_ylabel( r"Midrange generators", fontsize=fontsize )
# plot mean and std range
if plot_mean:
# for plotting average -- these are already log values if
# log==True in kwargs
yhist = numpy.array( all_ny, dtype=numpy.float64 )#.ravel()
avg = yhist.mean( axis=0 )
err = yhist.std( axis=0 )
upper = avg + err
lower = avg - err
# now plot the interpolated average distribution here so it is on
# top of the other stuff
yp = None
xi = numpy.linspace( 0, bins[-1],200)
yi = stineman_interp( xi, bins[:-1], avg, yp )
# interpolate upper and lower error bars to get envelope
# COMMENTED OUT BELOW
upper_yi = stineman_interp( xi, bins[:-1], upper, yp )
lower_yi = stineman_interp( xi, bins[:-1], lower, yp )
#fig.show()
return fig
def plot_hist_cut_axis( cells, color='blue',
normed=False, fontsize=20,
cell_type='New', skip=1,
show_plot=False, log=False,
left_xlim=300, right_xlim=1600,
cutoff=0.08, ts_max=100, histtype='bar' ):
"""
Plot a histogram of generator lifespans computed as distance from diagonal.
cell -- full path to perseus output file.
"""
from mpl_toolkits.axes_grid.inset_locator import inset_axes, zoomed_inset_axes
from mpl_toolkits.axes_grid.inset_locator import mark_inset
# timing
start = time.time()
# cell is a list of frames
# create an array with first entry in cell, then extend in
# loop
# ts = [ get_ts( cell[0] ) ]
# # ts = ts[:-1]
# for f in cell[1:ts_max]:
# ts.append( get_ts( f ) )
# #new_ts = get_ts ( f )
# # the (almost) infinite generator overwhelms the plot
# # new_ts = new_ts[:-1]
# # extend last ts of generators by new_ts
# #ts = numpy.hstack( ( ts, new_ts ) )
# # the histogram of the data
# #vals = vals[1:]
# # convert and normalize
# ts = numpy.hstack( ts )
# ts = numpy.asarray( ts, dtype=numpy.float )
# ts /= ts.max()
all_ts = []
for c, cell in enumerate( cells ):
# list of lifespans for each frame in cell
# concatenate all of the diagrams for cell lifespans into one
# timeseries
ts = [ get_ts ( frame ) for frame in cell[:ts_max:skip] ]
ts = numpy.hstack( ts )
# convert and normalize, then append to the list of time series
ts = numpy.asarray( ts, dtype=numpy.float )
if not log:
ts /= ts.max()
all_ts.append( ts )
newtime = time.time()
print "Done creating time series. Took", newtime - start, " seconds"
start = newtime
# This method works with mpl 0.99 ( plt.subplot() works with >1.1.0 )
fig = plt.figure( )
ax = fig.add_subplot( 121 )
ax2 = fig.add_subplot( 122 )
# create two identical histograms
n, bins, patches = ax.hist( all_ts, bins=int(len(ts)/10.),\
normed=normed, facecolor=color,\
alpha=0.75, histtype=histtype, log=log )
n, bins, patches = ax2.hist( all_ts, bins=int(len(ts)/10.),\
normed=normed, facecolor=color,\
alpha=0.75, histtype=histtype, log=log )
# make the inset
# options of loc: BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = range(11)
#axins = zoomed_inset_axes(ax, 10, loc=10 ) # ( axes, zoom power, location )
if not log:
axins = inset_axes( ax, width='70%', height=1., loc=10 )
axins.hist( ts, bins=int(len(ts)/10.),\
normed=normed, facecolor=color,\
alpha=0.75, histtype=histtype, log=log )
print "Done creating histograms. Took", time.time() - start, " seconds"
# set box position by hand
# pos = [left, bottom, width, height]
# axins.set_position
# hide spines between axes
ax.spines['right'].set_visible(False)
ax.xaxis.tick_bottom()
ax.yaxis.tick_left()
# the tail side
ax2.spines['left'].set_visible(False)
ax2.xaxis.tick_bottom()
ax2.yaxis.tick_right()
#ax2.set_yticks( [] )
#ax.tick_params(labeltop='off') # don't put tick labels at the top
d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
# remember, plot takes list of x's, followed by list of y's
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot( (1-d,1+d),(-d,+d), **kwargs ) # bottom-right diagonal
ax.plot( (1-d,1+d),(1-d,1+d), **kwargs ) # top-right diagonal
kwargs.update(transform=ax2.transAxes) # switch to the right axes
ax2.plot((-d,+d),(-d,+d), **kwargs) # bottom-left diagonal
ax2.plot((-d,+d),(1-d,1+d), **kwargs) # top-left diagonal
# vertical separator
ax2.set_ylabel( '---------- ---------- ---------- ---------- ----------', horizontalalignment='center' )
x_max = max( [x.max() for x in all_ts] )
y_max = max( [y.max() for y in n ] )
# zoom-in / limit the view to different portions of the data
ax.set_xlim( 0., left_xlim ) # most of the data
#ax.set_ylim( 0., y_max+ 10 )
ax2.set_xlim( right_xlim, x_max + 0.01) # outliers/inf gens
#ax2.set_ylim( 0., 0.001 * y_max ) # zoom in to see the inf gens
if not log:
# sub region of the original image
yscale = n.max() * (1./60)
# set the zom cutoff as a percentage of the max
xmax = cutoff * ts.max()
x1, x2, y1, y2 = 0.015, xmax, 0, yscale
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
axins.set_aspect( xmax/yscale )
axins.set_xticks([])
axins.set_yticks([])
# set some tick marks
left_ticks = numpy.arange( 0, left_xlim-0.01, 0.02 )
left_labels = [ str( x ) for x in left_ticks ]
ax.set_xticks( left_ticks )
ax.set_xticklabels( left_labels )
# scientific notation
if not log:
ax.ticklabel_format( style='sci', scilimits=(0,0), axis='y' )
# draw a bbox of the region of the inset axes in the parent axes and
# connecting lines between the bbox and the inset axes area
if not log:
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
fig.subplots_adjust( wspace=0.1 )
plt.show()
return fig, ts
def plot_hist_stack( cells, color='blue',
normed=False, fontsize=20,
cell_type='New',
show_plot=False, log=False,
left_xlim=300, right_xlim=1600,
cutoff=0.08, ts_max=100, histtype='stepfilled',
rwidth=1, skip=1, nbins=500, nbins_inset=200):
"""
cells -- list of cells (full paths to) whose generator lifespans
we want to stack in a single histogram.
This is similar to the above, plot_hist_cut_axis(), except that it
stacks numerous histograms.
"""
from mpl_toolkits.axes_grid.inset_locator import inset_axes, zoomed_inset_axes
from mpl_toolkits.axes_grid.inset_locator import mark_inset
import matplotlib.colors as colors
# This method works with mpl 0.99 (plt.subplot() works with >1.1.0 )
fig = plt.figure( figsize=(6,5) )
ax = fig.add_subplot( 121 )
ax2 = fig.add_subplot( 122 )
# time shit
t0 = start = time.time()