Skip to content

Commit

Permalink
Add Serializable object and drop Wrapped classes
Browse files Browse the repository at this point in the history
refactor R installation
  • Loading branch information
vigsterkr committed Jul 9, 2017
1 parent ea00c02 commit e552db5
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 465 deletions.
24 changes: 14 additions & 10 deletions examples/meta/generator/translate.py
Expand Up @@ -178,8 +178,8 @@ def injectVarsStoring(self, statementList, programName, varsToStore):
""" Injects statements at the end of the program that perform variable
storing
"""
storage = "__sg_storage"
storageFile = "__sg_storage_file"
storage = "sg_storage"
storageFile = "sg_storage_file"

# TODO: handle directories
storageFilename = {
Expand All @@ -188,7 +188,7 @@ def injectVarsStoring(self, statementList, programName, varsToStore):
# 'w'
storageFilemode = {"Expr": {"CharLiteral": 'w'}}
storageComment = {"Comment": " Serialize output for integration testing (automatically generated)"}
storageInit = {"Init": [{"ObjectType": "WrappedObjectArray"},
storageInit = {"Init": [{"ObjectType": "DynamicObjectArray"},

This comment has been minimized.

Copy link
@karlnapf

karlnapf Jul 9, 2017

Member

The old array could handle a mix of matrix/vector/scalar, not sure the dynamic object array can?

{"Identifier": storage},
{"ArgumentList": []}]}
storageFileInit = {
Expand All @@ -214,16 +214,20 @@ def injectVarsStoring(self, statementList, programName, varsToStore):
# i.e. one key
# python2/3 compatible key accessing
sgType = vartypeAST[list(vartypeAST.keys())[0]]

assert sgType in getBasicTypesToStore() or sgType in getSGTypesToStore()
if sgType in getBasicTypesToStore():
methodNameSuffix = sgType
elif sgType in getSGTypesToStore():
methodNameSuffix = getSGTypeToStoreMethodName(sgType)


if "cpp" in self.targetDict["FileExtension"]:
methodNameSuffix = "<%s>" % self.targetDict["Type"][sgType]
else:
if sgType in getBasicTypesToStore():
methodNameSuffix = sgType
elif sgType in getSGTypesToStore():
methodNameSuffix = getSGTypeToStoreMethodName(sgType)
methodNameSuffix = "_%s" % methodNameSuffix

methodCall = {
"MethodCall": [{"Identifier": storage},
{"Identifier": "append_wrapped_%s" % methodNameSuffix},
{"Identifier": "append_element%s" % methodNameSuffix},
{"ArgumentList": [varnameIdentifierExpr,
varnameExpr]}]
}
Expand Down
26 changes: 26 additions & 0 deletions src/interfaces/r/CMakeLists.txt
Expand Up @@ -11,6 +11,28 @@ ENDIF()
GENERATE_INTERFACE_TARGET(r ${CMAKE_CURRENT_SOURCE_DIR} ${R_LIBRARIES})
set_target_properties(interface_r PROPERTIES PREFIX "")

EXECUTE_PROCESS(COMMAND ${R_EXECUTABLE} --slave -e "with(R.version, cat(sprintf('%s.%s', major, minor)))"
OUTPUT_VARIABLE R_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)

EXECUTE_PROCESS(COMMAND ${R_EXECUTABLE} --slave -e "cat(R.version\\$platform)"
OUTPUT_VARIABLE PLATFORM
OUTPUT_STRIP_TRAILING_WHITESPACE
)

EXECUTE_PROCESS(COMMAND ${R_EXECUTABLE} --slave -e "cat(.Platform\\$OS.type)"
OUTPUT_VARIABLE OSTYPE
OUTPUT_STRIP_TRAILING_WHITESPACE
)

EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "from time import gmtime, strftime; print(strftime("%Y-%m-%d %H:%M:%S", gmtime()))"
OUTPUT_VARIABLE DATE
OUTPUT_STRIP_TRAILING_WHITESPACE
)

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/DESCRIPTION.in ${CMAKE_CURRENT_BINARY_DIR}/DESCRIPTION @ONLY)

ADD_CUSTOM_COMMAND(TARGET interface_r
POST_BUILD
COMMAND echo 'wd="${CMAKE_CURRENT_BINARY_DIR}" \; setwd( wd ) \; f="shogun.R" \; fdata="shogun.RData" \; source( f ) \; save( list=ls( all=TRUE ) , file=fdata , compress=TRUE ) \; q( save="no" ) \;' | ${R_EXECUTABLE} --silent --no-save
Expand All @@ -26,6 +48,10 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/shogun.RData
DESTINATION ${R_COMPONENT_LIB_PATH}/shogun/R
COMPONENT r)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/DESCRIPTION ${CMAKE_CURRENT_BINARY_DIR}/NAMESPACE
DESTINATION ${R_COMPONENT_LIB_PATH}/shogun
COMPONENT r)

install(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}/${R_COMPONENT_LIB_PATH}/shogun/Meta)")

install(CODE "EXECUTE_PROCESS(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/r-install.sh \$ENV{DESTDIR}/${R_COMPONENT_LIB_PATH} shogun so saveRDS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
26 changes: 26 additions & 0 deletions src/interfaces/r/DESCRIPTION.in
@@ -0,0 +1,26 @@
Package: shogun
Version: @VERSION@
Date: @DATE@
Title: The SHOGUN Machine Learning Toolbox
Author: Shogun Team
Maintainer: Shogun Team <shogun-team@shogun-toolbox.org>
Depends: R (>= 2.10.0)
Suggests:
Description: SHOGUN - is a new machine learning toolbox with focus on large
scale kernel methods and especially on Support Vector Machines (SVM) with focus
to bioinformatics. It provides a generic SVM object interfacing to several
different SVM implementations. Each of the SVMs can be combined with a variety
of the many kernels implemented. It can deal with weighted linear combination
of a number of sub-kernels, each of which not necessarily working on the same
domain, where an optimal sub-kernel weighting can be learned using Multiple
Kernel Learning. Apart from SVM 2-class classification and regression
problems, a number of linear methods like Linear Discriminant Analysis (LDA),
Linear Programming Machine (LPM), (Kernel) Perceptrons and also algorithms to
train hidden markov models are implemented. The input feature-objects can be
dense, sparse or strings and of type int/short/double/char and can be converted
into different feature types. Chains of preprocessors (e.g. substracting the
mean) can be attached to each feature object allowing for on-the-fly
pre-processing.
License: GPL Version 3 or later.
URL: http://www.shogun-toolbox.org
Built: @R_VERSION@; @PLATFORM@; @OSTYPE@;
1 change: 1 addition & 0 deletions src/interfaces/r/NAMESPACE
@@ -0,0 +1 @@
useDynLib(shogun, .registration = TRUE)
33 changes: 0 additions & 33 deletions src/interfaces/r/r-install.sh
Expand Up @@ -9,35 +9,6 @@ DATE="`date '+%Y-%m-%d %H:%M:%S'`"
PKGFILE="$1/$2/Meta/package.rds"
SAVERDS="$4"

cat >"$1/$2/DESCRIPTION" <<EOF
Package: $2
Version: $VERSION
Date: $DATE
Title: The SHOGUN Machine Learning Toolbox
Author: Shogun Team
Maintainer: Shogun Team <shogun-team@shogun-toolbox.org>
Depends: R (>= 2.10.0)
Suggests:
Description: SHOGUN - is a new machine learning toolbox with focus on large
scale kernel methods and especially on Support Vector Machines (SVM) with focus
to bioinformatics. It provides a generic SVM object interfacing to several
different SVM implementations. Each of the SVMs can be combined with a variety
of the many kernels implemented. It can deal with weighted linear combination
of a number of sub-kernels, each of which not necessarily working on the same
domain, where an optimal sub-kernel weighting can be learned using Multiple
Kernel Learning. Apart from SVM 2-class classification and regression
problems, a number of linear methods like Linear Discriminant Analysis (LDA),
Linear Programming Machine (LPM), (Kernel) Perceptrons and also algorithms to
train hidden markov models are implemented. The input feature-objects can be
dense, sparse or strings and of type int/short/double/char and can be converted
into different feature types. Chains of preprocessors (e.g. substracting the
mean) can be attached to each feature object allowing for on-the-fly
pre-processing.
License: GPL Version 3 or later.
URL: http://www.shogun-toolbox.org
Built: $RVERSION; $PLATFORM; $OSTYPE;
EOF

echo "x=structure(list(DESCRIPTION = c(Package='$2',\
Version=\"$VERSION\",\
Date=\"$DATE\",\
Expand All @@ -58,10 +29,6 @@ echo "x=structure(list(DESCRIPTION = c(Package='$2',\
# R-MODULAR
echo "Installing modular shogun interface for R"

cat >"$1/$2/NAMESPACE" <<EOF
useDynLib(shogun, .registration = TRUE)
EOF

cat >"$1/$2/R/$2" <<EOF
.packageName <- "$2"
#$2 <- function(...) .External("$2",...,PACKAGE="$2")
Expand Down
13 changes: 11 additions & 2 deletions src/interfaces/swig/Library.i
Expand Up @@ -22,7 +22,6 @@
%rename(Hash) CHash;
%rename(StructuredData) CStructuredData;
%rename(DynamicObjectArray) CDynamicObjectArray;
%rename(WrappedObjectArray) CWrappedObjectArray;
%rename(Tokenizer) CTokenizer;
%rename(DelimiterTokenizer) CDelimiterTokenizer;
%rename(NGramTokenizer) CNGramTokenizer;
Expand Down Expand Up @@ -399,7 +398,17 @@ namespace shogun
%include <shogun/lib/StructuredDataTypes.h>
%include <shogun/lib/StructuredData.h>
%include <shogun/lib/DynamicObjectArray.h>
%include <shogun/lib/WrappedObjectArray.h>
namespace shogun
{
/* Specialize DynamicObjectArray::append_element function */
%template(append_element_real) CDynamicObjectArray::append_element<float64_t>;
%template(append_element_float) CDynamicObjectArray::append_element<float32_t>;
%template(append_element_int) CDynamicObjectArray::append_element<int32_t>;
%template(append_element_real_vector) CDynamicObjectArray::append_element<SGVector<float64_t>>;

This comment has been minimized.

Copy link
@karlnapf

karlnapf Jul 9, 2017

Member

ah that is a sweeter solution! nice!

This comment has been minimized.

Copy link
@vigsterkr

vigsterkr Jul 9, 2017

Author Member

it is but actually not yet... since there are couple of templates involved interim in this process i need to add some more stuff here...

This comment has been minimized.

Copy link
@karlnapf

karlnapf Jul 9, 2017

Member

Ok!
Did the old solution cause any problems?

This comment has been minimized.

Copy link
@vigsterkr

vigsterkr Jul 9, 2017

Author Member

yeah it didn't work with R

%template(append_element_float_vector) CDynamicObjectArray::append_element<SGVector<float32_t>>;
%template(append_element_real_matrix) CDynamicObjectArray::append_element<SGMatrix<float64_t>>;
%template(append_element_float_matrix) CDynamicObjectArray::append_element<SGMatrix<float32_t>>;
}
%include <shogun/lib/IndexBlock.h>
%include <shogun/lib/IndexBlockRelation.h>
%include <shogun/lib/IndexBlockGroup.h>
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/swig/Library_includes.i
Expand Up @@ -21,7 +21,6 @@
#include <shogun/lib/StructuredDataTypes.h>
#include <shogun/lib/StructuredData.h>
#include <shogun/lib/DynamicObjectArray.h>
#include <shogun/lib/WrappedObjectArray.h>
#include <shogun/lib/IndexBlock.h>
#include <shogun/lib/IndexBlockRelation.h>
#include <shogun/lib/IndexBlockGroup.h>
Expand Down
79 changes: 34 additions & 45 deletions src/shogun/lib/WrappedSGMatrix.h → src/shogun/io/Serializable.h
Expand Up @@ -2,7 +2,7 @@
* -*- coding: utf-8 -*-
* vim: set fileencoding=utf-8
*
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -31,81 +31,70 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Heiko Strathmann
*/
#ifdef USE_META_INTEGRATION_TESTS
#ifndef WRAPPED_SGMATRIX_H__
#define WRAPPED_SGMATRIX_H__
#ifndef SHOGUN_SERIALIZABLE_H__
#define SHOGUN_SERIALIZABLE_H__

#include <shogun/base/SGObject.h>
#include <shogun/lib/SGString.h>
#include <shogun/lib/SGMatrix.h>


namespace shogun
{

/** @brief Simple wrapper class that allows to store any Shogun SGMatrix<T>
* in a CSGObject, and therefore to make it serializable. Using a template
* argument that is not a Shogun parameter will cause a compile error when
* trying to register the passed value as a parameter in the constructors.
template<typename T>
struct extract_value_type
{
typedef T value_type;
};

template<template<typename, typename ...> class X, typename T, typename ...Args>
struct extract_value_type<X<T, Args...>>
{
typedef T value_type;
};

/** @brief A trait that makes a none SGObject SG-serializable
* This only works with classes derived of SGReferencedData (SGVector, SGMatrix etc)
* and fundamental types (std::is_arithmetic)
*/
template<class T> class CWrappedSGMatrix: public CSGObject
template<class T> class CSerializable: public CSGObject

This comment has been minimized.

Copy link
@vigsterkr

vigsterkr Jul 9, 2017

Author Member

@lisitsyn can you plz look at this a bit if you are fine with it

This comment has been minimized.

Copy link
@lisitsyn

lisitsyn Jul 9, 2017

Member

Yeah looks good

{
public:
/** Default constructor. Do not use. */
CWrappedSGMatrix() : CSGObject()
CSerializable() : CSGObject()
{
set_generic<T>();
register_params();
init();
}

/** Constructor.
* @param value Value to wrap as CSGObject.
* @param value Value to serialize as CSGObject.
* @param value_name Name under which value is registered.
*/
CWrappedSGMatrix(SGMatrix<T> value, const char* value_name="")
CSerializable(T value, const char* value_name="")
{
set_generic<T>();
register_params();
init();
m_value = value;
m_value_name = value_name;
}

/** @return name of the CSGObject, without C prefix */
virtual const char* get_name() const { return "WrappedSGMatrix"; }
virtual const char* get_name() const { return "Serializable"; }

private:
void register_params()
void init()
{
m_value_name = "Unnamed";
m_value = SGMatrix<T>();
SG_ADD(&m_value, "value", "Wrapped value", MS_NOT_AVAILABLE);
set_generic<typename extract_value_type<T>::value_type>();

This comment has been minimized.

Copy link
@lisitsyn

lisitsyn Jul 9, 2017

Member

Cool thing

m_value_name = "Unnamed";

SG_ADD(&m_value, "value", "Serialized value", MS_NOT_AVAILABLE);
}

protected:
/** Wrapped value. */
SGMatrix<T> m_value;
/** Serialized value. */
T m_value;

/** Name of wrapped value */
/** Name of serialized value */
const char* m_value_name;
};

template class CWrappedSGMatrix<bool>;
template class CWrappedSGMatrix<char>;
template class CWrappedSGMatrix<int8_t>;
template class CWrappedSGMatrix<uint8_t>;
template class CWrappedSGMatrix<int16_t>;
template class CWrappedSGMatrix<uint16_t>;
template class CWrappedSGMatrix<int32_t>;
template class CWrappedSGMatrix<uint32_t>;
template class CWrappedSGMatrix<int64_t>;
template class CWrappedSGMatrix<uint64_t>;
template class CWrappedSGMatrix<float32_t>;
template class CWrappedSGMatrix<float64_t>;
template class CWrappedSGMatrix<floatmax_t>;

};
#endif // WRAPPED_SGMATRIX_H__
#endif // USE_META_INTEGRATION_TESTS
#endif // SHOGUN_SERIALIZABLE_H_
12 changes: 12 additions & 0 deletions src/shogun/lib/DynamicObjectArray.h
Expand Up @@ -17,6 +17,7 @@
#include <shogun/base/SGObject.h>
#include <shogun/base/DynArray.h>
#include <shogun/base/Parameter.h>
#include <shogun/io/Serializable.h>

namespace shogun
{
Expand Down Expand Up @@ -277,6 +278,17 @@ class CDynamicObjectArray : public CSGObject
return success;
}

template <typename T>
inline bool append_element(typename std::enable_if<!std::is_base_of<CSGObject, std::remove_pointer<T>>::value, T>::type e, const char* name="")
{
auto serializedElement = new CSerializable<T>(e, name);
bool success = m_array.append_element(serializedElement);
if (success)
SG_REF(serializedElement);

return success;
}

/** append array element to the end of array
*
* @param e element to append
Expand Down

0 comments on commit e552db5

Please sign in to comment.