void test_migration()
{
	char filename_template[] = "migration_dropping_test.XXXXXX";
	char* filename = mktemp(filename_template);

	/* create one instance of each class */
	CTestClassOld* old_instance=new CTestClassOld();
	CTestClassNew* new_instance=new CTestClassNew();

	CSerializableAsciiFile* file;

	/* serialize int instance, use custom parameter version */
	file=new CSerializableAsciiFile(filename, 'w');
	old_instance->save_serializable(file, "", 0);
	file->close();
	SG_UNREF(file);

	/* de-serialize float instance, use custom parameter version */
	file=new CSerializableAsciiFile(filename, 'r');
	new_instance->load_serializable(file, "", 1);
	file->close();
	SG_UNREF(file);

	/* assert that content is equal */
	check_equalness(old_instance, new_instance);

	SG_UNREF(old_instance);
	SG_UNREF(new_instance);
	SG_UNREF(file);
	unlink(filename);
}
void test_migration()
{
	/* create one instance of each class */
	CTestClassInt* int_instance=new CTestClassInt();
	CTestClassFloat* float_instance=new CTestClassFloat();

	CSerializableAsciiFile* file;

	/* serialize int instance, use custom parameter version */
	file=new CSerializableAsciiFile(filename, 'w');
	int_instance->save_serializable(file, "", -1);
	file->close();
	SG_UNREF(file);

	/* now the magic happens, the float instance is derserialized from file.
	 * Note that the parameter types are different. they will all be mapped.
	 * See migration methods. Everything is just converted, value is kept.
	 * The float instance has different initial values for all members, however,
	 * after de-serializing it from the int_instance file, the values should be
	 * the same
	 *
	 * The parameter mappings are chosen in such way that CTestClassInt could
	 * be seen as an old version of CTestClassFloat. */

	/* de-serialize float instance, use custom parameter version
	 * Note that a warning will appear, complaining that there is no parameter
	 * version in file. This is not true, the version is -1, which is used here
	 * as custom version. Normally numbers >=0 are used. */
	file=new CSerializableAsciiFile(filename, 'r');
	float_instance->load_serializable(file, "", 1);
	file->close();
	SG_UNREF(file);

	/* assert that content is equal */
	check_equalness(int_instance, float_instance);

	SG_UNREF(int_instance);
	SG_UNREF(float_instance);
	SG_UNREF(file);
}
int main(int argc, char** argv)
{
	init_shogun_with_defaults();
	sg_io->set_loglevel(MSG_DEBUG);

	/* create feature data matrix */
	SGMatrix<int32_t> data(3, 20);

	/* fill matrix with random data */
	for (index_t i=0; i<20*3; ++i)
	{
		if (i%2==0)
			data.matrix[i]=0;
		else
			data.matrix[i]=CMath::random(1, 9);
	}

	/* create sparse features */
	CSparseFeatures<int32_t>* sparse_features=new CSparseFeatures<int32_t>(data);

	CSerializableAsciiFile* file;

	file=new CSerializableAsciiFile("sparseFeatures.txt", 'w');
	sparse_features->save_serializable(file);
	file->close();
	SG_UNREF(file);

	/* this will fail with a warning, same with CSerializableHdf5File and xml serialization*/
	CSparseFeatures<int32_t>* sparse_features_loaded = new CSparseFeatures<int32_t>();
	file = new CSerializableAsciiFile("sparseFeatures.txt", 'r');
	sparse_features_loaded->load_serializable(file);
	SG_UNREF(file);
	SG_UNREF(sparse_features_loaded);
	SG_UNREF(sparse_features);

	exit_shogun();
}
Ejemplo n.º 4
0
void test_load_file_parameter()
{
	/* create one instance of each class */
	CTestClassInt* int_instance=new CTestClassInt();
	CTestClassFloat* float_instance=new CTestClassFloat();

	CSerializableAsciiFile* file;

	/* serialize int instance */
	file=new CSerializableAsciiFile(filename, 'w');
	int_instance->save_serializable(file);
	file->close();
	SG_UNREF(file);

	/* reopen file for reading */
	file=new CSerializableAsciiFile(filename, 'r');

	/* versions that are used in this example */
	int32_t file_version=-1;
	int32_t current_version=1;

	/* load all parameter data, current version is set to 1 here */
	DynArray<TParameter*>* params=
			float_instance->load_all_file_parameters(file_version,
					current_version, file, "");

	/* create an array of param infos from float instance parameters */
	DynArray<const SGParamInfo*>* param_infos=
			new DynArray<const SGParamInfo*>();
	for (index_t i=0; i<float_instance->m_parameters->get_num_parameters(); ++i)
	{
		param_infos->append_element(
				new SGParamInfo(float_instance->m_parameters->get_parameter(i),
						current_version));
	}

	/* here the magic mapping happens */
	float_instance->map_parameters(params, file_version, param_infos);

	/* assert equalness of all parameters
	 * alphabetical order is "float_features", "matrix", "number", "vector" */
	TParameter* current=NULL;

	/* "float_features" (no type change here) */
	current=params->get_element(0);
	SG_SPRINT("checking \"float_features\":\n");
	ASSERT(!strcmp(current->m_name, "float_features"));
	/* cast to simple features */
	CDenseFeatures<int32_t>* features=
			*((CDenseFeatures<int32_t>**)current->m_parameter);
	SG_SPRINT("checking address (mapped!=original): %p!=%p\n", features,
			int_instance->m_features);
	ASSERT((void*)features!=(void*)int_instance->m_features);
	SG_SPRINT("checking cache size: %d==%d\n", features->get_cache_size(),
				int_instance->m_features->get_cache_size());
	ASSERT(features->get_cache_size()==
			int_instance->m_features->get_cache_size());
	SG_SPRINT("checking combined feature weight: %f==%f\n",
			features->get_combined_feature_weight(),
			int_instance->m_features->get_combined_feature_weight());
	ASSERT(features->get_combined_feature_weight()==
			int_instance->m_features->get_combined_feature_weight());
	SG_SPRINT("checking feature matrix:\n");
	SGMatrix<int32_t> int_matrix=int_instance->m_features->get_feature_matrix();
	SGMatrix<int32_t> float_matrix=features->get_feature_matrix();
	SG_SPRINT("number of rows: %d==%d\n", int_matrix.num_rows,
			float_matrix.num_rows);
	ASSERT(int_matrix.num_rows==float_matrix.num_rows);
	SG_SPRINT("number of cols: %d==%d\n", int_matrix.num_cols,
			float_matrix.num_cols);
	ASSERT(int_matrix.num_cols==float_matrix.num_cols);
	SGMatrix<int32_t>::display_matrix(float_matrix.matrix, float_matrix.num_rows,
			float_matrix.num_cols, "mapped");
	SGMatrix<int32_t>::display_matrix(int_matrix.matrix, int_matrix.num_rows,
			int_matrix.num_cols, "original");
	for (index_t i=0; i<int_matrix.num_rows*int_matrix.num_cols; ++i)
		ASSERT(int_matrix.matrix[i]==float_matrix.matrix[i]);

	/* "matrix" */
	current=params->get_element(1);
	ASSERT(!strcmp(current->m_name, "matrix"));
	SGMatrix<float64_t> matrix(*(float64_t**)current->m_parameter,
			*current->m_datatype.m_length_y, *current->m_datatype.m_length_x, false);
	SG_SPRINT("checking \"matrix:\n");
	SG_SPRINT("number of rows: %d==%d\n", *current->m_datatype.m_length_y,
			int_instance->m_matrix_rows);
	ASSERT(*current->m_datatype.m_length_y==int_instance->m_matrix_rows);
	SGMatrix<float64_t>::display_matrix(matrix.matrix, matrix.num_rows, matrix.num_cols,
			"mapped");
	SGMatrix<int32_t>::display_matrix(int_instance->m_matrix, int_instance->m_matrix_rows,
			int_instance->m_matrix_cols, "original");
	for (index_t i=0; i<int_instance->m_matrix_rows*int_instance->m_matrix_cols;
			++i)
	{
		ASSERT(matrix.matrix[i]==int_instance->m_matrix[i]);
	}

	/* "number" */
	current=params->get_element(2);
	ASSERT(!strcmp(current->m_name, "number"));
	float64_t number=*((float64_t*)current->m_parameter);
	SG_SPRINT("checking \"number\": %f == %d\n", number,
			int_instance->m_number);
	ASSERT(number==int_instance->m_number);

	/* "vector" */
	current=params->get_element(3);
	ASSERT(!strcmp(current->m_name, "vector"));
	SGVector<float64_t> vector(*(float64_t**)current->m_parameter,
			*current->m_datatype.m_length_y, false);
	SG_SPRINT("checking \"vector:\n");
	SG_SPRINT("length: %d==%d\n", *current->m_datatype.m_length_y,
			int_instance->m_vector_length);
	ASSERT(*current->m_datatype.m_length_y==int_instance->m_vector_length);
	SGVector<float64_t>::display_vector(vector.vector, vector.vlen, "mapped");
	SGVector<int32_t>::display_vector(int_instance->m_vector, int_instance->m_vector_length,
			"original");
	for (index_t i=0; i<int_instance->m_vector_length; ++i)
		ASSERT(vector.vector[i]==int_instance->m_vector[i]);

	/* clean up */
	for (index_t i=0; i<param_infos->get_num_elements(); ++i)
		delete param_infos->get_element(i);

	delete param_infos;

	for (index_t i=0; i<params->get_num_elements(); ++i)
	{
		/* delete data of TParameters because they were mapped */
		params->get_element(i)->m_delete_data=true;
		delete params->get_element(i);
	}

	delete params;

	file->close();
	SG_UNREF(file);
	SG_UNREF(int_instance);
	SG_UNREF(float_instance);
}
Ejemplo n.º 5
0
void test_load_file_parameter()
{
	char filename_tmp[] = "load_all_file_test.XXXXXX";
	int fd = mkstemp(filename_tmp);
	ASSERT(fd != -1);
	int retval = close(fd);
	ASSERT(retval != -1);
	char* filename = filename_tmp;

	/* create one instance of each class */
	CTestClassInt* int_instance=new CTestClassInt();
	CTestClassFloat* float_instance=new CTestClassFloat();

	CSerializableAsciiFile* file;

	/* serialize int instance */
	file=new CSerializableAsciiFile(filename, 'w');
	int_instance->save_serializable(file);
	file->close();
	SG_UNREF(file);

	/* reopen file for reading */
	file=new CSerializableAsciiFile(filename, 'r');

	int32_t file_version=-1;

	/* load all parameter data, current version is set to 1 here */
	DynArray<TParameter*>* params=
			float_instance->load_all_file_parameters(file_version, 1, file, "");

	/* test the result */
	for (index_t i=0; i<params->get_num_elements(); ++i)
	{
		TParameter* current=params->get_element(i);
		current->m_delete_data = true; // TODO: This shouldn't be necessary!

		/* ensure that data is same as of the instance for all parameters */
		if (!strcmp(current->m_name, "number"))
		{
			int32_t value_number=*((int32_t*)current->m_parameter);
			SG_SPRINT("%i\n", value_number);
			ASSERT(value_number=int_instance->m_number);
		}
		else if (!strcmp(current->m_name, "vector"))
		{
			int32_t* value_vector=*((int32_t**)current->m_parameter);
			SGVector<int32_t>::display_vector(value_vector, int_instance->m_vector_length);

			for (index_t j=0; j<int_instance->m_vector_length; ++j)
				ASSERT(value_vector[j]=int_instance->m_vector[j]);
		}
		else if (!strcmp(current->m_name, "matrix"))
		{
			int32_t* value_matrix=*((int32_t**)current->m_parameter);
			SGMatrix<int32_t>::display_matrix(value_matrix, int_instance->m_matrix_rows,
					int_instance->m_matrix_cols);

			for (index_t j=0; j<int_instance->m_matrix_rows*int_instance->m_matrix_cols;
					++j)
			{
				ASSERT(value_matrix[j]==int_instance->m_matrix[j]);
			}
		}
		else if (!strcmp(current->m_name, "int_features"))
		{
			CDenseFeatures<int32_t>* features=
					*((CDenseFeatures<int32_t>**)
							current->m_parameter);
			SGMatrix<int32_t> feature_matrix_loaded=
					features->get_feature_matrix();
			SGMatrix<int32_t> feature_matrix_original=
					int_instance->m_features->get_feature_matrix();

			SGMatrix<int32_t>::display_matrix(feature_matrix_loaded.matrix,
					feature_matrix_loaded.num_rows,
					feature_matrix_loaded.num_cols,
					"features");

			for (index_t j=0;
					j<int_instance->m_matrix_rows*int_instance->m_matrix_cols;
					++j)
			{
				ASSERT(feature_matrix_original.matrix[j]==
						feature_matrix_loaded.matrix[j]);
			}
		}
	}

	/* assert that parameter data is sorted */
	for (index_t i=1; i<params->get_num_elements(); ++i)
	{
		/* assert via TParameter < and == operator */
		TParameter* t1=params->get_element(i-1);
		TParameter* t2=params->get_element(i);
		ASSERT((*t1)<(*t2) || (*t1)==(*t2));

		/* assert via name (which is used in the operator, but to be sure */
		const char* s1=t1->m_name;
		const char* s2=t2->m_name;
		SG_SPRINT("param \"%s\" <= \"%s\" ? ... ", s1, s2);
		ASSERT(strcmp(s1, s2)<=0);
		SG_SPRINT("yes\n");
	}


	/* clean up */
	for (index_t i=0; i<params->get_num_elements(); ++i)
		delete params->get_element(i);

	delete params;

	file->close();
	SG_UNREF(file);
	SG_UNREF(int_instance);
	SG_UNREF(float_instance);
	unlink(filename);
}