-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdynsnap.py
1301 lines (1171 loc) · 51.9 KB
/
dynsnap.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
# Richard Darst, November 2014
from __future__ import print_function, division
import argparse
import collections
import datetime
import heapq
from itertools import product
import math
from math import floor, log, log10, sqrt
import numpy
import os
import random
import sqlite3
import sys
import time
# Python 2/3 compatibility. Avoid dependency on six for one function.
if sys.version_info.major < 3:
def iteritems(x): return x.iteritems()
else:
def iteritems(x): return iter(x.items())
from events import Events, load_events
# This is used to hold output, but it is not really used anymore.
ResultsRow = collections.namedtuple('ResultsRow',
('tlow', 'thigh', 'dt', 'x_max', 'measure_data',
'finder_data'))
# The following implement weighed sets.
class _LenProxy(object):
def __init__(self, l): self.l = l
def __len__(self): return self.l
class WeightedSet(object):
"""Weighted set implementation, emulating set()"""
def __init__(self, it):
data = self._data = { }
for x, w in it:
if x not in data: data[x] = w
else: data[x] += w
@classmethod
def _from_data(cls, data):
"""Directly create a WeightedSet from a data dict"""
self = cls([])
self._data = data
return self
def update(self, it):
"""Like set.update() but add weights"""
data = self._data
for x, w in it:
if x not in data: data[x] = w
else: data[x] += w
def __len__(self):
"""Number of elements in set"""
return len(self._data)
def __and__(self, other):
"""Set intersection"""
if len(self) <= len(other):
A, B = self._data, other._data
else:
A, B = other._data, self._data
# A is the smaller set
intersection = 0.0
for x, w in iteritems(A):
if x in B:
intersection += min(A[x], B[x])
return _LenProxy(intersection)
def __or__(self, other):
"""Set union"""
if len(self) <= len(other):
A, B = self._data, other._data
else:
A, B = other._data, self._data
# A is the smaller set
union = sum(B.itervalues())
for x, w in iteritems(A):
union += max(0, A[x] - B.get(x, 0))
return _LenProxy(union)
def union(self, other):
"""Set union, return new set"""
if len(self) <= len(other):
A, B = self._data, other._data
else:
A, B = other._data, self._data
data = dict(B)
# A is the smaller set
for x, w in iteritems(A):
if x not in data: data[x] = w
else: data[x] += w
return self._from_data(data)
def dot(self, other):
"""Dot product of two sets (as vectors)"""
if len(self._data) <= len(other._data):
A, B = self._data, other._data
else:
A, B = other._data, self._data
# A is the smaller set
return sum(w*B.get(x, 0.0) for x, w in iteritems(A))
def dot_uw(self, other):
"""Dot product of two sets (as vectors), unweighted"""
if len(self._data) <= len(other._data):
A, B = self._data, other._data
else:
A, B = other._data, self._data
# A is the smaller set
return sum(1 for x, w in iteritems(A) if x in B)
def norm(self):
"""Norm of this set (as vector)"""
return sqrt(sum(w*w for w in self._data.itervalues()))
def test_weightedset():
"""Small test function to test weighted sets"""
from nose.tools import assert_equal
A = WeightedSet([('a',1), ('b',2),])
B = WeightedSet([('b',1), ('c',2),])
C = WeightedSet([('a',1), ('c',1), ('d',3)])
assert_equal(len(A & B), 1)
assert_equal(len(A | B), 5)
assert_equal(len(A & C), 1)
assert_equal(len(A | C), 7)
def approxeq(x, y, d=1e-6):
"""Approximately equal to function, to a tolerance d"""
if x+y==0: return x==y
return 2*abs(x-y)/(x+y) < d
class SnapshotFinder(object):
"""This is the core snapshot finder class. Basic usage:
evs = events.Events() # or load events
finder = SnapshotFinder(evs, ...)
while True:
out = finder.find()
if out is None: break
In most cases, you can use the main() function which handles much
more of the set up and data collection.
"""
quiet = False
old_es = None # None on first round
old_incremental_es = None
dt_min = None
dt_max = None
dt_step = 1
log_dt_min = None
log_dt_max = None
dt_first_override = None
merge_first = False
peakfinder = 'longest'
measure = 'jacc'
dtmode = 'log'
find_critical_events = True
dt_pastpeak_factor = 25
dt_peak_factor = 0.0
dt_pastpeak_min = 0
dt_pastpeak_max = None
dt_search_min = 0
def __init__(self, evs, tstart=None, tstop=None, weighted=False,
measure=None,
dtmode=None, peakfinder=None,
merge_first=None,
args={},
dt_min=None, dt_max=None, dt_step=None,
log_dt_min=None, log_dt_max=None,
dt_pastpeak_factor=None, dt_peak_factor=None,
dt_pastpeak_min=None, dt_pastpeak_max=None,
dt_search_min=0,
find_critical_events=None,
quiet=None,
):
self.evs = evs
if isinstance(args, argparse.Namespace):
args = args.__dict__
self.args = args
self.weighted = weighted
self.last_dt_max = 0
if len(evs) == 0:
raise ValueError("There are no events within our Events object!")
try:
if measure is None: measure = self.measure
self.measure = getattr(self, 'measure_'+measure)
except AttributeError:
raise ValueError("Unknown measure: %s"%measure)
if tstart is not None: self.tstart = tstart
else: self.tstart = evs.t_min()
if tstop is not None: self.tstop = tstop
else: self.tstop = evs.t_max()
try:
if dtmode is None: dtmode = self.dtmode
self.iter_all_dts = getattr(self, 'iter_all_dts_'+dtmode)
except AttributeError:
raise ValueError("Unknown dtmode: %s"%dtmode)
try:
if peakfinder is None: peakfinder = self.peakfinder
self.pick_best_dt = getattr(self, 'pick_best_dt_'+peakfinder)
except AttributeError:
raise ValueError("Unknown peakfinder: %s"%peakfinder)
locals_ = locals()
for name in ('dt_min', 'dt_max', 'dt_step',
'log_dt_min', 'log_dt_max',
'merge_first',
'dt_pastpeak_factor',
'dt_peak_factor',
'dt_pastpeak_min',
'dt_pastpeak_max',
'dt_search_min',
'find_critical_events',
'quiet',
):
if locals_[name] is not None:
setattr(self, name, locals_[name])
if self.dt_min is None: self.dt_min = self.dt_step
if self.dt_max is None: self.dt_max = 1000*self.dt_step
def run(self, verbose=False, **kwargs):
"""Do a run, return results"""
results = Results(self, args=kwargs)
while True:
x = self.find()
if x is None:
break
results.add(self)
# Printing if desired
if verbose:
tlow = x[0]
thigh = x[1]
dt = thigh-tlow
val = self.found_x_max
print(tlow, thigh, dt, val, len(self.old_es))
return results
# Two generalized set-making functions. These take an iterator
# over events, and return set objects. They are separate methods,
# so that we can have either unweighted or weighted sets, or even
# higher-level ideas. Returned objects should be able to do
# len(a|b), len(a&b), and a.union(b)
def _set_make(self, cursor):
if not self.weighted:
es = set(e for t, e, w in cursor)
else:
es = WeightedSet((e,w) for t, e, w in cursor)
return es
def _set_update(self, set_, cursor):
if not self.weighted:
set_.update(set(e for t,e,w in cursor))
else:
set_.update(set((e,w) for t,e,w in cursor))
# Interval-getting functions. These use the internal state
# `self.tstart` and the argument dt to return edge sets (made
# using _make_set).
def get_first(self, dt):
"""Get a first interval: return the two intervals.
This uses self.tstart to make to intervals using dt."""
#cursor = self.evs[self.tstart: self.tstart+dt]
#es1b = self._set_make(cursor)
# cached version
es1 = self.get_succesive(dt)
#assert es1 == es1b
cursor = self.evs[self.tstart+dt: self.tstart+2*dt]
es2 = self._set_make(cursor)
return es1, es2
def get_succesive(self, dt):
"""Get a successive interval.
self.old_es should be provided."""
es = None
if self.old_incremental_es is not None:
# We can try to use the old edge set to save recreating
# everything. We only add the new new edges between
# old_dt and dt.
old_tstart, old_dt, old_es = self.old_incremental_es
if old_tstart == self.tstart and old_dt < dt:
#print "using cache"
cursor = self.evs[self.tstart+old_dt : self.tstart+dt]
self._set_update(old_es, cursor)
es = old_es
if es is None:
# Recreate edge set from scratch
es = self.evs[self.tstart: self.tstart+dt]
es = self._set_make(es)
# Cache our results for the next interval
self.old_incremental_es = (self.tstart, dt, es)
return es
def get(self, dt):
"""Calls either get_first or get_succesive.
Also, for successive intervals, returns (old_es, next_es)."""
if self.old_es == None:
return self.get_first(dt)
else:
return self.old_es, self.get_succesive(dt)
# Different measurement functions: either Jaccard or NMI. (Note:
# NMI is old, was for graphs which is no longer supported).
def measure_jacc(self, es1s, es2s):
"""Jaccard similarity of event sets. Weighted or unweighted."""
union = (es1s | es2s).__len__()
if union == 0:
x = float('nan')
self._measure_data = (0, 0, es1s.__len__(), es2s.__len__())
else:
intersect = (es1s & es2s).__len__()
alpha = 1
x = intersect / float(alpha*(union-intersect) + intersect)
self._measure_data = (intersect, union, es1s.__len__(), es2s.__len__())
return x
def measure_nmi(self, es1s, es2s):
"""NMI similarity of event sets. Graphs only, *not* implemented now."""
g1 = nx.Graph(x for x in es1s)
g2 = nx.Graph(x for x in es2s)
g1lcc = g1.subgraph(nx.connected_components(g1)[0])
g2lcc = g2.subgraph(nx.connected_components(g2)[0])
cdargs = dict(verbosity=0)
c1 = algs.Louvain(g1lcc, **cdargs).cmtys
c2 = algs.Louvain(g2lcc, **cdargs).cmtys
#c1, c2 = pcd.cmtycmp.limit_to_overlap(c1, c2)
#nmi = pcd.cmtycmp.nmi(c1, c2)
nmi = pcd.cmtycmp.F1_python2(c1, c2)
return nmi
def measure_cosine(self, es1s, es2s):
"""Cosine similarity of event sets. Weighted sets only."""
dot = es1s.dot(es2s)
norm1 = es1s.norm()
norm2 = es2s.norm()
cossim = dot/(norm1*norm2)
self._measure_data = (dot, norm1, norm2, len(es1s), len(es2s))
return cossim
def measure_cosine_uw(self, es1s, es2s):
"""Cosine similarity of event sets, as unweighted sets."""
dot = len(es1s & es2s)
cossim = dot/sqrt(len(es1s) * len(es2s))
self._measure_data = (dot, len(es1s), len(es2s))
return cossim
def iter_all_dts_linear(self):
"""Get all dt values we will search."""
# Create our range of dts to test.
#all_dts = numpy.arange(self.dt_min, self.dt_max+self.dt_step,
# self.dt_step)
# sqlite3 can't handle numpy.int64, convert all to floats.
#all_dts = [ eval(repr(x)) for x in all_dts ]
#return all_dts
dt = self.dt_min
stop = self.dt_max+self.dt_step
step = self.dt_step
while True:
yield dt
if dt > stop: break
dt += step
#if self.tstart + dt > self.tstop: break # moved to find()
def iter_all_dts_log(self):
# Find the next event to set the log scale properly.
c = self.evs.conn.cursor()
c.execute('SELECT DISTINCT t FROM event WHERE t > ? '
'ORDER BY t ASC LIMIT 1', (self.tstart, ))
smallest_dt = c.fetchone()[0] - self.tstart
c.close()
# set this to minimum dt we scan. Should be a power of ten
# (10**(int)).
if self.log_dt_min is not None:
base_scale = self.log_dt_min
# Find a base scale smaller than our next event.
else:
base_scale = 10.**floor(log10(smallest_dt))
# Specifies how many digits we scan in the log thing.
# 0 = 1,2,3,..10,20,..100,200
#-1 = 1,2,..10,11...100,110,333
log_precision = 1
i = 1
while True:
dt = i * base_scale
yield dt
i += int(10**( max(0, int(log10(i))-log_precision) ))
#if self.log_dt_max and dt > self.log_dt_max: break
#if self.tstart + dt > self.tstop: break # moved to find()
def iter_all_dts_event(self):
"""Iterate dts that actually exist.
This is the most adaptive sampling method, but is bad when the
intrinsic time is much greater than the shortest event time.
In that case, set dtstep and use linear mode."""
tstart = self.tstart
stop = self.dt_max
c = self.evs.conn.cursor()
c.execute('SELECT DISTINCT t FROM event WHERE t >= ? '
'ORDER BY t ASC', (self.tstart, ))
for row in c:
yield row[0] - tstart
if row[0] - tstart > stop: break
iter_all_dts = iter_all_dts_linear
# Below, we have the --peakfinder related options. These relate
# to picking the maximum value out of the plateau, and
# implementing stop conditions.
class StopSearch(BaseException):
"""Relay a stop condition from the code to several layers up.
"""
def __init__(self, i_max, *args, **kwargs):
self.i_max = i_max
return super(self.__class__, self).__init__(*args, **kwargs)
def pick_best_dt_shortest(self, dt, dts, xs):
"""--peakfinder=shortest"""
i_max = numpy.argmax(xs)
dt_peak = dts[i_max]
dt_extra_ = max(self.dt_pastpeak_factor*dt_peak,
self.dt_pastpeak_factor*self.last_dt_max,
self.dt_pastpeak_min)
# If we have found a peak, and the value has now decreased to
# J_peak*dt_peak_factor, stop the search.
if (xs[-1] < xs[i_max]*self.dt_peak_factor
and len(dts) > 10
and dt>=self.dt_search_min):
raise self.StopSearch(i_max)
# If we have searched to (dt_peak + dt_extra_), then stop
# search. dt_extra_ is a dynamic quantity that encapsulates
# "a certain factor past the current peak."
if (len(dts) > 10
and dt > dt_peak + dt_extra_
and dt>=self.dt_search_min):
raise self.StopSearch(i_max)
# If we have gone beyond self.dt_pastpeak_max past the max,
# then unconditionally stop the search.
if self.dt_pastpeak_max and dt > dt_peak + self.dt_pastpeak_max:
raise self.StopSearch(i_max)
return i_max
def pick_best_dt_longest(self, dt, dts, xs):
"""--peakfinder=longest"""
xs_array_reversedview = numpy.asarray(xs)[::-1]
i_max_reversed = numpy.argmax(xs_array_reversedview)
i_max = len(xs) - 1 - i_max_reversed
dt_peak = dts[i_max]
dt_extra_ = max(self.dt_pastpeak_factor*dt_peak,
self.dt_pastpeak_factor*self.last_dt_max,
self.dt_pastpeak_min)
# If we have found a peak, and the value has now decreased to
# J_peak*dt_peak_factor, stop the search.
if (xs[-1] < xs[i_max]*self.dt_peak_factor
and len(dts) > 10
and dt>=self.dt_search_min):
raise self.StopSearch(i_max)
# If we have searched to (dt_peak + dt_extra_), then stop
# search. dt_extra_ is a dynamic quantity that encapsulates
# "a certain factor past the current peak."
if (len(dts) > 10
and dt > dt_peak + dt_extra_
and dt>=self.dt_search_min):
raise self.StopSearch(i_max)
# If we have gone beyond self.dt_pastpeak_max past the max,
# then unconditionally stop the search.
if self.dt_pastpeak_max and dt > dt_peak + self.dt_pastpeak_max:
raise self.StopSearch(i_max)
return i_max
def pick_best_dt_greedy(self, dt, dts, xs):
"""--peakfinder=greedy"""
# if we have decreased on the last step AND if we have
# exceeded dt_search_min.
if len(xs) > 2 and xs[-2] > xs[-1] and dt>self.dt_search_min:
raise self.StopSearch(len(xs)-2)
# break condition when exceeding total available time.
if self.tstart + dt > self.tstop:
raise self.StopSearch(len(xs)-1)
return len(xs) - 1
pick_best_dt = pick_best_dt_longest
# This is the core function that does a search.
def find(self):
"""Core snapshot finding method.
Returns (low, high) values. This method can be repeatedly
called to do a segmentation, but most real use actually calls
the main() function.
Internal documentation about how this method works:
iter_all_dts(): Gives dt values to check. Repeats indefinitely,
break happens in pick_best_dt().
- dt_min: starting dt (linear)
- dt_max: ending dt (linear)
- dt_step: step size (linear)
- log_dt_min: minimum log step size (log)
- (event: no parameters)
pick_best_dt(): picks the optimal dt value.
- Options are pick_best_dt_shortest, pick_best_dt_longest,
pick_best_dt_greedy. The following options are defined:
"""
# Our stop condition. Returning None is a sentinel to the
# caller stop the analysis.
if self.tstart >= self.tstop:
return None
# Do not do a search for the first interval but just select something.
if self.old_es is None and self.dt_first_override:
self.t_crit = False
self._finder_data = dict(xs=[], ts=[], dts=[],
measure_data=[])
es1s, es2s = self.get(self.dt_first_override)
x = self.measure(es1s, es2s)
self.found_x_max = x
old_tstart = self.tstart
self.interval_low = old_tstart
self.tstart = self.tstart + self.dt_first_override
self.interval_high = self.tstart
self.old_n_events = self.evs.count_interval(old_tstart, self.tstart)
if self.old_es is None:
self.old_es = es1s
return self.interval_low, self.interval_high
dts = [ ]
xs = [ ]
self._finder_data = dict(xs=[], ts=[], dts=[],
measure_data=[])
i_max = None
try:
break_next_round = False
for i, dt in enumerate(self.iter_all_dts()):
# Condition for breaking. This assumes that the dt values
# are monitonically increasing. If not, the iter_all_dts
# method needs to ensure that this condition is never
# fulfilled until after it is ready to stop.
# We need to break _after_ the loop, because we must test
# one value greater than the stopping point at least
# because the upper interval is open.
if break_next_round:
break
if self.tstart + dt > self.tstop:
break_next_round = True
continue
# Get our new event sets for old and new intervals.
es1s, es2s = self.get(dt)
# Skip if there are no events in either interval (we don't
# expect most measures to be defined in this case.)
if len(es1s) == 0 or len(es2s) == 0:
continue
x = self.measure(es1s, es2s)
dts.append(dt)
xs.append(x)
# Store data for later plotting.
self._finder_data['dts'].append(dt)
self._finder_data['ts'].append(self.tstart+dt)
self._finder_data['xs'].append(x)
self._finder_data['measure_data'].append(self._measure_data)
# Find best dt. This can raise self.StopSearch in order
# to terminate the search early (in that case it should
# set i_max as an attribute of the exception.
i_max = self.pick_best_dt(dt, dts, xs)
except self.StopSearch as e:
i_max = e.i_max
# We are completly done with the interval. break the loop.
if i_max is None:
return None
#assert xs[i_max] != xs[-1], "Plateaued value"
#assert i_max != len(xs)-1, "Continually increasing value"
# In the case below, we have a continually increasing jacc,
# which indicates that there was some extreme event. In this
# case, we restart completly.
self.t_crit = False
if ((.95*xs[i_max] <= xs[-1])
and self.old_es is not None
and (self.tstart+dts[-1] < self.tstop-.001
or approxeq(xs[len(xs)//2], xs[-1], .01))
and self.find_critical_events):
if not self.quiet:
print("### critical event detected at t=%s"%self.tstart)
# At this point, we have had an extreme event and we
# should restart from zero.
# Save some old values to use when recalculating things.
old_tstart = self.tstart
prev_es = self.old_es
# Remove old interval. We need a fresh restart as in the
# beginning of the calculation.
self.old_es = None
# Do the actual finding. Save return since that is the
# signature we need to return in the end.
ret_val = self.find()
self.t_crit = True
# The following things need to be re-calculated since the
# eself.find is comparing the two initial intervals, and
# not the previos interval and this interval. Note that
# not everything is being updated! So far, nly
# self.found_x_max is.
self.found_x_max = self.measure(prev_es, self.old_es)
#self.last_dt_max = self.tstart - old_tstart # not updated
return ret_val
#print xs
dt_max = self.found_dt_max = dts[i_max]
x_max = self.found_x_max = xs[i_max]
# best es2s and best self._measuer_data is overwritten in the
# loop above. Rerun the lines below to save this again.
es1s, es2s = self.get(dt_max)
self.measure(es1s, es2s) # rerun to store correct self._measure_data
self.last_dt_max = dt_max
# Clean up, save old edge set.
old_tstart = self.interval_low = self.tstart
self.tstart = old_tstart + dt_max
if self.old_es is None:
# first round, comparing two expanding intervals.
if self.merge_first:
# Merge the first two intervals into a big one.
self.old_es = es1s.union(es2s)
self.tstart += dt_max # double interval
self.interval_high = self.tstart
# Double dts, since our dt actually represents a
# doubled interval.
self._finder_data['dts'] = \
numpy.multiply(2, self._finder_data['dts'])
self._finder_data['ts'] = \
numpy.subtract(self._finder_data['ts'], old_tstart) \
* 2+old_tstart
if not self.quiet:
print('### merging first two intervals')
self.old_n_events = self.evs.count_interval(old_tstart, self.tstart)
return old_tstart, self.tstart
else:
# Old (normal) way of handling the first interval.
self.old_es = es1s
self.old_n_events = self.evs.count_interval(old_tstart, self.tstart)
self.interval_high = self.tstart
return old_tstart, self.tstart
else:
# Normal continuing process.
self.old_es = es2s
self.old_n_events = self.evs.count_interval(old_tstart, self.tstart)
self.interval_high = self.tstart
return old_tstart, self.tstart
def find_uniform(self):
"""Alternative version of find that returns uniform intervals.
This can not be set via an option, but must be set by manual
code so far:
dynsnap.SnapshotFinder.find = dynsnap.SnapshotFinder.find_uniform
dynsnap.SnapshotFinder.dt_uniform = dt_uniform
The only purpose is this method is to override *all* detection
and return something for testing.
"""
# Uniform initialization
self.t_crit = False
if self.tstart >= self.tstop:
return None
# Propagation
old_tstart = self.tstart
self._finder_data = dict(xs=[], ts=[], dts=[],
measure_data=[])
es1s, es2s = self.get(self.dt_uniform)
x = self.measure(es1s, es2s)
#self._finder_data['xs'].append(x)
#self._finder_data['ts'].append(self.tstart+self.dt_uniform)
#self._finder_data['dts'].append(self.dt_uniform)
#self._finder_data['measure_data'].append(self._measure_data)
self.found_x_max = x
self.interval_low = old_tstart
self.tstart = self.tstart + self.dt_uniform
self.interval_high = self.tstart
self.old_n_events = self.evs.count_interval(old_tstart, self.tstart)
if self.old_es is None:
self.old_es = es1s
else:
self.old_es = es2s
return self.interval_low, self.interval_high
class Results(object):
"""This holds results and provides various analysis methods."""
def __init__(self, finder, args=None):
self.finder = finder
self.args = args
self.tlows = [ ]
self.thighs = [ ]
self.sims = [ ]
self.finding_data = [ ]
self.n_distinct = [ ]
self.n_events = [ ]
self.t_crit = [ ]
def add(self, finder):
"""Record state, for used in plotting"""
tlow = finder.interval_low
thigh = finder.interval_high
self.tlows.append(tlow)
self.thighs.append(thigh)
self.sims.append(finder.found_x_max)
self.n_distinct.append(len(finder.old_es))
self.n_events.append(finder.old_n_events)
# The following things don't always need to be stored
self.finding_data.append((finder._finder_data['ts'],
finder._finder_data['xs']))
if finder.t_crit:
self.t_crit.append(finder.interval_low)
def plot_1(self, path, callback=None, **kwargs):
"""Plot of snapshot lengths and similarity.
Save to path.[pdf,png]."""
# If we have no data, don't do anything:
if len(self.tlows) == 0:
return
try:
import pcd.support.matplotlibutil as mplutil
raise ImportError
except ImportError:
import mplutil
fname = path + '.[pdf,png]'
fig, extra = mplutil.get_axes(fname, figsize=(10, 10),
ret_fig=True)
ax = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
ax.set_xlabel('time (also snapshots intervals)')
ax.set_ylabel('Snapshot length (t)')
ax.set_xlabel('time')
ax2.set_ylabel('Jaccard score (or measure)')
points = [ ]
for tlow, thigh in zip(self.tlows, self.thighs):
points.append((tlow, thigh-tlow))
points.append((thigh, thigh-tlow))
x, y = zip(*points)
ax.set_xlim(self.tlows[0], self.thighs[-1])
ax2.set_xlim(self.tlows[0], self.thighs[-1])
ls = ax.plot(x, y, '-o')
for tlow, thigh, (ts, xs) in zip(self.tlows, self.thighs, self.finding_data):
ls = ax2.plot(ts, xs, '-')
#ax.axvline(x=new_tstart, color=ls[0].get_color())
if self.args.get('annotate_peaks', False):
ax2.annotate(str(thigh), xy=(thigh, max(xs)))
if callback:
callback(locals())
mplutil.save_axes(fig, extra)
def plot_Jfinding(self, ax, convert_t=lambda t: t):
"""Plot the J finding at every interval"""
xlow, xhigh = ax.get_xlim()
for tlow, thigh, (ts, xs) in zip(self.tlows, self.thighs,
self.finding_data):
ls = ax.plot([convert_t(t) for t in ts], xs, '-')
#ax.axvline(x=new_tstart, color=ls[0].get_color())
if self.args.get('annotate_peaks', False):
ax.annotate(str(convert_t(thigh)), xy=(convert_t(thigh), max(xs)))
ax.set_xlim(xlow, xhigh)
return ls
def plot_density(self, ax, evs,
convert_t=lambda t: t,
style='-',
scale=1,
**kwargs):
"""Create local event density and plot it on an axis."""
# Calculate local event density for the plot
tlow = self.tlows[0]
thigh = self.thighs[-1]
data_size = thigh-tlow
interval = data_size/1000.
halfwidth = data_size/200
tlow = math.floor(tlow/interval)*interval
thigh = math.ceil(thigh/interval)*interval
domain = numpy.arange(tlow, thigh, interval)
#domain, densities = evs.event_density(domain=domain, halfwidth=halfwidth)
domain, densities = evs.event_density(domain=domain, halfwidth=None,
high=0, low=2*halfwidth)
domain = [convert_t(x) for x in domain]
densities = numpy.asarray(densities, dtype=float)
densities = numpy.divide(densities, halfwidth*2)
#print sum(densities[numpy.isnan(densities)==False])
ls = ax.plot(domain, densities*scale, '-', color='#3F1F9E', zorder=0)
return ls
def plot_similarities(self, ax,
convert_t=lambda t: t,
style='g-'):
# Plot similarities
ts = self.tlows[:1] + self.thighs[:-1]
sims = self.sims
ls = ax.plot([convert_t(t) for t in ts], sims, style, color='#006F07')
return ls
def plot_intervals(self, ax,
convert_t=lambda t: t,
style='g-'):
ts = self.thighs
# Plot vertical lines for intervals
lines = [ ]
for thigh in ts:
ls = ax.axvline(x=convert_t(thigh), color=(.21,.21,.21), linewidth=.5)
lines.append(ls)
return lines
def plot_intervals_patches(self, ax,
convert_t=lambda t: t,
shift=0,
ylow=0,
yhigh=1):
"""Plot detected intervals as patches."""
import matplotlib.patches
import matplotlib.dates as mdates
if isinstance(convert_t(0), datetime.datetime):
def calc_width(high, low):
low = convert_t(low)
high = convert_t(high)
return mdates.date2num(high) - mdates.date2num(low)
else:
def calc_width(high, low):
return high-low
#ylow, yhigh = ax.get_xlim()
ts = self.thighs
if len(ts)%2 == 1: # if odd
ts = [self.tlows[0]] + ts
# Plot vertical lines for intervals
patches = [ ]
for tA, tB in zip(ts[0::2], ts[1::2]):
#p = ax.add_patch(matplotlib.patches.Rectangle(
# (convert_t(tA+shift), ylow),
# calc_width(tB, tA),
# yhigh-ylow,
# facecolor=(.81,.81,.81),
# edgecolor=(.51,.51,.51),
# zorder=-10))
p = ax.axvspan(convert_t(tA+shift), convert_t(tB+shift),
facecolor=(.81,.81,.81),
edgecolor=(.51,.51,.51),
zorder=-10)
patches.append(p)
return patches
def plot_actual(self, ax,
convert_t=lambda t: t,
style='o', color='red'):
for e, label in major_events:
#ax.axvline(x=e, color='red')
ax.plot([convert_t(e)], 1, 'o', color='red')
def plot_2(self, path, callback=None, evs=None, convert_t=lambda t: t, **kwargs):
"""Plot of similarity scores and local event density.
Save to path.[pdf,png]."""
if len(self.tlows) == 0:
return
try:
import pcd.support.matplotlibutil as mplutil
raise ImportError
except ImportError:
import mplutil
fname = path + '.[pdf,png]'
fig, extra = mplutil.get_axes(fname, figsize=(10, 5),
ret_fig=True)
ax = fig.add_subplot(1, 1, 1)
ax2 = ax.twinx()
ax.set_xlabel('time')
ax.set_ylabel('Local event density')
ax2.set_ylabel('Similarity score')
ax.set_xlim(convert_t(self.tlows[0]), convert_t(self.thighs[-1]))
#ax2.set_xlim(self.tlows[0], self.thighs[-1])
# Calculate local event density for the plot
tlow = self.tlows[0]
thigh = self.thighs[-1]
data_size = thigh-tlow
interval = data_size/1000.
halfwidth = data_size/100.
tlow = math.floor(tlow/interval)*interval
thigh = math.ceil(thigh/interval)*interval
domain = numpy.arange(tlow, thigh, interval)
domain, densities = evs.event_density(domain=domain, halfwidth=halfwidth)
densities = numpy.asarray(densities, dtype=float)
densities = numpy.divide(densities, halfwidth*2)
#print sum(densities[numpy.isnan(densities)==False])
# Transform domain into human-readable times, if wanted.
domain = [convert_t(t) for t in domain ]
# Plot local density.
ls = ax.plot(domain, densities, '-')
ax.set_yscale("log")
adf = ax.xaxis.get_major_formatter()
if hasattr(adf, 'scaled'):
adf.scaled[1./(24*60)] = '%H:%M' # set the < 1d scale to H:M
adf.scaled[1./24] = '%H:%M' # set the < 1d scale to H:M
adf.scaled[1.0] = '%m-%d' # set the > 1d < 1m scale to Y-m-d
adf.scaled[30.] = '%Y-%m' # set the > 1m < 1Y scale to Y-m
adf.scaled[365.] = '%Y' # set the > 1y scale to Y
# Plot similarities
ts = self.thighs
sims = self.sims
ls = ax2.plot([convert_t(t) for t in ts], sims, 'g-')
# Plot vertical lines for intervals
for thigh in ts:
ax.axvline(x=convert_t(thigh), color='k', linewidth=.5)
if callback:
callback(locals())
mplutil.save_axes(fig, extra)
def tf_idf(self, evs, n=10):
"""TF-IDF analysis of intervals.
This functions calculates the term frequency-inverse document
frequency of events within intervals. This allows one to see
the most characteristic events within each interval.
Term frequency: fraction of term weights each term occupies
within an interval. This does consider weights always.
Inverse document frequency: -log10(n/N), N=total intervals,
n=intervals containing term. Events occuring in all intervals
are excluded from analysis. This does not consider weights.
The returned value is the product of both of these. The top
10 terms are returned.
"""
# calculate document frequency for each term.
dfs = collections.defaultdict(int)
for tlow, thigh in zip(self.tlows, self.thighs):
c = evs._execute("""SELECT DISTINCT e from %s WHERE ?<=t AND t<?"""%evs.table,
(tlow, thigh))
for (e, ) in c:
dfs[e] += 1
# make the logarithmic DF
for e in dfs:
#if dfs[e] == float(len(self.tlows)):
# print dfs[e], float(len(self.tlows)), -log10(dfs[e]/float(len(self.tlows))), \
# evs.get_event_names(e)
dfs[e] = -log10(dfs[e]/float(len(self.tlows)))
dfs = dict(dfs)
# For each interval, compute TFIDF
return_data = [ ]
for tlow, thigh in zip(self.tlows, self.thighs):
total_terms = evs._execute("""SELECT sum(w) from %s WHERE ?<=t AND t<? """%evs.table,
(tlow, thigh)).fetchone()[0]
c = evs._execute("""SELECT e, sum(w)/? from %s WHERE ?<=t AND t<? GROUP BY e"""%evs.table,
(total_terms, tlow, thigh))
tfs = dict(c.fetchall())
items = [ ]
mostcommon = heapq.nlargest(10,
((tf*dfs[e], e) for e, tf in iteritems(tfs) if dfs[e]!=0),
key=lambda x: x[0])
if mostcommon:
names = evs.get_event_names(tuple(zip(*mostcommon))[1])
else:
names = [ ]
#print tlow, thigh
#for (tfidf, e), name in zip(mostcommon, names):
# print " %5.2f %d %s"%(tfidf, tfs[e], name)
return_data.append((tfidf, name) for (tfidf, e), name in zip(mostcommon, names))
return return_data
def entropy(self, evs):
"""Calculate entropies in detected intervals. Return entropies."""
all_S = [ ]
for tlow, thigh in zip(self.tlows, self.thighs):
c = evs._execute("""SELECT count(*) from %s WHERE ?<=t AND t<? GROUP BY e"""%evs.table,
(tlow, thigh))
# Could use scipy.stats.entropy, but not depending on
# scipy yet.
counts = [x for (x,) in c ]
total = float(sum(counts))
S = -sum(p/total*log(p/total) for p in counts if p>0 ) / log(2)
all_S.append(S)
return all_S