Mercurial > hg > audiodb
comparison bindings/python/pyadb.py @ 717:159becb0701e
the access level wrapper now exposes the python native data insert.
there's a nasty segfault to track though...
author | map01bf |
---|---|
date | Wed, 23 Jun 2010 17:52:30 +0000 |
parents | c014e4d5b45d |
children | e3f1cf653c30 |
comparison
equal
deleted
inserted
replaced
716:26a19beb7e3d | 717:159becb0701e |
---|---|
14 import sys | 14 import sys |
15 import os, os.path | 15 import os, os.path |
16 import unittest | 16 import unittest |
17 import _pyadb | 17 import _pyadb |
18 | 18 |
19 ADB_HEADER_FLAG_L2NORM = 0x1#annoyingly I can't find a means | 19 ADB_HEADER_FLAG_L2NORM = 0x1 #annoyingly I can't find a means |
20 ADB_HEADER_FLAG_POWER = 0x4#around defining these flag definitions | 20 ADB_HEADER_FLAG_POWER = 0x4 #around defining these flag definitions |
21 ADB_HEADER_FLAG_TIMES = 0x20#as they aren't even exported to the | 21 ADB_HEADER_FLAG_TIMES = 0x20 #as they aren't even exported to the |
22 ADB_HEADER_FLAG_REFERENCES = 0x40#api, so this is the only way to get them. | 22 ADB_HEADER_FLAG_REFERENCES = 0x40 #api, so this is the only way to get them. |
23 | 23 |
24 class Usage(Exception): | 24 class Usage(Exception): |
25 """error to indicate that a method has been called with incorrect args""" | 25 """error to indicate that a method has been called with incorrect args""" |
26 def __init__(self, msg): | 26 def __init__(self, msg): |
27 self.msg = msg | 27 self.msg = msg |
43 self._db = _pyadb._pyadb_create(path,0,0,0) | 43 self._db = _pyadb._pyadb_create(path,0,0,0) |
44 self._updateDBAttributes() | 44 self._updateDBAttributes() |
45 return | 45 return |
46 | 46 |
47 def insert(self, featFile=None, powerFile=None, timesFile=None, featData=None, powerData=None, timesData=None, key=None): | 47 def insert(self, featFile=None, powerFile=None, timesFile=None, featData=None, powerData=None, timesData=None, key=None): |
48 """Insert features into database. Can be done with data provided directly or by giving a path to a binary fftExtract style feature file. If power and/or timing is engaged in the database header, it must be provided (via the same means as the feature) or a Usage exception will be raised. Power files should be of the same binary type as features. Times files should be the ascii number length of time in seconds from the begining of the file to segment start, one per line.\n---Note that direct data insertion is not yet implemented.---""" | 48 """ |
49 Insert features into database. Can be done with data provided directly or by giving a path to a binary fftExtract style feature file. If power and/or timing is engaged in the database header, it must be provided (via the same means as the feature) or a Usage exception will be raised. Power files should be of the same binary type as features. Times files should be the ascii number length of time in seconds from the begining of the file to segment start, one per line. | |
50 If providing data directly, featData should be a numpy array with shape= (number of Dimensions, number of Vectors) | |
51 """ | |
49 #While python style normally advocates leaping before looking, these check are nessecary as | 52 #While python style normally advocates leaping before looking, these check are nessecary as |
50 #it is very difficult to assertain why the insertion failed once it has been called. | 53 #it is very difficult to assertain why the insertion failed once it has been called. |
51 if (self.hasPower and (((featFile) and powerFile==None) or ((featData) and powerData==None))): | 54 if (self.hasPower and (((featFile) and powerFile==None) or ((featData) and powerData==None))): |
52 raise(Usage, "The db you are attempting an insert on (%s) expects power and you either\ | 55 raise(Usage, "The db you are attempting an insert on (%s) expects power and you either\ |
53 haven't provided any or have done so in the wrong format."%self.path) | 56 haven't provided any or have done so in the wrong format."%self.path) |
55 raise(Usage, "The db you are attempting an insert on (%s) expects times and you either\ | 58 raise(Usage, "The db you are attempting an insert on (%s) expects times and you either\ |
56 haven't provided any or have done so in the wrong format."%self.path) | 59 haven't provided any or have done so in the wrong format."%self.path) |
57 args = {"db":self._db} | 60 args = {"db":self._db} |
58 if featFile: | 61 if featFile: |
59 args["features"] = featFile | 62 args["features"] = featFile |
60 elif featData: | 63 elif (featData != None): |
61 args["features"] = featData | 64 args["features"] = featData |
62 else: | 65 else: |
63 raise(Usage, "Must provide some feature data!") | 66 raise(Usage, "Must provide some feature data!") |
64 if self.hasPower: | 67 if self.hasPower: |
65 if featFile: | 68 if featFile: |
73 pass | 76 pass |
74 if key: | 77 if key: |
75 args["key"]=str(key) | 78 args["key"]=str(key) |
76 if featFile: | 79 if featFile: |
77 if not _pyadb._pyadb_insertFromFile(**args): | 80 if not _pyadb._pyadb_insertFromFile(**args): |
78 raise(RuntimeError, "Insertion failed for an unknown reason.") | 81 raise(RuntimeError, "Insertion from file failed for an unknown reason.") |
79 else: | 82 else: |
80 self._updateDBAttributes() | 83 self._updateDBAttributes() |
81 return | 84 return |
82 elif featData: | 85 elif (featData != None): |
83 raise(NotImplementedError, "direct data insertion not yet implemented") | 86 if (len(args["features"].shape) == 1) : args["features"] = args["features"].reshape((args["features"].shape[0],1)) |
87 args["nDim"], args["nVect"] = args["features"].shape | |
88 args["features"] = args["features"].flatten() | |
89 print "args: " + str(args) | |
90 if not _pyadb._pyadb_insertFromArray(**args): | |
91 raise(RuntimeError, "Direct data insertion failed for an unknown reason.") | |
92 else: | |
93 self._updateDBAttributes() | |
94 return | |
84 | 95 |
85 def configCheck(self, scrub=False): | 96 def configCheck(self, scrub=False): |
86 """examine self.configQuery dict. For each key encouters confirm it is in the validConfigTerms list and if appropriate, type check. If scrub is False, leave unexpected keys and values alone and return False, if scrub try to correct errors (attempt type casts and remove unexpected entries) and continue. If self.configQuery only contains expected keys with correctly typed values, return True. See Pyadb.validConfigTerms for allowed keys and types. Note also that include/exclude key lists memebers or string switched are not verified here, but rather when they are converted to const char * in the C api call and if malformed, an error will be rasied from there. Valid keys and values in queryconfig: | 97 """examine self.configQuery dict. For each key encouters confirm it is in the validConfigTerms list and if appropriate, type check. If scrub is False, leave unexpected keys and values alone and return False, if scrub try to correct errors (attempt type casts and remove unexpected entries) and continue. If self.configQuery only contains expected keys with correctly typed values, return True. See Pyadb.validConfigTerms for allowed keys and types. Note also that include/exclude key lists memebers or string switched are not verified here, but rather when they are converted to const char * in the C api call and if malformed, an error will be rasied from there. Valid keys and values in queryconfig: |
87 {seqLength : Int Sequence Length, \n\ | 98 {seqLength : Int Sequence Length, \n\ |
88 seqStart : Int offset from start for key, \n\ | 99 seqStart : Int offset from start for key, \n\ |