-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An example using hdf5 references (#396) #397
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm probably not the best person to review, because I don't actually know references in HDF5. That being said, it didn't become clear from this example either... Some line of text would surely help there: describe what the example is doing in a few lines of comment somewhere.
Where I really got lost though is with the std::vector<Reference>
. The question is: Are reference in HDF5 something similar to those in C++? If so, wouldn't it be easier to (also) have a basic example with one reference?
const std::string FILE_NAME("dataset_integer.h5"); | ||
const std::string SOURCE_INT_DATASET_NAME("source_dateset"); | ||
const std::string REFERENCE_DATASET_NAME("reference_dateset"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'm not a fan of these 'defines' (although I know that it's done in other examples). I'm not sure if it helps understanding the example. So I would probably use strings as argument below, especially when the string is used only once.
Regardless, it would be more consistent to use 'defines' everywhere or nowhere. I.e. if it is kept like this, the group name should also be defined this way.
Also I would propose that you use dataset
not dateset
;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind either. Defining the strings doesn't hurt, but doesn't add much. Either is fine.
const std::string FILE_NAME("dataset_integer.h5"); | ||
const std::string SOURCE_INT_DATASET_NAME("source_dateset"); | ||
const std::string REFERENCE_DATASET_NAME("reference_dateset"); | ||
const size_t size_dataset = 20; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, does it really make the example more comprehensible to define it separately here?
const std::string REFERENCE_DATASET_NAME("reference_dateset"); | ||
const size_t size_dataset = 20; | ||
|
||
// create a dataset 1D from a vector of string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that that is true, you specify a vector of ints
File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate); | ||
|
||
// we create a new group | ||
Group group = file.createGroup("a_group"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"mygroup"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you define const strings for the file name and dataset names, but not the group. I would be consistent, whatever you choose.
// let's create a dataset of native integer with the size of the vector | ||
// 'data' inside the group |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that this is true either. You actually write the data too, you are not only allocating some 'empty' vector
#include <highfive/H5File.hpp> | ||
#include <highfive/H5Reference.hpp> | ||
|
||
using namespace HighFive; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that this is done in other examples too. However, personally I find this an absolute no-go. If one is new to the library (and/or relatively new to C++) this will create nothing but confusion. If it about the line-length one can always alias from something short.
Probably the others will argue that one should stay consistent with other examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, please remove the using namespace declaration. As @tdegeus said, this just makes things less clear and is generally a bad practice that should be avoided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @plusangel, sorry for taking so long to review this. Overall the example is small enough and a good idea to add. We actually have in the unit tests already something very similar (but I understand that looking at test cases is a pain, especially when you're just getting into the library).
@tdegeus gave already some very good feedback. Could I ask you to please implement his and my suggestions into an additional patch. I"ll try to then re-review fairly quickly and have this merged!
Here's the test case, for reference: https://github.com/BlueBrain/HighFive/blob/master/tests/unit/tests_high_five_base.cpp#L1587
#include <highfive/H5File.hpp> | ||
#include <highfive/H5Reference.hpp> | ||
|
||
using namespace HighFive; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, please remove the using namespace declaration. As @tdegeus said, this just makes things less clear and is generally a bad practice that should be avoided.
using namespace HighFive; | ||
|
||
const std::string FILE_NAME("dataset_integer.h5"); | ||
const std::string SOURCE_INT_DATASET_NAME("source_dateset"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const std::string SOURCE_INT_DATASET_NAME("source_dateset"); | |
const std::string SOURCE_INT_DATASET_NAME("source_dataset"); |
const std::string FILE_NAME("dataset_integer.h5"); | ||
const std::string SOURCE_INT_DATASET_NAME("source_dateset"); | ||
const std::string REFERENCE_DATASET_NAME("reference_dateset"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind either. Defining the strings doesn't hurt, but doesn't add much. Either is fine.
const std::string REFERENCE_DATASET_NAME("reference_dateset"); | ||
const size_t size_dataset = 20; | ||
|
||
// create a dataset 1D from a vector of string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// create a dataset 1D from a vector of string | |
// create a dataset 1D from a vector of int |
File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate); | ||
|
||
// we create a new group | ||
Group group = file.createGroup("a_group"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you define const strings for the file name and dataset names, but not the group. I would be consistent, whatever you choose.
Reference ref = Reference(group, dataset); | ||
std::vector<Reference> ref_container{ref}; | ||
|
||
// in similar fashion, we store as dataset the reference that we want |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// in similar fashion, we store as dataset the reference that we want | |
// in similar fashion, we store as dataset the vector of references that we want |
Thank you both for your valuable comments and your time to review it. I think I catch them all. Keep up the good work. Thank you! |
@@ -0,0 +1,97 @@ | |||
/* | |||
* Copyright (c), 2017, Adrien Devresse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Copyright (c), 2017, Adrien Devresse | |
* Copyright (c), 2021 Blue Brain Project - EPFL |
// const std::string FILE_NAME("dataset_integer.h5"); | ||
//const std::string SOURCE_INT_DATASET_NAME("source_dateset"); | ||
//const std::string REFERENCE_DATASET_NAME("reference_dataset"); | ||
//const size_t size_dataset = 20; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// const std::string FILE_NAME("dataset_integer.h5"); | |
//const std::string SOURCE_INT_DATASET_NAME("source_dateset"); | |
//const std::string REFERENCE_DATASET_NAME("reference_dataset"); | |
//const size_t size_dataset = 20; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally liked the definitions in the header, but if you don't use them delete the code
HighFive::File file("dataset_integer.h5", HighFive::File::ReadWrite | | ||
HighFive::File::Create | | ||
HighFive::File::Truncate); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HighFive::File file("dataset_integer.h5", HighFive::File::ReadWrite | | |
HighFive::File::Create | | |
HighFive::File::Truncate); | |
HighFive::File file("dataset_integer.h5", HighFive::File::Overwrite); |
std::vector<int> data(20); | ||
for (size_t i = 0; i < data.size(); ++i) { | ||
data[i] = int(i); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::vector<int> data(20); | |
for (size_t i = 0; i < data.size(); ++i) { | |
data[i] = int(i); | |
} | |
std::vector<int> data(20); | |
std::iota(data.begin(), data.end(), 0); |
HighFive::DataSet dataset = group.createDataSet<int>("source_dateset", | ||
HighFive::DataSpace::From(data)); | ||
dataset.write(data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HighFive::DataSet dataset = group.createDataSet<int>("source_dateset", | |
HighFive::DataSpace::From(data)); | |
dataset.write(data); | |
auto dataset = group.createDataSet("source_dataset", data); |
} | ||
|
||
return 0; // successfully terminated | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | |
} | |
HighFive::DataSet ref_set = group.createDataSet<HighFive::Reference>( | ||
"reference_dataset", HighFive::DataSpace::From(ref_container)); | ||
ref_set.write(ref_container); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HighFive::DataSet ref_set = group.createDataSet<HighFive::Reference>( | |
"reference_dataset", HighFive::DataSpace::From(ref_container)); | |
ref_set.write(ref_container); | |
HighFive::DataSet ref_set = group.createDataSet("reference_dataset", ref_container); |
write_dataset(); | ||
read_dataset(); | ||
|
||
} catch (HighFive::Exception& err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} catch (HighFive::Exception& err) { | |
} catch (const HighFive::Exception& err) { |
@plusangel Do you have an estimate on when you could address this PR suggestions? |
Sorry for the delay @ferdonline. Done :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
I included an example to demonstrate how references work in HighFive. PR suggested by @tdegeus
Thank you guys