// Function to return a selected part of a data frame as a list List ch5ChunkSel(string chunkName, CharacterVector selCols, string filePath) { // Open the file in Read/Write Mode, H5F_ACC_RDONLY H5File *file = new H5File(filePath, H5F_ACC_RDONLY); // Opening the data set DataSet dataset = file->openDataSet((H5std_string)chunkName); // Opening the data space DataSpace dataspace = dataset.getSpace(); // Get the number of dimensions int ndim = dataspace.getSimpleExtentNdims(); // Create a dimension object to be filled with the dimensions of the data set hsize_t dims[ndim]; // Fill the dimension of the dataset dataspace.getSimpleExtentDims(dims, NULL); // Create the return data // Filling the matrix with data form the dataspace SEXP data; // Allocating a matrix of the right size and dimension data = PROTECT(Rf_allocMatrix(REALSXP, dims[0], dims[1])); // Filling the matrix with data form the dataspace dataset.read(REAL(data), PredType::NATIVE_DOUBLE, dataspace); UNPROTECT(1); // converting the R object to a numeric matrix NumericMatrix M = as<NumericMatrix>(data); CharacterVector colNames = ch5ReadCharVector("ColumnNames", filePath); CharacterVector colClasses = ch5ReadCharVector("ColumnClasses", filePath); // Create the output List DF; string colName; string colClass; NumericVector vec; CharacterVector levels; int n = selCols.size(); IntegerVector sel(n); int selN; // First we need to find which of the columns has been selected sel = match(selCols, colNames); for(int i = 0; i < n; i++) { colName = selCols[i]; selN = sel[i] - 1; colClass = colClasses[selN]; if(colClass != "factor") { DF[colName] = M(_, selN); }else{ vec = M(_, selN); levels = (CharacterVector)ch5ReadFactor(colName, filePath); DF[colName] = cCreateFactor(vec, levels); } } dataset.close(); file->close(); return DF; }
void BkgParamH5::InitBeads_BestRegion( H5File &h5_local_ref, int nBeads_live, const Region *region) { char buff[80]; //cout << "BkgParamH5::InitBeads_BestRegion... bestRegion=" << bestRegion.first << "," << bestRegion.second << endl << flush; // only once (the first flow, not all flows) beads_bestRegion_timeframe.InitBasicCube (h5_local_ref,1,max_frames,1,"/bestRegion/timeframe", "Time Frame", "frameNumber"); beads_bestRegion_location.InitBasicCube (h5_local_ref,nBeads_live,2,1,"/bestRegion/location", "y(row),x(col) for each bead", "row,col" ); beads_bestRegion_gainSens.InitBasicCube(h5_local_ref,nBeads_live,1,1,"/bestRegion/gainSens","gain*sens",""); beads_bestRegion_kmult.InitBasicCube(h5_local_ref,nBeads_live,1,1,"/bestRegion/kmult","kMult",""); beads_bestRegion_dmult.InitBasicCube(h5_local_ref,nBeads_live,1,1,"/bestRegion/dmult","dMult",""); beads_bestRegion_SP.InitBasicCube(h5_local_ref,nBeads_live,1,1,"/bestRegion/SP","SP: copies*copy_multiplier",""); beads_bestRegion_R.InitBasicCube(h5_local_ref,nBeads_live,1,1,"/bestRegion/R","R: ratio of bead buffering to empty buffering",""); // all flows beads_bestRegion_predicted.InitBasicCube(h5_local_ref,nBeads_live,max_frames,datacube_numflows,"/bestRegion/predicted","predicted trace",""); beads_bestRegion_corrected.InitBasicCube(h5_local_ref,nBeads_live,max_frames,datacube_numflows,"/bestRegion/corrected","background-adjusted trace",""); beads_bestRegion_amplitude.InitBasicCube(h5_local_ref,nBeads_live,1,datacube_numflows,"/bestRegion/amplitude","amplititude",""); beads_bestRegion_residual.InitBasicCube(h5_local_ref,nBeads_live,1,datacube_numflows,"/bestRegion/residual","residual error",""); beads_bestRegion_fittype.InitBasicCube(h5_local_ref,nBeads_live,1,datacube_numflows,"/bestRegion/fittype","fittype",""); beads_bestRegion_taub.InitBasicCube(h5_local_ref,nBeads_live,1,datacube_numflows,"/bestRegion/taub","taub",""); sprintf(buff,"%d",nBeads_live); h5_local_ref.CreateAttribute(beads_bestRegion_location.h5_set->getDataSetId(),"nBeads_live",buff); sprintf(buff,"%d",region->h); h5_local_ref.CreateAttribute(beads_bestRegion_location.h5_set->getDataSetId(),"region_h",buff); sprintf(buff,"%d",region->w); h5_local_ref.CreateAttribute(beads_bestRegion_location.h5_set->getDataSetId(),"region_w",buff); sprintf(buff,"%d",region->row); h5_local_ref.CreateAttribute(beads_bestRegion_location.h5_set->getDataSetId(),"region_y(row)",buff); sprintf(buff,"%d",region->col); h5_local_ref.CreateAttribute(beads_bestRegion_location.h5_set->getDataSetId(),"region_x(col)",buff); }
//'@title Function to write a matrix chunk to file //' //'@description Function is intended for internal use //' //'@param dset character denoting the meta data name of the data set //'@param chunk matrix that will be written to h5file //'@param dim numeric containing the dimension of the matrix that will be written to file //'@param filePath character denoting the location of the h5 file //'@return int 0 // [[Rcpp::export]] int h5WriteDoubleMat (std::string dset, SEXP chunk, NumericVector dim, std::string filePath) { H5File *file = new H5File(filePath, H5F_ACC_RDWR); // Data initialization. int rank = 2; hsize_t dims[rank]; // dataset dimensions for(int k = 0; k < rank; k++) dims[k] = dim(k); const void *buf = REAL(chunk); // Create the data space for the dataset. DataSpace dataspace (rank, dims, NULL); // Create the dataset. H5std_string dsetName(dset); DataSet dataset = file->createDataSet(dsetName, PredType::NATIVE_DOUBLE, dataspace); // Write the data to the dataset using default memory space, file // space, and transfer properties. dataset.write(buf, PredType::NATIVE_DOUBLE); dataset.close(); //nn file->close(); return 0; }
CharacterVector ch5ReadFactor(string charName, string filePath) { H5File *file = new H5File(filePath, H5F_ACC_RDONLY); // Group Meta Group Group* metaGroup = new Group(file->openGroup("/MetaData/Factor")); // Getting the data set from the file DataSet dataset = metaGroup->openDataSet((H5std_string)charName); // Getting the data space from the dataset DataSpace dataspace = dataset.getSpace(); // We know that it is a char vector array so ndim = 1 hsize_t dims[1]; // Getting the length of strings dataspace.getSimpleExtentDims(dims, NULL); // for convenience int dim = dims[0]; // String Type StrType vlst(0, H5T_VARIABLE); // Returning the data char *strRet[dim]; dataset.read(strRet, vlst); CharacterVector out(dim); for(int i = 0; i < dim; i++) { out[i] = strRet[i]; } dataset.close(); //nn metaGroup->close(); file->close(); return out; }
int TestCompress() { unsigned int flags = 0; unsigned int config = 0; size_t cd_nelemts = 0; TESTING("compression") #ifdef H5_HAVE_FILTER_DEFLATE try { /* Create packet table with compression. */ FL_PacketTable wrapper(fileID, "/compressTest", H5T_NATIVE_CHAR, 100, 8); /* Create an HDF5 C++ file object */ H5File file; file.setId(fileID); /* Make sure that the deflate filter is set by opening the packet table * as a dataset and getting its creation property list */ DataSet dsetID = file.openDataSet("/compressTest"); DSetCreatPropList dcplID = dsetID.getCreatePlist(); dcplID.getFilterById(H5Z_FILTER_DEFLATE, flags, cd_nelemts, NULL, 0, NULL, config); } catch (Exception e) { H5_FAILED(); return 1; } PASSED(); #else SKIPPED(); puts(" deflate filter not enabled"); #endif /* H5_HAVE_FILTER_DEFLATE */ return 0; }
//'@title This function writes an integer meta data to file //' //'@description This function is inteded for internal use //' //'@param intName the name of the meta data item to be written //'@param integer int that will be written to the meta data described by intName //'@param filePath character path to the h5 file where data will be written //'@param update int flag for whether item is new (0) or whether it will overwrite a previous item (1) //'@return int 0 // [[Rcpp::export]] int h5WriteInt(std::string intName, int integer, std::string filePath, int update) { H5File *file = new H5File(filePath, H5F_ACC_RDWR); // Colclasses dim hsize_t dim[1] = {1}; string meta = "/MetaData"; // Group Meta Group Group* metaGroup = new Group(file->openGroup(meta)); // dataspace DataSpace dataspace = DataSpace(1, dim); DataSet dataset; if(update == 1) { string slash = "/"; string groupName = meta + slash + intName; file->unlink(groupName); } dataset = metaGroup->createDataSet(intName, PredType::NATIVE_INT, dataspace); dataset.write(&integer, PredType::NATIVE_INT); dataset.close(); //nn metaGroup->close(); file->close(); return 0; }
Tuplef Wrapper_i_hdf::get_dims()const { Tuplef tmp; H5File file = H5File( file_name_, H5F_ACC_RDONLY ); Group group = Group(file.openGroup("/")); Attr_list_hdf attr_list(&group); if(!attr_list.contains_attr("version")) throw invalid_argument("input file does not have a version"); int file_version = 0; if(attr_list.get_value("version",file_version) != 1) throw invalid_argument("input file is not the right version"); if(attr_list.contains_attr("dims")) { attr_list.get_value("dims",tmp); } group.close(); file.close(); return Tuplef(tmp); }
//'@title Function creates the groups for the meta data to be written to the h5 file //' //'@description Function to create the groups in the h5 file before it is populated. //'This function is intended for internal use only //' //'@param filePath character path to the location where the h5 file will be written //'@return int 0 // [[Rcpp::export]] int h5CreateMetaData(std::string filePath) { H5File *file = new H5File(filePath, H5F_ACC_RDWR); hsize_t dims[1] = {1}; // The variable length string type StrType vlst(0, H5T_VARIABLE); // Creating the meta data group Group *metaGroup = new Group(file->createGroup("/MetaData")); // File path H5std_string fString("FilePath"); DataSpace fileDataSpace (1, dims, NULL); DataSet fileDataSet; // Create a dataset in the group fileDataSet = metaGroup->createDataSet(fString, vlst, fileDataSpace); fileDataSet.write(filePath, vlst); // Create the factor group Group *factorGroup = new Group(metaGroup->createGroup("/MetaData/Factor")); fileDataSet.close(); //nn factorGroup->close(); metaGroup->close(); file->close(); return 0; }
//'@title Function for dummy read //' //'@param chunkName the name of the chunk to be read back //'@param filePath the path to the h5 file //'@return int 0 // [[Rcpp::export]] int h5DummyRead(std::string chunkName, std::string filePath) { // Open the file in Read/Write Mode, H5F_ACC_RDONLY H5File *file = new H5File(filePath, H5F_ACC_RDONLY); // Opening the data set DataSet dataset = file->openDataSet((H5std_string)chunkName); // Opening the data space DataSpace dataspace = dataset.getSpace(); // Get the number of dimensions int ndim = dataspace.getSimpleExtentNdims(); // Create a dimension object to be filled with the dimensions of the data set hsize_t dims[ndim]; // Fill the dimension of the dataset dataspace.getSimpleExtentDims(dims, NULL); // Create the return data SEXP data; // Allocating a matrix of the right size and dimension data = PROTECT(Rf_allocMatrix(REALSXP, dims[0], dims[1])); // Filling the matrix with data form the dataspace dataset.read(REAL(data), PredType::NATIVE_DOUBLE, dataspace); UNPROTECT(1); dataset.close(); file->close(); return 0; }
vector<string> get_feature_names(H5File h5f) { vector<string> out; int num_features; for (int i = 0; i < h5f.getNumObjs(); i++) out.push_back(h5f.getObjnameByIdx(i)); return out; }
//'@title Legacy function to return a data frame chunk as a list //' //'@description Experimental function not intended for use at all //' //'@param chunkName the name of the chunk to be read //'@param filePath the path to the h5 file //'@return List of the data frame chunk // [[Rcpp::export]] SEXP h5ReadDoubleMat3(std::string chunkName, std::string filePath) { // Open the file in Read/Write Mode, H5F_ACC_RDONLY H5File *file = new H5File(filePath, H5F_ACC_RDONLY); // Opening the data set DataSet dataset = file->openDataSet((H5std_string)chunkName); // Opening the data space DataSpace dataspace = dataset.getSpace(); // Get the number of dimensions int ndim = dataspace.getSimpleExtentNdims(); // Create a dimension object to be filled with the dimensions of the data set hsize_t dims[ndim]; // Fill the dimension of the dataset dataspace.getSimpleExtentDims(dims, NULL); // Create the return data // Filling the matrix with data form the dataspace //double (*buf)[dims[1]]*[dims[0]] = malloc(dims[1]]*[dims[0] * sizeof *buf); //buf[dims[1]][dims[0]] = 0.0; double **buf = (double**) calloc (dims[1]*dims[0], sizeof(double)); buf[dims[1]][dims[0]] = 0.0; //double buf[dims[1]][dims[0]]; dataset.read(buf, PredType::NATIVE_DOUBLE, dataspace); // Attempt tp append the contents to a list List out; NumericVector vec(dims[0]); NumericMatrix M(dims[0], dims[1]); CharacterVector colNames = ch5ReadCharVector("ColumnNames", filePath); CharacterVector colClasses = ch5ReadCharVector("ColumnClasses", filePath); string colName; for(int i = 0; i < dims[1]; i++) { NumericVector vec(dims[0]); for(int j = 0; j < dims[0]; j++) { M(j,i) = buf[i][j]; vec(j) = buf[i][j]; } colName = colNames[i]; if(colClasses[i] == "factor") { CharacterVector levels; levels = h5ReadFactor(colName, filePath); IntegerVector fact(vec.size()); fact = cCreateFactor(vec, levels); out[colName] = fact; }else{ out[colName] = vec; } } free(buf); dataset.close(); //nn file->close(); return wrap(out); }
TEST(H5Handle_Test, WriteReadTest) { float data[12]; for (size_t i = 0; i < ArraySize(data); i++) { data[i] = i; } { H5File file; file.SetFile("tmp.h5"); file.Open(true); hsize_t dims[] = {3,4}; hsize_t chunking[] = {3,4}; H5DataSet *ds = file.CreateDataSet("/tmp/here/testdata", 2, dims, chunking, 2, H5T_NATIVE_FLOAT); size_t starts[] = {0,0}; size_t ends[] = {3,4}; //ds->WriteRangeData(starts, ends, ArraySize(data), data); ds->WriteRangeData(starts, ends, data); ds->Close(); } { H5File file; file.SetFile("tmp.h5"); file.Open(); H5DataSet *ds = file.OpenDataSet("/tmp/here/testdata"); float query[12]; size_t starts[] = {0,0}; size_t ends[] = {3,4}; ds->ReadRangeData(starts, ends, ArraySize(query), query); for (size_t i = 0; i < ArraySize(data); i++) { if (data[i] != query[i]) { EXPECT_EQ(query[i], data[i]); } } { float update[4]; size_t starts[] = {1,2}; size_t ends[] = {3,4}; size_t count = 0; for (size_t i = 11; i >= 8; i--) { update[count++] = i; } //ds->WriteRangeData(starts, ends, ArraySize(update), update); ds->WriteRangeData(starts, ends, update); float query[12]; size_t rstarts[] = {0,0}; size_t rends[] = {3,4}; float gold[12] = {0,1,2,3,4,5,11,10,8,9,9,8}; ds->ReadRangeData(rstarts, rends, ArraySize(query), query); for (size_t i = 0; i < 1; i++) { EXPECT_EQ(gold[i], query[i]); } } // ds->Close(); ds = file.OpenDataSet("shouldbenull"); EXPECT_EQ(NULL,ds); } }
//'@title Legacy function to return a data frame chunk as a list //' //'@description Experimental function not intended for use at all //' //'@param chunkName the name of the chunk to be read //'@param filePath the path to the h5 file //'@return List of the data frame chunk // [[Rcpp::export]] SEXP h5ReadDoubleMat2(std::string chunkName, std::string filePath) { // Open the file in Read/Write Mode, H5F_ACC_RDONLY H5File *file = new H5File(filePath, H5F_ACC_RDONLY); // Opening the data set DataSet dataset = file->openDataSet((H5std_string)chunkName); // Opening the data space DataSpace dataspace = dataset.getSpace(); // Get the number of dimensions int ndim = dataspace.getSimpleExtentNdims(); // Create a dimension object to be filled with the dimensions of the data set hsize_t dims[ndim]; // Fill the dimension of the dataset dataspace.getSimpleExtentDims(dims, NULL); // Create the return data // Filling the matrix with data form the dataspace SEXP data; // Allocating a matrix of the right size and dimension data = PROTECT(Rf_allocMatrix(REALSXP, dims[0], dims[1])); // Filling the matrix with data form the dataspace dataset.read(REAL(data), PredType::NATIVE_DOUBLE, dataspace); UNPROTECT(1); // converting the R object to a numeric matrix NumericMatrix M = as<NumericMatrix>(data); List out; NumericVector vec(dims[0]); CharacterVector colNames = ch5ReadCharVector("ColumnNames", filePath); CharacterVector colClasses = ch5ReadCharVector("ColumnClasses", filePath); string colName; for(int i = 0; i < dims[1]; i++) { NumericVector vec(dims[0]); for(int j = 0; j < dims[0]; j++) { vec(j) = M(j,i); } colName = colNames[i]; if(colClasses[i] == "factor") { CharacterVector levels; levels = ch5ReadFactor(colName, filePath); IntegerVector fact(vec.size()); fact = cCreateFactor(vec, levels); out[colName] = fact; }else{ out[colName] = vec; } } dataset.close(); //nn file->close(); // Returning the data return wrap(out); }
void BkgParamH5::ConstructOneFile ( H5File &h5_local_ref, string &hgLocalFile, string &local_results, const char *my_name ) { hgLocalFile = local_results; if ( hgLocalFile[hgLocalFile.length()-1] != '/' ) hgLocalFile += '/'; hgLocalFile += my_name; cout << "H5File for params:" << hgLocalFile << endl; h5_local_ref.Init(); h5_local_ref.SetFile ( hgLocalFile ); h5_local_ref.Open ( true ); }
void TraceChunkSerializer::ReadInfo(H5File &h5, SynchDat &sdat) { sdat.mInfo.Clear(); vector<string> keys; vector<string> values; h5.ReadStringVector(INFO_KEYS, keys); h5.ReadStringVector(INFO_VALUES, values); if (keys.size() != values.size()) { ION_ABORT("Keys and Values don't match in size."); } for (size_t i = 0; i < keys.size(); i++) { if (!sdat.mInfo.SetValue(keys[i], values[i])) { ION_ABORT("Error: Could not set key: " + keys[i] + " with value: " + values[i]); } } }
Md_store utilities::extract_prams(const std::string & fname,int comp_num,const vector<string> &pram_list) { // hdf stuff H5File file = H5File( fname, H5F_ACC_RDONLY ); Group group = file.openGroup("/parameters/" + format_dset_name(utilities::D_XPOS,comp_num)); Attr_list_hdf attr_list(&group); Md_store filter_prams; for(vector<string>::const_iterator it = pram_list.begin(); it <pram_list.end(); ++it) { string pram = *it; if(!attr_list.contains_attr(pram)) throw runtime_error("the field " + pram + " is not in the file"); utilities::V_TYPE vtype = attr_list.get_type(pram); int tmpi; unsigned int tmpui; float tmpf; switch(vtype) { case V_UINT: filter_prams.add_element(pram.c_str(),attr_list.get_value(pram,tmpui)); break; case V_INT: filter_prams.add_element(pram.c_str(),attr_list.get_value(pram,tmpi)); break; case V_FLOAT: filter_prams.add_element(pram.c_str(),attr_list.get_value(pram,tmpf)); break; case V_ERROR: case V_COMPLEX: case V_STRING: case V_BOOL: case V_TIME: case V_GUID: throw runtime_error("the field " + pram + " is of type " + VT2str_s(vtype) + " which is not implemented yet."); } } // return a copy of the assembled parameters return Md_store(filter_prams); }
void writeScalar(H5File &file, string DSitem, Scalar value){ hsize_t dim[0]; DataSpace dsp(0, dim); PredType type = getPredType<Scalar>(); DataSet item = file.createDataSet(DSitem, type, dsp); item.write(&value, type); }
void RotatingCube::RotateMyCube ( H5File &h5_local_ref, int total_blocks, const char *cube_name, const char *cube_description ) { char s[128]; string str; for ( int i=0; i<total_blocks; i++ ) { sprintf ( s,"%s/block_%04d",cube_name, i ); H5DataSet *ptr = h5_local_ref.CreateDataSet ( s, source, 3 ); sprintf ( s,"%s",cube_description ); h5_local_ref.CreateAttribute ( ptr->getDataSetId(),"description",s ); h5_local_ref.makeParamNames ( "ndx_",source.GetNumZ(),str ); h5_local_ref.CreateAttribute ( ptr->getDataSetId(),"paramNames",str.c_str() ); h5_vec.push_back ( ptr ); } }
int main(int argc, char ** argv) { if (argc < 3){ cout << "Usage:" << argv[0] << " <filename> <outputfilename> - display some statistics of specified synapse." << endl; return 0; } const H5std_string FILE_NAME(argv[1]); ofstream outfile; outfile.open(argv[2]); try{ H5File * file = new H5File(FILE_NAME, H5F_ACC_RDONLY); Group * netgroup = new Group(file->openGroup("network")); const DataSet * syndataset = new DataSet(netgroup->openDataSet("synapse")); synstat(*syndataset, outfile); file->close(); outfile.close(); } catch( FileIException error ) { error.printError(); return -1; } // catch failure caused by the DataSet operations catch( DataSetIException error ) { error.printError(); return -1; } // catch failure caused by the DataSpace operations catch( DataSpaceIException error ) { error.printError(); return -1; } // catch failure caused by the DataSpace operations catch( DataTypeIException error ) { error.printError(); return -1; } return 0; }
//-------------------------------------------------------------------------- // Function: Group overload constructor - dereference ///\brief Given a reference, ref, to an hdf5 group, creates a Group object ///\param h5file - IN: Location referenced object is in ///\param ref - IN: Reference pointer ///\param ref_type - IN: Reference type - default to H5R_OBJECT ///\exception H5::ReferenceException // Programmer Binh-Minh Ribler - Oct, 2006 //-------------------------------------------------------------------------- Group::Group(H5File& h5file, const void* ref, H5R_type_t ref_type) : H5Object() { try { id = p_dereference(h5file.getId(), ref, ref_type); } catch (ReferenceException deref_error) { throw ReferenceException("Group constructor - located by an H5File", deref_error.getDetailMsg()); } }
//-------------------------------------------------------------------------- // Function: DataSet overload constructor - dereference ///\brief Given a reference, ref, to an hdf5 dataset, creates a /// DataSet object ///\param h5file - IN: Location referenced object is in ///\param ref - IN: Reference pointer ///\param ref_type - IN: Reference type - default to H5R_OBJECT ///\exception H5::DataSetIException // Programmer Binh-Minh Ribler - Oct, 2006 // Modification // Jul, 2008 // Added for application convenience. //-------------------------------------------------------------------------- DataSet::DataSet(H5File& h5file, const void* ref, H5R_type_t ref_type) : H5Object(), AbstractDs() { try { id = p_dereference(h5file.getId(), ref, ref_type); } catch (ReferenceException deref_error) { throw ReferenceException("DataSet constructor - located by HDF5 file", deref_error.getDetailMsg()); } }
//'@title Function to create file a h5 file //' //'@description Function to create a h5 file. It is intended for internal use only //' //'@param filePath a character denoting the path to the location where the h5 file will be written //'@param overwrite integer 1 for overwrite and 0 for not overwrite. Will fail if overwrite is 0 //'@return int 0 // [[Rcpp::export]] int h5CreateFile(std::string filePath, int overwrite) { H5File* file; if(fileExists(filePath)) { if(overwrite) { file = new H5File(filePath, H5F_ACC_TRUNC); }else{ throw "Error: file exists and overwrite is set to 0."; } }else{ file = new H5File(filePath, H5F_ACC_TRUNC); } file->close(); return 0; }
void read_feature_size(H5File h5f, Size &size_out, const char *name) { DataSet dataset = h5f.openDataSet(name); DataSpace dspace = dataset.getSpace(); assert (dspace.getSimpleExtentNdims() == 2); hsize_t dims[2]; dspace.getSimpleExtentDims(dims); size_out.height = dims[0]; size_out.width = dims[1]; }
//-------------------------------------------------------------------------- // Function: H5Object::dereference ///\brief Dereferences a reference into an HDF5 object, given an HDF5 file. ///\param h5file - IN: HDF5 file specifying the location of the referenced object ///\param ref - IN: Reference pointer ///\param ref_type - IN: Reference type ///\exception H5::ReferenceException // Programmer Binh-Minh Ribler - Oct, 2006 // Modification // May, 2008 // Corrected missing parameters. - BMR //-------------------------------------------------------------------------- void H5Object::dereference(H5File& h5file, const void* ref, H5R_type_t ref_type) { hid_t temp_id; try { temp_id = p_dereference(h5file.getId(), ref, ref_type); } catch (ReferenceException E) { throw ReferenceException("H5Object::dereference - located by file", E.getDetailMsg()); } p_setId(temp_id); }
Scalar readScalar(H5File &file, string DSitem){ DataSet item = file.openDataSet(DSitem); DataSpace dsp = item.getSpace(); // assert(dsp.getSimpleExtentNdims() == 0); // hsize_t dims[1]; // int ndims = dsp.getSimpleExtentDims(dims, NULL); Scalar value(0); PredType type = getPredType<Scalar>(); item.read(&value, type); return value; }
// set up a basic data cube + matched h5 set //@TODO: use templates to avoid "INT" duplication void MatchedCubeInt::InitBasicCube ( H5File &h5_local_ref, int col, int row, int maxflows, const char *set_name, const char *set_description, const char *param_root ) { //printf ( "%s\n",set_name ); string str; source.Init ( col, row, maxflows ); source.SetRange ( 0,col, 0, row, 0, maxflows ); source.AllocateBuffer(); h5_set = h5_local_ref.CreateDataSet ( set_name, source, 3 ); h5_local_ref.CreateAttribute ( h5_set->getDataSetId(),"description",set_description ); // either we're just using the axis for nothing special if ( strlen ( param_root ) <1 ) { h5_local_ref.makeParamNames ( "ndx_",maxflows, str ); } else { // or we have a specific set of variables for this axis str = param_root; } h5_local_ref.CreateAttribute ( h5_set->getDataSetId(),"paramNames",str.c_str() ); }
//'@title Function to read an integer item from meta data //' //'@param intName character for the name of the item to be read back //'@param filePath character for the path to the h5 file //'@return int iteger item defined by intName in the meta data // [[Rcpp::export]] int h5ReadInt(std::string intName, std::string filePath) { H5File *file = new H5File(filePath, H5F_ACC_RDONLY); // Group Meta Group Group* metaGroup = new Group(file->openGroup("/MetaData")); // Getting the data set from the file DataSet dataset = metaGroup->openDataSet((H5std_string)intName); // Getting the data space from the dataset DataSpace dataspace = dataset.getSpace(); // Returning the data int intRet; dataset.read(&intRet, PredType::NATIVE_INT); dataset.close(); //nn metaGroup->close(); file->close(); return intRet; }
void write_hdf5_image(H5File h5f, const char *name, const Mat &im) { DSetCreatPropList cparms; hsize_t chunk_dims[2] = {256, 256}; hsize_t dims[2]; dims[0] = im.size().height; dims[1] = im.size().width; if (chunk_dims[0] > dims[0]) { chunk_dims[0] = dims[0]; } if (chunk_dims[1] > dims[1]) { chunk_dims[1] = dims[1]; } cparms.setChunk(2, chunk_dims); cparms.setShuffle(); cparms.setDeflate(5); DataSet dataset = h5f.createDataSet(name, PredType::NATIVE_FLOAT, DataSpace(2, dims, dims), cparms); Mat image; if (im.type() != CV_32F) im.convertTo(image, CV_32F); else image = im; DataSpace imspace; float *imdata; if (image.isContinuous()) { imspace = dataset.getSpace(); // same size as imspace.selectAll(); imdata = image.ptr<float>(); } else { // we are working with an ROI assert (image.isSubmatrix()); Size parent_size; Point parent_ofs; image.locateROI(parent_size, parent_ofs); hsize_t parent_count[2]; parent_count[0] = parent_size.height; parent_count[1] = parent_size.width; imspace.setExtentSimple(2, parent_count); hsize_t im_offset[2], im_size[2]; im_offset[0] = parent_ofs.y; im_offset[1] = parent_ofs.x; im_size[0] = image.size().height; im_size[1] = image.size().width; imspace.selectHyperslab(H5S_SELECT_SET, im_size, im_offset); imdata = image.ptr<float>() - parent_ofs.x - parent_ofs.y * parent_size.width; } dataset.write(imdata, PredType::NATIVE_FLOAT, imspace); }
//'@title This function writes a character vector to the meta data //' //'@description This function writes a character vector to the meta data and is intended for internal use. //' //'@param charName the name that will be given to the meta data character vector //'@param charVec the character vector to be written as meta data //'@param filePath the path to the h5 file where the data will be written //'@param update integer denoting whether the data item is new or whether it is an update //'(which will overwrite any previous item) //'@return int 0 // [[Rcpp::export]] int h5WriteCharVector(std::string charName, SEXP charVec, std::string filePath, int update) { H5File *file = new H5File(filePath, H5F_ACC_RDWR); int len = Rf_length(charVec); hsize_t DIM1 = len; int rank = 1; //cout << "The length is ... " << len << endl; // Create a datatype to refer to StrType vlst(0, H5T_VARIABLE); // This is the char array char** arr = convertCharArray(charVec); string meta = "/MetaData"; // Group Meta Group Group* metaGroup = new Group(file->openGroup(meta)); // The dataset and dataspace hsize_t dims[] = {DIM1}; //hsize_t maxdims[] = {H5S_UNLIMITED}; DataSet dataset; if(update == 1) { string slash = "/"; string groupName = meta + slash + charName; file->unlink(groupName); } DataSpace dataspace(rank, dims); dataset = metaGroup->createDataSet(charName, vlst, dataspace); dataset.write(arr, vlst); dataset.close(); //nn metaGroup->close(); file->close(); return 0; }
bool TraceChunkSerializer::Write(H5File &h5, GridMesh<TraceChunk> &dataMesh) { mNumChunks = dataMesh.GetNumBin(); ClockTimer timer; mChunks = (struct FlowChunk *) malloc(sizeof(struct FlowChunk) * mNumChunks); ArrangeDataForWriting(dataMesh, mChunks); computeMicroSec = timer.GetMicroSec(); hsize_t dims1[1]; dims1[0] = mNumChunks; hid_t fcDataSpace = H5Screate_simple(1, dims1, NULL); hid_t charArrayType = H5Tvlen_create (H5T_NATIVE_CHAR); hid_t charArrayType2 = H5Tvlen_create (H5T_NATIVE_CHAR); hid_t fcType = H5Tcreate(H5T_COMPOUND, sizeof(struct FlowChunk)); H5Tinsert(fcType, "CompressionType", HOFFSET(struct FlowChunk, CompressionType), H5T_NATIVE_B64); H5Tinsert(fcType, "ChipRow", HOFFSET(struct FlowChunk, ChipRow), H5T_NATIVE_B64); H5Tinsert(fcType, "ChipCol", HOFFSET(struct FlowChunk, ChipCol), H5T_NATIVE_B64); H5Tinsert(fcType, "ChipFrame", HOFFSET(struct FlowChunk, ChipFrame), H5T_NATIVE_B64); H5Tinsert(fcType, "RowStart", HOFFSET(struct FlowChunk, RowStart), H5T_NATIVE_B64); H5Tinsert(fcType, "ColStart", HOFFSET(struct FlowChunk, ColStart), H5T_NATIVE_B64); H5Tinsert(fcType, "FrameStart", HOFFSET(struct FlowChunk, FrameStart), H5T_NATIVE_B64); H5Tinsert(fcType, "FrameStep", HOFFSET(struct FlowChunk, FrameStep), H5T_NATIVE_B64); H5Tinsert(fcType, "Height", HOFFSET(struct FlowChunk, Height), H5T_NATIVE_B64); H5Tinsert(fcType, "Width", HOFFSET(struct FlowChunk, Width), H5T_NATIVE_B64); H5Tinsert(fcType, "Depth", HOFFSET(struct FlowChunk, Depth), H5T_NATIVE_B64); H5Tinsert(fcType, "OrigFrames", HOFFSET(struct FlowChunk, OrigFrames), H5T_NATIVE_B64); H5Tinsert(fcType, "StartDetailedTime", HOFFSET(struct FlowChunk, StartDetailedTime), H5T_NATIVE_INT); H5Tinsert(fcType, "StopDetailedTime", HOFFSET(struct FlowChunk, StopDetailedTime), H5T_NATIVE_INT); H5Tinsert(fcType, "LeftAvg", HOFFSET(struct FlowChunk, LeftAvg), H5T_NATIVE_INT); H5Tinsert(fcType, "T0", HOFFSET(struct FlowChunk, T0), H5T_NATIVE_FLOAT); H5Tinsert(fcType, "Sigma", HOFFSET(struct FlowChunk, Sigma), H5T_NATIVE_FLOAT); H5Tinsert(fcType, "TMidNuc", HOFFSET(struct FlowChunk, TMidNuc), H5T_NATIVE_FLOAT); H5Tinsert(fcType, "BaseFrameRate", HOFFSET(struct FlowChunk, BaseFrameRate), H5T_NATIVE_FLOAT); H5Tinsert(fcType, "DeltaFrame", HOFFSET(struct FlowChunk, DeltaFrame), charArrayType2); H5Tinsert(fcType, "Data", HOFFSET(struct FlowChunk, Data), charArrayType); timer.StartTimer(); hid_t dataset = H5Dcreate2(h5.GetFileId(), "FlowChunk", fcType, fcDataSpace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); herr_t status = H5Dwrite(dataset, fcType, H5S_ALL, H5S_ALL, H5P_DEFAULT, mChunks); status = H5Dvlen_reclaim(fcType, fcDataSpace, H5P_DEFAULT, mChunks); // // delete [] mChunks; free(mChunks); mChunks = NULL; ION_ASSERT(status == 0, "Couldn't write dataset"); H5Tclose(fcType); H5Tclose(charArrayType); H5Tclose(charArrayType2); H5Sclose(fcDataSpace); H5Dclose(dataset); ioMicroSec += timer.GetMicroSec(); return status == 0; }