Changeset 8 for trunk/lib


Ignore:
Timestamp:
07/09/15 19:01:23 (9 years ago)
Author:
tim
Message:

.

Location:
trunk/lib/nanownlib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/nanownlib/stats.py

    r7 r8  
    311311    ret_val = []
    312312    for t in range(num_trials):
    313         ret_val.append(estimator(subseries(db, probe_type, unusual_case, subseries_size)))
     313        ret_val.append(estimator(db.subseries(probe_type, unusual_case, subseries_size)))
    314314
    315315    return ret_val
     
    344344#         0 if it isn't unusual
    345345#        -1 if it is unusual in the wrong direction
    346 def multiBoxTest(params, unusual_case, greater, samples):
    347     #XXX: packet_rtt field from params
    348     dists = samples2Distributions(samples, 'packet_rtt')
    349    
    350     uc = dists[unusual_case]
    351     rest = []
    352     for tc,d in dists.items():
    353         if tc != unusual_case:
    354             rest.extend(d)
    355 
     346def multiBoxTest(params, greater, samples):
     347    uc = [s['unusual_case'] for s in samples]
     348    rest = [s['other_cases'] for s in samples]
     349   
    356350    uc_high = numpy.percentile(uc, params['high'])
    357351    rest_low = numpy.percentile(rest, params['low'])
     
    379373
    380374    mh = midhinge(diffs, params['distance'])
     375    #mh = trimean(diffs, params['distance'])
    381376    if greater:
    382377        if mh > params['threshold']:
  • trunk/lib/nanownlib/storage.py

    r7 r8  
    66import threading
    77import sqlite3
     8
     9import numpy
    810
    911def _newid():
     
    1517    cursor = None
    1618    _population_sizes = None
     19    _population_cache = None
    1720   
    1821    def __init__(self, path):
     
    2225        self.conn.row_factory = sqlite3.Row
    2326        self._population_sizes = {}
     27        self._population_cache = {}
    2428       
    2529        if not exists:
     
    8993            self.conn.close()
    9094
     95   
    9196    def populationSize(self, probe_type):
    9297        if probe_type in self._population_sizes:
     
    101106            print(e)
    102107            return 0
     108
     109
     110    def subseries(self, probe_type, unusual_case, size=None, offset=None, field='packet_rtt'):
     111        if (probe_type,unusual_case,field) not in self._population_cache:
     112            query="""
     113            SELECT %(field)s AS unusual_case,
     114                   (SELECT avg(%(field)s) FROM probes,analysis
     115                    WHERE analysis.probe_id=probes.id AND probes.test_case!=:unusual_case AND probes.type=:probe_type AND sample=u.sample) AS other_cases
     116            FROM   (SELECT probes.sample,%(field)s FROM probes,analysis
     117                    WHERE analysis.probe_id=probes.id AND probes.test_case =:unusual_case AND probes.type=:probe_type) u
     118            """ % {"field":field}
     119   
     120            params = {"probe_type":probe_type, "unusual_case":unusual_case}
     121            cursor = self.conn.cursor()
     122            cursor.execute(query, params)
     123            self._population_cache[(probe_type,unusual_case,field)] = [dict(row) for row in cursor.fetchall()]
     124
     125        population = self._population_cache[(probe_type,unusual_case,field)]
     126
     127        if size == None or size > len(population):
     128            size = len(population)
     129        if offset == None or offset >= len(population) or offset < 0:
     130            offset = numpy.random.random_integers(0,len(population)-1)
     131
     132        ret_val = population[offset:offset+size]
     133        if len(ret_val) < size:
     134            ret_val += population[0:size-len(ret_val)]
     135       
     136        return ret_val
     137
     138   
     139    def clearCache(self):
     140        self._population_cache = {}
     141
    103142       
    104143    def _insert(self, table, row):
Note: See TracChangeset for help on using the changeset viewer.