-
Notifications
You must be signed in to change notification settings - Fork 0
/
annindex.py
123 lines (89 loc) · 3.96 KB
/
annindex.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
# Copyright 2014 NeuroData (http://neurodata.io)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#RBTODO make all get/put Index based on timerange
import numpy as np
import cStringIO
import blosc
import logging
logger=logging.getLogger("neurodata")
class AnnotateIndex:
def __init__(self,kvio,proj):
"""Give an active connection. This puts all index operations in the same transation as the calling db."""
self.proj = proj
self.kvio = kvio
# KLTODO legacy inteface
if False and self.proj.kvengine == 'MySQL':
self.NPZ = True
else:
self.NPZ = False
def getIndex ( self, ch, entityid, timestamp, resolution, update=False):
"""Retrieve the index for the annotation with id"""
idxstr = self.kvio.getIndex(ch, entityid, timestamp, resolution, update)
if idxstr:
if self.NPZ:
fobj = cStringIO.StringIO ( idxstr )
return np.load ( fobj )
else:
return blosc.unpack_array(idxstr)
else:
return []
def putIndex ( self, ch, entityid, timestamp, resolution, index, update=False ):
"""Write the index for the annotation with id"""
if self.NPZ:
fileobj = cStringIO.StringIO ()
np.save ( fileobj, index )
self.kvio.putIndex(ch, entityid, timestamp, resolution, fileobj.getvalue(), update )
else:
self.kvio.putIndex(ch, entityid, timestamp, resolution, blosc.pack_array(index), update)
def updateIndexDense(self, ch, index, timestamp, resolution ):
"""Updated the database index table with the input index hash table"""
for key, value in index.iteritems():
cubelist = list(value)
cubeindex = np.array(cubelist, dtype=np.uint64)
curindex = self.getIndex(ch, key, timestamp, resolution, True)
if curindex == []:
self.putIndex(ch, key, timestamp, resolution, cubeindex, False )
else:
# Update index to the union of the currentIndex and the updated index
newIndex = np.union1d(curindex, cubeindex)
self.putIndex(ch, key, timestamp, resolution, newIndex, True)
def deleteIndexResolution ( self, ch, annid, res ):
"""delete the index for a given annid at the given resolution"""
# delete Index table for each resolution
self.kvio.deleteIndex(ch, annid, res)
def deleteIndex ( self, ch, annid, resolutions ):
"""delete the index for a given annid"""
#delete Index table for each resolution
for res in resolutions:
self.kvio.deleteIndex(ch,annid,res)
def updateIndex ( self, ch, entityid, index, timestamp, resolution ):
"""Updated the database index table with the input index hash table"""
curindex = self.getIndex(ch, entityid, resolution, True)
if curindex == []:
if self.NPZ:
fileobj = cStringIO.StringIO ()
np.save ( fileobj, index )
self.kvio.putIndex(ch, entityid, timestamp, resolution, fileobj.getvalue())
else:
self.kvio.putIndex(ch, entityid, timestamp, resolution, blosc.pack_array(index))
else:
# Update Index to the union of the currentIndex and the updated index
newIndex = np.union1d(curindex, index)
# Update the index in the database
if self.NPZ:
fileobj = cStringIO.StringIO ()
np.save ( fileobj, newIndex )
self.kvio.putIndex(ch, entityid, timestamp, resolution, fileobj.getvalue(), True )
else:
self.kvio.putIndex(ch, entityid, timestamp, resolution, blosc.pack_array(newIndex), True )