def __init__(self, filename): self._dbName = filename db = SqliteDb() db._createConnection(filename, 1000) # Tables should be at pairs: # PREFIX_Classes # PREFIX_Objects # where PREFIX can be empty self.tablePrefixes = OrderedDict() tables = db.getTables() for t in tables: if t.endswith('Classes'): prefix = t.replace('Classes', '') to = prefix + 'Objects' if to not in tables: raise Exception('SqliteDataSet: table "%s" found, but not "%s"' % (t, to)) flatDb = SqliteFlatDb(filename, tablePrefix=prefix) tableName = prefix + self._getPlural(flatDb.getSelfClassName()) self.tablePrefixes[tableName] = prefix #tablePrefixes.append(prefix) DataSet.__init__(self, self.tablePrefixes.keys()) db.close()
def _loadTable(self, tableName): """ Load information from tables PREFIX_Classes, PREFIX_Objects. """ tableName = self.tablePrefixes[tableName] BASIC_COLUMNS = [ Column('id', int, renderType=COL_RENDER_ID), Column('enabled', bool, renderType=COL_RENDER_CHECKBOX), Column('label', str), Column('comment', str), Column('creation', str) ] # Load columns from PREFIX_Classes table columns = list(BASIC_COLUMNS) db = SqliteDb() db._createConnection(self._dbName, 1000) db.executeCommand("SELECT * FROM %sClasses;" % tableName) # This will store the images columsn to join # the _index and the _filename imgCols = {} for row in db._iterResults(): renderType = COL_RENDER_NONE colName = row['column_name'] colLabel = row['label_property'] if colLabel != 'self': # Keep track of _index and _filename pairs to mark as renderable images if colLabel.endswith('_index'): imgCols[colLabel.replace('_index', '')] = colName elif colLabel.endswith('_filename'): # TODO: Maybe not all the labels endswith "_filename" # have to be rendered. # for example in the RotSpectra with '_representative._filename' prefix = colLabel.replace('_filename', '') if prefix in imgCols: renderType = COL_RENDER_IMAGE imgCols[colName] = imgCols[prefix] #CTF FIX elif (colLabel.endswith('_psdFile') or colLabel.endswith('_enhanced_psd') or colLabel.endswith('_ctfmodel_quadrant') or colLabel.endswith('_ctfmodel_halfplane')): renderType = COL_RENDER_IMAGE if row['class_name'] == 'Boolean': renderType = COL_RENDER_CHECKBOX columns.append( Column(colName, str, label=colLabel, renderType=renderType)) table = Table(*columns) checkedImgCols = {} # Check if the image columns are volumes ih = ImageHandler() # Populate the table in the DataSet db.executeCommand("SELECT * FROM %sObjects;" % tableName) for row in db._iterResults(): rowDict = dict(row) for k, v in rowDict.iteritems(): if v is None: rowDict[k] = '' # Set the index@filename for images columns values if k in imgCols: colName = imgCols[k] index = rowDict[colName] filename = os.path.join(self.projectPath, rowDict[k]) filepath = filename.replace(":mrc", "") if not checkedImgCols.get(colName, False): if os.path.exists(filepath): #print "Fn to get dims: %s@%s" % (index,filename) x, y, z, n = ih.getDimensions((index, filename)) if z > 1: table.getColumn(k).setRenderType( COL_RENDER_VOLUME) checkedImgCols[colName] = True if index: rowDict[k] = '%06d@%s' % (index, filename) table.addRow(row['id'], **rowDict) return table
def _loadTable(self, tableName): """ Load information from tables PREFIX_Classes, PREFIX_Objects. """ tableName = self.tablePrefixes[tableName] BASIC_COLUMNS = [Column('id', int, renderType=COL_RENDER_ID), Column('enabled', bool ,renderType=COL_RENDER_CHECKBOX), Column('label', str), Column('comment', str), Column('creation', str)] # Load columns from PREFIX_Classes table columns = list(BASIC_COLUMNS) db = SqliteDb() db._createConnection(self._dbName, 1000) db.executeCommand("SELECT * FROM %sClasses;" % tableName) # This will store the images columsn to join # the _index and the _filename imgCols = {} for row in db._iterResults(): renderType = COL_RENDER_NONE colName = row['column_name'] colLabel = row['label_property'] if colLabel != 'self': # Keep track of _index and _filename pairs to mark as renderable images if colLabel.endswith('_index'): imgCols[colLabel.replace('_index', '')] = colName elif colLabel.endswith('_filename'): # TODO: Maybe not all the labels endswith "_filename" # have to be rendered. # for example in the RotSpectra with '_representative._filename' prefix = colLabel.replace('_filename', '') if prefix in imgCols: renderType = COL_RENDER_IMAGE imgCols[colName] = imgCols[prefix] #CTF FIX elif (colLabel.endswith('_psdFile') or colLabel.endswith('_enhanced_psd') or colLabel.endswith('_ctfmodel_quadrant') or colLabel.endswith('_ctfmodel_halfplane')): renderType = COL_RENDER_IMAGE if row['class_name'] == 'Boolean': renderType = COL_RENDER_CHECKBOX columns.append(Column(colName, str, label=colLabel, renderType=renderType)) table = Table(*columns) checkedImgCols = {} # Check if the image columns are volumes ih = ImageHandler() # Populate the table in the DataSet db.executeCommand("SELECT * FROM %sObjects;" % tableName) for row in db._iterResults(): rowDict = dict(row) for k, v in rowDict.iteritems(): if v is None: rowDict[k] = '' # Set the index@filename for images columns values if k in imgCols: colName = imgCols[k] index = rowDict[colName] filename = os.path.join(self.projectPath, rowDict[k]) filepath = filename.replace(":mrc", "") if not checkedImgCols.get(colName, False): if os.path.exists(filepath): #print "Fn to get dims: %s@%s" % (index,filename) x, y, z, n = ih.getDimensions((index, filename)) if z > 1: table.getColumn(k).setRenderType(COL_RENDER_VOLUME) checkedImgCols[colName] = True if index: rowDict[k] = '%06d@%s' % (index, filename) table.addRow(row['id'], **rowDict) return table