Skip to content

Commit

Permalink
move implementions from .h to .cpp in preprocessors
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Sep 21, 2011
1 parent 8e32b0d commit b3eccaf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 199 deletions.
2 changes: 0 additions & 2 deletions src/shogun/features/SparseFeatures.cpp
Expand Up @@ -781,14 +781,12 @@ template<class ST> CLabels* CSparseFeatures<ST>::load_svmlight_file(char* fname,
while (sz == blocksize)
{
sz=fread(dummy, sizeof(uint8_t), blocksize, f);
bool contains_cr=false;
for (size_t i=0; i<sz; i++)
{
block_offs++;
if (dummy[i]=='\n' || (i==sz-1 && sz<blocksize))
{
num_vectors++;
contains_cr=true;
required_blocksize=CMath::max(required_blocksize, block_offs-old_block_offs+1);
old_block_offs=block_offs;
}
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/StringFeatures.cpp
Expand Up @@ -672,7 +672,7 @@ template<class ST> bool CStringFeatures<ST>::load_fastq_file(const char* fname,

SGString<ST>* strings;

ST* str;
ST* str=NULL;
if (bitremap_in_single_string)
{
strings=SG_MALLOC(SGString<ST>, 1);
Expand Down
84 changes: 10 additions & 74 deletions src/shogun/preprocessor/DecompressString.h
Expand Up @@ -37,104 +37,40 @@ template <class ST> class CDecompressString : public CStringPreprocessor<ST>
{
public:
/** default constructor */
CDecompressString()
: CStringPreprocessor<ST>()
{
compressor=NULL;
}
CDecompressString();

/** constructor
*/
CDecompressString(E_COMPRESSION_TYPE ct)
: CStringPreprocessor<ST>()
{
compressor=new CCompressor(ct);
}
CDecompressString(E_COMPRESSION_TYPE ct);

/** destructor */
virtual ~CDecompressString()
{
delete compressor;
}
virtual ~CDecompressString();

/// initialize preprocessor from features
virtual bool init(CFeatures* f)
{
ASSERT(f->get_feature_class()==C_STRING);
return true;
}
virtual bool init(CFeatures* f);

/// cleanup
virtual void cleanup()
{
}
virtual void cleanup();

/// initialize preprocessor from file
bool load(FILE* f)
{
SG_SET_LOCALE_C;
SG_RESET_LOCALE;
return false;
}
bool load(FILE* f);

/// save preprocessor init-data to file
bool save(FILE* f)
{
SG_SET_LOCALE_C;
SG_RESET_LOCALE;
return false;
}
bool save(FILE* f);

/// apply preproc on feature matrix
/// result in feature matrix
/// return pointer to feature_matrix, i.e. f->get_feature_matrix();
virtual bool apply_to_string_features(CFeatures* f)
{
int32_t i;
int32_t num_vec=((CStringFeatures<ST>*)f)->get_num_vectors();

for (i=0; i<num_vec; i++)
{
int32_t len=0;
bool free_vec;
ST* vec=((CStringFeatures<ST>*)f)->
get_feature_vector(i, len, free_vec);

ST* decompressed=apply_to_string(vec, len);
((CStringFeatures<ST>*)f)->
free_feature_vector(vec, i, free_vec);
((CStringFeatures<ST>*)f)->
cleanup_feature_vector(i);
((CStringFeatures<ST>*)f)->
set_feature_vector(i, decompressed, len);
}
return true;
}
virtual bool apply_to_string_features(CFeatures* f);

/// apply preproc on single feature vector
virtual ST* apply_to_string(ST* f, int32_t &len)
{
uint64_t compressed_size=((int32_t*) f)[0];
uint64_t uncompressed_size=((int32_t*) f)[1];

int32_t offs=CMath::ceil(2.0*sizeof(int32_t)/sizeof(ST));
ASSERT(uint64_t(len)==uint64_t(offs)+compressed_size);

len=uncompressed_size;
uncompressed_size*=sizeof(ST);
ST* vec=SG_MALLOC(ST, len);
compressor->decompress((uint8_t*) (&f[offs]), compressed_size,
(uint8_t*) vec, uncompressed_size);

ASSERT(uncompressed_size==((uint64_t) len)*sizeof(ST));
return vec;
}
virtual ST* apply_to_string(ST* f, int32_t &len);

/** @return object name */
virtual inline const char* get_name() const { return "DecompressString"; }

/// return a type of preprocessor TODO: template specification of get_type
virtual inline EPreprocessorType get_type() const { return P_DECOMPRESSSTRING; }
virtual EPreprocessorType get_type() const;

protected:
/** compressor used to decompress strings */
Expand Down
96 changes: 14 additions & 82 deletions src/shogun/preprocessor/DimensionReductionPreprocessor.h
Expand Up @@ -14,8 +14,6 @@
#include <shogun/preprocessor/SimplePreprocessor.h>
#include <shogun/features/Features.h>
#include <shogun/distance/Distance.h>
#include <shogun/distance/EuclidianDistance.h>
#include <shogun/kernel/GaussianKernel.h>

namespace shogun
{
Expand All @@ -33,110 +31,58 @@ class CDimensionReductionPreprocessor: public CSimplePreprocessor<float64_t>
public:

/* constructor */
CDimensionReductionPreprocessor() : CSimplePreprocessor<float64_t>()
{
m_target_dim = 1;
m_distance = new CEuclidianDistance();
m_distance->parallel = this->parallel;
SG_REF(this->parallel);
m_kernel = new CGaussianKernel();
m_kernel->parallel = this->parallel;
SG_REF(this->parallel);

init();
};
CDimensionReductionPreprocessor();

/* destructor */
virtual ~CDimensionReductionPreprocessor()
{
delete m_distance;
SG_UNREF(this->parallel);
delete m_kernel;
SG_UNREF(this->parallel);
};
virtual ~CDimensionReductionPreprocessor();

/** init
* set true by default, should be defined if dimension reduction
* preprocessor is using some initialization
*/
virtual bool init(CFeatures* data)
{
return true;
};
virtual bool init(CFeatures* data);

/** cleanup
* set empty by default, should be defined if dimension reduction
* preprocessor should free some resources
*/
virtual void cleanup()
{

};
virtual void cleanup();

/** apply preproc to feature matrix
* by default does nothing, returns given features' matrix
*/
virtual SGMatrix<float64_t> apply_to_feature_matrix(CFeatures* features)
{
return ((CSimpleFeatures<float64_t>*)features)->get_feature_matrix();
};
virtual SGMatrix<float64_t> apply_to_feature_matrix(CFeatures* features);

/** apply preproc to feature vector
* by default does nothing, returns given feature vector
*/
virtual SGVector<float64_t> apply_to_feature_vector(SGVector<float64_t> vector)
{
return vector;
};
virtual SGVector<float64_t> apply_to_feature_vector(SGVector<float64_t> vector);

/** get name */
virtual inline const char* get_name() const { return "DIMREDUCTIONPREPROCESSOR"; };
virtual const char* get_name() const { return "DimensionReductionPreprocessor"; };

/** get type */
virtual inline EPreprocessorType get_type() const { return P_DIMENSIONREDUCTIONPREPROCESSOR; };
virtual EPreprocessorType get_type() const;

/** setter for target dimension
* @param dim target dimension
*/
void inline set_target_dim(int32_t dim)
{
ASSERT(dim>0 || dim==AUTO_TARGET_DIM);
m_target_dim = dim;
}
void set_target_dim(int32_t dim);

/** getter for target dimension
* @return target dimension
*/
int32_t inline get_target_dim() const
{
return m_target_dim;
}
int32_t get_target_dim() const;

/** setter for distance
* @param distance distance to set
*/
void inline set_distance(CDistance* distance)
{
SG_UNREF(m_distance->parallel);
SG_UNREF(m_distance);
SG_REF(distance);
m_distance = distance;
m_distance->parallel = this->parallel;
SG_REF(this->parallel);
}
void set_distance(CDistance* distance);

/** setter for kernel
* @param kernel kernel to set
*/
void inline set_kernel(CKernel* kernel)
{
SG_UNREF(m_kernel->parallel);
SG_UNREF(m_kernel);
SG_REF(kernel);
m_kernel = kernel;
m_kernel->parallel = this->parallel;
SG_REF(this->parallel);
}
void set_kernel(CKernel* kernel);

public:

Expand All @@ -149,23 +95,10 @@ class CDimensionReductionPreprocessor: public CSimplePreprocessor<float64_t>
* @param distance_matrix distance matrix to be used
* @return detected dimensionality
*/
virtual int32_t detect_dim(SGMatrix<float64_t> distance_matrix)
{
SG_NOTIMPLEMENTED;
return 0;
}

virtual int32_t detect_dim(SGMatrix<float64_t> distance_matrix);

/** default init */
void init()
{
m_parameters->add(&m_target_dim, "target_dim",
"target dimensionality of preprocessor");
m_parameters->add((CSGObject**)&m_distance, "distance",
"distance to be used for embedding");
m_parameters->add((CSGObject**)&m_kernel, "kernel",
"kernel to be used for embedding");
}
void init();

protected:

Expand All @@ -177,7 +110,6 @@ class CDimensionReductionPreprocessor: public CSimplePreprocessor<float64_t>

/** kernel to be used */
CKernel* m_kernel;

};
}

Expand Down
14 changes: 7 additions & 7 deletions src/shogun/preprocessor/MultidimensionalScaling.h
Expand Up @@ -175,16 +175,16 @@ class CMultidimensionalScaling: public CDimensionReductionPreprocessor
protected:

/** run triangulation thread for landmark embedding
→→→→→→→→ * @param p thread parameters
→→→→→→→→ */
* @param p thread parameters
*/
static void* run_triangulation_thread(void* p);

/** subroutine used to shuffle count indexes among of total_count ones
→→→→→→→→ * with Fisher-Yates (known as Knuth too) shuffle algorithm
→→→→→→→→ * @param count number of indexes to be shuffled and returned
→→→→→→→→ * @param total_count total number of indexes
→→→→→→→→ * @return sorted shuffled indexes for landmarks
→→→→→→→→ */
* with Fisher-Yates (known as Knuth too) shuffle algorithm
* @param count number of indexes to be shuffled and returned
* @param total_count total number of indexes
* @return sorted shuffled indexes for landmarks
*/
static SGVector<int32_t> shuffle(int32_t count, int32_t total_count);

};
Expand Down
37 changes: 4 additions & 33 deletions src/shogun/preprocessor/SimplePreprocessor.h
Expand Up @@ -33,7 +33,7 @@ template <class ST> class CSimplePreprocessor : public CPreprocessor
public:
/** constructor
*/
CSimplePreprocessor() : CPreprocessor() {}
CSimplePreprocessor();

/// apply preproc on feature matrix
/// result in feature matrix
Expand All @@ -45,43 +45,14 @@ template <class ST> class CSimplePreprocessor : public CPreprocessor
virtual SGVector<ST> apply_to_feature_vector(SGVector<ST> vector)=0;

/// return that we are simple features (just fixed size matrices)
virtual inline EFeatureClass get_feature_class() { return C_SIMPLE; }
virtual EFeatureClass get_feature_class();
/// return feature type
virtual inline EFeatureType get_feature_type();
virtual EFeatureType get_feature_type();

/// return a type of preprocessor
virtual inline EPreprocessorType get_type() const { return P_UNKNOWN; }
virtual EPreprocessorType get_type() const;

};

template<> inline EFeatureType CSimplePreprocessor<float64_t>::get_feature_type()
{
return F_DREAL;
}

template<> inline EFeatureType CSimplePreprocessor<int16_t>::get_feature_type()
{
return F_SHORT;
}

template<> inline EFeatureType CSimplePreprocessor<uint16_t>::get_feature_type()
{
return F_WORD;
}

template<> inline EFeatureType CSimplePreprocessor<char>::get_feature_type()
{
return F_CHAR;
}

template<> inline EFeatureType CSimplePreprocessor<uint8_t>::get_feature_type()
{
return F_BYTE;
}

template<> inline EFeatureType CSimplePreprocessor<uint64_t>::get_feature_type()
{
return F_ULONG;
}
}
#endif

0 comments on commit b3eccaf

Please sign in to comment.