static std::tuple< std::vector<RMatrixXs>, std::vector<RMatrixXb> > readMat( const std::string & name ) { std::vector<RMatrixXs> seg; std::vector<RMatriXb> bnd; mat_t * mat = Mat_Open(name.c_str(),MAT_ACC_RDONLY); if(mat) { matvar_t * gt = Mat_VarRead( mat, (char*)"groundTruth" ); if(!gt) return std::make_tuple(seg,bnd); int nSeg = gt->dims[1]; if(!nSeg) return std::make_tuple(seg,bnd); for( int s = 0; s < nSeg; s++ ) { const char * names[2] = {"Segmentation","Boundaries"}; const int types[2] = {MAT_T_UINT16,MAT_T_UINT8}; for( int it=0; it<2; it++ ) { matvar_t * cl = Mat_VarGetCell(gt, s); matvar_t * arr = Mat_VarGetStructField( cl, (void*)names[it], MAT_BY_NAME, 0 ); int W = arr->dims[1], H = arr->dims[0]; if( arr->data_type != types[it] ) printf("Unexpected %s type! Continuing in denial ...\n",names[it]); if(it==0) seg.push_back( MatrixXus::Map((const uint16_t*)arr->data,H,W)-1 ); else bnd.push_back( RMatrixXb::Map((const bool*)arr->data,H,W) ); } } Mat_VarFree( gt ); Mat_Close(mat); } return std::make_tuple(seg,bnd); }
static np::ndarray bnread( const std::string & name ) { np::ndarray r(np::empty(make_tuple(0), np::dtype::get_builtin<unsigned char>())); mat_t * mat = Mat_Open(name.c_str(),MAT_ACC_RDONLY); if(mat) { matvar_t * gt = Mat_VarRead( mat, (char*)"groundTruth" ); if(!gt) return r; int nSeg = gt->dims[1]; if(!nSeg) return r; for( int s = 0; s < nSeg; s++ ) { matvar_t * cl = Mat_VarGetCell(gt, s); matvar_t * bnd = Mat_VarGetStructField( cl, (void*)"Boundaries", MAT_BY_NAME, 0 ); int W = bnd->dims[1], H = bnd->dims[0]; if( bnd->data_type != MAT_T_UINT8 ) printf("Unexpected boundary type! Continuing in denial and assuming uint8_t\n"); if( r.shape(0) <= 0 ) r = np::empty(make_tuple(nSeg,H,W), np::dtype::get_builtin<unsigned char>()); assert( r.shape(1) == H && r.shape(2) == W ); const unsigned char * pdata = static_cast<const unsigned char*>(bnd->data); unsigned char * pr = reinterpret_cast<unsigned char*>(r.get_data()+s*W*H); for( int j=0; j<H; j++ ) for( int i=0; i<W; i++ ) pr[j*W+i] = pdata[i*H+j]; } Mat_VarFree( gt ); Mat_Close(mat); } return r; }
static void read_selected_data(mat_t *mat,matvar_t *matvar,char *index_str) { char *next_tok_pos, next_tok = 0; char *open = NULL, *close = NULL; int err, i = 0, j, done = 0; next_tok_pos = get_next_token(index_str); next_tok = *next_tok_pos; while ( !done ) { /* Check If the user is selecting a subset of the dataset */ if ( next_tok == '(' ) { int rank, *start, *stride, *edge,nmemb; open = next_tok_pos; close = strchr(open+1,')'); /* Get the next token after this selection */ next_tok_pos = get_next_token(close+1); if ( next_tok_pos != (close+1) ) { *next_tok_pos = '\0'; next_tok = *next_tok_pos; } else { done = 1; } /* Make sure that the partial I/O is the last token */ if ( !done ) { fprintf(stderr,"Partial I/O must be the last operation in " "the expression"); break; } /* Get the rank of the dataset */ rank = slab_get_rank(open,close); start = malloc(rank*sizeof(int)); stride = malloc(rank*sizeof(int)); edge = malloc(rank*sizeof(int)); for ( j = 0; j < rank; j++ ) { start[j] = 0; stride[j] = 1; edge[j] = 1; } /* Get the start,stride,edge using matlab syntax */ slab_get_select(open,close,rank,start,stride,edge); /* Check if the users selection is valid and if so read the data */ if ((nmemb = slab_select_valid(rank,start,stride,edge,matvar))) { matvar->data_size = Mat_SizeOfClass(matvar->class_type); matvar->nbytes = nmemb*matvar->data_size; if ( matvar->isComplex ) { mat_complex_split_t *z; matvar->data = malloc(sizeof(*z)); z = matvar->data; z->Re = malloc(matvar->nbytes); z->Im = malloc(matvar->nbytes); } else { matvar->data = malloc(matvar->nbytes); } if ( matvar->data == NULL ) { fprintf(stderr,"Couldn't allocate memory for the data"); err = 1; } else if ( rank == 1 ) { Mat_VarReadDataLinear(mat,matvar,matvar->data,*start, *stride,*edge); if (matvar->rank == 2 && matvar->dims[0] == 1) { matvar->dims[1] = *edge; } else if (matvar->rank == 2 && matvar->dims[1] == 1) { matvar->dims[0] = *edge; } else { matvar->rank = 2; matvar->dims[0] = *edge; matvar->dims[1] = 1; } } else { Mat_VarReadData(mat,matvar,matvar->data,start,stride,edge); for ( i = 0; i < rank; i++ ) matvar->dims[i] = (size_t)edge[i]; } } free(start); free(stride); free(edge); } else if ( next_tok == '.' ) { matvar_t *field; char *varname; if ( matvar->class_type == MAT_C_STRUCT ) { varname = next_tok_pos+1; next_tok_pos = get_next_token(next_tok_pos+1); if ( next_tok_pos != varname ) { next_tok = *next_tok_pos; *next_tok_pos = '\0'; } else { done = 1; } /* FIXME: Handle structures > 1x1 */ field = Mat_VarGetStructFieldByName(matvar, varname, 0); if ( field == NULL ) { fprintf(stderr,"field %s was not found in structure %s", varname,matvar->name); break; } field = Mat_VarDuplicate(field,1); Mat_VarFree(matvar); matvar = field; } else if ( matvar->class_type == MAT_C_CELL ) { int ncells; matvar_t *cell, **cells; ncells = matvar->nbytes / matvar->data_size; cells = matvar->data; varname = next_tok_pos+1; next_tok_pos = get_next_token(next_tok_pos+1); if ( next_tok_pos != varname ) { next_tok = *next_tok_pos; *next_tok_pos = '\0'; } else { done = 1; } for ( j = 0 ; j < ncells; j++ ) { cell = Mat_VarGetCell(matvar,j); if ( cell == NULL || cell->class_type != MAT_C_STRUCT ) { fprintf(stderr,"cell index %d is not a structure",j); break; } else { /* FIXME: Handle structures > 1x1 */ field = Mat_VarGetStructFieldByName(cell,varname,0); if ( field == NULL ) { fprintf(stderr,"field %s was not found in " "structure %s",varname,matvar->name); break; } field = Mat_VarDuplicate(field,1); Mat_VarFree(cell); cells[j] = field; } } if ( j != ncells ) break; } else { fprintf(stderr,"%s is not a structure", varname); break; } } else if ( next_tok == '{' ) { int rank, *start, *stride, *edge,nmemb, err = 0; if ( matvar->class_type != MAT_C_CELL ) { fprintf(stderr,"Only Cell Arrays can index with {}"); break; } open = next_tok_pos; close = strchr(open+1,'}'); /* Get the next token after this selection */ next_tok_pos = get_next_token(close+1); if ( *next_tok_pos != '\0' ) { next_tok = *next_tok_pos; *next_tok_pos = '\0'; } else { done = 1; } /* Get the rank of the dataset */ rank = slab_get_rank(open,close); start = malloc(rank*sizeof(int)); stride = malloc(rank*sizeof(int)); edge = malloc(rank*sizeof(int)); for ( j = 0; j < rank; j++ ) { start[j] = 0; stride[j] = 1; edge[j] = 1; } /* Get the start,stride,edge using matlab syntax */ slab_get_select(open,close,rank,start,stride,edge); /* Check if the users selection is valid and if so read the data */ if ((nmemb = slab_select_valid(rank,start,stride,edge,matvar))) { matvar_t **cells, *tmp; if ( rank == 1 ) { cells = Mat_VarGetCellsLinear(matvar,*start, *stride,*edge); if (matvar->rank == 2 && matvar->dims[0] == 1) { matvar->dims[1] = *edge; } else if (matvar->rank == 2 && matvar->dims[1] == 1) { matvar->dims[0] = *edge; } else { matvar->rank = 1; matvar->dims[0] = *edge; } } else { cells = Mat_VarGetCells(matvar,start,stride,edge); memcpy(matvar->dims,edge,matvar->rank*sizeof(int)); } if ( cells == NULL ) { fprintf(stderr,"Error getting the indexed cells"); err = 1; } else { for ( j = 0; j < nmemb; j++ ) cells[j] = Mat_VarDuplicate(cells[j],1); tmp = Mat_VarCreate(matvar->name,MAT_C_CELL, MAT_T_CELL,matvar->rank,matvar->dims,cells, MAT_F_DONT_COPY_DATA); Mat_VarFree(matvar); matvar = tmp; } } else { fprintf(stderr,"Cell selection not valid"); err = 1; } free(start); free(stride); free(edge); if ( err ) break; } } }
void Job::run(){ if (Print) cout << endl << "------ new run ------ iRip==" << iRip << endl << endl; // int iTr=0,iTs=0,iVar,iV,iFRip, i,ii, trsz=ds.pr->TrSzD, tssz=ds.pr->TsSzD, valdim=ds.LabelValSelSize; double acc[trsz][tssz][FRip], mse[trsz][tssz][FRip], scc[trsz][tssz][FRip], featNum[FeatSelSize][FRip], *res,trends[valdim][trsz][FRip],actualLabel[valdim],ValidTrend[valdim]; matvar_t *varnameC; // for (iVar=0; iVar<LabelSelSize; ++iVar){ iV=LabelSelIdx[iVar]; // questa e` la variabile che vogliamo decodificare // -------------- cambio il nome del file in modo da coincidere con la variabile -------------------------------------- string resFile_=resFile,VARNAME; // cosi` lo scope e` locale in questo ciclo varnameC = Mat_VarGetCell(VARIABLEs,iV); // recupero il nome di questa variabile VARNAME=(const char*)varnameC->data; // segnalo dove sono a video if (Print) cout << endl << "------ iV=" << VARNAME << endl << endl; // resFile_+=VARNAME; resFile_+="-"; // riporto quale variabile sto esaminando nel filename resFile_+="nF_"; resFile_+=to_string(FeatSelSize); resFile_+="-"; // e quante features resFile_+="res.mat"; // aggiungo l'estensione // elimino gli spazi resFile_.erase(remove_if(resFile_.begin(), resFile_.end(), [](char x){return isspace(x);}), resFile_.end()); // -------------------------------------------------------------------------------------------------------------------- // // ---------------------------- recupero l'andamento della label sul validation set ---------------------------------- for (ii=0; ii<valdim; ++ii){ i=ds.label_valid_selection[ii]; actualLabel[ii]=labels[LabelSize[0]*iV+i]; // prendo l'i-esimo indice del del validation set della variabile iV // LabelSize[0] e` il numero totale delle istanze } // --------------------------------------------------------------------------------------------------------------------- // for (iTr=0; iTr<trsz; ++iTr){ ds.assegnoTrainingSet(iTr); for (iFRip=0; iFRip<FRip; ++iFRip){ if (Print) cout << endl << "------ iTr%=" << (double)iTr/trsz << endl << "------ iFRip%=" << (double)iFRip/FRip << endl << endl; UpdateFeatureSelection(); // cambio, se devo, la selezione delle features // // ----------------------------------------- recupero gli indici delle features --------------------------------------- for (i=0; i<FeatSelSize; ++i) // recupero gli indici delle feature che ho usato featNum[i][iFRip]=(double)(feature_sel[i]+1); // per metterla nel ws di matio // aggiungo 1.0 per come funziona l'indicizzazione su matlab // --------------------------------------------------------------------------------------------------------------------- // // per debug if (0){ cout << endl << "feat idx" << endl; for (int i=0; i<FeatSelSize; ++i) cout << " " << feature_sel[i]; cout << endl; } assegnato_svm=false; // devo riaddestrare per ogni Training Set o per ogni sottocampionamento delle features TrainingFromAssignedProblem(iV); // addestro con lo stesso Training Set ma (forse) diverse Features predictValidationSet(ValidTrend,iV); // Test sul Validation Set // ------------------------------- recupero i trends sul Validation Set ----------------------------------------- for (ii=0; ii<valdim; ++ii) trends[ii][iTr][iFRip]=ValidTrend[ii]; // -------------------------------------------------------------------------------------------------------------- // for (iTs=0; iTs<tssz; ++iTs){ ds.assegnoTestSet(iTs); predictTestSet(iV); // recupero le performance del modello res = (double *)((matvar_t *)RES[1])->data; acc[iTr][iTs][iFRip]=res[0]; mse[iTr][iTs][iFRip]=res[1]; scc[iTr][iTs][iFRip]=res[2]; } } } // -------------- salvo un file per ogni variabile | setto il workspace da salvare -------------------------------------- int WsSize=12; matvar_t *workspace[WsSize]; size_t dims_3[3], dims_2[2], dims_1[1]; dims_3[2]=trsz; dims_3[1]=tssz; dims_3[0]=FRip; dims_2[1]=FeatSelSize; dims_2[0]=FRip; dims_1[0]=1; workspace[0] = Mat_VarCreate("acc",MAT_C_DOUBLE,MAT_T_DOUBLE,3,dims_3,acc,0); workspace[1] = Mat_VarCreate("mse",MAT_C_DOUBLE,MAT_T_DOUBLE,3,dims_3,mse,0); workspace[2] = Mat_VarCreate("scc",MAT_C_DOUBLE,MAT_T_DOUBLE,3,dims_3,scc,0); workspace[3] = Mat_VarCreate("featIdx",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims_2,featNum,0); dims_2[1]=valdim; dims_2[0]=1; workspace[4] = Mat_VarCreate("actualLabel",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims_2,actualLabel,0); dims_3[2]=valdim; dims_3[1]=trsz; dims_3[0]=FRip; workspace[5] = Mat_VarCreate("trends",MAT_C_DOUBLE,MAT_T_DOUBLE,3,dims_3,trends,0); double FRip_ws[1], trsz_ws[1], tssz_ws[1], FeatSelSize_ws[1], validSz_ws[1]; FRip_ws[0]=FRip; trsz_ws[0]=trsz; tssz_ws[0]=tssz; FeatSelSize_ws[0]=FeatSelSize; validSz_ws[0]=valdim; workspace[6] = Mat_VarCreate("FeatSelectionRip",MAT_C_DOUBLE,MAT_T_DOUBLE,1,dims_1,FRip_ws,0); workspace[7] = Mat_VarCreate("NumFeatures",MAT_C_DOUBLE,MAT_T_DOUBLE,1,dims_1,FeatSelSize_ws,0); workspace[8] = Mat_VarCreate("TrSz",MAT_C_DOUBLE,MAT_T_DOUBLE,1,dims_1,trsz_ws,0); workspace[9] = Mat_VarCreate("TsSz",MAT_C_DOUBLE,MAT_T_DOUBLE,1,dims_1,tssz_ws,0); workspace[10] = Mat_VarCreate("ValidationSetSize",MAT_C_DOUBLE,MAT_T_DOUBLE,1,dims_1,validSz_ws,0); string help ="CONTENUTO DEL MATFILE:\n"; help+="{acc,mse,scc}\n"; help+="\tdimensione RxTsxTr\n"; help+="\t(accuratezza,mean squared error ed r2\n\toutput di libsvm)\n"; help+="featIdx\n"; help+="\tdimensione RxN\n"; help+="\t(indice delle features usate per addestrare\n\t il modello ad ogni ricampionamento)\n"; help+="actualLabel\n"; help+="\tdimensione 1xV\n"; help+="\t(valore della label corrispondente al validation set\n\tNB: salvo un file diverso per ogni label)\n"; help+="trends\n"; help+="\tdimensione RxTrxV\n"; help+="\t(predizione della label, ne ho una diverso per ogni modello\n\tNB: ho un modello diverso per ogni\n\t +ricampionamento delle features\n\t +ricampionamento del training set)\n"; help+="***LEGENDA***\n"; help+="\tR=FeatSelectionRip\n"; help+="\tTs=TsSz\n"; help+="\tTr=TrSz\n"; help+="\tN=NumFeatures\n"; help+="\tV=ValidationSetSize\n"; dims_2[1]=help.size(); dims_2[0]=1; workspace[11] = Mat_VarCreate("README",MAT_C_CHAR,MAT_T_UINT8,2,dims_2,(char*)help.c_str(),0); // Apro scrivo e chiudo il matfile mat_t *Out_MATFILE = Mat_CreateVer((const char*)resFile_.c_str(),NULL,MAT_FT_DEFAULT); for (int iWS=0; iWS<WsSize; ++iWS) Mat_VarWrite(Out_MATFILE, workspace[iWS], MAT_COMPRESSION_NONE); Mat_Close(Out_MATFILE); // ---------------------------------------------------------------------------------------------------------------------- } return; }