<commit_before>/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/

#include "saveitemsdialog.h"
#include "mainwindow.h"
#include "vcsmanager.h"

#include <coreplugin/ifile.h>

#include <QtCore/QFileInfo>
#include <QtGui/QPushButton>
#include <QtGui/QTreeWidget>
#include <QtGui/QHeaderView>
#include <QtGui/QCheckBox>
#include <QtGui/QPushButton>

Q_DECLARE_METATYPE(Core::IFile*);

using namespace Core;
using namespace Core::Internal;

SaveItemsDialog::SaveItemsDialog(QWidget *parent,
                                 QList<IFile *> items)
    : QDialog(parent)
{
    m_ui.setupUi(this);
    QPushButton *discardButton = m_ui.buttonBox->addButton(tr("Don't Save"), QDialogButtonBox::DestructiveRole);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setDefault(true);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setFocus(Qt::TabFocusReason);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setMinimumWidth(130); // bad magic number to avoid resizing of button

    m_ui.saveBeforeBuildCheckBox->setVisible(false);

    foreach (IFile *file, items) {
        QString visibleName;
        QString directory;
        QString fileName = file->fileName();
        if (fileName.isEmpty()) {
            visibleName = file->suggestedFileName();
        } else {
            QFileInfo info = QFileInfo(fileName);
            directory = info.absolutePath();
            visibleName = info.fileName();
        }
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList()
                                                    << visibleName << QDir::toNativeSeparators(directory));
        item->setData(0, Qt::UserRole, qVariantFromValue(file));
    }

    m_ui.treeWidget->resizeColumnToContents(0);
    m_ui.treeWidget->selectAll();
    updateSaveButton();

    connect(m_ui.buttonBox->button(QDialogButtonBox::Save), SIGNAL(clicked()),
            this, SLOT(collectItemsToSave()));
    connect(discardButton, SIGNAL(clicked()), this, SLOT(discardAll()));
    connect(m_ui.treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(updateSaveButton()));
}

void SaveItemsDialog::setMessage(const QString &msg)
{
    m_ui.msgLabel->setText(msg);
}

void SaveItemsDialog::updateSaveButton()
{
    int count = m_ui.treeWidget->selectedItems().count();
    QPushButton *button = m_ui.buttonBox->button(QDialogButtonBox::Save);
    if (count == m_ui.treeWidget->topLevelItemCount()) {
        button->setEnabled(true);
        button->setText(tr("Save All"));
    } else if (count == 0) {
        button->setEnabled(false);
        button->setText(tr("Save"));
    } else {
        button->setEnabled(true);
        button->setText(tr("Save Selected"));
    }
}

void SaveItemsDialog::collectItemsToSave()
{
    m_itemsToSave.clear();
    foreach (QTreeWidgetItem *item, m_ui.treeWidget->selectedItems()) {
        m_itemsToSave.append(item->data(0, Qt::UserRole).value<IFile*>());
    }
    accept();
}

void SaveItemsDialog::discardAll()
{
    m_ui.treeWidget->clearSelection();
    collectItemsToSave();
}

QList<IFile*> SaveItemsDialog::itemsToSave() const
{
    return m_itemsToSave;
}

void SaveItemsDialog::setAlwaysSaveMessage(const QString &msg)
{
    m_ui.saveBeforeBuildCheckBox->setText(msg);
    m_ui.saveBeforeBuildCheckBox->setVisible(true);
}

bool SaveItemsDialog::alwaysSaveChecked()
{
    return m_ui.saveBeforeBuildCheckBox->isChecked();
}
<commit_msg>compile<commit_after>/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/

#include "saveitemsdialog.h"
#include "mainwindow.h"
#include "vcsmanager.h"

#include <coreplugin/ifile.h>

#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtGui/QPushButton>
#include <QtGui/QTreeWidget>
#include <QtGui/QHeaderView>
#include <QtGui/QCheckBox>
#include <QtGui/QPushButton>

Q_DECLARE_METATYPE(Core::IFile*);

using namespace Core;
using namespace Core::Internal;

SaveItemsDialog::SaveItemsDialog(QWidget *parent,
                                 QList<IFile *> items)
    : QDialog(parent)
{
    m_ui.setupUi(this);
    QPushButton *discardButton = m_ui.buttonBox->addButton(tr("Don't Save"), QDialogButtonBox::DestructiveRole);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setDefault(true);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setFocus(Qt::TabFocusReason);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setMinimumWidth(130); // bad magic number to avoid resizing of button

    m_ui.saveBeforeBuildCheckBox->setVisible(false);

    foreach (IFile *file, items) {
        QString visibleName;
        QString directory;
        QString fileName = file->fileName();
        if (fileName.isEmpty()) {
            visibleName = file->suggestedFileName();
        } else {
            QFileInfo info = QFileInfo(fileName);
            directory = info.absolutePath();
            visibleName = info.fileName();
        }
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList()
                                                    << visibleName << QDir::toNativeSeparators(directory));
        item->setData(0, Qt::UserRole, qVariantFromValue(file));
    }

    m_ui.treeWidget->resizeColumnToContents(0);
    m_ui.treeWidget->selectAll();
    updateSaveButton();

    connect(m_ui.buttonBox->button(QDialogButtonBox::Save), SIGNAL(clicked()),
            this, SLOT(collectItemsToSave()));
    connect(discardButton, SIGNAL(clicked()), this, SLOT(discardAll()));
    connect(m_ui.treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(updateSaveButton()));
}

void SaveItemsDialog::setMessage(const QString &msg)
{
    m_ui.msgLabel->setText(msg);
}

void SaveItemsDialog::updateSaveButton()
{
    int count = m_ui.treeWidget->selectedItems().count();
    QPushButton *button = m_ui.buttonBox->button(QDialogButtonBox::Save);
    if (count == m_ui.treeWidget->topLevelItemCount()) {
        button->setEnabled(true);
        button->setText(tr("Save All"));
    } else if (count == 0) {
        button->setEnabled(false);
        button->setText(tr("Save"));
    } else {
        button->setEnabled(true);
        button->setText(tr("Save Selected"));
    }
}

void SaveItemsDialog::collectItemsToSave()
{
    m_itemsToSave.clear();
    foreach (QTreeWidgetItem *item, m_ui.treeWidget->selectedItems()) {
        m_itemsToSave.append(item->data(0, Qt::UserRole).value<IFile*>());
    }
    accept();
}

void SaveItemsDialog::discardAll()
{
    m_ui.treeWidget->clearSelection();
    collectItemsToSave();
}

QList<IFile*> SaveItemsDialog::itemsToSave() const
{
    return m_itemsToSave;
}

void SaveItemsDialog::setAlwaysSaveMessage(const QString &msg)
{
    m_ui.saveBeforeBuildCheckBox->setText(msg);
    m_ui.saveBeforeBuildCheckBox->setVisible(true);
}

bool SaveItemsDialog::alwaysSaveChecked()
{
    return m_ui.saveBeforeBuildCheckBox->isChecked();
}
<|endoftext|>


<commit_before>/**
 * The Seeks proxy and plugin framework are part of the SEEKS project.
 * Copyright (C) 2010 Loic Dachary <loic@dacary.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 **/

#define _PCREPOSIX_H // avoid pcreposix.h conflict with regex.h used by gtest
#include <gtest/gtest.h>

#include "json_renderer.h"

using namespace seeks_plugins;

TEST(JsonRendererTest, render_engines) {
  EXPECT_EQ("\"google\",\"bing\",\"yauba\",\"yahoo\",\"exalead\",\"twitter\"", json_renderer::render_engines(SE_GOOGLE|SE_BING|SE_YAUBA|SE_YAHOO|SE_EXALEAD|SE_TWITTER));
}

TEST(JsonRendererTest, render_snippets) {
  websearch::_wconfig = new websearch_configuration("not a real filename");
  std::string json_str;

  int current_page = 1;
  std::vector<search_snippet*> snippets;
  const std::string query_clean;
  hash_map<const char*, const char*, hash<const char*>, eqstr> parameters;

  // zero snippet
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_EQ("\"snippets\":[]", json_str);

  // 1 snippet, page 1
  json_str = "";
  snippets.resize(1);
  search_snippet s1;
  s1.set_url("URL1");
  snippets[0] = &s1;
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_TRUE(json_str.find(s1._url) != std::string::npos);
  ASSERT_EQ(std::string::npos, json_str.find("thumb"));

  // 1 snippet, page 1, thumbs on
  json_str = "";
  parameters.insert(std::pair<const char*,const char*>("thumbs", "on"));
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_TRUE(json_str.find(s1._url) != std::string::npos);
  ASSERT_NE(std::string::npos, json_str.find("thumb"));

  // 3 snippets, page 2, 2 result per page, thumbs on
  snippets.resize(3);
  search_snippet s2;
  s2.set_url("URL2");
  snippets[1] = &s2;
  search_snippet s3;
  s3.set_url("URL3");
  snippets[2] = &s3;
  parameters.insert(std::pair<const char*,const char*>("rpp", "2"));

  current_page = 2;
  json_str = "";
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_EQ(std::string::npos, json_str.find(s1._url));
  ASSERT_EQ(std::string::npos, json_str.find(s2._url));
  ASSERT_NE(std::string::npos, json_str.find(s3._url));

  // 3 snippets, page 1, 2 result per page, thumbs on
  current_page = 1;
  json_str = "";
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_NE(std::string::npos, json_str.find(s1._url));
  ASSERT_NE(std::string::npos, json_str.find(s2._url));
  ASSERT_EQ(std::string::npos, json_str.find(s3._url));

  // 3 snippets, page 1, 2 result per page, similarity on, thumbs on
  current_page = 1;
  s1._seeks_ir = 1.0;
  s2._seeks_ir = 0.0;
  s3._seeks_ir = 1.0;
  json_str = "";
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_EQ("\"snippets\":[]", json_str);
  ASSERT_EQ(std::string::npos, json_str.find(s1._url));
  ASSERT_NE(std::string::npos, json_str.find(s2._url));
  ASSERT_EQ(std::string::npos, json_str.find(s3._url));
}

int main(int argc, char **argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  RUN_ALL_TESTS();
}
<commit_msg>implement TEST(JsonRendererTest, render_clustered_snippets)<commit_after>/**
 * The Seeks proxy and plugin framework are part of the SEEKS project.
 * Copyright (C) 2010 Loic Dachary <loic@dacary.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 **/

#define _PCREPOSIX_H // avoid pcreposix.h conflict with regex.h used by gtest
#include <gtest/gtest.h>

#include "json_renderer.h"

using namespace seeks_plugins;

TEST(JsonRendererTest, render_engines) {
  EXPECT_EQ("\"google\",\"bing\",\"yauba\",\"yahoo\",\"exalead\",\"twitter\"", json_renderer::render_engines(SE_GOOGLE|SE_BING|SE_YAUBA|SE_YAHOO|SE_EXALEAD|SE_TWITTER));
}

TEST(JsonRendererTest, render_snippets) {
  websearch::_wconfig = new websearch_configuration("not a real filename");
  std::string json_str;

  int current_page = 1;
  std::vector<search_snippet*> snippets;
  const std::string query_clean;
  hash_map<const char*, const char*, hash<const char*>, eqstr> parameters;

  // zero snippet
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_EQ("\"snippets\":[]", json_str);

  // 1 snippet, page 1
  json_str = "";
  snippets.resize(1);
  search_snippet s1;
  s1.set_url("URL1");
  snippets[0] = &s1;
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_TRUE(json_str.find(s1._url) != std::string::npos);
  ASSERT_EQ(std::string::npos, json_str.find("thumb"));

  // 1 snippet, page 1, thumbs on
  json_str = "";
  parameters.insert(std::pair<const char*,const char*>("thumbs", "on"));
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_TRUE(json_str.find(s1._url) != std::string::npos);
  ASSERT_NE(std::string::npos, json_str.find("thumb"));

  // 3 snippets, page 2, 2 result per page, thumbs on
  snippets.resize(3);
  search_snippet s2;
  s2.set_url("URL2");
  snippets[1] = &s2;
  search_snippet s3;
  s3.set_url("URL3");
  snippets[2] = &s3;
  parameters.insert(std::pair<const char*,const char*>("rpp", "2"));

  current_page = 2;
  json_str = "";
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_EQ(std::string::npos, json_str.find(s1._url));
  ASSERT_EQ(std::string::npos, json_str.find(s2._url));
  ASSERT_NE(std::string::npos, json_str.find(s3._url));

  // 3 snippets, page 1, 2 result per page, thumbs on
  current_page = 1;
  json_str = "";
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_NE(std::string::npos, json_str.find(s1._url));
  ASSERT_NE(std::string::npos, json_str.find(s2._url));
  ASSERT_EQ(std::string::npos, json_str.find(s3._url));

  // 3 snippets, page 1, 2 result per page, similarity on, thumbs on
  current_page = 1;
  s1._seeks_ir = 1.0;
  s2._seeks_ir = 0.0;
  s3._seeks_ir = 1.0;
  json_str = "";
  ASSERT_EQ(SP_ERR_OK, json_renderer::render_snippets(query_clean, current_page, snippets, json_str, &parameters));
  ASSERT_EQ("\"snippets\":[]", json_str);
  ASSERT_EQ(std::string::npos, json_str.find(s1._url));
  ASSERT_NE(std::string::npos, json_str.find(s2._url));
  ASSERT_EQ(std::string::npos, json_str.find(s3._url));
}

TEST(JsonRendererTest, render_clustered_snippets) {
  websearch::_wconfig = new websearch_configuration("not a real filename");
  query_context context;
  cluster clusters[2];

  std::string json_str;

  std::vector<search_snippet*> snippets;
  const std::string query_clean;
  hash_map<const char*, const char*, hash<const char*>, eqstr> parameters;

  search_snippet s1;
  s1.set_url("URL1");
  context.add_to_unordered_cache(&s1);
  clusters[0].add_point(s1._id, NULL);
  clusters[0]._label = "CLUSTER1";

  search_snippet s2;
  s2.set_url("URL2");
  context.add_to_unordered_cache(&s2);
  clusters[1].add_point(s2._id, NULL);
  clusters[1]._label = "CLUSTER2";

  ASSERT_EQ(SP_ERR_OK, json_renderer::render_clustered_snippets(query_clean, clusters, 2, &context, json_str, &parameters));
  ASSERT_NE(std::string::npos, json_str.find(clusters[0]._label));
  ASSERT_NE(std::string::npos, json_str.find(s1._url));
  ASSERT_NE(std::string::npos, json_str.find(clusters[1]._label));
  ASSERT_NE(std::string::npos, json_str.find(s2._url));
}

int main(int argc, char **argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  RUN_ALL_TESTS();
}
<|endoftext|>


<commit_before>#include "sdm/basics.hpp"

#include <gtest/gtest.h>

#include <iostream>
#include <stdexcept>

#include <np-divs/div-funcs/div_l2.hpp>
#include <np-divs/div_params.hpp>

#include "sdm/kernel_projection.hpp"
#include "sdm/kernels.hpp"
#include "sdm/sdm.hpp"

#include <svm.h>

namespace {

using namespace sdm;
using namespace std;

TEST(UtilitiesTest, PSDProjection) {
    double mat[25] = {
        0.8404, -0.6003, -2.1384,  0.1240,  2.9080,
       -0.8880,  0.4900, -0.8396,  1.4367,  0.8252,
        0.1001,  0.7394,  1.3546, -1.9609,  1.3790,
       -0.5445,  1.7119, -1.0722, -0.1977, -1.0582,
        0.3035, -0.1941,  0.9610, -1.2078, -0.4686
    };
    double psd[25] = {
        1.5021, -0.4976, -0.5390, -0.2882,  0.7051,
       -0.4976,  1.0476, -0.1796,  0.7752, -0.2417,
       -0.5390, -0.1796,  1.9070, -1.0633,  0.6634,
       -0.2882,  0.7752, -1.0633,  1.0847, -0.6603,
        0.7051, -0.2417,  0.6634, -0.6603,  0.8627
    };
    project_to_symmetric_psd(mat, 5);

    for (size_t i = 0; i < 25; i++)
        EXPECT_NEAR(mat[i], psd[i], 1e-4) << "i = " << i;
}

TEST(UtilitiesTest, CovarianceProjection) {
    double mat[25] = {
        0.8404, -0.6003, -2.1384,  0.1240,  2.9080,
       -0.8880,  0.4900, -0.8396,  1.4367,  0.8252,
        0.1001,  0.7394,  1.3546, -1.9609,  1.3790,
       -0.5445,  1.7119, -1.0722, -0.1977, -1.0582,
        0.3035, -0.1941,  0.9610, -1.2078, -0.4686
    };
    double kernel[25] = {
        1.0000, -0.4095, -0.2387, -0.2668,  0.6339,
       -0.4095,  1.0000, -0.2026,  0.7296, -0.2502,
       -0.2387, -0.2026,  1.0000, -0.7841,  0.5518,
       -0.2668,  0.7296, -0.7841,  1.0000, -0.6903,
        0.6339, -0.2502,  0.5518, -0.6903,  1.0000
    };
    project_to_covariance(mat, 5);

    for (size_t i = 0; i < 25; i++)
        EXPECT_NEAR(mat[i], kernel[i], 1e-4) << "i = " << i;
}

TEST(SDMTest, BasicTrainingTesting) {
    typedef flann::Matrix<double> Matrix;

    // set up a stupid dataset: 1D N(0,1) vs N(.5, .5) with size-ten samples
    double train11[10] = { 0.5377, 1.8339,-2.2588, 0.8622, 0.3188,-1.3077,-0.4336, 0.3426, 3.5784, 2.7694};
    double train12[10] = { 0.8884,-1.1471,-1.0689,-0.8095,-2.9443, 1.4384, 0.3252,-0.7549, 1.3703,-1.7115};
    double train13[10] = {-0.1022,-0.2414, 0.3192, 0.3129,-0.8649,-0.0301,-0.1649, 0.6277, 1.0933, 1.1093};
    double train14[10] = {-0.8637, 0.0774,-1.2141,-1.1135,-0.0068, 1.5326,-0.7697, 0.3714,-0.2256, 1.1174};
    double train15[10] = {-1.0891, 0.0326, 0.5525, 1.1006, 1.5442, 0.0859,-1.4916,-0.7423,-1.0616, 2.3505};

    double train21[10] = { 1.2097, 0.6458, 0.5989, 1.2938, 0.0978, 0.8483, 0.9175, 0.3781, 0.6078,-0.0829};
    double train22[10] = {-0.0740, 0.5524, 0.8611, 1.7927, 0.1666, 0.5937, 0.4588,-0.4665, 0.2805,-0.3973};
    double train23[10] = { 0.9202, 0.0560, 0.5500, 0.2277, 0.6518, 0.1998, 0.7450, 0.8697, 1.3559, 0.4029};
    double train24[10] = {-0.5692, 0.0802, 1.1773,-0.0361, 0.9805, 0.5620, 1.2183,-0.4804, 0.4012,-0.1039};
    double train25[10] = { 1.9540, 0.9126, 1.1895,-0.0291, 0.2657, 0.3638, 1.0492, 0.3611, 0.8508,-0.5259};

    Matrix train[10] = {
        Matrix(train11, 10, 1),
        Matrix(train12, 10, 1),
        Matrix(train13, 10, 1),
        Matrix(train14, 10, 1),
        Matrix(train15, 10, 1),
        Matrix(train21, 10, 1),
        Matrix(train22, 10, 1),
        Matrix(train23, 10, 1),
        Matrix(train24, 10, 1),
        Matrix(train25, 10, 1)
    };

    std::vector<int> labels(10);
    for (size_t i = 0; i < 5; i++) {
        labels[i] = 0;
        labels[i+5] = 1;
    }

    // set up parameters
    NPDivs::DivL2 div_func;
    GaussianKernel kernel(div_func, .0067);

    NPDivs::DivParams div_params;
    div_params.num_threads = 1;

    // TODO: write a convenince function to set up svm_params, or something
    svm_parameter svm_params;
    svm_params.svm_type = C_SVC;
    svm_params.kernel_type = PRECOMPUTED;
    svm_params.gamma = 0;
    svm_params.degree = 0;
    svm_params.cache_size = 10;
    svm_params.C = .002;
    svm_params.eps = 1e-3;
    svm_params.nr_weight = 0;
    svm_params.shrinking = 0;
    svm_params.probability = 0;

    // train a model
    const SDM<double> &model =
        train_sdm(train, 10, labels, kernel, div_params, svm_params);

    // an N(0, 1) bag
    double test1[10] = {-1.3499, 3.0349, 0.7254,-0.0631, 0.7147,-0.2050,-0.1241, 1.4897, 1.4090, 1.4172};

    // test an N(.5, .5) bag with 5 samples
    double test2[5] = { 0.8357,-0.1037, 0.8586, 1.3151, 0.7444 };

    Matrix test[2] = {
        Matrix(test1, 10, 1),
        Matrix(test2, 5, 1)
    };
    vector< vector<double> > vals(2);

    vector<int> pred_lab = model.predict(test, 2, vals);

    EXPECT_EQ(0, pred_lab[0]);
    cout << "Vals[0]: " << vals[0][0] << " " << vals[0][1] << endl;

    EXPECT_EQ(1, pred_lab[1]);
    cout << "Vals[1]: " << vals[1][0] << " " << vals[1][1] << endl;

}

} // end namespace

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
<commit_msg>switch to SVM with prob estimates, which doesn't work<commit_after>#include "sdm/basics.hpp"

#include <gtest/gtest.h>

#include <iostream>
#include <stdexcept>

#include <np-divs/div-funcs/div_l2.hpp>
#include <np-divs/div_params.hpp>

#include "sdm/kernel_projection.hpp"
#include "sdm/kernels.hpp"
#include "sdm/sdm.hpp"

#include <svm.h>

namespace {

using namespace sdm;
using namespace std;

TEST(UtilitiesTest, PSDProjection) {
    double mat[25] = {
        0.8404, -0.6003, -2.1384,  0.1240,  2.9080,
       -0.8880,  0.4900, -0.8396,  1.4367,  0.8252,
        0.1001,  0.7394,  1.3546, -1.9609,  1.3790,
       -0.5445,  1.7119, -1.0722, -0.1977, -1.0582,
        0.3035, -0.1941,  0.9610, -1.2078, -0.4686
    };
    double psd[25] = {
        1.5021, -0.4976, -0.5390, -0.2882,  0.7051,
       -0.4976,  1.0476, -0.1796,  0.7752, -0.2417,
       -0.5390, -0.1796,  1.9070, -1.0633,  0.6634,
       -0.2882,  0.7752, -1.0633,  1.0847, -0.6603,
        0.7051, -0.2417,  0.6634, -0.6603,  0.8627
    };
    project_to_symmetric_psd(mat, 5);

    for (size_t i = 0; i < 25; i++)
        EXPECT_NEAR(mat[i], psd[i], 1e-4) << "i = " << i;
}

TEST(UtilitiesTest, CovarianceProjection) {
    double mat[25] = {
        0.8404, -0.6003, -2.1384,  0.1240,  2.9080,
       -0.8880,  0.4900, -0.8396,  1.4367,  0.8252,
        0.1001,  0.7394,  1.3546, -1.9609,  1.3790,
       -0.5445,  1.7119, -1.0722, -0.1977, -1.0582,
        0.3035, -0.1941,  0.9610, -1.2078, -0.4686
    };
    double kernel[25] = {
        1.0000, -0.4095, -0.2387, -0.2668,  0.6339,
       -0.4095,  1.0000, -0.2026,  0.7296, -0.2502,
       -0.2387, -0.2026,  1.0000, -0.7841,  0.5518,
       -0.2668,  0.7296, -0.7841,  1.0000, -0.6903,
        0.6339, -0.2502,  0.5518, -0.6903,  1.0000
    };
    project_to_covariance(mat, 5);

    for (size_t i = 0; i < 25; i++)
        EXPECT_NEAR(mat[i], kernel[i], 1e-4) << "i = " << i;
}

TEST(SDMTest, BasicTrainingTesting) {
    typedef flann::Matrix<double> Matrix;

    // set up a stupid dataset: 1D N(0,1) vs N(.5, .5) with size-ten samples
    double train11[10] = { 0.5377, 1.8339,-2.2588, 0.8622, 0.3188,-1.3077,-0.4336, 0.3426, 3.5784, 2.7694};
    double train12[10] = { 0.8884,-1.1471,-1.0689,-0.8095,-2.9443, 1.4384, 0.3252,-0.7549, 1.3703,-1.7115};
    double train13[10] = {-0.1022,-0.2414, 0.3192, 0.3129,-0.8649,-0.0301,-0.1649, 0.6277, 1.0933, 1.1093};
    double train14[10] = {-0.8637, 0.0774,-1.2141,-1.1135,-0.0068, 1.5326,-0.7697, 0.3714,-0.2256, 1.1174};
    double train15[10] = {-1.0891, 0.0326, 0.5525, 1.1006, 1.5442, 0.0859,-1.4916,-0.7423,-1.0616, 2.3505};

    double train21[10] = { 1.2097, 0.6458, 0.5989, 1.2938, 0.0978, 0.8483, 0.9175, 0.3781, 0.6078,-0.0829};
    double train22[10] = {-0.0740, 0.5524, 0.8611, 1.7927, 0.1666, 0.5937, 0.4588,-0.4665, 0.2805,-0.3973};
    double train23[10] = { 0.9202, 0.0560, 0.5500, 0.2277, 0.6518, 0.1998, 0.7450, 0.8697, 1.3559, 0.4029};
    double train24[10] = {-0.5692, 0.0802, 1.1773,-0.0361, 0.9805, 0.5620, 1.2183,-0.4804, 0.4012,-0.1039};
    double train25[10] = { 1.9540, 0.9126, 1.1895,-0.0291, 0.2657, 0.3638, 1.0492, 0.3611, 0.8508,-0.5259};

    Matrix train[10] = {
        Matrix(train11, 10, 1),
        Matrix(train12, 10, 1),
        Matrix(train13, 10, 1),
        Matrix(train14, 10, 1),
        Matrix(train15, 10, 1),
        Matrix(train21, 10, 1),
        Matrix(train22, 10, 1),
        Matrix(train23, 10, 1),
        Matrix(train24, 10, 1),
        Matrix(train25, 10, 1)
    };

    std::vector<int> labels(10);
    for (size_t i = 0; i < 5; i++) {
        labels[i] = 0;
        labels[i+5] = 1;
    }

    // set up parameters
    NPDivs::DivL2 div_func;
    GaussianKernel kernel(div_func, .0067);

    NPDivs::DivParams div_params;
    div_params.num_threads = 1;

    // TODO: write a convenince function to set up svm_params, or something
    svm_parameter svm_params;
    svm_params.svm_type = C_SVC;
    svm_params.kernel_type = PRECOMPUTED;
    svm_params.gamma = 0;
    svm_params.degree = 0;
    svm_params.cache_size = 10;
    svm_params.C = .002;
    svm_params.eps = 1e-3;
    svm_params.nr_weight = 0;
    svm_params.shrinking = 0;
    svm_params.probability = 1;

    // train a model
    const SDM<double> &model =
        train_sdm(train, 10, labels, kernel, div_params, svm_params);

    // an N(0, 1) bag
    double test1[10] = {-1.3499, 3.0349, 0.7254,-0.0631, 0.7147,-0.2050,-0.1241, 1.4897, 1.4090, 1.4172};

    // test an N(.5, .5) bag with 5 samples
    double test2[5] = { 0.8357,-0.1037, 0.8586, 1.3151, 0.7444 };

    Matrix test[2] = {
        Matrix(test1, 10, 1),
        Matrix(test2, 5, 1)
    };
    vector< vector<double> > vals(2);

    vector<int> pred_lab = model.predict(test, 2, vals);

    EXPECT_EQ(0, pred_lab[0]);
    cout << "Vals[0]: " << vals[0][0] << " " << vals[0][1] << endl;

    EXPECT_EQ(1, pred_lab[1]);
    cout << "Vals[1]: " << vals[1][0] << " " << vals[1][1] << endl;

}

} // end namespace

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
<|endoftext|>


<commit_before>//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//

//
// bench.cpp : spdlog benchmarks
//
#include "spdlog/async.h"
#include "spdlog/async_logger.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
#include <iostream>
#include <memory>
#include <string>
#include <thread>

using namespace std;
using namespace std::chrono;
using namespace spdlog;
using namespace spdlog::sinks;
using namespace utils;

size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);

int main(int argc, char *argv[])
{

    int howmany;
    int tp_queue_size;
    int tp_threads = 1;
    int client_threads = 10;
    int iters = 10;

    try
    {
        if (argc < 2)
        {
            cout << "Usage: " << argv[0] << " <howmany> [client_threads] [q_size] [tp_threads]" << endl;
            return (1);
        }

        howmany = atoi(argv[1]);
        if (argc > 2)
            client_threads = atoi(argv[2]);

        if (argc > 3)
            tp_queue_size = atoi(argv[3]);
        else
            tp_queue_size = howmany;

        if (argc > 4)
            tp_threads = atoi(argv[4]);

        cout << "\n*******************************************************************************\n";
        cout << "messages:\t" << format(howmany) << endl;
        cout << "client_threads:\t" << client_threads << endl;
        cout << "tp queue:\t" << format(tp_queue_size) << endl;
        cout << "tp threads:\t" << tp_threads << endl;
        cout << "*******************************************************************************\n";

        size_t total_rate = 0;

        for (int i = 0; i < iters; ++i)
        {
            spdlog::init_thread_pool(tp_queue_size, tp_threads);
            // auto as = spdlog::daily_logger_st("as", "logs/daily_async");
            auto as = spdlog::create_async_logger<null_sink_st>("async(null-sink)");
            total_rate += bench_as(howmany, as, client_threads);
            spdlog::drop("async(null-sink)");
        }
        std::cout << endl;
        std::cout << "Avg rate: " << format(total_rate / iters) << "/sec" << std::endl;
    }
    catch (std::exception &ex)
    {
        std::cerr << "Error: " << ex.what() << std::endl;
        perror("Last error");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

// return rate/sec
size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{
    cout << log->name() << "...\t\t" << flush;
    std::atomic<int> msg_counter{0};
    vector<thread> threads;
    auto start = system_clock::now();
    for (int t = 0; t < thread_count; ++t)
    {
        threads.push_back(std::thread([&]() {
            for (;;)
            {
                int counter = ++msg_counter;
                if (counter > howmany)
                    break;
                log->info("Hello logger: msg number {}", counter);
            }
        }));
    }

    for (auto &t : threads)
    {
        t.join();
    }

    auto delta = system_clock::now() - start;
    auto delta_d = duration_cast<duration<double>>(delta).count();
    auto per_sec = size_t(howmany / delta_d);
    cout << format(per_sec) << "/sec" << endl;
    return per_sec;
}
<commit_msg>update bench<commit_after>//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//

//
// bench.cpp : spdlog benchmarks
//
#include "spdlog/async.h"
#include "spdlog/async_logger.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
#include <iostream>
#include <memory>
#include <string>
#include <thread>

using namespace std;
using namespace std::chrono;
using namespace spdlog;
using namespace spdlog::sinks;
using namespace utils;

size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);

int main(int argc, char *argv[])
{

    int howmany;
    int tp_queue_size;
    int tp_threads = 1;
    int client_threads = 10;
    int iters = 10;

    try
    {
        if (argc < 2)
        {
            cout << "Usage: " << argv[0] << " <howmany> [client_threads] [q_size] [tp_threads]" << endl;
            return (1);
        }

        howmany = atoi(argv[1]);
        if (argc > 2)
            client_threads = atoi(argv[2]);

        if (argc > 3)
            tp_queue_size = atoi(argv[3]);
        else
            tp_queue_size = howmany;

        if (argc > 4)
            tp_threads = atoi(argv[4]);

        cout << "\n*******************************************************************************\n";
        cout << "messages:\t" << format(howmany) << endl;
        cout << "client_threads:\t" << client_threads << endl;
        cout << "tp queue:\t" << format(tp_queue_size) << endl;
        cout << "tp threads:\t" << tp_threads << endl;
        cout << "*******************************************************************************\n";

        size_t total_rate = 0;
		spdlog::init_thread_pool(tp_queue_size, tp_threads);
        for (int i = 0; i < iters; ++i)
        {
            auto as = spdlog::create_async_logger<null_sink_mt>("async(null-sink)");
            total_rate += bench_as(howmany, as, client_threads);
            spdlog::drop("async(null-sink)");
			spdlog::details::registry::instance().get_thread_pool()->wait_empty ();
        }
        std::cout << endl;
        std::cout << "Avg rate: " << format(total_rate / iters) << "/sec" << std::endl;

    }
    catch (std::exception &ex)
    {
        std::cerr << "Error: " << ex.what() << std::endl;
        perror("Last error");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

// return rate/sec
size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{
    cout << log->name() << "...\t\t" << flush;
    std::atomic<int> msg_counter{0};
    vector<thread> threads;
    auto start = system_clock::now();
    for (int t = 0; t < thread_count; ++t)
    {
        threads.push_back(std::thread([&]() {
            for (;;)
            {
                int counter = ++msg_counter;
                if (counter > howmany)
                    break;
                log->info("Hello logger: msg number {}", counter);
            }
        }));
    }

    for (auto &t : threads)
    {
        t.join();
    }

    auto delta = system_clock::now() - start;
    auto delta_d = duration_cast<duration<double>>(delta).count();
    auto per_sec = size_t(howmany / delta_d);
    cout << format(per_sec) << "/sec" << endl;
    return per_sec;
}
<|endoftext|>


<commit_before>//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//

//
// bench.cpp : spdlog benchmarks
//
#include "spdlog/async.h"
#include "spdlog/async_logger.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
#include <iostream>
#include <memory>
#include <string>
#include <thread>

using namespace std;
using namespace std::chrono;
using namespace spdlog;
using namespace spdlog::sinks;
using namespace utils;

size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);

int main(int argc, char *argv[])
{

    int howmany;
    int tp_queue_size;
    int tp_threads = 1;
    int client_threads = 10;
    int iters = 10;

    try
    {
        if (argc < 2)
        {
            cout << "Usage: " << argv[0] << " <howmany> [q_size] [client_threads] [tp_threads]" << endl;
            return (1);
        }

        howmany = atoi(argv[1]);
        if (argc > 2)
            client_threads = atoi(argv[2]);

        if (argc > 3)
            tp_queue_size = atoi(argv[3]);
        else
            tp_queue_size = howmany;

        if (argc > 4)
            tp_threads = atoi(argv[4]);

        cout << "\n*******************************************************************************\n";
        cout << "client_threads:\t" << client_threads << endl;
        cout << "messages:\t" << format(howmany) << endl;
        cout << "tp queue:\t" << format(tp_queue_size) << endl;
        cout << "tp threads:\t" << tp_threads << endl;
        cout << "*******************************************************************************\n";

        size_t total_rate = 0;

        for (int i = 0; i < iters; ++i)
        {
            spdlog::init_thread_pool(tp_queue_size, tp_threads);
            // auto as = spdlog::daily_logger_st("as", "logs/daily_async");
            auto as = spdlog::create_async_logger<null_sink_st>("async(null-sink)");
            total_rate += bench_as(howmany, as, client_threads);
            spdlog::drop("async(null-sink)");
        }
        std::cout << endl;
        std::cout << "Avg rate: " << format(total_rate / iters) << "/sec" << std::endl;
    }
    catch (std::exception &ex)
    {
        std::cerr << "Error: " << ex.what() << std::endl;
        perror("Last error");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

// return rate/sec
size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{
    cout << log->name() << "...\t\t" << flush;
    std::atomic<int> msg_counter{0};
    vector<thread> threads;
    auto start = system_clock::now();
    for (int t = 0; t < thread_count; ++t)
    {
        threads.push_back(std::thread([&]() {
            for (;;)
            {
                int counter = ++msg_counter;
                if (counter > howmany)
                    break;
                log->info("Hello logger: msg number {}", counter);
            }
        }));
    }

    for (auto &t : threads)
    {
        t.join();
    }

    auto delta = system_clock::now() - start;
    auto delta_d = duration_cast<duration<double>>(delta).count();
    auto per_sec = size_t(howmany / delta_d);
    cout << format(per_sec) << "/sec" << endl;
    return per_sec;
}
<commit_msg>Fixed usage message in async null bench<commit_after>//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//

//
// bench.cpp : spdlog benchmarks
//
#include "spdlog/async.h"
#include "spdlog/async_logger.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
#include <iostream>
#include <memory>
#include <string>
#include <thread>

using namespace std;
using namespace std::chrono;
using namespace spdlog;
using namespace spdlog::sinks;
using namespace utils;

size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);

int main(int argc, char *argv[])
{

    int howmany;
    int tp_queue_size;
    int tp_threads = 1;
    int client_threads = 10;
    int iters = 10;

    try
    {
        if (argc < 2)
        {
            cout << "Usage: " << argv[0] << " <howmany> [client_threads] [q_size] [tp_threads]" << endl;
            return (1);
        }

        howmany = atoi(argv[1]);
        if (argc > 2)
            client_threads = atoi(argv[2]);

        if (argc > 3)
            tp_queue_size = atoi(argv[3]);
        else
            tp_queue_size = howmany;

        if (argc > 4)
            tp_threads = atoi(argv[4]);

        cout << "\n*******************************************************************************\n";
        cout << "client_threads:\t" << client_threads << endl;
        cout << "messages:\t" << format(howmany) << endl;
        cout << "tp queue:\t" << format(tp_queue_size) << endl;
        cout << "tp threads:\t" << tp_threads << endl;
        cout << "*******************************************************************************\n";

        size_t total_rate = 0;

        for (int i = 0; i < iters; ++i)
        {
            spdlog::init_thread_pool(tp_queue_size, tp_threads);
            // auto as = spdlog::daily_logger_st("as", "logs/daily_async");
            auto as = spdlog::create_async_logger<null_sink_st>("async(null-sink)");
            total_rate += bench_as(howmany, as, client_threads);
            spdlog::drop("async(null-sink)");
        }
        std::cout << endl;
        std::cout << "Avg rate: " << format(total_rate / iters) << "/sec" << std::endl;
    }
    catch (std::exception &ex)
    {
        std::cerr << "Error: " << ex.what() << std::endl;
        perror("Last error");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

// return rate/sec
size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{
    cout << log->name() << "...\t\t" << flush;
    std::atomic<int> msg_counter{0};
    vector<thread> threads;
    auto start = system_clock::now();
    for (int t = 0; t < thread_count; ++t)
    {
        threads.push_back(std::thread([&]() {
            for (;;)
            {
                int counter = ++msg_counter;
                if (counter > howmany)
                    break;
                log->info("Hello logger: msg number {}", counter);
            }
        }));
    }

    for (auto &t : threads)
    {
        t.join();
    }

    auto delta = system_clock::now() - start;
    auto delta_d = duration_cast<duration<double>>(delta).count();
    auto per_sec = size_t(howmany / delta_d);
    cout << format(per_sec) << "/sec" << endl;
    return per_sec;
}
<|endoftext|>


<commit_before>#include <cstddef>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

#include "xtensor-python/pytensor.hpp"

#include "nifty/graph/rag/grid_rag.hxx"
#include "nifty/graph/rag/feature_accumulation/grid_rag_affinity_features.hxx"

namespace py = pybind11;

namespace nifty{
namespace graph{

    using namespace py;

    template<class RAG, unsigned DIM>
    void exportAccumulateAffinityFeaturesT(
        py::module & ragModule
    ){
        // features for lifted nh not supported right now
        /*
        ragModule.def("computeFeaturesAndNhFromAffinities",
        [](
            const RAG & rag,
            xt::pytensor<float, DIM+1> affinities,
            const std::vector<std::vector<int>> & offsets,
            const int numberOfThreads
        ){

            // TODO
            //{
            //    py::gil_scoped_release allowThreads;
            //}
            LiftedNh<RAG> lnh(
                rag, offsets.begin(), offsets.end(), numberOfThreads
            );

            int64_t nLocal  = rag.edgeIdUpperBound() + 1;
            int64_t nLifted = lnh.edgeIdUpperBound() + 1;
            xt::pytensor<float, 2> outLocal({nLocal, int64_t(10)});
            xt::pytensor<float, 2> outLifted({nLifted, int64_t(10)});
            {
                py::gil_scoped_release allowThreads;
                accumulateLongRangeAffinities(rag, lnh, affinities, 0., 1.,  outLocal, outLifted, numberOfThreads);
            }
            xt::pytensor<uint32_t, 2> lnhOut({nLifted, int64_t(2)});
            {
                py::gil_scoped_release allowThreads;
                for(size_t e = 0; e < nLifted; ++e) {
                    lnhOut(e, 0) = lnh.u(e);
                    lnhOut(e, 1) = lnh.v(e);
                }
            }
            return std::make_tuple(lnhOut, outLocal, outLifted);
        },
        py::arg("rag"),
        py::arg("affinities"),
        py::arg("offsets"),
        py::arg("numberOfThreads")= -1
        );
        */

        ragModule.def("featuresFromLocalAffinities",
        [](
            const RAG & rag,
            const xt::pytensor<float, DIM+1> & affinities,
            const std::vector<std::array<int, 3>> & offsets,
            const float min, const float max,
            const int numberOfThreads
        ){

            int64_t nEdges = rag.numberOfEdges();
            typename xt::pytensor<float, 2>::shape_type shape = {nEdges, int64_t(9)};
            xt::pytensor<float, 2> out(shape);
            {
                py::gil_scoped_release allowThreads;
                accumulateAffinities(rag, affinities, offsets, out, min, max, numberOfThreads);
            }
            return out;
        }, py::arg("rag"),
           py::arg("affinities"),
           py::arg("offsets"),
           py::arg("min")=0., py::arg("max")=0.,
           py::arg("numberOfThreads")=-1
        );
    }

    void exportAccumulateAffinityFeatures(py::module & ragModule) {
        typedef xt::pytensor<uint32_t, 3> ExplicitPyLabels3D;
        typedef GridRag<3, ExplicitPyLabels3D> Rag3d;
        exportAccumulateAffinityFeaturesT<Rag3d, 3>(ragModule);
    }

}
}
<commit_msg>Rename rag affinity features<commit_after>#include <cstddef>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

#include "xtensor-python/pytensor.hpp"

#include "nifty/graph/rag/grid_rag.hxx"
#include "nifty/graph/rag/feature_accumulation/grid_rag_affinity_features.hxx"

namespace py = pybind11;

namespace nifty{
namespace graph{

    using namespace py;

    template<class RAG, unsigned DIM>
    void exportAccumulateAffinityFeaturesT(
        py::module & ragModule
    ){
        // features for lifted nh not supported right now
        /*
        ragModule.def("computeFeaturesAndNhFromAffinities",
        [](
            const RAG & rag,
            xt::pytensor<float, DIM+1> affinities,
            const std::vector<std::vector<int>> & offsets,
            const int numberOfThreads
        ){

            // TODO
            //{
            //    py::gil_scoped_release allowThreads;
            //}
            LiftedNh<RAG> lnh(
                rag, offsets.begin(), offsets.end(), numberOfThreads
            );

            int64_t nLocal  = rag.edgeIdUpperBound() + 1;
            int64_t nLifted = lnh.edgeIdUpperBound() + 1;
            xt::pytensor<float, 2> outLocal({nLocal, int64_t(10)});
            xt::pytensor<float, 2> outLifted({nLifted, int64_t(10)});
            {
                py::gil_scoped_release allowThreads;
                accumulateLongRangeAffinities(rag, lnh, affinities, 0., 1.,  outLocal, outLifted, numberOfThreads);
            }
            xt::pytensor<uint32_t, 2> lnhOut({nLifted, int64_t(2)});
            {
                py::gil_scoped_release allowThreads;
                for(size_t e = 0; e < nLifted; ++e) {
                    lnhOut(e, 0) = lnh.u(e);
                    lnhOut(e, 1) = lnh.v(e);
                }
            }
            return std::make_tuple(lnhOut, outLocal, outLifted);
        },
        py::arg("rag"),
        py::arg("affinities"),
        py::arg("offsets"),
        py::arg("numberOfThreads")= -1
        );
        */

        ragModule.def("accumulateAffinityStandartFeatures",
        [](
            const RAG & rag,
            const xt::pytensor<float, DIM+1> & affinities,
            const std::vector<std::array<int, 3>> & offsets,
            const float min, const float max,
            const int numberOfThreads
        ){

            int64_t nEdges = rag.numberOfEdges();
            typename xt::pytensor<float, 2>::shape_type shape = {nEdges, int64_t(9)};
            xt::pytensor<float, 2> out(shape);
            {
                py::gil_scoped_release allowThreads;
                accumulateAffinities(rag, affinities, offsets, out, min, max, numberOfThreads);
            }
            return out;
        }, py::arg("rag"),
           py::arg("affinities"),
           py::arg("offsets"),
           py::arg("min")=0., py::arg("max")=1.,
           py::arg("numberOfThreads")=-1
        );
    }

    void exportAccumulateAffinityFeatures(py::module & ragModule) {
        typedef xt::pytensor<uint32_t, 3> ExplicitPyLabels3D;
        typedef GridRag<3, ExplicitPyLabels3D> Rag3d;
        exportAccumulateAffinityFeaturesT<Rag3d, 3>(ragModule);
    }

}
}
<|endoftext|>


<commit_before>/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <string>
#include <testHelpers.hpp>

using std::endl;
using std::string;
using std::vector;
using af::array;
using af::cfloat;
using af::cdouble;
using af::freeHost;
using af::dim4;
using af::dtype_traits;

template<typename T>
class Write : public ::testing::Test
{
    public:
        virtual void SetUp() {
        }
};

// create a list of types to be tested
typedef ::testing::Types<float, cfloat, double, cdouble, int, unsigned, char, unsigned char, short, ushort> TestTypes;

// register the type list
TYPED_TEST_CASE(Write, TestTypes);

template<typename T>
void writeTest(dim4 dims)
{
    if (noDoubleTests<T>()) return;

    array A = randu(dims, (af_dtype) dtype_traits<T>::af_type);
    array B = randu(dims, (af_dtype) dtype_traits<T>::af_type);

    array A_copy = A.copy();
    array B_copy = B.copy();

    T *a_host = A.host<T>();
    T *b_dev  = B.device<T>();

    A.write(b_dev, dims.elements() * sizeof(T), afDevice);
    B.write(a_host, dims.elements() * sizeof(T), afHost);

    ASSERT_ARRAYS_EQ(B_copy, A);
    ASSERT_ARRAYS_EQ(A_copy, B);

    af_free_device(b_dev);
    freeHost(a_host);
}

TYPED_TEST(Write, Vector0)
{
    writeTest<TypeParam>(dim4(10));
}

TYPED_TEST(Write, Vector1)
{
    writeTest<TypeParam>(dim4(1000));
}

TYPED_TEST(Write, Matrix0)
{
    writeTest<TypeParam>(dim4(64, 8));
}

TYPED_TEST(Write, Matrix1)
{
    writeTest<TypeParam>(dim4(256, 256));
}

TYPED_TEST(Write, Volume0)
{
    writeTest<TypeParam>(dim4(10, 10, 10));
}

TYPED_TEST(Write, Volume1)
{
    writeTest<TypeParam>(dim4(32, 64, 16));
}

TEST(Write, VoidPointer) {
  vector<float> gold(100, 5);

  array a(100);

  void* h_gold = (void*)&gold.front();
  a.write(h_gold, 100 * sizeof(float), afHost);

  ASSERT_VEC_ARRAY_EQ(gold, dim4(100), a);
}
<commit_msg>Fixed failing `write` tests.<commit_after>/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <string>
#include <testHelpers.hpp>

using std::endl;
using std::string;
using std::vector;
using af::array;
using af::cfloat;
using af::cdouble;
using af::freeHost;
using af::dim4;
using af::dtype_traits;

template<typename T>
class Write : public ::testing::Test
{
    public:
        virtual void SetUp() {
        }
};

// create a list of types to be tested
typedef ::testing::Types<float, cfloat, double, cdouble, int, unsigned, char, unsigned char, short, ushort> TestTypes;

// register the type list
TYPED_TEST_CASE(Write, TestTypes);

template<typename T>
void writeTest(dim4 dims)
{
    if (noDoubleTests<T>()) return;

    array A = randu(dims, (af_dtype) dtype_traits<T>::af_type);
    array B = randu(dims, (af_dtype) dtype_traits<T>::af_type);

    array A_copy = A.copy();
    array B_copy = B.copy();

    T *a_host = A.host<T>();
    T *b_dev  = B.device<T>();

    A.write(b_dev, dims.elements() * sizeof(T), afDevice);
    B.write(a_host, dims.elements() * sizeof(T), afHost);

    ASSERT_ARRAYS_EQ(B_copy, A);
    ASSERT_ARRAYS_EQ(A_copy, B);

    freeHost(a_host);
}

TYPED_TEST(Write, Vector0)
{
    writeTest<TypeParam>(dim4(10));
}

TYPED_TEST(Write, Vector1)
{
    writeTest<TypeParam>(dim4(1000));
}

TYPED_TEST(Write, Matrix0)
{
    writeTest<TypeParam>(dim4(64, 8));
}

TYPED_TEST(Write, Matrix1)
{
    writeTest<TypeParam>(dim4(256, 256));
}

TYPED_TEST(Write, Volume0)
{
    writeTest<TypeParam>(dim4(10, 10, 10));
}

TYPED_TEST(Write, Volume1)
{
    writeTest<TypeParam>(dim4(32, 64, 16));
}

TEST(Write, VoidPointer) {
  vector<float> gold(100, 5);

  array a(100);

  void* h_gold = (void*)&gold.front();
  a.write(h_gold, 100 * sizeof(float), afHost);

  ASSERT_VEC_ARRAY_EQ(gold, dim4(100), a);
}
<|endoftext|>


<commit_before>#pragma once
//=====================================================================//
/*!	@file
	@brief	DS3231 RTC ドライバー @n
			中華製モジュールの注意点：@n
			・バッテリーバックアップにリチウム電池を使う場合、直列抵抗を除く事
    @author 平松邦仁 (hira@rvf-rc45.net)
	@copyright	Copyright (C) 2016, 2020 Kunihito Hiramatsu @n
				Released under the MIT license @n
				https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <cstdint>
#include "common/iica_io.hpp"
#include "common/time.h"

namespace chip {

	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
	/*!
		@brief  DS3231 RTC テンプレートクラス
		@param[in]	I2C		i2c クラス
	*/
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
	template <class I2C>
	class DS3231 {

		// R/W ビットを含まない７ビット値
		static const uint8_t DS3231_ADR_ = 0x68;

		I2C&	i2c_;

		bool	start_;

		struct reg_t {
			uint8_t	reg[7];
			bool operator != (const reg_t& t) {
				for(uint8_t i = 0; i < 7; ++i) {
					if(reg[i] != t.reg[i]) return true;
				}
				return false;
			}
		};

		bool get_time_(reg_t& t) const {
			uint8_t reg[1];
			reg[0] = 0x00;	// set address
			if(!i2c_.send(DS3231_ADR_, reg, 1)) {
				return false;
			}
			if(!i2c_.recv(DS3231_ADR_, &t.reg[0], 7)) {
				return false;
			}
			return true;
		}

	public:
		//-----------------------------------------------------------------//
		/*!
			@brief	コンストラクター
			@param[in]	i2c	I2C クラスを参照で渡す
		 */
		//-----------------------------------------------------------------//
		DS3231(I2C& i2c) : i2c_(i2c), start_(false) { }

		//-----------------------------------------------------------------//
		/*!
			@brief	開始
			@return エラーなら「false」を返す
		 */
		//-----------------------------------------------------------------//
		bool start() {
			uint8_t reg[2];
			reg[0] = 0x0e;	/// internal register address
			reg[1] = 0x00;
			start_ = i2c_.send(DS3231_ADR_, reg, 2);
			return start_;
		}

		//-----------------------------------------------------------------//
		/*!
			@brief	DS3231 時間設定
			@param[in]	t	時間
			@return 成功なら「true」
		 */
		//-----------------------------------------------------------------//
		bool set_time(time_t t) const {
			if(!start_) return false;

			const tm* tp = gmtime(&t);
			uint8_t reg[7];
			reg[0] = ((tp->tm_sec  / 10) << 4) | (tp->tm_sec  % 10);  // 0 to 59
			reg[1] = ((tp->tm_min  / 10) << 4) | (tp->tm_min  % 10);  // 0 to 59
			reg[2] = ((tp->tm_hour / 10) << 4) | (tp->tm_hour % 10);  // 0 to 23
			reg[3] = tp->tm_wday + 1;  // 1 to 7
			reg[4] = ((tp->tm_mday / 10) << 4) | (tp->tm_mday % 10);  // 1 to 31
			uint8_t mon = tp->tm_mon + 1;
			reg[5] = ((mon / 10) << 4) | (mon % 10);  // 1 to 12
			uint16_t y = tp->tm_year % 100;
			reg[6] = ((y / 10) << 4) | (y % 10);  // 0 to 99
			return i2c_.send(DS3231_ADR_, 0x00, reg, 7);
		}

		//-----------------------------------------------------------------//
		/*!
			@brief	DS3231 時間呼び出し
			@param[out]	t	取得時間
			@return 成功なら「true」
		 */
		//-----------------------------------------------------------------//
		bool get_time(time_t& t) const {
			if(!start_) return false;

			reg_t tt;
			reg_t tmp;
			tm ts;
			// 二度読んで、同じだったら正しい時間とする
			uint8_t n = 5; // ５回ループして正常に読めなかったら、エラーとする
			do {
				tmp = tt;
				if(!get_time_(tt)) return false;
				--n;
				if(n == 0) {
					return false;
				}
			} while(tt != tmp) ;

			ts.tm_sec  = ((tt.reg[0] >> 4) * 10) + (tt.reg[0] & 0xf);
			ts.tm_min  = ((tt.reg[1] >> 4) * 10) + (tt.reg[1] & 0xf);
			ts.tm_hour = ((tt.reg[2] >> 4) * 10) + (tt.reg[2] & 0xf);
			ts.tm_mday = ((tt.reg[4] >> 4) * 10) + (tt.reg[4] & 0xf);
			ts.tm_mon  = ((((tt.reg[5] & 0x10) >> 4) * 10) + (tt.reg[5] & 0xf)) - 1;
			ts.tm_year = ((tt.reg[6] >> 4) * 10) + (tt.reg[6] & 0xf);
			ts.tm_year += 100;
			t = mktime_gmt(&ts);
			return true;
		}
	};
}
<commit_msg>Update: cleanup<commit_after>#pragma once
//=====================================================================//
/*!	@file
	@brief	DS3231 RTC ドライバー @n
			中華製モジュールの注意点：@n
			・バッテリーバックアップにリチウム電池を使う場合、直列抵抗を除く事
    @author 平松邦仁 (hira@rvf-rc45.net)
	@copyright	Copyright (C) 2016, 2020 Kunihito Hiramatsu @n
				Released under the MIT license @n
				https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <cstdint>
#include "common/iica_io.hpp"
#include "common/time.h"

namespace chip {

	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
	/*!
		@brief  DS3231 RTC テンプレートクラス
		@param[in]	I2C		i2c クラス
	*/
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
	template <class I2C>
	class DS3231 {

		// R/W ビットを含まない７ビット値
		static const uint8_t DS3231_ADR_ = 0x68;

		I2C&	i2c_;

		bool	start_;

		struct reg_t {
			uint8_t	reg[7];
			bool operator != (const reg_t& t) {
				for(uint8_t i = 0; i < 7; ++i) {
					if(reg[i] != t.reg[i]) return true;
				}
				return false;
			}
		};

		bool get_time_(reg_t& t) const {
			uint8_t reg[1];
			reg[0] = 0x00;	// set address
			if(!i2c_.send(DS3231_ADR_, reg, 1)) {
				return false;
			}
			if(!i2c_.recv(DS3231_ADR_, &t.reg[0], 7)) {
				return false;
			}
			return true;
		}

	public:
		//-----------------------------------------------------------------//
		/*!
			@brief	コンストラクター
			@param[in]	i2c	I2C クラスを参照で渡す
		 */
		//-----------------------------------------------------------------//
		DS3231(I2C& i2c) : i2c_(i2c), start_(false) { }

		//-----------------------------------------------------------------//
		/*!
			@brief	開始
			@return エラーなら「false」を返す
		 */
		//-----------------------------------------------------------------//
		bool start() {
			uint8_t reg[2];
			reg[0] = 0x0e;	/// internal register address
			reg[1] = 0x00;
			start_ = i2c_.send(DS3231_ADR_, reg, 2);
			return start_;
		}

		//-----------------------------------------------------------------//
		/*!
			@brief	DS3231 時間設定
			@param[in]	t	時間
			@return 成功なら「true」
		 */
		//-----------------------------------------------------------------//
		bool set_time(time_t t) const {
			if(!start_) return false;

			tm tmt;
			gmtime_r(&t, &tmt);
			uint8_t reg[7];
			reg[0] = ((tmt.tm_sec  / 10) << 4) | (tmt.tm_sec  % 10);  // 0 to 59
			reg[1] = ((tmt.tm_min  / 10) << 4) | (tmt.tm_min  % 10);  // 0 to 59
			reg[2] = ((tmt.tm_hour / 10) << 4) | (tmt.tm_hour % 10);  // 0 to 23
			reg[3] = tmt.tm_wday + 1;  // 1 to 7
			reg[4] = ((tmt.tm_mday / 10) << 4) | (tmt.tm_mday % 10);  // 1 to 31
			uint8_t mon = tmt.tm_mon + 1;
			reg[5] = ((mon / 10) << 4) | (mon % 10);  // 1 to 12
			uint16_t y = tmt.tm_year % 100;
			reg[6] = ((y / 10) << 4) | (y % 10);  // 0 to 99
			return i2c_.send(DS3231_ADR_, 0x00, reg, 7);
		}

		//-----------------------------------------------------------------//
		/*!
			@brief	DS3231 時間呼び出し
			@param[out]	t	取得時間
			@return 成功なら「true」
		 */
		//-----------------------------------------------------------------//
		bool get_time(time_t& t) const {
			if(!start_) return false;

			reg_t tt;
			reg_t tmp;
			tm ts;
			// 二度読んで、同じだったら正しい時間とする
			uint8_t n = 5; // ５回ループして正常に読めなかったら、エラーとする
			do {
				tmp = tt;
				if(!get_time_(tt)) return false;
				--n;
				if(n == 0) {
					return false;
				}
			} while(tt != tmp) ;

			ts.tm_sec  = ((tt.reg[0] >> 4) * 10) + (tt.reg[0] & 0xf);
			ts.tm_min  = ((tt.reg[1] >> 4) * 10) + (tt.reg[1] & 0xf);
			ts.tm_hour = ((tt.reg[2] >> 4) * 10) + (tt.reg[2] & 0xf);
			ts.tm_mday = ((tt.reg[4] >> 4) * 10) + (tt.reg[4] & 0xf);
			ts.tm_mon  = ((((tt.reg[5] & 0x10) >> 4) * 10) + (tt.reg[5] & 0xf)) - 1;
			ts.tm_year = ((tt.reg[6] >> 4) * 10) + (tt.reg[6] & 0xf);
			ts.tm_year += 100;
			t = mktime_gmt(&ts);
			return true;
		}
	};
}
<|endoftext|>


<commit_before>#include "ROOT/RDataFrame.hxx"
#include "ROOT/TSeq.hxx"
#include "ROOT/RTrivialDS.hxx"
#include "TH1F.h"
#include "TRandom.h"
#include "TSystem.h"

#include "gtest/gtest.h"

#include <algorithm>

using namespace ROOT::RDF;
using namespace ROOT::VecOps;

TEST(Cache, FundType)
{
   ROOT::RDataFrame tdf(5);
   int i = 1;
   auto cached =
      tdf.Define("c0", [&i]() { return i++; }).Define("c1", []() { return 1.; }).Cache<int, double>({"c0", "c1"});

   auto c = cached.Count();
   auto m = cached.Min<int>("c0");
   auto v = *cached.Take<int>("c0");

   EXPECT_EQ(1, *m);
   EXPECT_EQ(5UL, *c);
   for (auto j : ROOT::TSeqI(5)) {
      EXPECT_EQ(j + 1, v[j]);
   }
}

TEST(Cache, Ambiguity)
{
// This test verifies that the correct method is called and there is no ambiguity between the JIT call to Cache using
// a column list as a parameter and the JIT call to Cache using the Regexp.
ROOT::RDataFrame tdf(5);
int i = 1;
auto d = tdf.Define("c0", [&i]() { return i++; }).Define("c1", []() { return 1.; });

auto cached_1 = d.Cache({"c0"});
auto cached_2 = d.Cache({"c0", "c1"});

auto c_1 = cached_1.Count();
auto c_2 = cached_2.Count();

EXPECT_EQ(5UL, *c_1);
EXPECT_EQ(5UL, *c_2);
}

/*
// The contiguity has been dropped since now caching is backed up by a TDS
// TODO: we can optimise this. The reason why addresses are not contiguous with
// data sources (now cache is achieved with a data source) is linked to Snapshot.
// Indeed we need to set an address for each branch and keep that constant
// through the entire event loop (this is how the TTree works!).
// We could remove this constraint from data sources if Snapshot offered some
// sort of cache to which the data is copied.
TEST(Cache, Contiguity)
{
   ROOT::RDataFrame tdf(2);
   auto f = 0.f;
   auto cached = tdf.Define("float", [&f]() { return f++; }).Cache<float>({"float"});
   int counter = 0;
   float *fPrec = nullptr;
   auto count = [&counter, &fPrec](float &ff) {
      if (1 == counter++) {
         EXPECT_EQ(1, std::distance(fPrec, &ff));
      }
      fPrec = &ff;
   };
   cached.Foreach(count, {"float"});
}
*/

TEST(Cache, Class)
{
   TH1F h("", "h", 64, 0, 1);
   gRandom->SetSeed(1);
   h.FillRandom("gaus", 10);
   ROOT::RDataFrame tdf(1);
   auto cached = tdf.Define("c0", [&h]() { return h; }).Cache<TH1F>({"c0"});

   auto c = cached.Count();
   auto d = cached.Define("Mean", [](TH1F &hh) { return hh.GetMean(); }, {"c0"})
               .Define("StdDev", [](TH1F &hh) { return hh.GetStdDev(); }, {"c0"});
   auto m = d.Max<double>("Mean");
   auto s = d.Max<double>("StdDev");

   EXPECT_EQ(h.GetMean(), *m);
   EXPECT_EQ(h.GetStdDev(), *s);
   EXPECT_EQ(1UL, *c);
}

TEST(Cache, RunTwiceOnCached)
{
   auto nevts = 10U;
   ROOT::RDataFrame tdf(nevts);
   auto f = 0.f;
   auto nCalls = 0U;
   auto orig = tdf.Define("float", [&f, &nCalls]() {
      nCalls++;
      return f++;
   });

   auto cached = orig.Cache<float>({"float"});
   EXPECT_EQ(0U, nCalls);
   auto m0 = cached.Mean<float>("float");
   *m0;
   EXPECT_EQ(nevts, nCalls);
   cached.Foreach([]() {});               // run the event loop
   auto m1 = cached.Mean<float>("float"); // re-run the event loop
   EXPECT_EQ(nevts, nCalls);
   EXPECT_EQ(*m0, *m1);
}

// Broken - caching a cached tdf destroys the cache of the cached.
TEST(Cache, CacheFromCache)
{
   auto nevts = 10U;
   ROOT::RDataFrame tdf(nevts);
   auto f = 0.f;
   auto orig = tdf.Define("float", [&f]() { return f++; });

   auto cached = orig.Cache<float>({"float"});
   f = 0.f;
   auto recached = cached.Cache<float>({"float"});
   auto ofloat = *orig.Take<float>("float");
   auto cfloat = *cached.Take<float>("float");
   auto rcfloat = *recached.Take<float>("float");

   for (auto j : ROOT::TSeqU(nevts)) {
      EXPECT_EQ(ofloat[j], cfloat[j]);
      EXPECT_EQ(ofloat[j], rcfloat[j]);
   }
}

TEST(Cache, InternalColumnsSnapshot)
{
   ROOT::RDataFrame tdf(2);
   auto f = 0.f;
   auto colName = "tdfMySecretcol_";
   auto orig = tdf.Define(colName, [&f]() { return f++; }).Define("dummy", []() { return 0.f; });
   auto cached = orig.Cache<float, float>({colName, "dummy"});
   auto snapshot = cached.Snapshot("t", "InternalColumnsSnapshot.root", "", {"RECREATE", ROOT::kZLIB, 0, 0, 99, false});
   int ret(1);
   try {
      testing::internal::CaptureStderr();
      snapshot->Mean<ULong64_t>(colName);
   } catch (const std::runtime_error &) {
      ret = 0;
   }
   EXPECT_EQ(0, ret) << "Internal column " << colName << " has been snapshotted!";
}

TEST(Cache, CollectionColumns)
{
   ROOT::RDataFrame tdf(3);
   int i = 0;
   auto d = tdf.Define("vector",
                       [&i]() {
                          std::vector<int> v(3);
                          v[0] = i;
                          v[1] = i + 1;
                          v[2] = i + 2;
                          return v;
                       })
               .Define("list",
                       [&i]() {
                          std::list<int> v;
                          for (auto j : {0, 1, 2})
                             v.emplace_back(j + i);
                          return v;
                       })
               .Define("deque",
                       [&i]() {
                          std::deque<int> v(3);
                          v[0] = i;
                          v[1] = i + 1;
                          v[2] = i + 2;
                          return v;
                       })
               .Define("blob", [&i]() { return ++i; });
   {
      auto c = d.Cache<std::vector<int>, std::list<int>, std::deque<int>>({"vector", "list", "deque"});
      auto hv = c.Histo1D<std::vector<int>>("vector");
      auto hl = c.Histo1D<std::list<int>>("list");
      auto hd = c.Histo1D<std::deque<int>>("deque");
      EXPECT_EQ(1, hv->GetMean());
      EXPECT_EQ(1, hl->GetMean());
      EXPECT_EQ(1, hd->GetMean());
   }

   // same but jitted
   auto c = d.Cache({"vector", "list", "deque"});
   auto hv = c.Histo1D<std::vector<int>>("vector");
   auto hl = c.Histo1D<std::list<int>>("list");
   auto hd = c.Histo1D<std::deque<int>>("deque");
   EXPECT_EQ(1, hv->GetMean());
   EXPECT_EQ(1, hl->GetMean());
   EXPECT_EQ(1, hd->GetMean());
}

TEST(Cache, evtCounter)
{

   const std::vector<ULong64_t> evenE_ref{0, 2};
   const std::vector<ULong64_t> allE_ref{0, 1};
   ROOT::RDataFrame tdf(4);

   auto test = [&](std::string_view entryColName){
      auto c0 = tdf.Alias("entry", entryColName)
                  .Filter([](ULong64_t e) { return 0 == e % 2; }, {"entry"})
                  .Cache<ULong64_t>({"entry"});
      auto evenE = c0.Take<ULong64_t>("entry");
      for (auto i : {0, 1}) {
         EXPECT_EQ(evenE->at(i), evenE_ref[i]);
      }
      auto allE = c0.Alias("entry2", entryColName).Take<ULong64_t>("entry2");
      for (auto i : {0, 1}) {
         EXPECT_EQ(allE->at(i), allE_ref[i]);
      }
   };

   test("tdfentry_");
   test("rdfentry_");

}

#ifdef R__B64

TEST(Cache, Regex)
{

   ROOT::RDataFrame tdf(1);
   auto d = tdf.Define("c0", []() { return 0; }).Define("c1", []() { return 1; }).Define("b0", []() { return 2; });

   auto cachedAll = d.Cache();
   auto cachedC = d.Cache("c[0,1].*");

   auto sumAll = [](int c0, int c1, int b0) { return c0 + c1 + b0; };
   auto mAll = cachedAll.Define("sum", sumAll, {"c0", "c1", "b0"}).Max<int>("sum");
   EXPECT_EQ(3, *mAll);
   auto sumC = [](int c0, int c1) { return c0 + c1; };
   auto mC = cachedC.Define("sum", sumC, {"c0", "c1"}).Max<int>("sum");
   EXPECT_EQ(1, *mC);

   // Now from source
   std::unique_ptr<RDataSource> tds(new RTrivialDS(4));
   ROOT::RDataFrame tdfs(std::move(tds));
   auto cached = tdfs.Cache();
   auto m = cached.Max<ULong64_t>("col0");
   EXPECT_EQ(3UL, *m);

   // second part
   ROOT::RDataFrame df(1);
   ROOT::RDF::RNode n(df);
   std::string base("col_");
   std::vector<std::string> defColNames; defColNames.reserve(128);
   auto addCol = [base, &defColNames](ROOT::RDF::RNode &n, int i) {
      auto colName = base+std::to_string(i);
      defColNames.emplace_back(colName);
      return ROOT::RDF::RNode(n.Define(colName, [](){return 0;}));
   };
   for (auto i : ROOT::TSeqI(128)) {
      n = addCol(n, i);
   }
   int cursor = 0;
   const auto df_even_cols = n.Cache(".*[02468]$").GetColumnNames();
   for (auto &&col : df_even_cols) {
      EXPECT_TRUE(col == defColNames[cursor]) << "Checking even columns. An error was encountered: expecting "
                                              << defColNames[cursor] << " but found " << col;
      cursor+=2;
   }

   cursor = 1;
   const auto df_odd_cols = n.Cache(".*[13579]$").GetColumnNames();
   for (auto &&col : df_odd_cols) {
      EXPECT_TRUE(col == defColNames[cursor]) << "Checking odd columns. An error was encountered: expecting "
                                              << defColNames[cursor] << " but found " << col;
      cursor+=2;
   }

   cursor = 1;
   const auto df_or_cols = n.Cache("(col_.*[2]$|col_.*[5]$)").GetColumnNames();
   for (auto &&col : df_or_cols) {
      cursor++;
      auto cursorAsString = std::to_string(cursor);
      auto last = cursorAsString.back();
      while (last != '2' && last != '5') {
        cursor++;
        cursorAsString = std::to_string(cursor);
        last = cursorAsString.back();
      }
      EXPECT_TRUE(col == defColNames[cursor]) << "Checking columns chosen with an or. An error was encountered!: expecting "
                                              << defColNames[cursor] << " but found " << col;
   }

}

TEST(Cache, Carrays)
{
   auto treeName = "t";
   auto fileName = "CacheCarrays.root";

   {
      TFile f(fileName, "RECREATE");
      TTree t(treeName, treeName);
      float arr[4];
      t.Branch("arr", arr, "arr[4]/F");
      for (auto i : ROOT::TSeqU(4)) {
         for (auto j : ROOT::TSeqU(4)) {
            arr[j] = i + j;
         }
         t.Fill();
      }
      t.Write();
   }

   ROOT::RDataFrame tdf(treeName, fileName);
   auto cache = tdf.Cache<RVec<float>>({"arr"});
   int i = 0;
   auto checkArr = [&i](RVec<float> av) {
      auto ifloat = float(i);
      EXPECT_EQ(ifloat, av[0]);
      EXPECT_EQ(ifloat + 1, av[1]);
      EXPECT_EQ(ifloat + 2, av[2]);
      EXPECT_EQ(ifloat + 3, av[3]);
      i++;
   };
   cache.Foreach(checkArr, {"arr"});

   // now jitted
   auto cachej = tdf.Cache("arr");
   i = 0;
   cache.Foreach(checkArr, {"arr"});

   gSystem->Unlink(fileName);
}

#endif // R__B64
<commit_msg>[DF][NFC] Remove compiler warning, better comment<commit_after>#include "ROOT/RDataFrame.hxx"
#include "ROOT/TSeq.hxx"
#include "ROOT/RTrivialDS.hxx"
#include "TH1F.h"
#include "TRandom.h"
#include "TSystem.h"

#include "gtest/gtest.h"

#include <algorithm>

using namespace ROOT::RDF;
using namespace ROOT::VecOps;

TEST(Cache, FundType)
{
   ROOT::RDataFrame tdf(5);
   int i = 1;
   auto cached =
      tdf.Define("c0", [&i]() { return i++; }).Define("c1", []() { return 1.; }).Cache<int, double>({"c0", "c1"});

   auto c = cached.Count();
   auto m = cached.Min<int>("c0");
   auto v = *cached.Take<int>("c0");

   EXPECT_EQ(1, *m);
   EXPECT_EQ(5UL, *c);
   for (auto j : ROOT::TSeqI(5)) {
      EXPECT_EQ(j + 1, v[j]);
   }
}

TEST(Cache, Ambiguity)
{
// This test verifies that the correct method is called and there is no ambiguity between the JIT call to Cache using
// a column list as a parameter and the JIT call to Cache using the Regexp.
ROOT::RDataFrame tdf(5);
int i = 1;
auto d = tdf.Define("c0", [&i]() { return i++; }).Define("c1", []() { return 1.; });

auto cached_1 = d.Cache({"c0"});
auto cached_2 = d.Cache({"c0", "c1"});

auto c_1 = cached_1.Count();
auto c_2 = cached_2.Count();

EXPECT_EQ(5UL, *c_1);
EXPECT_EQ(5UL, *c_2);
}

/*
// The contiguity has been dropped since now caching is backed up by a TDS
// TODO: we can optimise this. The reason why addresses are not contiguous with
// data sources (now cache is achieved with a data source) is linked to Snapshot.
// Indeed we need to set an address for each branch and keep that constant
// through the entire event loop (this is how the TTree works!).
// We could remove this constraint from data sources if Snapshot offered some
// sort of cache to which the data is copied.
TEST(Cache, Contiguity)
{
   ROOT::RDataFrame tdf(2);
   auto f = 0.f;
   auto cached = tdf.Define("float", [&f]() { return f++; }).Cache<float>({"float"});
   int counter = 0;
   float *fPrec = nullptr;
   auto count = [&counter, &fPrec](float &ff) {
      if (1 == counter++) {
         EXPECT_EQ(1, std::distance(fPrec, &ff));
      }
      fPrec = &ff;
   };
   cached.Foreach(count, {"float"});
}
*/

TEST(Cache, Class)
{
   TH1F h("", "h", 64, 0, 1);
   gRandom->SetSeed(1);
   h.FillRandom("gaus", 10);
   ROOT::RDataFrame tdf(1);
   auto cached = tdf.Define("c0", [&h]() { return h; }).Cache<TH1F>({"c0"});

   auto c = cached.Count();
   auto d = cached.Define("Mean", [](TH1F &hh) { return hh.GetMean(); }, {"c0"})
               .Define("StdDev", [](TH1F &hh) { return hh.GetStdDev(); }, {"c0"});
   auto m = d.Max<double>("Mean");
   auto s = d.Max<double>("StdDev");

   EXPECT_EQ(h.GetMean(), *m);
   EXPECT_EQ(h.GetStdDev(), *s);
   EXPECT_EQ(1UL, *c);
}

TEST(Cache, RunTwiceOnCached)
{
   auto nevts = 10U;
   ROOT::RDataFrame tdf(nevts);
   auto f = 0.f;
   auto nCalls = 0U;
   auto orig = tdf.Define("float", [&f, &nCalls]() {
      nCalls++;
      return f++;
   });

   auto cached = orig.Cache<float>({"float"});
   EXPECT_EQ(0U, nCalls);
   auto m0 = cached.Mean<float>("float");
   *m0;
   EXPECT_EQ(nevts, nCalls);
   cached.Foreach([]() {});               // run the event loop
   auto m1 = cached.Mean<float>("float"); // re-run the event loop
   EXPECT_EQ(nevts, nCalls);
   EXPECT_EQ(*m0, *m1);
}

// Broken - caching a cached tdf destroys the cache of the cached.
TEST(Cache, CacheFromCache)
{
   auto nevts = 10U;
   ROOT::RDataFrame tdf(nevts);
   auto f = 0.f;
   auto orig = tdf.Define("float", [&f]() { return f++; });

   auto cached = orig.Cache<float>({"float"});
   f = 0.f;
   auto recached = cached.Cache<float>({"float"});
   auto ofloat = *orig.Take<float>("float");
   auto cfloat = *cached.Take<float>("float");
   auto rcfloat = *recached.Take<float>("float");

   for (auto j : ROOT::TSeqU(nevts)) {
      EXPECT_EQ(ofloat[j], cfloat[j]);
      EXPECT_EQ(ofloat[j], rcfloat[j]);
   }
}

TEST(Cache, InternalColumnsSnapshot)
{
   ROOT::RDataFrame tdf(2);
   auto f = 0.f;
   auto colName = "tdfMySecretcol_";
   auto orig = tdf.Define(colName, [&f]() { return f++; }).Define("dummy", []() { return 0.f; });
   auto cached = orig.Cache<float, float>({colName, "dummy"});
   auto snapshot = cached.Snapshot("t", "InternalColumnsSnapshot.root", "", {"RECREATE", ROOT::kZLIB, 0, 0, 99, false});
   int ret(1);
   try {
      testing::internal::CaptureStderr();
      snapshot->Mean<ULong64_t>(colName);
   } catch (const std::runtime_error &) {
      ret = 0;
   }
   EXPECT_EQ(0, ret) << "Internal column " << colName << " has been snapshotted!";
}

TEST(Cache, CollectionColumns)
{
   ROOT::RDataFrame tdf(3);
   int i = 0;
   auto d = tdf.Define("vector",
                       [&i]() {
                          std::vector<int> v(3);
                          v[0] = i;
                          v[1] = i + 1;
                          v[2] = i + 2;
                          return v;
                       })
               .Define("list",
                       [&i]() {
                          std::list<int> v;
                          for (auto j : {0, 1, 2})
                             v.emplace_back(j + i);
                          return v;
                       })
               .Define("deque",
                       [&i]() {
                          std::deque<int> v(3);
                          v[0] = i;
                          v[1] = i + 1;
                          v[2] = i + 2;
                          return v;
                       })
               .Define("blob", [&i]() { return ++i; });
   {
      auto c = d.Cache<std::vector<int>, std::list<int>, std::deque<int>>({"vector", "list", "deque"});
      auto hv = c.Histo1D<std::vector<int>>("vector");
      auto hl = c.Histo1D<std::list<int>>("list");
      auto hd = c.Histo1D<std::deque<int>>("deque");
      EXPECT_EQ(1, hv->GetMean());
      EXPECT_EQ(1, hl->GetMean());
      EXPECT_EQ(1, hd->GetMean());
   }

   // same but jitted
   auto c = d.Cache({"vector", "list", "deque"});
   auto hv = c.Histo1D<std::vector<int>>("vector");
   auto hl = c.Histo1D<std::list<int>>("list");
   auto hd = c.Histo1D<std::deque<int>>("deque");
   EXPECT_EQ(1, hv->GetMean());
   EXPECT_EQ(1, hl->GetMean());
   EXPECT_EQ(1, hd->GetMean());
}

TEST(Cache, evtCounter)
{

   const std::vector<ULong64_t> evenE_ref{0, 2};
   const std::vector<ULong64_t> allE_ref{0, 1};
   ROOT::RDataFrame tdf(4);

   auto test = [&](std::string_view entryColName){
      auto c0 = tdf.Alias("entry", entryColName)
                  .Filter([](ULong64_t e) { return 0 == e % 2; }, {"entry"})
                  .Cache<ULong64_t>({"entry"});
      auto evenE = c0.Take<ULong64_t>("entry");
      for (auto i : {0, 1}) {
         EXPECT_EQ(evenE->at(i), evenE_ref[i]);
      }
      auto allE = c0.Alias("entry2", entryColName).Take<ULong64_t>("entry2");
      for (auto i : {0, 1}) {
         EXPECT_EQ(allE->at(i), allE_ref[i]);
      }
   };

   test("tdfentry_");
   test("rdfentry_");

}

#ifdef R__B64

TEST(Cache, Regex)
{

   ROOT::RDataFrame tdf(1);
   auto d = tdf.Define("c0", []() { return 0; }).Define("c1", []() { return 1; }).Define("b0", []() { return 2; });

   auto cachedAll = d.Cache();
   auto cachedC = d.Cache("c[0,1].*");

   auto sumAll = [](int c0, int c1, int b0) { return c0 + c1 + b0; };
   auto mAll = cachedAll.Define("sum", sumAll, {"c0", "c1", "b0"}).Max<int>("sum");
   EXPECT_EQ(3, *mAll);
   auto sumC = [](int c0, int c1) { return c0 + c1; };
   auto mC = cachedC.Define("sum", sumC, {"c0", "c1"}).Max<int>("sum");
   EXPECT_EQ(1, *mC);

   // Now from source
   std::unique_ptr<RDataSource> tds(new RTrivialDS(4));
   ROOT::RDataFrame tdfs(std::move(tds));
   auto cached = tdfs.Cache();
   auto m = cached.Max<ULong64_t>("col0");
   EXPECT_EQ(3UL, *m);

   // Now stress a bit more the regexps
   ROOT::RDataFrame df(1);
   ROOT::RDF::RNode n(df);
   std::string base("col_");
   std::vector<std::string> defColNames; defColNames.reserve(128);
   auto addCol = [base, &defColNames](ROOT::RDF::RNode &node, int i) {
      auto colName = base+std::to_string(i);
      defColNames.emplace_back(colName);
      return ROOT::RDF::RNode(node.Define(colName, [](){return 0;}));
   };
   for (auto i : ROOT::TSeqI(128)) {
      n = addCol(n, i);
   }
   int cursor = 0;
   const auto df_even_cols = n.Cache(".*[02468]$").GetColumnNames();
   for (auto &&col : df_even_cols) {
      EXPECT_TRUE(col == defColNames[cursor]) << "Checking even columns. An error was encountered: expecting "
                                              << defColNames[cursor] << " but found " << col;
      cursor+=2;
   }

   cursor = 1;
   const auto df_odd_cols = n.Cache(".*[13579]$").GetColumnNames();
   for (auto &&col : df_odd_cols) {
      EXPECT_TRUE(col == defColNames[cursor]) << "Checking odd columns. An error was encountered: expecting "
                                              << defColNames[cursor] << " but found " << col;
      cursor+=2;
   }

   cursor = 1;
   const auto df_or_cols = n.Cache("(col_.*[2]$|col_.*[5]$)").GetColumnNames();
   for (auto &&col : df_or_cols) {
      cursor++;
      auto cursorAsString = std::to_string(cursor);
      auto last = cursorAsString.back();
      while (last != '2' && last != '5') {
        cursor++;
        cursorAsString = std::to_string(cursor);
        last = cursorAsString.back();
      }
      EXPECT_TRUE(col == defColNames[cursor]) << "Checking columns chosen with an or. An error was encountered!: expecting "
                                              << defColNames[cursor] << " but found " << col;
   }

}

TEST(Cache, Carrays)
{
   auto treeName = "t";
   auto fileName = "CacheCarrays.root";

   {
      TFile f(fileName, "RECREATE");
      TTree t(treeName, treeName);
      float arr[4];
      t.Branch("arr", arr, "arr[4]/F");
      for (auto i : ROOT::TSeqU(4)) {
         for (auto j : ROOT::TSeqU(4)) {
            arr[j] = i + j;
         }
         t.Fill();
      }
      t.Write();
   }

   ROOT::RDataFrame tdf(treeName, fileName);
   auto cache = tdf.Cache<RVec<float>>({"arr"});
   int i = 0;
   auto checkArr = [&i](RVec<float> av) {
      auto ifloat = float(i);
      EXPECT_EQ(ifloat, av[0]);
      EXPECT_EQ(ifloat + 1, av[1]);
      EXPECT_EQ(ifloat + 2, av[2]);
      EXPECT_EQ(ifloat + 3, av[3]);
      i++;
   };
   cache.Foreach(checkArr, {"arr"});

   // now jitted
   auto cachej = tdf.Cache("arr");
   i = 0;
   cache.Foreach(checkArr, {"arr"});

   gSystem->Unlink(fileName);
}

#endif // R__B64
<|endoftext|>