~bertrand-nouvel/pycvf-indexes/trunk

« back to all changes in this revision

Viewing changes to tests/test_index.py

  • Committer: tranx
  • Date: 2010-10-01 16:51:43 UTC
  • Revision ID: tranx@havane-20101001165143-wkahluadtbueyqsu
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
#########################################################################################################################################
 
4
# 2009 CNRS Postdoctorate JFLI
 
5
#
 
6
# (c) All rights reserved
 
7
# ###############################################
 
8
#
 
9
#########################################################################################################################################
 
10
# Import required objects
 
11
#########################################################################################################################################
 
12
 
 
13
import time, numpy
 
14
from pycvf.core.errors import *
 
15
from pycvf.core.generic_application import *
 
16
from pycvf.core.utilities import with_progressbar
 
17
 
 
18
class DbIdxTester(IndexUsingApplication):
 
19
  class ProgramMetadata(object):
 
20
      name="Index Implementation Tester"
 
21
      version="1.0"
 
22
      author="Bertrand Nouvel bertrand.nouvel@gmail.com"
 
23
      copyright="        COPYRIGHT Bertrand Nouvel - JFLI - CNRS 2010"
 
24
      license="GPLv3"
 
25
 
 
26
        
 
27
 
 
28
  key=CmdLineString(None,"key","modelpath","specified within the model what should be used as key when indexing","0")
 
29
  value=CmdLineString(None,"value","modelpath","specified within the model what should be used as value when indexing","@")
 
30
 
 
31
  @classmethod
 
32
  def process(cls,*args, **kwargs):
 
33
      keyval=cls.mdl.resolve_modelpath("0")
 
34
      valueval=cls.mdl.resolve_modelpath("@")
 
35
      #cls.vdb=
 
36
      for e in cls.vdb:
 
37
          keys=cls.mdl.process_path([e[0]],[e[1]],keyval,lambda x:x)
 
38
          values=cls.mdl.process_path([e[0]],[e[1]],valueval,lambda x:x)
 
39
          for c in zip(keys,values):
 
40
               cls.idx.add(c[0],c[1])
 
41
      try:
 
42
        cls.idx.recompute()
 
43
      except:
 
44
        pass
 
45
 
 
46
      idb=iter(cls.vdb)
 
47
      try:
 
48
        while True:
 
49
          nq=3
 
50
          nr=5
 
51
          e=[ idb.next() for i in range(nq) ]
 
52
          keys=cls.mdl.process_path(map(lambda f:f[0],e),map(lambda f:f[1],e),keyval,lambda x:x)
 
53
          keys=numpy.array(keys)
 
54
          l=cls.idx.getitems(keys,nr)
 
55
          if (type(l)==numpy.ndarray):
 
56
            l=l.tolist()
 
57
          print type(l),type(l[0]),type(l[0][0]), type(l[0][0][0]),type(l[0][0][1])
 
58
          print len(l),len(l[0])
 
59
          #print l
 
60
          assert (type(l)==list)      # queries
 
61
          assert (len(l)==nq)
 
62
          assert (type(l[0])==list)   # results
 
63
          assert (len(l[0])<=nr)
 
64
          assert (len(l[0][0])==2)
 
65
          #assert (type(l[0][0])==tuple)           
 
66
          xxx=map (lambda y:map(lambda x:(cls.vdb[x[0]],x[1]),y),l)
 
67
          #xxx=map (lambda y:map(lambda x:(cls.vdb[x[0][:]],x[1]),y),l)
 
68
          print xxx
 
69
      except StopIteration:
 
70
        print "OK"
 
71
  
 
72
if __name__=="__main__":       
 
73
  DbIdxTester.run(sys.argv[1:])
 
74