Changeset 10 for trunk/lib/nanownlib/storage.py
- Timestamp:
- 07/13/15 19:16:30 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/nanownlib/storage.py
r9 r10 4 4 import os 5 5 import uuid 6 import random 6 7 import threading 7 8 import sqlite3 8 9 9 10 import numpy 11 # Don't trust numpy's seeding 12 numpy.random.seed(random.SystemRandom().randint(0,2**32-1)) 10 13 11 14 def _newid(): … … 18 21 _population_sizes = None 19 22 _population_cache = None 23 _offset_cache = None 24 _cur_offsets = None 20 25 21 26 def __init__(self, path): … … 26 31 self._population_sizes = {} 27 32 self._population_cache = {} 33 self._offset_cache = {} 34 self._cur_offsets = {} 28 35 29 36 if not exists: … … 79 86 self.conn.execute( 80 87 """CREATE TABLE classifier_results (id BLOB PRIMARY KEY, 81 algorithm TEXT, 88 classifier TEXT, 89 trial_type TEXT, 90 num_observations INTEGER, 91 num_trials INTEGER, 82 92 params TEXT, 83 sample_size INTEGER,84 num_trials INTEGER,85 trial_type TEXT,86 93 false_positives REAL, 87 94 false_negatives REAL) … … 109 116 110 117 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: 118 cache_key = (probe_type,unusual_case,field) 119 120 if cache_key not in self._population_cache: 112 121 query=""" 113 122 SELECT %(field)s AS unusual_case, … … 121 130 cursor = self.conn.cursor() 122 131 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)] 132 p = [dict(row) for row in cursor.fetchall()] 133 self._population_cache[cache_key] = p 134 self._offset_cache[cache_key] = tuple(numpy.random.random_integers(0,len(p)-1, len(p)/5)) 135 self._cur_offsets[cache_key] = 0 136 137 population = self._population_cache[cache_key] 126 138 127 139 if size == None or size > len(population): 128 140 size = len(population) 129 141 if offset == None or offset >= len(population) or offset < 0: 130 offset = numpy.random.random_integers(0,len(population)-1) 131 142 offset = self._offset_cache[cache_key][self._cur_offsets[cache_key]] 143 self._cur_offsets[cache_key] = (offset + 1) % len(self._offset_cache[cache_key]) 144 132 145 try: 133 ret_val = population[offset:offset+size] 146 offset = int(offset) 147 size = int(size) 134 148 except Exception as e: 135 149 print(e, offset, size) 136 150 return None 151 152 ret_val = population[offset:offset+size] 137 153 if len(ret_val) < size: 138 154 ret_val += population[0:size-len(ret_val)] 139 155 140 156 return ret_val 141 142 157 158 159 def resetOffsets(self): 160 for k in self._cur_offsets.keys(): 161 self._cur_offsets[k] = 0 162 163 143 164 def clearCache(self): 144 165 self._population_cache = {} 166 self._offset_cache = {} 167 self._cur_offsets = {} 145 168 146 169 … … 188 211 self.conn.commit() 189 212 return ret_val 213 214 def fetchClassifierResult(self, classifier, trial_type, num_observations): 215 query = """ 216 SELECT * FROM classifier_results 217 WHERE classifier=? AND trial_type=? AND num_observations=? 218 ORDER BY false_positives+false_negatives 219 LIMIT 1; 220 """ 221 cursor = self.conn.cursor() 222 cursor.execute(query, (classifier, trial_type, num_observations)) 223 ret_val = cursor.fetchone() 224 225 if ret_val != None: 226 ret_val = dict(ret_val) 227 return ret_val 228 229 def deleteClassifierResults(self, classifier, trial_type, num_observations=None): 230 params = {"classifier":classifier,"trial_type":trial_type,"num_observations":num_observations} 231 query = """ 232 DELETE FROM classifier_results 233 WHERE classifier=:classifier AND trial_type=:trial_type 234 """ 235 if num_observations != None: 236 query += " AND num_observations=:num_observations" 237 238 self.conn.execute(query, params) 239 self.conn.commit() 240
Note: See TracChangeset
for help on using the changeset viewer.