data.cpp 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
#include <ctype.h>

#define MISS_VAL    FLT_MAX 
#define CV_VAR_MISS    0

47
CvTrainTestSplit::CvTrainTestSplit()
48 49 50 51 52 53
{
    train_sample_part_mode = CV_COUNT;
    train_sample_part.count = -1;
    mix = false;
}

54
CvTrainTestSplit::CvTrainTestSplit( int _train_sample_count, bool _mix )
55 56 57 58 59 60
{
    train_sample_part_mode = CV_COUNT;
    train_sample_part.count = _train_sample_count;
    mix = _mix;
}
    
61
CvTrainTestSplit::CvTrainTestSplit( float _train_sample_portion, bool _mix )
62 63 64 65 66 67 68 69
{
    train_sample_part_mode = CV_PORTION;
    train_sample_part.portion = _train_sample_portion;
    mix = _mix;
}

////////////////

70
CvMLData::CvMLData()
71 72 73
{
    values = missing = var_types = var_idx_mask = response_out = var_idx_out = var_types_out = 0;
    train_sample_idx = test_sample_idx = 0;
74
	header_lines_number = 0;
75 76 77 78 79 80 81 82 83
    sample_idx = 0;
    response_idx = -1;

    train_sample_count = -1;

    delimiter = ',';
    miss_ch = '?';
    //flt_separator = '.';

84
    rng = &cv::theRNG();
85 86
}

87
CvMLData::~CvMLData()
88 89 90 91
{
    clear();
}

92
void CvMLData::free_train_test_idx()
93 94 95 96 97 98
{
    cvReleaseMat( &train_sample_idx );
    cvReleaseMat( &test_sample_idx );
    sample_idx = 0;
}

99
void CvMLData::clear()
100
{
101
    class_map.clear();
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    cvReleaseMat( &values );
    cvReleaseMat( &missing );
    cvReleaseMat( &var_types );
    cvReleaseMat( &var_idx_mask );

    cvReleaseMat( &response_out );
    cvReleaseMat( &var_idx_out );
    cvReleaseMat( &var_types_out );

    free_train_test_idx();
    
    total_class_count = 0;

    response_idx = -1;

    train_sample_count = -1;
}

121 122 123 124 125 126 127 128 129 130 131

void CvMLData::set_header_lines_number( int idx )
{
	header_lines_number = std::max(0, idx);
}

int CvMLData::get_header_lines_number() const
{
	return header_lines_number;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
static char *fgets_chomp(char *str, int n, FILE *stream)
{
	char *head = fgets(str, n, stream);
	if( head )
	{
		for(char *tail = head + strlen(head) - 1; tail >= head; --tail)
		{
			if( *tail != '\r'  && *tail != '\n' )
				break;
			*tail = '\0';
		}
	}
	return head;
}


148
int CvMLData::read_csv(const char* filename)
149
{
150
    const int M = 1000000;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    const char str_delimiter[3] = { ' ', delimiter, '\0' };
    FILE* file = 0;
    CvMemStorage* storage;
    CvSeq* seq;
    char *ptr;
    float* el_ptr;
    CvSeqReader reader;
    int cols_count = 0;    
    uchar *var_types_ptr = 0;

    clear();

    file = fopen( filename, "rt" );
    
    if( !file )
        return -1;

168
	std::vector<char> _buf(M);
169
    char* buf = &_buf[0];
170 171 172 173 174 175 176
    
	// skip header lines
	for( int i = 0; i < header_lines_number; i++ )
		if( fgets( buf, M, file ) == 0 )
			return -1;

    // read the first data line and determine the number of variables
177 178 179
    if( !fgets_chomp( buf, M, file ))
    {
        fclose(file);
180
        return -1;
181
    }
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

    ptr = buf;
    while( *ptr == ' ' )
        ptr++;
    for( ; *ptr != '\0'; )
    {
        if(*ptr == delimiter || *ptr == ' ')
        {
            cols_count++;
            ptr++;
            while( *ptr == ' ' ) ptr++;
        }
        else
            ptr++;
    }
197

Maria Dimashova's avatar
Maria Dimashova committed
198 199
	cols_count++;

200 201 202
    if ( cols_count == 0)
    {
        fclose(file);
203
        return -1;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    }

    // create temporary memory storage to store the whole database
    el_ptr = new float[cols_count];
    storage = cvCreateMemStorage();
    seq = cvCreateSeq( 0, sizeof(*seq), cols_count*sizeof(float), storage );

    var_types = cvCreateMat( 1, cols_count, CV_8U );
    cvZero( var_types );
    var_types_ptr = var_types->data.ptr;

    for(;;)
    {
        char *token = NULL;
        int type;
        token = strtok(buf, str_delimiter);
        if (!token) 
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
221
            break;
222 223 224 225 226 227 228 229
        for (int i = 0; i < cols_count-1; i++)
        {
            str_to_flt_elem( token, el_ptr[i], type);
            var_types_ptr[i] |= type;
            token = strtok(NULL, str_delimiter);
            if (!token)
            {
                fclose(file);
230
                return -1;
231 232 233 234 235
            }
        }
        str_to_flt_elem( token, el_ptr[cols_count-1], type);
        var_types_ptr[cols_count-1] |= type;
        cvSeqPush( seq, el_ptr );
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
236
        if( !fgets_chomp( buf, M, file ) )
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
            break;
    }
    fclose(file);

    values = cvCreateMat( seq->total, cols_count, CV_32FC1 );
    missing = cvCreateMat( seq->total, cols_count, CV_8U );
    var_idx_mask = cvCreateMat( 1, values->cols, CV_8UC1 );
    cvSet( var_idx_mask, cvRealScalar(1) );
    train_sample_count = seq->total;

    cvStartReadSeq( seq, &reader );
    for(int i = 0; i < seq->total; i++ )
    {
        const float* sdata = (float*)reader.ptr;
        float* ddata = values->data.fl + cols_count*i;
        uchar* dm = missing->data.ptr + cols_count*i;

        for( int j = 0; j < cols_count; j++ )
        {
            ddata[j] = sdata[j];
            dm[j] = ( fabs( MISS_VAL - sdata[j] ) <= FLT_EPSILON );
        }
        CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
    }

    if ( cvNorm( missing, 0, CV_L1 ) <= FLT_EPSILON )
        cvReleaseMat( &missing );

    cvReleaseMemStorage( &storage );
    delete []el_ptr;
    return 0;
}

270
const CvMat* CvMLData::get_values() const
271 272 273 274
{
    return values;
}

275
const CvMat* CvMLData::get_missing() const
276
{
277 278 279 280 281 282 283 284
    CV_FUNCNAME( "CvMLData::get_missing" );
    __BEGIN__;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    __END__;

285 286 287
    return missing;
}

288 289 290 291 292
const std::map<std::string, int>& CvMLData::get_class_labels_map() const
{
    return class_map;
}

293
void CvMLData::str_to_flt_elem( const char* token, float& flt_elem, int& type)
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
    
    char* stopstring = NULL;
    flt_elem = (float)strtod( token, &stopstring );
    assert( stopstring );
    type = CV_VAR_ORDERED;
    if ( *stopstring == miss_ch && strlen(stopstring) == 1 ) // missed value
    {
        flt_elem = MISS_VAL;
        type = CV_VAR_MISS;
    }
    else
    {
        if ( (*stopstring != 0) && (*stopstring != '\n') && (strcmp(stopstring, "\r\n") != 0) ) // class label
        {
309
            int idx = class_map[token];
310 311 312 313
            if ( idx == 0)
            {
                total_class_count++;
                idx = total_class_count;
314
                class_map[token] = idx;
315 316 317 318 319 320 321
            }
            flt_elem = (float)idx;
            type = CV_VAR_CATEGORICAL;
        }
    }
}

322
void CvMLData::set_delimiter(char ch)
323
{
324
    CV_FUNCNAME( "CvMLData::set_delimited" );
325 326 327 328 329 330 331 332 333 334
    __BEGIN__;

    if (ch == miss_ch /*|| ch == flt_separator*/)
        CV_ERROR(CV_StsBadArg, "delimited, miss_character and flt_separator must be different");
    
    delimiter = ch;

    __END__;
}

335
char CvMLData::get_delimiter() const
336
{
337 338 339 340 341 342
    return delimiter;
}

void CvMLData::set_miss_ch(char ch)
{
    CV_FUNCNAME( "CvMLData::set_miss_ch" );
343 344 345 346 347 348 349 350 351 352
    __BEGIN__;

    if (ch == delimiter/* || ch == flt_separator*/)
        CV_ERROR(CV_StsBadArg, "delimited, miss_character and flt_separator must be different");
   
    miss_ch = ch;

    __END__;
}

353
char CvMLData::get_miss_ch() const
354 355 356 357 358
{
    return miss_ch;
}

void CvMLData::set_response_idx( int idx )
359
{
360
    CV_FUNCNAME( "CvMLData::set_response_idx" );
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    __BEGIN__;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    if ( idx >= values->cols)
        CV_ERROR( CV_StsBadArg, "idx value is not correct" );

    if ( response_idx >= 0 )
        chahge_var_idx( response_idx, true );
    if ( idx >= 0 )
        chahge_var_idx( idx, false );
    response_idx = idx;

    __END__;    
}

378
int CvMLData::get_response_idx() const
379
{
380 381 382 383 384 385
    CV_FUNCNAME( "CvMLData::get_response_idx" );
    __BEGIN__;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );
     __END__;
386 387 388 389 390 391
    return response_idx;
}

void CvMLData::change_var_type( int var_idx, int type )
{
    CV_FUNCNAME( "CvMLData::change_var_type" );
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    __BEGIN__;
    
    int var_count = 0;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );
    
     var_count = values->cols;

    if ( var_idx < 0 || var_idx >= var_count)
        CV_ERROR( CV_StsBadArg, "var_idx is not correct" );

    if ( type != CV_VAR_ORDERED && type != CV_VAR_CATEGORICAL)
         CV_ERROR( CV_StsBadArg, "type is not correct" );

    assert( var_types );    
    if ( var_types->data.ptr[var_idx] == CV_VAR_CATEGORICAL && type == CV_VAR_ORDERED)
        CV_ERROR( CV_StsBadArg, "it`s impossible to assign CV_VAR_ORDERED type to categorical variable" );
    var_types->data.ptr[var_idx] = (uchar)type;

    __END__;

    return;
}

417
void CvMLData::set_var_types( const char* str )
418
{
419
    CV_FUNCNAME( "CvMLData::set_var_types" );
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    __BEGIN__;

    const char* ord = 0, *cat = 0;
    int var_count = 0, set_var_type_count = 0;
    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    var_count = values->cols;

    assert( var_types );
 
    ord = strstr( str, "ord" );
    cat = strstr( str, "cat" );    
    if ( !ord && !cat )
        CV_ERROR( CV_StsBadArg, "types string is not correct" );
    
    if ( !ord && strlen(cat) == 3 ) // str == "cat"
    {
        cvSet( var_types, cvScalarAll(CV_VAR_CATEGORICAL) );
        return;
    }

    if ( !cat && strlen(ord) == 3 ) // str == "ord"
    {
        cvSet( var_types, cvScalarAll(CV_VAR_ORDERED) );
        return;
    }

    if ( ord ) // parse ord str
    {
        char* stopstring = NULL;            
        if ( ord[3] != '[')
            CV_ERROR( CV_StsBadArg, "types string is not correct" );
        
        ord += 4; // pass "ord["
        do
        {
            int b1 = (int)strtod( ord, &stopstring );
            if ( *stopstring == 0 || (*stopstring != ',' && *stopstring != ']' && *stopstring != '-') )
                CV_ERROR( CV_StsBadArg, "types string is not correct" );
            ord = stopstring + 1;
            if ( (stopstring[0] == ',') || (stopstring[0] == ']'))
            {
                if ( var_types->data.ptr[b1] == CV_VAR_CATEGORICAL)
                    CV_ERROR( CV_StsBadArg, "it`s impossible to assign CV_VAR_ORDERED type to categorical variable" );
                var_types->data.ptr[b1] = CV_VAR_ORDERED;
                set_var_type_count++;
            }
            else 
            {
                if ( stopstring[0] == '-') 
                {
                    int b2 = (int)strtod( ord, &stopstring);
                    if ( (*stopstring == 0) || (*stopstring != ',' && *stopstring != ']') )
                        CV_ERROR( CV_StsBadArg, "types string is not correct" );           
                    ord = stopstring + 1;
                    for (int i = b1; i <= b2; i++)
                    {
                        if ( var_types->data.ptr[i] == CV_VAR_CATEGORICAL)
                            CV_ERROR( CV_StsBadArg, "it`s impossible to assign CV_VAR_ORDERED type to categorical variable" );                
                        var_types->data.ptr[i] = CV_VAR_ORDERED;
                    }
                    set_var_type_count += b2 - b1 + 1;
                }
                else
                    CV_ERROR( CV_StsBadArg, "types string is not correct" );

            }
        }
        while (*stopstring != ']');

        if ( stopstring[1] != '\0' && stopstring[1] != ',')
            CV_ERROR( CV_StsBadArg, "types string is not correct" );
    }    

    if ( cat ) // parse cat str
    {
        char* stopstring = NULL;            
        if ( cat[3] != '[')
            CV_ERROR( CV_StsBadArg, "types string is not correct" );
        
        cat += 4; // pass "cat["
        do
        {
            int b1 = (int)strtod( cat, &stopstring );
            if ( *stopstring == 0 || (*stopstring != ',' && *stopstring != ']' && *stopstring != '-') )
                CV_ERROR( CV_StsBadArg, "types string is not correct" );
            cat = stopstring + 1;
            if ( (stopstring[0] == ',') || (stopstring[0] == ']'))
            {
                var_types->data.ptr[b1] = CV_VAR_CATEGORICAL;
                set_var_type_count++;
            }
            else 
            {
                if ( stopstring[0] == '-') 
                {
                    int b2 = (int)strtod( cat, &stopstring);
                    if ( (*stopstring == 0) || (*stopstring != ',' && *stopstring != ']') )
                        CV_ERROR( CV_StsBadArg, "types string is not correct" );           
                    cat = stopstring + 1;
                    for (int i = b1; i <= b2; i++)
                        var_types->data.ptr[i] = CV_VAR_CATEGORICAL;
                    set_var_type_count += b2 - b1 + 1;
                }
                else
                    CV_ERROR( CV_StsBadArg, "types string is not correct" );

            }
        }
        while (*stopstring != ']');

        if ( stopstring[1] != '\0' && stopstring[1] != ',')
            CV_ERROR( CV_StsBadArg, "types string is not correct" );
    }    

    if (set_var_type_count != var_count)
        CV_ERROR( CV_StsBadArg, "types string is not correct" );

     __END__;
}

542
const CvMat* CvMLData::get_var_types()
543
{
544
    CV_FUNCNAME( "CvMLData::get_var_types" );
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    __BEGIN__;

    uchar *var_types_out_ptr = 0;
    int avcount, vt_size;
    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    assert( var_idx_mask );

    avcount = cvFloor( cvNorm( var_idx_mask, 0, CV_L1 ) );
    vt_size = avcount + (response_idx >= 0);

    if ( avcount == values->cols || (avcount == values->cols-1 && response_idx == values->cols-1) )
        return var_types;

    if ( !var_types_out || ( var_types_out && var_types_out->cols != vt_size ) ) 
    {
        cvReleaseMat( &var_types_out );
        var_types_out = cvCreateMat( 1, vt_size, CV_8UC1 );
    }

    var_types_out_ptr = var_types_out->data.ptr;
    for( int i = 0; i < var_types->cols; i++)
    {
        if (i == response_idx || !var_idx_mask->data.ptr[i]) continue;
        *var_types_out_ptr = var_types->data.ptr[i];
        var_types_out_ptr++;
    }
    if ( response_idx >= 0 )
        *var_types_out_ptr = var_types->data.ptr[response_idx];

    __END__;

    return var_types_out;
}

581
int CvMLData::get_var_type( int var_idx ) const
582 583 584 585 586
{
    return var_types->data.ptr[var_idx];
}

const CvMat* CvMLData::get_responses()
587
{
588
    CV_FUNCNAME( "CvMLData::get_responses_ptr" );
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
    __BEGIN__;

    int var_count = 0;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );
    var_count = values->cols;
    
    if ( response_idx < 0 || response_idx >= var_count )
       return 0;
    if ( !response_out )
        response_out = cvCreateMatHeader( values->rows, 1, CV_32FC1 );
    else
        cvInitMatHeader( response_out, values->rows, 1, CV_32FC1);
    cvGetCol( values, response_out, response_idx );

    __END__;

    return response_out;
}

610
void CvMLData::set_train_test_split( const CvTrainTestSplit * spl)
611
{
612
    CV_FUNCNAME( "CvMLData::set_division" );
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    __BEGIN__;

    int sample_count = 0;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    sample_count = values->rows;
    
    float train_sample_portion;

    if (spl->train_sample_part_mode == CV_COUNT)
    {
        train_sample_count = spl->train_sample_part.count;
        if (train_sample_count > sample_count)
            CV_ERROR( CV_StsBadArg, "train samples count is not correct" );
        train_sample_count = train_sample_count<=0 ? sample_count : train_sample_count;
    }
    else // dtype.train_sample_part_mode == CV_PORTION
    {
        train_sample_portion = spl->train_sample_part.portion;
        if ( train_sample_portion > 1)
            CV_ERROR( CV_StsBadArg, "train samples count is not correct" );
        train_sample_portion = train_sample_portion <= FLT_EPSILON || 
            1 - train_sample_portion <= FLT_EPSILON ? 1 : train_sample_portion;
638
        train_sample_count = std::max(1, cvFloor( train_sample_portion * sample_count ));
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
    }

    if ( train_sample_count == sample_count )
    {
        free_train_test_idx();
        return;
    }

    if ( train_sample_idx && train_sample_idx->cols != train_sample_count )
        free_train_test_idx();

    if ( !sample_idx)
    {
        int test_sample_count = sample_count- train_sample_count;
        sample_idx = (int*)cvAlloc( sample_count * sizeof(sample_idx[0]) );
        for (int i = 0; i < sample_count; i++ )
            sample_idx[i] = i;
        train_sample_idx = cvCreateMatHeader( 1, train_sample_count, CV_32SC1 );
        *train_sample_idx = cvMat( 1, train_sample_count, CV_32SC1, &sample_idx[0] );
658 659 660

        CV_Assert(test_sample_count > 0);
        test_sample_idx = cvCreateMatHeader( 1, test_sample_count, CV_32SC1 );
661 662 663 664 665 666 667 668 669 670
        *test_sample_idx = cvMat( 1, test_sample_count, CV_32SC1, &sample_idx[train_sample_count] );
    }
    
    mix = spl->mix;
    if ( mix )
        mix_train_and_test_idx();
    
    __END__;
}

671
const CvMat* CvMLData::get_train_sample_idx() const
672
{
673 674 675 676 677 678 679
    CV_FUNCNAME( "CvMLData::get_train_sample_idx" );
    __BEGIN__;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );
    __END__;

680 681 682
    return train_sample_idx;
}

683
const CvMat* CvMLData::get_test_sample_idx() const
684
{
685 686 687 688 689 690 691
    CV_FUNCNAME( "CvMLData::get_test_sample_idx" );
    __BEGIN__;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );
    __END__;

692 693 694 695
    return test_sample_idx;
}

void CvMLData::mix_train_and_test_idx()
696
{
697 698 699 700 701 702 703 704 705
    CV_FUNCNAME( "CvMLData::mix_train_and_test_idx" );
    __BEGIN__;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );
    __END__;

    if ( !sample_idx)
        return;
706 707 708 709 710 711

    if ( train_sample_count > 0 && train_sample_count < values->rows )
    {
        int n = values->rows;
        for (int i = 0; i < n; i++)
        {
712 713
            int a = (*rng)(n);
            int b = (*rng)(n);
714 715 716 717 718 719
            int t;
            CV_SWAP( sample_idx[a], sample_idx[b], t );
        }
    }
}

720
const CvMat* CvMLData::get_var_idx()
721
{
722
     CV_FUNCNAME( "CvMLData::get_var_idx" );
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
    __BEGIN__;

    int avcount = 0;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    assert( var_idx_mask );
    
    avcount = cvFloor( cvNorm( var_idx_mask, 0, CV_L1 ) );
    int* vidx;

    if ( avcount == values->cols )
        return 0;
     
    if ( !var_idx_out || ( var_idx_out && var_idx_out->cols != avcount ) ) 
    {
        cvReleaseMat( &var_idx_out );
        var_idx_out = cvCreateMat( 1, avcount, CV_32SC1);
        if ( response_idx >=0 )
            var_idx_mask->data.ptr[response_idx] = 0;
    }

    vidx = var_idx_out->data.i;
    
    for(int i = 0; i < var_idx_mask->cols; i++)
        if ( var_idx_mask->data.ptr[i] )
        {            
            *vidx = i;
            vidx++;
        }

    __END__;

    return var_idx_out;
}

760
void CvMLData::chahge_var_idx( int vi, bool state )
761
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
762 763 764 765 766 767
    change_var_idx( vi, state );
}

void CvMLData::change_var_idx( int vi, bool state )
{
     CV_FUNCNAME( "CvMLData::change_var_idx" );
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
    __BEGIN__;

    int var_count = 0;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    var_count = values->cols;

    if ( vi < 0 || vi >= var_count)
        CV_ERROR( CV_StsBadArg, "variable index is not correct" );

    assert( var_idx_mask );    
    var_idx_mask->data.ptr[vi] = state;

    __END__;
}

/* End of file. */