Skip to content

Commit

Permalink
CMake: External mixmod
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Nov 23, 2023
1 parent 6e5d357 commit 742b181
Show file tree
Hide file tree
Showing 212 changed files with 202 additions and 57,951 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ if (NOT DEFINED OPENTURNS_PYTHON_MODULE_PATH)
set (OPENTURNS_PYTHON_MODULE_PATH ${OPENTURNS_PYTHON3_MODULE_PATH})
endif ()

find_package (Mixmod REQUIRED)

if (NOT BUILD_SHARED_LIBS)
list ( APPEND OTMIXMOD_DEFINITIONS "-DOTMIXMOD_STATIC" )
endif ()
Expand Down
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
= 0.16 release (in-progress)

- Depend on mixmod library (https://github.com/mixmod)
- Removed MixtureClassifierFactory

= 0.15 release (2023-11-15)

* Maintenance release
Expand Down
87 changes: 87 additions & 0 deletions cmake/FindMixmod.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# - Find Mixmod
# Classification with Mixture Modelling
# available at https://github.com/mixmod
#
# The module defines the following variables:
# MIXMOD_FOUND - the system has LibSVM
# MIXMOD_INCLUDE_DIR - where to find svm.h
# MIXMOD_INCLUDE_DIRS - libsvm includes
# MIXMOD_LIBRARY - where to find the LibSVM library
# MIXMOD_LIBRARIES - aditional libraries
# MIXMOD_MAJOR_VERSION - major version
# MIXMOD_MINOR_VERSION - minor version
# MIXMOD_VERSION_STRING - version (ex. 2.9.0)
# MIXMOD_ROOT_DIR - root dir (ex. /usr/local)

#=============================================================================
# Copyright 2010-2023, Julien Schueller
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.
#=============================================================================

# set MIXMOD_INCLUDE_DIR
find_path (MIXMOD_INCLUDE_DIR
NAMES
mixmod.h
)


# set MIXMOD_INCLUDE_DIRS
set (MIXMOD_INCLUDE_DIRS ${MIXMOD_INCLUDE_DIR})

# set MIXMOD_LIBRARY
find_library (MIXMOD_LIBRARY
NAMES
mixmod
DOC
"Mixmod library location"
)

# set MIXMOD_LIBRARIES
set (MIXMOD_LIBRARIES ${MIXMOD_LIBRARY})



# try to guess root dir from include dir
if (MIXMOD_INCLUDE_DIR)
string (REGEX REPLACE "(.*)/include.*" "\\1" MIXMOD_ROOT_DIR ${MIXMOD_INCLUDE_DIR})
# try to guess root dir from library dir
elseif (MIXMOD_LIBRARY)
string (REGEX REPLACE "(.*)/lib[/|32|64].*" "\\1" MIXMOD_ROOT_DIR ${MIXMOD_LIBRARY})
endif ()


# handle REQUIRED and QUIET options
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (Mixmod REQUIRED_VARS MIXMOD_LIBRARY MIXMOD_INCLUDE_DIR)

mark_as_advanced (
MIXMOD_LIBRARY
MIXMOD_LIBRARIES
MIXMOD_INCLUDE_DIR
MIXMOD_INCLUDE_DIRS
MIXMOD_ROOT_DIR
)
9 changes: 4 additions & 5 deletions lib/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@

add_subdirectory ( mixmod )

ot_add_current_dir_to_include_dirs ()

ot_add_source_file ( MixtureClassifierFactory.cxx )
ot_add_source_file ( MixtureFactory.cxx )

ot_install_header_file ( MixtureClassifierFactory.hxx )
ot_install_header_file ( MixtureFactory.hxx )

include_directories ( ${INTERNAL_INCLUDE_DIRS} )
Expand All @@ -21,6 +16,10 @@ endif ()
set_target_properties ( otmixmod PROPERTIES VERSION ${LIB_VERSION} )
set_target_properties ( otmixmod PROPERTIES SOVERSION ${LIB_SOVERSION} )
target_link_libraries (otmixmod ${OPENTURNS_LIBRARY})

target_include_directories(otmixmod PRIVATE ${MIXMOD_INCLUDE_DIRS})
target_link_libraries (otmixmod ${MIXMOD_LIBRARIES})

set_target_properties (otmixmod PROPERTIES UNITY_BUILD OFF)

# Add targets to the build-tree export set
Expand Down
69 changes: 0 additions & 69 deletions lib/src/MixtureClassifierFactory.cxx

This file was deleted.

53 changes: 28 additions & 25 deletions lib/src/MixtureFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@
#include <openturns/Path.hxx>
#include <openturns/Os.hxx>

#include "XEMGaussianData.h"
#include "XEMClusteringInput.h"
#include "XEMClusteringOutput.h"
#include "XEMClusteringMain.h"
#include "XEMGaussianEDDAParameter.h"
#include <mixmod/Clustering/ClusteringInput.h>
#include <mixmod/Clustering/ClusteringOutput.h>
#include <mixmod/Clustering/ClusteringMain.h>
#include <mixmod/Clustering/ClusteringModelOutput.h>
#include <mixmod/Kernel/IO/GaussianData.h>
#include <mixmod/Kernel/Parameter/GaussianEDDAParameter.h>
#include <mixmod/Kernel/IO/ParameterDescription.h>
#include <mixmod/Kernel/IO/LabelDescription.h>
#include <mixmod/Kernel/IO/Label.h>
#include <mixmod/Kernel/Model/Model.h>
#include <mixmod/Matrix/Matrix.h>

namespace OTMIXMOD
{
Expand Down Expand Up @@ -100,33 +106,33 @@ OT::Mixture MixtureFactory::buildAsMixture(const OT::Sample & sample,
for (OT::UnsignedInteger j = 0; j < dimension; ++j)
matrix[i][j] = sample[i][j];

XEMGaussianData * gaussianData = new XEMGaussianData(sampleSize, dimension, matrix);
XEM::GaussianData * gaussianData = new XEM::GaussianData(sampleSize, dimension, matrix);
for (OT::UnsignedInteger i = 0; i < sampleSize; ++i) delete [] matrix[i];
delete [] matrix;

// Create a Mixmod description
XEMDataDescription dataDescription(gaussianData);
XEM::DataDescription dataDescription(gaussianData);
vector<int64_t> nbCluster;
nbCluster.push_back(atomsNumber_);

// Prepare Mixmod for clustering
XEMClusteringInput * clusteringInput(new XEMClusteringInput(nbCluster, dataDescription));
XEMModelType modelType(StringToXEMModelName(covarianceModel_));
XEM::ClusteringInput * clusteringInput(new XEM::ClusteringInput(nbCluster, dataDescription));
XEM::ModelType modelType(XEM::StringToModelName(covarianceModel_));
clusteringInput->setModelType(&modelType, 0);
clusteringInput->finalize();

// Do the computation
XEMClusteringMain clusteringMain(clusteringInput);
clusteringMain.run();
XEM::ClusteringMain clusteringMain(clusteringInput);
clusteringMain.run(seed_);

// Extract the results
XEMClusteringOutput * clusteringOutput(clusteringMain.getClusteringOutput());
XEM::ClusteringOutput * clusteringOutput(clusteringMain.getOutput());

XEMClusteringModelOutput * clusteringModelOutput(clusteringOutput->getClusteringModelOutput(0));
XEMParameterDescription * paramDescription(clusteringModelOutput->getParameterDescription());
XEM::ClusteringModelOutput * clusteringModelOutput(clusteringOutput->getClusteringModelOutput(0));
XEM::ParameterDescription * paramDescription(clusteringModelOutput->getParameterDescription());
if (paramDescription == NULL) throw OT::InternalException(HERE) << "Error: Mixmod is unable to estimate a mixture with the given data and the current number of atoms. You may have repeated points in your data, a situation not well handled by Mixmod.";
XEMParameter * paramTmp(paramDescription->getParameter());
XEMGaussianEDDAParameter * param = dynamic_cast< XEMGaussianEDDAParameter * >(paramTmp);
XEM::Parameter * paramTmp(paramDescription->getParameter());
XEM::GaussianEDDAParameter *param = dynamic_cast< XEM::GaussianEDDAParameter * >(paramTmp);

OT::Mixture::DistributionCollection coll(0);
for (OT::UnsignedInteger i = 0; i < atomsNumber_; ++i)
Expand All @@ -149,17 +155,15 @@ OT::Mixture MixtureFactory::buildAsMixture(const OT::Sample & sample,
atom.setWeight(w);
coll.add(atom);
}
XEMLabelDescription * labelDescription(clusteringModelOutput->getLabelDescription());
XEM::LabelDescription * labelDescription(clusteringModelOutput->getLabelDescription());
labels = OT::Indices(sampleSize);
// Labels run from 1 to nbAtoms, we want indices in {0,\dots,nbAtoms-1}
// getTabLabel() returns a copy of original vector, it must be destroyed explicitly
int64_t * tabLabels = labelDescription->getLabel()->getTabLabel();
for (OT::UnsignedInteger i = 0; i < sampleSize; ++i) labels[i] = tabLabels[i] - 1;
delete [] tabLabels;
BICLogLikelihood = OT::Point(3);
BICLogLikelihood[0] = param->getModel()->getLogLikelihood(false);
BICLogLikelihood[1] = param->getModel()->getCompletedLogLikelihood();
BICLogLikelihood[2] = param->getModel()->getEntropy();

BICLogLikelihood = {clusteringModelOutput->getLikelihood()};
return OT::Mixture(coll);
}

Expand All @@ -182,7 +186,7 @@ OT::UnsignedInteger MixtureFactory::getAtomsNumber () const
/* MixmodCovariance model accessors */
void MixtureFactory::setCovarianceModel (const OT::String covarianceModel)
{
(void) StringToXEMModelName(covarianceModel);
(void) XEM::StringToModelName(covarianceModel);
covarianceModel_ = covarianceModel;
}

Expand All @@ -209,10 +213,9 @@ MixtureFactory::SampleCollection MixtureFactory::BuildClusters(const OT::Sample
}

/* Mixmod PRNG state accessor */
void MixtureFactory::setState(const OT::UnsignedInteger yState,
const OT::UnsignedInteger zState)
void MixtureFactory::setSeed(const OT::SignedInteger seed)
{
setSeed(yState, zState);
seed_ = seed;
}

} // namespace OTMIXMOD
2 changes: 0 additions & 2 deletions lib/src/mixmod/CMakeLists.txt

This file was deleted.

79 changes: 0 additions & 79 deletions lib/src/mixmod/MIXMOD/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit 742b181

Please sign in to comment.