RcppExport SEXP getDeformationField (SEXP _transform, SEXP _jacobian) { BEGIN_RCPP RObject transform(_transform); RObject result; NiftiImage targetImage(SEXP(transform.attr("target"))); DeformationField field; if (transform.inherits("affine")) { AffineMatrix affine = AffineMatrix(SEXP(transform)); field = DeformationField(targetImage, affine); } else { NiftiImage transformationImage(_transform); field = DeformationField(targetImage, transformationImage); } result = field.getFieldImage().toPointer("Deformation field"); result.attr("source") = transform.attr("source"); result.attr("target") = transform.attr("target"); if (as<bool>(_jacobian)) result.attr("jacobian") = field.getJacobian().toPointer("Jacobian of deformation field"); return result; END_RCPP }
int variable(int index, readstat_variable_t *variable, const char *val_labels) { names_[index] = readstat_variable_get_name(variable); switch(readstat_variable_get_type(variable)) { case READSTAT_TYPE_LONG_STRING: case READSTAT_TYPE_STRING: output_[index] = CharacterVector(nrows_); break; case READSTAT_TYPE_CHAR: case READSTAT_TYPE_INT16: case READSTAT_TYPE_INT32: output_[index] = IntegerVector(nrows_); break; case READSTAT_TYPE_FLOAT: case READSTAT_TYPE_DOUBLE: output_[index] = NumericVector(nrows_); break; } RObject col = output_[index]; const char* var_label = readstat_variable_get_label(variable); if (var_label != NULL && strcmp(var_label, "") != 0) { col.attr("label") = CharacterVector::create(Rf_mkCharCE(var_label, CE_UTF8)); } if (val_labels != NULL) val_labels_[index] = val_labels; const char* var_format = readstat_variable_get_format(variable); VarType var_type = numType(type_, var_format); // Rcout << var_name << ": " << var_format << " [" << var_type << "]\n"; var_types_[index] = var_type; switch(var_type) { case HAVEN_DATE: col.attr("class") = "Date"; break; case HAVEN_TIME: col.attr("class") = "hms"; break; case HAVEN_DATETIME: col.attr("class") = CharacterVector::create("POSIXct", "POSIXt"); col.attr("tzone") = "UTC"; break; default: break; } return 0; }
// [[Rcpp::export]] List matrixToDataFrame(RObject x) { SEXPTYPE type = TYPEOF(x); if (!x.hasAttribute("dim")) stop("`x` is not a matrix"); IntegerVector dim = x.attr("dim"); if (dim.size() != 2) stop("`x` is not a matrix"); int nrow = dim[0], ncol = dim[1]; List out = List(ncol); for (int j = 0; j < ncol; ++j) { out[j] = Rf_allocVector(type, nrow); SEXP col = out[j]; Rf_copyMostAttrib(x, col); int offset = j * nrow; for (int i = 0; i < nrow; ++i) { switch(type) { case LGLSXP: case INTSXP: INTEGER(col)[i] = INTEGER(x)[offset + i]; break; case REALSXP: REAL(col)[i] = REAL(x)[offset + i]; break; case CPLXSXP: COMPLEX(col)[i] = COMPLEX(x)[offset + i]; break; case STRSXP: SET_STRING_ELT(col, i, STRING_ELT(x, offset + i)); break; case VECSXP: SET_VECTOR_ELT(col, i, VECTOR_ELT(x, offset + i)); break; } } } if (x.hasAttribute("dimnames")) { List dimnames = x.attr("dimnames"); out.attr("names") = dimnames[1]; } out.attr("class") = "data.frame"; out.attr("row.names") = IntegerVector::create(NA_INTEGER, -nrow); return out; }
// Extract the label attr from an R object // Only works for a character label (will crash in case of an integer value) std::string get_label(RObject x){ std::string label = "<EMPTY>"; if (x.hasAttribute("label")) { label = as<std::string>(x.attr( "label" )) ; } return label; // either TRUE or FALSE. But often, if it exists - it is TRUE. }
SEXP check_grouped(RObject data) { static SEXP groups_symbol = Rf_install("groups"); static SEXP vars_symbol = Rf_install("vars"); // compat with old style grouped data frames SEXP vars = Rf_getAttrib(data, vars_symbol); if (!Rf_isNull(vars)) { DataFrame groups = build_index_cpp(data, SymbolVector(vars)); data.attr("groups") = groups; } // get the groups attribute and check for consistency SEXP groups = Rf_getAttrib(data, groups_symbol); // groups must be a data frame if (!is<DataFrame>(groups)) { bad_arg(".data", "is a corrupt grouped_df, the `\"groups\"` attribute must be a data frame"); } // it must have at least 1 column int nc = Rf_length(groups); if (nc <= 1) { bad_arg(".data", "is a corrupt grouped_df, the `\"groups\"` attribute must have at least two columns"); } // the last column must be a list and called `.rows` SEXP names = Rf_getAttrib(groups, R_NamesSymbol); SEXP last = VECTOR_ELT(groups, nc - 1); static String rows = ".rows"; if (TYPEOF(last) != VECSXP || STRING_ELT(names, nc - 1) != rows) { bad_arg(".data", "is a corrupt grouped_df, the `\"groups\"` attribute must have a list column named `.rows` as last column"); } return data ; }
std::string rClass(RObject x) { RObject klass_ = x.attr("class"); std::string klass; if (klass_ == R_NilValue) return ""; CharacterVector klassv = as<Rcpp::CharacterVector>(klass_); return std::string(klassv[0]); }
List output() { for (int i = 0; i < output_.size(); ++i) { std::string label = val_labels_[i]; if (label == "") continue; if (label_sets_.count(label) == 0) continue; RObject col = output_[i]; col.attr("class") = "labelled"; col.attr("labels") = label_sets_[label].labels(); col.attr("is_na") = rep(false, label_sets_[label].size()); } output_.attr("names") = names_; output_.attr("class") = CharacterVector::create("tbl_df", "tbl", "data.frame"); output_.attr("row.names") = IntegerVector::create(NA_INTEGER, -nrows_); return output_; }
RcppExport SEXP composeTransforms (SEXP _transform1, SEXP _transform2) { BEGIN_RCPP RObject transform1(_transform1); RObject transform2(_transform2); RObject result; if (transform1.inherits("affine") && transform2.inherits("affine")) { Eigen::MatrixXd matrix = as<Eigen::MatrixXd>(_transform2) * as<Eigen::MatrixXd>(_transform1); result = AffineMatrix(matrix); } else { DeformationField field1, field2; NiftiImage targetImage1(SEXP(transform1.attr("target"))); NiftiImage targetImage2(SEXP(transform2.attr("target"))); if (transform1.inherits("affine")) { AffineMatrix transformMatrix(_transform1); field1 = DeformationField(targetImage1, transformMatrix, true); } else { NiftiImage transformImage(_transform1); field1 = DeformationField(targetImage1, transformImage, true); } if (transform2.inherits("affine")) { AffineMatrix transformMatrix(_transform2); field2 = DeformationField(targetImage2, transformMatrix, true); } else { NiftiImage transformImage(_transform2); field2 = DeformationField(targetImage2, transformImage, true); } // Order of composition is possibly not as expected field2.compose(field1); result = field2.getFieldImage().toPointer("Deformation field"); } result.attr("source") = transform1.attr("source"); result.attr("target") = transform2.attr("target"); return result; END_RCPP }
const ConvertersVec get_converters( const NameClassMapping& reference_class, RObject tf, DataFrameLike data, HashFunction* _h_main, HashFunction* _h_binary, size_t hash_size ) { NumericMatrix tfactors(wrap(tf.attr("factors"))); CharacterVector reference_name, feature_name; Environment feature_hashing(Environment::namespace_env("FeatureHashing")); Function parse_split(feature_hashing["parse_split"]); std::set<int> specials; { List tmp(tf.attr("specials")); SEXP ptag = tmp["split"]; if (!Rf_isNull(ptag)) { IntegerVector tmpvec(ptag); specials.insert(tmpvec.begin(), tmpvec.end()); } } { List tmp(tfactors.attr("dimnames")); reference_name = CharacterVector(tmp[0]); // rownames feature_name = CharacterVector(tmp[1]); // colnames } std::vector< pVectorConverter > retval; for(int i = 0;i < feature_name.size();i++) { bool is_interaction = false; for(int j = 0;j < reference_name.size();j++) { if (tfactors(j, i) == 0) continue; std::string rname(as<std::string>(reference_name[j])); #ifdef NOISY_DEBUG Rprintf("%s -> ", rname.c_str()); #endif pVectorConverter p(NULL); try{ if (specials.find(j + 1) == specials.end()) { if (reference_class.find(rname) == reference_class.end()) throw std::invalid_argument("Failed to find the column:"); const std::string& rclass(reference_class.find(rname)->second); #ifdef NOISY_DEBUG Rprintf("%s\n", rclass.c_str()); #endif Param param(rname, _h_main, _h_binary, hash_size); if (rclass.compare("factor") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize FactorConverter\n"); #endif p.reset(new FactorConverter(wrap(data[rname.c_str()]), param)); } else if (rclass.compare("numeric") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize NumConverter\n"); #endif p.reset(new NumConverter(wrap(data[rname.c_str()]), param)); } else if (rclass.compare("integer") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize IntConverter\n"); #endif p.reset(new IntConverter(wrap(data[rname.c_str()]), param)); } else if (rclass.compare("logical") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize LogicalConverter\n"); #endif p.reset(new LogicalConverter(wrap(data[rname.c_str()]), param)); } else if (rclass.compare("character") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize CharacterConverter\n"); #endif p.reset(new CharacterConverter(wrap(data[rname.c_str()]), param)); } else { throw std::invalid_argument("Non supported type at name: "); } } else { #ifdef NOISY_DEBUG Rprintf(" (parsing tag..) "); #endif List expression(parse_split(wrap(rname))); rname.assign(as<std::string>(expression["reference_name"])); Param param(rname, _h_main, _h_binary, hash_size); #ifdef NOISY_DEBUG Rprintf(" (rname ==> %s) ", rname.c_str()); #endif if (reference_class.find(rname) == reference_class.end()) throw std::invalid_argument("Failed to find the column: "); const std::string& rclass(reference_class.find(rname)->second); #ifdef NOISY_DEBUG Rprintf("%s\n", rclass.c_str()); #endif std::string delim(as<std::string>(expression["delim"])), type(as<std::string>(expression["type"])); #ifdef NOISY_DEBUG Rprintf("delim: %s type: %s\n", delim.c_str(), type.c_str()); #endif if (rclass.compare("factor") == 0) { if (type.compare("existence") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize TagExistenceFactorConverter\n"); #endif p.reset(new TagExistenceFactorConverter(wrap(data[rname.c_str()]), param, delim)); } else if (type.compare("count") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize TagCountFactorConverter\n"); #endif p.reset(new TagCountFactorConverter(wrap(data[rname.c_str()]), param, delim)); } else { throw std::invalid_argument("Non supported type at name: "); } } else if (rclass.compare("character") == 0) { if (type.compare("existence") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize TagExistenceCharacterConverter\n"); #endif p.reset(new TagExistenceCharacterConverter(wrap(data[rname.c_str()]), param, delim)); } else if (type.compare("count") == 0) { #ifdef NOISY_DEBUG Rprintf("Initialize TagCountCharacterConverter\n"); #endif p.reset(new TagCountCharacterConverter(wrap(data[rname.c_str()]), param, delim)); } else { throw std::invalid_argument("Non supported type at name: "); } } else { throw std::invalid_argument("Non supported type at name: "); } } } catch(std::invalid_argument& e) { std::string message(e.what()); message.append(rname); throw std::invalid_argument(message); } if (!is_interaction) { retval.push_back(p); is_interaction = true; } else { pVectorConverter q(*retval.rbegin()); Param param("", _h_main, _h_binary, hash_size); *retval.rbegin() = pInteractionConverter(new InteractionConverter(q, p, param)); } } } return retval; }
SEXP hashed_model_matrix(RObject tf, DataFrameLike data, unsigned long hash_size, bool transpose, S4 retval, bool keep_hashing_mapping, bool is_xi, bool progress) { if (hash_size > 4294967296) throw std::invalid_argument("hash_size is too big!"); NameClassMapping reference_class(get_class(data)); Environment e(Environment::base_env().new_child(wrap(true))); std::shared_ptr<HashFunction> pHF(NULL), pBHF(NULL); if (keep_hashing_mapping) { pHF.reset(new MurmurHash3LogHashFunction(wrap(e), MURMURHASH3_H_SEED)); } else { pHF.reset(new MurmurHash3HashFunction(MURMURHASH3_H_SEED)); } if (is_xi) pBHF.reset(new MurmurHash3HashFunction(MURMURHASH3_XI_SEED)); else pBHF.reset(new NullHashFunction); ConvertersVec converters(get_converters(reference_class, tf, data, pHF.get(), pBHF.get(), hash_size)); #ifdef NOISY_DEBUG Rprintf("The size of convertres is %d\n", converters.size()); #endif std::vector<int> ivec, pvec(1, 0); std::vector<double> xvec; bool is_intercept = as<bool>(tf.attr("intercept")); #ifdef NOISY_DEBUG Rprintf("nrow(data): %d length(converters): %d\n", data.nrows(), converters.size()); #endif std::shared_ptr<boost::progress_display> pd(NULL); if (transpose) { if (progress) pd.reset(new boost::progress_display(data.nrows(), Rcpp::Rcout)); for(auto i = 0;i < data.nrows();i++) { if (progress) ++(*pd); if (is_intercept) { ivec.push_back(0); xvec.push_back(1.0); } for(auto j = converters.begin();j != converters.end();j++) { pVectorConverter& p(*j); const std::vector<uint32_t>& i_origin(p->get_feature(i)); const std::vector<double>& x_origin(p->get_value(i)); #ifdef NOISY_DEBUG std::for_each(i_origin.begin(), i_origin.end(), [&hash_size](uint32_t hashed_value) { Rprintf("(%zu module %d = %d),", hashed_value, hash_size, hashed_value % hash_size); }); Rprintf("\n"); #endif std::for_each(i_origin.begin(), i_origin.end(), [&ivec, &xvec, &hash_size](uint32_t hashed_value) { ivec.push_back(hashed_value); }); xvec.insert(xvec.end(), x_origin.begin(), x_origin.end()); } pvec.push_back(ivec.size()); } } else { if (progress) pd.reset(new boost::progress_display(data.nrows(), Rcpp::Rcout)); std::map< uint32_t, std::pair< std::vector<int>, std::vector<double> > > cache; if (is_intercept) { std::pair< std::vector<int>, std::vector<double> >& k(cache[0]); k.first.resize(data.nrows()); for(int i = 0;i < data.nrows();i++) { k.first[i] = i; } k.second.resize(data.nrows(), 1.0); } for(auto i = 0;i < data.nrows();i++) { if (progress) ++(*pd); for(auto j = converters.begin();j != converters.end();j++) { pVectorConverter& p(*j); const std::vector<uint32_t>& i_origin(p->get_feature(i)); const std::vector<double>& x_origin(p->get_value(i)); auto x_value = x_origin.begin(); std::for_each(i_origin.begin(), i_origin.end(), [&cache, &hash_size, &x_value, &i](uint32_t hashed_value) { std::pair< std::vector<int>, std::vector<double> >& k(cache[hashed_value]); k.first.push_back(i); k.second.push_back(*(x_value++)); }); } } int pvec_value = ivec.size(); for(auto i = cache.begin();i != cache.end();i++) { while(pvec.size() <= i->first) pvec.push_back(pvec_value); ivec.insert(ivec.end(), i->second.first.begin(), i->second.first.end()); { std::vector<int> tmp; i->second.first.swap(tmp); } xvec.insert(xvec.end(), i->second.second.begin(), i->second.second.end()); { std::vector<double> tmp; i->second.second.swap(tmp); } pvec_value = ivec.size(); } pvec.resize(hash_size + 1, pvec_value); } retval.slot("i") = wrap(ivec); retval.slot("p") = wrap(pvec); retval.slot("x") = wrap(xvec); IntegerVector dim(2); if (transpose) { dim[0] = hash_size; dim[1] = pvec.size() - 1; retval.slot("Dim") = dim; } else { dim[0] = data.nrows(); dim[1] = hash_size; retval.slot("Dim") = dim; } { List dimnames(2); dimnames[0] = CharacterVector(0); dimnames[1] = CharacterVector(0); retval.slot("Dimnames") = dimnames; } retval.slot("factors") = List(); { CharacterVector key(e.ls(true)); std::for_each(key.begin(), key.end(), [&e, &hash_size](const char* s) { uint32_t *p = (uint32_t*) INTEGER(e[s]); p[0] = p[0] % hash_size; }); } retval.attr("mapping") = e; return retval; }
RcppExport SEXP halfTransform (SEXP _transform) { BEGIN_RCPP RObject transform(_transform); RObject result; if (transform.inherits("affine")) { Eigen::MatrixXd matrix = as<Eigen::MatrixXd>(_transform); matrix = (matrix.log() * 0.5).exp(); result = AffineMatrix(matrix); } else { NiftiImage transformationImage(_transform); switch (reg_round(transformationImage->intent_p1)) { case SPLINE_GRID: reg_getDisplacementFromDeformation(transformationImage); reg_tools_multiplyValueToImage(transformationImage,transformationImage,0.5f); reg_getDeformationFromDisplacement(transformationImage); break; case DEF_FIELD: reg_getDisplacementFromDeformation(transformationImage); reg_tools_multiplyValueToImage(transformationImage,transformationImage,0.5f); reg_getDeformationFromDisplacement(transformationImage); break; case DISP_FIELD: reg_tools_multiplyValueToImage(transformationImage,transformationImage,0.5f); break; case SPLINE_VEL_GRID: reg_getDisplacementFromDeformation(transformationImage); reg_tools_multiplyValueToImage(transformationImage,transformationImage,0.5f); reg_getDeformationFromDisplacement(transformationImage); --transformationImage->intent_p2; if (transformationImage->num_ext>1) --transformationImage->num_ext; break; case DEF_VEL_FIELD: reg_getDisplacementFromDeformation(transformationImage); reg_tools_multiplyValueToImage(transformationImage,transformationImage,0.5f); reg_getDeformationFromDisplacement(transformationImage); --transformationImage->intent_p2; break; case DISP_VEL_FIELD: reg_tools_multiplyValueToImage(transformationImage,transformationImage,0.5f); --transformationImage->intent_p2; break; default: throw std::runtime_error("The specified transformation image is not valid or not supported"); } result = transformationImage.toPointer("F3D transformation"); } result.attr("source") = transform.attr("source"); result.attr("target") = transform.attr("target"); return result; END_RCPP }
// [[Rcpp::export]] List readStructurized(std::string fname) { ifstream file(fname.c_str(), ifstream::binary); List types; List blocks; Function DB_channels("DB.channels", "R3"); Function DB_message("DB.event", "R3"); try { if(file) { char head[3]; file.read(head,3); if(head[0]!='!'||head[1]!='R'||head[2]!='3') { Rf_warning("Wrong header"); return List(); } SerializedData sd(readFromFile, &file); bool packed = sd.extractField<FileHeader::packed>(); if(packed) return List(); // @todo: support packed files for(;;) { SerializedData item(readFromFile, &file); switch(item.recordId()) { case File_Stream::ID: { int id = item.extractField<File_Stream::id>(); SerializedData info = item.extractAny<File_Stream::info>(); SerializedData fields = info.extractAny<ConnectionHeader::description>(); if(info.recordId() == ConnectionHeader::ID) { switch(info.extractField<ConnectionHeader::type>()) { case Double::ID: { List si = List::create( Named("id") = id, Named("name") = item.extractString<File_Stream::name>(), Named("channels") = fields.extractField<ConnectionHeader_Double::channels>(), Named("samplingRate") = fields.extractField<ConnectionHeader_Double::samplingRate>(), Named("type") = "channels" ); rs_addType(types, si, id); } break; case Resonance::R3::Int32::ID: { List si = List::create( Named("id") = id, Named("name") = item.extractString<File_Stream::name>(), Named("channels") = fields.extractField<ConnectionHeader_Int32::channels>(), Named("samplingRate") = fields.extractField<ConnectionHeader_Int32::samplingRate>(), Named("type") = "channels" ); rs_addType(types, si, id); } break; case Message::ID: { List si = List::create( Named("id") = id, Named("name") = item.extractString<File_Stream::name>(), Named("type") = "event" ); rs_addType(types, si, id);; } break; } } } break; case File_DataBlock::ID: { int stream = item.extractField<File_DataBlock::stream>(); SerializedData data = item.extractAny<File_DataBlock::block>(); switch(data.recordId()) { case Double::ID: { std::vector<double> raw(data.extractVector<Double::data>()); RObject db = DB_channels(types[stream], (double)data.extractField<Double::created>()/1E3, raw); db.attr("created") = (double)data.extractField<Double::created>()/1E3; db.attr("received") = (double)data.extractField<Double::received>()/1E3; blocks.push_back(db); } break; case Resonance::R3::Int32::ID: { std::vector<int> raw(data.extractVector<Resonance::R3::Int32::data>()); RObject db = DB_channels(types[stream], (double)data.extractField<Resonance::R3::Int32::created>()/1E3, raw); db.attr("created") = (double)data.extractField<Resonance::R3::Int32::created>()/1E3; db.attr("received") = (double)data.extractField<Resonance::R3::Int32::received>()/1E3; blocks.push_back(db); } break; case Message::ID: { CharacterVector raw(data.extractString<Message::message>()); RObject db = DB_message(types[stream], (double)data.extractField<Message::created>()/1E3, raw); db.attr("created") = (double)data.extractField<Message::created>()/1E3; db.attr("received") = (double)data.extractField<Message::received>()/1E3; blocks.push_back(db); } break; } } break; } } } } catch(const Eof &f) { } catch(...) { Rf_warning("Bugs (%i blocks readed)", blocks.length()); /*std::cout << "\nUnknown exception type: '" << (__cxa_current_exception_type()->name()) << "'" << std::endl; int status; std::cout << "f**k: " << abi::__cxa_demangle(__cxa_current_exception_type()->name(), 0, 0, &status) << std::endl;*/ } return List::create( Named("streams") = types, Named("blocks") = blocks ); }