Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Simulator/Connections/Neuro/ConnGrowth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include "ParseParamError.h"

#ifdef USE_HDF5
//#include "Hdf5GrowthRecorder.h"
#include "Hdf5Recorder.h"
#endif

Expand Down
104 changes: 68 additions & 36 deletions Simulator/Recorders/Hdf5Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,82 @@ Hdf5Recorder::Hdf5Recorder()
// Initialize the logger for file operations
fileLogger_ = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("file"));

// Initialize the HDF5 file object to nullptr
// This is the HDF5 file (H5File) object.
resultOut_ = nullptr;
// I just comment this for now because something wrong with my init function
//init();
}

// destructor
Hdf5Recorder::~Hdf5Recorder()
{
term();
}

// Other member functions implementation...
void Hdf5Recorder::init()
{
// Check the output file extension is .h5
/*string suffix = ".h5";
if ((resultFileName_.size() <= suffix.size()) ||
(resultFileName_.compare(resultFileName_.size() - suffix.size(), suffix.size(), suffix) != 0)) {
std::cerr << "The file extension is not .h5" << std::endl;
exit(EXIT_FAILURE);
}
string suffix = ".h5";
if ((resultFileName_.size() <= suffix.size())
|| (resultFileName_.compare(resultFileName_.size() - suffix.size(), suffix.size(), suffix)
!= 0)) {
//perror("the file extention is not .h5 ");
string errorMsg
= "Error: the file extension is not .h5. Provided file name: " + resultFileName_;
perror(errorMsg.c_str());
exit(EXIT_FAILURE);
}

// Check if we can create and write to the file
std::ofstream testFileWrite(resultFileName_);
if (!testFileWrite.is_open()) {
std::cerr << "Error opening output file for writing" << std::endl;
exit(EXIT_FAILURE);
}
testFileWrite.close();
// Check if we can create and write to the file
ofstream testFileWrite(resultFileName_.c_str());
if (!testFileWrite.is_open()) {
perror("Error opening output file for writing ");
exit(EXIT_FAILURE);
}
testFileWrite.close();

try {
// Create a new file using the default property lists
resultOut_ = new H5File(resultFileName_, H5F_ACC_TRUNC);
initDataSet();
} catch (FileIException &error) {
cerr << "HDF5 File I/O Exception: " << endl;
error.printErrorStack();
return;
} catch (DataSetIException &error) {
cerr << "HDF5 Dataset Exception: " << endl;
error.printErrorStack();
return;
} catch (DataSpaceIException &error) {
cerr << "HDF5 Dataspace Exception: " << endl;
error.printErrorStack();
return;
} catch (DataTypeIException &error) {
cerr << "HDF5 Datatype Exception: " << endl;
error.printErrorStack();
return;
}
}

try {
// Create a new file using the default property lists
resultOut_ = H5File(resultFileName_, H5F_ACC_TRUNC);
initDataSet();
} catch (const FileIException& error) {
std::cerr << "HDF5 File I/O Exception: ";
error.printErrorStack();
return;
} catch (const DataSetIException& error) {
std::cerr << "HDF5 Dataset Exception: ";
error.printErrorStack();
return;
} catch (const DataSpaceIException& error) {
std::cerr << "HDF5 Dataspace Exception: ";
error.printErrorStack();
return;
} catch (const DataTypeIException& error) {
std::cerr << "HDF5 Datatype Exception: ";
error.printErrorStack();
return;
}*/
// This method closes the HDF5 file and releases any associated resources
void Hdf5Recorder::term()
{
// checks if the file object `resultOut_` is not null, then attempts to close the file and delete the object
if (resultOut_ != nullptr) {
try {
resultOut_->close();
delete resultOut_;
resultOut_ = nullptr;
} catch (FileIException &error) {
// If an exception occurs during this process, it prints the error stack for debugging purposes
cerr << "HDF5 File I/O Exception during termination: ";
error.printErrorStack();
}
}
}

// Create data spaces and data sets of the hdf5 for recording histories.
void Hdf5Recorder::initDataSet()
{
}
#endif // HDF5
9 changes: 3 additions & 6 deletions Simulator/Recorders/Hdf5Recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using std::vector;
class Hdf5Recorder : public Recorder {
public:
Hdf5Recorder();
virtual ~Hdf5Recorder();

/**
* @brief Factory method to create a new HDF5Recorder instance.
Expand Down Expand Up @@ -55,9 +56,7 @@ class Hdf5Recorder : public Recorder {
}

/// Terminate process
virtual void term() override
{
}
virtual void term() override;

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

private:
virtual void initDataSet()
{
}
virtual void initDataSet();

/// Populates Starter neuron matrix based with boolean values based on starterMap state
///@param[in] matrix starter neuron matrix
Expand Down
25 changes: 17 additions & 8 deletions Testing/UnitTesting/Hdf5RecorderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ TEST(Hdf5RecorderTest, CreateInstanceSuccess)
ASSERT_TRUE(recorder != nullptr);
}

// Test case for open file successfully
TEST(Hdf5RecorderTest, Hdf5InitTest)
// Test case for init() and term()
TEST(Hdf5RecorderTest, Hdf5InitAndTermTest)
{
// Create an instance of Hdf5Recorder
std::string outputFile = "../Testing/UnitTesting/TestOutput/Hdf5test_output.h5";
// Create an instance of Hdf5Recorder with a specific output file name
std::string outputFile = "../Testing/UnitTesting/TestOutput/Hdf5test_output_term.h5";
Hdf5Recorder recorder(outputFile);
/*recorder.init();
// Test to see if output file exist
FILE *f = fopen("../Testing/UnitTesting/TestOutput/Hdf5test_output.h5", "r");
recorder.init();

// Ensure the file has been created successfully by the constructor
FILE *f = fopen(outputFile.c_str(), "r");
ASSERT_TRUE(f != NULL);
fclose(f);

// Call the term() method to close the HDF5 file
recorder.term();

// Check if the file can be reopened, indicating it was closed properly
f = fopen(outputFile.c_str(), "r");
bool fileExist = f != NULL;
fclose(f);
ASSERT_TRUE(fileExist);*/
ASSERT_TRUE(fileExist);
}
#endif // HDF5