<commit_before>/*
 * Copyright (C) 2006-2016  Music Technology Group - Universitat Pompeu Fabra
 *
 * This file is part of Essentia
 *
 * Essentia is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation (FSF), either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the Affero GNU General Public License
 * version 3 along with this program.  If not, see http://www.gnu.org/licenses/
 */

#include "gaiatransform.h"
#include "essentia.h"
#include <gaia2/point.h>
#include <gaia2/convert.h>

using namespace std;
using namespace essentia;
using namespace standard;

const char* GaiaTransform::name = "GaiaTransform";
const char* GaiaTransform::category = "Transformations";
const char* GaiaTransform::description = DOC(
"Applies a given Gaia2 transformation history to a given pool. It first converts the pool into a gaia2::Point suitable for the history, applies the history, and converts back the resulting point into an essentia Pool. In particular, it allows classification.\n"
"\n"
"Note that in order to enable this algorithm it is necessary to install Gaia2 library before building Essentia.\n"
"\n"
"References:\n"
"  [1] Gaia - A library for similarity in high-dimensional spaces,\n"
"  http://github.com/MTG/gaia");



void GaiaTransform::configure() {
  string filename;
  try {
    filename = parameter("history").toString();
  }
  catch (...) {}

  if (filename.empty()) {
    _configured = false;
    return;
  }

  _history.load(QString::fromStdString(filename));

  _configured = true;
}



/**
 * Create a new point with the given layout and copy those values it needs from the
 * given pool.
 */
gaia2::Point* poolToPoint(const Pool& pool, const gaia2::PointLayout& layout) {
  gaia2::Point* result = new gaia2::Point(layout);

  foreach (const QString& d, layout.descriptorNames(gaia2::RealType)) {

    string dname = d.mid(1).toStdString();
    if (pool.contains<Real>(dname)) {
      // descriptor is a single value
      Real value = pool.value<Real>(dname);
      result->setValue(d, gaia2::RealDescriptor(value));
    }
    else if (pool.contains<vector<Real> >(dname)) {
      // descriptor is a vector of reals<commit_msg>Throw nicer exception on gaia history load problem<commit_after>/*
 * Copyright (C) 2006-2016  Music Technology Group - Universitat Pompeu Fabra
 *
 * This file is part of Essentia
 *
 * Essentia is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation (FSF), either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the Affero GNU General Public License
 * version 3 along with this program.  If not, see http://www.gnu.org/licenses/
 */

#include "gaiatransform.h"
#include "essentia.h"
#include <gaia2/point.h>
#include <gaia2/convert.h>

using namespace std;
using namespace essentia;
using namespace standard;

const char* GaiaTransform::name = "GaiaTransform";
const char* GaiaTransform::category = "Transformations";
const char* GaiaTransform::description = DOC(
"Applies a given Gaia2 transformation history to a given pool. It first converts the pool into a gaia2::Point suitable for the history, applies the history, and converts back the resulting point into an essentia Pool. In particular, it allows classification.\n"
"\n"
"Note that in order to enable this algorithm it is necessary to install Gaia2 library before building Essentia.\n"
"\n"
"References:\n"
"  [1] Gaia - A library for similarity in high-dimensional spaces,\n"
"  http://github.com/MTG/gaia");



void GaiaTransform::configure() {
  string filename;
  try {
    filename = parameter("history").toString();
  }
  catch (...) {}

  if (filename.empty()) {
    _configured = false;
    return;
  }

  try {
  _history.load(QString::fromStdString(filename));
  }
  catch (gaia2::GaiaException& e) {
    throw EssentiaException("GaiaTransform: error loading gaia history: ", e.what());
  }

  _configured = true;
}



/**
 * Create a new point with the given layout and copy those values it needs from the
 * given pool.
 */
gaia2::Point* poolToPoint(const Pool& pool, const gaia2::PointLayout& layout) {
  gaia2::Point* result = new gaia2::Point(layout);

  foreach (const QString& d, layout.descriptorNames(gaia2::RealType)) {

    string dname = d.mid(1).toStdString();
    if (pool.contains<Real>(dname)) {
      // descriptor is a single value
      Real value = pool.value<Real>(dname);
      result->setValue(d, gaia2::RealDescriptor(value));
    }
    else if (pool.contains<vector<Real> >(dname)) {
      // descriptor is a vector of reals
{'fraction_non_alphanumeric': 0.10280549802297119, 'fraction_numerical': 0.008849557522123894, 'mean_word_length': 4.016052880075543, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6635427', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1520, 'n_words': 605}