def test_openTable(self):
        tid = self.create_table()

        tc = TableConnection(client=self.cli, tableName=self.tableName)
        t = tc.openTable(tid)
        self.assertIsNotNone(t)

        t.close()
 def deleteAllTables(self):
     """
     Don't run unless explicitly requested.
     Doesn't seem to work.
     """
     tc = TableConnection(client=self.cli, tableName=self.tableName)
     tc.deleteAllTables()
     ofiles = list(tc.findByName())
     self.assertEqual(len(ofiles), 0)
    def test_findByName(self):
        tid = self.create_table()

        tc = TableConnection(client=self.cli, tableName=self.tableName)
        found = False
        for ofiles in tc.findByName():
            found = found or unwrap(ofiles.getId()) == tid
        self.assertTrue(found)

        tc.close()
    def test_headersRows(self):
        tid = self.create_table()
        tc = TableConnection(client=self.cli)
        t = tc.openTable(tid)

        headers = tc.getHeaders()
        self.assertEqual(len(headers), 1)
        self.assertEqual(headers[0].name, 'lc1')
        # It looks like descriptions aren't returned?????
        #self.assertEqual(headers[0].description, 'l c 1')
        self.assertEqual(tc.getNumberOfRows(), 4)
    def test_chunked(self):
        tid = self.create_table()
        tc = TableConnection(client=self.cli, tableName=self.tableName)
        t = tc.openTable(tid)

        cols = tc.getHeaders()
        cols[0].values = [5, 6]
        n = tc.chunkedAddData(cols, 1)
        self.assertEqual(n, 2)

        data = tc.chunkedRead([0], 1, 6, 3)
        self.assertEqual(len(data.columns), 1)
        self.assertEqual(data.columns[0].values, [2, 3, 4, 5, 6])

        tc.close()
 def __init__(self, client, tableNameF, tableNameW, tableNameL):
     self.tcF = TableConnection(client=client, tableName=tableNameF)
     self.tcW = TableConnection(client=client, tableName=tableNameW)
     self.tcL = TableConnection(client=client, tableName=tableNameL)
     self.versiontag = None
class ClassifierTables(object):
    """
    Create a set of OMERO.tables for storing the state of a trained image
    classifier. The first table stores the training samples with reduced
    features and classes, the second stores a list of weights and feature
    names, and the third stores the class IDs and class names
    """

    def __init__(self, client, tableNameF, tableNameW, tableNameL):
        self.tcF = TableConnection(client=client, tableName=tableNameF)
        self.tcW = TableConnection(client=client, tableName=tableNameW)
        self.tcL = TableConnection(client=client, tableName=tableNameL)
        self.versiontag = None

    def close(self):
        self.tcF.close(False)
        self.tcW.close(False)
        self.tcL.close(False)

    def openTables(self, tidF, tidW, tidL, version=None):
        try:
            self.tcF.openTable(tidF)
            vertag = getVersion(self.tcF.conn, 'OriginalFile', self.tcF.tableId)
            if not vertag:
                raise WndcharmStorageError(
                    'Table id %d has no version tag' % self.tcF.tableId)

            if version is not None:
                assertVersionMatch(
                    version, vertag, 'table:%d' % self.tcF.tableId)
            self.versiontag = vertag

            self.tcW.openTable(tidW)
            vertag = getVersion(self.tcW.conn, 'OriginalFile', self.tcW.tableId)
            assertVersionMatch(
                self.versiontag, vertag, 'table:%d' % self.tcW.tableId)

            self.tcL.openTable(tidL)
            vertag = getVersion(self.tcL.conn, 'OriginalFile', self.tcL.tableId)
            assertVersionMatch(
                self.versiontag, vertag, 'table:%d' % self.tcL.tableId)

            return True
        except TableConnectionError as e:
            print "Failed to open one or more tables: %s" % e
            return False


    def createClassifierTables(self, featureNames, version):
        self.versiontag = getVersionAnnotation(self.tcF.conn, version)
        if not self.versiontag:
            self.versiontag = createVersionAnnotation(self.tcF.conn, version)

        schemaF = [
            omero.grid.LongColumn('id'),
            omero.grid.LongColumn('label'),
            omero.grid.DoubleArrayColumn('features', '', len(featureNames)),
            ]
        self.tcF.newTable(schemaF)
        addTagTo(
            self.tcF.conn, self.versiontag, 'OriginalFile', self.tcF.tableId)

        schemaW = [
            omero.grid.StringColumn('featurename', '', 1024),
            omero.grid.DoubleColumn('weight'),
            ]
        self.tcW.newTable(schemaW)
        addTagTo(
            self.tcW.conn, self.versiontag, 'OriginalFile', self.tcW.tableId)

        schemaL = [
            omero.grid.LongColumn('classID'),
            omero.grid.StringColumn('className', '', 1024),
            ]
        self.tcL.newTable(schemaL)
        addTagTo(
            self.tcL.conn, self.versiontag, 'OriginalFile', self.tcL.tableId)


    def saveClassifierTables(self,
                             ids, classIds, featureMatrix,
                             featureNames, weights, classNames):
        """
        Save the classifier state (reduced features, labels and weights)
        """
        colsF = self.tcF.getHeaders()
        colsF[0].values = ids
        colsF[1].values = classIds
        colsF[2].values = featureMatrix
        self.tcF.chunkedAddData(colsF, CHUNK_SIZE)

        colsW = self.tcW.getHeaders()
        colsW[0].values = featureNames
        colsW[1].values = weights
        self.tcW.chunkedAddData(colsW, CHUNK_SIZE)

        colsL = self.tcL.getHeaders()
        colsL[0].values = range(len(classNames))
        colsL[1].values = classNames
        self.tcL.chunkedAddData(colsL, CHUNK_SIZE)


    def loadClassifierTables(self):
        """
        Load the classifier state (reduced features, labels and weights)
        """
        dF = self.tcF.chunkedRead(
            range(len(self.tcF.getHeaders())), 0,
            self.tcF.getNumberOfRows(), CHUNK_SIZE)
        colsF = dF.columns
        ids = colsF[0].values
        trainClassIds = colsF[1].values
        featureMatrix = colsF[2].values

        dW = self.tcW.chunkedRead(
            range(len(self.tcW.getHeaders())), 0,
            self.tcW.getNumberOfRows(), CHUNK_SIZE)
        colsW = dW.columns
        featureNames = colsW[0].values
        weights = colsW[1].values

        dL = self.tcL.chunkedRead(
            range(len(self.tcL.getHeaders())), 0,
            self.tcL.getNumberOfRows(), CHUNK_SIZE)
        colsL = dL.columns
        classIds = colsL[0].values
        classNames = colsL[1].values

        return {'ids': ids, 'trainClassIds': trainClassIds,
                'featureMatrix': featureMatrix,
                'featureNames': featureNames, 'weights': weights,
                'classIds': classIds, 'classNames': classNames}
 def test_newTable(self):
     tc = TableConnection(client=self.cli, tableName=self.tableName)
     cols = [omero.grid.LongColumn('lc1', 'l c 1', [1, 2, 3])]
     t = tc.newTable(cols)
     self.assertIsNotNone(t)