def _validate_input_and_create_table()

in community-artifacts/Deep-learning/Utilities/madlib_image_loader.py [0:0]


    def _validate_input_and_create_table(self, data_x=[], data_y=[]):
        if len(data_x) != len(data_y):
            raise ValueError("Invalid dataset passed, number of labels in "
                             "data_y ({0}) does not match number of images "
                             "in data_x ({1})"\
                .format(len(data_y), len(data_x)))

        self.db_connect()

        if self.append:
            # Validate that table already exists
            try:
                self.db_exec("SELECT count(*) FROM {0}".format(self.table_name),
                             echo=False)
            except db.DatabaseError:
                raise RuntimeError("append=True passed, but cannot append to "
                                   "table {0} in db {1}.  Either make sure the "
                                   "table exists and you have access to it, or "
                                   "use append=False (default) to auto-create it"
                                   "during loading."
                    .format(self.table_name, self.db_creds.db_name))

            print "Appending to table {0} in {1} db".format(self.table_name,
                                                            self.db_creds.db_name)
        else:
            y_type = self.label_datatype
            # Create new table
            try:
                if self.from_disk:
                    sql = "CREATE TABLE {0} (id SERIAL, x REAL[], y {1},\
                        img_name TEXT)".format(self.table_name, y_type)
                else:
                    sql = "CREATE TABLE {0} (id SERIAL, x REAL[], y {1})"\
                        .format( self.table_name, y_type)
                self.db_exec(sql)
            except db.DatabaseError as e:
                raise RuntimeError("{0} while creating {1} in db {2}.\n"
                                   "If the table already exists, you can use "
                                   "append=True to append more images to it."
                                .format(e.message.strip(), self.table_name,
                                        self.db_creds.db_name))

            print "Created table {0} in {1} db".format(self.table_name,
                self.db_creds.db_name)

        self.db_close()