16acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*M///////////////////////////////////////////////////////////////////////////////////////
26acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
36acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
46acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
56acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  By downloading, copying, installing or using the software you agree to this license.
66acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  If you do not agree to this license, do not download, install,
76acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  copy or use the software.
86acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
96acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                        Intel License Agreement
116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Copyright (C) 2000, Intel Corporation, all rights reserved.
136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Third party copyrights are property of their respective owners.
146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Redistribution and use in source and binary forms, with or without modification,
166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// are permitted provided that the following conditions are met:
176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's of source code must retain the above copyright notice,
196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer.
206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's in binary form must reproduce the above copyright notice,
226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer in the documentation
236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     and/or other materials provided with the distribution.
246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * The name of Intel Corporation may not be used to endorse or promote products
266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     derived from this software without specific prior written permission.
276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// This software is provided by the copyright holders and contributors "as is" and
296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// any express or implied warranties, including, but not limited to, the implied
306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// warranties of merchantability and fitness for a particular purpose are disclaimed.
316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// In no event shall the Intel Corporation or contributors be liable for any direct,
326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// indirect, incidental, special, exemplary, or consequential damages
336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// (including, but not limited to, procurement of substitute goods or services;
346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// loss of use, data, or profits; or business interruption) however caused
356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// and on any theory of liability, whether in contract, strict liability,
366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// or tort (including negligence or otherwise) arising in any way out of
376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// the use of this software, even if advised of the possibility of such damage.
386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//M*/
406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "_ml.h"
426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#if 0
446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL int
466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvCmpIntegers (const void* a, const void* b) {return *(const int*)a - *(const int*)b;}
476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************\
496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn*                    Cross-validation algorithms realizations                            *
506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn\****************************************************************************************/
516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Return pointer to trainIdx. Function DOES NOT FILL this matrix!
536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL
546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennconst CvMat* cvCrossValGetTrainIdxMatrix (const CvStatModel* estimateModel)
556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMat* result = NULL;
576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvCrossValGetTrainIdxMatrix");
596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_CROSSVAL(estimateModel))
626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Pointer point to not CvCrossValidationModel");
646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    result = ((CvCrossValidationModel*)estimateModel)->sampleIdxTrain;
676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return result;
716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn} // End of cvCrossValGetTrainIdxMatrix
726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************/
746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Return pointer to checkIdx. Function DOES NOT FILL this matrix!
756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL
766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennconst CvMat* cvCrossValGetCheckIdxMatrix (const CvStatModel* estimateModel)
776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMat* result = NULL;
796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvCrossValGetCheckIdxMatrix");
816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_CROSSVAL (estimateModel))
846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Pointer point to not CvCrossValidationModel");
866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    result = ((CvCrossValidationModel*)estimateModel)->sampleIdxEval;
896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return result;
936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn} // End of cvCrossValGetCheckIdxMatrix
946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************/
966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Create new Idx-matrix for next classifiers training and return code of result.
976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   Result is 0 if function can't make next step (error input or folds are finished),
986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   it is 1 if all was correct, and it is 2 if current fold wasn't' checked.
996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL
1006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennint cvCrossValNextStep (CvStatModel* estimateModel)
1016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int result = 0;
1036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvCrossValGetNextTrainIdx");
1056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
1066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvCrossValidationModel* crVal = (CvCrossValidationModel*) estimateModel;
1086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int k, fold;
1096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_CROSSVAL (estimateModel))
1116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Pointer point to not CvCrossValidationModel");
1136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    fold = ++crVal->current_fold;
1166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (fold >= crVal->folds_all)
1186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (fold == crVal->folds_all)
1206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            EXIT;
1216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
1226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR (CV_StsInternal, "All iterations has end long ago");
1246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    k = crVal->folds[fold + 1] - crVal->folds[fold];
1286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sampleIdxTrain->data.i = crVal->sampleIdxAll + crVal->folds[fold + 1];
1296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sampleIdxTrain->cols = crVal->samples_all - k;
1306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sampleIdxEval->data.i = crVal->sampleIdxAll + crVal->folds[fold];
1316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sampleIdxEval->cols = k;
1326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (crVal->is_checked)
1346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->is_checked = 0;
1366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        result = 1;
1376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
1396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        result = 2;
1416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
1446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return result;
1466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************/
1496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Do checking part of loop  of cross-validations metod.
1506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL
1516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennvoid cvCrossValCheckClassifier (CvStatModel*  estimateModel,
1526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          const CvStatModel*  model,
1536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          const CvMat*        trainData,
1546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                int           sample_t_flag,
1556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          const CvMat*        trainClasses)
1566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvCrossValCheckClassifier ");
1586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
1596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvCrossValidationModel* crVal = (CvCrossValidationModel*) estimateModel;
1616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int  i, j, k;
1626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int* data;
1636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float* responses_fl;
1646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int    step;
1656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float* responses_result;
1666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int* responses_i;
1676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double te, te1;
1686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double sum_c, sum_p, sum_pp, sum_cp, sum_cc, sq_err;
1696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Check input data to correct values.
1716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_CROSSVAL (estimateModel))
1726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg,"First parameter point to not CvCrossValidationModel");
1746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_STAT_MODEL (model))
1766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Second parameter point to not CvStatModel");
1786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_MAT (trainData))
1806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Third parameter point to not CvMat");
1826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_MAT (trainClasses))
1846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Fifth parameter point to not CvMat");
1866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (crVal->is_checked)
1886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsInternal, "This iterations already was checked");
1906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Initialize.
1936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    k = crVal->sampleIdxEval->cols;
1946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    data = crVal->sampleIdxEval->data.i;
1956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Eval tested feature vectors.
1976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL (cvStatModelMultiPredict (model, trainData, sample_t_flag,
1986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                         crVal->predict_results, NULL, crVal->sampleIdxEval));
1996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Count number if correct results.
2006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    responses_result = crVal->predict_results->data.fl;
2016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (crVal->is_regression)
2026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        sum_c = sum_p = sum_pp = sum_cp = sum_cc = sq_err = 0;
2046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (CV_MAT_TYPE (trainClasses->type) == CV_32FC1)
2056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            responses_fl = trainClasses->data.fl;
2076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            step = trainClasses->rows == 1 ? 1 : trainClasses->step / sizeof(float);
2086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for (i = 0; i < k; i++)
2096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
2106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te = responses_result[*data];
2116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te1 = responses_fl[*data * step];
2126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_c += te1;
2136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_p += te;
2146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_cc += te1 * te1;
2156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_pp += te * te;
2166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_cp += te1 * te;
2176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te -= te1;
2186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sq_err += te  * te;
2196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                data++;
2216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
2226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
2246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            responses_i = trainClasses->data.i;
2266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            step = trainClasses->rows == 1 ? 1 : trainClasses->step / sizeof(int);
2276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for (i = 0; i < k; i++)
2286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
2296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te = responses_result[*data];
2306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te1 = responses_i[*data * step];
2316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_c += te1;
2326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_p += te;
2336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_cc += te1 * te1;
2346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_pp += te * te;
2356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sum_cp += te1 * te;
2366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te -= te1;
2376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                sq_err += te  * te;
2386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                data++;
2406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
2416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // Fixing new internal values of accuracy.
2436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->sum_correct += sum_c;
2446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->sum_predict += sum_p;
2456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->sum_cc += sum_cc;
2466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->sum_pp += sum_pp;
2476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->sum_cp += sum_cp;
2486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->sq_error += sq_err;
2496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
2516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (CV_MAT_TYPE (trainClasses->type) == CV_32FC1)
2536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            responses_fl = trainClasses->data.fl;
2556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            step = trainClasses->rows == 1 ? 1 : trainClasses->step / sizeof(float);
2566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for (i = 0, j = 0; i < k; i++)
2576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
2586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if (cvRound (responses_result[*data]) == cvRound (responses_fl[*data * step]))
2596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    j++;
2606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                data++;
2616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
2626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
2646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            responses_i = trainClasses->data.i;
2666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            step = trainClasses->rows == 1 ? 1 : trainClasses->step / sizeof(int);
2676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for (i = 0, j = 0; i < k; i++)
2686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
2696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if (cvRound (responses_result[*data]) == responses_i[*data * step])
2706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    j++;
2716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                data++;
2726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
2736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // Fixing new internal values of accuracy.
2756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->correct_results += j;
2766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Fixing that this fold already checked.
2786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->all_results += k;
2796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->is_checked = 1;
2806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
2826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn} // End of cvCrossValCheckClassifier
2836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************/
2856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Return current accuracy.
2866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL
2876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennfloat cvCrossValGetResult (const CvStatModel* estimateModel,
2886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                 float*       correlation)
2896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
2906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float result = 0;
2916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvCrossValGetResult");
2936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
2946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double te, te1;
2966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvCrossValidationModel* crVal = (CvCrossValidationModel*)estimateModel;
2976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_CROSSVAL (estimateModel))
2996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Pointer point to not CvCrossValidationModel");
3016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (crVal->all_results)
3046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (crVal->is_regression)
3066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
3076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            result = ((float)crVal->sq_error) / crVal->all_results;
3086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if (correlation)
3096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
3106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te = crVal->all_results * crVal->sum_cp -
3116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                             crVal->sum_correct * crVal->sum_predict;
3126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te *= te;
3136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                te1 = (crVal->all_results * crVal->sum_cc -
3146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                    crVal->sum_correct * crVal->sum_correct) *
3156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                           (crVal->all_results * crVal->sum_pp -
3166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                    crVal->sum_predict * crVal->sum_predict);
3176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                *correlation = (float)(te / te1);
3186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
3206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
3216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
3226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
3236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            result = ((float)crVal->correct_results) / crVal->all_results;
3246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
3256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
3286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return result;
3306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************/
3336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Reset cross-validation EstimateModel to state the same as it was immidiatly after
3346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   its creating.
3356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL
3366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennvoid cvCrossValReset (CvStatModel* estimateModel)
3376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvCrossValReset");
3396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
3406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvCrossValidationModel* crVal = (CvCrossValidationModel*)estimateModel;
3426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_CROSSVAL (estimateModel))
3446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Pointer point to not CvCrossValidationModel");
3466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->current_fold = -1;
3496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->is_checked = 1;
3506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->all_results = 0;
3516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->correct_results = 0;
3526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sq_error = 0;
3536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sum_correct = 0;
3546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sum_predict = 0;
3556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sum_cc = 0;
3566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sum_pp = 0;
3576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sum_cp = 0;
3586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
3606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************/
3636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// This function is standart CvStatModel field to release cross-validation EstimateModel.
3646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL
3656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennvoid cvReleaseCrossValidationModel (CvStatModel** model)
3666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvCrossValidationModel* pModel;
3686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvReleaseCrossValidationModel");
3706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
3716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!model)
3736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsNullPtr, "");
3756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    pModel = (CvCrossValidationModel*)*model;
3786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!pModel)
3796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return;
3816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!CV_IS_CROSSVAL (pModel))
3836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "");
3856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree (&pModel->sampleIdxAll);
3886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree (&pModel->folds);
3896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvReleaseMat (&pModel->sampleIdxEval);
3906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvReleaseMat (&pModel->sampleIdxTrain);
3916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvReleaseMat (&pModel->predict_results);
3926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree (model);
3946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
3966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn} // End of cvReleaseCrossValidationModel.
3976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************/
3996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// This function create cross-validation EstimateModel.
4006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL CvStatModel*
4016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvCreateCrossValidationEstimateModel(
4026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn             int                samples_all,
4036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn       const CvStatModelParams* estimateParams,
4046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn       const CvMat*             sampleIdx)
4056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
4066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvStatModel*            model   = NULL;
4076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvCrossValidationModel* crVal   = NULL;
4086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvCreateCrossValidationEstimateModel");
4106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
4116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int  k_fold = 10;
4136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int  i, j, k, s_len;
4156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int  samples_selected;
4166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvRNG rng;
4176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvRNG* prng;
4186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int* res_s_data;
4196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int* te_s_data;
4206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int* folds;
4216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    rng = cvRNG(cvGetTickCount());
4236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvRandInt (&rng); cvRandInt (&rng); cvRandInt (&rng); cvRandInt (&rng);
4246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Check input parameters.
4256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (estimateParams)
4266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        k_fold = ((CvCrossValidationParams*)estimateParams)->k_fold;
4276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!k_fold)
4286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
4296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "Error in parameters of cross-validation (k_fold == 0)!");
4306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
4316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (samples_all <= 0)
4326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
4336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg, "<samples_all> should be positive!");
4346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
4356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Alloc memory and fill standart StatModel's fields.
4376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL (crVal = (CvCrossValidationModel*)cvCreateStatModel (
4386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                            CV_STAT_MODEL_MAGIC_VAL | CV_CROSSVAL_MAGIC_VAL,
4396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                            sizeof(CvCrossValidationModel),
4406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                            cvReleaseCrossValidationModel,
4416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                            NULL, NULL));
4426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->current_fold    = -1;
4436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->folds_all       = k_fold;
4446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (estimateParams && ((CvCrossValidationParams*)estimateParams)->is_regression)
4456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->is_regression = 1;
4466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
4476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->is_regression = 0;
4486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (estimateParams && ((CvCrossValidationParams*)estimateParams)->rng)
4496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        prng = ((CvCrossValidationParams*)estimateParams)->rng;
4506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
4516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        prng = &rng;
4526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // Check and preprocess sample indices.
4546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (sampleIdx)
4556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
4566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        int s_step;
4576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        int s_type = 0;
4586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (!CV_IS_MAT (sampleIdx))
4606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR (CV_StsBadArg, "Invalid sampleIdx array");
4616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (sampleIdx->rows != 1 && sampleIdx->cols != 1)
4636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR (CV_StsBadSize, "sampleIdx array must be 1-dimensional");
4646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        s_len = sampleIdx->rows + sampleIdx->cols - 1;
4666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        s_step = sampleIdx->rows == 1 ?
4676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                     1 : sampleIdx->step / CV_ELEM_SIZE(sampleIdx->type);
4686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        s_type = CV_MAT_TYPE (sampleIdx->type);
4706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        switch (s_type)
4726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
4736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        case CV_8UC1:
4746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        case CV_8SC1:
4756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
4766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            uchar* s_data = sampleIdx->data.ptr;
4776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            // sampleIdx is array of 1's and 0's -
4796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            // i.e. it is a mask of the selected samples
4806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( s_len != samples_all )
4816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                CV_ERROR (CV_StsUnmatchedSizes,
4826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn       "Sample mask should contain as many elements as the total number of samples");
4836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            samples_selected = 0;
4856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for (i = 0; i < s_len; i++)
4866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                samples_selected += s_data[i * s_step] != 0;
4876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if (samples_selected == 0)
4896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                CV_ERROR (CV_StsOutOfRange, "No samples is selected!");
4906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
4916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            s_len = samples_selected;
4926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
4936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        case CV_32SC1:
4946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if (s_len > samples_all)
4956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                CV_ERROR (CV_StsOutOfRange,
4966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        "sampleIdx array may not contain more elements than the total number of samples");
4976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            samples_selected = s_len;
4986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
4996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        default:
5006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR (CV_StsUnsupportedFormat, "Unsupported sampleIdx array data type "
5016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                               "(it should be 8uC1, 8sC1 or 32sC1)");
5026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
5036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        // Alloc additional memory for internal Idx and fill it.
5056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*!!*/  CV_CALL (res_s_data = crVal->sampleIdxAll =
5066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                 (int*)cvAlloc (2 * s_len * sizeof(int)));
5076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (s_type < CV_32SC1)
5096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
5106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            uchar* s_data = sampleIdx->data.ptr;
5116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for (i = 0; i < s_len; i++)
5126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if (s_data[i * s_step])
5136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
5146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    *res_s_data++ = i;
5156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
5166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            res_s_data = crVal->sampleIdxAll;
5176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
5186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
5196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
5206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            int* s_data = sampleIdx->data.i;
5216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            int out_of_order = 0;
5226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for (i = 0; i < s_len; i++)
5246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
5256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                res_s_data[i] = s_data[i * s_step];
5266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if (i > 0 && res_s_data[i] < res_s_data[i - 1])
5276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    out_of_order = 1;
5286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
5296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if (out_of_order)
5316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                qsort (res_s_data, s_len, sizeof(res_s_data[0]), icvCmpIntegers);
5326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if (res_s_data[0] < 0 ||
5346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                res_s_data[s_len - 1] >= samples_all)
5356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    CV_ERROR (CV_StsBadArg, "There are out-of-range sample indices");
5366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for (i = 1; i < s_len; i++)
5376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if (res_s_data[i] <= res_s_data[i - 1])
5386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    CV_ERROR (CV_StsBadArg, "There are duplicated");
5396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
5406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
5416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else // if (sampleIdx)
5426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
5436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        // Alloc additional memory for internal Idx and fill it.
5446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        s_len = samples_all;
5456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL (res_s_data = crVal->sampleIdxAll = (int*)cvAlloc (2 * s_len * sizeof(int)));
5466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for (i = 0; i < s_len; i++)
5476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
5486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            *res_s_data++ = i;
5496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
5506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        res_s_data = crVal->sampleIdxAll;
5516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    } // if (sampleIdx) ... else
5526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Resort internal Idx.
5546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    te_s_data = res_s_data + s_len;
5556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for (i = s_len; i > 1; i--)
5566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
5576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        j = cvRandInt (prng) % i;
5586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        k = *(--te_s_data);
5596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        *te_s_data = res_s_data[j];
5606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        res_s_data[j] = k;
5616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
5626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Duplicate resorted internal Idx.
5646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// It will be used to simplify operation of getting trainIdx.
5656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    te_s_data = res_s_data + s_len;
5666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for (i = 0; i < s_len; i++)
5676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
5686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        *te_s_data++ = *res_s_data++;
5696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
5706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Cut sampleIdxAll to parts.
5726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (k_fold > 0)
5736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
5746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (k_fold > s_len)
5756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
5766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR (CV_StsBadArg,
5776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        "Error in parameters of cross-validation ('k_fold' > #samples)!");
5786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
5796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        folds = crVal->folds = (int*) cvAlloc ((k_fold + 1) * sizeof (int));
5806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        *folds++ = 0;
5816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for (i = 1; i < k_fold; i++)
5826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
5836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            *folds++ = cvRound (i * s_len * 1. / k_fold);
5846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
5856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        *folds = s_len;
5866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        folds = crVal->folds;
5876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->max_fold_size = (s_len - 1) / k_fold + 1;
5896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
5906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
5916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
5926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        k = -k_fold;
5936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->max_fold_size = k;
5946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (k >= s_len)
5956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
5966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            CV_ERROR (CV_StsBadArg,
5976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                      "Error in parameters of cross-validation (-'k_fold' > #samples)!");
5986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
5996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->folds_all = k = (s_len - 1) / k + 1;
6006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        folds = crVal->folds = (int*) cvAlloc ((k + 1) * sizeof (int));
6026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for (i = 0; i < k; i++)
6036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
6046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            *folds++ = -i * k_fold;
6056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
6066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        *folds = s_len;
6076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        folds = crVal->folds;
6086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
6096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Prepare other internal fields to working.
6116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL (crVal->predict_results = cvCreateMat (1, samples_all, CV_32FC1));
6126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL (crVal->sampleIdxEval = cvCreateMatHeader (1, 1, CV_32SC1));
6136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL (crVal->sampleIdxTrain = cvCreateMatHeader (1, 1, CV_32SC1));
6146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sampleIdxEval->cols = 0;
6156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->sampleIdxTrain->cols = 0;
6166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->samples_all = s_len;
6176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->is_checked = 1;
6186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->getTrainIdxMat = cvCrossValGetTrainIdxMatrix;
6206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->getCheckIdxMat = cvCrossValGetCheckIdxMatrix;
6216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->nextStep = cvCrossValNextStep;
6226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->check = cvCrossValCheckClassifier;
6236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->getResult = cvCrossValGetResult;
6246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    crVal->reset = cvCrossValReset;
6256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    model = (CvStatModel*)crVal;
6276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
6296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (!model)
6316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
6326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        cvReleaseCrossValidationModel ((CvStatModel**)&crVal);
6336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
6346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return model;
6366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn} // End of cvCreateCrossValidationEstimateModel
6376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************\
6406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn*                Extended interface with backcalls for models                            *
6416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn\****************************************************************************************/
6426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennML_IMPL float
6436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvCrossValidation (const CvMat*            trueData,
6446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                         int               tflag,
6456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                   const CvMat*            trueClasses,
6466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                         CvStatModel*     (*createClassifier) (const CvMat*,
6476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                                     int,
6486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                               const CvMat*,
6496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                               const CvClassifierTrainParams*,
6506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                               const CvMat*,
6516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                               const CvMat*,
6526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                               const CvMat*,
6536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                               const CvMat*),
6546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                   const CvClassifierTrainParams*    estimateParams,
6556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                   const CvClassifierTrainParams*    trainParams,
6566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                   const CvMat*            compIdx,
6576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                   const CvMat*            sampleIdx,
6586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                         CvStatModel**     pCrValModel,
6596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                   const CvMat*            typeMask,
6606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                   const CvMat*            missedMeasurementMask)
6616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
6626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvCrossValidationModel* crVal = NULL;
6636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float  result = 0;
6646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvStatModel* pClassifier = NULL;
6656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_FUNCNAME ("cvCrossValidation");
6676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __BEGIN__
6686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const CvMat* trainDataIdx;
6706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int    samples_all;
6716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// checking input data
6736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if ((createClassifier) == NULL)
6746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
6756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsNullPtr, "Null pointer to functiion which create classifier");
6766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
6776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (pCrValModel && *pCrValModel && !CV_IS_CROSSVAL(*pCrValModel))
6786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
6796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR (CV_StsBadArg,
6806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn           "<pCrValModel> point to not cross-validation model");
6816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
6826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// initialization
6846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (pCrValModel && *pCrValModel)
6856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
6866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal = (CvCrossValidationModel*)*pCrValModel;
6876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->reset ((CvStatModel*)crVal);
6886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
6896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
6906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
6916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        samples_all = ((tflag) ? trueData->rows : trueData->cols);
6926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL (crVal = (CvCrossValidationModel*)
6936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn           cvCreateCrossValidationEstimateModel (samples_all, estimateParams, sampleIdx));
6946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
6956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL (trainDataIdx = crVal->getTrainIdxMat ((CvStatModel*)crVal));
6976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
6986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// operation loop
6996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for (; crVal->nextStep((CvStatModel*)crVal) != 0; )
7006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
7016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL (pClassifier = createClassifier (trueData, tflag, trueClasses,
7026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    trainParams, compIdx, trainDataIdx, typeMask, missedMeasurementMask));
7036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL (crVal->check ((CvStatModel*)crVal, pClassifier,
7046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                                           trueData, tflag, trueClasses));
7056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
7066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        pClassifier->release (&pClassifier);
7076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
7086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
7096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Get result and fill output field.
7106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL (result = crVal->getResult ((CvStatModel*)crVal, 0));
7116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
7126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (pCrValModel && !*pCrValModel)
7136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        *pCrValModel = (CvStatModel*)crVal;
7146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
7156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        __END__
7166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
7176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Free all memory that should be freed.
7186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (pClassifier)
7196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        pClassifier->release (&pClassifier);
7206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if (crVal && (!pCrValModel || !*pCrValModel))
7216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        crVal->release ((CvStatModel**)&crVal);
7226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
7236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return result;
7246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn} // End of cvCrossValidation
7256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
7266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
7276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
7286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/* End of file */
729