#NEWLINE# def create_generator( self , bdb , table , schema , instantiate ) : #NEWLINE# #INDENT# parsed_schema = crosscat_generator_schema . parse ( schema , subsample_default = self . _subsample ) #NEWLINE# with bdb . savepoint ( ) : #NEWLINE# #INDENT# if parsed_schema . guess : #NEWLINE# #INDENT# column_names = core . bayesdb_table_column_names ( bdb , table ) #NEWLINE# qt = sqlite3_quote_name ( table ) #NEWLINE# rows = bdb . sql_execute ( ( 'SELECT * FROM %s' % ( qt , ) ) ) . fetchall ( ) #NEWLINE# stattypes = guess . bayesdb_guess_stattypes ( column_names , rows , overrides = parsed_schema . columns ) #NEWLINE# columns = zip ( column_names , stattypes ) #NEWLINE# columns = [ ( name , stattype ) for ( name , stattype ) in columns if ( stattype not in ( 'key' , 'ignore' ) ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# columns = parsed_schema . columns #NEWLINE# #UNINDENT# ( generator_id , column_list ) = instantiate ( columns ) #NEWLINE# M_c = create_metadata ( bdb , generator_id , column_list ) #NEWLINE# insert_metadata_sql = '\n                INSERT INTO bayesdb_crosscat_metadata\n                    (generator_id, metadata_json)\n                    VALUES (?, ?)\n            ' #NEWLINE# metadata_json = json . dumps ( M_c ) #NEWLINE# bdb . sql_execute ( insert_metadata_sql , ( generator_id , metadata_json ) ) #NEWLINE# cc_cache = self . _crosscat_cache ( bdb ) #NEWLINE# if ( cc_cache is not None ) : #NEWLINE# #INDENT# assert ( generator_id not in cc_cache . metadata ) #NEWLINE# cc_cache . metadata [ generator_id ] = M_c #NEWLINE# #UNINDENT# insert_column_sql = '\n                INSERT INTO bayesdb_crosscat_column\n                    (generator_id, colno, cc_colno, disttype)\n                    VALUES (:generator_id, :colno, :cc_colno, :disttype)\n            ' #NEWLINE# insert_codemap_sql = '\n                INSERT INTO bayesdb_crosscat_column_codemap\n                    (generator_id, cc_colno, code, value)\n                    VALUES (:generator_id, :cc_colno, :code, :value)\n            ' #NEWLINE# for ( cc_colno , ( colno , name , _stattype ) ) in enumerate ( column_list ) : #NEWLINE# #INDENT# column_metadata = M_c [ 'column_metadata' ] [ cc_colno ] #NEWLINE# disttype = column_metadata [ 'modeltype' ] #NEWLINE# bdb . sql_execute ( insert_column_sql , { #NEWLINE# #INDENT# 'generator_id' : generator_id , #NEWLINE# 'colno' : colno , #NEWLINE# 'cc_colno' : cc_colno , #NEWLINE# 'disttype' : disttype , #NEWLINE# #UNINDENT# } ) #NEWLINE# codemap = column_metadata [ 'value_to_code' ] #NEWLINE# for code in codemap : #NEWLINE# #INDENT# bdb . sql_execute ( insert_codemap_sql , { #NEWLINE# #INDENT# 'generator_id' : generator_id , #NEWLINE# 'cc_colno' : cc_colno , #NEWLINE# 'code' : code , #NEWLINE# 'value' : codemap [ code ] , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# qt = sqlite3_quote_name ( table ) #NEWLINE# cursor = None #NEWLINE# if parsed_schema . subsample : #NEWLINE# #INDENT# k = parsed_schema . subsample #NEWLINE# sql = ( 'SELECT COUNT(*) FROM %s' % ( qt , ) ) #NEWLINE# n = cursor_value ( bdb . sql_execute ( sql ) ) #NEWLINE# sql = ( 'SELECT _rowid_ FROM %s ORDER BY _rowid_ ASC' % ( qt , ) ) #NEWLINE# cursor = bdb . sql_execute ( sql ) #NEWLINE# seed = struct . pack ( '<QQQQ' , 0 , 0 , k , n ) #NEWLINE# uniform = weakprng . weakprng ( seed ) . weakrandom_uniform #NEWLINE# samples = [ ] #NEWLINE# for ( i , row ) in enumerate ( cursor ) : #NEWLINE# #INDENT# if ( i < k ) : #NEWLINE# #INDENT# samples . append ( row ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# r = uniform ( ( i + 1 ) ) #NEWLINE# if ( r < k ) : #NEWLINE# #INDENT# samples [ r ] = row #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# cursor = samples #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# cursor = bdb . sql_execute ( ( '\n                     SELECT _rowid_ FROM %s ORDER BY _rowid_ ASC\n                ' % ( qt , ) ) ) #NEWLINE# #UNINDENT# insert_subsample_sql = '\n                INSERT INTO bayesdb_crosscat_subsample\n                    (generator_id, sql_rowid, cc_row_id)\n                    VALUES (?, ?, ?)\n            ' #NEWLINE# for ( i , row ) in enumerate ( cursor ) : #NEWLINE# #INDENT# sql_rowid = row [ 0 ] #NEWLINE# cc_row_id = i #NEWLINE# bdb . sql_execute ( insert_subsample_sql , ( generator_id , sql_rowid , cc_row_id ) ) #NEWLINE# #UNINDENT# insert_dep_constraint_sql = '\n                INSERT INTO bayesdb_crosscat_column_dependency\n                    (generator_id, colno0, colno1, dependent)\n                    VALUES (?, ?, ?, ?)\n            ' #NEWLINE# for ( columns , dependent ) in parsed_schema . dep_constraints : #NEWLINE# #INDENT# for ( col1 , col2 ) in itertools . combinations ( columns , 2 ) : #NEWLINE# #INDENT# col1_id = core . bayesdb_generator_column_number ( bdb , generator_id , col1 ) #NEWLINE# col2_id = core . bayesdb_generator_column_number ( bdb , generator_id , col2 ) #NEWLINE# min_col_id = min ( col1_id , col2_id ) #NEWLINE# max_col_id = max ( col1_id , col2_id ) #NEWLINE# try : #NEWLINE# #INDENT# bdb . sql_execute ( insert_dep_constraint_sql , ( generator_id , min_col_id , max_col_id , dependent ) ) #NEWLINE# #UNINDENT# except apsw . ConstraintError : #NEWLINE# #INDENT# raise BQLError ( bdb , 'Invalid dependency constraints!' )