Skip to content
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

Merged
merged 4 commits into from
Mar 31, 2021

Conversation

plusangel
Copy link
Contributor

I included an example to demonstrate how references work in HighFive. PR suggested by @tdegeus

Thank you guys

Copy link
Collaborator

@tdegeus tdegeus left a 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?

Comment on lines 20 to 22
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");
Copy link
Collaborator

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 ;)

Copy link
Contributor

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;
Copy link
Collaborator

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
Copy link
Collaborator

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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"mygroup"?

Copy link
Contributor

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.

Comment on lines +39 to +40
// let's create a dataset of native integer with the size of the vector
// 'data' inside the group
Copy link
Collaborator

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

src/examples/read_write_vector_dataset_references.cpp Outdated Show resolved Hide resolved
#include <highfive/H5File.hpp>
#include <highfive/H5Reference.hpp>

using namespace HighFive;
Copy link
Collaborator

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.

Copy link
Contributor

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.

@ohm314 ohm314 self-requested a review March 17, 2021 12:53
Copy link
Contributor

@ohm314 ohm314 left a 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;
Copy link
Contributor

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const std::string SOURCE_INT_DATASET_NAME("source_dateset");
const std::string SOURCE_INT_DATASET_NAME("source_dataset");

Comment on lines 20 to 22
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");
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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");
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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

@plusangel
Copy link
Contributor Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c), 2017, Adrien Devresse
* Copyright (c), 2021 Blue Brain Project - EPFL

Comment on lines 18 to 22
// 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;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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;

Copy link
Contributor

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

Comment on lines 27 to 29
HighFive::File file("dataset_integer.h5", HighFive::File::ReadWrite |
HighFive::File::Create |
HighFive::File::Truncate);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
HighFive::File file("dataset_integer.h5", HighFive::File::ReadWrite |
HighFive::File::Create |
HighFive::File::Truncate);
HighFive::File file("dataset_integer.h5", HighFive::File::Overwrite);

Comment on lines 34 to 37
std::vector<int> data(20);
for (size_t i = 0; i < data.size(); ++i) {
data[i] = int(i);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Comment on lines 41 to 43
HighFive::DataSet dataset = group.createDataSet<int>("source_dateset",
HighFive::DataSpace::From(data));
dataset.write(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Comment on lines 50 to 52
HighFive::DataSet ref_set = group.createDataSet<HighFive::Reference>(
"reference_dataset", HighFive::DataSpace::From(ref_container));
ref_set.write(ref_container);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (HighFive::Exception& err) {
} catch (const HighFive::Exception& err) {

@ferdonline
Copy link
Contributor

@plusangel Do you have an estimate on when you could address this PR suggestions?

@plusangel
Copy link
Contributor Author

Sorry for the delay @ferdonline. Done :)

ferdonline
ferdonline previously approved these changes Mar 31, 2021
@ferdonline ferdonline requested a review from ohm314 March 31, 2021 16:01
Copy link
Contributor

@ohm314 ohm314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ferdonline ferdonline merged commit 1841062 into BlueBrain:master Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants