Skip to content

Instantly share code, notes, and snippets.

@karlnapf
Created August 26, 2013 16:24
Show Gist options
  • Save karlnapf/6343428 to your computer and use it in GitHub Desktop.
Save karlnapf/6343428 to your computer and use it in GitHub Desktop.
#include <shogun/base/SGObject.h>
#include <shogun/base/class_list.h>
#define HAVE_JSON 1
#include <shogun/io/SerializableJsonFile.h>
#include <unistd.h>
using namespace shogun;
void test()
{
std::string class_name("TwoStateModel");
std::string file_template = "/tmp/" + class_name + ".XXXXXX";
char* filename = mktemp(const_cast<char*>(file_template.c_str()));
CSGObject* object = new_sgserializable(class_name.c_str(), PT_NOT_GENERIC);
ASSERT(object != NULL);
// save object to an ascii file
CSerializableJsonFile *file=new CSerializableJsonFile(filename, 'w');
object->save_serializable(file);
file->close();
SG_UNREF(file);
// load parameter from an ascii file
file=new CSerializableJsonFile(filename, 'r');
CSGObject* deserializedObject = new_sgserializable(class_name.c_str(), PT_NOT_GENERIC);
ASSERT(deserializedObject != NULL);
deserializedObject->load_serializable(file);
file->close();
SG_UNREF(file);
// check whether they are equal, up to accuracy since json is lossy
float64_t accuracy=1e-6;
ASSERT(object->equals(deserializedObject, accuracy));
SG_UNREF(object)
SG_UNREF(deserializedObject);
}
int main()
{
init_shogun_with_defaults();
sg_io->set_loglevel(MSG_DEBUG);
test();
exit_shogun();
}
[DEBUG] START SAVING CSGObject 'TwoStateModel'
[DEBUG] Saving parameter 'version_parameter' of type 'int32'
[DEBUG] Saving parameter 'm_num_states' of type 'int32'
[DEBUG] Saving parameter 'm_num_tranmission_params' of type 'int32'
[DEBUG] Saving parameter 'm_state_loss_mat' of type 'SGMatrix<float64>'
[DEBUG] Saving parameter 'm_p' of type 'SGVector<float64>'
[DEBUG] Saving parameter 'm_q' of type 'SGVector<float64>'
[DEBUG] DONE SAVING CSGObject 'TwoStateModel' (0xbbf67e0)
[ERROR] In file /home/heiko/Desktop/shogun/shogun/src/shogun/io/SerializableJsonFile.cpp line 104: Could not open file `/tmp/TwoStateModel.HwUZVH' for reading!
terminate called after throwing an instance of 'shogun::ShogunException'
heiko@heiko-ThinkPad-T420:~/Desktop/shogun/workspace/shogun-test$ cat /tmp/TwoStateModel.HwUZVH
{"filetype":"_SHOGUN_SERIALIZABLE_JSON_FILE_V_00_","version_parameter":{"type":"int32","data":1},"m_num_states":{"type":"int32","data":4},"m_num_tranmission_params":{"type":"int32","data":4},"m_state_loss_mat":{"type":"SGMatrix<float64>","data":[[0.000000,0.000000,0.000000,1.000000],[0.000000,0.000000,0.000000,1.000000],[0.000000,0.000000,0.000000,1.000000],[1.000000,1.000000,1.000000,0.000000]]},"m_p":{"type":"SGVector<float64>","data":[0.000000,-inf,-inf,-inf]},"m_q":{"type":"SGVector<float64>","data":[-inf,0.000000,-inf,-inf]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment