Skip to content

Commit b6e2aaa

Browse files
authored
Merge pull request #672 from UWB-Biocomputing/issue-655-add-InitTermToHdf5Recorder
implement the init() and term() for Hdf5 Recorder
2 parents c12ab88 + 2ef0a4c commit b6e2aaa

File tree

4 files changed

+88
-51
lines changed

4 files changed

+88
-51
lines changed

Simulator/Connections/Neuro/ConnGrowth.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#include "ParseParamError.h"
4747

4848
#ifdef USE_HDF5
49-
//#include "Hdf5GrowthRecorder.h"
5049
#include "Hdf5Recorder.h"
5150
#endif
5251

Simulator/Recorders/Hdf5Recorder.cpp

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,82 @@ Hdf5Recorder::Hdf5Recorder()
3030
// Initialize the logger for file operations
3131
fileLogger_ = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("file"));
3232

33+
// Initialize the HDF5 file object to nullptr
34+
// This is the HDF5 file (H5File) object.
3335
resultOut_ = nullptr;
34-
// I just comment this for now because something wrong with my init function
35-
//init();
36+
}
37+
38+
// destructor
39+
Hdf5Recorder::~Hdf5Recorder()
40+
{
41+
term();
3642
}
3743

3844
// Other member functions implementation...
3945
void Hdf5Recorder::init()
4046
{
4147
// Check the output file extension is .h5
42-
/*string suffix = ".h5";
43-
if ((resultFileName_.size() <= suffix.size()) ||
44-
(resultFileName_.compare(resultFileName_.size() - suffix.size(), suffix.size(), suffix) != 0)) {
45-
std::cerr << "The file extension is not .h5" << std::endl;
46-
exit(EXIT_FAILURE);
47-
}
48+
string suffix = ".h5";
49+
if ((resultFileName_.size() <= suffix.size())
50+
|| (resultFileName_.compare(resultFileName_.size() - suffix.size(), suffix.size(), suffix)
51+
!= 0)) {
52+
//perror("the file extention is not .h5 ");
53+
string errorMsg
54+
= "Error: the file extension is not .h5. Provided file name: " + resultFileName_;
55+
perror(errorMsg.c_str());
56+
exit(EXIT_FAILURE);
57+
}
4858

49-
// Check if we can create and write to the file
50-
std::ofstream testFileWrite(resultFileName_);
51-
if (!testFileWrite.is_open()) {
52-
std::cerr << "Error opening output file for writing" << std::endl;
53-
exit(EXIT_FAILURE);
54-
}
55-
testFileWrite.close();
59+
// Check if we can create and write to the file
60+
ofstream testFileWrite(resultFileName_.c_str());
61+
if (!testFileWrite.is_open()) {
62+
perror("Error opening output file for writing ");
63+
exit(EXIT_FAILURE);
64+
}
65+
testFileWrite.close();
66+
67+
try {
68+
// Create a new file using the default property lists
69+
resultOut_ = new H5File(resultFileName_, H5F_ACC_TRUNC);
70+
initDataSet();
71+
} catch (FileIException &error) {
72+
cerr << "HDF5 File I/O Exception: " << endl;
73+
error.printErrorStack();
74+
return;
75+
} catch (DataSetIException &error) {
76+
cerr << "HDF5 Dataset Exception: " << endl;
77+
error.printErrorStack();
78+
return;
79+
} catch (DataSpaceIException &error) {
80+
cerr << "HDF5 Dataspace Exception: " << endl;
81+
error.printErrorStack();
82+
return;
83+
} catch (DataTypeIException &error) {
84+
cerr << "HDF5 Datatype Exception: " << endl;
85+
error.printErrorStack();
86+
return;
87+
}
88+
}
5689

57-
try {
58-
// Create a new file using the default property lists
59-
resultOut_ = H5File(resultFileName_, H5F_ACC_TRUNC);
60-
initDataSet();
61-
} catch (const FileIException& error) {
62-
std::cerr << "HDF5 File I/O Exception: ";
63-
error.printErrorStack();
64-
return;
65-
} catch (const DataSetIException& error) {
66-
std::cerr << "HDF5 Dataset Exception: ";
67-
error.printErrorStack();
68-
return;
69-
} catch (const DataSpaceIException& error) {
70-
std::cerr << "HDF5 Dataspace Exception: ";
71-
error.printErrorStack();
72-
return;
73-
} catch (const DataTypeIException& error) {
74-
std::cerr << "HDF5 Datatype Exception: ";
75-
error.printErrorStack();
76-
return;
77-
}*/
90+
// This method closes the HDF5 file and releases any associated resources
91+
void Hdf5Recorder::term()
92+
{
93+
// checks if the file object `resultOut_` is not null, then attempts to close the file and delete the object
94+
if (resultOut_ != nullptr) {
95+
try {
96+
resultOut_->close();
97+
delete resultOut_;
98+
resultOut_ = nullptr;
99+
} catch (FileIException &error) {
100+
// If an exception occurs during this process, it prints the error stack for debugging purposes
101+
cerr << "HDF5 File I/O Exception during termination: ";
102+
error.printErrorStack();
103+
}
104+
}
105+
}
106+
107+
// Create data spaces and data sets of the hdf5 for recording histories.
108+
void Hdf5Recorder::initDataSet()
109+
{
78110
}
79111
#endif // HDF5

Simulator/Recorders/Hdf5Recorder.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using std::vector;
2323
class Hdf5Recorder : public Recorder {
2424
public:
2525
Hdf5Recorder();
26+
virtual ~Hdf5Recorder();
2627

2728
/**
2829
* @brief Factory method to create a new HDF5Recorder instance.
@@ -55,9 +56,7 @@ class Hdf5Recorder : public Recorder {
5556
}
5657

5758
/// Terminate process
58-
virtual void term() override
59-
{
60-
}
59+
virtual void term() override;
6160

6261
// TODO: No parameters needed (AllVertices &vertices)
6362
/// Compile/capture variable history information in every epoch
@@ -105,9 +104,7 @@ class Hdf5Recorder : public Recorder {
105104
}
106105

107106
private:
108-
virtual void initDataSet()
109-
{
110-
}
107+
virtual void initDataSet();
111108

112109
/// Populates Starter neuron matrix based with boolean values based on starterMap state
113110
///@param[in] matrix starter neuron matrix

Testing/UnitTesting/Hdf5RecorderTests.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,26 @@ TEST(Hdf5RecorderTest, CreateInstanceSuccess)
1111
ASSERT_TRUE(recorder != nullptr);
1212
}
1313

14-
// Test case for open file successfully
15-
TEST(Hdf5RecorderTest, Hdf5InitTest)
14+
// Test case for init() and term()
15+
TEST(Hdf5RecorderTest, Hdf5InitAndTermTest)
1616
{
17-
// Create an instance of Hdf5Recorder
18-
std::string outputFile = "../Testing/UnitTesting/TestOutput/Hdf5test_output.h5";
17+
// Create an instance of Hdf5Recorder with a specific output file name
18+
std::string outputFile = "../Testing/UnitTesting/TestOutput/Hdf5test_output_term.h5";
1919
Hdf5Recorder recorder(outputFile);
20-
/*recorder.init();
21-
// Test to see if output file exist
22-
FILE *f = fopen("../Testing/UnitTesting/TestOutput/Hdf5test_output.h5", "r");
20+
recorder.init();
21+
22+
// Ensure the file has been created successfully by the constructor
23+
FILE *f = fopen(outputFile.c_str(), "r");
24+
ASSERT_TRUE(f != NULL);
25+
fclose(f);
26+
27+
// Call the term() method to close the HDF5 file
28+
recorder.term();
29+
30+
// Check if the file can be reopened, indicating it was closed properly
31+
f = fopen(outputFile.c_str(), "r");
2332
bool fileExist = f != NULL;
2433
fclose(f);
25-
ASSERT_TRUE(fileExist);*/
34+
ASSERT_TRUE(fileExist);
2635
}
2736
#endif // HDF5

0 commit comments

Comments
 (0)