<commit_before>#include "Heatmap.h"
#include "Config.h"
#include "Hash.h"

#include <spine/Exception.h>
#include <stdexcept>

namespace
{
float linearKernel(float d)
{
  return d;
}

float sqrtKernel(float d)
{
  return sqrtf(d);
}

float expKernel(float d, double deviation)
{
  return static_cast<float>(exp(-0.5 * sqrtf(d / deviation)));
}
}  // namespace

namespace SmartMet
{
namespace Plugin
{
namespace Dali
{
// ----------------------------------------------------------------------
/*!
 * \brief Initialize from JSON
 */
// ----------------------------------------------------------------------

void Heatmap::init(const Json::Value& theJson, const Config& theConfig)
{
  try
  {
    if (!theJson.isObject())
      throw Spine::Exception(BCP, "Heatmap JSON is not map");

    // Iterate through all the members.

    const auto members = theJson.getMemberNames();
    for (const auto& name : members)
    {
      const Json::Value& json = theJson[name];

      if (name == "resolution")
        resolution = json.asDouble();
      else if (name == "radius")
        radius = json.asDouble();
      else if (name == "kernel")
        kernel = json.asString();
      else if (name == "deviation")
        deviation = json.asDouble();
      else
        throw Spine::Exception(BCP, "Heatmap does not have a setting named '" + name + "'");
    }

    if (!resolution)
      throw Spine::Exception(BCP, "Heatmap resolution is not set");
    if (!radius)
      throw Spine::Exception(BCP, "Heatmap radius is not set");

    if (!kernel)
      kernel = "exp";
    if (!deviation)
      deviation = 10.0;

    max_points = std::min(theConfig.maxHeatmapPoints(), max_max_points);
  }
  catch (...)
  {
    throw Spine::Exception::Trace(BCP, "Operation failed!");
  }
}

// ----------------------------------------------------------------------
/*!
 * \brief Get stamp using the selected kernel and radius
 */
// ----------------------------------------------------------------------

std::unique_ptr<heatmap_stamp_t, void (*)(heatmap_stamp_t*)> Heatmap::getStamp(unsigned r)
{
  try
  {
    heatmap_stamp_t* hms;

    if (*kernel == "linear")
      hms = heatmap_stamp_gen_nonlinear(r, &linearKernel);
    else if (*kernel == "sqrt")
      hms = heatmap_stamp_gen_nonlinear(r, &sqrtKernel);
    else if (*kernel == "exp")
    {
      // Code below to is copied from heatmap_stamp_gen_nonlinear() (heatmap.c).
      //
      // The kernel call is different, passing second argument (deviation) too.
      // heatmap_stamp_load makes a copy of the stamp buffer; it must be deleted at return.

      unsigned y;
      unsigned d = 2 * r + 1;

      auto* stamp = static_cast<float*>(calloc(d * d, sizeof(float)));

      if (!stamp)
        throw Spine::Exception(BCP, "Could not allocate memory for heatmap stamp");
      std::unique_ptr<float> up_stamp(stamp);

      for (y = 0; y < d; ++y)
      {
        float* line = stamp + y * d;
        unsigned x;

        for (x = 0; x < d; ++x, ++line)<commit_msg>Avoid implicit conversion of void * to boolean<commit_after>#include "Heatmap.h"
#include "Config.h"
#include "Hash.h"

#include <spine/Exception.h>
#include <stdexcept>

namespace
{
float linearKernel(float d)
{
  return d;
}

float sqrtKernel(float d)
{
  return sqrtf(d);
}

float expKernel(float d, double deviation)
{
  return static_cast<float>(exp(-0.5 * sqrtf(d / deviation)));
}
}  // namespace

namespace SmartMet
{
namespace Plugin
{
namespace Dali
{
// ----------------------------------------------------------------------
/*!
 * \brief Initialize from JSON
 */
// ----------------------------------------------------------------------

void Heatmap::init(const Json::Value& theJson, const Config& theConfig)
{
  try
  {
    if (!theJson.isObject())
      throw Spine::Exception(BCP, "Heatmap JSON is not map");

    // Iterate through all the members.

    const auto members = theJson.getMemberNames();
    for (const auto& name : members)
    {
      const Json::Value& json = theJson[name];

      if (name == "resolution")
        resolution = json.asDouble();
      else if (name == "radius")
        radius = json.asDouble();
      else if (name == "kernel")
        kernel = json.asString();
      else if (name == "deviation")
        deviation = json.asDouble();
      else
        throw Spine::Exception(BCP, "Heatmap does not have a setting named '" + name + "'");
    }

    if (!resolution)
      throw Spine::Exception(BCP, "Heatmap resolution is not set");
    if (!radius)
      throw Spine::Exception(BCP, "Heatmap radius is not set");

    if (!kernel)
      kernel = "exp";
    if (!deviation)
      deviation = 10.0;

    max_points = std::min(theConfig.maxHeatmapPoints(), max_max_points);
  }
  catch (...)
  {
    throw Spine::Exception::Trace(BCP, "Operation failed!");
  }
}

// ----------------------------------------------------------------------
/*!
 * \brief Get stamp using the selected kernel and radius
 */
// ----------------------------------------------------------------------

std::unique_ptr<heatmap_stamp_t, void (*)(heatmap_stamp_t*)> Heatmap::getStamp(unsigned r)
{
  try
  {
    heatmap_stamp_t* hms;

    if (*kernel == "linear")
      hms = heatmap_stamp_gen_nonlinear(r, &linearKernel);
    else if (*kernel == "sqrt")
      hms = heatmap_stamp_gen_nonlinear(r, &sqrtKernel);
    else if (*kernel == "exp")
    {
      // Code below to is copied from heatmap_stamp_gen_nonlinear() (heatmap.c).
      //
      // The kernel call is different, passing second argument (deviation) too.
      // heatmap_stamp_load makes a copy of the stamp buffer; it must be deleted at return.

      unsigned y;
      unsigned d = 2 * r + 1;

      auto* stamp = static_cast<float*>(calloc(d * d, sizeof(float)));

      if (stamp == nullptr)
        throw Spine::Exception(BCP, "Could not allocate memory for heatmap stamp");
      std::unique_ptr<float> up_stamp(stamp);

      for (y = 0; y < d; ++y)
      {
        float* line = stamp + y * d;
        unsigned x;

        for (x = 0; x < d; ++x, ++line)
{'fraction_non_alphanumeric': 0.2204984320845024, 'fraction_numerical': 0.0029707872586235353, 'mean_word_length': 2.026973026973027, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5151005', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1801, 'n_words': 516}