示例#1
0
int main(){
    apop_opts.db_engine='s'; //SQLite only.
    apop_query("create table atab (a numeric)");
    for (int i=0; i< 1e5; i++)
        apop_query("insert into atab values(ran())");
    apop_query("create table powa as "
            "select a, pow(a, 2) as sq, pow(a, 0.5) as sqrt "
            "from atab");

    //compare the std dev of a uniform as reported by the 
    //database routine, the matrix routine, and math.
    double db_pop_stddev = apop_query_to_float("select stddev_pop(a) from powa");
    apop_data *d = apop_query_to_data("select * from powa");
    apop_data *cov = apop_data_covariance(d);
    double matrix_pop_stddev = sqrt(apop_data_get(cov)*(d->matrix->size1/(d->matrix->size1-1.)));
    assert(fabs(db_pop_stddev - matrix_pop_stddev) < 1e-4);
    double actual_stddev = sqrt(2*gsl_pow_3(.5)/3);
    assert(fabs(db_pop_stddev - actual_stddev) < 1e-3);

    float sq_mean = apop_query_to_float("select avg(sq) from powa");
    float actual_sq_mean = 1./3;
    assert(fabs(sq_mean - actual_sq_mean) < 1e-3);

    float sqrt_mean = apop_query_to_float("select avg(sqrt) from powa");
    float actual_sqrt_mean = 2./3;
    assert(fabs(sqrt_mean - actual_sqrt_mean) < 1e-3);
}
示例#2
0
int main(int argc, char **argv){
    char c, msg[1000];
    int colnames = 'y',
        rownames = 0,
        tab_exists_check = 0;
    char **field_names = NULL;
    apop_data *field_name_data, *field_name_data_t;

	sprintf(msg, "%s [-d delimiters] text_file table_name dbname\n"
                "e.g.: %s -d\",|\" infile.txt a_table info.db\n"
"If the input text file name is a single dash, -, then read from STDIN.\n"
"Input must be plain ASCII or UTF-8.\n"
"-d\t\tThe single-character delimiters to use, e.g., -d \" ,\" or -d \"\\t\" (which you \n"
"\t\t\twill almost certainly have to write as -d \"\\\\t\"). Default: \"| ,\\t\", meaning \n"
"\t\t\tthat any of a pipe, space, comma, or tab will delimit separate entries\n"
"-nc\t\tData does not include column names\n"
"-n regex\t\tCase-insensitive regular expression indicating Null values. Default: NaN \n"
"-m\t\tUse a mysql database (default: SQLite)\n"
"-f\t\tfixed width field ends: -f\"3,8,12,17\" (first char is one, not zero)\n"
"-u\t\tmysql username\n"
"-p\t\tmysql password\n"
"-r\t\tData includes row names\n"
"-v\t\tVerbose\n"
"-N\t\tA comma-separated list of column names: -N\"apple,banana,carrot,durian\"\n"
"-O\t\tIf table exists, erase it and write from scratch (i.e., Overwrite)\n"
"-h\t\tPrint this help\n\n"
, argv[0], argv[0]); 
    int * field_list = NULL;

	if(argc<3){
		printf("%s", msg);
		return 0;
	}
	while ((c = getopt (argc, argv, "n:d:f:hmp:ru:vN:O")) != -1)
        if (c=='n') {
              if (optarg[0]=='c')
			    colnames='n';
              else
                strcpy(apop_opts.db_nan, optarg);
        }
		else if (c=='N') {
            apop_regex(optarg, " *([^,]*[^ ]) *(,|$) *", &field_name_data);
            field_name_data_t = apop_data_transpose(field_name_data);
            field_names = field_name_data_t->text[0];
        }
        else if (c=='d') strcpy(apop_opts.input_delimiters, optarg);
		else if (c=='f') field_list = break_down(optarg);
		else if (c=='h') printf("%s", msg);
		else if (c=='m') apop_opts.db_engine = 'm';
		else if (c=='u') strcpy(apop_opts.db_user, optarg);
		else if (c=='p') strcpy(apop_opts.db_pass, optarg);
		else if (c=='r') rownames++;
		else if (c=='v') apop_opts.verbose=2;
		else if (c=='O') tab_exists_check++;
	apop_db_open(argv[optind + 2]);
    if (tab_exists_check) apop_table_exists(argv[optind+1],1);
    apop_query("begin;");
	apop_text_to_db(argv[optind], argv[optind+1], rownames, colnames, field_names, .field_ends=field_list);
    apop_query("commit;");
}
示例#3
0
文件: checkout.c 项目: b-k/tea
void check_out_impute(char **origin, char **destin, int *imputation_number, char **subset, char **filltabin){
    char *filltab = (filltabin && *filltabin) ? *filltabin : "filled";
    Tea_stopif(!origin || !*origin, return, 0, "NULL origin table, but I need that.");
    char *id_column= get_key_word(NULL, "id");
    const char *dest = destin ? *destin : NULL;
    int use_rowids = 0;
    if (!id_column) {
        use_rowids++;
        id_column = strdup("rowid");
    }
    sprintf(apop_opts.db_name_column, "%s",  id_column);
    begin_transaction();
    if (dest && strcmp(*origin, dest)){
        apop_table_exists(dest, 'd');
        apop_query("create table %s as select %s * from %s %s %s", 
                        dest, 
                        use_rowids ? "rowid as id_col, " : " ", *origin,
                        (subset && *subset) ? "where" : " ",
                        (subset && *subset) ? *subset : " "
                        );
    } else dest = *origin;
    create_index(dest, use_rowids ? "id_col" : id_column);
    Tea_stopif(!apop_table_exists(filltab), return , 0, "No table named '%s'; did you already doMImpute()?", filltab);
    apop_data *fills = apop_query_to_text("select %s, field, value from %s where (draw=%i or draw = -1)"
                                              , id_column, filltab, *imputation_number);
    Tea_stopif(!fills || fills->error, return, 0, "Expected fill-in table "
                "%s, but couldn't query it.", filltab);
    for(int i=0; i< *fills->textsize; i++){
        _Bool is_null = !strcmp(fills->text[i][1], apop_opts.nan_string);
        char tick = is_null ? ' ' : '\'';
        apop_query("update %s set %s = %c%s%c "
                   "where cast(%s as numeric) = %s", 
                      dest, fills->text[i][0], 
                      tick, is_null ? "NULL" : fills->text[i][1], tick,
                      id_column, fills->names->row[i]);
    }
    commit_transaction();
    apop_data_free(fills);
    free(id_column);
}
示例#4
0
//these work by checking that K-L divergence shrunk, and that individual margins are correct.
void test_raking_further(){
    apop_table_exists("rake_test", 'd');
    apop_query("create table rake_test (first, second, weights);"
            "insert into rake_test values(1, 1, 10);"
            "insert into rake_test values(1, 2, 2);"
            "insert into rake_test values(2, 1, 15);"
            "insert into rake_test values(2, 2, 5);"
            );

    //Synthetic data, starting at all ones.
    apop_data_print(
            apop_rake(.margin_table="rake_test", .count_col="weights", 
                .contrasts=(char*[]){"first", "second"}, .contrast_ct=2),
示例#5
0
文件: checkout.c 项目: rforge/tea
void check_out_impute(char **origin, char **destin, int *imputation_number, char **subset, char **filltabin){
    char *filltab = (filltabin && *filltabin) ? *filltabin : "filled";
    Apop_stopif(!origin || !*origin, return, 0, "NULL origin table, but I need that.");
    char *id_column= get_key_word(NULL, "id");
    const char *dest = destin ? *destin : NULL;
    int use_rowids = 0;
    if (!id_column) {
        use_rowids++;
        id_column = strdup("rowid");
    }
    sprintf(apop_opts.db_name_column, "%s",  id_column);
    if (dest){
        apop_table_exists(dest, 'd');
        apop_query("create table %s as select %s * from %s %s %s", 
                        dest, 
                        use_rowids ? "rowid as id_col, " : " ", *origin,
                        (subset && *subset) ? "where" : " ",
                        (subset && *subset) ? *subset : " "
                        );
    } else dest = *origin;
    has_sqlite3_index(dest, use_rowids ? "id_col" : id_column, 'y');
    Apop_stopif(!apop_table_exists(filltab), return , 0, "No table named '%s'; did you already doMImpute()?", filltab);
    apop_data *fills = apop_query_to_text("select %s, field, value from %s where draw+0.0=%i"
                                              , id_column, filltab, *imputation_number);
    Apop_stopif(!fills || fills->error, return, 0, "Expected fill-in table "
                "%s, but couldn't query it.", filltab);
    begin_transaction();
    if (fills)
        for(int i=0; i< *fills->textsize; i++)
            apop_query("update %s set %s = '%s' "
                       "where %s = %s", 
                          dest, fills->text[i][0], fills->text[i][1], 
                          id_column, fills->names->row[i]);
    commit_transaction();
    apop_data_free(fills);
    free(id_column);
}
示例#6
0
文件: checkout.c 项目: rforge/tea
void test_check_out_impute(){
    apop_table_exists("testcheckoutbase", 'd');
    apop_table_exists("testcheckoutfill", 'd');
    apop_table_exists("testcheckoutbase_copy", 'd');
    apop_table_exists("tcb", 'd');
    apop_query("create table testcheckoutbase (id, a, b); "
            " insert into testcheckoutbase values(0, 3, 0./0.);"
            " insert into testcheckoutbase values(1, 0./0., 3);"
            " insert into testcheckoutbase values(2, 3, 3);"
            "create table testcheckoutfill (draw, value, id, field); "
            " insert into testcheckoutfill values(0, 3, 1, 'a');"
            " insert into testcheckoutfill values(0, 3, 0, 'b');"
            " insert into testcheckoutfill values(1, 9, 1, 'a');"
            " insert into testcheckoutfill values(1, 6, 0, 'b');"
            );
    char *strings[] = {"testcheckoutbase", //0
                       "testcheckoutfill", //1
                       "testcheckoutbase_copy", //2
                       "tcb" //3
    };
    set_key_text("id", NULL, "id");
    check_out_impute( strings+0, strings+2, (int[]){0}, NULL, strings+1);
示例#7
0
int main(int argc, char **argv){
    char c, msg[1000];
    int colnames = 1,
        rownames = 0,
        tab_exists_check = 0;

	sprintf(msg, "%s [-d delimiters] text_file table_name dbname\n"
                "e.g.: %s -d\",|\" infile.txt a_table info.db\n"
"If the input text file name is a single dash, -, then read from STDIN.\n"
"Input must be plain ASCII or UTF-8.\n"
"-d\t\tThe single-character delimiters to use, e.g., -d \" ,\" or -d \"\\t\" (which you \n"
"\t\t\twill almost certainly have to write as -d \"\\\\t\"). Default: \"| ,\\t\", meaning \n"
"\t\t\tthat any of a pipe, space, comma, or tab will delimit separate entries\n"
"-nc\t\tData does not include column names\n"
"-n regex\t\tCase-insensitive regular expression indicating Null values. Default: NaN \n"
"-m\t\tUse a mysql database (default: SQLite)\n"
"-f\t\tfixed width field ends: -f\"3,8,12,17\" (first char is one, not zero)\n"
"-u\t\tmysql username\n"
"-p\t\tmysql password\n"
"-r\t\tData includes row names\n"
"-v\t\tVerbose\n"
"-O\t\tIf table exists, erase it and write from scratch (i.e., Overwrite)\n"
"-h\t\tPrint this help\n\n"
, argv[0], argv[0]); 
    int * field_list = NULL;

	if(argc<3){
		printf("%s", msg);
		return 0;
	}
	while ((c = getopt (argc, argv, "n:d:f:hmp:ru:vO")) != -1){
		switch (c){
		  case 'n':
              if (optarg[0]=='c')
			    colnames    --;
              else
                strcpy(apop_opts.db_nan, optarg);
			break;
		  case 'd':
			strcpy(apop_opts.input_delimiters, optarg);
			break;
		  case 'f':
            field_list = break_down(optarg);
            break;
		  case 'h':
			printf("%s", msg);
			return 0;
		  case 'm':
			apop_opts.db_engine = 'm';
            break;
		  case 'u':
			strcpy(apop_opts.db_user, optarg);
			break;
		  case 'p':
			strcpy(apop_opts.db_pass, optarg);
			break;
		  case 'r':
			rownames    ++;
			break;
		  case 'v':
			apop_opts.verbose ++;
			break;
		  case 'O':
            tab_exists_check    ++;
			break;
		}
	}
	apop_db_open(argv[optind + 2]);
    if (tab_exists_check) apop_table_exists(argv[optind+1],1);
    apop_query("begin;");
	apop_text_to_db(argv[optind], argv[optind+1], rownames,colnames, NULL, .field_ends=field_list);
    apop_query("commit;");
}